ExecutionContextCas.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // ExecutionContextCas.cs - CAS Unit Tests for ExecutionContext
  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. #if NET_2_0
  29. using System;
  30. using System.Security;
  31. using System.Security.Permissions;
  32. using System.Threading;
  33. using NUnit.Framework;
  34. namespace MonoCasTests.System.Threading {
  35. [TestFixture]
  36. [Category ("CAS")]
  37. public class ExecutionContextCas {
  38. static bool success;
  39. static void Callback (object o)
  40. {
  41. new SecurityPermission (SecurityPermissionFlag.UnmanagedCode).Demand ();
  42. success = (bool)o;
  43. }
  44. [SetUp]
  45. public void SetUp ()
  46. {
  47. if (!SecurityManager.SecurityEnabled)
  48. Assert.Ignore ("SecurityManager isn't enabled");
  49. success = false;
  50. }
  51. [TearDown]
  52. public void TearDown ()
  53. {
  54. if (ExecutionContext.IsFlowSuppressed ())
  55. ExecutionContext.RestoreFlow ();
  56. }
  57. private void Thread_Run_Empty ()
  58. {
  59. Assert.IsFalse (success, "pre-check");
  60. ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
  61. Assert.IsTrue (success, "post-check");
  62. }
  63. [Test]
  64. public void Run_Empty ()
  65. {
  66. Thread t = new Thread (new ThreadStart (Thread_Run_Empty));
  67. t.Start ();
  68. t.Join ();
  69. }
  70. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  71. private ExecutionContext GetExecutionContextUnmanaged ()
  72. {
  73. return ExecutionContext.Capture ();
  74. // the Deny disappears with this stack frame but we got a capture of it
  75. }
  76. private void Thread_Run_UnmanagedCode ()
  77. {
  78. bool result = false;
  79. Assert.IsFalse (success, "pre-check");
  80. try {
  81. ExecutionContext ec = GetExecutionContextUnmanaged ();
  82. // run with the captured security stack (i.e. deny unmanaged)
  83. ExecutionContext.Run (ec, new ContextCallback (Callback), true);
  84. }
  85. catch (SecurityException) {
  86. result = true;
  87. }
  88. finally {
  89. Assert.IsFalse (success, "post-check");
  90. Assert.IsTrue (result, "Result");
  91. }
  92. }
  93. [Test]
  94. public void Run_UnmanagedCode ()
  95. {
  96. Thread t = new Thread (new ThreadStart (Thread_Run_UnmanagedCode));
  97. t.Start ();
  98. t.Join ();
  99. }
  100. private void Thread_Run_UnmanagedCode_SuppressFlow_BeforeCapture ()
  101. {
  102. bool result = false;
  103. Assert.IsFalse (success, "pre-check");
  104. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  105. try {
  106. ExecutionContext ec = GetExecutionContextUnmanaged ();
  107. ExecutionContext.Run (ec, new ContextCallback (Callback), true);
  108. }
  109. catch (InvalidOperationException) {
  110. result = true;
  111. }
  112. finally {
  113. Assert.IsFalse (success, "post-check");
  114. afc.Undo ();
  115. Assert.IsTrue (result, "Result");
  116. }
  117. }
  118. [Test]
  119. public void Run_UnmanagedCode_SuppressFlow_BeforeCapture ()
  120. {
  121. Thread t = new Thread (new ThreadStart (Thread_Run_UnmanagedCode_SuppressFlow_BeforeCapture));
  122. t.Start ();
  123. t.Join ();
  124. }
  125. private void Thread_Run_UnmanagedCode_SuppressFlow_AfterCapture ()
  126. {
  127. bool result = false;
  128. Assert.IsFalse (success, "pre-check");
  129. ExecutionContext ec = GetExecutionContextUnmanaged ();
  130. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  131. try {
  132. ExecutionContext.Run (ec, new ContextCallback (Callback), true);
  133. }
  134. catch (SecurityException) {
  135. result = true;
  136. }
  137. finally {
  138. Assert.IsFalse (success, "post-check");
  139. afc.Undo ();
  140. Assert.IsTrue (result, "Result");
  141. }
  142. }
  143. [Test]
  144. public void Run_UnmanagedCode_SuppressFlow_AfterCapture ()
  145. {
  146. Thread t = new Thread (new ThreadStart (Thread_Run_UnmanagedCode_SuppressFlow_AfterCapture));
  147. t.Start ();
  148. t.Join ();
  149. }
  150. }
  151. }
  152. #endif