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.Threading;
42 using System.Runtime.Serialization;
43 using System.Security;
45 namespace MonoTests.System.Reflection
48 public class AssemblyTest
50 static string TempFolder = Path.Combine (Path.GetTempPath (),
51 "MonoTests.System.Reflection.AssemblyTest");
56 while (Directory.Exists (TempFolder))
57 TempFolder = Path.Combine (TempFolder, "2");
58 Directory.CreateDirectory (TempFolder);
62 public void TearDown ()
65 // This throws an exception under MS.NET, since the directory contains loaded
67 Directory.Delete (TempFolder, true);
73 public void CreateInstance()
75 Type type = typeof (AssemblyTest);
76 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
77 Assert.IsNotNull (obj, "#01");
78 Assert.AreEqual (GetType (), obj.GetType (), "#02");
82 public void CreateInvalidInstance()
84 Type type = typeof (AssemblyTest);
85 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
86 Assert.IsNull (obj, "#03");
91 [Category ("NotWorking")]
92 [ExpectedException (typeof (ArgumentException))]
94 [ExpectedException (typeof (TypeLoadException))]
96 public void GetType_TypeName_Invalid ()
98 typeof (int).Assembly.GetType ("&blabla", true, true);
101 [Test] // bug #334203
102 public void GetType_TypeName_AssemblyName ()
104 Assembly a = typeof (int).Assembly;
105 string typeName = typeof (string).AssemblyQualifiedName;
108 a.GetType (typeName, true, false);
110 } catch (ArgumentException ex) {
111 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
112 Assert.IsNull (ex.InnerException, "#A3");
113 Assert.IsNotNull (ex.Message, "#A4");
114 Assert.IsNull (ex.ParamName, "#A5");
118 a.GetType (typeName, true, false);
120 } catch (TypeLoadException ex) {
121 Assert.AreEqual (typeof (TypeLoadException), ex.GetType (), "#A2");
122 Assert.IsNull (ex.InnerException, "#A3");
123 Assert.IsNotNull (ex.Message, "#A4");
124 Assert.IsTrue (ex.Message.IndexOf (typeName) != -1, "#A5");
128 Type type = a.GetType (typeName, false);
129 Assert.IsNull (type, "#B1");
130 type = a.GetType (typeName, false, true);
131 Assert.IsNull (type, "#B2");
135 public void GetEntryAssembly ()
137 // note: only available in default appdomain
138 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
139 // Not sure we should emulate this behavior.
140 string fname = AppDomain.CurrentDomain.FriendlyName;
141 if (fname.EndsWith (".dll")) { // nunit-console
142 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
143 #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
144 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
147 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
148 #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
149 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
154 #if !TARGET_JVM // Reflection.Emit is not supported.
156 public void GetModules_MissingFile ()
158 AssemblyName newName = new AssemblyName ();
159 newName.Name = "AssemblyTest";
161 AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
163 ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
165 ab.Save ("test_assembly.dll");
167 File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
169 Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
173 } catch (FileNotFoundException ex) {
174 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
179 #if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
181 [Category ("NotWorking")]
184 public void Corlib ()
186 Assembly corlib = typeof (int).Assembly;
187 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
188 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
189 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
190 Assert.IsNotNull (corlib.Evidence, "Evidence");
191 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
193 // corlib doesn't reference anything
194 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
196 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
197 // not really "true" but it's even more trusted so...
198 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
199 Assert.AreEqual (0, corlib.HostContext, "HostContext");
200 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
201 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
202 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
204 Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
205 Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
206 Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
211 public void Corlib_test ()
213 Assembly corlib_test = Assembly.GetExecutingAssembly ();
214 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
215 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
216 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
218 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
220 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
221 Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
222 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
223 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
225 Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
231 public void GetAssembly ()
233 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
234 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
238 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
239 public void GetFile_Null ()
242 Assembly.GetExecutingAssembly ().GetFile (null);
244 } catch (ArgumentNullException ex) {
245 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
246 Assert.IsNull (ex.InnerException, "#3");
247 Assert.IsNotNull (ex.Message, "#4");
248 Assert.IsNull (ex.ParamName, "#5");
253 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
254 public void GetFile_Empty ()
257 Assembly.GetExecutingAssembly ().GetFile (
260 } catch (ArgumentException ex) {
261 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
262 Assert.IsNull (ex.InnerException, "#3");
263 Assert.IsNotNull (ex.Message, "#4");
264 Assert.IsNull (ex.ParamName, "#5");
269 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
270 public void GetFiles_False ()
272 Assembly corlib = typeof (int).Assembly;
273 FileStream[] fss = corlib.GetFiles ();
274 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
276 Assembly corlib_test = Assembly.GetExecutingAssembly ();
277 fss = corlib_test.GetFiles ();
278 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
282 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
283 public void GetFiles_True ()
285 Assembly corlib = typeof (int).Assembly;
286 FileStream[] fss = corlib.GetFiles ();
287 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
289 Assembly corlib_test = Assembly.GetExecutingAssembly ();
290 fss = corlib_test.GetFiles ();
291 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
295 public void GetManifestResourceStream_Name_Empty ()
297 Assembly corlib = typeof (int).Assembly;
300 corlib.GetManifestResourceStream (string.Empty);
302 } catch (ArgumentException ex) {
303 // String cannot have zero length
304 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
305 Assert.IsNull (ex.InnerException, "#A3");
306 Assert.IsNotNull (ex.Message, "#A4");
309 corlib.GetManifestResourceStream (typeof (int), string.Empty);
312 corlib.GetManifestResourceStream ((Type) null, string.Empty);
314 } catch (ArgumentException ex) {
315 // String cannot have zero length
316 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
317 Assert.IsNull (ex.InnerException, "#B3");
318 Assert.IsNotNull (ex.Message, "#B4");
323 public void GetManifestResourceStream_Name_Null ()
325 Assembly corlib = typeof (int).Assembly;
328 corlib.GetManifestResourceStream ((string) null);
330 } catch (ArgumentNullException ex) {
331 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
332 Assert.IsNull (ex.InnerException, "#A3");
333 Assert.IsNotNull (ex.Message, "#A4");
336 corlib.GetManifestResourceStream (typeof (int), (string) null);
339 corlib.GetManifestResourceStream ((Type) null, (string) null);
341 } catch (ArgumentNullException ex) {
342 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
343 Assert.IsNull (ex.InnerException, "#B3");
344 Assert.IsNotNull (ex.Message, "#B4");
345 Assert.IsNotNull (ex.ParamName, "#B5");
346 Assert.AreEqual ("type", ex.ParamName, "#B6");
351 public void IsDefined_AttributeType_Null ()
353 Assembly corlib = typeof (int).Assembly;
356 corlib.IsDefined ((Type) null, false);
358 } catch (ArgumentNullException ex) {
359 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
360 Assert.IsNull (ex.InnerException, "#3");
361 Assert.IsNotNull (ex.Message, "#4");
362 Assert.IsNotNull (ex.ParamName, "#5");
363 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
369 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
371 public void LoadFrom_Empty_Assembly ()
373 string tempFile = Path.GetTempFileName ();
376 Assembly.LoadFrom (tempFile);
378 } catch (BadImageFormatException ex) {
379 Assert.IsNull (ex.InnerException, "#2");
381 File.Delete (tempFile);
386 public void LoadFrom_Invalid_Assembly ()
388 string tempFile = Path.GetTempFileName ();
389 using (StreamWriter sw = File.CreateText (tempFile)) {
390 sw.WriteLine ("foo");
395 Assembly.LoadFrom (tempFile);
397 } catch (BadImageFormatException ex) {
398 Assert.IsNull (ex.InnerException, "#2");
400 File.Delete (tempFile);
405 public void LoadFrom_NonExisting_Assembly ()
407 string tempFile = Path.GetTempFileName ();
408 File.Delete (tempFile);
411 Assembly.LoadFrom (tempFile);
413 } catch (FileNotFoundException ex) {
414 Assert.IsNull (ex.InnerException, "#2");
416 File.Delete (tempFile);
421 public void LoadWithPartialName ()
423 string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_plattest" };
425 foreach (string s in names)
426 if (Assembly.LoadWithPartialName (s) != null)
428 Assertion.Fail ("Was not able to load any corlib test");
431 #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
433 public void GetObjectData_Info_Null ()
435 Assembly corlib = typeof (int).Assembly;
437 corlib.GetObjectData (null, new StreamingContext (
438 StreamingContextStates.All));
440 } catch (ArgumentNullException ex) {
441 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
442 Assert.IsNull (ex.InnerException, "#3");
443 Assert.IsNotNull (ex.Message, "#4");
444 Assert.IsNotNull (ex.ParamName, "#5");
445 Assert.AreEqual ("info", ex.ParamName, "#6");
451 public void GetReferencedAssemblies ()
453 Assembly corlib_test = Assembly.GetExecutingAssembly ();
454 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
455 foreach (AssemblyName an in names) {
456 Assert.IsNull (an.CodeBase, "CodeBase");
457 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
458 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
459 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
460 Assert.IsNotNull (an.FullName, "FullName");
461 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
462 Assert.IsNull (an.KeyPair, "KeyPair");
463 Assert.IsNotNull (an.Name, "Name");
464 Assert.IsNotNull (an.Version, "Version");
465 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
466 an.VersionCompatibility, "VersionCompatibility");
470 #if !TARGET_JVM // Reflection.Emit is not supported.
472 public void Location_Empty() {
473 string assemblyFileName = Path.Combine (
474 Path.GetTempPath (), "AssemblyLocation.dll");
477 AssemblyName assemblyName = new AssemblyName ();
478 assemblyName.Name = "AssemblyLocation";
480 AssemblyBuilder ab = AppDomain.CurrentDomain
481 .DefineDynamicAssembly (assemblyName,
482 AssemblyBuilderAccess.Save,
484 AppDomain.CurrentDomain.Evidence);
485 ab.Save (Path.GetFileName (assemblyFileName));
487 using (FileStream fs = File.OpenRead (assemblyFileName)) {
488 byte[] buffer = new byte[fs.Length];
489 fs.Read (buffer, 0, buffer.Length);
490 Assembly assembly = Assembly.Load (buffer);
491 Assert.AreEqual (string.Empty, assembly.Location);
495 File.Delete (assemblyFileName);
500 [Category ("NotWorking")]
501 public void bug78464 ()
503 string assemblyFileName = Path.Combine (
504 Path.GetTempPath (), "bug78464.dll");
507 // execute test in separate appdomain to allow assembly to be unloaded
508 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
509 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
511 crossDomainTester.bug78464 (assemblyFileName);
513 AppDomain.Unload (testDomain);
516 File.Delete (assemblyFileName);
521 public void bug78465 ()
523 string assemblyFileName = Path.Combine (
524 Path.GetTempPath (), "bug78465.dll");
527 AssemblyName assemblyName = new AssemblyName ();
528 assemblyName.Name = "bug78465";
530 AssemblyBuilder ab = AppDomain.CurrentDomain
531 .DefineDynamicAssembly (assemblyName,
532 AssemblyBuilderAccess.Save,
533 Path.GetDirectoryName (assemblyFileName),
534 AppDomain.CurrentDomain.Evidence);
535 ab.Save (Path.GetFileName (assemblyFileName));
537 using (FileStream fs = File.OpenRead (assemblyFileName)) {
538 byte[] buffer = new byte[fs.Length];
539 fs.Read (buffer, 0, buffer.Length);
540 Assembly assembly = Assembly.Load (buffer);
541 Assert.AreEqual (string.Empty, assembly.Location, "#1");
545 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
546 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
548 crossDomainTester.bug78465 (assemblyFileName);
550 AppDomain.Unload (testDomain);
553 File.Delete (assemblyFileName);
558 public void bug78468 ()
560 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
562 string resourceFileName = Path.Combine (Path.GetTempPath (),
565 using (StreamWriter sw = File.CreateText (resourceFileName)) {
566 sw.WriteLine ("FOO");
571 AssemblyName assemblyName = new AssemblyName ();
572 assemblyName.Name = "bug78468a";
574 AssemblyBuilder ab = AppDomain.CurrentDomain
575 .DefineDynamicAssembly (assemblyName,
576 AssemblyBuilderAccess.Save,
578 AppDomain.CurrentDomain.Evidence);
579 ab.AddResourceFile ("read", "readme.txt");
580 ab.Save (Path.GetFileName (assemblyFileNameA));
584 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
585 byte[] buffer = new byte[fs.Length];
586 fs.Read (buffer, 0, buffer.Length);
587 assembly = Assembly.Load (buffer);
591 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
592 string[] resNames = assembly.GetManifestResourceNames ();
593 Assert.IsNotNull (resNames, "#A2");
594 Assert.AreEqual (1, resNames.Length, "#A3");
595 Assert.AreEqual ("read", resNames[0], "#A4");
596 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
597 Assert.IsNotNull (resInfo, "#A5");
598 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
599 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
600 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
603 assembly.GetManifestResourceStream ("read");
605 } catch (FileNotFoundException) {
608 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
611 assembly.GetFile ("readme.txt");
612 Assert.Fail ("#A10");
613 } catch (FileNotFoundException) {
616 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
619 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
620 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
622 crossDomainTester.bug78468 (assemblyFileNameB);
624 AppDomain.Unload (testDomain);
625 File.Delete (assemblyFileNameB);
628 File.Delete (assemblyFileNameA);
629 File.Delete (resourceFileName);
635 [Category ("NotWorking")]
636 public void ReflectionOnlyLoad ()
638 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
640 Assert.IsNotNull (assembly);
641 Assert.IsTrue (assembly.ReflectionOnly);
645 public void ReflectionOnlyLoadFrom ()
647 string loc = typeof (AssemblyTest).Assembly.Location;
648 string filename = Path.GetFileName (loc);
649 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
651 Assert.IsNotNull (assembly);
652 Assert.IsTrue (assembly.ReflectionOnly);
656 [ExpectedException (typeof (ArgumentException))]
657 public void CreateInstanceOnRefOnly ()
659 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
660 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
665 [Category ("NotWorking")] // patch for bug #79720 must be committed first
666 public void Load_Culture ()
668 string tempDir = Path.Combine (Path.GetTempPath (),
669 "MonoTests.System.Reflection.AssemblyTest");
670 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
671 if (!Directory.Exists (cultureTempDir))
672 Directory.CreateDirectory (cultureTempDir);
673 cultureTempDir = Path.Combine (tempDir, "en-US");
674 if (!Directory.Exists (cultureTempDir))
675 Directory.CreateDirectory (cultureTempDir);
678 AppDomain ad = CreateTestDomain (tempDir, true);
680 CrossDomainTester cdt = CreateCrossDomainTester (ad);
684 AssemblyName aname = new AssemblyName ();
685 aname.Name = "culturea";
686 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
688 aname = new AssemblyName ();
689 aname.Name = "culturea";
690 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
692 aname = new AssemblyName ();
693 aname.Name = "culturea";
694 aname.CultureInfo = new CultureInfo ("nl-BE");
695 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
697 aname = new AssemblyName ();
698 aname.Name = "culturea";
699 aname.CultureInfo = CultureInfo.InvariantCulture;
700 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
704 aname = new AssemblyName ();
705 aname.Name = "cultureb";
706 aname.CultureInfo = new CultureInfo ("nl-BE");
707 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
709 aname = new AssemblyName ();
710 aname.Name = "cultureb";
711 aname.CultureInfo = new CultureInfo ("nl-BE");
712 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
714 aname = new AssemblyName ();
715 aname.Name = "cultureb";
716 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
718 aname = new AssemblyName ();
719 aname.Name = "cultureb";
720 aname.CultureInfo = new CultureInfo ("en-US");
721 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
725 aname = new AssemblyName ();
726 aname.Name = "culturec";
727 aname.CultureInfo = new CultureInfo ("nl-BE");
728 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
730 aname = new AssemblyName ();
731 aname.Name = "culturec";
732 aname.CultureInfo = new CultureInfo ("nl-BE");
733 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
735 aname = new AssemblyName ();
736 aname.Name = "culturec";
737 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
739 aname = new AssemblyName ();
740 aname.Name = "culturec";
741 aname.CultureInfo = CultureInfo.InvariantCulture;
742 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
746 aname = new AssemblyName ();
747 aname.Name = "cultured";
748 aname.CultureInfo = new CultureInfo ("nl-BE");
749 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
751 aname = new AssemblyName ();
752 aname.Name = "cultured";
753 aname.CultureInfo = new CultureInfo ("nl-BE");
754 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
756 aname = new AssemblyName ();
757 aname.Name = "cultured";
758 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
760 aname = new AssemblyName ();
761 aname.Name = "cultured";
762 aname.CultureInfo = CultureInfo.InvariantCulture;
763 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
765 AppDomain.Unload (ad);
766 if (Directory.Exists (tempDir))
767 Directory.Delete (tempDir, true);
773 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
775 [Category ("NotWorking")]
777 public void Load_Culture_Mismatch ()
779 string tempDir = Path.Combine (Path.GetTempPath (),
780 "MonoTests.System.Reflection.AssemblyTest");
781 string cultureTempDir = Path.Combine (tempDir, "en-US");
782 if (!Directory.Exists (cultureTempDir))
783 Directory.CreateDirectory (cultureTempDir);
785 AppDomain ad = CreateTestDomain (tempDir, true);
787 CrossDomainTester cdt = CreateCrossDomainTester (ad);
791 AssemblyName aname = new AssemblyName ();
792 aname.Name = "bug79712a";
793 aname.CultureInfo = new CultureInfo ("nl-BE");
794 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
796 aname = new AssemblyName ();
797 aname.Name = "bug79712a";
798 aname.CultureInfo = CultureInfo.InvariantCulture;
800 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
802 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
807 aname = new AssemblyName ();
808 aname.Name = "bug79712b";
809 aname.CultureInfo = new CultureInfo ("nl-BE");
810 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
812 aname = new AssemblyName ();
813 aname.Name = "bug79712b";
814 aname.CultureInfo = new CultureInfo ("en-US");
816 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
818 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
821 AppDomain.Unload (ad);
822 if (Directory.Exists (tempDir))
823 Directory.Delete (tempDir, true);
829 public void Load_PartialVersion ()
831 string tempDir = Path.Combine (Path.GetTempPath (),
832 "MonoTests.System.Reflection.AssemblyTest");
833 if (!Directory.Exists (tempDir))
834 Directory.CreateDirectory (tempDir);
836 AppDomain ad = CreateTestDomain (tempDir, true);
838 CrossDomainTester cdt = CreateCrossDomainTester (ad);
840 AssemblyName aname = new AssemblyName ();
841 aname.Name = "bug79715";
842 aname.Version = new Version (1, 2, 3, 4);
843 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
845 aname = new AssemblyName ();
846 aname.Name = "bug79715";
847 aname.Version = new Version (1, 2);
848 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
849 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
851 aname = new AssemblyName ();
852 aname.Name = "bug79715";
853 aname.Version = new Version (1, 2, 3);
854 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
855 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
857 aname = new AssemblyName ();
858 aname.Name = "bug79715";
859 aname.Version = new Version (1, 2, 3, 4);
860 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
861 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
863 AppDomain.Unload (ad);
864 if (Directory.Exists (tempDir))
865 Directory.Delete (tempDir, true);
869 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
871 AppDomainSetup setup = new AppDomainSetup ();
872 setup.ApplicationBase = baseDirectory;
873 setup.ApplicationName = "testdomain";
875 AppDomain ad = AppDomain.CreateDomain ("testdomain",
876 AppDomain.CurrentDomain.Evidence, setup);
878 if (assemblyResolver) {
879 Assembly ea = Assembly.GetExecutingAssembly ();
880 ad.CreateInstanceFrom (ea.CodeBase,
881 typeof (AssemblyResolveHandler).FullName,
883 BindingFlags.Public | BindingFlags.Instance,
885 new object [] { ea.Location, ea.FullName },
886 CultureInfo.InvariantCulture,
894 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
896 Type testerType = typeof (CrossDomainTester);
897 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
898 testerType.Assembly.FullName, testerType.FullName, false,
899 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
900 CultureInfo.InvariantCulture, new object[0], null);
903 private class CrossDomainTester : MarshalByRefObject
905 public void GenerateAssembly (AssemblyName aname, string path)
907 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
908 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
909 ab.Save (Path.GetFileName (path));
912 public void Load (AssemblyName assemblyRef)
914 Assembly.Load (assemblyRef);
917 public void LoadFrom (string assemblyFile)
919 Assembly.LoadFrom (assemblyFile);
922 public bool AssertLoad (AssemblyName assemblyRef)
925 Assembly.Load (assemblyRef);
932 public bool AssertLoad (string assemblyString)
935 Assembly.Load (assemblyString);
942 public bool AssertFileLoadException (AssemblyName assemblyRef)
945 Assembly.Load (assemblyRef);
947 } catch (FileLoadException) {
952 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
955 Assembly.Load (assemblyRef);
957 } catch (FileNotFoundException) {
962 public void bug78464 (string assemblyFileName)
964 AssemblyName assemblyName = new AssemblyName ();
965 assemblyName.Name = "bug78464";
967 AssemblyBuilder ab = AppDomain.CurrentDomain
968 .DefineDynamicAssembly (assemblyName,
969 AssemblyBuilderAccess.Save,
970 Path.GetDirectoryName (assemblyFileName),
971 AppDomain.CurrentDomain.Evidence);
972 ab.Save (Path.GetFileName (assemblyFileName));
976 using (FileStream fs = File.OpenRead (assemblyFileName)) {
977 byte[] buffer = new byte[fs.Length];
978 fs.Read (buffer, 0, buffer.Length);
979 assembly = Assembly.Load (buffer);
983 Assert.AreEqual (string.Empty, assembly.Location, "#1");
985 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
986 Assert.IsFalse (assembly.Location == string.Empty, "#2");
987 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
988 // note: we cannot check if directory names match, as MS.NET seems to
989 // convert directory part of assembly location to lowercase
990 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
993 public void bug78465 (string assemblyFileName)
995 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
996 Assert.IsFalse (assembly.Location == string.Empty, "#2");
997 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
998 // note: we cannot check if directory names match, as MS.NET seems to
999 // convert directory part of assembly location to lowercase
1000 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1003 public void bug78468 (string assemblyFileName)
1005 AssemblyName assemblyName = new AssemblyName ();
1006 assemblyName.Name = "bug78468b";
1008 AssemblyBuilder ab = AppDomain.CurrentDomain
1009 .DefineDynamicAssembly (assemblyName,
1010 AssemblyBuilderAccess.Save,
1011 Path.GetDirectoryName (assemblyFileName),
1012 AppDomain.CurrentDomain.Evidence);
1013 ab.AddResourceFile ("read", "readme.txt");
1014 ab.Save (Path.GetFileName (assemblyFileName));
1016 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1017 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1018 string[] resNames = assembly.GetManifestResourceNames ();
1019 Assert.IsNotNull (resNames, "#B2");
1020 Assert.AreEqual (1, resNames.Length, "#B3");
1021 Assert.AreEqual ("read", resNames[0], "#B4");
1022 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1023 Assert.IsNotNull (resInfo, "#B5");
1024 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1025 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1026 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1027 Stream s = assembly.GetManifestResourceStream ("read");
1028 Assert.IsNotNull (s, "#B9");
1030 s = assembly.GetFile ("readme.txt");
1031 Assert.IsNotNull (s, "#B10");
1037 public void bug79872 ()
1039 Random rnd = new Random ();
1044 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1045 if (Directory.Exists (outdir)) {
1047 Directory.Delete (outdir, true);
1054 Directory.CreateDirectory (outdir);
1056 AssemblyName an = new AssemblyName ();
1057 an.Name = "bug79872";
1058 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1059 string dllname = "bug79872.dll";
1060 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1061 string netmodule = "bug79872.netmodule";
1062 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1063 TypeBuilder a1 = mb2.DefineType ("A");
1069 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1073 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1077 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1078 } catch (BadImageFormatException) {
1079 ok = true; // mono and .net 2.0 throw this
1080 } catch (FileLoadException) {
1081 ok = true; // .net 1.1 throws this
1085 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1087 Directory.Delete (outdir, true);
1089 #endif // TARGET_JVM
1093 public void ManifestModule ()
1095 Assembly assembly = typeof (int).Assembly;
1096 Module module = assembly.ManifestModule;
1097 Assert.IsNotNull (module, "#1");
1098 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1099 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1100 Assert.IsFalse (module.IsResource (), "#4");
1101 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1102 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1103 Assert.AreSame (module, assembly.ManifestModule, "#7");
1108 private class AssemblyResolveHandler
1110 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1112 _assemblyFile = assemblyFile;
1113 _assemblyName = assemblyName;
1115 AppDomain.CurrentDomain.AssemblyResolve +=
1116 new ResolveEventHandler (ResolveAssembly);
1119 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1121 if (args.Name == _assemblyName)
1122 return Assembly.LoadFrom (_assemblyFile);
1127 private readonly string _assemblyFile;
1128 private readonly string _assemblyName;
1131 protected internal class Bug328812_NestedFamORAssem { };
1134 public void bug328812 ()
1136 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1137 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1138 // Just a sanity check, in case the above passed for some other reason
1139 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1143 public void GetCustomAttributes_AttributeType_Null ()
1145 Assembly a = typeof (int).Assembly;
1147 a.GetCustomAttributes (null, false);
1149 } catch (ArgumentNullException ex) {
1150 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1151 Assert.IsNull (ex.InnerException, "#3");
1152 Assert.IsNotNull (ex.Message, "#4");
1153 Assert.IsNotNull (ex.ParamName, "#5");
1154 Assert.AreEqual ("attributeType", ex.ParamName, "#6");