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.
127 string fname = AppDomain.CurrentDomain.FriendlyName;
128 if (fname.EndsWith (".dll")) { // nunit-console
129 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
130 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
132 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
133 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
136 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
137 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
141 #if !MONOTOUCH // Reflection.Emit is not supported.
143 [Category("AndroidNotWorking")] // Missing Mono.CompilerServices.SymbolWriter
144 public void GetModules_MissingFile ()
146 AssemblyName newName = new AssemblyName ();
147 newName.Name = "AssemblyTest";
149 AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
151 ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
153 ab.Save ("test_assembly.dll");
155 File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
157 Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
161 } catch (FileNotFoundException ex) {
162 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
167 [Category ("NotWorking")]
169 public void Corlib ()
171 Assembly corlib = typeof (int).Assembly;
172 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
173 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
174 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
175 Assert.IsNotNull (corlib.Evidence, "Evidence");
176 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
178 // corlib doesn't reference anything
179 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
180 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
181 // not really "true" but it's even more trusted so...
182 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
183 Assert.AreEqual (0, corlib.HostContext, "HostContext");
184 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
185 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
186 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
190 public void Corlib_test ()
192 Assembly corlib_test = Assembly.GetExecutingAssembly ();
194 Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
195 Assert.IsNull (corlib_test.Evidence, "Evidence");
197 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
198 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
200 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
202 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
203 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
204 Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
206 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
207 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
211 public void GetAssembly ()
213 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
214 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
218 public void GetFile_Null ()
221 Assembly.GetExecutingAssembly ().GetFile (null);
223 } catch (ArgumentNullException ex) {
224 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
225 Assert.IsNull (ex.InnerException, "#3");
226 Assert.IsNotNull (ex.Message, "#4");
227 Assert.IsNull (ex.ParamName, "#5");
232 public void GetFile_Empty ()
235 Assembly.GetExecutingAssembly ().GetFile (
238 } catch (ArgumentException ex) {
239 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
240 Assert.IsNull (ex.InnerException, "#3");
241 Assert.IsNotNull (ex.Message, "#4");
242 Assert.IsNull (ex.ParamName, "#5");
247 public void GetFiles_False ()
249 Assembly corlib = typeof (int).Assembly;
250 FileStream[] fss = corlib.GetFiles ();
251 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
253 Assembly corlib_test = Assembly.GetExecutingAssembly ();
254 fss = corlib_test.GetFiles ();
255 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
259 public void GetFiles_True ()
261 Assembly corlib = typeof (int).Assembly;
262 FileStream[] fss = corlib.GetFiles ();
263 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
265 Assembly corlib_test = Assembly.GetExecutingAssembly ();
266 fss = corlib_test.GetFiles ();
267 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
271 public void GetManifestResourceStream_Name_Empty ()
273 Assembly corlib = typeof (int).Assembly;
276 corlib.GetManifestResourceStream (string.Empty);
278 } catch (ArgumentException ex) {
279 // String cannot have zero length
280 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
281 Assert.IsNull (ex.InnerException, "#A3");
282 Assert.IsNotNull (ex.Message, "#A4");
285 corlib.GetManifestResourceStream (typeof (int), string.Empty);
288 corlib.GetManifestResourceStream ((Type) null, string.Empty);
290 } catch (ArgumentException ex) {
291 // String cannot have zero length
292 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
293 Assert.IsNull (ex.InnerException, "#B3");
294 Assert.IsNotNull (ex.Message, "#B4");
299 public void GetManifestResourceStream_Name_Null ()
301 Assembly corlib = typeof (int).Assembly;
304 corlib.GetManifestResourceStream ((string) null);
306 } catch (ArgumentNullException ex) {
307 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
308 Assert.IsNull (ex.InnerException, "#A3");
309 Assert.IsNotNull (ex.Message, "#A4");
312 corlib.GetManifestResourceStream (typeof (int), (string) null);
315 corlib.GetManifestResourceStream ((Type) null, (string) null);
317 } catch (ArgumentNullException ex) {
318 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
319 Assert.IsNull (ex.InnerException, "#B3");
320 Assert.IsNotNull (ex.Message, "#B4");
321 Assert.IsNotNull (ex.ParamName, "#B5");
322 Assert.AreEqual ("type", ex.ParamName, "#B6");
327 public void IsDefined_AttributeType_Null ()
329 Assembly corlib = typeof (int).Assembly;
332 corlib.IsDefined ((Type) null, false);
334 } catch (ArgumentNullException ex) {
335 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
336 Assert.IsNull (ex.InnerException, "#3");
337 Assert.IsNotNull (ex.Message, "#4");
338 Assert.IsNotNull (ex.ParamName, "#5");
339 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
344 public void LoadFrom_Empty_Assembly ()
346 string tempFile = Path.GetTempFileName ();
349 Assembly.LoadFrom (tempFile);
351 } catch (BadImageFormatException ex) {
352 Assert.IsNull (ex.InnerException, "#2");
354 File.Delete (tempFile);
359 public void LoadFrom_Invalid_Assembly ()
361 string tempFile = Path.GetTempFileName ();
362 using (StreamWriter sw = File.CreateText (tempFile)) {
363 sw.WriteLine ("foo");
368 Assembly.LoadFrom (tempFile);
370 } catch (BadImageFormatException ex) {
371 Assert.IsNull (ex.InnerException, "#2");
373 File.Delete (tempFile);
378 public void LoadFrom_NonExisting_Assembly ()
380 string tempFile = Path.GetTempFileName ();
381 File.Delete (tempFile);
384 Assembly.LoadFrom (tempFile);
386 } catch (FileNotFoundException ex) {
387 Assert.IsNull (ex.InnerException, "#2");
389 File.Delete (tempFile);
394 public void LoadWithPartialName ()
396 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" };
398 foreach (string s in names)
399 if (Assembly.LoadWithPartialName (s) != null)
401 Assert.Fail ("Was not able to load any corlib test");
405 public void GetObjectData_Info_Null ()
407 Assembly corlib = typeof (int).Assembly;
409 corlib.GetObjectData (null, new StreamingContext (
410 StreamingContextStates.All));
412 } catch (ArgumentNullException ex) {
413 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
414 Assert.IsNull (ex.InnerException, "#3");
415 Assert.IsNotNull (ex.Message, "#4");
416 Assert.IsNotNull (ex.ParamName, "#5");
417 Assert.AreEqual ("info", ex.ParamName, "#6");
422 public void GetReferencedAssemblies ()
424 Assembly corlib_test = Assembly.GetExecutingAssembly ();
425 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
426 foreach (AssemblyName an in names) {
427 Assert.IsNull (an.CodeBase, "CodeBase");
428 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
429 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
430 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
431 Assert.IsNotNull (an.FullName, "FullName");
432 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
433 Assert.IsNull (an.KeyPair, "KeyPair");
434 Assert.IsNotNull (an.Name, "Name");
435 Assert.IsNotNull (an.Version, "Version");
436 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
437 an.VersionCompatibility, "VersionCompatibility");
441 #if !MONOTOUCH // Reflection.Emit is not supported.
443 public void Location_Empty() {
444 string assemblyFileName = Path.Combine (
445 Path.GetTempPath (), "AssemblyLocation.dll");
448 AssemblyName assemblyName = new AssemblyName ();
449 assemblyName.Name = "AssemblyLocation";
451 AssemblyBuilder ab = AppDomain.CurrentDomain
452 .DefineDynamicAssembly (assemblyName,
453 AssemblyBuilderAccess.Save,
455 AppDomain.CurrentDomain.Evidence);
456 ab.Save (Path.GetFileName (assemblyFileName));
458 using (FileStream fs = File.OpenRead (assemblyFileName)) {
459 byte[] buffer = new byte[fs.Length];
460 fs.Read (buffer, 0, buffer.Length);
461 Assembly assembly = Assembly.Load (buffer);
462 Assert.AreEqual (string.Empty, assembly.Location);
466 File.Delete (assemblyFileName);
471 public void SateliteAssemblyForInMemoryAssembly ()
473 string assemblyFileName = Path.Combine (
474 Path.GetTempPath (), "AssemblyLocation1.dll");
477 AssemblyName assemblyName = new AssemblyName ();
478 assemblyName.Name = "AssemblyLocation1";
480 AssemblyBuilder ab = AppDomain.CurrentDomain
481 .DefineDynamicAssembly (assemblyName,
482 AssemblyBuilderAccess.Save,
483 Path.GetTempPath ());
485 ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
486 TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
488 MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
489 ILGenerator gen = methodBuilder.GetILGenerator ();
492 // var resourceManager = new ResourceManager (typeof (Program));
493 // resourceManager.GetString ("test");
495 gen.Emit (OpCodes.Ldtoken, typeBuilder);
496 gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
497 gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
498 gen.Emit (OpCodes.Ldstr, "test");
499 gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
500 gen.Emit (OpCodes.Pop);
501 gen.Emit (OpCodes.Ret);
503 typeBuilder.CreateType ();
505 ab.Save (Path.GetFileName (assemblyFileName));
507 using (FileStream fs = File.OpenRead (assemblyFileName)) {
508 byte[] buffer = new byte[fs.Length];
509 fs.Read (buffer, 0, buffer.Length);
510 Assembly assembly = Assembly.Load (buffer);
512 var mm = assembly.GetType ("Program").GetMethod ("TestCall");
514 mm.Invoke (null, null);
516 } catch (TargetInvocationException e) {
517 Assert.IsTrue (e.InnerException is MissingManifestResourceException);
523 File.Delete (assemblyFileName);
528 [Category ("NotWorking")]
529 public void bug78464 ()
531 string assemblyFileName = Path.Combine (
532 Path.GetTempPath (), "bug78464.dll");
535 // execute test in separate appdomain to allow assembly to be unloaded
536 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
537 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
539 crossDomainTester.bug78464 (assemblyFileName);
541 AppDomain.Unload (testDomain);
544 File.Delete (assemblyFileName);
549 public void bug78465 ()
551 string assemblyFileName = Path.Combine (
552 Path.GetTempPath (), "bug78465.dll");
555 AssemblyName assemblyName = new AssemblyName ();
556 assemblyName.Name = "bug78465";
558 AssemblyBuilder ab = AppDomain.CurrentDomain
559 .DefineDynamicAssembly (assemblyName,
560 AssemblyBuilderAccess.Save,
561 Path.GetDirectoryName (assemblyFileName),
562 AppDomain.CurrentDomain.Evidence);
563 ab.Save (Path.GetFileName (assemblyFileName));
565 using (FileStream fs = File.OpenRead (assemblyFileName)) {
566 byte[] buffer = new byte[fs.Length];
567 fs.Read (buffer, 0, buffer.Length);
568 Assembly assembly = Assembly.Load (buffer);
569 Assert.AreEqual (string.Empty, assembly.Location, "#1");
573 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
574 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
576 crossDomainTester.bug78465 (assemblyFileName);
578 AppDomain.Unload (testDomain);
581 File.Delete (assemblyFileName);
586 public void bug78468 ()
588 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
590 string resourceFileName = Path.Combine (Path.GetTempPath (),
593 using (StreamWriter sw = File.CreateText (resourceFileName)) {
594 sw.WriteLine ("FOO");
599 AssemblyName assemblyName = new AssemblyName ();
600 assemblyName.Name = "bug78468a";
602 AssemblyBuilder ab = AppDomain.CurrentDomain
603 .DefineDynamicAssembly (assemblyName,
604 AssemblyBuilderAccess.Save,
606 AppDomain.CurrentDomain.Evidence);
607 ab.AddResourceFile ("read", "readme.txt");
608 ab.Save (Path.GetFileName (assemblyFileNameA));
612 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
613 byte[] buffer = new byte[fs.Length];
614 fs.Read (buffer, 0, buffer.Length);
615 assembly = Assembly.Load (buffer);
619 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
620 string[] resNames = assembly.GetManifestResourceNames ();
621 Assert.IsNotNull (resNames, "#A2");
622 Assert.AreEqual (1, resNames.Length, "#A3");
623 Assert.AreEqual ("read", resNames[0], "#A4");
624 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
625 Assert.IsNotNull (resInfo, "#A5");
626 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
627 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
628 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
630 assembly.GetManifestResourceStream ("read");
632 } catch (FileNotFoundException) {
635 assembly.GetFile ("readme.txt");
636 Assert.Fail ("#A10");
637 } catch (FileNotFoundException) {
640 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
643 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
644 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
646 crossDomainTester.bug78468 (assemblyFileNameB);
648 AppDomain.Unload (testDomain);
649 File.Delete (assemblyFileNameB);
652 File.Delete (assemblyFileNameA);
653 File.Delete (resourceFileName);
658 [Category ("NotWorking")]
659 public void ReflectionOnlyLoad ()
661 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
663 Assert.IsNotNull (assembly);
664 Assert.IsTrue (assembly.ReflectionOnly);
668 public void ReflectionOnlyLoadFrom ()
670 string loc = typeof (AssemblyTest).Assembly.Location;
671 string filename = Path.GetFileName (loc);
672 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
674 Assert.IsNotNull (assembly);
675 Assert.IsTrue (assembly.ReflectionOnly);
679 [ExpectedException (typeof (ArgumentException))]
680 public void CreateInstanceOnRefOnly ()
682 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
683 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
687 [Category ("NotWorking")] // patch for bug #79720 must be committed first
688 public void Load_Culture ()
690 string tempDir = Path.Combine (Path.GetTempPath (),
691 "MonoTests.System.Reflection.AssemblyTest");
692 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
693 if (!Directory.Exists (cultureTempDir))
694 Directory.CreateDirectory (cultureTempDir);
695 cultureTempDir = Path.Combine (tempDir, "en-US");
696 if (!Directory.Exists (cultureTempDir))
697 Directory.CreateDirectory (cultureTempDir);
700 AppDomain ad = CreateTestDomain (tempDir, true);
702 CrossDomainTester cdt = CreateCrossDomainTester (ad);
706 AssemblyName aname = new AssemblyName ();
707 aname.Name = "culturea";
708 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
710 aname = new AssemblyName ();
711 aname.Name = "culturea";
712 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
714 aname = new AssemblyName ();
715 aname.Name = "culturea";
716 aname.CultureInfo = new CultureInfo ("nl-BE");
717 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
719 aname = new AssemblyName ();
720 aname.Name = "culturea";
721 aname.CultureInfo = CultureInfo.InvariantCulture;
722 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
726 aname = new AssemblyName ();
727 aname.Name = "cultureb";
728 aname.CultureInfo = new CultureInfo ("nl-BE");
729 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
731 aname = new AssemblyName ();
732 aname.Name = "cultureb";
733 aname.CultureInfo = new CultureInfo ("nl-BE");
734 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
736 aname = new AssemblyName ();
737 aname.Name = "cultureb";
738 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
740 aname = new AssemblyName ();
741 aname.Name = "cultureb";
742 aname.CultureInfo = new CultureInfo ("en-US");
743 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
747 aname = new AssemblyName ();
748 aname.Name = "culturec";
749 aname.CultureInfo = new CultureInfo ("nl-BE");
750 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
752 aname = new AssemblyName ();
753 aname.Name = "culturec";
754 aname.CultureInfo = new CultureInfo ("nl-BE");
755 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
757 aname = new AssemblyName ();
758 aname.Name = "culturec";
759 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
761 aname = new AssemblyName ();
762 aname.Name = "culturec";
763 aname.CultureInfo = CultureInfo.InvariantCulture;
764 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
768 aname = new AssemblyName ();
769 aname.Name = "cultured";
770 aname.CultureInfo = new CultureInfo ("nl-BE");
771 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
773 aname = new AssemblyName ();
774 aname.Name = "cultured";
775 aname.CultureInfo = new CultureInfo ("nl-BE");
776 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
778 aname = new AssemblyName ();
779 aname.Name = "cultured";
780 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
782 aname = new AssemblyName ();
783 aname.Name = "cultured";
784 aname.CultureInfo = CultureInfo.InvariantCulture;
785 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
787 AppDomain.Unload (ad);
788 if (Directory.Exists (tempDir))
789 Directory.Delete (tempDir, true);
794 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
795 public void Load_Culture_Mismatch ()
797 string tempDir = Path.Combine (Path.GetTempPath (),
798 "MonoTests.System.Reflection.AssemblyTest");
799 string cultureTempDir = Path.Combine (tempDir, "en-US");
800 if (!Directory.Exists (cultureTempDir))
801 Directory.CreateDirectory (cultureTempDir);
803 AppDomain ad = CreateTestDomain (tempDir, true);
805 CrossDomainTester cdt = CreateCrossDomainTester (ad);
809 AssemblyName aname = new AssemblyName ();
810 aname.Name = "bug79712a";
811 aname.CultureInfo = new CultureInfo ("nl-BE");
812 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
814 aname = new AssemblyName ();
815 aname.Name = "bug79712a";
816 aname.CultureInfo = CultureInfo.InvariantCulture;
817 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
821 aname = new AssemblyName ();
822 aname.Name = "bug79712b";
823 aname.CultureInfo = new CultureInfo ("nl-BE");
824 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
826 aname = new AssemblyName ();
827 aname.Name = "bug79712b";
828 aname.CultureInfo = new CultureInfo ("en-US");
829 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
831 AppDomain.Unload (ad);
832 if (Directory.Exists (tempDir))
833 Directory.Delete (tempDir, true);
839 public void Load_PartialVersion ()
841 string tempDir = Path.Combine (Path.GetTempPath (),
842 "MonoTests.System.Reflection.AssemblyTest");
843 if (!Directory.Exists (tempDir))
844 Directory.CreateDirectory (tempDir);
846 AppDomain ad = CreateTestDomain (tempDir, true);
848 CrossDomainTester cdt = CreateCrossDomainTester (ad);
850 AssemblyName aname = new AssemblyName ();
851 aname.Name = "bug79715";
852 aname.Version = new Version (1, 2, 3, 4);
853 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
855 aname = new AssemblyName ();
856 aname.Name = "bug79715";
857 aname.Version = new Version (1, 2);
858 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
859 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
861 aname = new AssemblyName ();
862 aname.Name = "bug79715";
863 aname.Version = new Version (1, 2, 3);
864 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
865 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
867 aname = new AssemblyName ();
868 aname.Name = "bug79715";
869 aname.Version = new Version (1, 2, 3, 4);
870 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
871 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
873 AppDomain.Unload (ad);
874 if (Directory.Exists (tempDir))
875 Directory.Delete (tempDir, true);
879 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
881 AppDomainSetup setup = new AppDomainSetup ();
882 setup.ApplicationBase = baseDirectory;
883 setup.ApplicationName = "testdomain";
885 AppDomain ad = AppDomain.CreateDomain ("testdomain",
886 AppDomain.CurrentDomain.Evidence, setup);
888 if (assemblyResolver) {
889 Assembly ea = Assembly.GetExecutingAssembly ();
890 ad.CreateInstanceFrom (ea.CodeBase,
891 typeof (AssemblyResolveHandler).FullName,
893 BindingFlags.Public | BindingFlags.Instance,
895 new object [] { ea.Location, ea.FullName },
896 CultureInfo.InvariantCulture,
904 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
906 Type testerType = typeof (CrossDomainTester);
907 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
908 testerType.Assembly.FullName, testerType.FullName, false,
909 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
910 CultureInfo.InvariantCulture, new object[0], null);
913 private class CrossDomainTester : MarshalByRefObject
915 public void GenerateAssembly (AssemblyName aname, string path)
917 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
918 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
919 ab.Save (Path.GetFileName (path));
922 public void Load (AssemblyName assemblyRef)
924 Assembly.Load (assemblyRef);
927 public void LoadFrom (string assemblyFile)
929 Assembly.LoadFrom (assemblyFile);
932 public bool AssertLoad (AssemblyName assemblyRef)
935 Assembly.Load (assemblyRef);
942 public bool AssertLoad (string assemblyString)
945 Assembly.Load (assemblyString);
952 public bool AssertFileLoadException (AssemblyName assemblyRef)
955 Assembly.Load (assemblyRef);
957 } catch (FileLoadException) {
962 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
965 Assembly.Load (assemblyRef);
967 } catch (FileNotFoundException) {
972 public void bug78464 (string assemblyFileName)
974 AssemblyName assemblyName = new AssemblyName ();
975 assemblyName.Name = "bug78464";
977 AssemblyBuilder ab = AppDomain.CurrentDomain
978 .DefineDynamicAssembly (assemblyName,
979 AssemblyBuilderAccess.Save,
980 Path.GetDirectoryName (assemblyFileName),
981 AppDomain.CurrentDomain.Evidence);
982 ab.Save (Path.GetFileName (assemblyFileName));
986 using (FileStream fs = File.OpenRead (assemblyFileName)) {
987 byte[] buffer = new byte[fs.Length];
988 fs.Read (buffer, 0, buffer.Length);
989 assembly = Assembly.Load (buffer);
993 Assert.AreEqual (string.Empty, assembly.Location, "#1");
995 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 bug78465 (string assemblyFileName)
1005 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1006 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1007 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1008 // note: we cannot check if directory names match, as MS.NET seems to
1009 // convert directory part of assembly location to lowercase
1010 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1013 public void bug78468 (string assemblyFileName)
1015 AssemblyName assemblyName = new AssemblyName ();
1016 assemblyName.Name = "bug78468b";
1018 AssemblyBuilder ab = AppDomain.CurrentDomain
1019 .DefineDynamicAssembly (assemblyName,
1020 AssemblyBuilderAccess.Save,
1021 Path.GetDirectoryName (assemblyFileName),
1022 AppDomain.CurrentDomain.Evidence);
1023 ab.AddResourceFile ("read", "readme.txt");
1024 ab.Save (Path.GetFileName (assemblyFileName));
1026 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1027 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1028 string[] resNames = assembly.GetManifestResourceNames ();
1029 Assert.IsNotNull (resNames, "#B2");
1030 Assert.AreEqual (1, resNames.Length, "#B3");
1031 Assert.AreEqual ("read", resNames[0], "#B4");
1032 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1033 Assert.IsNotNull (resInfo, "#B5");
1034 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1035 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1036 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1037 Stream s = assembly.GetManifestResourceStream ("read");
1038 Assert.IsNotNull (s, "#B9");
1040 s = assembly.GetFile ("readme.txt");
1041 Assert.IsNotNull (s, "#B10");
1047 public void bug79872 ()
1049 Random rnd = new Random ();
1054 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1055 if (Directory.Exists (outdir)) {
1057 Directory.Delete (outdir, true);
1064 Directory.CreateDirectory (outdir);
1066 AssemblyName an = new AssemblyName ();
1067 an.Name = "bug79872";
1068 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1069 string dllname = "bug79872.dll";
1070 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1071 string netmodule = "bug79872.netmodule";
1072 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1073 TypeBuilder a1 = mb2.DefineType ("A");
1079 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1083 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1087 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1088 } catch (BadImageFormatException) {
1089 ok = true; // mono and .net 2.0 throw this
1090 } catch (FileLoadException) {
1091 ok = true; // .net 1.1 throws this
1095 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1097 Directory.Delete (outdir, true);
1102 public void ManifestModule ()
1104 Assembly assembly = typeof (int).Assembly;
1105 Module module = assembly.ManifestModule;
1106 Assert.IsNotNull (module, "#1");
1109 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1111 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1115 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1117 Assert.IsFalse (module.IsResource (), "#4");
1118 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1119 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1120 Assert.AreSame (module, assembly.ManifestModule, "#7");
1125 private class AssemblyResolveHandler
1127 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1129 _assemblyFile = assemblyFile;
1130 _assemblyName = assemblyName;
1132 AppDomain.CurrentDomain.AssemblyResolve +=
1133 new ResolveEventHandler (ResolveAssembly);
1136 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1138 if (args.Name == _assemblyName)
1139 return Assembly.LoadFrom (_assemblyFile);
1144 private readonly string _assemblyFile;
1145 private readonly string _assemblyName;
1148 protected internal class Bug328812_NestedFamORAssem { };
1151 public void bug328812 ()
1153 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1154 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1155 // Just a sanity check, in case the above passed for some other reason
1156 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1160 public void GetCustomAttributes_AttributeType_Null ()
1162 Assembly a = typeof (int).Assembly;
1164 a.GetCustomAttributes (null, false);
1166 } catch (ArgumentNullException ex) {
1167 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1168 Assert.IsNull (ex.InnerException, "#3");
1169 Assert.IsNotNull (ex.Message, "#4");
1170 Assert.IsNotNull (ex.ParamName, "#5");
1171 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1176 public void GetTypeWithEmptyStringShouldThrow ()
1179 typeof (string).Assembly.GetType ("");
1181 } catch (ArgumentException) {}
1186 public void DefinedTypes_Equality ()
1188 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1189 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1191 Assert.AreSame (x1, x2, "#1");
1196 public class TestDefinedTypes