2 // ExecutionContextCas.cs - CAS Unit Tests for ExecutionContext
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Security;
33 using System.Security.Permissions;
34 using System.Threading;
36 using NUnit.Framework;
38 namespace MonoCasTests.System.Threading {
42 public class ExecutionContextCas {
46 static void Callback (object o)
48 new SecurityPermission (SecurityPermissionFlag.UnmanagedCode).Demand ();
55 if (!SecurityManager.SecurityEnabled)
56 Assert.Ignore ("SecurityManager isn't enabled");
62 public void TearDown ()
64 if (ExecutionContext.IsFlowSuppressed ())
65 ExecutionContext.RestoreFlow ();
68 private void Thread_Run_Empty ()
70 Assert.IsFalse (success, "pre-check");
71 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
72 Assert.IsTrue (success, "post-check");
76 public void Run_Empty ()
78 Thread t = new Thread (new ThreadStart (Thread_Run_Empty));
83 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
84 private ExecutionContext GetExecutionContextUnmanaged ()
86 return ExecutionContext.Capture ();
87 // the Deny disappears with this stack frame but we got a capture of it
90 private void Thread_Run_UnmanagedCode ()
93 Assert.IsFalse (success, "pre-check");
95 ExecutionContext ec = GetExecutionContextUnmanaged ();
96 // run with the captured security stack (i.e. deny unmanaged)
97 ExecutionContext.Run (ec, new ContextCallback (Callback), true);
99 catch (SecurityException) {
103 Assert.IsFalse (success, "post-check");
104 Assert.IsTrue (result, "Result");
109 public void Run_UnmanagedCode ()
111 Thread t = new Thread (new ThreadStart (Thread_Run_UnmanagedCode));
116 private void Thread_Run_UnmanagedCode_SuppressFlow_BeforeCapture ()
119 Assert.IsFalse (success, "pre-check");
120 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
122 ExecutionContext ec = GetExecutionContextUnmanaged ();
123 ExecutionContext.Run (ec, new ContextCallback (Callback), true);
125 catch (InvalidOperationException) {
129 Assert.IsFalse (success, "post-check");
131 Assert.IsTrue (result, "Result");
136 public void Run_UnmanagedCode_SuppressFlow_BeforeCapture ()
138 Thread t = new Thread (new ThreadStart (Thread_Run_UnmanagedCode_SuppressFlow_BeforeCapture));
143 private void Thread_Run_UnmanagedCode_SuppressFlow_AfterCapture ()
146 Assert.IsFalse (success, "pre-check");
147 ExecutionContext ec = GetExecutionContextUnmanaged ();
148 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
150 ExecutionContext.Run (ec, new ContextCallback (Callback), true);
152 catch (SecurityException) {
156 Assert.IsFalse (success, "post-check");
158 Assert.IsTrue (result, "Result");
163 public void Run_UnmanagedCode_SuppressFlow_AfterCapture ()
165 Thread t = new Thread (new ThreadStart (Thread_Run_UnmanagedCode_SuppressFlow_AfterCapture));