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.IsNull (corlib_test.EntryPoint, "EntryPoint");
195 Assert.IsNull (corlib_test.Evidence, "Evidence");
197 Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
198 Assert.IsNull (corlib_test.Evidence, "Evidence");
200 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
201 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
203 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
205 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
206 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
207 Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
209 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
210 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
214 public void GetAssembly ()
216 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
217 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
221 public void GetFile_Null ()
224 Assembly.GetExecutingAssembly ().GetFile (null);
226 } catch (ArgumentNullException ex) {
227 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
228 Assert.IsNull (ex.InnerException, "#3");
229 Assert.IsNotNull (ex.Message, "#4");
230 Assert.IsNull (ex.ParamName, "#5");
235 public void GetFile_Empty ()
238 Assembly.GetExecutingAssembly ().GetFile (
241 } catch (ArgumentException ex) {
242 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
243 Assert.IsNull (ex.InnerException, "#3");
244 Assert.IsNotNull (ex.Message, "#4");
245 Assert.IsNull (ex.ParamName, "#5");
250 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
251 public void GetFiles_False ()
253 Assembly corlib = typeof (int).Assembly;
254 FileStream[] fss = corlib.GetFiles ();
255 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
257 Assembly corlib_test = Assembly.GetExecutingAssembly ();
258 fss = corlib_test.GetFiles ();
259 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
263 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
264 public void GetFiles_True ()
266 Assembly corlib = typeof (int).Assembly;
267 FileStream[] fss = corlib.GetFiles ();
268 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
270 Assembly corlib_test = Assembly.GetExecutingAssembly ();
271 fss = corlib_test.GetFiles ();
272 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
276 public void GetManifestResourceStream_Name_Empty ()
278 Assembly corlib = typeof (int).Assembly;
281 corlib.GetManifestResourceStream (string.Empty);
283 } catch (ArgumentException ex) {
284 // String cannot have zero length
285 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
286 Assert.IsNull (ex.InnerException, "#A3");
287 Assert.IsNotNull (ex.Message, "#A4");
290 corlib.GetManifestResourceStream (typeof (int), string.Empty);
293 corlib.GetManifestResourceStream ((Type) null, string.Empty);
295 } catch (ArgumentException ex) {
296 // String cannot have zero length
297 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
298 Assert.IsNull (ex.InnerException, "#B3");
299 Assert.IsNotNull (ex.Message, "#B4");
304 public void GetManifestResourceStream_Name_Null ()
306 Assembly corlib = typeof (int).Assembly;
309 corlib.GetManifestResourceStream ((string) null);
311 } catch (ArgumentNullException ex) {
312 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
313 Assert.IsNull (ex.InnerException, "#A3");
314 Assert.IsNotNull (ex.Message, "#A4");
317 corlib.GetManifestResourceStream (typeof (int), (string) null);
320 corlib.GetManifestResourceStream ((Type) null, (string) null);
322 } catch (ArgumentNullException ex) {
323 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
324 Assert.IsNull (ex.InnerException, "#B3");
325 Assert.IsNotNull (ex.Message, "#B4");
326 Assert.IsNotNull (ex.ParamName, "#B5");
327 Assert.AreEqual ("type", ex.ParamName, "#B6");
332 public void IsDefined_AttributeType_Null ()
334 Assembly corlib = typeof (int).Assembly;
337 corlib.IsDefined ((Type) null, false);
339 } catch (ArgumentNullException ex) {
340 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
341 Assert.IsNull (ex.InnerException, "#3");
342 Assert.IsNotNull (ex.Message, "#4");
343 Assert.IsNotNull (ex.ParamName, "#5");
344 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
349 public void LoadFrom_Empty_Assembly ()
351 string tempFile = Path.GetTempFileName ();
354 Assembly.LoadFrom (tempFile);
356 } catch (BadImageFormatException ex) {
357 Assert.IsNull (ex.InnerException, "#2");
359 File.Delete (tempFile);
364 public void LoadFrom_Invalid_Assembly ()
366 string tempFile = Path.GetTempFileName ();
367 using (StreamWriter sw = File.CreateText (tempFile)) {
368 sw.WriteLine ("foo");
373 Assembly.LoadFrom (tempFile);
375 } catch (BadImageFormatException ex) {
376 Assert.IsNull (ex.InnerException, "#2");
378 File.Delete (tempFile);
383 public void LoadFrom_NonExisting_Assembly ()
385 string tempFile = Path.GetTempFileName ();
386 File.Delete (tempFile);
389 Assembly.LoadFrom (tempFile);
391 } catch (FileNotFoundException ex) {
392 Assert.IsNull (ex.InnerException, "#2");
394 File.Delete (tempFile);
399 public void LoadWithPartialName ()
401 string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_test_net_4_0", "corlib_test_net_4_5", "corlib_test_net_4_x", "corlib_plattest", "mscorlibtests", "BclTests" };
403 foreach (string s in names)
404 if (Assembly.LoadWithPartialName (s) != null)
406 Assert.Fail ("Was not able to load any corlib test");
410 public void GetObjectData_Info_Null ()
412 Assembly corlib = typeof (int).Assembly;
414 corlib.GetObjectData (null, new StreamingContext (
415 StreamingContextStates.All));
417 } catch (ArgumentNullException ex) {
418 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
419 Assert.IsNull (ex.InnerException, "#3");
420 Assert.IsNotNull (ex.Message, "#4");
421 Assert.IsNotNull (ex.ParamName, "#5");
422 Assert.AreEqual ("info", ex.ParamName, "#6");
427 public void GetReferencedAssemblies ()
429 Assembly corlib_test = Assembly.GetExecutingAssembly ();
430 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
431 foreach (AssemblyName an in names) {
432 Assert.IsNull (an.CodeBase, "CodeBase");
433 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
434 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
435 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
436 Assert.IsNotNull (an.FullName, "FullName");
437 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
438 Assert.IsNull (an.KeyPair, "KeyPair");
439 Assert.IsNotNull (an.Name, "Name");
440 Assert.IsNotNull (an.Version, "Version");
441 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
442 an.VersionCompatibility, "VersionCompatibility");
446 #if !MONOTOUCH // Reflection.Emit is not supported.
448 public void Location_Empty() {
449 string assemblyFileName = Path.Combine (
450 Path.GetTempPath (), "AssemblyLocation.dll");
453 AssemblyName assemblyName = new AssemblyName ();
454 assemblyName.Name = "AssemblyLocation";
456 AssemblyBuilder ab = AppDomain.CurrentDomain
457 .DefineDynamicAssembly (assemblyName,
458 AssemblyBuilderAccess.Save,
460 AppDomain.CurrentDomain.Evidence);
461 ab.Save (Path.GetFileName (assemblyFileName));
463 using (FileStream fs = File.OpenRead (assemblyFileName)) {
464 byte[] buffer = new byte[fs.Length];
465 fs.Read (buffer, 0, buffer.Length);
466 Assembly assembly = Assembly.Load (buffer);
467 Assert.AreEqual (string.Empty, assembly.Location);
471 File.Delete (assemblyFileName);
476 public void SateliteAssemblyForInMemoryAssembly ()
478 string assemblyFileName = Path.Combine (
479 Path.GetTempPath (), "AssemblyLocation1.dll");
482 AssemblyName assemblyName = new AssemblyName ();
483 assemblyName.Name = "AssemblyLocation1";
485 AssemblyBuilder ab = AppDomain.CurrentDomain
486 .DefineDynamicAssembly (assemblyName,
487 AssemblyBuilderAccess.Save,
488 Path.GetTempPath ());
490 ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
491 TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
493 MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
494 ILGenerator gen = methodBuilder.GetILGenerator ();
497 // var resourceManager = new ResourceManager (typeof (Program));
498 // resourceManager.GetString ("test");
500 gen.Emit (OpCodes.Ldtoken, typeBuilder);
501 gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
502 gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
503 gen.Emit (OpCodes.Ldstr, "test");
504 gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
505 gen.Emit (OpCodes.Pop);
506 gen.Emit (OpCodes.Ret);
508 typeBuilder.CreateType ();
510 ab.Save (Path.GetFileName (assemblyFileName));
512 using (FileStream fs = File.OpenRead (assemblyFileName)) {
513 byte[] buffer = new byte[fs.Length];
514 fs.Read (buffer, 0, buffer.Length);
515 Assembly assembly = Assembly.Load (buffer);
517 var mm = assembly.GetType ("Program").GetMethod ("TestCall");
519 mm.Invoke (null, null);
521 } catch (TargetInvocationException e) {
522 Assert.IsTrue (e.InnerException is MissingManifestResourceException);
528 File.Delete (assemblyFileName);
533 [Category ("NotWorking")]
534 public void bug78464 ()
536 string assemblyFileName = Path.Combine (
537 Path.GetTempPath (), "bug78464.dll");
540 // execute test in separate appdomain to allow assembly to be unloaded
541 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
542 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
544 crossDomainTester.bug78464 (assemblyFileName);
546 AppDomain.Unload (testDomain);
549 File.Delete (assemblyFileName);
554 [Category("MobileNotWorking")]
555 public void bug78465 ()
557 string assemblyFileName = Path.Combine (
558 Path.GetTempPath (), "bug78465.dll");
561 AssemblyName assemblyName = new AssemblyName ();
562 assemblyName.Name = "bug78465";
564 AssemblyBuilder ab = AppDomain.CurrentDomain
565 .DefineDynamicAssembly (assemblyName,
566 AssemblyBuilderAccess.Save,
567 Path.GetDirectoryName (assemblyFileName),
568 AppDomain.CurrentDomain.Evidence);
569 ab.Save (Path.GetFileName (assemblyFileName));
571 using (FileStream fs = File.OpenRead (assemblyFileName)) {
572 byte[] buffer = new byte[fs.Length];
573 fs.Read (buffer, 0, buffer.Length);
574 Assembly assembly = Assembly.Load (buffer);
575 Assert.AreEqual (string.Empty, assembly.Location, "#1");
579 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
580 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
582 crossDomainTester.bug78465 (assemblyFileName);
584 AppDomain.Unload (testDomain);
587 File.Delete (assemblyFileName);
592 [Category("MobileNotWorking")]
593 public void bug78468 ()
595 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
597 string resourceFileName = Path.Combine (Path.GetTempPath (),
600 using (StreamWriter sw = File.CreateText (resourceFileName)) {
601 sw.WriteLine ("FOO");
606 AssemblyName assemblyName = new AssemblyName ();
607 assemblyName.Name = "bug78468a";
609 AssemblyBuilder ab = AppDomain.CurrentDomain
610 .DefineDynamicAssembly (assemblyName,
611 AssemblyBuilderAccess.Save,
613 AppDomain.CurrentDomain.Evidence);
614 ab.AddResourceFile ("read", "readme.txt");
615 ab.Save (Path.GetFileName (assemblyFileNameA));
619 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
620 byte[] buffer = new byte[fs.Length];
621 fs.Read (buffer, 0, buffer.Length);
622 assembly = Assembly.Load (buffer);
626 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
627 string[] resNames = assembly.GetManifestResourceNames ();
628 Assert.IsNotNull (resNames, "#A2");
629 Assert.AreEqual (1, resNames.Length, "#A3");
630 Assert.AreEqual ("read", resNames[0], "#A4");
631 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
632 Assert.IsNotNull (resInfo, "#A5");
633 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
634 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
635 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
637 assembly.GetManifestResourceStream ("read");
639 } catch (FileNotFoundException) {
642 assembly.GetFile ("readme.txt");
643 Assert.Fail ("#A10");
644 } catch (FileNotFoundException) {
647 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
650 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
651 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
653 crossDomainTester.bug78468 (assemblyFileNameB);
655 AppDomain.Unload (testDomain);
656 File.Delete (assemblyFileNameB);
659 File.Delete (assemblyFileNameA);
660 File.Delete (resourceFileName);
665 [Category ("NotWorking")]
666 public void ReflectionOnlyLoad ()
668 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
670 Assert.IsNotNull (assembly);
671 Assert.IsTrue (assembly.ReflectionOnly);
675 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be directly as files
676 public void ReflectionOnlyLoadFrom ()
678 string loc = typeof (AssemblyTest).Assembly.Location;
679 string filename = Path.GetFileName (loc);
680 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
682 Assert.IsNotNull (assembly);
683 Assert.IsTrue (assembly.ReflectionOnly);
687 [ExpectedException (typeof (ArgumentException))]
688 public void CreateInstanceOnRefOnly ()
690 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
691 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
695 [Category ("NotWorking")] // patch for bug #79720 must be committed first
696 public void Load_Culture ()
698 string tempDir = Path.Combine (Path.GetTempPath (),
699 "MonoTests.System.Reflection.AssemblyTest");
700 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
701 if (!Directory.Exists (cultureTempDir))
702 Directory.CreateDirectory (cultureTempDir);
703 cultureTempDir = Path.Combine (tempDir, "en-US");
704 if (!Directory.Exists (cultureTempDir))
705 Directory.CreateDirectory (cultureTempDir);
708 AppDomain ad = CreateTestDomain (tempDir, true);
710 CrossDomainTester cdt = CreateCrossDomainTester (ad);
714 AssemblyName aname = new AssemblyName ();
715 aname.Name = "culturea";
716 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
718 aname = new AssemblyName ();
719 aname.Name = "culturea";
720 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
722 aname = new AssemblyName ();
723 aname.Name = "culturea";
724 aname.CultureInfo = new CultureInfo ("nl-BE");
725 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
727 aname = new AssemblyName ();
728 aname.Name = "culturea";
729 aname.CultureInfo = CultureInfo.InvariantCulture;
730 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
734 aname = new AssemblyName ();
735 aname.Name = "cultureb";
736 aname.CultureInfo = new CultureInfo ("nl-BE");
737 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
739 aname = new AssemblyName ();
740 aname.Name = "cultureb";
741 aname.CultureInfo = new CultureInfo ("nl-BE");
742 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
744 aname = new AssemblyName ();
745 aname.Name = "cultureb";
746 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
748 aname = new AssemblyName ();
749 aname.Name = "cultureb";
750 aname.CultureInfo = new CultureInfo ("en-US");
751 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
755 aname = new AssemblyName ();
756 aname.Name = "culturec";
757 aname.CultureInfo = new CultureInfo ("nl-BE");
758 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
760 aname = new AssemblyName ();
761 aname.Name = "culturec";
762 aname.CultureInfo = new CultureInfo ("nl-BE");
763 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
765 aname = new AssemblyName ();
766 aname.Name = "culturec";
767 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
769 aname = new AssemblyName ();
770 aname.Name = "culturec";
771 aname.CultureInfo = CultureInfo.InvariantCulture;
772 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
776 aname = new AssemblyName ();
777 aname.Name = "cultured";
778 aname.CultureInfo = new CultureInfo ("nl-BE");
779 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
781 aname = new AssemblyName ();
782 aname.Name = "cultured";
783 aname.CultureInfo = new CultureInfo ("nl-BE");
784 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
786 aname = new AssemblyName ();
787 aname.Name = "cultured";
788 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
790 aname = new AssemblyName ();
791 aname.Name = "cultured";
792 aname.CultureInfo = CultureInfo.InvariantCulture;
793 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
795 AppDomain.Unload (ad);
796 if (Directory.Exists (tempDir))
797 Directory.Delete (tempDir, true);
802 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
803 public void Load_Culture_Mismatch ()
805 string tempDir = Path.Combine (Path.GetTempPath (),
806 "MonoTests.System.Reflection.AssemblyTest");
807 string cultureTempDir = Path.Combine (tempDir, "en-US");
808 if (!Directory.Exists (cultureTempDir))
809 Directory.CreateDirectory (cultureTempDir);
811 AppDomain ad = CreateTestDomain (tempDir, true);
813 CrossDomainTester cdt = CreateCrossDomainTester (ad);
817 AssemblyName aname = new AssemblyName ();
818 aname.Name = "bug79712a";
819 aname.CultureInfo = new CultureInfo ("nl-BE");
820 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
822 aname = new AssemblyName ();
823 aname.Name = "bug79712a";
824 aname.CultureInfo = CultureInfo.InvariantCulture;
825 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
829 aname = new AssemblyName ();
830 aname.Name = "bug79712b";
831 aname.CultureInfo = new CultureInfo ("nl-BE");
832 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
834 aname = new AssemblyName ();
835 aname.Name = "bug79712b";
836 aname.CultureInfo = new CultureInfo ("en-US");
837 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
839 AppDomain.Unload (ad);
840 if (Directory.Exists (tempDir))
841 Directory.Delete (tempDir, true);
847 [Category("MobileNotWorking")]
848 public void Load_PartialVersion ()
850 string tempDir = Path.Combine (Path.GetTempPath (),
851 "MonoTests.System.Reflection.AssemblyTest");
852 if (!Directory.Exists (tempDir))
853 Directory.CreateDirectory (tempDir);
855 AppDomain ad = CreateTestDomain (tempDir, true);
857 CrossDomainTester cdt = CreateCrossDomainTester (ad);
859 AssemblyName aname = new AssemblyName ();
860 aname.Name = "bug79715";
861 aname.Version = new Version (1, 2, 3, 4);
862 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
864 aname = new AssemblyName ();
865 aname.Name = "bug79715";
866 aname.Version = new Version (1, 2);
867 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
868 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
870 aname = new AssemblyName ();
871 aname.Name = "bug79715";
872 aname.Version = new Version (1, 2, 3);
873 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
874 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
876 aname = new AssemblyName ();
877 aname.Name = "bug79715";
878 aname.Version = new Version (1, 2, 3, 4);
879 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
880 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
882 AppDomain.Unload (ad);
883 if (Directory.Exists (tempDir))
884 Directory.Delete (tempDir, true);
888 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
890 AppDomainSetup setup = new AppDomainSetup ();
891 setup.ApplicationBase = baseDirectory;
892 setup.ApplicationName = "testdomain";
894 AppDomain ad = AppDomain.CreateDomain ("testdomain",
895 AppDomain.CurrentDomain.Evidence, setup);
897 if (assemblyResolver) {
898 Assembly ea = Assembly.GetExecutingAssembly ();
899 ad.CreateInstanceFrom (ea.CodeBase,
900 typeof (AssemblyResolveHandler).FullName,
902 BindingFlags.Public | BindingFlags.Instance,
904 new object [] { ea.Location, ea.FullName },
905 CultureInfo.InvariantCulture,
913 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
915 Type testerType = typeof (CrossDomainTester);
916 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
917 testerType.Assembly.FullName, testerType.FullName, false,
918 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
919 CultureInfo.InvariantCulture, new object[0], null);
922 private class CrossDomainTester : MarshalByRefObject
924 public void GenerateAssembly (AssemblyName aname, string path)
926 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
927 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
928 ab.Save (Path.GetFileName (path));
931 public void Load (AssemblyName assemblyRef)
933 Assembly.Load (assemblyRef);
936 public void LoadFrom (string assemblyFile)
938 Assembly.LoadFrom (assemblyFile);
941 public bool AssertLoad (AssemblyName assemblyRef)
944 Assembly.Load (assemblyRef);
951 public bool AssertLoad (string assemblyString)
954 Assembly.Load (assemblyString);
961 public bool AssertFileLoadException (AssemblyName assemblyRef)
964 Assembly.Load (assemblyRef);
966 } catch (FileLoadException) {
971 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
974 Assembly.Load (assemblyRef);
976 } catch (FileNotFoundException) {
981 public void bug78464 (string assemblyFileName)
983 AssemblyName assemblyName = new AssemblyName ();
984 assemblyName.Name = "bug78464";
986 AssemblyBuilder ab = AppDomain.CurrentDomain
987 .DefineDynamicAssembly (assemblyName,
988 AssemblyBuilderAccess.Save,
989 Path.GetDirectoryName (assemblyFileName),
990 AppDomain.CurrentDomain.Evidence);
991 ab.Save (Path.GetFileName (assemblyFileName));
995 using (FileStream fs = File.OpenRead (assemblyFileName)) {
996 byte[] buffer = new byte[fs.Length];
997 fs.Read (buffer, 0, buffer.Length);
998 assembly = Assembly.Load (buffer);
1002 Assert.AreEqual (string.Empty, assembly.Location, "#1");
1004 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1005 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1006 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
1007 // note: we cannot check if directory names match, as MS.NET seems to
1008 // convert directory part of assembly location to lowercase
1009 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
1012 public void bug78465 (string assemblyFileName)
1014 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1015 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1016 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1017 // note: we cannot check if directory names match, as MS.NET seems to
1018 // convert directory part of assembly location to lowercase
1019 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1022 public void bug78468 (string assemblyFileName)
1024 AssemblyName assemblyName = new AssemblyName ();
1025 assemblyName.Name = "bug78468b";
1027 AssemblyBuilder ab = AppDomain.CurrentDomain
1028 .DefineDynamicAssembly (assemblyName,
1029 AssemblyBuilderAccess.Save,
1030 Path.GetDirectoryName (assemblyFileName),
1031 AppDomain.CurrentDomain.Evidence);
1032 ab.AddResourceFile ("read", "readme.txt");
1033 ab.Save (Path.GetFileName (assemblyFileName));
1035 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1036 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1037 string[] resNames = assembly.GetManifestResourceNames ();
1038 Assert.IsNotNull (resNames, "#B2");
1039 Assert.AreEqual (1, resNames.Length, "#B3");
1040 Assert.AreEqual ("read", resNames[0], "#B4");
1041 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1042 Assert.IsNotNull (resInfo, "#B5");
1043 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1044 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1045 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1046 Stream s = assembly.GetManifestResourceStream ("read");
1047 Assert.IsNotNull (s, "#B9");
1049 s = assembly.GetFile ("readme.txt");
1050 Assert.IsNotNull (s, "#B10");
1056 public void bug79872 ()
1058 Random rnd = new Random ();
1063 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1064 if (Directory.Exists (outdir)) {
1066 Directory.Delete (outdir, true);
1073 Directory.CreateDirectory (outdir);
1075 AssemblyName an = new AssemblyName ();
1076 an.Name = "bug79872";
1077 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1078 string dllname = "bug79872.dll";
1079 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1080 string netmodule = "bug79872.netmodule";
1081 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1082 TypeBuilder a1 = mb2.DefineType ("A");
1088 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1092 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1096 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1097 } catch (BadImageFormatException) {
1098 ok = true; // mono and .net 2.0 throw this
1099 } catch (FileLoadException) {
1100 ok = true; // .net 1.1 throws this
1104 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1106 Directory.Delete (outdir, true);
1111 public void ManifestModule ()
1113 Assembly assembly = typeof (int).Assembly;
1114 Module module = assembly.ManifestModule;
1115 Assert.IsNotNull (module, "#1");
1117 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1120 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1122 Assert.IsFalse (module.IsResource (), "#4");
1123 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1124 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1125 Assert.AreSame (module, assembly.ManifestModule, "#7");
1130 private class AssemblyResolveHandler
1132 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1134 _assemblyFile = assemblyFile;
1135 _assemblyName = assemblyName;
1137 AppDomain.CurrentDomain.AssemblyResolve +=
1138 new ResolveEventHandler (ResolveAssembly);
1141 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1143 if (args.Name == _assemblyName)
1144 return Assembly.LoadFrom (_assemblyFile);
1149 private readonly string _assemblyFile;
1150 private readonly string _assemblyName;
1153 protected internal class Bug328812_NestedFamORAssem { };
1156 public void bug328812 ()
1158 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1159 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1160 // Just a sanity check, in case the above passed for some other reason
1161 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1165 public void GetCustomAttributes_AttributeType_Null ()
1167 Assembly a = typeof (int).Assembly;
1169 a.GetCustomAttributes (null, false);
1171 } catch (ArgumentNullException ex) {
1172 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1173 Assert.IsNull (ex.InnerException, "#3");
1174 Assert.IsNotNull (ex.Message, "#4");
1175 Assert.IsNotNull (ex.ParamName, "#5");
1176 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1181 public void GetTypeWithEmptyStringShouldThrow ()
1184 typeof (string).Assembly.GetType ("");
1186 } catch (ArgumentException) {}
1191 public void DefinedTypes_Equality ()
1193 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1194 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1196 Assert.AreSame (x1, x2, "#1");
1201 public class TestDefinedTypes