2 // SecurityContextTest.cs - NUnit tests for SecurityContext
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.Principal;
34 using System.Threading;
36 using NUnit.Framework;
38 namespace MonoTests.System.Security {
41 public class SecurityContextTest {
45 static void Callback (object o)
57 public void TearDown ()
59 if (SecurityContext.IsFlowSuppressed () || SecurityContext.IsWindowsIdentityFlowSuppressed ())
60 SecurityContext.RestoreFlow ();
64 public void Capture ()
66 SecurityContext sc = SecurityContext.Capture ();
67 Assert.IsNotNull (sc, "Capture");
69 AsyncFlowControl afc = SecurityContext.SuppressFlow ();
70 Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
72 sc = SecurityContext.Capture ();
73 Assert.IsNull (sc, "Capture with SuppressFlow");
79 afc = SecurityContext.SuppressFlowWindowsIdentity ();
80 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
82 sc = SecurityContext.Capture ();
83 Assert.IsNotNull (sc, "Capture with SuppressFlowWindowsIdentity");
93 SecurityContext sc = SecurityContext.Capture ();
94 Assert.IsNotNull (sc, "Capture");
96 SecurityContext copy = sc.CreateCopy ();
97 Assert.IsNotNull (copy, "Copy of Capture");
99 Assert.IsFalse (sc.Equals (copy));
100 Assert.IsFalse (copy.Equals (sc));
101 Assert.IsFalse (Object.ReferenceEquals (sc, copy));
103 SecurityContext copy2nd = copy.CreateCopy ();
104 Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
108 public void IsFlowSuppressed ()
110 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
112 AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
113 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
116 afc = SecurityContext.SuppressFlow ();
117 Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
120 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
124 public void IsWindowsIdentityFlowSuppressed ()
126 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
128 AsyncFlowControl afc = SecurityContext.SuppressFlow ();
129 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
132 afc = SecurityContext.SuppressFlowWindowsIdentity ();
133 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-3");
136 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
140 [ExpectedException (typeof (InvalidOperationException))]
141 public void RestoreFlow_None ()
143 SecurityContext.RestoreFlow ();
147 public void RestoreFlow_SuppressFlow ()
149 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
150 SecurityContext.SuppressFlow ();
151 Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
152 SecurityContext.RestoreFlow ();
153 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
157 public void RestoreFlow_SuppressFlowWindowsIdentity ()
159 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
160 SecurityContext.SuppressFlowWindowsIdentity ();
161 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
162 SecurityContext.RestoreFlow ();
163 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-3");
169 Assert.IsFalse (success, "pre-check");
170 SecurityContext.Run (SecurityContext.Capture (), new ContextCallback (Callback), true);
171 Assert.IsTrue (success, "post-check");
175 [ExpectedException (typeof (InvalidOperationException))]
176 public void Run_SuppressFlow ()
178 Assert.IsFalse (SecurityContext.IsFlowSuppressed ());
179 AsyncFlowControl afc = SecurityContext.SuppressFlow ();
180 Assert.IsTrue (SecurityContext.IsFlowSuppressed ());
182 SecurityContext.Run (SecurityContext.Capture (), new ContextCallback (Callback), "Hello world.");
190 public void SuppressFlow ()
192 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
193 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
195 AsyncFlowControl afc = SecurityContext.SuppressFlow ();
196 Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
197 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-3");
200 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
201 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
205 public void SuppressFlowWindowsIdentity ()
207 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
208 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
210 AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
211 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
212 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
215 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
216 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
220 public void SuppressFlow_Both ()
222 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
223 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
225 AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
226 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
227 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
229 AsyncFlowControl afc2 = SecurityContext.SuppressFlow ();
230 Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
231 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
234 // note: afc2 Undo return to the original (not the previous) state
235 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
236 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
240 [ExpectedException (typeof (InvalidOperationException))]
241 public void SuppressFlow_Both_Undo ()
243 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
244 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
246 AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
247 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
248 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
250 AsyncFlowControl afc2 = SecurityContext.SuppressFlow ();
251 Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
252 Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
255 // note: afc2 Undo return to the original (not the previous) state
256 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
257 Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
259 // we can't use the first AsyncFlowControl
263 // effects of ExecutionContext on SecurityContext
266 public void ExecutionContext_SuppressFlow ()
268 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
269 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
271 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
272 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
273 Assert.IsNotNull (SecurityContext.Capture (), "Capture");
281 public void WindowsIdentity_ ()
283 Thread.CurrentPrincipal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
284 SecurityContext sc = SecurityContext.Capture ();
285 Assert.IsNotNull (sc, "Capture");