2
0

ExecutionContextCas.cs 4.6 KB

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