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)
8 // Aleksey Kliger (aleksey@xamarin.com)
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
11 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using NUnit.Framework;
36 using System.Configuration.Assemblies;
37 using System.Globalization;
39 using System.Reflection;
41 using System.Reflection.Emit;
43 using System.Threading;
44 using System.Runtime.Serialization;
45 using System.Runtime.CompilerServices;
46 using System.Security;
48 using System.Resources;
50 namespace MonoTests.System.Reflection
53 public class AssemblyTest
55 static string TempFolder = Path.Combine (Path.GetTempPath (),
56 "MonoTests.System.Reflection.AssemblyTest");
61 while (Directory.Exists (TempFolder))
62 TempFolder = Path.Combine (TempFolder, "2");
63 Directory.CreateDirectory (TempFolder);
67 public void TearDown ()
70 // This throws an exception under MS.NET, since the directory contains loaded
72 Directory.Delete (TempFolder, true);
78 public void CreateInstance()
80 Type type = typeof (AssemblyTest);
81 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
82 Assert.IsNotNull (obj, "#01");
83 Assert.AreEqual (GetType (), obj.GetType (), "#02");
87 public void CreateInvalidInstance()
89 Type type = typeof (AssemblyTest);
90 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
91 Assert.IsNull (obj, "#03");
95 [ExpectedException (typeof (ArgumentException))]
96 public void GetType_TypeName_Invalid ()
98 typeof (int).Assembly.GetType ("&blabla", true, true);
102 [ExpectedException (typeof (ArgumentException))]
103 public void GetType_Invalid_RefPtr () {
104 typeof (int).Assembly.GetType ("System.Int32&*", true, true);
108 [ExpectedException (typeof (ArgumentException))]
109 public void GetType_Invalid_RefArray () {
110 typeof (int).Assembly.GetType ("System.Int32&[]", true, true);
114 [ExpectedException (typeof (ArgumentException))]
115 public void GetType_Invalid_RefGeneric () {
116 typeof (int).Assembly.GetType ("System.Tuple`1&[System.Int32]", true, true);
120 [ExpectedException (typeof (ArgumentException))]
121 public void GetType_Invalid_PtrGeneric () {
122 typeof (int).Assembly.GetType ("System.Tuple`1*[System.Int32]", true, true);
126 public void GetType_ComposeModifiers () {
127 var a = typeof(int).Assembly;
129 var e1 = typeof (Int32).MakePointerType().MakeByRefType();
130 var t1 = a.GetType ("System.Int32*&", true, true);
131 Assert.AreEqual (e1, t1, "#1");
133 var e2 = typeof (Int32).MakeArrayType(2).MakeByRefType();
134 var t2 = a.GetType ("System.Int32[,]&", true, true);
135 Assert.AreEqual (e2, t2, "#2");
137 var e3 = typeof (Int32).MakePointerType().MakeArrayType();
138 var t3 = a.GetType ("System.Int32*[]", true, true);
139 Assert.AreEqual (e3, t3, "#3");
141 var e4 = typeof (Int32).MakeArrayType().MakePointerType().MakePointerType().MakeArrayType().MakePointerType().MakeByRefType();
142 var t4 = a.GetType ("System.Int32[]**[]*&", true, true);
143 Assert.AreEqual (e4, t4, "#4");
147 [Test] // bug #334203
148 public void GetType_TypeName_AssemblyName ()
150 Assembly a = typeof (int).Assembly;
151 string typeName = typeof (string).AssemblyQualifiedName;
153 a.GetType (typeName, true, false);
155 } catch (ArgumentException ex) {
156 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
157 Assert.IsNull (ex.InnerException, "#A3");
158 Assert.IsNotNull (ex.Message, "#A4");
159 Assert.IsNull (ex.ParamName, "#A5");
162 Type type = a.GetType (typeName, false);
163 Assert.IsNull (type, "#B1");
164 type = a.GetType (typeName, false, true);
165 Assert.IsNull (type, "#B2");
169 public void GetEntryAssembly ()
171 // note: only available in default appdomain
172 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
173 // Not sure we should emulate this behavior.
175 string fname = AppDomain.CurrentDomain.FriendlyName;
176 if (fname.EndsWith (".dll")) { // nunit-console
177 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
178 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
180 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
181 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
184 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
185 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
189 #if !MONOTOUCH // Reflection.Emit is not supported.
191 [Category("AndroidNotWorking")] // Missing Mono.CompilerServices.SymbolWriter
192 public void GetModules_MissingFile ()
194 AssemblyName newName = new AssemblyName ();
195 newName.Name = "AssemblyTest";
197 AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
199 ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
201 ab.Save ("test_assembly.dll");
203 File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
205 Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
209 } catch (FileNotFoundException ex) {
210 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
215 [Category ("NotWorking")]
217 public void Corlib ()
219 Assembly corlib = typeof (int).Assembly;
220 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
221 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
222 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
223 Assert.IsNotNull (corlib.Evidence, "Evidence");
224 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
226 // corlib doesn't reference anything
227 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
228 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
229 // not really "true" but it's even more trusted so...
230 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
231 Assert.AreEqual (0, corlib.HostContext, "HostContext");
232 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
233 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
234 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
238 public void Corlib_test ()
240 Assembly corlib_test = Assembly.GetExecutingAssembly ();
242 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
243 Assert.IsNull (corlib_test.Evidence, "Evidence");
245 Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
246 Assert.IsNull (corlib_test.Evidence, "Evidence");
248 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
249 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
251 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
253 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
254 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
255 Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
257 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
258 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
262 public void GetAssembly ()
264 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
265 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
269 public void GetFile_Null ()
272 Assembly.GetExecutingAssembly ().GetFile (null);
274 } catch (ArgumentNullException ex) {
275 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
276 Assert.IsNull (ex.InnerException, "#3");
277 Assert.IsNotNull (ex.Message, "#4");
278 Assert.IsNull (ex.ParamName, "#5");
283 public void GetFile_Empty ()
286 Assembly.GetExecutingAssembly ().GetFile (
289 } catch (ArgumentException ex) {
290 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
291 Assert.IsNull (ex.InnerException, "#3");
292 Assert.IsNotNull (ex.Message, "#4");
293 Assert.IsNull (ex.ParamName, "#5");
298 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
299 public void GetFiles_False ()
301 Assembly corlib = typeof (int).Assembly;
302 FileStream[] fss = corlib.GetFiles ();
303 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
305 Assembly corlib_test = Assembly.GetExecutingAssembly ();
306 fss = corlib_test.GetFiles ();
307 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
311 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
312 public void GetFiles_True ()
314 Assembly corlib = typeof (int).Assembly;
315 FileStream[] fss = corlib.GetFiles ();
316 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
318 Assembly corlib_test = Assembly.GetExecutingAssembly ();
319 fss = corlib_test.GetFiles ();
320 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
324 public void GetManifestResourceStream_Name_Empty ()
326 Assembly corlib = typeof (int).Assembly;
329 corlib.GetManifestResourceStream (string.Empty);
331 } catch (ArgumentException ex) {
332 // String cannot have zero length
333 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
334 Assert.IsNull (ex.InnerException, "#A3");
335 Assert.IsNotNull (ex.Message, "#A4");
338 corlib.GetManifestResourceStream (typeof (int), string.Empty);
341 corlib.GetManifestResourceStream ((Type) null, string.Empty);
343 } catch (ArgumentException ex) {
344 // String cannot have zero length
345 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
346 Assert.IsNull (ex.InnerException, "#B3");
347 Assert.IsNotNull (ex.Message, "#B4");
352 public void GetManifestResourceStream_Name_Null ()
354 Assembly corlib = typeof (int).Assembly;
357 corlib.GetManifestResourceStream ((string) null);
359 } catch (ArgumentNullException ex) {
360 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
361 Assert.IsNull (ex.InnerException, "#A3");
362 Assert.IsNotNull (ex.Message, "#A4");
365 corlib.GetManifestResourceStream (typeof (int), (string) null);
368 corlib.GetManifestResourceStream ((Type) null, (string) null);
370 } catch (ArgumentNullException ex) {
371 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
372 Assert.IsNull (ex.InnerException, "#B3");
373 Assert.IsNotNull (ex.Message, "#B4");
374 Assert.IsNotNull (ex.ParamName, "#B5");
375 Assert.AreEqual ("type", ex.ParamName, "#B6");
380 public void IsDefined_AttributeType_Null ()
382 Assembly corlib = typeof (int).Assembly;
385 corlib.IsDefined ((Type) null, false);
387 } catch (ArgumentNullException ex) {
388 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
389 Assert.IsNull (ex.InnerException, "#3");
390 Assert.IsNotNull (ex.Message, "#4");
391 Assert.IsNotNull (ex.ParamName, "#5");
392 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
397 public void LoadFrom_Empty_Assembly ()
399 string tempFile = Path.GetTempFileName ();
402 Assembly.LoadFrom (tempFile);
404 } catch (BadImageFormatException ex) {
405 Assert.IsNull (ex.InnerException, "#2");
407 File.Delete (tempFile);
412 public void LoadFrom_Invalid_Assembly ()
414 string tempFile = Path.GetTempFileName ();
415 using (StreamWriter sw = File.CreateText (tempFile)) {
416 sw.WriteLine ("foo");
421 Assembly.LoadFrom (tempFile);
423 } catch (BadImageFormatException ex) {
424 Assert.IsNull (ex.InnerException, "#2");
426 File.Delete (tempFile);
431 public void LoadFrom_NonExisting_Assembly ()
433 string tempFile = Path.GetTempFileName ();
434 File.Delete (tempFile);
437 Assembly.LoadFrom (tempFile);
439 } catch (FileNotFoundException ex) {
440 Assert.IsNull (ex.InnerException, "#2");
442 File.Delete (tempFile);
447 public void LoadWithPartialName ()
449 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" };
451 foreach (string s in names)
452 if (Assembly.LoadWithPartialName (s) != null)
454 Assert.Fail ("Was not able to load any corlib test");
458 public void GetObjectData_Info_Null ()
460 Assembly corlib = typeof (int).Assembly;
462 corlib.GetObjectData (null, new StreamingContext (
463 StreamingContextStates.All));
465 } catch (ArgumentNullException ex) {
466 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
467 Assert.IsNull (ex.InnerException, "#3");
468 Assert.IsNotNull (ex.Message, "#4");
469 Assert.IsNotNull (ex.ParamName, "#5");
470 Assert.AreEqual ("info", ex.ParamName, "#6");
475 public void GetReferencedAssemblies ()
477 Assembly corlib_test = Assembly.GetExecutingAssembly ();
478 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
479 foreach (AssemblyName an in names) {
480 Assert.IsNull (an.CodeBase, "CodeBase");
481 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
482 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
483 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
484 Assert.IsNotNull (an.FullName, "FullName");
485 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
486 Assert.IsNull (an.KeyPair, "KeyPair");
487 Assert.IsNotNull (an.Name, "Name");
488 Assert.IsNotNull (an.Version, "Version");
489 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
490 an.VersionCompatibility, "VersionCompatibility");
494 #if !MONOTOUCH // Reflection.Emit is not supported.
496 public void Location_Empty() {
497 string assemblyFileName = Path.Combine (
498 Path.GetTempPath (), "AssemblyLocation.dll");
501 AssemblyName assemblyName = new AssemblyName ();
502 assemblyName.Name = "AssemblyLocation";
504 AssemblyBuilder ab = AppDomain.CurrentDomain
505 .DefineDynamicAssembly (assemblyName,
506 AssemblyBuilderAccess.Save,
508 AppDomain.CurrentDomain.Evidence);
509 ab.Save (Path.GetFileName (assemblyFileName));
511 using (FileStream fs = File.OpenRead (assemblyFileName)) {
512 byte[] buffer = new byte[fs.Length];
513 fs.Read (buffer, 0, buffer.Length);
514 Assembly assembly = Assembly.Load (buffer);
515 Assert.AreEqual (string.Empty, assembly.Location);
519 File.Delete (assemblyFileName);
524 public void SateliteAssemblyForInMemoryAssembly ()
526 string assemblyFileName = Path.Combine (
527 Path.GetTempPath (), "AssemblyLocation1.dll");
530 AssemblyName assemblyName = new AssemblyName ();
531 assemblyName.Name = "AssemblyLocation1";
533 AssemblyBuilder ab = AppDomain.CurrentDomain
534 .DefineDynamicAssembly (assemblyName,
535 AssemblyBuilderAccess.Save,
536 Path.GetTempPath ());
538 ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
539 TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
541 MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
542 ILGenerator gen = methodBuilder.GetILGenerator ();
545 // var resourceManager = new ResourceManager (typeof (Program));
546 // resourceManager.GetString ("test");
548 gen.Emit (OpCodes.Ldtoken, typeBuilder);
549 gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
550 gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
551 gen.Emit (OpCodes.Ldstr, "test");
552 gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
553 gen.Emit (OpCodes.Pop);
554 gen.Emit (OpCodes.Ret);
556 typeBuilder.CreateType ();
558 ab.Save (Path.GetFileName (assemblyFileName));
560 using (FileStream fs = File.OpenRead (assemblyFileName)) {
561 byte[] buffer = new byte[fs.Length];
562 fs.Read (buffer, 0, buffer.Length);
563 Assembly assembly = Assembly.Load (buffer);
565 var mm = assembly.GetType ("Program").GetMethod ("TestCall");
567 mm.Invoke (null, null);
569 } catch (TargetInvocationException e) {
570 Assert.IsTrue (e.InnerException is MissingManifestResourceException);
576 File.Delete (assemblyFileName);
581 [Category ("NotWorking")]
582 public void bug78464 ()
584 string assemblyFileName = Path.Combine (
585 Path.GetTempPath (), "bug78464.dll");
588 // execute test in separate appdomain to allow assembly to be unloaded
589 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
590 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
592 crossDomainTester.bug78464 (assemblyFileName);
594 AppDomain.Unload (testDomain);
597 File.Delete (assemblyFileName);
602 [Category("MobileNotWorking")]
603 public void bug78465 ()
605 string assemblyFileName = Path.Combine (
606 Path.GetTempPath (), "bug78465.dll");
609 AssemblyName assemblyName = new AssemblyName ();
610 assemblyName.Name = "bug78465";
612 AssemblyBuilder ab = AppDomain.CurrentDomain
613 .DefineDynamicAssembly (assemblyName,
614 AssemblyBuilderAccess.Save,
615 Path.GetDirectoryName (assemblyFileName),
616 AppDomain.CurrentDomain.Evidence);
617 ab.Save (Path.GetFileName (assemblyFileName));
619 using (FileStream fs = File.OpenRead (assemblyFileName)) {
620 byte[] buffer = new byte[fs.Length];
621 fs.Read (buffer, 0, buffer.Length);
622 Assembly assembly = Assembly.Load (buffer);
623 Assert.AreEqual (string.Empty, assembly.Location, "#1");
627 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
628 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
630 crossDomainTester.bug78465 (assemblyFileName);
632 AppDomain.Unload (testDomain);
635 File.Delete (assemblyFileName);
640 [Category("MobileNotWorking")]
641 public void bug78468 ()
643 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
645 string resourceFileName = Path.Combine (Path.GetTempPath (),
648 using (StreamWriter sw = File.CreateText (resourceFileName)) {
649 sw.WriteLine ("FOO");
654 AssemblyName assemblyName = new AssemblyName ();
655 assemblyName.Name = "bug78468a";
657 AssemblyBuilder ab = AppDomain.CurrentDomain
658 .DefineDynamicAssembly (assemblyName,
659 AssemblyBuilderAccess.Save,
661 AppDomain.CurrentDomain.Evidence);
662 ab.AddResourceFile ("read", "readme.txt");
663 ab.Save (Path.GetFileName (assemblyFileNameA));
667 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
668 byte[] buffer = new byte[fs.Length];
669 fs.Read (buffer, 0, buffer.Length);
670 assembly = Assembly.Load (buffer);
674 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
675 string[] resNames = assembly.GetManifestResourceNames ();
676 Assert.IsNotNull (resNames, "#A2");
677 Assert.AreEqual (1, resNames.Length, "#A3");
678 Assert.AreEqual ("read", resNames[0], "#A4");
679 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
680 Assert.IsNotNull (resInfo, "#A5");
681 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
682 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
683 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
685 assembly.GetManifestResourceStream ("read");
687 } catch (FileNotFoundException) {
690 assembly.GetFile ("readme.txt");
691 Assert.Fail ("#A10");
692 } catch (FileNotFoundException) {
695 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
698 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
699 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
701 crossDomainTester.bug78468 (assemblyFileNameB);
703 AppDomain.Unload (testDomain);
704 File.Delete (assemblyFileNameB);
707 File.Delete (assemblyFileNameA);
708 File.Delete (resourceFileName);
713 [Category ("NotWorking")]
714 public void ReflectionOnlyLoad ()
716 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
718 Assert.IsNotNull (assembly);
719 Assert.IsTrue (assembly.ReflectionOnly);
723 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be directly as files
724 public void ReflectionOnlyLoadFrom ()
726 string loc = typeof (AssemblyTest).Assembly.Location;
727 string filename = Path.GetFileName (loc);
728 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
730 Assert.IsNotNull (assembly);
731 Assert.IsTrue (assembly.ReflectionOnly);
735 [ExpectedException (typeof (ArgumentException))]
736 public void CreateInstanceOnRefOnly ()
738 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
739 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
743 [Category ("NotWorking")] // patch for bug #79720 must be committed first
744 public void Load_Culture ()
746 string tempDir = Path.Combine (Path.GetTempPath (),
747 "MonoTests.System.Reflection.AssemblyTest");
748 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
749 if (!Directory.Exists (cultureTempDir))
750 Directory.CreateDirectory (cultureTempDir);
751 cultureTempDir = Path.Combine (tempDir, "en-US");
752 if (!Directory.Exists (cultureTempDir))
753 Directory.CreateDirectory (cultureTempDir);
756 AppDomain ad = CreateTestDomain (tempDir, true);
758 CrossDomainTester cdt = CreateCrossDomainTester (ad);
762 AssemblyName aname = new AssemblyName ();
763 aname.Name = "culturea";
764 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
766 aname = new AssemblyName ();
767 aname.Name = "culturea";
768 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
770 aname = new AssemblyName ();
771 aname.Name = "culturea";
772 aname.CultureInfo = new CultureInfo ("nl-BE");
773 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
775 aname = new AssemblyName ();
776 aname.Name = "culturea";
777 aname.CultureInfo = CultureInfo.InvariantCulture;
778 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
782 aname = new AssemblyName ();
783 aname.Name = "cultureb";
784 aname.CultureInfo = new CultureInfo ("nl-BE");
785 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
787 aname = new AssemblyName ();
788 aname.Name = "cultureb";
789 aname.CultureInfo = new CultureInfo ("nl-BE");
790 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
792 aname = new AssemblyName ();
793 aname.Name = "cultureb";
794 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
796 aname = new AssemblyName ();
797 aname.Name = "cultureb";
798 aname.CultureInfo = new CultureInfo ("en-US");
799 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
803 aname = new AssemblyName ();
804 aname.Name = "culturec";
805 aname.CultureInfo = new CultureInfo ("nl-BE");
806 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
808 aname = new AssemblyName ();
809 aname.Name = "culturec";
810 aname.CultureInfo = new CultureInfo ("nl-BE");
811 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
813 aname = new AssemblyName ();
814 aname.Name = "culturec";
815 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
817 aname = new AssemblyName ();
818 aname.Name = "culturec";
819 aname.CultureInfo = CultureInfo.InvariantCulture;
820 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
824 aname = new AssemblyName ();
825 aname.Name = "cultured";
826 aname.CultureInfo = new CultureInfo ("nl-BE");
827 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
829 aname = new AssemblyName ();
830 aname.Name = "cultured";
831 aname.CultureInfo = new CultureInfo ("nl-BE");
832 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
834 aname = new AssemblyName ();
835 aname.Name = "cultured";
836 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
838 aname = new AssemblyName ();
839 aname.Name = "cultured";
840 aname.CultureInfo = CultureInfo.InvariantCulture;
841 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
843 AppDomain.Unload (ad);
844 if (Directory.Exists (tempDir))
845 Directory.Delete (tempDir, true);
850 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
851 public void Load_Culture_Mismatch ()
853 string tempDir = Path.Combine (Path.GetTempPath (),
854 "MonoTests.System.Reflection.AssemblyTest");
855 string cultureTempDir = Path.Combine (tempDir, "en-US");
856 if (!Directory.Exists (cultureTempDir))
857 Directory.CreateDirectory (cultureTempDir);
859 AppDomain ad = CreateTestDomain (tempDir, true);
861 CrossDomainTester cdt = CreateCrossDomainTester (ad);
865 AssemblyName aname = new AssemblyName ();
866 aname.Name = "bug79712a";
867 aname.CultureInfo = new CultureInfo ("nl-BE");
868 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
870 aname = new AssemblyName ();
871 aname.Name = "bug79712a";
872 aname.CultureInfo = CultureInfo.InvariantCulture;
873 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
877 aname = new AssemblyName ();
878 aname.Name = "bug79712b";
879 aname.CultureInfo = new CultureInfo ("nl-BE");
880 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
882 aname = new AssemblyName ();
883 aname.Name = "bug79712b";
884 aname.CultureInfo = new CultureInfo ("en-US");
885 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
887 AppDomain.Unload (ad);
888 if (Directory.Exists (tempDir))
889 Directory.Delete (tempDir, true);
895 [Category("MobileNotWorking")]
896 public void Load_PartialVersion ()
898 string tempDir = Path.Combine (Path.GetTempPath (),
899 "MonoTests.System.Reflection.AssemblyTest");
900 if (!Directory.Exists (tempDir))
901 Directory.CreateDirectory (tempDir);
903 AppDomain ad = CreateTestDomain (tempDir, true);
905 CrossDomainTester cdt = CreateCrossDomainTester (ad);
907 AssemblyName aname = new AssemblyName ();
908 aname.Name = "bug79715";
909 aname.Version = new Version (1, 2, 3, 4);
910 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
912 aname = new AssemblyName ();
913 aname.Name = "bug79715";
914 aname.Version = new Version (1, 2);
915 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
916 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
918 aname = new AssemblyName ();
919 aname.Name = "bug79715";
920 aname.Version = new Version (1, 2, 3);
921 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
922 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
924 aname = new AssemblyName ();
925 aname.Name = "bug79715";
926 aname.Version = new Version (1, 2, 3, 4);
927 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
928 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
930 AppDomain.Unload (ad);
931 if (Directory.Exists (tempDir))
932 Directory.Delete (tempDir, true);
936 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
938 AppDomainSetup setup = new AppDomainSetup ();
939 setup.ApplicationBase = baseDirectory;
940 setup.ApplicationName = "testdomain";
942 AppDomain ad = AppDomain.CreateDomain ("testdomain",
943 AppDomain.CurrentDomain.Evidence, setup);
945 if (assemblyResolver) {
946 Assembly ea = Assembly.GetExecutingAssembly ();
947 ad.CreateInstanceFrom (ea.CodeBase,
948 typeof (AssemblyResolveHandler).FullName,
950 BindingFlags.Public | BindingFlags.Instance,
952 new object [] { ea.Location, ea.FullName },
953 CultureInfo.InvariantCulture,
961 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
963 Type testerType = typeof (CrossDomainTester);
964 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
965 testerType.Assembly.FullName, testerType.FullName, false,
966 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
967 CultureInfo.InvariantCulture, new object[0], null);
970 private class CrossDomainTester : MarshalByRefObject
972 public void GenerateAssembly (AssemblyName aname, string path)
974 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
975 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
976 ab.Save (Path.GetFileName (path));
979 public void Load (AssemblyName assemblyRef)
981 Assembly.Load (assemblyRef);
984 public void LoadFrom (string assemblyFile)
986 Assembly.LoadFrom (assemblyFile);
989 public bool AssertLoad (AssemblyName assemblyRef)
992 Assembly.Load (assemblyRef);
999 public bool AssertLoad (string assemblyString)
1002 Assembly.Load (assemblyString);
1009 public bool AssertFileLoadException (AssemblyName assemblyRef)
1012 Assembly.Load (assemblyRef);
1014 } catch (FileLoadException) {
1019 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
1022 Assembly.Load (assemblyRef);
1024 } catch (FileNotFoundException) {
1029 public void bug78464 (string assemblyFileName)
1031 AssemblyName assemblyName = new AssemblyName ();
1032 assemblyName.Name = "bug78464";
1034 AssemblyBuilder ab = AppDomain.CurrentDomain
1035 .DefineDynamicAssembly (assemblyName,
1036 AssemblyBuilderAccess.Save,
1037 Path.GetDirectoryName (assemblyFileName),
1038 AppDomain.CurrentDomain.Evidence);
1039 ab.Save (Path.GetFileName (assemblyFileName));
1043 using (FileStream fs = File.OpenRead (assemblyFileName)) {
1044 byte[] buffer = new byte[fs.Length];
1045 fs.Read (buffer, 0, buffer.Length);
1046 assembly = Assembly.Load (buffer);
1050 Assert.AreEqual (string.Empty, assembly.Location, "#1");
1052 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1053 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1054 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
1055 // note: we cannot check if directory names match, as MS.NET seems to
1056 // convert directory part of assembly location to lowercase
1057 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
1060 public void bug78465 (string assemblyFileName)
1062 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1063 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1064 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1065 // note: we cannot check if directory names match, as MS.NET seems to
1066 // convert directory part of assembly location to lowercase
1067 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1070 public void bug78468 (string assemblyFileName)
1072 AssemblyName assemblyName = new AssemblyName ();
1073 assemblyName.Name = "bug78468b";
1075 AssemblyBuilder ab = AppDomain.CurrentDomain
1076 .DefineDynamicAssembly (assemblyName,
1077 AssemblyBuilderAccess.Save,
1078 Path.GetDirectoryName (assemblyFileName),
1079 AppDomain.CurrentDomain.Evidence);
1080 ab.AddResourceFile ("read", "readme.txt");
1081 ab.Save (Path.GetFileName (assemblyFileName));
1083 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1084 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1085 string[] resNames = assembly.GetManifestResourceNames ();
1086 Assert.IsNotNull (resNames, "#B2");
1087 Assert.AreEqual (1, resNames.Length, "#B3");
1088 Assert.AreEqual ("read", resNames[0], "#B4");
1089 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1090 Assert.IsNotNull (resInfo, "#B5");
1091 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1092 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1093 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1094 Stream s = assembly.GetManifestResourceStream ("read");
1095 Assert.IsNotNull (s, "#B9");
1097 s = assembly.GetFile ("readme.txt");
1098 Assert.IsNotNull (s, "#B10");
1104 public void bug79872 ()
1106 Random rnd = new Random ();
1111 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1112 if (Directory.Exists (outdir)) {
1114 Directory.Delete (outdir, true);
1121 Directory.CreateDirectory (outdir);
1123 AssemblyName an = new AssemblyName ();
1124 an.Name = "bug79872";
1125 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1126 string dllname = "bug79872.dll";
1127 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1128 string netmodule = "bug79872.netmodule";
1129 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1130 TypeBuilder a1 = mb2.DefineType ("A");
1136 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1140 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1144 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1145 } catch (BadImageFormatException) {
1146 ok = true; // mono and .net 2.0 throw this
1147 } catch (FileLoadException) {
1148 ok = true; // .net 1.1 throws this
1152 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1154 Directory.Delete (outdir, true);
1159 public void ManifestModule ()
1161 Assembly assembly = typeof (int).Assembly;
1162 Module module = assembly.ManifestModule;
1163 Assert.IsNotNull (module, "#1");
1165 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1168 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1170 Assert.IsFalse (module.IsResource (), "#4");
1171 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1172 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1173 Assert.AreSame (module, assembly.ManifestModule, "#7");
1178 private class AssemblyResolveHandler
1180 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1182 _assemblyFile = assemblyFile;
1183 _assemblyName = assemblyName;
1185 AppDomain.CurrentDomain.AssemblyResolve +=
1186 new ResolveEventHandler (ResolveAssembly);
1189 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1191 if (args.Name == _assemblyName)
1192 return Assembly.LoadFrom (_assemblyFile);
1197 private readonly string _assemblyFile;
1198 private readonly string _assemblyName;
1201 protected internal class Bug328812_NestedFamORAssem { };
1204 public void bug328812 ()
1206 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1207 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1208 // Just a sanity check, in case the above passed for some other reason
1209 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1213 public void GetCustomAttributes_AttributeType_Null ()
1215 Assembly a = typeof (int).Assembly;
1217 a.GetCustomAttributes (null, false);
1219 } catch (ArgumentNullException ex) {
1220 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1221 Assert.IsNull (ex.InnerException, "#3");
1222 Assert.IsNotNull (ex.Message, "#4");
1223 Assert.IsNotNull (ex.ParamName, "#5");
1224 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1229 public void GetTypeWithEmptyStringShouldThrow ()
1232 typeof (string).Assembly.GetType ("");
1234 } catch (ArgumentException) {}
1237 class GetCallingAssemblyCallee {
1240 static void sideEffect () {
1244 // GetCallingAssembly may see an unpredictable
1245 // view of the stack if it's called in tail
1246 // position, or if its caller or the caller's
1247 // caller is inlined. So we put in a side
1248 // effect to get out of tail position, and we
1249 // tag the methods NoInlining to discourage
1251 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1252 public static Assembly Leaf () {
1253 var a = Assembly.GetCallingAssembly ();
1258 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1259 public static Assembly DirectCall () {
1265 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1266 public static Assembly InvokeCall () {
1267 var ty = typeof (GetCallingAssemblyCallee);
1268 var mi = ty.GetMethod("Leaf");
1269 var o = mi.Invoke(null, null);
1276 public void GetCallingAssembly_Direct() {
1277 var a = GetCallingAssemblyCallee.DirectCall ();
1278 Assert.IsNotNull (a);
1280 Assert.AreEqual (GetType().Assembly, a);
1284 public void GetCallingAssembly_SkipsReflection () {
1285 // check that the calling assembly is this
1286 // one, not mscorlib (aka, the reflection
1288 var a = GetCallingAssemblyCallee.InvokeCall ();
1289 Assert.IsNotNull (a);
1291 var invokeAssembly =
1292 typeof (MethodInfo).Assembly;
1293 Assert.AreNotEqual (invokeAssembly, a);
1295 Assert.AreEqual (GetType().Assembly, a);
1300 public void DefinedTypes_Equality ()
1302 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1303 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1305 Assert.AreSame (x1, x2, "#1");
1310 public class TestDefinedTypes