ThreadCas.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. [Test]
  62. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  63. [ExpectedException (typeof (SecurityException))]
  64. public void Abort ()
  65. {
  66. Thread.CurrentThread.Abort ();
  67. }
  68. [Test]
  69. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  70. [ExpectedException (typeof (SecurityException))]
  71. public void Abort_Object ()
  72. {
  73. Thread.CurrentThread.Abort (new object [0]);
  74. }
  75. [Test]
  76. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  77. [ExpectedException (typeof (SecurityException))]
  78. public void CurrentCulture ()
  79. {
  80. Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  81. }
  82. [Test]
  83. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  84. [ExpectedException (typeof (SecurityException))]
  85. public void Interrupt ()
  86. {
  87. Thread.CurrentThread.Interrupt ();
  88. }
  89. [Test]
  90. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  91. [ExpectedException (typeof (SecurityException))]
  92. public void ResetAbort ()
  93. {
  94. Thread.ResetAbort ();
  95. }
  96. [Test]
  97. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  98. [ExpectedException (typeof (SecurityException))]
  99. public void Resume ()
  100. {
  101. Thread.CurrentThread.Resume ();
  102. }
  103. [Test]
  104. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  105. [ExpectedException (typeof (SecurityException))]
  106. public void Suspend ()
  107. {
  108. Thread.CurrentThread.Suspend ();
  109. }
  110. // we use reflection to call Mutex as it's named constructors are protected by
  111. // a LinkDemand (which will be converted into full demand, i.e. a stack walk)
  112. // when reflection is used (i.e. it gets testable).
  113. [Test]
  114. [SecurityPermission (SecurityAction.Deny, Infrastructure = true)]
  115. [ExpectedException (typeof (SecurityException))]
  116. public void CurrentContext ()
  117. {
  118. MethodInfo mi = ThreadType.GetProperty ("CurrentContext").GetGetMethod ();
  119. Assert.IsNotNull (mi, "get_CurrentContext");
  120. mi.Invoke (null, null);
  121. }
  122. }
  123. }