(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System / AppDomainTest.cs
blob3d87da1d9a65c0668beae45899a432707da076e3
1 //
2 // AppDomainTest.cs - NUnit Test Cases for AppDomain
3 //
4 // Author:
5 // Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // Copyright (C) 2004 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.
29 using NUnit.Framework;
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.Reflection;
34 using System.Reflection.Emit;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38 using System.Security.Principal;
40 namespace MonoTests.System {
42 [TestFixture]
43 public class AppDomainTest {
45 private AppDomain ad;
46 private ArrayList files = new ArrayList ();
48 [TearDown]
49 public void TearDown ()
51 if (ad != null) {
52 try {
53 // FIXME: Lots of GC warning when unloading
54 // AppDomain.Unload (ad);
55 ad = null;
57 catch {} // do not affect unit test results in TearDown
59 foreach (string fname in files) {
60 File.Delete (fname);
62 files.Clear ();
65 [Test]
66 public void SetThreadPrincipal ()
68 IIdentity i = new GenericIdentity ("sebastien@ximian.com", "rfc822");
69 IPrincipal p = new GenericPrincipal (i, null);
70 ad = AppDomain.CreateDomain ("SetThreadPrincipal");
71 ad.SetThreadPrincipal (p);
74 [Test]
75 [ExpectedException (typeof (ArgumentNullException))]
76 public void SetThreadPrincipalNull ()
78 AppDomain.CurrentDomain.SetThreadPrincipal (null);
81 [Test]
82 [ExpectedException (typeof (PolicyException))]
83 public void SetThreadPrincipalTwice ()
85 IIdentity i = new GenericIdentity ("sebastien@ximian.com", "rfc822");
86 IPrincipal p = new GenericPrincipal (i, null);
87 ad = AppDomain.CreateDomain ("SetThreadPrincipalTwice");
88 ad.SetThreadPrincipal (p);
89 // you only live twice (or so James told me ;-)
90 ad.SetThreadPrincipal (p);
93 [Test]
94 [ExpectedException (typeof (AppDomainUnloadedException))]
95 [Ignore ("Unloading cause lots of GC warning")]
96 public void SetThreadPrincipalUnloaded ()
98 ad = AppDomain.CreateDomain ("Ximian");
99 AppDomain.Unload (ad);
100 IIdentity i = new GenericIdentity ("sebastien@ximian.com", "rfc822");
101 IPrincipal p = new GenericPrincipal (i, null);
102 ad.SetThreadPrincipal (p);
105 [Test]
106 public void SetPrincipalPolicy_NoPrincipal ()
108 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
111 [Test]
112 public void SetPrincipalPolicy_UnauthenticatedPrincipal ()
114 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
117 [Test]
118 public void SetPrincipalPolicy_WindowsPrincipal ()
120 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
123 [Test]
124 [ExpectedException (typeof (AppDomainUnloadedException))]
125 [Ignore ("Unloading cause lots of GC warning")]
126 public void SetPrincipalPolicyUnloaded ()
128 ad = AppDomain.CreateDomain ("Ximian");
129 AppDomain.Unload (ad);
130 ad.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
133 [Test]
134 public void CreateDomain_String ()
136 ad = AppDomain.CreateDomain ("CreateDomain_String");
137 Assert.IsNotNull (ad.Evidence, "Evidence");
138 // Evidence are copied (or referenced?) from default app domain
139 // we can't get default so we use the current (which should have copied the default)
140 Assert.AreEqual (AppDomain.CurrentDomain.Evidence.Count, ad.Evidence.Count, "Evidence.Count");
143 [Test]
144 [ExpectedException (typeof (ArgumentNullException))]
145 public void CreateDomain_String_Null ()
147 ad = AppDomain.CreateDomain (null);
150 [Test]
151 public void CreateDomain_StringEvidence ()
153 Evidence e = new Evidence ();
154 ad = AppDomain.CreateDomain ("CreateDomain_StringEvidence", e);
155 Assert.IsNotNull (ad.Evidence, "Evidence");
156 Assert.AreEqual (0, ad.Evidence.Count, "Evidence.Count");
158 e.AddHost (new Zone (SecurityZone.MyComputer));
159 Assert.AreEqual (0, ad.Evidence.Count, "Evidence.Count");
160 // evidence isn't copied but referenced
163 [Test]
164 [ExpectedException (typeof (ArgumentNullException))]
165 public void CreateDomain_StringNullEvidence ()
167 ad = AppDomain.CreateDomain (null, new Evidence ());
170 [Test]
171 public void CreateDomain_StringEvidenceNull ()
173 ad = AppDomain.CreateDomain ("CreateDomain_StringEvidenceNull", null);
174 Assert.IsNotNull (ad.Evidence, "Evidence");
175 // Evidence are copied (or referenced?) from default app domain
176 // we can't get default so we use the current (which should have copied the default)
177 Evidence e = AppDomain.CurrentDomain.Evidence;
178 Assert.AreEqual (e.Count, ad.Evidence.Count, "Evidence.Count-1");
179 e.AddHost (new Zone (SecurityZone.MyComputer));
180 Assert.AreEqual (e.Count - 1, ad.Evidence.Count, "Evidence.Count-2");
181 // evidence are copied
184 [Test]
185 public void CreateDomain_StringEvidenceAppDomainSetup ()
187 Evidence e = new Evidence ();
188 AppDomainSetup info = new AppDomainSetup ();
189 info.ApplicationName = "ApplicationName";
191 ad = AppDomain.CreateDomain ("CreateDomain_StringEvidenceAppDomainSetup", e, info);
192 Assert.IsNotNull (ad.Evidence, "Evidence");
193 Assert.AreEqual (0, ad.Evidence.Count, "Evidence.Count");
194 Assert.IsNotNull (ad.SetupInformation, "SetupInformation");
195 Assert.AreEqual ("ApplicationName", ad.SetupInformation.ApplicationName);
197 e.AddHost (new Zone (SecurityZone.MyComputer));
198 Assert.AreEqual (0, ad.Evidence.Count, "Evidence.Count");
199 // evidence isn't copied but referenced
202 [Test]
203 [ExpectedException (typeof (ArgumentNullException))]
204 public void CreateDomain_StringNullEvidenceAppDomainSetup ()
206 AppDomainSetup info = new AppDomainSetup ();
207 ad = AppDomain.CreateDomain (null, new Evidence (), info);
210 [Test]
211 public void CreateDomain_StringEvidenceNullAppDomainSetup ()
213 AppDomainSetup info = new AppDomainSetup ();
214 info.ApplicationName = "ApplicationName";
215 ad = AppDomain.CreateDomain ("CreateDomain_StringEvidenceNullAppDomainSetup", null, info);
216 Assert.IsNotNull (ad.Evidence, "Evidence");
217 // Evidence are copied (or referenced?) from default app domain
218 // we can't get default so we use the current (which should have copied the default)
219 Assert.AreEqual (AppDomain.CurrentDomain.Evidence.Count, ad.Evidence.Count, "Evidence.Count");
220 Assert.AreEqual ("ApplicationName", ad.SetupInformation.ApplicationName, "ApplicationName-1");
221 info.ApplicationName = "Test";
222 Assert.AreEqual ("Test", info.ApplicationName, "ApplicationName-2");
223 Assert.AreEqual ("ApplicationName", ad.SetupInformation.ApplicationName, "ApplicationName-3");
224 // copied
227 [Test]
228 public void CreateDomain_StringEvidenceAppDomainSetupNull ()
230 Evidence e = new Evidence ();
231 ad = AppDomain.CreateDomain ("CreateDomain_StringEvidenceAppDomainSetupNull", e, null);
232 Assert.IsNotNull (ad.Evidence, "Evidence");
233 Assert.AreEqual (0, ad.Evidence.Count, "Evidence.Count");
234 // SetupInformation is copied from default app domain
235 Assert.IsNotNull (ad.SetupInformation, "SetupInformation");
238 [Test]
239 public void SetAppDomainPolicy ()
241 ad = AppDomain.CreateDomain ("SetAppDomainPolicy_Null");
242 ad.SetAppDomainPolicy (PolicyLevel.CreateAppDomainLevel ());
243 // not much to see
246 [Test]
247 [ExpectedException (typeof (ArgumentNullException))]
248 public void SetAppDomainPolicy_Null ()
250 ad = AppDomain.CreateDomain ("SetAppDomainPolicy_Null");
251 ad.SetAppDomainPolicy (null);
254 [Test]
255 #if ! NET_2_0
256 // MS bug for 2.x ???
257 [ExpectedException (typeof (PolicyException))]
258 #endif
259 public void SetAppDomainPolicy_Dual ()
261 ad = AppDomain.CreateDomain ("SetAppDomainPolicy_Dual");
262 PolicyLevel pl = PolicyLevel.CreateAppDomainLevel ();
263 PermissionSet ps = new PermissionSet (PermissionState.Unrestricted);
264 pl.RootCodeGroup.PolicyStatement = new PolicyStatement (ps);
265 ad.SetAppDomainPolicy (pl);
267 // only one time!
268 pl = PolicyLevel.CreateAppDomainLevel ();
269 ps = new PermissionSet (PermissionState.None);
270 pl.RootCodeGroup.PolicyStatement = new PolicyStatement (ps);
271 ad.SetAppDomainPolicy (pl);
274 [Test]
275 [ExpectedException (typeof (AppDomainUnloadedException))]
276 [Ignore ("Unloading cause lots of GC warning")]
277 public void SetAppDomainPolicy_Unloaded ()
279 ad = AppDomain.CreateDomain ("SetAppDomainPolicy_Unloaded");
280 AppDomain.Unload (ad);
281 ad.SetAppDomainPolicy (PolicyLevel.CreateAppDomainLevel ());
284 [Test]
285 [ExpectedException (typeof (ArgumentNullException))]
286 public void GetData_Null ()
288 AppDomain.CurrentDomain.GetData (null);
291 [Test]
292 public void SetData ()
294 AppDomain.CurrentDomain.SetData ("data", "data");
295 Assert.AreEqual ("data", AppDomain.CurrentDomain.GetData ("data"), "GetData");
296 AppDomain.CurrentDomain.SetData ("data", null);
297 Assert.IsNull (AppDomain.CurrentDomain.GetData ("data"), "GetData-Null");
300 [Test]
301 [ExpectedException (typeof (ArgumentNullException))]
302 public void SetData_Null ()
304 AppDomain.CurrentDomain.SetData (null, "data");
307 #if NET_2_0
308 [Test]
309 [ExpectedException (typeof (ArgumentNullException))]
310 public void Activate_Null ()
312 AppDomain.Activate (null);
315 [Test]
316 [ExpectedException (typeof (ArgumentNullException))]
317 public void ActivateNewProcess_Null ()
319 AppDomain.ActivateNewProcess (null);
322 [Test]
323 public void ApplyPolicy ()
325 ad = AppDomain.CreateDomain ("ApplyPolicy");
326 string fullname = Assembly.GetExecutingAssembly ().FullName;
327 string result = ad.ApplyPolicy (fullname);
328 Assert.AreEqual (fullname, result, "ApplyPolicy");
329 // doesn't even requires an assembly name
330 Assert.AreEqual ("123", ad.ApplyPolicy ("123"), "Invalid FullName");
333 [Test]
334 [ExpectedException (typeof (ArgumentException))]
335 public void ApplyPolicy_Empty ()
337 ad = AppDomain.CreateDomain ("ApplyPolicy_Empty");
338 ad.ApplyPolicy (String.Empty);
341 [Test]
342 [ExpectedException (typeof (ArgumentNullException))]
343 public void ApplyPolicy_Null ()
345 ad = AppDomain.CreateDomain ("ApplyPolicy_Null");
346 ad.ApplyPolicy (null);
349 [Test]
350 public void DomainManager ()
352 Assert.IsNull (AppDomain.CurrentDomain.DomainManager, "CurrentDomain.DomainManager");
353 ad = AppDomain.CreateDomain ("DomainManager");
354 Assert.IsNull (ad.DomainManager, "ad.DomainManager");
357 [Test]
358 public void IsDefaultAppDomain ()
360 ad = AppDomain.CreateDomain ("ReflectionOnlyGetAssemblies");
361 Assert.IsFalse (ad.IsDefaultAppDomain (), "IsDefaultAppDomain");
362 // we have no public way to get the default appdomain
365 [Test]
366 public void ReflectionOnlyGetAssemblies ()
368 ad = AppDomain.CreateDomain ("ReflectionOnlyGetAssemblies");
369 Assembly [] a = ad.ReflectionOnlyGetAssemblies ();
370 Assert.IsNotNull (a, "ReflectionOnlyGetAssemblies");
371 Assert.AreEqual (0, a.Length, "Count");
373 #endif