New test.
[mono.git] / mcs / class / corlib / Test / System / AppDomainCas.cs
1 //
2 // AppDomainCas.cs - CAS unit tests for System.AppDomain
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 using NUnit.Framework;
30
31 using System;
32 using System.IO;
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Principal;
38
39 namespace MonoCasTests.System {
40
41         [TestFixture]
42         [Category ("CAS")]
43         public class AppDomainCas {
44
45                 private AppDomain ad;
46
47                 [TestFixtureSetUp]
48                 public void FixtureSetUp ()
49                 {
50                         // it's safe to create the AppDomain here
51                         string temp = Path.GetTempPath ();
52                         AppDomainSetup setup = new AppDomainSetup ();
53                         setup.ApplicationName = "CAS";
54                         setup.PrivateBinPath = temp;
55                         setup.DynamicBase = temp;
56                         ad = AppDomain.CreateDomain ("CAS", null, setup);
57                 }
58
59                 [SetUp]
60                 public void SetUp ()
61                 {
62                         if (!SecurityManager.SecurityEnabled)
63                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
64                 }
65
66                 // Partial Trust Tests
67
68                 [Test]
69                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
70                 public void PartialTrust_Deny_Unrestricted ()
71                 {
72                         // static
73                         Assert.IsNotNull (AppDomain.CurrentDomain, "CurrentDomain");
74                         // instance
75                         Assert.IsNotNull (ad.FriendlyName, "FriendlyName");
76                         Assert.IsNotNull (ad.SetupInformation, "SetupInformation");
77                         Assert.IsFalse (ad.ShadowCopyFiles, "ShadowCopyFiles");
78                 }
79
80 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
81                 [Category ("NotWorking")]
82                 [Test]
83                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
84                 [ExpectedException (typeof (SecurityException))]
85                 public void BaseDirectory_Deny_FileIOPermission ()
86                 {
87                         Assert.IsNotNull (ad.BaseDirectory, "BaseDirectory");
88                 }
89
90                 [Test]
91                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
92                 [ExpectedException (typeof (SecurityException))]
93                 public void CreateDomain1_Deny_ControlAppDomain ()
94                 {
95                         AppDomain.CreateDomain (null);
96                 }
97
98                 [Test]
99                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
100                 [ExpectedException (typeof (SecurityException))]
101                 public void CreateDomain2_Deny_ControlAppDomain ()
102                 {
103                         AppDomain.CreateDomain (null, null);
104                 }
105
106                 [Test]
107                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
108                 [ExpectedException (typeof (SecurityException))]
109                 public void CreateDomain3_Deny_ControlAppDomain ()
110                 {
111                         AppDomain.CreateDomain (null, null, null);
112                 }
113
114                 [Test]
115                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
116                 [ExpectedException (typeof (SecurityException))]
117                 public void CreateDomain5_Deny_ControlAppDomain ()
118                 {
119                         AppDomain.CreateDomain (null, null, null, null, false);
120                 }
121 #if NET_2_0
122                 [Test]
123                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
124                 [ExpectedException (typeof (SecurityException))]
125                 public void CreateDomain7_Deny_ControlAppDomain ()
126                 {
127                         AppDomain.CreateDomain (null, null, null, null, false, null, null);
128                 }
129 #endif
130 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
131                 [Category ("NotWorking")]
132                 [Test]
133                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
134                 [ExpectedException (typeof (SecurityException))]
135                 public void DynamicDirectory_Deny_FileIOPermission ()
136                 {
137                         Assert.IsNotNull (ad.DynamicDirectory, "DynamicDirectory");
138                 }
139
140                 [Category ("NotWorking")] // check not yet implemented
141                 [Test]
142                 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
143                 [ExpectedException (typeof (SecurityException))]
144                 public void Evidence_Deny_ControlEvidence ()
145                 {
146                         Assert.IsNotNull (ad.Evidence, "Evidence");
147                 }
148
149 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
150                 [Category ("NotWorking")]
151                 [Test]
152                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
153                 [ExpectedException (typeof (SecurityException))]
154                 public void RelativeSearchPath_Deny_FileIOPermission ()
155                 {
156                         Assert.IsNotNull (ad.RelativeSearchPath, "RelativeSearchPath");
157                 }
158
159 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
160                 [Category ("NotWorking")]
161                 [Test]
162                 [SecurityPermission (SecurityAction.Deny, ControlPrincipal = true)]
163                 [ExpectedException (typeof (SecurityException))]
164                 public void SetPrincipalPolicy_Deny_ControlPrincipal ()
165                 {
166                         ad.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
167                 }
168
169 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
170                 [Category ("NotWorking")]
171                 [Test]
172                 [SecurityPermission (SecurityAction.Deny, ControlPrincipal = true)]
173                 [ExpectedException (typeof (SecurityException))]
174                 public void SetThreadPrincipal_Deny_ControlPrincipal ()
175                 {
176                         ad.SetThreadPrincipal (new GenericPrincipal (new GenericIdentity ("me"), null));
177                 }
178
179                 [Test]
180                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
181                 [ExpectedException (typeof (SecurityException))]
182                 public void Unload_Deny_ControlAppDomain ()
183                 {
184                         AppDomain.Unload (null);
185                 }
186
187
188                 [Test]
189                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
190                 public void PermitOnly_FileIOPermission ()
191                 {
192                         Assert.IsNotNull (ad.BaseDirectory, "BaseDirectory");
193                         Assert.IsNotNull (ad.DynamicDirectory, "DynamicDirectory");
194                         Assert.IsNotNull (ad.RelativeSearchPath, "RelativeSearchPath");
195                 }
196
197                 [Test]
198                 [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
199                 public void PermitOnly_ControlEvidence ()
200                 {
201                         // other permissions required to get evidence from another domain
202                         Assert.IsNotNull (AppDomain.CurrentDomain.Evidence, "Evidence");
203                 }
204
205                 [Test]
206                 [SecurityPermission (SecurityAction.PermitOnly, ControlPrincipal = true)]
207                 public void PermitOnly_ControlPrincipal ()
208                 {
209                         ad.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
210                         ad.SetThreadPrincipal (new GenericPrincipal (new GenericIdentity ("me"), null));
211                 }
212
213                 // we use reflection to call AppDomain as some methods and events are protected 
214                 // by LinkDemand (which will be converted into full demand, i.e. a stack walk)
215                 // when reflection is used (i.e. it gets testable).
216
217                 [Test]
218                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
219                 [ExpectedException (typeof (SecurityException))]
220                 public void AppendPrivatePath ()
221                 {
222                         MethodInfo mi = typeof (AppDomain).GetMethod ("AppendPrivatePath");
223                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { String.Empty });
224                 }
225
226                 [Test]
227                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
228                 [ExpectedException (typeof (SecurityException))]
229                 public void ClearPrivatePath ()
230                 {
231                         MethodInfo mi = typeof (AppDomain).GetMethod ("ClearPrivatePath");
232                         mi.Invoke (AppDomain.CurrentDomain, null);
233                 }
234
235                 [Test]
236                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
237                 [ExpectedException (typeof (SecurityException))]
238                 public void ClearShadowCopyPath ()
239                 {
240                         MethodInfo mi = typeof (AppDomain).GetMethod ("ClearShadowCopyPath");
241                         mi.Invoke (AppDomain.CurrentDomain, null);
242                 }
243
244                 [Test]
245                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
246                 [ExpectedException (typeof (SecurityException))]
247                 public void SetCachePath ()
248                 {
249                         MethodInfo mi = typeof (AppDomain).GetMethod ("SetCachePath");
250                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { String.Empty });
251                 }
252
253                 [Test]
254                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
255                 [ExpectedException (typeof (SecurityException))]
256                 public void SetData ()
257                 {
258                         MethodInfo mi = typeof (AppDomain).GetMethod ("SetData");
259                         mi.Invoke (AppDomain.CurrentDomain, new object [2] { String.Empty, null });
260                 }
261
262                 [Test]
263                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
264                 [ExpectedException (typeof (SecurityException))]
265                 public void SetShadowCopyFiles ()
266                 {
267                         MethodInfo mi = typeof (AppDomain).GetMethod ("SetShadowCopyFiles");
268                         mi.Invoke (AppDomain.CurrentDomain, null);
269                 }
270
271                 [Test]
272                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
273                 [ExpectedException (typeof (SecurityException))]
274                 public void SetDynamicBase ()
275                 {
276                         MethodInfo mi = typeof (AppDomain).GetMethod ("SetDynamicBase");
277                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { String.Empty });
278                 }
279
280                 // events
281
282                 [Test]
283                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
284                 [ExpectedException (typeof (SecurityException))]
285                 public void AddDomainUnload ()
286                 {
287                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_DomainUnload");
288                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
289                 }
290
291                 [Test]
292                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
293                 [ExpectedException (typeof (SecurityException))]
294                 public void RemoveDomainUnload ()
295                 {
296                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_DomainUnload");
297                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
298                 }
299
300                 [Test]
301                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
302                 [ExpectedException (typeof (SecurityException))]
303                 public void AddAssemblyLoad ()
304                 {
305                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_AssemblyLoad");
306                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
307                 }
308
309                 [Test]
310                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
311                 [ExpectedException (typeof (SecurityException))]
312                 public void RemoveAssemblyLoad ()
313                 {
314                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_AssemblyLoad");
315                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
316                 }
317
318                 [Test]
319                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
320                 [ExpectedException (typeof (SecurityException))]
321                 public void AddProcessExit ()
322                 {
323                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_ProcessExit");
324                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
325                 }
326
327                 [Test]
328                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
329                 [ExpectedException (typeof (SecurityException))]
330                 public void RemoveProcessExit ()
331                 {
332                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_ProcessExit");
333                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
334                 }
335
336                 [Test]
337                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
338                 [ExpectedException (typeof (SecurityException))]
339                 public void AddTypeResolve ()
340                 {
341                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_TypeResolve");
342                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
343                 }
344
345                 [Test]
346                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
347                 [ExpectedException (typeof (SecurityException))]
348                 public void RemoveTypeResolve ()
349                 {
350                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_TypeResolve");
351                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
352                 }
353
354                 [Test]
355                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
356                 [ExpectedException (typeof (SecurityException))]
357                 public void AddResourceResolve ()
358                 {
359                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_ResourceResolve");
360                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
361                 }
362
363                 [Test]
364                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
365                 [ExpectedException (typeof (SecurityException))]
366                 public void RemoveResourceResolve ()
367                 {
368                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_ResourceResolve");
369                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
370                 }
371
372                 [Test]
373                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
374                 [ExpectedException (typeof (SecurityException))]
375                 public void AddAssemblyResolve ()
376                 {
377                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_AssemblyResolve");
378                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
379                 }
380
381                 [Test]
382                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
383                 [ExpectedException (typeof (SecurityException))]
384                 public void RemoveAssemblyResolve ()
385                 {
386                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_AssemblyResolve");
387                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
388                 }
389
390                 [Test]
391                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
392                 [ExpectedException (typeof (SecurityException))]
393                 public void AddUnhandledException ()
394                 {
395                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_UnhandledException");
396                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
397                 }
398
399                 [Test]
400                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
401                 [ExpectedException (typeof (SecurityException))]
402                 public void RemoveUnhandledException ()
403                 {
404                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_UnhandledException");
405                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
406                 }
407         }
408 }