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_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 public void bug78465 ()
556 string assemblyFileName = Path.Combine (
557 Path.GetTempPath (), "bug78465.dll");
560 AssemblyName assemblyName = new AssemblyName ();
561 assemblyName.Name = "bug78465";
563 AssemblyBuilder ab = AppDomain.CurrentDomain
564 .DefineDynamicAssembly (assemblyName,
565 AssemblyBuilderAccess.Save,
566 Path.GetDirectoryName (assemblyFileName),
567 AppDomain.CurrentDomain.Evidence);
568 ab.Save (Path.GetFileName (assemblyFileName));
570 using (FileStream fs = File.OpenRead (assemblyFileName)) {
571 byte[] buffer = new byte[fs.Length];
572 fs.Read (buffer, 0, buffer.Length);
573 Assembly assembly = Assembly.Load (buffer);
574 Assert.AreEqual (string.Empty, assembly.Location, "#1");
578 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
579 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
581 crossDomainTester.bug78465 (assemblyFileName);
583 AppDomain.Unload (testDomain);
586 File.Delete (assemblyFileName);
591 public void bug78468 ()
593 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
595 string resourceFileName = Path.Combine (Path.GetTempPath (),
598 using (StreamWriter sw = File.CreateText (resourceFileName)) {
599 sw.WriteLine ("FOO");
604 AssemblyName assemblyName = new AssemblyName ();
605 assemblyName.Name = "bug78468a";
607 AssemblyBuilder ab = AppDomain.CurrentDomain
608 .DefineDynamicAssembly (assemblyName,
609 AssemblyBuilderAccess.Save,
611 AppDomain.CurrentDomain.Evidence);
612 ab.AddResourceFile ("read", "readme.txt");
613 ab.Save (Path.GetFileName (assemblyFileNameA));
617 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
618 byte[] buffer = new byte[fs.Length];
619 fs.Read (buffer, 0, buffer.Length);
620 assembly = Assembly.Load (buffer);
624 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
625 string[] resNames = assembly.GetManifestResourceNames ();
626 Assert.IsNotNull (resNames, "#A2");
627 Assert.AreEqual (1, resNames.Length, "#A3");
628 Assert.AreEqual ("read", resNames[0], "#A4");
629 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
630 Assert.IsNotNull (resInfo, "#A5");
631 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
632 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
633 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
635 assembly.GetManifestResourceStream ("read");
637 } catch (FileNotFoundException) {
640 assembly.GetFile ("readme.txt");
641 Assert.Fail ("#A10");
642 } catch (FileNotFoundException) {
645 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
648 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
649 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
651 crossDomainTester.bug78468 (assemblyFileNameB);
653 AppDomain.Unload (testDomain);
654 File.Delete (assemblyFileNameB);
657 File.Delete (assemblyFileNameA);
658 File.Delete (resourceFileName);
663 [Category ("NotWorking")]
664 public void ReflectionOnlyLoad ()
666 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
668 Assert.IsNotNull (assembly);
669 Assert.IsTrue (assembly.ReflectionOnly);
673 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be directly as files
674 public void ReflectionOnlyLoadFrom ()
676 string loc = typeof (AssemblyTest).Assembly.Location;
677 string filename = Path.GetFileName (loc);
678 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
680 Assert.IsNotNull (assembly);
681 Assert.IsTrue (assembly.ReflectionOnly);
685 [ExpectedException (typeof (ArgumentException))]
686 public void CreateInstanceOnRefOnly ()
688 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
689 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
693 [Category ("NotWorking")] // patch for bug #79720 must be committed first
694 public void Load_Culture ()
696 string tempDir = Path.Combine (Path.GetTempPath (),
697 "MonoTests.System.Reflection.AssemblyTest");
698 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
699 if (!Directory.Exists (cultureTempDir))
700 Directory.CreateDirectory (cultureTempDir);
701 cultureTempDir = Path.Combine (tempDir, "en-US");
702 if (!Directory.Exists (cultureTempDir))
703 Directory.CreateDirectory (cultureTempDir);
706 AppDomain ad = CreateTestDomain (tempDir, true);
708 CrossDomainTester cdt = CreateCrossDomainTester (ad);
712 AssemblyName aname = new AssemblyName ();
713 aname.Name = "culturea";
714 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
716 aname = new AssemblyName ();
717 aname.Name = "culturea";
718 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
720 aname = new AssemblyName ();
721 aname.Name = "culturea";
722 aname.CultureInfo = new CultureInfo ("nl-BE");
723 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
725 aname = new AssemblyName ();
726 aname.Name = "culturea";
727 aname.CultureInfo = CultureInfo.InvariantCulture;
728 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
732 aname = new AssemblyName ();
733 aname.Name = "cultureb";
734 aname.CultureInfo = new CultureInfo ("nl-BE");
735 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
737 aname = new AssemblyName ();
738 aname.Name = "cultureb";
739 aname.CultureInfo = new CultureInfo ("nl-BE");
740 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
742 aname = new AssemblyName ();
743 aname.Name = "cultureb";
744 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
746 aname = new AssemblyName ();
747 aname.Name = "cultureb";
748 aname.CultureInfo = new CultureInfo ("en-US");
749 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
753 aname = new AssemblyName ();
754 aname.Name = "culturec";
755 aname.CultureInfo = new CultureInfo ("nl-BE");
756 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
758 aname = new AssemblyName ();
759 aname.Name = "culturec";
760 aname.CultureInfo = new CultureInfo ("nl-BE");
761 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
763 aname = new AssemblyName ();
764 aname.Name = "culturec";
765 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
767 aname = new AssemblyName ();
768 aname.Name = "culturec";
769 aname.CultureInfo = CultureInfo.InvariantCulture;
770 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
774 aname = new AssemblyName ();
775 aname.Name = "cultured";
776 aname.CultureInfo = new CultureInfo ("nl-BE");
777 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
779 aname = new AssemblyName ();
780 aname.Name = "cultured";
781 aname.CultureInfo = new CultureInfo ("nl-BE");
782 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
784 aname = new AssemblyName ();
785 aname.Name = "cultured";
786 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
788 aname = new AssemblyName ();
789 aname.Name = "cultured";
790 aname.CultureInfo = CultureInfo.InvariantCulture;
791 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
793 AppDomain.Unload (ad);
794 if (Directory.Exists (tempDir))
795 Directory.Delete (tempDir, true);
800 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
801 public void Load_Culture_Mismatch ()
803 string tempDir = Path.Combine (Path.GetTempPath (),
804 "MonoTests.System.Reflection.AssemblyTest");
805 string cultureTempDir = Path.Combine (tempDir, "en-US");
806 if (!Directory.Exists (cultureTempDir))
807 Directory.CreateDirectory (cultureTempDir);
809 AppDomain ad = CreateTestDomain (tempDir, true);
811 CrossDomainTester cdt = CreateCrossDomainTester (ad);
815 AssemblyName aname = new AssemblyName ();
816 aname.Name = "bug79712a";
817 aname.CultureInfo = new CultureInfo ("nl-BE");
818 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
820 aname = new AssemblyName ();
821 aname.Name = "bug79712a";
822 aname.CultureInfo = CultureInfo.InvariantCulture;
823 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
827 aname = new AssemblyName ();
828 aname.Name = "bug79712b";
829 aname.CultureInfo = new CultureInfo ("nl-BE");
830 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
832 aname = new AssemblyName ();
833 aname.Name = "bug79712b";
834 aname.CultureInfo = new CultureInfo ("en-US");
835 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
837 AppDomain.Unload (ad);
838 if (Directory.Exists (tempDir))
839 Directory.Delete (tempDir, true);
845 public void Load_PartialVersion ()
847 string tempDir = Path.Combine (Path.GetTempPath (),
848 "MonoTests.System.Reflection.AssemblyTest");
849 if (!Directory.Exists (tempDir))
850 Directory.CreateDirectory (tempDir);
852 AppDomain ad = CreateTestDomain (tempDir, true);
854 CrossDomainTester cdt = CreateCrossDomainTester (ad);
856 AssemblyName aname = new AssemblyName ();
857 aname.Name = "bug79715";
858 aname.Version = new Version (1, 2, 3, 4);
859 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
861 aname = new AssemblyName ();
862 aname.Name = "bug79715";
863 aname.Version = new Version (1, 2);
864 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
865 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
867 aname = new AssemblyName ();
868 aname.Name = "bug79715";
869 aname.Version = new Version (1, 2, 3);
870 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
871 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
873 aname = new AssemblyName ();
874 aname.Name = "bug79715";
875 aname.Version = new Version (1, 2, 3, 4);
876 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
877 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
879 AppDomain.Unload (ad);
880 if (Directory.Exists (tempDir))
881 Directory.Delete (tempDir, true);
885 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
887 AppDomainSetup setup = new AppDomainSetup ();
888 setup.ApplicationBase = baseDirectory;
889 setup.ApplicationName = "testdomain";
891 AppDomain ad = AppDomain.CreateDomain ("testdomain",
892 AppDomain.CurrentDomain.Evidence, setup);
894 if (assemblyResolver) {
895 Assembly ea = Assembly.GetExecutingAssembly ();
896 ad.CreateInstanceFrom (ea.CodeBase,
897 typeof (AssemblyResolveHandler).FullName,
899 BindingFlags.Public | BindingFlags.Instance,
901 new object [] { ea.Location, ea.FullName },
902 CultureInfo.InvariantCulture,
910 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
912 Type testerType = typeof (CrossDomainTester);
913 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
914 testerType.Assembly.FullName, testerType.FullName, false,
915 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
916 CultureInfo.InvariantCulture, new object[0], null);
919 private class CrossDomainTester : MarshalByRefObject
921 public void GenerateAssembly (AssemblyName aname, string path)
923 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
924 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
925 ab.Save (Path.GetFileName (path));
928 public void Load (AssemblyName assemblyRef)
930 Assembly.Load (assemblyRef);
933 public void LoadFrom (string assemblyFile)
935 Assembly.LoadFrom (assemblyFile);
938 public bool AssertLoad (AssemblyName assemblyRef)
941 Assembly.Load (assemblyRef);
948 public bool AssertLoad (string assemblyString)
951 Assembly.Load (assemblyString);
958 public bool AssertFileLoadException (AssemblyName assemblyRef)
961 Assembly.Load (assemblyRef);
963 } catch (FileLoadException) {
968 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
971 Assembly.Load (assemblyRef);
973 } catch (FileNotFoundException) {
978 public void bug78464 (string assemblyFileName)
980 AssemblyName assemblyName = new AssemblyName ();
981 assemblyName.Name = "bug78464";
983 AssemblyBuilder ab = AppDomain.CurrentDomain
984 .DefineDynamicAssembly (assemblyName,
985 AssemblyBuilderAccess.Save,
986 Path.GetDirectoryName (assemblyFileName),
987 AppDomain.CurrentDomain.Evidence);
988 ab.Save (Path.GetFileName (assemblyFileName));
992 using (FileStream fs = File.OpenRead (assemblyFileName)) {
993 byte[] buffer = new byte[fs.Length];
994 fs.Read (buffer, 0, buffer.Length);
995 assembly = Assembly.Load (buffer);
999 Assert.AreEqual (string.Empty, assembly.Location, "#1");
1001 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1002 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1003 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
1004 // note: we cannot check if directory names match, as MS.NET seems to
1005 // convert directory part of assembly location to lowercase
1006 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
1009 public void bug78465 (string assemblyFileName)
1011 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1012 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1013 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1014 // note: we cannot check if directory names match, as MS.NET seems to
1015 // convert directory part of assembly location to lowercase
1016 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1019 public void bug78468 (string assemblyFileName)
1021 AssemblyName assemblyName = new AssemblyName ();
1022 assemblyName.Name = "bug78468b";
1024 AssemblyBuilder ab = AppDomain.CurrentDomain
1025 .DefineDynamicAssembly (assemblyName,
1026 AssemblyBuilderAccess.Save,
1027 Path.GetDirectoryName (assemblyFileName),
1028 AppDomain.CurrentDomain.Evidence);
1029 ab.AddResourceFile ("read", "readme.txt");
1030 ab.Save (Path.GetFileName (assemblyFileName));
1032 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1033 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1034 string[] resNames = assembly.GetManifestResourceNames ();
1035 Assert.IsNotNull (resNames, "#B2");
1036 Assert.AreEqual (1, resNames.Length, "#B3");
1037 Assert.AreEqual ("read", resNames[0], "#B4");
1038 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1039 Assert.IsNotNull (resInfo, "#B5");
1040 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1041 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1042 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1043 Stream s = assembly.GetManifestResourceStream ("read");
1044 Assert.IsNotNull (s, "#B9");
1046 s = assembly.GetFile ("readme.txt");
1047 Assert.IsNotNull (s, "#B10");
1053 public void bug79872 ()
1055 Random rnd = new Random ();
1060 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1061 if (Directory.Exists (outdir)) {
1063 Directory.Delete (outdir, true);
1070 Directory.CreateDirectory (outdir);
1072 AssemblyName an = new AssemblyName ();
1073 an.Name = "bug79872";
1074 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1075 string dllname = "bug79872.dll";
1076 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1077 string netmodule = "bug79872.netmodule";
1078 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1079 TypeBuilder a1 = mb2.DefineType ("A");
1085 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1089 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1093 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1094 } catch (BadImageFormatException) {
1095 ok = true; // mono and .net 2.0 throw this
1096 } catch (FileLoadException) {
1097 ok = true; // .net 1.1 throws this
1101 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1103 Directory.Delete (outdir, true);
1108 public void ManifestModule ()
1110 Assembly assembly = typeof (int).Assembly;
1111 Module module = assembly.ManifestModule;
1112 Assert.IsNotNull (module, "#1");
1115 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1117 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1121 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1123 Assert.IsFalse (module.IsResource (), "#4");
1124 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1125 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1126 Assert.AreSame (module, assembly.ManifestModule, "#7");
1131 private class AssemblyResolveHandler
1133 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1135 _assemblyFile = assemblyFile;
1136 _assemblyName = assemblyName;
1138 AppDomain.CurrentDomain.AssemblyResolve +=
1139 new ResolveEventHandler (ResolveAssembly);
1142 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1144 if (args.Name == _assemblyName)
1145 return Assembly.LoadFrom (_assemblyFile);
1150 private readonly string _assemblyFile;
1151 private readonly string _assemblyName;
1154 protected internal class Bug328812_NestedFamORAssem { };
1157 public void bug328812 ()
1159 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1160 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1161 // Just a sanity check, in case the above passed for some other reason
1162 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1166 public void GetCustomAttributes_AttributeType_Null ()
1168 Assembly a = typeof (int).Assembly;
1170 a.GetCustomAttributes (null, false);
1172 } catch (ArgumentNullException ex) {
1173 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1174 Assert.IsNull (ex.InnerException, "#3");
1175 Assert.IsNotNull (ex.Message, "#4");
1176 Assert.IsNotNull (ex.ParamName, "#5");
1177 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1182 public void GetTypeWithEmptyStringShouldThrow ()
1185 typeof (string).Assembly.GetType ("");
1187 } catch (ArgumentException) {}
1192 public void DefinedTypes_Equality ()
1194 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1195 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1197 Assert.AreSame (x1, x2, "#1");
1202 public class TestDefinedTypes