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