2 // System.Reflection.Assembly Test Cases
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Philippe Lavoie (philippe.lavoie@cactus.ca)
7 // Sebastien Pouliot (sebastien@ximian.com)
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
34 using System.Configuration.Assemblies;
35 using System.Globalization;
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Runtime.Serialization;
40 using System.Security;
42 namespace MonoTests.System.Reflection
45 public class AssemblyTest
48 public void CreateInstance()
50 Type type = typeof (AssemblyTest);
51 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
52 Assert.IsNotNull (obj, "#01");
53 Assert.AreEqual (GetType (), obj.GetType (), "#02");
57 public void CreateInvalidInstance()
59 Type type = typeof (AssemblyTest);
60 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
61 Assert.IsNull (obj, "#03");
66 [Category ("NotWorking")]
67 [ExpectedException (typeof (ArgumentException))]
69 [ExpectedException (typeof (TypeLoadException))]
71 public void TestGetType ()
74 typeof (int).Assembly.GetType ("&blabla", true, true);
78 public void GetEntryAssembly ()
80 // note: only available in default appdomain
81 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
82 // Not sure we should emulate this behavior.
83 string fname = AppDomain.CurrentDomain.FriendlyName;
84 if (fname.EndsWith (".dll")) { // nunit-console
85 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
87 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
90 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
92 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
97 [Category ("NotWorking")]
100 public void Corlib ()
102 Assembly corlib = typeof (int).Assembly;
103 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
104 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
105 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
106 Assert.IsNotNull (corlib.Evidence, "Evidence");
107 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
109 // corlib doesn't reference anything
110 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
112 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
113 // not really "true" but it's even more trusted so...
114 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
115 Assert.AreEqual (0, corlib.HostContext, "HostContext");
116 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
117 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
118 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
120 Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
121 Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
122 Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
127 public void Corlib_test ()
129 Assembly corlib_test = Assembly.GetExecutingAssembly ();
130 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
131 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
132 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
134 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
136 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
137 Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
138 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
139 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
141 Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
146 public void GetAssembly ()
148 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
149 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
153 [ExpectedException (typeof (ArgumentNullException))]
154 public void GetFile_Null ()
156 Assembly.GetExecutingAssembly ().GetFile (null);
160 [ExpectedException (typeof (ArgumentException))]
161 public void GetFile_Empty ()
163 Assembly.GetExecutingAssembly ().GetFile (String.Empty);
167 public void GetFiles_False ()
169 Assembly corlib = typeof (int).Assembly;
170 FileStream[] fss = corlib.GetFiles ();
171 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
173 Assembly corlib_test = Assembly.GetExecutingAssembly ();
174 fss = corlib_test.GetFiles ();
175 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
179 public void GetFiles_True ()
181 Assembly corlib = typeof (int).Assembly;
182 FileStream[] fss = corlib.GetFiles ();
183 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
185 Assembly corlib_test = Assembly.GetExecutingAssembly ();
186 fss = corlib_test.GetFiles ();
187 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
192 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
194 public void LoadFrom_Empty_Assembly ()
196 string tempFile = Path.GetTempFileName ();
199 Assembly.LoadFrom (tempFile);
201 } catch (BadImageFormatException ex) {
202 Assert.IsNull (ex.InnerException, "#2");
204 File.Delete (tempFile);
209 public void LoadFrom_Invalid_Assembly ()
211 string tempFile = Path.GetTempFileName ();
212 using (StreamWriter sw = File.CreateText (tempFile)) {
213 sw.WriteLine ("foo");
218 Assembly.LoadFrom (tempFile);
220 } catch (BadImageFormatException ex) {
221 Assert.IsNull (ex.InnerException, "#2");
223 File.Delete (tempFile);
228 public void LoadFrom_NonExisting_Assembly ()
230 string tempFile = Path.GetTempFileName ();
231 File.Delete (tempFile);
234 Assembly.LoadFrom (tempFile);
236 } catch (FileNotFoundException ex) {
237 Assert.IsNull (ex.InnerException, "#2");
239 File.Delete (tempFile);
244 public void LoadWithPartialName ()
246 string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
248 foreach (string s in names)
249 if (Assembly.LoadWithPartialName (s) != null)
251 Assertion.Fail ("Was not able to load any corlib test");
255 [ExpectedException (typeof (ArgumentNullException))]
256 public void GetObjectData_Null ()
258 Assembly corlib = typeof (int).Assembly;
259 corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
263 public void GetReferencedAssemblies ()
265 Assembly corlib_test = Assembly.GetExecutingAssembly ();
266 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
267 foreach (AssemblyName an in names) {
268 Assert.IsNull (an.CodeBase, "CodeBase");
269 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
270 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
271 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
272 Assert.IsNotNull (an.FullName, "FullName");
273 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
274 Assert.IsNull (an.KeyPair, "KeyPair");
275 Assert.IsNotNull (an.Name, "Name");
276 Assert.IsNotNull (an.Version, "Version");
277 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
278 an.VersionCompatibility, "VersionCompatibility");
283 public void Location_Empty() {
284 string assemblyFileName = Path.Combine (
285 Path.GetTempPath (), "AssemblyLocation.dll");
288 AssemblyName assemblyName = new AssemblyName ();
289 assemblyName.Name = "AssemblyLocation";
291 AssemblyBuilder ab = AppDomain.CurrentDomain
292 .DefineDynamicAssembly (assemblyName,
293 AssemblyBuilderAccess.Save,
295 AppDomain.CurrentDomain.Evidence);
296 ab.Save (Path.GetFileName (assemblyFileName));
298 using (FileStream fs = File.OpenRead (assemblyFileName)) {
299 byte[] buffer = new byte[fs.Length];
300 fs.Read (buffer, 0, buffer.Length);
301 Assembly assembly = Assembly.Load (buffer);
302 Assert.AreEqual (string.Empty, assembly.Location);
306 File.Delete (assemblyFileName);
311 [Category ("NotWorking")]
312 public void bug78464 ()
314 string assemblyFileName = Path.Combine (
315 Path.GetTempPath (), "bug78464.dll");
318 // execute test in separate appdomain to allow assembly to be unloaded
319 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
320 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
322 crossDomainTester.bug78464 (assemblyFileName);
324 AppDomain.Unload (testDomain);
327 File.Delete (assemblyFileName);
332 [Category ("NotWorking")]
333 public void bug78465 ()
335 string assemblyFileName = Path.Combine (
336 Path.GetTempPath (), "bug78465.dll");
339 AssemblyName assemblyName = new AssemblyName ();
340 assemblyName.Name = "bug78465";
342 AssemblyBuilder ab = AppDomain.CurrentDomain
343 .DefineDynamicAssembly (assemblyName,
344 AssemblyBuilderAccess.Save,
345 Path.GetDirectoryName (assemblyFileName),
346 AppDomain.CurrentDomain.Evidence);
347 ab.Save (Path.GetFileName (assemblyFileName));
349 using (FileStream fs = File.OpenRead (assemblyFileName)) {
350 byte[] buffer = new byte[fs.Length];
351 fs.Read (buffer, 0, buffer.Length);
352 Assembly assembly = Assembly.Load (buffer);
353 Assert.AreEqual (string.Empty, assembly.Location, "#1");
357 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
358 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
360 crossDomainTester.bug78465 (assemblyFileName);
362 AppDomain.Unload (testDomain);
365 File.Delete (assemblyFileName);
370 public void bug78468 ()
372 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
374 string resourceFileName = Path.Combine (Path.GetTempPath (),
377 using (StreamWriter sw = File.CreateText (resourceFileName)) {
378 sw.WriteLine ("FOO");
383 AssemblyName assemblyName = new AssemblyName ();
384 assemblyName.Name = "bug78468a";
386 AssemblyBuilder ab = AppDomain.CurrentDomain
387 .DefineDynamicAssembly (assemblyName,
388 AssemblyBuilderAccess.Save,
390 AppDomain.CurrentDomain.Evidence);
391 ab.AddResourceFile ("read", "readme.txt");
392 ab.Save (Path.GetFileName (assemblyFileNameA));
396 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
397 byte[] buffer = new byte[fs.Length];
398 fs.Read (buffer, 0, buffer.Length);
399 assembly = Assembly.Load (buffer);
403 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
404 string[] resNames = assembly.GetManifestResourceNames ();
405 Assert.IsNotNull (resNames, "#A2");
406 Assert.AreEqual (1, resNames.Length, "#A3");
407 Assert.AreEqual ("read", resNames[0], "#A4");
408 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
409 Assert.IsNotNull (resInfo, "#A5");
410 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
411 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
412 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
415 assembly.GetManifestResourceStream ("read");
417 } catch (FileNotFoundException) {
420 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
423 assembly.GetFile ("readme.txt");
424 Assert.Fail ("#A10");
425 } catch (FileNotFoundException) {
428 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
431 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
432 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
434 crossDomainTester.bug78468 (assemblyFileNameB);
436 AppDomain.Unload (testDomain);
437 File.Delete (assemblyFileNameB);
440 File.Delete (assemblyFileNameA);
441 File.Delete (resourceFileName);
447 [Category ("NotWorking")]
448 public void ReflectionOnlyLoad ()
450 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
452 Assert.IsNotNull (assembly);
453 Assert.IsTrue (assembly.ReflectionOnly);
457 public void ReflectionOnlyLoadFrom ()
459 string loc = typeof (AssemblyTest).Assembly.Location;
460 string filename = Path.GetFileName (loc);
461 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
463 Assert.IsNotNull (assembly);
464 Assert.IsTrue (assembly.ReflectionOnly);
468 [ExpectedException (typeof (ArgumentException))]
469 public void CreateInstanceOnRefOnly ()
471 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
472 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
477 [Category ("NotWorking")] // patch for bug #79720 must be committed first
478 public void Load_Culture ()
480 string tempDir = Path.Combine (Path.GetTempPath (),
481 "MonoTests.System.Reflection.AssemblyTest");
482 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
483 if (!Directory.Exists (cultureTempDir))
484 Directory.CreateDirectory (cultureTempDir);
485 cultureTempDir = Path.Combine (tempDir, "en-US");
486 if (!Directory.Exists (cultureTempDir))
487 Directory.CreateDirectory (cultureTempDir);
490 AppDomain ad = CreateTestDomain (tempDir, true);
492 CrossDomainTester cdt = CreateCrossDomainTester (ad);
496 AssemblyName aname = new AssemblyName ();
497 aname.Name = "culturea";
498 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
500 aname = new AssemblyName ();
501 aname.Name = "culturea";
502 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
504 aname = new AssemblyName ();
505 aname.Name = "culturea";
506 aname.CultureInfo = new CultureInfo ("nl-BE");
507 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
509 aname = new AssemblyName ();
510 aname.Name = "culturea";
511 aname.CultureInfo = CultureInfo.InvariantCulture;
512 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
516 aname = new AssemblyName ();
517 aname.Name = "cultureb";
518 aname.CultureInfo = new CultureInfo ("nl-BE");
519 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
521 aname = new AssemblyName ();
522 aname.Name = "cultureb";
523 aname.CultureInfo = new CultureInfo ("nl-BE");
524 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
526 aname = new AssemblyName ();
527 aname.Name = "cultureb";
528 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
530 aname = new AssemblyName ();
531 aname.Name = "cultureb";
532 aname.CultureInfo = new CultureInfo ("en-US");
533 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
537 aname = new AssemblyName ();
538 aname.Name = "culturec";
539 aname.CultureInfo = new CultureInfo ("nl-BE");
540 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
542 aname = new AssemblyName ();
543 aname.Name = "culturec";
544 aname.CultureInfo = new CultureInfo ("nl-BE");
545 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
547 aname = new AssemblyName ();
548 aname.Name = "culturec";
549 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
551 aname = new AssemblyName ();
552 aname.Name = "culturec";
553 aname.CultureInfo = CultureInfo.InvariantCulture;
554 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
558 aname = new AssemblyName ();
559 aname.Name = "cultured";
560 aname.CultureInfo = new CultureInfo ("nl-BE");
561 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
563 aname = new AssemblyName ();
564 aname.Name = "cultured";
565 aname.CultureInfo = new CultureInfo ("nl-BE");
566 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
568 aname = new AssemblyName ();
569 aname.Name = "cultured";
570 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
572 aname = new AssemblyName ();
573 aname.Name = "cultured";
574 aname.CultureInfo = CultureInfo.InvariantCulture;
575 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
577 AppDomain.Unload (ad);
578 if (Directory.Exists (tempDir))
579 Directory.Delete (tempDir, true);
585 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
587 [Category ("NotWorking")]
589 public void Load_Culture_Mismatch ()
591 string tempDir = Path.Combine (Path.GetTempPath (),
592 "MonoTests.System.Reflection.AssemblyTest");
593 string cultureTempDir = Path.Combine (tempDir, "en-US");
594 if (!Directory.Exists (cultureTempDir))
595 Directory.CreateDirectory (cultureTempDir);
597 AppDomain ad = CreateTestDomain (tempDir, true);
599 CrossDomainTester cdt = CreateCrossDomainTester (ad);
603 AssemblyName aname = new AssemblyName ();
604 aname.Name = "bug79712a";
605 aname.CultureInfo = new CultureInfo ("nl-BE");
606 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
608 aname = new AssemblyName ();
609 aname.Name = "bug79712a";
610 aname.CultureInfo = CultureInfo.InvariantCulture;
612 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
614 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
619 aname = new AssemblyName ();
620 aname.Name = "bug79712b";
621 aname.CultureInfo = new CultureInfo ("nl-BE");
622 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
624 aname = new AssemblyName ();
625 aname.Name = "bug79712b";
626 aname.CultureInfo = new CultureInfo ("en-US");
628 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
630 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
633 AppDomain.Unload (ad);
634 if (Directory.Exists (tempDir))
635 Directory.Delete (tempDir, true);
640 public void Load_PartialVersion ()
642 string tempDir = Path.Combine (Path.GetTempPath (),
643 "MonoTests.System.Reflection.AssemblyTest");
644 if (!Directory.Exists (tempDir))
645 Directory.CreateDirectory (tempDir);
647 AppDomain ad = CreateTestDomain (tempDir, true);
649 CrossDomainTester cdt = CreateCrossDomainTester (ad);
651 AssemblyName aname = new AssemblyName ();
652 aname.Name = "bug79715";
653 aname.Version = new Version (1, 2, 3, 4);
654 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
656 aname = new AssemblyName ();
657 aname.Name = "bug79715";
658 aname.Version = new Version (1, 2);
659 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
660 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
662 aname = new AssemblyName ();
663 aname.Name = "bug79715";
664 aname.Version = new Version (1, 2, 3);
665 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
666 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
668 aname = new AssemblyName ();
669 aname.Name = "bug79715";
670 aname.Version = new Version (1, 2, 3, 4);
671 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
672 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
674 AppDomain.Unload (ad);
675 if (Directory.Exists (tempDir))
676 Directory.Delete (tempDir, true);
680 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
682 AppDomainSetup setup = new AppDomainSetup ();
683 setup.ApplicationBase = baseDirectory;
684 setup.ApplicationName = "testdomain";
686 AppDomain ad = AppDomain.CreateDomain ("testdomain",
687 AppDomain.CurrentDomain.Evidence, setup);
689 if (assemblyResolver) {
690 Assembly ea = Assembly.GetExecutingAssembly ();
691 ad.CreateInstanceFrom (ea.CodeBase,
692 typeof (AssemblyResolveHandler).FullName,
694 BindingFlags.Public | BindingFlags.Instance,
696 new object [] { ea.Location, ea.FullName },
697 CultureInfo.InvariantCulture,
705 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
707 Type testerType = typeof (CrossDomainTester);
708 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
709 testerType.Assembly.FullName, testerType.FullName, false,
710 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
711 CultureInfo.InvariantCulture, new object[0], null);
714 private class CrossDomainTester : MarshalByRefObject
716 public void GenerateAssembly (AssemblyName aname, string path)
718 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
719 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
720 ab.Save (Path.GetFileName (path));
723 public void Load (AssemblyName assemblyRef)
725 Assembly.Load (assemblyRef);
728 public void LoadFrom (string assemblyFile)
730 Assembly.LoadFrom (assemblyFile);
733 public bool AssertLoad (AssemblyName assemblyRef)
736 Assembly.Load (assemblyRef);
743 public bool AssertLoad (string assemblyString)
746 Assembly.Load (assemblyString);
753 public bool AssertFileLoadException (AssemblyName assemblyRef)
756 Assembly.Load (assemblyRef);
758 } catch (FileLoadException) {
763 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
766 Assembly.Load (assemblyRef);
768 } catch (FileNotFoundException) {
773 public void bug78464 (string assemblyFileName)
775 AssemblyName assemblyName = new AssemblyName ();
776 assemblyName.Name = "bug78464";
778 AssemblyBuilder ab = AppDomain.CurrentDomain
779 .DefineDynamicAssembly (assemblyName,
780 AssemblyBuilderAccess.Save,
781 Path.GetDirectoryName (assemblyFileName),
782 AppDomain.CurrentDomain.Evidence);
783 ab.Save (Path.GetFileName (assemblyFileName));
787 using (FileStream fs = File.OpenRead (assemblyFileName)) {
788 byte[] buffer = new byte[fs.Length];
789 fs.Read (buffer, 0, buffer.Length);
790 assembly = Assembly.Load (buffer);
794 Assert.AreEqual (string.Empty, assembly.Location, "#1");
796 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
797 Assert.IsFalse (assembly.Location == string.Empty, "#2");
798 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
799 // note: we cannot check if directory names match, as MS.NET seems to
800 // convert directory part of assembly location to lowercase
801 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
804 public void bug78465 (string assemblyFileName)
806 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
807 Assert.IsFalse (assembly.Location == string.Empty, "#2");
808 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
809 // note: we cannot check if directory names match, as MS.NET seems to
810 // convert directory part of assembly location to lowercase
811 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
814 public void bug78468 (string assemblyFileName)
816 AssemblyName assemblyName = new AssemblyName ();
817 assemblyName.Name = "bug78468b";
819 AssemblyBuilder ab = AppDomain.CurrentDomain
820 .DefineDynamicAssembly (assemblyName,
821 AssemblyBuilderAccess.Save,
822 Path.GetDirectoryName (assemblyFileName),
823 AppDomain.CurrentDomain.Evidence);
824 ab.AddResourceFile ("read", "readme.txt");
825 ab.Save (Path.GetFileName (assemblyFileName));
827 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
828 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
829 string[] resNames = assembly.GetManifestResourceNames ();
830 Assert.IsNotNull (resNames, "#B2");
831 Assert.AreEqual (1, resNames.Length, "#B3");
832 Assert.AreEqual ("read", resNames[0], "#B4");
833 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
834 Assert.IsNotNull (resInfo, "#B5");
835 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
836 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
837 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
838 Stream s = assembly.GetManifestResourceStream ("read");
839 Assert.IsNotNull (s, "#B9");
841 s = assembly.GetFile ("readme.txt");
842 Assert.IsNotNull (s, "#B10");
848 public void bug79872 ()
850 Random rnd = new Random ();
855 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
856 if (Directory.Exists (outdir)) {
858 Directory.Delete (outdir, true);
865 Directory.CreateDirectory (outdir);
867 AssemblyName an = new AssemblyName ();
868 an.Name = "bug79872";
869 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
870 string dllname = "bug79872.dll";
871 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
872 string netmodule = "bug79872.netmodule";
873 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
874 TypeBuilder a1 = mb2.DefineType ("A");
880 Assembly.LoadFrom (Path.Combine (outdir, dllname));
884 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
888 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
889 } catch (BadImageFormatException) {
890 ok = true; // mono and .net 2.0 throw this
891 } catch (FileLoadException) {
892 ok = true; // .net 1.1 throws this
896 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
898 Directory.Delete (outdir, true);
902 private class AssemblyResolveHandler
904 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
906 _assemblyFile = assemblyFile;
907 _assemblyName = assemblyName;
909 AppDomain.CurrentDomain.AssemblyResolve +=
910 new ResolveEventHandler (ResolveAssembly);
913 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
915 if (args.Name == _assemblyName)
916 return Assembly.LoadFrom (_assemblyFile);
921 private readonly string _assemblyFile;
922 private readonly string _assemblyName;