New test.
[mono.git] / mcs / class / corlib / Test / System.Security / SecurityContextTest.cs
1 //
2 // SecurityContextTest.cs - NUnit tests for SecurityContext
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.Principal;
34 using System.Threading;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Security {
39
40         [TestFixture]
41         public class SecurityContextTest {
42
43                 static bool success;
44
45                 static void Callback (object o)
46                 {
47                         success = (bool) o;
48                 }
49
50                 [SetUp]
51                 public void SetUp ()
52                 {
53                         success = false;
54                 }
55
56                 [TearDown]
57                 public void TearDown ()
58                 {
59                         if (SecurityContext.IsFlowSuppressed () || SecurityContext.IsWindowsIdentityFlowSuppressed ())
60                                 SecurityContext.RestoreFlow ();
61                 }
62
63                 [Test]
64                 public void Capture ()
65                 {
66                         SecurityContext sc = SecurityContext.Capture ();
67                         Assert.IsNotNull (sc, "Capture");
68
69                         AsyncFlowControl afc = SecurityContext.SuppressFlow ();
70                         Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
71                         try {
72                                 sc = SecurityContext.Capture ();
73                                 Assert.IsNull (sc, "Capture with SuppressFlow");
74                         }
75                         finally {
76                                 afc.Undo ();
77                         }
78
79                         afc = SecurityContext.SuppressFlowWindowsIdentity ();
80                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
81                         try {
82                                 sc = SecurityContext.Capture ();
83                                 Assert.IsNotNull (sc, "Capture with SuppressFlowWindowsIdentity");
84                         }
85                         finally {
86                                 afc.Undo ();
87                         }
88                 }
89
90                 [Test]
91                 public void Copy ()
92                 {
93                         SecurityContext sc = SecurityContext.Capture ();
94                         Assert.IsNotNull (sc, "Capture");
95
96                         SecurityContext copy = sc.CreateCopy ();
97                         Assert.IsNotNull (copy, "Copy of Capture");
98
99                         Assert.IsFalse (sc.Equals (copy));
100                         Assert.IsFalse (copy.Equals (sc));
101                         Assert.IsFalse (Object.ReferenceEquals (sc, copy));
102
103                         SecurityContext copy2nd = copy.CreateCopy ();
104                         Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
105                 }
106
107                 [Test]
108                 public void IsFlowSuppressed ()
109                 {
110                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
111
112                         AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
113                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
114                         afc.Undo ();
115
116                         afc = SecurityContext.SuppressFlow ();
117                         Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
118                         afc.Undo ();
119
120                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
121                 }
122
123                 [Test]
124                 public void IsWindowsIdentityFlowSuppressed ()
125                 {
126                         Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
127
128                         AsyncFlowControl afc = SecurityContext.SuppressFlow ();
129                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
130                         afc.Undo ();
131
132                         afc = SecurityContext.SuppressFlowWindowsIdentity ();
133                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-3");
134                         afc.Undo ();
135
136                         Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (InvalidOperationException))]
141                 public void RestoreFlow_None ()
142                 {
143                         SecurityContext.RestoreFlow ();
144                 }
145
146                 [Test]
147                 public void RestoreFlow_SuppressFlow ()
148                 {
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");
154                 }
155
156                 [Test]
157                 public void RestoreFlow_SuppressFlowWindowsIdentity ()
158                 {
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");
164                 }
165
166                 [Test]
167                 public void Run ()
168                 {
169                         Assert.IsFalse (success, "pre-check");
170                         SecurityContext.Run (SecurityContext.Capture (), new ContextCallback (Callback), true);
171                         Assert.IsTrue (success, "post-check");
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (InvalidOperationException))]
176                 public void Run_SuppressFlow ()
177                 {
178                         Assert.IsFalse (SecurityContext.IsFlowSuppressed ());
179                         AsyncFlowControl afc = SecurityContext.SuppressFlow ();
180                         Assert.IsTrue (SecurityContext.IsFlowSuppressed ());
181                         try {
182                                 SecurityContext.Run (SecurityContext.Capture (), new ContextCallback (Callback), "Hello world.");
183                         }
184                         finally {
185                                 afc.Undo ();
186                         }
187                 }
188
189                 [Test]
190                 public void SuppressFlow ()
191                 {
192                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
193                         Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
194
195                         AsyncFlowControl afc = SecurityContext.SuppressFlow ();
196                         Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
197                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-3");
198                         afc.Undo ();
199
200                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
201                         Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
202                 }
203
204                 [Test]
205                 public void SuppressFlowWindowsIdentity ()
206                 {
207                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
208                         Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
209
210                         AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
211                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
212                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
213                         afc.Undo ();
214
215                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
216                         Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-4");
217                 }
218
219                 [Test]
220                 public void SuppressFlow_Both ()
221                 {
222                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
223                         Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
224
225                         AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
226                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
227                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
228
229                         AsyncFlowControl afc2 = SecurityContext.SuppressFlow ();
230                         Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
231                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
232                         afc2.Undo ();
233
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");
237                 }
238
239                 [Test]
240                 [ExpectedException (typeof (InvalidOperationException))]
241                 public void SuppressFlow_Both_Undo ()
242                 {
243                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
244                         Assert.IsFalse (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-1");
245
246                         AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity ();
247                         Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
248                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
249
250                         AsyncFlowControl afc2 = SecurityContext.SuppressFlow ();
251                         Assert.IsTrue (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
252                         Assert.IsTrue (SecurityContext.IsWindowsIdentityFlowSuppressed (), "IsWindowsIdentityFlowSuppressed-2");
253                         afc2.Undo ();
254
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");
258
259                         // we can't use the first AsyncFlowControl
260                         afc.Undo ();
261                 }
262
263                 // effects of ExecutionContext on SecurityContext
264
265                 [Test]
266                 public void ExecutionContext_SuppressFlow ()
267                 {
268                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
269                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
270                         try {
271                                 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
272                                 Assert.IsFalse (SecurityContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
273                                 Assert.IsNotNull (SecurityContext.Capture (), "Capture");
274                         }
275                         finally {
276                                 afc.Undo ();
277                         }
278                 }
279
280                 [Test]
281                 public void WindowsIdentity_ ()
282                 {
283                         Thread.CurrentPrincipal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
284                         SecurityContext sc = SecurityContext.Capture ();
285                         Assert.IsNotNull (sc, "Capture");
286                 }
287         }
288 }
289
290 #endif