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;
39 using System.Reflection.Emit;
41 using System.Runtime.Serialization;
42 using System.Security;
44 namespace MonoTests.System.Reflection
47 public class AssemblyTest
50 public void CreateInstance()
52 Type type = typeof (AssemblyTest);
53 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
54 Assert.IsNotNull (obj, "#01");
55 Assert.AreEqual (GetType (), obj.GetType (), "#02");
59 public void CreateInvalidInstance()
61 Type type = typeof (AssemblyTest);
62 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
63 Assert.IsNull (obj, "#03");
68 [Category ("NotWorking")]
69 [ExpectedException (typeof (ArgumentException))]
71 [ExpectedException (typeof (TypeLoadException))]
73 public void TestGetType ()
76 typeof (int).Assembly.GetType ("&blabla", true, true);
80 public void GetEntryAssembly ()
82 // note: only available in default appdomain
83 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
84 // Not sure we should emulate this behavior.
85 string fname = AppDomain.CurrentDomain.FriendlyName;
86 if (fname.EndsWith (".dll")) { // nunit-console
87 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
88 #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
89 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
92 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
93 #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
94 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
99 #if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
101 [Category ("NotWorking")]
104 public void Corlib ()
106 Assembly corlib = typeof (int).Assembly;
107 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
108 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
109 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
110 Assert.IsNotNull (corlib.Evidence, "Evidence");
111 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
113 // corlib doesn't reference anything
114 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
116 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
117 // not really "true" but it's even more trusted so...
118 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
119 Assert.AreEqual (0, corlib.HostContext, "HostContext");
120 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
121 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
122 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
124 Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
125 Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
126 Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
131 public void Corlib_test ()
133 Assembly corlib_test = Assembly.GetExecutingAssembly ();
134 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
135 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
136 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
138 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
140 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
141 Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
142 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
143 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
145 Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
151 public void GetAssembly ()
153 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
154 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
158 [ExpectedException (typeof (ArgumentNullException))]
159 public void GetFile_Null ()
161 Assembly.GetExecutingAssembly ().GetFile (null);
165 [ExpectedException (typeof (ArgumentException))]
166 public void GetFile_Empty ()
168 Assembly.GetExecutingAssembly ().GetFile (String.Empty);
172 public void GetFiles_False ()
174 Assembly corlib = typeof (int).Assembly;
175 FileStream[] fss = corlib.GetFiles ();
176 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
178 Assembly corlib_test = Assembly.GetExecutingAssembly ();
179 fss = corlib_test.GetFiles ();
180 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
184 public void GetFiles_True ()
186 Assembly corlib = typeof (int).Assembly;
187 FileStream[] fss = corlib.GetFiles ();
188 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
190 Assembly corlib_test = Assembly.GetExecutingAssembly ();
191 fss = corlib_test.GetFiles ();
192 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
197 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
199 public void LoadFrom_Empty_Assembly ()
201 string tempFile = Path.GetTempFileName ();
204 Assembly.LoadFrom (tempFile);
206 } catch (BadImageFormatException ex) {
207 Assert.IsNull (ex.InnerException, "#2");
209 File.Delete (tempFile);
214 public void LoadFrom_Invalid_Assembly ()
216 string tempFile = Path.GetTempFileName ();
217 using (StreamWriter sw = File.CreateText (tempFile)) {
218 sw.WriteLine ("foo");
223 Assembly.LoadFrom (tempFile);
225 } catch (BadImageFormatException ex) {
226 Assert.IsNull (ex.InnerException, "#2");
228 File.Delete (tempFile);
233 public void LoadFrom_NonExisting_Assembly ()
235 string tempFile = Path.GetTempFileName ();
236 File.Delete (tempFile);
239 Assembly.LoadFrom (tempFile);
241 } catch (FileNotFoundException ex) {
242 Assert.IsNull (ex.InnerException, "#2");
244 File.Delete (tempFile);
249 public void LoadWithPartialName ()
251 string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
253 foreach (string s in names)
254 if (Assembly.LoadWithPartialName (s) != null)
256 Assertion.Fail ("Was not able to load any corlib test");
259 #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
261 [ExpectedException (typeof (ArgumentNullException))]
262 public void GetObjectData_Null ()
264 Assembly corlib = typeof (int).Assembly;
265 corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
270 public void GetReferencedAssemblies ()
272 Assembly corlib_test = Assembly.GetExecutingAssembly ();
273 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
274 foreach (AssemblyName an in names) {
275 Assert.IsNull (an.CodeBase, "CodeBase");
276 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
277 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
278 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
279 Assert.IsNotNull (an.FullName, "FullName");
280 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
281 Assert.IsNull (an.KeyPair, "KeyPair");
282 Assert.IsNotNull (an.Name, "Name");
283 Assert.IsNotNull (an.Version, "Version");
284 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
285 an.VersionCompatibility, "VersionCompatibility");
289 #if !TARGET_JVM // Reflection.Emit is not supported.
291 public void Location_Empty() {
292 string assemblyFileName = Path.Combine (
293 Path.GetTempPath (), "AssemblyLocation.dll");
296 AssemblyName assemblyName = new AssemblyName ();
297 assemblyName.Name = "AssemblyLocation";
299 AssemblyBuilder ab = AppDomain.CurrentDomain
300 .DefineDynamicAssembly (assemblyName,
301 AssemblyBuilderAccess.Save,
303 AppDomain.CurrentDomain.Evidence);
304 ab.Save (Path.GetFileName (assemblyFileName));
306 using (FileStream fs = File.OpenRead (assemblyFileName)) {
307 byte[] buffer = new byte[fs.Length];
308 fs.Read (buffer, 0, buffer.Length);
309 Assembly assembly = Assembly.Load (buffer);
310 Assert.AreEqual (string.Empty, assembly.Location);
314 File.Delete (assemblyFileName);
319 [Category ("NotWorking")]
320 public void bug78464 ()
322 string assemblyFileName = Path.Combine (
323 Path.GetTempPath (), "bug78464.dll");
326 // execute test in separate appdomain to allow assembly to be unloaded
327 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
328 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
330 crossDomainTester.bug78464 (assemblyFileName);
332 AppDomain.Unload (testDomain);
335 File.Delete (assemblyFileName);
340 [Category ("NotWorking")]
341 public void bug78465 ()
343 string assemblyFileName = Path.Combine (
344 Path.GetTempPath (), "bug78465.dll");
347 AssemblyName assemblyName = new AssemblyName ();
348 assemblyName.Name = "bug78465";
350 AssemblyBuilder ab = AppDomain.CurrentDomain
351 .DefineDynamicAssembly (assemblyName,
352 AssemblyBuilderAccess.Save,
353 Path.GetDirectoryName (assemblyFileName),
354 AppDomain.CurrentDomain.Evidence);
355 ab.Save (Path.GetFileName (assemblyFileName));
357 using (FileStream fs = File.OpenRead (assemblyFileName)) {
358 byte[] buffer = new byte[fs.Length];
359 fs.Read (buffer, 0, buffer.Length);
360 Assembly assembly = Assembly.Load (buffer);
361 Assert.AreEqual (string.Empty, assembly.Location, "#1");
365 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
366 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
368 crossDomainTester.bug78465 (assemblyFileName);
370 AppDomain.Unload (testDomain);
373 File.Delete (assemblyFileName);
378 public void bug78468 ()
380 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
382 string resourceFileName = Path.Combine (Path.GetTempPath (),
385 using (StreamWriter sw = File.CreateText (resourceFileName)) {
386 sw.WriteLine ("FOO");
391 AssemblyName assemblyName = new AssemblyName ();
392 assemblyName.Name = "bug78468a";
394 AssemblyBuilder ab = AppDomain.CurrentDomain
395 .DefineDynamicAssembly (assemblyName,
396 AssemblyBuilderAccess.Save,
398 AppDomain.CurrentDomain.Evidence);
399 ab.AddResourceFile ("read", "readme.txt");
400 ab.Save (Path.GetFileName (assemblyFileNameA));
404 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
405 byte[] buffer = new byte[fs.Length];
406 fs.Read (buffer, 0, buffer.Length);
407 assembly = Assembly.Load (buffer);
411 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
412 string[] resNames = assembly.GetManifestResourceNames ();
413 Assert.IsNotNull (resNames, "#A2");
414 Assert.AreEqual (1, resNames.Length, "#A3");
415 Assert.AreEqual ("read", resNames[0], "#A4");
416 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
417 Assert.IsNotNull (resInfo, "#A5");
418 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
419 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
420 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
423 assembly.GetManifestResourceStream ("read");
425 } catch (FileNotFoundException) {
428 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
431 assembly.GetFile ("readme.txt");
432 Assert.Fail ("#A10");
433 } catch (FileNotFoundException) {
436 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
439 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
440 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
442 crossDomainTester.bug78468 (assemblyFileNameB);
444 AppDomain.Unload (testDomain);
445 File.Delete (assemblyFileNameB);
448 File.Delete (assemblyFileNameA);
449 File.Delete (resourceFileName);
455 [Category ("NotWorking")]
456 public void ReflectionOnlyLoad ()
458 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
460 Assert.IsNotNull (assembly);
461 Assert.IsTrue (assembly.ReflectionOnly);
465 public void ReflectionOnlyLoadFrom ()
467 string loc = typeof (AssemblyTest).Assembly.Location;
468 string filename = Path.GetFileName (loc);
469 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
471 Assert.IsNotNull (assembly);
472 Assert.IsTrue (assembly.ReflectionOnly);
476 [ExpectedException (typeof (ArgumentException))]
477 public void CreateInstanceOnRefOnly ()
479 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
480 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
485 [Category ("NotWorking")] // patch for bug #79720 must be committed first
486 public void Load_Culture ()
488 string tempDir = Path.Combine (Path.GetTempPath (),
489 "MonoTests.System.Reflection.AssemblyTest");
490 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
491 if (!Directory.Exists (cultureTempDir))
492 Directory.CreateDirectory (cultureTempDir);
493 cultureTempDir = Path.Combine (tempDir, "en-US");
494 if (!Directory.Exists (cultureTempDir))
495 Directory.CreateDirectory (cultureTempDir);
498 AppDomain ad = CreateTestDomain (tempDir, true);
500 CrossDomainTester cdt = CreateCrossDomainTester (ad);
504 AssemblyName aname = new AssemblyName ();
505 aname.Name = "culturea";
506 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
508 aname = new AssemblyName ();
509 aname.Name = "culturea";
510 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
512 aname = new AssemblyName ();
513 aname.Name = "culturea";
514 aname.CultureInfo = new CultureInfo ("nl-BE");
515 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
517 aname = new AssemblyName ();
518 aname.Name = "culturea";
519 aname.CultureInfo = CultureInfo.InvariantCulture;
520 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
524 aname = new AssemblyName ();
525 aname.Name = "cultureb";
526 aname.CultureInfo = new CultureInfo ("nl-BE");
527 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
529 aname = new AssemblyName ();
530 aname.Name = "cultureb";
531 aname.CultureInfo = new CultureInfo ("nl-BE");
532 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
534 aname = new AssemblyName ();
535 aname.Name = "cultureb";
536 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
538 aname = new AssemblyName ();
539 aname.Name = "cultureb";
540 aname.CultureInfo = new CultureInfo ("en-US");
541 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
545 aname = new AssemblyName ();
546 aname.Name = "culturec";
547 aname.CultureInfo = new CultureInfo ("nl-BE");
548 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
550 aname = new AssemblyName ();
551 aname.Name = "culturec";
552 aname.CultureInfo = new CultureInfo ("nl-BE");
553 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
555 aname = new AssemblyName ();
556 aname.Name = "culturec";
557 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
559 aname = new AssemblyName ();
560 aname.Name = "culturec";
561 aname.CultureInfo = CultureInfo.InvariantCulture;
562 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
566 aname = new AssemblyName ();
567 aname.Name = "cultured";
568 aname.CultureInfo = new CultureInfo ("nl-BE");
569 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
571 aname = new AssemblyName ();
572 aname.Name = "cultured";
573 aname.CultureInfo = new CultureInfo ("nl-BE");
574 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
576 aname = new AssemblyName ();
577 aname.Name = "cultured";
578 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
580 aname = new AssemblyName ();
581 aname.Name = "cultured";
582 aname.CultureInfo = CultureInfo.InvariantCulture;
583 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
585 AppDomain.Unload (ad);
586 if (Directory.Exists (tempDir))
587 Directory.Delete (tempDir, true);
593 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
595 [Category ("NotWorking")]
597 public void Load_Culture_Mismatch ()
599 string tempDir = Path.Combine (Path.GetTempPath (),
600 "MonoTests.System.Reflection.AssemblyTest");
601 string cultureTempDir = Path.Combine (tempDir, "en-US");
602 if (!Directory.Exists (cultureTempDir))
603 Directory.CreateDirectory (cultureTempDir);
605 AppDomain ad = CreateTestDomain (tempDir, true);
607 CrossDomainTester cdt = CreateCrossDomainTester (ad);
611 AssemblyName aname = new AssemblyName ();
612 aname.Name = "bug79712a";
613 aname.CultureInfo = new CultureInfo ("nl-BE");
614 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
616 aname = new AssemblyName ();
617 aname.Name = "bug79712a";
618 aname.CultureInfo = CultureInfo.InvariantCulture;
620 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
622 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
627 aname = new AssemblyName ();
628 aname.Name = "bug79712b";
629 aname.CultureInfo = new CultureInfo ("nl-BE");
630 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
632 aname = new AssemblyName ();
633 aname.Name = "bug79712b";
634 aname.CultureInfo = new CultureInfo ("en-US");
636 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
638 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
641 AppDomain.Unload (ad);
642 if (Directory.Exists (tempDir))
643 Directory.Delete (tempDir, true);
648 public void Load_PartialVersion ()
650 string tempDir = Path.Combine (Path.GetTempPath (),
651 "MonoTests.System.Reflection.AssemblyTest");
652 if (!Directory.Exists (tempDir))
653 Directory.CreateDirectory (tempDir);
655 AppDomain ad = CreateTestDomain (tempDir, true);
657 CrossDomainTester cdt = CreateCrossDomainTester (ad);
659 AssemblyName aname = new AssemblyName ();
660 aname.Name = "bug79715";
661 aname.Version = new Version (1, 2, 3, 4);
662 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
664 aname = new AssemblyName ();
665 aname.Name = "bug79715";
666 aname.Version = new Version (1, 2);
667 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
668 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
670 aname = new AssemblyName ();
671 aname.Name = "bug79715";
672 aname.Version = new Version (1, 2, 3);
673 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
674 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
676 aname = new AssemblyName ();
677 aname.Name = "bug79715";
678 aname.Version = new Version (1, 2, 3, 4);
679 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
680 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
682 AppDomain.Unload (ad);
683 if (Directory.Exists (tempDir))
684 Directory.Delete (tempDir, true);
688 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
690 AppDomainSetup setup = new AppDomainSetup ();
691 setup.ApplicationBase = baseDirectory;
692 setup.ApplicationName = "testdomain";
694 AppDomain ad = AppDomain.CreateDomain ("testdomain",
695 AppDomain.CurrentDomain.Evidence, setup);
697 if (assemblyResolver) {
698 Assembly ea = Assembly.GetExecutingAssembly ();
699 ad.CreateInstanceFrom (ea.CodeBase,
700 typeof (AssemblyResolveHandler).FullName,
702 BindingFlags.Public | BindingFlags.Instance,
704 new object [] { ea.Location, ea.FullName },
705 CultureInfo.InvariantCulture,
713 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
715 Type testerType = typeof (CrossDomainTester);
716 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
717 testerType.Assembly.FullName, testerType.FullName, false,
718 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
719 CultureInfo.InvariantCulture, new object[0], null);
722 private class CrossDomainTester : MarshalByRefObject
724 public void GenerateAssembly (AssemblyName aname, string path)
726 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
727 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
728 ab.Save (Path.GetFileName (path));
731 public void Load (AssemblyName assemblyRef)
733 Assembly.Load (assemblyRef);
736 public void LoadFrom (string assemblyFile)
738 Assembly.LoadFrom (assemblyFile);
741 public bool AssertLoad (AssemblyName assemblyRef)
744 Assembly.Load (assemblyRef);
751 public bool AssertLoad (string assemblyString)
754 Assembly.Load (assemblyString);
761 public bool AssertFileLoadException (AssemblyName assemblyRef)
764 Assembly.Load (assemblyRef);
766 } catch (FileLoadException) {
771 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
774 Assembly.Load (assemblyRef);
776 } catch (FileNotFoundException) {
781 public void bug78464 (string assemblyFileName)
783 AssemblyName assemblyName = new AssemblyName ();
784 assemblyName.Name = "bug78464";
786 AssemblyBuilder ab = AppDomain.CurrentDomain
787 .DefineDynamicAssembly (assemblyName,
788 AssemblyBuilderAccess.Save,
789 Path.GetDirectoryName (assemblyFileName),
790 AppDomain.CurrentDomain.Evidence);
791 ab.Save (Path.GetFileName (assemblyFileName));
795 using (FileStream fs = File.OpenRead (assemblyFileName)) {
796 byte[] buffer = new byte[fs.Length];
797 fs.Read (buffer, 0, buffer.Length);
798 assembly = Assembly.Load (buffer);
802 Assert.AreEqual (string.Empty, assembly.Location, "#1");
804 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
805 Assert.IsFalse (assembly.Location == string.Empty, "#2");
806 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
807 // note: we cannot check if directory names match, as MS.NET seems to
808 // convert directory part of assembly location to lowercase
809 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
812 public void bug78465 (string assemblyFileName)
814 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
815 Assert.IsFalse (assembly.Location == string.Empty, "#2");
816 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
817 // note: we cannot check if directory names match, as MS.NET seems to
818 // convert directory part of assembly location to lowercase
819 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
822 public void bug78468 (string assemblyFileName)
824 AssemblyName assemblyName = new AssemblyName ();
825 assemblyName.Name = "bug78468b";
827 AssemblyBuilder ab = AppDomain.CurrentDomain
828 .DefineDynamicAssembly (assemblyName,
829 AssemblyBuilderAccess.Save,
830 Path.GetDirectoryName (assemblyFileName),
831 AppDomain.CurrentDomain.Evidence);
832 ab.AddResourceFile ("read", "readme.txt");
833 ab.Save (Path.GetFileName (assemblyFileName));
835 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
836 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
837 string[] resNames = assembly.GetManifestResourceNames ();
838 Assert.IsNotNull (resNames, "#B2");
839 Assert.AreEqual (1, resNames.Length, "#B3");
840 Assert.AreEqual ("read", resNames[0], "#B4");
841 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
842 Assert.IsNotNull (resInfo, "#B5");
843 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
844 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
845 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
846 Stream s = assembly.GetManifestResourceStream ("read");
847 Assert.IsNotNull (s, "#B9");
849 s = assembly.GetFile ("readme.txt");
850 Assert.IsNotNull (s, "#B10");
856 public void bug79872 ()
858 Random rnd = new Random ();
863 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
864 if (Directory.Exists (outdir)) {
866 Directory.Delete (outdir, true);
873 Directory.CreateDirectory (outdir);
875 AssemblyName an = new AssemblyName ();
876 an.Name = "bug79872";
877 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
878 string dllname = "bug79872.dll";
879 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
880 string netmodule = "bug79872.netmodule";
881 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
882 TypeBuilder a1 = mb2.DefineType ("A");
888 Assembly.LoadFrom (Path.Combine (outdir, dllname));
892 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
896 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
897 } catch (BadImageFormatException) {
898 ok = true; // mono and .net 2.0 throw this
899 } catch (FileLoadException) {
900 ok = true; // .net 1.1 throws this
904 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
906 Directory.Delete (outdir, true);
911 private class AssemblyResolveHandler
913 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
915 _assemblyFile = assemblyFile;
916 _assemblyName = assemblyName;
918 AppDomain.CurrentDomain.AssemblyResolve +=
919 new ResolveEventHandler (ResolveAssembly);
922 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
924 if (args.Name == _assemblyName)
925 return Assembly.LoadFrom (_assemblyFile);
930 private readonly string _assemblyFile;
931 private readonly string _assemblyName;