2 // AppDomainTest.cs - NUnit Test Cases for AppDomain
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // Copyright (C) 2004 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.
29 using NUnit
.Framework
;
31 using System
.Collections
;
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
{
43 public class AppDomainTest
{
46 private ArrayList files
= new ArrayList ();
49 public void TearDown ()
53 // FIXME: Lots of GC warning when unloading
54 // AppDomain.Unload (ad);
57 catch {} // do not affect unit test results in TearDown
59 foreach (string fname
in files
) {
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
);
75 [ExpectedException (typeof (ArgumentNullException
))]
76 public void SetThreadPrincipalNull ()
78 AppDomain
.CurrentDomain
.SetThreadPrincipal (null);
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
);
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
);
106 public void SetPrincipalPolicy_NoPrincipal ()
108 AppDomain
.CurrentDomain
.SetPrincipalPolicy (PrincipalPolicy
.NoPrincipal
);
112 public void SetPrincipalPolicy_UnauthenticatedPrincipal ()
114 AppDomain
.CurrentDomain
.SetPrincipalPolicy (PrincipalPolicy
.UnauthenticatedPrincipal
);
118 public void SetPrincipalPolicy_WindowsPrincipal ()
120 AppDomain
.CurrentDomain
.SetPrincipalPolicy (PrincipalPolicy
.WindowsPrincipal
);
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
);
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");
144 [ExpectedException (typeof (ArgumentNullException
))]
145 public void CreateDomain_String_Null ()
147 ad
= AppDomain
.CreateDomain (null);
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
164 [ExpectedException (typeof (ArgumentNullException
))]
165 public void CreateDomain_StringNullEvidence ()
167 ad
= AppDomain
.CreateDomain (null, new Evidence ());
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
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
203 [ExpectedException (typeof (ArgumentNullException
))]
204 public void CreateDomain_StringNullEvidenceAppDomainSetup ()
206 AppDomainSetup info
= new AppDomainSetup ();
207 ad
= AppDomain
.CreateDomain (null, new Evidence (), info
);
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");
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");
239 public void SetAppDomainPolicy ()
241 ad
= AppDomain
.CreateDomain ("SetAppDomainPolicy_Null");
242 ad
.SetAppDomainPolicy (PolicyLevel
.CreateAppDomainLevel ());
247 [ExpectedException (typeof (ArgumentNullException
))]
248 public void SetAppDomainPolicy_Null ()
250 ad
= AppDomain
.CreateDomain ("SetAppDomainPolicy_Null");
251 ad
.SetAppDomainPolicy (null);
256 // MS bug for 2.x ???
257 [ExpectedException (typeof (PolicyException
))]
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
);
268 pl
= PolicyLevel
.CreateAppDomainLevel ();
269 ps
= new PermissionSet (PermissionState
.None
);
270 pl
.RootCodeGroup
.PolicyStatement
= new PolicyStatement (ps
);
271 ad
.SetAppDomainPolicy (pl
);
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 ());
285 [ExpectedException (typeof (ArgumentNullException
))]
286 public void GetData_Null ()
288 AppDomain
.CurrentDomain
.GetData (null);
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");
301 [ExpectedException (typeof (ArgumentNullException
))]
302 public void SetData_Null ()
304 AppDomain
.CurrentDomain
.SetData (null, "data");
309 [ExpectedException (typeof (ArgumentNullException
))]
310 public void Activate_Null ()
312 AppDomain
.Activate (null);
316 [ExpectedException (typeof (ArgumentNullException
))]
317 public void ActivateNewProcess_Null ()
319 AppDomain
.ActivateNewProcess (null);
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");
334 [ExpectedException (typeof (ArgumentException
))]
335 public void ApplyPolicy_Empty ()
337 ad
= AppDomain
.CreateDomain ("ApplyPolicy_Empty");
338 ad
.ApplyPolicy (String
.Empty
);
342 [ExpectedException (typeof (ArgumentNullException
))]
343 public void ApplyPolicy_Null ()
345 ad
= AppDomain
.CreateDomain ("ApplyPolicy_Null");
346 ad
.ApplyPolicy (null);
350 public void DomainManager ()
352 Assert
.IsNull (AppDomain
.CurrentDomain
.DomainManager
, "CurrentDomain.DomainManager");
353 ad
= AppDomain
.CreateDomain ("DomainManager");
354 Assert
.IsNull (ad
.DomainManager
, "ad.DomainManager");
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
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");