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 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
159 [ExpectedException (typeof (ArgumentNullException))]
160 public void GetFile_Null ()
162 Assembly.GetExecutingAssembly ().GetFile (null);
166 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
167 [ExpectedException (typeof (ArgumentException))]
168 public void GetFile_Empty ()
170 Assembly.GetExecutingAssembly ().GetFile (String.Empty);
174 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
175 public void GetFiles_False ()
177 Assembly corlib = typeof (int).Assembly;
178 FileStream[] fss = corlib.GetFiles ();
179 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
181 Assembly corlib_test = Assembly.GetExecutingAssembly ();
182 fss = corlib_test.GetFiles ();
183 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
187 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
188 public void GetFiles_True ()
190 Assembly corlib = typeof (int).Assembly;
191 FileStream[] fss = corlib.GetFiles ();
192 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
194 Assembly corlib_test = Assembly.GetExecutingAssembly ();
195 fss = corlib_test.GetFiles ();
196 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
201 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
203 public void LoadFrom_Empty_Assembly ()
205 string tempFile = Path.GetTempFileName ();
208 Assembly.LoadFrom (tempFile);
210 } catch (BadImageFormatException ex) {
211 Assert.IsNull (ex.InnerException, "#2");
213 File.Delete (tempFile);
218 public void LoadFrom_Invalid_Assembly ()
220 string tempFile = Path.GetTempFileName ();
221 using (StreamWriter sw = File.CreateText (tempFile)) {
222 sw.WriteLine ("foo");
227 Assembly.LoadFrom (tempFile);
229 } catch (BadImageFormatException ex) {
230 Assert.IsNull (ex.InnerException, "#2");
232 File.Delete (tempFile);
237 public void LoadFrom_NonExisting_Assembly ()
239 string tempFile = Path.GetTempFileName ();
240 File.Delete (tempFile);
243 Assembly.LoadFrom (tempFile);
245 } catch (FileNotFoundException ex) {
246 Assert.IsNull (ex.InnerException, "#2");
248 File.Delete (tempFile);
253 public void LoadWithPartialName ()
255 string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
257 foreach (string s in names)
258 if (Assembly.LoadWithPartialName (s) != null)
260 Assertion.Fail ("Was not able to load any corlib test");
263 #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
265 [ExpectedException (typeof (ArgumentNullException))]
266 public void GetObjectData_Null ()
268 Assembly corlib = typeof (int).Assembly;
269 corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
274 public void GetReferencedAssemblies ()
276 Assembly corlib_test = Assembly.GetExecutingAssembly ();
277 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
278 foreach (AssemblyName an in names) {
279 Assert.IsNull (an.CodeBase, "CodeBase");
280 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
281 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
282 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
283 Assert.IsNotNull (an.FullName, "FullName");
284 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
285 Assert.IsNull (an.KeyPair, "KeyPair");
286 Assert.IsNotNull (an.Name, "Name");
287 Assert.IsNotNull (an.Version, "Version");
288 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
289 an.VersionCompatibility, "VersionCompatibility");
293 #if !TARGET_JVM // Reflection.Emit is not supported.
295 public void Location_Empty() {
296 string assemblyFileName = Path.Combine (
297 Path.GetTempPath (), "AssemblyLocation.dll");
300 AssemblyName assemblyName = new AssemblyName ();
301 assemblyName.Name = "AssemblyLocation";
303 AssemblyBuilder ab = AppDomain.CurrentDomain
304 .DefineDynamicAssembly (assemblyName,
305 AssemblyBuilderAccess.Save,
307 AppDomain.CurrentDomain.Evidence);
308 ab.Save (Path.GetFileName (assemblyFileName));
310 using (FileStream fs = File.OpenRead (assemblyFileName)) {
311 byte[] buffer = new byte[fs.Length];
312 fs.Read (buffer, 0, buffer.Length);
313 Assembly assembly = Assembly.Load (buffer);
314 Assert.AreEqual (string.Empty, assembly.Location);
318 File.Delete (assemblyFileName);
323 [Category ("NotWorking")]
324 public void bug78464 ()
326 string assemblyFileName = Path.Combine (
327 Path.GetTempPath (), "bug78464.dll");
330 // execute test in separate appdomain to allow assembly to be unloaded
331 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
332 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
334 crossDomainTester.bug78464 (assemblyFileName);
336 AppDomain.Unload (testDomain);
339 File.Delete (assemblyFileName);
344 [Category ("NotWorking")]
345 public void bug78465 ()
347 string assemblyFileName = Path.Combine (
348 Path.GetTempPath (), "bug78465.dll");
351 AssemblyName assemblyName = new AssemblyName ();
352 assemblyName.Name = "bug78465";
354 AssemblyBuilder ab = AppDomain.CurrentDomain
355 .DefineDynamicAssembly (assemblyName,
356 AssemblyBuilderAccess.Save,
357 Path.GetDirectoryName (assemblyFileName),
358 AppDomain.CurrentDomain.Evidence);
359 ab.Save (Path.GetFileName (assemblyFileName));
361 using (FileStream fs = File.OpenRead (assemblyFileName)) {
362 byte[] buffer = new byte[fs.Length];
363 fs.Read (buffer, 0, buffer.Length);
364 Assembly assembly = Assembly.Load (buffer);
365 Assert.AreEqual (string.Empty, assembly.Location, "#1");
369 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
370 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
372 crossDomainTester.bug78465 (assemblyFileName);
374 AppDomain.Unload (testDomain);
377 File.Delete (assemblyFileName);
382 public void bug78468 ()
384 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
386 string resourceFileName = Path.Combine (Path.GetTempPath (),
389 using (StreamWriter sw = File.CreateText (resourceFileName)) {
390 sw.WriteLine ("FOO");
395 AssemblyName assemblyName = new AssemblyName ();
396 assemblyName.Name = "bug78468a";
398 AssemblyBuilder ab = AppDomain.CurrentDomain
399 .DefineDynamicAssembly (assemblyName,
400 AssemblyBuilderAccess.Save,
402 AppDomain.CurrentDomain.Evidence);
403 ab.AddResourceFile ("read", "readme.txt");
404 ab.Save (Path.GetFileName (assemblyFileNameA));
408 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
409 byte[] buffer = new byte[fs.Length];
410 fs.Read (buffer, 0, buffer.Length);
411 assembly = Assembly.Load (buffer);
415 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
416 string[] resNames = assembly.GetManifestResourceNames ();
417 Assert.IsNotNull (resNames, "#A2");
418 Assert.AreEqual (1, resNames.Length, "#A3");
419 Assert.AreEqual ("read", resNames[0], "#A4");
420 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
421 Assert.IsNotNull (resInfo, "#A5");
422 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
423 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
424 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
427 assembly.GetManifestResourceStream ("read");
429 } catch (FileNotFoundException) {
432 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
435 assembly.GetFile ("readme.txt");
436 Assert.Fail ("#A10");
437 } catch (FileNotFoundException) {
440 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
443 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
444 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
446 crossDomainTester.bug78468 (assemblyFileNameB);
448 AppDomain.Unload (testDomain);
449 File.Delete (assemblyFileNameB);
452 File.Delete (assemblyFileNameA);
453 File.Delete (resourceFileName);
459 [Category ("NotWorking")]
460 public void ReflectionOnlyLoad ()
462 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
464 Assert.IsNotNull (assembly);
465 Assert.IsTrue (assembly.ReflectionOnly);
469 public void ReflectionOnlyLoadFrom ()
471 string loc = typeof (AssemblyTest).Assembly.Location;
472 string filename = Path.GetFileName (loc);
473 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
475 Assert.IsNotNull (assembly);
476 Assert.IsTrue (assembly.ReflectionOnly);
480 [ExpectedException (typeof (ArgumentException))]
481 public void CreateInstanceOnRefOnly ()
483 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
484 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
489 [Category ("NotWorking")] // patch for bug #79720 must be committed first
490 public void Load_Culture ()
492 string tempDir = Path.Combine (Path.GetTempPath (),
493 "MonoTests.System.Reflection.AssemblyTest");
494 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
495 if (!Directory.Exists (cultureTempDir))
496 Directory.CreateDirectory (cultureTempDir);
497 cultureTempDir = Path.Combine (tempDir, "en-US");
498 if (!Directory.Exists (cultureTempDir))
499 Directory.CreateDirectory (cultureTempDir);
502 AppDomain ad = CreateTestDomain (tempDir, true);
504 CrossDomainTester cdt = CreateCrossDomainTester (ad);
508 AssemblyName aname = new AssemblyName ();
509 aname.Name = "culturea";
510 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
512 aname = new AssemblyName ();
513 aname.Name = "culturea";
514 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
516 aname = new AssemblyName ();
517 aname.Name = "culturea";
518 aname.CultureInfo = new CultureInfo ("nl-BE");
519 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
521 aname = new AssemblyName ();
522 aname.Name = "culturea";
523 aname.CultureInfo = CultureInfo.InvariantCulture;
524 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
528 aname = new AssemblyName ();
529 aname.Name = "cultureb";
530 aname.CultureInfo = new CultureInfo ("nl-BE");
531 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
533 aname = new AssemblyName ();
534 aname.Name = "cultureb";
535 aname.CultureInfo = new CultureInfo ("nl-BE");
536 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
538 aname = new AssemblyName ();
539 aname.Name = "cultureb";
540 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
542 aname = new AssemblyName ();
543 aname.Name = "cultureb";
544 aname.CultureInfo = new CultureInfo ("en-US");
545 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
549 aname = new AssemblyName ();
550 aname.Name = "culturec";
551 aname.CultureInfo = new CultureInfo ("nl-BE");
552 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
554 aname = new AssemblyName ();
555 aname.Name = "culturec";
556 aname.CultureInfo = new CultureInfo ("nl-BE");
557 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
559 aname = new AssemblyName ();
560 aname.Name = "culturec";
561 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
563 aname = new AssemblyName ();
564 aname.Name = "culturec";
565 aname.CultureInfo = CultureInfo.InvariantCulture;
566 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
570 aname = new AssemblyName ();
571 aname.Name = "cultured";
572 aname.CultureInfo = new CultureInfo ("nl-BE");
573 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
575 aname = new AssemblyName ();
576 aname.Name = "cultured";
577 aname.CultureInfo = new CultureInfo ("nl-BE");
578 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
580 aname = new AssemblyName ();
581 aname.Name = "cultured";
582 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
584 aname = new AssemblyName ();
585 aname.Name = "cultured";
586 aname.CultureInfo = CultureInfo.InvariantCulture;
587 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
589 AppDomain.Unload (ad);
590 if (Directory.Exists (tempDir))
591 Directory.Delete (tempDir, true);
597 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
599 [Category ("NotWorking")]
601 public void Load_Culture_Mismatch ()
603 string tempDir = Path.Combine (Path.GetTempPath (),
604 "MonoTests.System.Reflection.AssemblyTest");
605 string cultureTempDir = Path.Combine (tempDir, "en-US");
606 if (!Directory.Exists (cultureTempDir))
607 Directory.CreateDirectory (cultureTempDir);
609 AppDomain ad = CreateTestDomain (tempDir, true);
611 CrossDomainTester cdt = CreateCrossDomainTester (ad);
615 AssemblyName aname = new AssemblyName ();
616 aname.Name = "bug79712a";
617 aname.CultureInfo = new CultureInfo ("nl-BE");
618 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
620 aname = new AssemblyName ();
621 aname.Name = "bug79712a";
622 aname.CultureInfo = CultureInfo.InvariantCulture;
624 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
626 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
631 aname = new AssemblyName ();
632 aname.Name = "bug79712b";
633 aname.CultureInfo = new CultureInfo ("nl-BE");
634 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
636 aname = new AssemblyName ();
637 aname.Name = "bug79712b";
638 aname.CultureInfo = new CultureInfo ("en-US");
640 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
642 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
645 AppDomain.Unload (ad);
646 if (Directory.Exists (tempDir))
647 Directory.Delete (tempDir, true);
652 public void Load_PartialVersion ()
654 string tempDir = Path.Combine (Path.GetTempPath (),
655 "MonoTests.System.Reflection.AssemblyTest");
656 if (!Directory.Exists (tempDir))
657 Directory.CreateDirectory (tempDir);
659 AppDomain ad = CreateTestDomain (tempDir, true);
661 CrossDomainTester cdt = CreateCrossDomainTester (ad);
663 AssemblyName aname = new AssemblyName ();
664 aname.Name = "bug79715";
665 aname.Version = new Version (1, 2, 3, 4);
666 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
668 aname = new AssemblyName ();
669 aname.Name = "bug79715";
670 aname.Version = new Version (1, 2);
671 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
672 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
674 aname = new AssemblyName ();
675 aname.Name = "bug79715";
676 aname.Version = new Version (1, 2, 3);
677 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
678 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
680 aname = new AssemblyName ();
681 aname.Name = "bug79715";
682 aname.Version = new Version (1, 2, 3, 4);
683 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
684 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
686 AppDomain.Unload (ad);
687 if (Directory.Exists (tempDir))
688 Directory.Delete (tempDir, true);
692 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
694 AppDomainSetup setup = new AppDomainSetup ();
695 setup.ApplicationBase = baseDirectory;
696 setup.ApplicationName = "testdomain";
698 AppDomain ad = AppDomain.CreateDomain ("testdomain",
699 AppDomain.CurrentDomain.Evidence, setup);
701 if (assemblyResolver) {
702 Assembly ea = Assembly.GetExecutingAssembly ();
703 ad.CreateInstanceFrom (ea.CodeBase,
704 typeof (AssemblyResolveHandler).FullName,
706 BindingFlags.Public | BindingFlags.Instance,
708 new object [] { ea.Location, ea.FullName },
709 CultureInfo.InvariantCulture,
717 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
719 Type testerType = typeof (CrossDomainTester);
720 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
721 testerType.Assembly.FullName, testerType.FullName, false,
722 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
723 CultureInfo.InvariantCulture, new object[0], null);
726 private class CrossDomainTester : MarshalByRefObject
728 public void GenerateAssembly (AssemblyName aname, string path)
730 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
731 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
732 ab.Save (Path.GetFileName (path));
735 public void Load (AssemblyName assemblyRef)
737 Assembly.Load (assemblyRef);
740 public void LoadFrom (string assemblyFile)
742 Assembly.LoadFrom (assemblyFile);
745 public bool AssertLoad (AssemblyName assemblyRef)
748 Assembly.Load (assemblyRef);
755 public bool AssertLoad (string assemblyString)
758 Assembly.Load (assemblyString);
765 public bool AssertFileLoadException (AssemblyName assemblyRef)
768 Assembly.Load (assemblyRef);
770 } catch (FileLoadException) {
775 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
778 Assembly.Load (assemblyRef);
780 } catch (FileNotFoundException) {
785 public void bug78464 (string assemblyFileName)
787 AssemblyName assemblyName = new AssemblyName ();
788 assemblyName.Name = "bug78464";
790 AssemblyBuilder ab = AppDomain.CurrentDomain
791 .DefineDynamicAssembly (assemblyName,
792 AssemblyBuilderAccess.Save,
793 Path.GetDirectoryName (assemblyFileName),
794 AppDomain.CurrentDomain.Evidence);
795 ab.Save (Path.GetFileName (assemblyFileName));
799 using (FileStream fs = File.OpenRead (assemblyFileName)) {
800 byte[] buffer = new byte[fs.Length];
801 fs.Read (buffer, 0, buffer.Length);
802 assembly = Assembly.Load (buffer);
806 Assert.AreEqual (string.Empty, assembly.Location, "#1");
808 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
809 Assert.IsFalse (assembly.Location == string.Empty, "#2");
810 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
811 // note: we cannot check if directory names match, as MS.NET seems to
812 // convert directory part of assembly location to lowercase
813 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
816 public void bug78465 (string assemblyFileName)
818 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
819 Assert.IsFalse (assembly.Location == string.Empty, "#2");
820 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
821 // note: we cannot check if directory names match, as MS.NET seems to
822 // convert directory part of assembly location to lowercase
823 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
826 public void bug78468 (string assemblyFileName)
828 AssemblyName assemblyName = new AssemblyName ();
829 assemblyName.Name = "bug78468b";
831 AssemblyBuilder ab = AppDomain.CurrentDomain
832 .DefineDynamicAssembly (assemblyName,
833 AssemblyBuilderAccess.Save,
834 Path.GetDirectoryName (assemblyFileName),
835 AppDomain.CurrentDomain.Evidence);
836 ab.AddResourceFile ("read", "readme.txt");
837 ab.Save (Path.GetFileName (assemblyFileName));
839 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
840 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
841 string[] resNames = assembly.GetManifestResourceNames ();
842 Assert.IsNotNull (resNames, "#B2");
843 Assert.AreEqual (1, resNames.Length, "#B3");
844 Assert.AreEqual ("read", resNames[0], "#B4");
845 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
846 Assert.IsNotNull (resInfo, "#B5");
847 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
848 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
849 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
850 Stream s = assembly.GetManifestResourceStream ("read");
851 Assert.IsNotNull (s, "#B9");
853 s = assembly.GetFile ("readme.txt");
854 Assert.IsNotNull (s, "#B10");
860 public void bug79872 ()
862 Random rnd = new Random ();
867 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
868 if (Directory.Exists (outdir)) {
870 Directory.Delete (outdir, true);
877 Directory.CreateDirectory (outdir);
879 AssemblyName an = new AssemblyName ();
880 an.Name = "bug79872";
881 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
882 string dllname = "bug79872.dll";
883 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
884 string netmodule = "bug79872.netmodule";
885 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
886 TypeBuilder a1 = mb2.DefineType ("A");
892 Assembly.LoadFrom (Path.Combine (outdir, dllname));
896 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
900 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
901 } catch (BadImageFormatException) {
902 ok = true; // mono and .net 2.0 throw this
903 } catch (FileLoadException) {
904 ok = true; // .net 1.1 throws this
908 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
910 Directory.Delete (outdir, true);
915 private class AssemblyResolveHandler
917 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
919 _assemblyFile = assemblyFile;
920 _assemblyName = assemblyName;
922 AppDomain.CurrentDomain.AssemblyResolve +=
923 new ResolveEventHandler (ResolveAssembly);
926 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
928 if (args.Name == _assemblyName)
929 return Assembly.LoadFrom (_assemblyFile);
934 private readonly string _assemblyFile;
935 private readonly string _assemblyName;