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 #if NET_4_0 && !MOBILE
199 Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
201 Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
204 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
205 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
209 public void GetAssembly ()
211 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
212 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
216 public void GetFile_Null ()
219 Assembly.GetExecutingAssembly ().GetFile (null);
221 } catch (ArgumentNullException ex) {
222 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
223 Assert.IsNull (ex.InnerException, "#3");
224 Assert.IsNotNull (ex.Message, "#4");
225 Assert.IsNull (ex.ParamName, "#5");
230 public void GetFile_Empty ()
233 Assembly.GetExecutingAssembly ().GetFile (
236 } catch (ArgumentException ex) {
237 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
238 Assert.IsNull (ex.InnerException, "#3");
239 Assert.IsNotNull (ex.Message, "#4");
240 Assert.IsNull (ex.ParamName, "#5");
245 public void GetFiles_False ()
247 Assembly corlib = typeof (int).Assembly;
248 FileStream[] fss = corlib.GetFiles ();
249 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
251 Assembly corlib_test = Assembly.GetExecutingAssembly ();
252 fss = corlib_test.GetFiles ();
253 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
257 public void GetFiles_True ()
259 Assembly corlib = typeof (int).Assembly;
260 FileStream[] fss = corlib.GetFiles ();
261 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
263 Assembly corlib_test = Assembly.GetExecutingAssembly ();
264 fss = corlib_test.GetFiles ();
265 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
269 public void GetManifestResourceStream_Name_Empty ()
271 Assembly corlib = typeof (int).Assembly;
274 corlib.GetManifestResourceStream (string.Empty);
276 } catch (ArgumentException ex) {
277 // String cannot have zero length
278 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
279 Assert.IsNull (ex.InnerException, "#A3");
280 Assert.IsNotNull (ex.Message, "#A4");
283 corlib.GetManifestResourceStream (typeof (int), string.Empty);
286 corlib.GetManifestResourceStream ((Type) null, string.Empty);
288 } catch (ArgumentException ex) {
289 // String cannot have zero length
290 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
291 Assert.IsNull (ex.InnerException, "#B3");
292 Assert.IsNotNull (ex.Message, "#B4");
297 public void GetManifestResourceStream_Name_Null ()
299 Assembly corlib = typeof (int).Assembly;
302 corlib.GetManifestResourceStream ((string) null);
304 } catch (ArgumentNullException ex) {
305 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
306 Assert.IsNull (ex.InnerException, "#A3");
307 Assert.IsNotNull (ex.Message, "#A4");
310 corlib.GetManifestResourceStream (typeof (int), (string) null);
313 corlib.GetManifestResourceStream ((Type) null, (string) null);
315 } catch (ArgumentNullException ex) {
316 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
317 Assert.IsNull (ex.InnerException, "#B3");
318 Assert.IsNotNull (ex.Message, "#B4");
319 Assert.IsNotNull (ex.ParamName, "#B5");
320 Assert.AreEqual ("type", ex.ParamName, "#B6");
325 public void IsDefined_AttributeType_Null ()
327 Assembly corlib = typeof (int).Assembly;
330 corlib.IsDefined ((Type) null, false);
332 } catch (ArgumentNullException ex) {
333 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
334 Assert.IsNull (ex.InnerException, "#3");
335 Assert.IsNotNull (ex.Message, "#4");
336 Assert.IsNotNull (ex.ParamName, "#5");
337 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
342 public void LoadFrom_Empty_Assembly ()
344 string tempFile = Path.GetTempFileName ();
347 Assembly.LoadFrom (tempFile);
349 } catch (BadImageFormatException ex) {
350 Assert.IsNull (ex.InnerException, "#2");
352 File.Delete (tempFile);
357 public void LoadFrom_Invalid_Assembly ()
359 string tempFile = Path.GetTempFileName ();
360 using (StreamWriter sw = File.CreateText (tempFile)) {
361 sw.WriteLine ("foo");
366 Assembly.LoadFrom (tempFile);
368 } catch (BadImageFormatException ex) {
369 Assert.IsNull (ex.InnerException, "#2");
371 File.Delete (tempFile);
376 public void LoadFrom_NonExisting_Assembly ()
378 string tempFile = Path.GetTempFileName ();
379 File.Delete (tempFile);
382 Assembly.LoadFrom (tempFile);
384 } catch (FileNotFoundException ex) {
385 Assert.IsNull (ex.InnerException, "#2");
387 File.Delete (tempFile);
392 public void LoadWithPartialName ()
394 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" };
396 foreach (string s in names)
397 if (Assembly.LoadWithPartialName (s) != null)
399 Assert.Fail ("Was not able to load any corlib test");
403 public void GetObjectData_Info_Null ()
405 Assembly corlib = typeof (int).Assembly;
407 corlib.GetObjectData (null, new StreamingContext (
408 StreamingContextStates.All));
410 } catch (ArgumentNullException ex) {
411 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
412 Assert.IsNull (ex.InnerException, "#3");
413 Assert.IsNotNull (ex.Message, "#4");
414 Assert.IsNotNull (ex.ParamName, "#5");
415 Assert.AreEqual ("info", ex.ParamName, "#6");
420 public void GetReferencedAssemblies ()
422 Assembly corlib_test = Assembly.GetExecutingAssembly ();
423 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
424 foreach (AssemblyName an in names) {
425 Assert.IsNull (an.CodeBase, "CodeBase");
426 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
427 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
428 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
429 Assert.IsNotNull (an.FullName, "FullName");
430 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
431 Assert.IsNull (an.KeyPair, "KeyPair");
432 Assert.IsNotNull (an.Name, "Name");
433 Assert.IsNotNull (an.Version, "Version");
434 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
435 an.VersionCompatibility, "VersionCompatibility");
439 #if !MONOTOUCH // Reflection.Emit is not supported.
441 public void Location_Empty() {
442 string assemblyFileName = Path.Combine (
443 Path.GetTempPath (), "AssemblyLocation.dll");
446 AssemblyName assemblyName = new AssemblyName ();
447 assemblyName.Name = "AssemblyLocation";
449 AssemblyBuilder ab = AppDomain.CurrentDomain
450 .DefineDynamicAssembly (assemblyName,
451 AssemblyBuilderAccess.Save,
453 AppDomain.CurrentDomain.Evidence);
454 ab.Save (Path.GetFileName (assemblyFileName));
456 using (FileStream fs = File.OpenRead (assemblyFileName)) {
457 byte[] buffer = new byte[fs.Length];
458 fs.Read (buffer, 0, buffer.Length);
459 Assembly assembly = Assembly.Load (buffer);
460 Assert.AreEqual (string.Empty, assembly.Location);
464 File.Delete (assemblyFileName);
469 public void SateliteAssemblyForInMemoryAssembly ()
471 string assemblyFileName = Path.Combine (
472 Path.GetTempPath (), "AssemblyLocation1.dll");
475 AssemblyName assemblyName = new AssemblyName ();
476 assemblyName.Name = "AssemblyLocation1";
478 AssemblyBuilder ab = AppDomain.CurrentDomain
479 .DefineDynamicAssembly (assemblyName,
480 AssemblyBuilderAccess.Save,
481 Path.GetTempPath ());
483 ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
484 TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
486 MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
487 ILGenerator gen = methodBuilder.GetILGenerator ();
490 // var resourceManager = new ResourceManager (typeof (Program));
491 // resourceManager.GetString ("test");
493 gen.Emit (OpCodes.Ldtoken, typeBuilder);
494 gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
495 gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
496 gen.Emit (OpCodes.Ldstr, "test");
497 gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
498 gen.Emit (OpCodes.Pop);
499 gen.Emit (OpCodes.Ret);
501 typeBuilder.CreateType ();
503 ab.Save (Path.GetFileName (assemblyFileName));
505 using (FileStream fs = File.OpenRead (assemblyFileName)) {
506 byte[] buffer = new byte[fs.Length];
507 fs.Read (buffer, 0, buffer.Length);
508 Assembly assembly = Assembly.Load (buffer);
510 var mm = assembly.GetType ("Program").GetMethod ("TestCall");
511 mm.Invoke (null, null);
516 File.Delete (assemblyFileName);
521 [Category ("NotWorking")]
522 public void bug78464 ()
524 string assemblyFileName = Path.Combine (
525 Path.GetTempPath (), "bug78464.dll");
528 // execute test in separate appdomain to allow assembly to be unloaded
529 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
530 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
532 crossDomainTester.bug78464 (assemblyFileName);
534 AppDomain.Unload (testDomain);
537 File.Delete (assemblyFileName);
542 public void bug78465 ()
544 string assemblyFileName = Path.Combine (
545 Path.GetTempPath (), "bug78465.dll");
548 AssemblyName assemblyName = new AssemblyName ();
549 assemblyName.Name = "bug78465";
551 AssemblyBuilder ab = AppDomain.CurrentDomain
552 .DefineDynamicAssembly (assemblyName,
553 AssemblyBuilderAccess.Save,
554 Path.GetDirectoryName (assemblyFileName),
555 AppDomain.CurrentDomain.Evidence);
556 ab.Save (Path.GetFileName (assemblyFileName));
558 using (FileStream fs = File.OpenRead (assemblyFileName)) {
559 byte[] buffer = new byte[fs.Length];
560 fs.Read (buffer, 0, buffer.Length);
561 Assembly assembly = Assembly.Load (buffer);
562 Assert.AreEqual (string.Empty, assembly.Location, "#1");
566 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
567 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
569 crossDomainTester.bug78465 (assemblyFileName);
571 AppDomain.Unload (testDomain);
574 File.Delete (assemblyFileName);
579 public void bug78468 ()
581 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
583 string resourceFileName = Path.Combine (Path.GetTempPath (),
586 using (StreamWriter sw = File.CreateText (resourceFileName)) {
587 sw.WriteLine ("FOO");
592 AssemblyName assemblyName = new AssemblyName ();
593 assemblyName.Name = "bug78468a";
595 AssemblyBuilder ab = AppDomain.CurrentDomain
596 .DefineDynamicAssembly (assemblyName,
597 AssemblyBuilderAccess.Save,
599 AppDomain.CurrentDomain.Evidence);
600 ab.AddResourceFile ("read", "readme.txt");
601 ab.Save (Path.GetFileName (assemblyFileNameA));
605 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
606 byte[] buffer = new byte[fs.Length];
607 fs.Read (buffer, 0, buffer.Length);
608 assembly = Assembly.Load (buffer);
612 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
613 string[] resNames = assembly.GetManifestResourceNames ();
614 Assert.IsNotNull (resNames, "#A2");
615 Assert.AreEqual (1, resNames.Length, "#A3");
616 Assert.AreEqual ("read", resNames[0], "#A4");
617 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
618 Assert.IsNotNull (resInfo, "#A5");
619 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
620 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
621 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
623 assembly.GetManifestResourceStream ("read");
625 } catch (FileNotFoundException) {
628 assembly.GetFile ("readme.txt");
629 Assert.Fail ("#A10");
630 } catch (FileNotFoundException) {
633 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
636 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
637 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
639 crossDomainTester.bug78468 (assemblyFileNameB);
641 AppDomain.Unload (testDomain);
642 File.Delete (assemblyFileNameB);
645 File.Delete (assemblyFileNameA);
646 File.Delete (resourceFileName);
651 [Category ("NotWorking")]
652 public void ReflectionOnlyLoad ()
654 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
656 Assert.IsNotNull (assembly);
657 Assert.IsTrue (assembly.ReflectionOnly);
661 public void ReflectionOnlyLoadFrom ()
663 string loc = typeof (AssemblyTest).Assembly.Location;
664 string filename = Path.GetFileName (loc);
665 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
667 Assert.IsNotNull (assembly);
668 Assert.IsTrue (assembly.ReflectionOnly);
672 [ExpectedException (typeof (ArgumentException))]
673 public void CreateInstanceOnRefOnly ()
675 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
676 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
680 [Category ("NotWorking")] // patch for bug #79720 must be committed first
681 public void Load_Culture ()
683 string tempDir = Path.Combine (Path.GetTempPath (),
684 "MonoTests.System.Reflection.AssemblyTest");
685 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
686 if (!Directory.Exists (cultureTempDir))
687 Directory.CreateDirectory (cultureTempDir);
688 cultureTempDir = Path.Combine (tempDir, "en-US");
689 if (!Directory.Exists (cultureTempDir))
690 Directory.CreateDirectory (cultureTempDir);
693 AppDomain ad = CreateTestDomain (tempDir, true);
695 CrossDomainTester cdt = CreateCrossDomainTester (ad);
699 AssemblyName aname = new AssemblyName ();
700 aname.Name = "culturea";
701 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
703 aname = new AssemblyName ();
704 aname.Name = "culturea";
705 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
707 aname = new AssemblyName ();
708 aname.Name = "culturea";
709 aname.CultureInfo = new CultureInfo ("nl-BE");
710 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
712 aname = new AssemblyName ();
713 aname.Name = "culturea";
714 aname.CultureInfo = CultureInfo.InvariantCulture;
715 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
719 aname = new AssemblyName ();
720 aname.Name = "cultureb";
721 aname.CultureInfo = new CultureInfo ("nl-BE");
722 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
724 aname = new AssemblyName ();
725 aname.Name = "cultureb";
726 aname.CultureInfo = new CultureInfo ("nl-BE");
727 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
729 aname = new AssemblyName ();
730 aname.Name = "cultureb";
731 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
733 aname = new AssemblyName ();
734 aname.Name = "cultureb";
735 aname.CultureInfo = new CultureInfo ("en-US");
736 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
740 aname = new AssemblyName ();
741 aname.Name = "culturec";
742 aname.CultureInfo = new CultureInfo ("nl-BE");
743 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
745 aname = new AssemblyName ();
746 aname.Name = "culturec";
747 aname.CultureInfo = new CultureInfo ("nl-BE");
748 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
750 aname = new AssemblyName ();
751 aname.Name = "culturec";
752 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
754 aname = new AssemblyName ();
755 aname.Name = "culturec";
756 aname.CultureInfo = CultureInfo.InvariantCulture;
757 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
761 aname = new AssemblyName ();
762 aname.Name = "cultured";
763 aname.CultureInfo = new CultureInfo ("nl-BE");
764 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
766 aname = new AssemblyName ();
767 aname.Name = "cultured";
768 aname.CultureInfo = new CultureInfo ("nl-BE");
769 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
771 aname = new AssemblyName ();
772 aname.Name = "cultured";
773 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
775 aname = new AssemblyName ();
776 aname.Name = "cultured";
777 aname.CultureInfo = CultureInfo.InvariantCulture;
778 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
780 AppDomain.Unload (ad);
781 if (Directory.Exists (tempDir))
782 Directory.Delete (tempDir, true);
787 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
788 public void Load_Culture_Mismatch ()
790 string tempDir = Path.Combine (Path.GetTempPath (),
791 "MonoTests.System.Reflection.AssemblyTest");
792 string cultureTempDir = Path.Combine (tempDir, "en-US");
793 if (!Directory.Exists (cultureTempDir))
794 Directory.CreateDirectory (cultureTempDir);
796 AppDomain ad = CreateTestDomain (tempDir, true);
798 CrossDomainTester cdt = CreateCrossDomainTester (ad);
802 AssemblyName aname = new AssemblyName ();
803 aname.Name = "bug79712a";
804 aname.CultureInfo = new CultureInfo ("nl-BE");
805 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
807 aname = new AssemblyName ();
808 aname.Name = "bug79712a";
809 aname.CultureInfo = CultureInfo.InvariantCulture;
810 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
814 aname = new AssemblyName ();
815 aname.Name = "bug79712b";
816 aname.CultureInfo = new CultureInfo ("nl-BE");
817 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
819 aname = new AssemblyName ();
820 aname.Name = "bug79712b";
821 aname.CultureInfo = new CultureInfo ("en-US");
822 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
824 AppDomain.Unload (ad);
825 if (Directory.Exists (tempDir))
826 Directory.Delete (tempDir, true);
832 public void Load_PartialVersion ()
834 string tempDir = Path.Combine (Path.GetTempPath (),
835 "MonoTests.System.Reflection.AssemblyTest");
836 if (!Directory.Exists (tempDir))
837 Directory.CreateDirectory (tempDir);
839 AppDomain ad = CreateTestDomain (tempDir, true);
841 CrossDomainTester cdt = CreateCrossDomainTester (ad);
843 AssemblyName aname = new AssemblyName ();
844 aname.Name = "bug79715";
845 aname.Version = new Version (1, 2, 3, 4);
846 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
848 aname = new AssemblyName ();
849 aname.Name = "bug79715";
850 aname.Version = new Version (1, 2);
851 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
852 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
854 aname = new AssemblyName ();
855 aname.Name = "bug79715";
856 aname.Version = new Version (1, 2, 3);
857 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
858 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
860 aname = new AssemblyName ();
861 aname.Name = "bug79715";
862 aname.Version = new Version (1, 2, 3, 4);
863 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
864 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
866 AppDomain.Unload (ad);
867 if (Directory.Exists (tempDir))
868 Directory.Delete (tempDir, true);
872 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
874 AppDomainSetup setup = new AppDomainSetup ();
875 setup.ApplicationBase = baseDirectory;
876 setup.ApplicationName = "testdomain";
878 AppDomain ad = AppDomain.CreateDomain ("testdomain",
879 AppDomain.CurrentDomain.Evidence, setup);
881 if (assemblyResolver) {
882 Assembly ea = Assembly.GetExecutingAssembly ();
883 ad.CreateInstanceFrom (ea.CodeBase,
884 typeof (AssemblyResolveHandler).FullName,
886 BindingFlags.Public | BindingFlags.Instance,
888 new object [] { ea.Location, ea.FullName },
889 CultureInfo.InvariantCulture,
897 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
899 Type testerType = typeof (CrossDomainTester);
900 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
901 testerType.Assembly.FullName, testerType.FullName, false,
902 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
903 CultureInfo.InvariantCulture, new object[0], null);
906 private class CrossDomainTester : MarshalByRefObject
908 public void GenerateAssembly (AssemblyName aname, string path)
910 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
911 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
912 ab.Save (Path.GetFileName (path));
915 public void Load (AssemblyName assemblyRef)
917 Assembly.Load (assemblyRef);
920 public void LoadFrom (string assemblyFile)
922 Assembly.LoadFrom (assemblyFile);
925 public bool AssertLoad (AssemblyName assemblyRef)
928 Assembly.Load (assemblyRef);
935 public bool AssertLoad (string assemblyString)
938 Assembly.Load (assemblyString);
945 public bool AssertFileLoadException (AssemblyName assemblyRef)
948 Assembly.Load (assemblyRef);
950 } catch (FileLoadException) {
955 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
958 Assembly.Load (assemblyRef);
960 } catch (FileNotFoundException) {
965 public void bug78464 (string assemblyFileName)
967 AssemblyName assemblyName = new AssemblyName ();
968 assemblyName.Name = "bug78464";
970 AssemblyBuilder ab = AppDomain.CurrentDomain
971 .DefineDynamicAssembly (assemblyName,
972 AssemblyBuilderAccess.Save,
973 Path.GetDirectoryName (assemblyFileName),
974 AppDomain.CurrentDomain.Evidence);
975 ab.Save (Path.GetFileName (assemblyFileName));
979 using (FileStream fs = File.OpenRead (assemblyFileName)) {
980 byte[] buffer = new byte[fs.Length];
981 fs.Read (buffer, 0, buffer.Length);
982 assembly = Assembly.Load (buffer);
986 Assert.AreEqual (string.Empty, assembly.Location, "#1");
988 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
989 Assert.IsFalse (assembly.Location == string.Empty, "#2");
990 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
991 // note: we cannot check if directory names match, as MS.NET seems to
992 // convert directory part of assembly location to lowercase
993 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
996 public void bug78465 (string assemblyFileName)
998 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
999 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1000 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1001 // note: we cannot check if directory names match, as MS.NET seems to
1002 // convert directory part of assembly location to lowercase
1003 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1006 public void bug78468 (string assemblyFileName)
1008 AssemblyName assemblyName = new AssemblyName ();
1009 assemblyName.Name = "bug78468b";
1011 AssemblyBuilder ab = AppDomain.CurrentDomain
1012 .DefineDynamicAssembly (assemblyName,
1013 AssemblyBuilderAccess.Save,
1014 Path.GetDirectoryName (assemblyFileName),
1015 AppDomain.CurrentDomain.Evidence);
1016 ab.AddResourceFile ("read", "readme.txt");
1017 ab.Save (Path.GetFileName (assemblyFileName));
1019 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1020 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1021 string[] resNames = assembly.GetManifestResourceNames ();
1022 Assert.IsNotNull (resNames, "#B2");
1023 Assert.AreEqual (1, resNames.Length, "#B3");
1024 Assert.AreEqual ("read", resNames[0], "#B4");
1025 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1026 Assert.IsNotNull (resInfo, "#B5");
1027 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1028 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1029 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1030 Stream s = assembly.GetManifestResourceStream ("read");
1031 Assert.IsNotNull (s, "#B9");
1033 s = assembly.GetFile ("readme.txt");
1034 Assert.IsNotNull (s, "#B10");
1040 public void bug79872 ()
1042 Random rnd = new Random ();
1047 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1048 if (Directory.Exists (outdir)) {
1050 Directory.Delete (outdir, true);
1057 Directory.CreateDirectory (outdir);
1059 AssemblyName an = new AssemblyName ();
1060 an.Name = "bug79872";
1061 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1062 string dllname = "bug79872.dll";
1063 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1064 string netmodule = "bug79872.netmodule";
1065 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1066 TypeBuilder a1 = mb2.DefineType ("A");
1072 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1076 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1080 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1081 } catch (BadImageFormatException) {
1082 ok = true; // mono and .net 2.0 throw this
1083 } catch (FileLoadException) {
1084 ok = true; // .net 1.1 throws this
1088 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1090 Directory.Delete (outdir, true);
1095 public void ManifestModule ()
1097 Assembly assembly = typeof (int).Assembly;
1098 Module module = assembly.ManifestModule;
1099 Assert.IsNotNull (module, "#1");
1102 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1104 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1108 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1110 Assert.IsFalse (module.IsResource (), "#4");
1111 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1112 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1113 Assert.AreSame (module, assembly.ManifestModule, "#7");
1118 private class AssemblyResolveHandler
1120 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1122 _assemblyFile = assemblyFile;
1123 _assemblyName = assemblyName;
1125 AppDomain.CurrentDomain.AssemblyResolve +=
1126 new ResolveEventHandler (ResolveAssembly);
1129 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1131 if (args.Name == _assemblyName)
1132 return Assembly.LoadFrom (_assemblyFile);
1137 private readonly string _assemblyFile;
1138 private readonly string _assemblyName;
1141 protected internal class Bug328812_NestedFamORAssem { };
1144 public void bug328812 ()
1146 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1147 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1148 // Just a sanity check, in case the above passed for some other reason
1149 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1153 public void GetCustomAttributes_AttributeType_Null ()
1155 Assembly a = typeof (int).Assembly;
1157 a.GetCustomAttributes (null, false);
1159 } catch (ArgumentNullException ex) {
1160 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1161 Assert.IsNull (ex.InnerException, "#3");
1162 Assert.IsNotNull (ex.Message, "#4");
1163 Assert.IsNotNull (ex.ParamName, "#5");
1164 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1169 public void GetTypeWithEmptyStringShouldThrow ()
1172 typeof (string).Assembly.GetType ("");
1174 } catch (ArgumentException) {}
1179 public void DefinedTypes_Equality ()
1181 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1182 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1184 Assert.AreSame (x1, x2, "#1");
1189 public class TestDefinedTypes