6d95120f8ae3eb7c6e5360813f136bf3f2cd9489
[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                 [Test]
122                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
123                 [ExpectedException (typeof (SecurityException))]
124                 public void CreateDomain7_Deny_ControlAppDomain ()
125                 {
126                         AppDomain.CreateDomain (null, null, null, null, false, null, null);
127                 }
128 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
129                 [Category ("NotWorking")]
130                 [Test]
131                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
132                 [ExpectedException (typeof (SecurityException))]
133                 public void DynamicDirectory_Deny_FileIOPermission ()
134                 {
135                         Assert.IsNotNull (ad.DynamicDirectory, "DynamicDirectory");
136                 }
137
138                 [Category ("NotWorking")] // check not yet implemented
139                 [Test]
140                 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
141                 [ExpectedException (typeof (SecurityException))]
142                 public void Evidence_Deny_ControlEvidence ()
143                 {
144                         Assert.IsNotNull (ad.Evidence, "Evidence");
145                 }
146
147 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
148                 [Category ("NotWorking")]
149                 [Test]
150                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
151                 [ExpectedException (typeof (SecurityException))]
152                 public void RelativeSearchPath_Deny_FileIOPermission ()
153                 {
154                         Assert.IsNotNull (ad.RelativeSearchPath, "RelativeSearchPath");
155                 }
156
157 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
158                 [Category ("NotWorking")]
159                 [Test]
160                 [SecurityPermission (SecurityAction.Deny, ControlPrincipal = true)]
161                 [ExpectedException (typeof (SecurityException))]
162                 public void SetPrincipalPolicy_Deny_ControlPrincipal ()
163                 {
164                         ad.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
165                 }
166
167 // see http://bugzilla.ximian.com/show_bug.cgi?id=74411
168                 [Category ("NotWorking")]
169                 [Test]
170                 [SecurityPermission (SecurityAction.Deny, ControlPrincipal = true)]
171                 [ExpectedException (typeof (SecurityException))]
172                 public void SetThreadPrincipal_Deny_ControlPrincipal ()
173                 {
174                         ad.SetThreadPrincipal (new GenericPrincipal (new GenericIdentity ("me"), null));
175                 }
176
177                 [Test]
178                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
179                 [ExpectedException (typeof (SecurityException))]
180                 public void Unload_Deny_ControlAppDomain ()
181                 {
182                         AppDomain.Unload (null);
183                 }
184
185
186                 [Test]
187                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
188                 public void PermitOnly_FileIOPermission ()
189                 {
190                         Assert.IsNotNull (ad.BaseDirectory, "BaseDirectory");
191                         Assert.IsNotNull (ad.DynamicDirectory, "DynamicDirectory");
192                         Assert.IsNotNull (ad.RelativeSearchPath, "RelativeSearchPath");
193                 }
194
195                 [Test]
196                 [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
197                 public void PermitOnly_ControlEvidence ()
198                 {
199                         // other permissions required to get evidence from another domain
200                         Assert.IsNotNull (AppDomain.CurrentDomain.Evidence, "Evidence");
201                 }
202
203                 [Test]
204                 [SecurityPermission (SecurityAction.PermitOnly, ControlPrincipal = true)]
205                 public void PermitOnly_ControlPrincipal ()
206                 {
207                         ad.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
208                         ad.SetThreadPrincipal (new GenericPrincipal (new GenericIdentity ("me"), null));
209                 }
210
211                 // we use reflection to call AppDomain as some methods and events are protected 
212                 // by LinkDemand (which will be converted into full demand, i.e. a stack walk)
213                 // when reflection is used (i.e. it gets testable).
214
215                 [Test]
216                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
217                 [ExpectedException (typeof (SecurityException))]
218                 public void AppendPrivatePath ()
219                 {
220                         MethodInfo mi = typeof (AppDomain).GetMethod ("AppendPrivatePath");
221                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { String.Empty });
222                 }
223
224                 [Test]
225                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
226                 [ExpectedException (typeof (SecurityException))]
227                 public void ClearPrivatePath ()
228                 {
229                         MethodInfo mi = typeof (AppDomain).GetMethod ("ClearPrivatePath");
230                         mi.Invoke (AppDomain.CurrentDomain, null);
231                 }
232
233                 [Test]
234                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
235                 [ExpectedException (typeof (SecurityException))]
236                 public void ClearShadowCopyPath ()
237                 {
238                         MethodInfo mi = typeof (AppDomain).GetMethod ("ClearShadowCopyPath");
239                         mi.Invoke (AppDomain.CurrentDomain, null);
240                 }
241
242                 [Test]
243                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
244                 [ExpectedException (typeof (SecurityException))]
245                 public void SetCachePath ()
246                 {
247                         MethodInfo mi = typeof (AppDomain).GetMethod ("SetCachePath");
248                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { String.Empty });
249                 }
250
251                 [Test]
252                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
253                 [ExpectedException (typeof (SecurityException))]
254                 public void SetData ()
255                 {
256                         MethodInfo mi = typeof (AppDomain).GetMethod ("SetData");
257                         mi.Invoke (AppDomain.CurrentDomain, new object [2] { String.Empty, null });
258                 }
259
260                 [Test]
261                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
262                 [ExpectedException (typeof (SecurityException))]
263                 public void SetShadowCopyFiles ()
264                 {
265                         MethodInfo mi = typeof (AppDomain).GetMethod ("SetShadowCopyFiles");
266                         mi.Invoke (AppDomain.CurrentDomain, null);
267                 }
268
269                 [Test]
270                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
271                 [ExpectedException (typeof (SecurityException))]
272                 public void SetDynamicBase ()
273                 {
274                         MethodInfo mi = typeof (AppDomain).GetMethod ("SetDynamicBase");
275                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { String.Empty });
276                 }
277
278                 // events
279
280                 [Test]
281                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
282                 [ExpectedException (typeof (SecurityException))]
283                 public void AddDomainUnload ()
284                 {
285                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_DomainUnload");
286                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
287                 }
288
289                 [Test]
290                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
291                 [ExpectedException (typeof (SecurityException))]
292                 public void RemoveDomainUnload ()
293                 {
294                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_DomainUnload");
295                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
296                 }
297
298                 [Test]
299                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
300                 [ExpectedException (typeof (SecurityException))]
301                 public void AddAssemblyLoad ()
302                 {
303                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_AssemblyLoad");
304                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
305                 }
306
307                 [Test]
308                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
309                 [ExpectedException (typeof (SecurityException))]
310                 public void RemoveAssemblyLoad ()
311                 {
312                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_AssemblyLoad");
313                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
314                 }
315
316                 [Test]
317                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
318                 [ExpectedException (typeof (SecurityException))]
319                 public void AddProcessExit ()
320                 {
321                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_ProcessExit");
322                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
323                 }
324
325                 [Test]
326                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
327                 [ExpectedException (typeof (SecurityException))]
328                 public void RemoveProcessExit ()
329                 {
330                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_ProcessExit");
331                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
332                 }
333
334                 [Test]
335                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
336                 [ExpectedException (typeof (SecurityException))]
337                 public void AddTypeResolve ()
338                 {
339                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_TypeResolve");
340                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
341                 }
342
343                 [Test]
344                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
345                 [ExpectedException (typeof (SecurityException))]
346                 public void RemoveTypeResolve ()
347                 {
348                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_TypeResolve");
349                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
350                 }
351
352                 [Test]
353                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
354                 [ExpectedException (typeof (SecurityException))]
355                 public void AddResourceResolve ()
356                 {
357                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_ResourceResolve");
358                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
359                 }
360
361                 [Test]
362                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
363                 [ExpectedException (typeof (SecurityException))]
364                 public void RemoveResourceResolve ()
365                 {
366                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_ResourceResolve");
367                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
368                 }
369
370                 [Test]
371                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
372                 [ExpectedException (typeof (SecurityException))]
373                 public void AddAssemblyResolve ()
374                 {
375                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_AssemblyResolve");
376                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
377                 }
378
379                 [Test]
380                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
381                 [ExpectedException (typeof (SecurityException))]
382                 public void RemoveAssemblyResolve ()
383                 {
384                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_AssemblyResolve");
385                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
386                 }
387
388                 [Test]
389                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
390                 [ExpectedException (typeof (SecurityException))]
391                 public void AddUnhandledException ()
392                 {
393                         MethodInfo mi = typeof (AppDomain).GetMethod ("add_UnhandledException");
394                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
395                 }
396
397                 [Test]
398                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
399                 [ExpectedException (typeof (SecurityException))]
400                 public void RemoveUnhandledException ()
401                 {
402                         MethodInfo mi = typeof (AppDomain).GetMethod ("remove_UnhandledException");
403                         mi.Invoke (AppDomain.CurrentDomain, new object [1] { null });
404                 }
405         }
406 }