ThreadCas.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // ThreadCas.cs - CAS unit tests for System.Threading.Thread
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.Globalization;
  31. using System.Reflection;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. using System.Threading;
  35. namespace MonoCasTests.System.Threading {
  36. [TestFixture]
  37. [Category ("CAS")]
  38. public class ThreadCas {
  39. private Type ThreadType;
  40. [TestFixtureSetUp]
  41. public void FixtureSetUp ()
  42. {
  43. ThreadType = typeof (Thread);
  44. }
  45. [SetUp]
  46. public void SetUp ()
  47. {
  48. if (!SecurityManager.SecurityEnabled)
  49. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  50. }
  51. // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
  52. [Test]
  53. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  54. public void PartialTrust_DenyUnrestricted_Success ()
  55. {
  56. MonoTests.System.Threading.ThreadTest tt = new MonoTests.System.Threading.ThreadTest ();
  57. tt.TestCtor1 ();
  58. // most tests use Abort so there's not much to call
  59. }
  60. // test Demand by denying the caller of the required privileges
  61. #if MONO_FEATURE_THREAD_ABORT
  62. [Test]
  63. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  64. [ExpectedException (typeof (SecurityException))]
  65. public void Abort ()
  66. {
  67. Thread.CurrentThread.Abort ();
  68. }
  69. [Test]
  70. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  71. [ExpectedException (typeof (SecurityException))]
  72. public void Abort_Object ()
  73. {
  74. Thread.CurrentThread.Abort (new object [0]);
  75. }
  76. #endif
  77. [Test]
  78. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  79. [ExpectedException (typeof (SecurityException))]
  80. public void CurrentCulture ()
  81. {
  82. Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  83. }
  84. [Test]
  85. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  86. [ExpectedException (typeof (SecurityException))]
  87. public void Interrupt ()
  88. {
  89. Thread.CurrentThread.Interrupt ();
  90. }
  91. #if MONO_FEATURE_THREAD_ABORT
  92. [Test]
  93. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  94. [ExpectedException (typeof (SecurityException))]
  95. public void ResetAbort ()
  96. {
  97. Thread.ResetAbort ();
  98. }
  99. #endif
  100. #if MONO_FEATURE_THREAD_SUSPEND_RESUME
  101. [Test]
  102. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  103. [ExpectedException (typeof (SecurityException))]
  104. public void Resume ()
  105. {
  106. Thread.CurrentThread.Resume ();
  107. }
  108. [Test]
  109. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  110. [ExpectedException (typeof (SecurityException))]
  111. public void Suspend ()
  112. {
  113. Thread.CurrentThread.Suspend ();
  114. }
  115. #endif
  116. // we use reflection to call Mutex as it's named constructors are protected by
  117. // a LinkDemand (which will be converted into full demand, i.e. a stack walk)
  118. // when reflection is used (i.e. it gets testable).
  119. [Test]
  120. [SecurityPermission (SecurityAction.Deny, Infrastructure = true)]
  121. [ExpectedException (typeof (SecurityException))]
  122. public void CurrentContext ()
  123. {
  124. MethodInfo mi = ThreadType.GetProperty ("CurrentContext").GetGetMethod ();
  125. Assert.IsNotNull (mi, "get_CurrentContext");
  126. mi.Invoke (null, null);
  127. }
  128. }
  129. }