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 using System.Resources;
47 namespace MonoTests.System.Reflection
50 public class AssemblyTest
52 static string TempFolder = Path.Combine (Path.GetTempPath (),
53 "MonoTests.System.Reflection.AssemblyTest");
58 while (Directory.Exists (TempFolder))
59 TempFolder = Path.Combine (TempFolder, "2");
60 Directory.CreateDirectory (TempFolder);
64 public void TearDown ()
67 // This throws an exception under MS.NET, since the directory contains loaded
69 Directory.Delete (TempFolder, true);
75 public void CreateInstance()
77 Type type = typeof (AssemblyTest);
78 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
79 Assert.IsNotNull (obj, "#01");
80 Assert.AreEqual (GetType (), obj.GetType (), "#02");
84 public void CreateInvalidInstance()
86 Type type = typeof (AssemblyTest);
87 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
88 Assert.IsNull (obj, "#03");
92 [Category ("NotWorking")]
93 [ExpectedException (typeof (ArgumentException))]
94 public void GetType_TypeName_Invalid ()
96 typeof (int).Assembly.GetType ("&blabla", true, true);
100 public void GetType_TypeName_AssemblyName ()
102 Assembly a = typeof (int).Assembly;
103 string typeName = typeof (string).AssemblyQualifiedName;
105 a.GetType (typeName, true, false);
107 } catch (ArgumentException ex) {
108 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
109 Assert.IsNull (ex.InnerException, "#A3");
110 Assert.IsNotNull (ex.Message, "#A4");
111 Assert.IsNull (ex.ParamName, "#A5");
114 Type type = a.GetType (typeName, false);
115 Assert.IsNull (type, "#B1");
116 type = a.GetType (typeName, false, true);
117 Assert.IsNull (type, "#B2");
121 public void GetEntryAssembly ()
123 // note: only available in default appdomain
124 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
125 // Not sure we should emulate this behavior.
126 string fname = AppDomain.CurrentDomain.FriendlyName;
127 if (fname.EndsWith (".dll")) { // nunit-console
128 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
129 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
131 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
132 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
136 #if !MONOTOUCH // Reflection.Emit is not supported.
138 public void GetModules_MissingFile ()
140 AssemblyName newName = new AssemblyName ();
141 newName.Name = "AssemblyTest";
143 AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
145 ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
147 ab.Save ("test_assembly.dll");
149 File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
151 Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
155 } catch (FileNotFoundException ex) {
156 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
161 [Category ("NotWorking")]
163 public void Corlib ()
165 Assembly corlib = typeof (int).Assembly;
166 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
167 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
168 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
169 Assert.IsNotNull (corlib.Evidence, "Evidence");
170 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
172 // corlib doesn't reference anything
173 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
174 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
175 // not really "true" but it's even more trusted so...
176 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
177 Assert.AreEqual (0, corlib.HostContext, "HostContext");
178 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
179 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
180 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
184 public void Corlib_test ()
186 Assembly corlib_test = Assembly.GetExecutingAssembly ();
188 Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
189 Assert.IsNull (corlib_test.Evidence, "Evidence");
191 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
192 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
194 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
196 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
197 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
198 Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
200 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
201 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
205 public void GetAssembly ()
207 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
208 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
212 public void GetFile_Null ()
215 Assembly.GetExecutingAssembly ().GetFile (null);
217 } catch (ArgumentNullException ex) {
218 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
219 Assert.IsNull (ex.InnerException, "#3");
220 Assert.IsNotNull (ex.Message, "#4");
221 Assert.IsNull (ex.ParamName, "#5");
226 public void GetFile_Empty ()
229 Assembly.GetExecutingAssembly ().GetFile (
232 } catch (ArgumentException ex) {
233 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
234 Assert.IsNull (ex.InnerException, "#3");
235 Assert.IsNotNull (ex.Message, "#4");
236 Assert.IsNull (ex.ParamName, "#5");
241 public void GetFiles_False ()
243 Assembly corlib = typeof (int).Assembly;
244 FileStream[] fss = corlib.GetFiles ();
245 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
247 Assembly corlib_test = Assembly.GetExecutingAssembly ();
248 fss = corlib_test.GetFiles ();
249 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
253 public void GetFiles_True ()
255 Assembly corlib = typeof (int).Assembly;
256 FileStream[] fss = corlib.GetFiles ();
257 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
259 Assembly corlib_test = Assembly.GetExecutingAssembly ();
260 fss = corlib_test.GetFiles ();
261 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
265 public void GetManifestResourceStream_Name_Empty ()
267 Assembly corlib = typeof (int).Assembly;
270 corlib.GetManifestResourceStream (string.Empty);
272 } catch (ArgumentException ex) {
273 // String cannot have zero length
274 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
275 Assert.IsNull (ex.InnerException, "#A3");
276 Assert.IsNotNull (ex.Message, "#A4");
279 corlib.GetManifestResourceStream (typeof (int), string.Empty);
282 corlib.GetManifestResourceStream ((Type) null, string.Empty);
284 } catch (ArgumentException ex) {
285 // String cannot have zero length
286 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
287 Assert.IsNull (ex.InnerException, "#B3");
288 Assert.IsNotNull (ex.Message, "#B4");
293 public void GetManifestResourceStream_Name_Null ()
295 Assembly corlib = typeof (int).Assembly;
298 corlib.GetManifestResourceStream ((string) null);
300 } catch (ArgumentNullException ex) {
301 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
302 Assert.IsNull (ex.InnerException, "#A3");
303 Assert.IsNotNull (ex.Message, "#A4");
306 corlib.GetManifestResourceStream (typeof (int), (string) null);
309 corlib.GetManifestResourceStream ((Type) null, (string) null);
311 } catch (ArgumentNullException ex) {
312 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
313 Assert.IsNull (ex.InnerException, "#B3");
314 Assert.IsNotNull (ex.Message, "#B4");
315 Assert.IsNotNull (ex.ParamName, "#B5");
316 Assert.AreEqual ("type", ex.ParamName, "#B6");
321 public void IsDefined_AttributeType_Null ()
323 Assembly corlib = typeof (int).Assembly;
326 corlib.IsDefined ((Type) null, false);
328 } catch (ArgumentNullException ex) {
329 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
330 Assert.IsNull (ex.InnerException, "#3");
331 Assert.IsNotNull (ex.Message, "#4");
332 Assert.IsNotNull (ex.ParamName, "#5");
333 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
338 public void LoadFrom_Empty_Assembly ()
340 string tempFile = Path.GetTempFileName ();
343 Assembly.LoadFrom (tempFile);
345 } catch (BadImageFormatException ex) {
346 Assert.IsNull (ex.InnerException, "#2");
348 File.Delete (tempFile);
353 public void LoadFrom_Invalid_Assembly ()
355 string tempFile = Path.GetTempFileName ();
356 using (StreamWriter sw = File.CreateText (tempFile)) {
357 sw.WriteLine ("foo");
362 Assembly.LoadFrom (tempFile);
364 } catch (BadImageFormatException ex) {
365 Assert.IsNull (ex.InnerException, "#2");
367 File.Delete (tempFile);
372 public void LoadFrom_NonExisting_Assembly ()
374 string tempFile = Path.GetTempFileName ();
375 File.Delete (tempFile);
378 Assembly.LoadFrom (tempFile);
380 } catch (FileNotFoundException ex) {
381 Assert.IsNull (ex.InnerException, "#2");
383 File.Delete (tempFile);
388 public void LoadWithPartialName ()
390 string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_test_net_4_0", "corlib_test_net_4_5", "corlib_plattest", "mscorlibtests" };
392 foreach (string s in names)
393 if (Assembly.LoadWithPartialName (s) != null)
395 Assert.Fail ("Was not able to load any corlib test");
399 public void GetObjectData_Info_Null ()
401 Assembly corlib = typeof (int).Assembly;
403 corlib.GetObjectData (null, new StreamingContext (
404 StreamingContextStates.All));
406 } catch (ArgumentNullException ex) {
407 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
408 Assert.IsNull (ex.InnerException, "#3");
409 Assert.IsNotNull (ex.Message, "#4");
410 Assert.IsNotNull (ex.ParamName, "#5");
411 Assert.AreEqual ("info", ex.ParamName, "#6");
416 public void GetReferencedAssemblies ()
418 Assembly corlib_test = Assembly.GetExecutingAssembly ();
419 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
420 foreach (AssemblyName an in names) {
421 Assert.IsNull (an.CodeBase, "CodeBase");
422 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
423 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
424 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
425 Assert.IsNotNull (an.FullName, "FullName");
426 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
427 Assert.IsNull (an.KeyPair, "KeyPair");
428 Assert.IsNotNull (an.Name, "Name");
429 Assert.IsNotNull (an.Version, "Version");
430 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
431 an.VersionCompatibility, "VersionCompatibility");
435 #if !MONOTOUCH // Reflection.Emit is not supported.
437 public void Location_Empty() {
438 string assemblyFileName = Path.Combine (
439 Path.GetTempPath (), "AssemblyLocation.dll");
442 AssemblyName assemblyName = new AssemblyName ();
443 assemblyName.Name = "AssemblyLocation";
445 AssemblyBuilder ab = AppDomain.CurrentDomain
446 .DefineDynamicAssembly (assemblyName,
447 AssemblyBuilderAccess.Save,
449 AppDomain.CurrentDomain.Evidence);
450 ab.Save (Path.GetFileName (assemblyFileName));
452 using (FileStream fs = File.OpenRead (assemblyFileName)) {
453 byte[] buffer = new byte[fs.Length];
454 fs.Read (buffer, 0, buffer.Length);
455 Assembly assembly = Assembly.Load (buffer);
456 Assert.AreEqual (string.Empty, assembly.Location);
460 File.Delete (assemblyFileName);
465 public void SateliteAssemblyForInMemoryAssembly ()
467 string assemblyFileName = Path.Combine (
468 Path.GetTempPath (), "AssemblyLocation1.dll");
471 AssemblyName assemblyName = new AssemblyName ();
472 assemblyName.Name = "AssemblyLocation1";
474 AssemblyBuilder ab = AppDomain.CurrentDomain
475 .DefineDynamicAssembly (assemblyName,
476 AssemblyBuilderAccess.Save,
477 Path.GetTempPath ());
479 ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
480 TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
482 MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
483 ILGenerator gen = methodBuilder.GetILGenerator ();
486 // var resourceManager = new ResourceManager (typeof (Program));
487 // resourceManager.GetString ("test");
489 gen.Emit (OpCodes.Ldtoken, typeBuilder);
490 gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
491 gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
492 gen.Emit (OpCodes.Ldstr, "test");
493 gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
494 gen.Emit (OpCodes.Pop);
495 gen.Emit (OpCodes.Ret);
497 typeBuilder.CreateType ();
499 ab.Save (Path.GetFileName (assemblyFileName));
501 using (FileStream fs = File.OpenRead (assemblyFileName)) {
502 byte[] buffer = new byte[fs.Length];
503 fs.Read (buffer, 0, buffer.Length);
504 Assembly assembly = Assembly.Load (buffer);
506 var mm = assembly.GetType ("Program").GetMethod ("TestCall");
508 mm.Invoke (null, null);
510 } catch (TargetInvocationException e) {
511 Assert.IsTrue (e.InnerException is MissingManifestResourceException);
517 File.Delete (assemblyFileName);
522 [Category ("NotWorking")]
523 public void bug78464 ()
525 string assemblyFileName = Path.Combine (
526 Path.GetTempPath (), "bug78464.dll");
529 // execute test in separate appdomain to allow assembly to be unloaded
530 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
531 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
533 crossDomainTester.bug78464 (assemblyFileName);
535 AppDomain.Unload (testDomain);
538 File.Delete (assemblyFileName);
543 public void bug78465 ()
545 string assemblyFileName = Path.Combine (
546 Path.GetTempPath (), "bug78465.dll");
549 AssemblyName assemblyName = new AssemblyName ();
550 assemblyName.Name = "bug78465";
552 AssemblyBuilder ab = AppDomain.CurrentDomain
553 .DefineDynamicAssembly (assemblyName,
554 AssemblyBuilderAccess.Save,
555 Path.GetDirectoryName (assemblyFileName),
556 AppDomain.CurrentDomain.Evidence);
557 ab.Save (Path.GetFileName (assemblyFileName));
559 using (FileStream fs = File.OpenRead (assemblyFileName)) {
560 byte[] buffer = new byte[fs.Length];
561 fs.Read (buffer, 0, buffer.Length);
562 Assembly assembly = Assembly.Load (buffer);
563 Assert.AreEqual (string.Empty, assembly.Location, "#1");
567 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
568 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
570 crossDomainTester.bug78465 (assemblyFileName);
572 AppDomain.Unload (testDomain);
575 File.Delete (assemblyFileName);
580 public void bug78468 ()
582 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
584 string resourceFileName = Path.Combine (Path.GetTempPath (),
587 using (StreamWriter sw = File.CreateText (resourceFileName)) {
588 sw.WriteLine ("FOO");
593 AssemblyName assemblyName = new AssemblyName ();
594 assemblyName.Name = "bug78468a";
596 AssemblyBuilder ab = AppDomain.CurrentDomain
597 .DefineDynamicAssembly (assemblyName,
598 AssemblyBuilderAccess.Save,
600 AppDomain.CurrentDomain.Evidence);
601 ab.AddResourceFile ("read", "readme.txt");
602 ab.Save (Path.GetFileName (assemblyFileNameA));
606 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
607 byte[] buffer = new byte[fs.Length];
608 fs.Read (buffer, 0, buffer.Length);
609 assembly = Assembly.Load (buffer);
613 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
614 string[] resNames = assembly.GetManifestResourceNames ();
615 Assert.IsNotNull (resNames, "#A2");
616 Assert.AreEqual (1, resNames.Length, "#A3");
617 Assert.AreEqual ("read", resNames[0], "#A4");
618 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
619 Assert.IsNotNull (resInfo, "#A5");
620 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
621 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
622 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
624 assembly.GetManifestResourceStream ("read");
626 } catch (FileNotFoundException) {
629 assembly.GetFile ("readme.txt");
630 Assert.Fail ("#A10");
631 } catch (FileNotFoundException) {
634 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
637 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
638 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
640 crossDomainTester.bug78468 (assemblyFileNameB);
642 AppDomain.Unload (testDomain);
643 File.Delete (assemblyFileNameB);
646 File.Delete (assemblyFileNameA);
647 File.Delete (resourceFileName);
652 [Category ("NotWorking")]
653 public void ReflectionOnlyLoad ()
655 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
657 Assert.IsNotNull (assembly);
658 Assert.IsTrue (assembly.ReflectionOnly);
662 public void ReflectionOnlyLoadFrom ()
664 string loc = typeof (AssemblyTest).Assembly.Location;
665 string filename = Path.GetFileName (loc);
666 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
668 Assert.IsNotNull (assembly);
669 Assert.IsTrue (assembly.ReflectionOnly);
673 [ExpectedException (typeof (ArgumentException))]
674 public void CreateInstanceOnRefOnly ()
676 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
677 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
681 [Category ("NotWorking")] // patch for bug #79720 must be committed first
682 public void Load_Culture ()
684 string tempDir = Path.Combine (Path.GetTempPath (),
685 "MonoTests.System.Reflection.AssemblyTest");
686 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
687 if (!Directory.Exists (cultureTempDir))
688 Directory.CreateDirectory (cultureTempDir);
689 cultureTempDir = Path.Combine (tempDir, "en-US");
690 if (!Directory.Exists (cultureTempDir))
691 Directory.CreateDirectory (cultureTempDir);
694 AppDomain ad = CreateTestDomain (tempDir, true);
696 CrossDomainTester cdt = CreateCrossDomainTester (ad);
700 AssemblyName aname = new AssemblyName ();
701 aname.Name = "culturea";
702 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
704 aname = new AssemblyName ();
705 aname.Name = "culturea";
706 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
708 aname = new AssemblyName ();
709 aname.Name = "culturea";
710 aname.CultureInfo = new CultureInfo ("nl-BE");
711 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
713 aname = new AssemblyName ();
714 aname.Name = "culturea";
715 aname.CultureInfo = CultureInfo.InvariantCulture;
716 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
720 aname = new AssemblyName ();
721 aname.Name = "cultureb";
722 aname.CultureInfo = new CultureInfo ("nl-BE");
723 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
725 aname = new AssemblyName ();
726 aname.Name = "cultureb";
727 aname.CultureInfo = new CultureInfo ("nl-BE");
728 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
730 aname = new AssemblyName ();
731 aname.Name = "cultureb";
732 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
734 aname = new AssemblyName ();
735 aname.Name = "cultureb";
736 aname.CultureInfo = new CultureInfo ("en-US");
737 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
741 aname = new AssemblyName ();
742 aname.Name = "culturec";
743 aname.CultureInfo = new CultureInfo ("nl-BE");
744 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
746 aname = new AssemblyName ();
747 aname.Name = "culturec";
748 aname.CultureInfo = new CultureInfo ("nl-BE");
749 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
751 aname = new AssemblyName ();
752 aname.Name = "culturec";
753 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
755 aname = new AssemblyName ();
756 aname.Name = "culturec";
757 aname.CultureInfo = CultureInfo.InvariantCulture;
758 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
762 aname = new AssemblyName ();
763 aname.Name = "cultured";
764 aname.CultureInfo = new CultureInfo ("nl-BE");
765 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
767 aname = new AssemblyName ();
768 aname.Name = "cultured";
769 aname.CultureInfo = new CultureInfo ("nl-BE");
770 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
772 aname = new AssemblyName ();
773 aname.Name = "cultured";
774 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
776 aname = new AssemblyName ();
777 aname.Name = "cultured";
778 aname.CultureInfo = CultureInfo.InvariantCulture;
779 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
781 AppDomain.Unload (ad);
782 if (Directory.Exists (tempDir))
783 Directory.Delete (tempDir, true);
788 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
789 public void Load_Culture_Mismatch ()
791 string tempDir = Path.Combine (Path.GetTempPath (),
792 "MonoTests.System.Reflection.AssemblyTest");
793 string cultureTempDir = Path.Combine (tempDir, "en-US");
794 if (!Directory.Exists (cultureTempDir))
795 Directory.CreateDirectory (cultureTempDir);
797 AppDomain ad = CreateTestDomain (tempDir, true);
799 CrossDomainTester cdt = CreateCrossDomainTester (ad);
803 AssemblyName aname = new AssemblyName ();
804 aname.Name = "bug79712a";
805 aname.CultureInfo = new CultureInfo ("nl-BE");
806 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
808 aname = new AssemblyName ();
809 aname.Name = "bug79712a";
810 aname.CultureInfo = CultureInfo.InvariantCulture;
811 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
815 aname = new AssemblyName ();
816 aname.Name = "bug79712b";
817 aname.CultureInfo = new CultureInfo ("nl-BE");
818 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
820 aname = new AssemblyName ();
821 aname.Name = "bug79712b";
822 aname.CultureInfo = new CultureInfo ("en-US");
823 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
825 AppDomain.Unload (ad);
826 if (Directory.Exists (tempDir))
827 Directory.Delete (tempDir, true);
833 public void Load_PartialVersion ()
835 string tempDir = Path.Combine (Path.GetTempPath (),
836 "MonoTests.System.Reflection.AssemblyTest");
837 if (!Directory.Exists (tempDir))
838 Directory.CreateDirectory (tempDir);
840 AppDomain ad = CreateTestDomain (tempDir, true);
842 CrossDomainTester cdt = CreateCrossDomainTester (ad);
844 AssemblyName aname = new AssemblyName ();
845 aname.Name = "bug79715";
846 aname.Version = new Version (1, 2, 3, 4);
847 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
849 aname = new AssemblyName ();
850 aname.Name = "bug79715";
851 aname.Version = new Version (1, 2);
852 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
853 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
855 aname = new AssemblyName ();
856 aname.Name = "bug79715";
857 aname.Version = new Version (1, 2, 3);
858 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
859 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
861 aname = new AssemblyName ();
862 aname.Name = "bug79715";
863 aname.Version = new Version (1, 2, 3, 4);
864 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
865 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
867 AppDomain.Unload (ad);
868 if (Directory.Exists (tempDir))
869 Directory.Delete (tempDir, true);
873 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
875 AppDomainSetup setup = new AppDomainSetup ();
876 setup.ApplicationBase = baseDirectory;
877 setup.ApplicationName = "testdomain";
879 AppDomain ad = AppDomain.CreateDomain ("testdomain",
880 AppDomain.CurrentDomain.Evidence, setup);
882 if (assemblyResolver) {
883 Assembly ea = Assembly.GetExecutingAssembly ();
884 ad.CreateInstanceFrom (ea.CodeBase,
885 typeof (AssemblyResolveHandler).FullName,
887 BindingFlags.Public | BindingFlags.Instance,
889 new object [] { ea.Location, ea.FullName },
890 CultureInfo.InvariantCulture,
898 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
900 Type testerType = typeof (CrossDomainTester);
901 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
902 testerType.Assembly.FullName, testerType.FullName, false,
903 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
904 CultureInfo.InvariantCulture, new object[0], null);
907 private class CrossDomainTester : MarshalByRefObject
909 public void GenerateAssembly (AssemblyName aname, string path)
911 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
912 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
913 ab.Save (Path.GetFileName (path));
916 public void Load (AssemblyName assemblyRef)
918 Assembly.Load (assemblyRef);
921 public void LoadFrom (string assemblyFile)
923 Assembly.LoadFrom (assemblyFile);
926 public bool AssertLoad (AssemblyName assemblyRef)
929 Assembly.Load (assemblyRef);
936 public bool AssertLoad (string assemblyString)
939 Assembly.Load (assemblyString);
946 public bool AssertFileLoadException (AssemblyName assemblyRef)
949 Assembly.Load (assemblyRef);
951 } catch (FileLoadException) {
956 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
959 Assembly.Load (assemblyRef);
961 } catch (FileNotFoundException) {
966 public void bug78464 (string assemblyFileName)
968 AssemblyName assemblyName = new AssemblyName ();
969 assemblyName.Name = "bug78464";
971 AssemblyBuilder ab = AppDomain.CurrentDomain
972 .DefineDynamicAssembly (assemblyName,
973 AssemblyBuilderAccess.Save,
974 Path.GetDirectoryName (assemblyFileName),
975 AppDomain.CurrentDomain.Evidence);
976 ab.Save (Path.GetFileName (assemblyFileName));
980 using (FileStream fs = File.OpenRead (assemblyFileName)) {
981 byte[] buffer = new byte[fs.Length];
982 fs.Read (buffer, 0, buffer.Length);
983 assembly = Assembly.Load (buffer);
987 Assert.AreEqual (string.Empty, assembly.Location, "#1");
989 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
990 Assert.IsFalse (assembly.Location == string.Empty, "#2");
991 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
992 // note: we cannot check if directory names match, as MS.NET seems to
993 // convert directory part of assembly location to lowercase
994 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
997 public void bug78465 (string assemblyFileName)
999 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1000 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1001 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1002 // note: we cannot check if directory names match, as MS.NET seems to
1003 // convert directory part of assembly location to lowercase
1004 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1007 public void bug78468 (string assemblyFileName)
1009 AssemblyName assemblyName = new AssemblyName ();
1010 assemblyName.Name = "bug78468b";
1012 AssemblyBuilder ab = AppDomain.CurrentDomain
1013 .DefineDynamicAssembly (assemblyName,
1014 AssemblyBuilderAccess.Save,
1015 Path.GetDirectoryName (assemblyFileName),
1016 AppDomain.CurrentDomain.Evidence);
1017 ab.AddResourceFile ("read", "readme.txt");
1018 ab.Save (Path.GetFileName (assemblyFileName));
1020 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1021 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1022 string[] resNames = assembly.GetManifestResourceNames ();
1023 Assert.IsNotNull (resNames, "#B2");
1024 Assert.AreEqual (1, resNames.Length, "#B3");
1025 Assert.AreEqual ("read", resNames[0], "#B4");
1026 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1027 Assert.IsNotNull (resInfo, "#B5");
1028 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1029 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1030 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1031 Stream s = assembly.GetManifestResourceStream ("read");
1032 Assert.IsNotNull (s, "#B9");
1034 s = assembly.GetFile ("readme.txt");
1035 Assert.IsNotNull (s, "#B10");
1041 public void bug79872 ()
1043 Random rnd = new Random ();
1048 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1049 if (Directory.Exists (outdir)) {
1051 Directory.Delete (outdir, true);
1058 Directory.CreateDirectory (outdir);
1060 AssemblyName an = new AssemblyName ();
1061 an.Name = "bug79872";
1062 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1063 string dllname = "bug79872.dll";
1064 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1065 string netmodule = "bug79872.netmodule";
1066 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1067 TypeBuilder a1 = mb2.DefineType ("A");
1073 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1077 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1081 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1082 } catch (BadImageFormatException) {
1083 ok = true; // mono and .net 2.0 throw this
1084 } catch (FileLoadException) {
1085 ok = true; // .net 1.1 throws this
1089 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1091 Directory.Delete (outdir, true);
1096 public void ManifestModule ()
1098 Assembly assembly = typeof (int).Assembly;
1099 Module module = assembly.ManifestModule;
1100 Assert.IsNotNull (module, "#1");
1103 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1105 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1109 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1111 Assert.IsFalse (module.IsResource (), "#4");
1112 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1113 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1114 Assert.AreSame (module, assembly.ManifestModule, "#7");
1119 private class AssemblyResolveHandler
1121 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1123 _assemblyFile = assemblyFile;
1124 _assemblyName = assemblyName;
1126 AppDomain.CurrentDomain.AssemblyResolve +=
1127 new ResolveEventHandler (ResolveAssembly);
1130 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1132 if (args.Name == _assemblyName)
1133 return Assembly.LoadFrom (_assemblyFile);
1138 private readonly string _assemblyFile;
1139 private readonly string _assemblyName;
1142 protected internal class Bug328812_NestedFamORAssem { };
1145 public void bug328812 ()
1147 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1148 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1149 // Just a sanity check, in case the above passed for some other reason
1150 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1154 public void GetCustomAttributes_AttributeType_Null ()
1156 Assembly a = typeof (int).Assembly;
1158 a.GetCustomAttributes (null, false);
1160 } catch (ArgumentNullException ex) {
1161 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1162 Assert.IsNull (ex.InnerException, "#3");
1163 Assert.IsNotNull (ex.Message, "#4");
1164 Assert.IsNotNull (ex.ParamName, "#5");
1165 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1170 public void GetTypeWithEmptyStringShouldThrow ()
1173 typeof (string).Assembly.GetType ("");
1175 } catch (ArgumentException) {}
1180 public void DefinedTypes_Equality ()
1182 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1183 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1185 Assert.AreSame (x1, x2, "#1");
1190 public class TestDefinedTypes