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 public void GetFiles_False ()
252 Assembly corlib = typeof (int).Assembly;
253 FileStream[] fss = corlib.GetFiles ();
254 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
256 Assembly corlib_test = Assembly.GetExecutingAssembly ();
257 fss = corlib_test.GetFiles ();
258 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
262 public void GetFiles_True ()
264 Assembly corlib = typeof (int).Assembly;
265 FileStream[] fss = corlib.GetFiles ();
266 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
268 Assembly corlib_test = Assembly.GetExecutingAssembly ();
269 fss = corlib_test.GetFiles ();
270 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
274 public void GetManifestResourceStream_Name_Empty ()
276 Assembly corlib = typeof (int).Assembly;
279 corlib.GetManifestResourceStream (string.Empty);
281 } catch (ArgumentException ex) {
282 // String cannot have zero length
283 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
284 Assert.IsNull (ex.InnerException, "#A3");
285 Assert.IsNotNull (ex.Message, "#A4");
288 corlib.GetManifestResourceStream (typeof (int), string.Empty);
291 corlib.GetManifestResourceStream ((Type) null, string.Empty);
293 } catch (ArgumentException ex) {
294 // String cannot have zero length
295 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
296 Assert.IsNull (ex.InnerException, "#B3");
297 Assert.IsNotNull (ex.Message, "#B4");
302 public void GetManifestResourceStream_Name_Null ()
304 Assembly corlib = typeof (int).Assembly;
307 corlib.GetManifestResourceStream ((string) null);
309 } catch (ArgumentNullException ex) {
310 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
311 Assert.IsNull (ex.InnerException, "#A3");
312 Assert.IsNotNull (ex.Message, "#A4");
315 corlib.GetManifestResourceStream (typeof (int), (string) null);
318 corlib.GetManifestResourceStream ((Type) null, (string) null);
320 } catch (ArgumentNullException ex) {
321 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
322 Assert.IsNull (ex.InnerException, "#B3");
323 Assert.IsNotNull (ex.Message, "#B4");
324 Assert.IsNotNull (ex.ParamName, "#B5");
325 Assert.AreEqual ("type", ex.ParamName, "#B6");
330 public void IsDefined_AttributeType_Null ()
332 Assembly corlib = typeof (int).Assembly;
335 corlib.IsDefined ((Type) null, false);
337 } catch (ArgumentNullException ex) {
338 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
339 Assert.IsNull (ex.InnerException, "#3");
340 Assert.IsNotNull (ex.Message, "#4");
341 Assert.IsNotNull (ex.ParamName, "#5");
342 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
347 public void LoadFrom_Empty_Assembly ()
349 string tempFile = Path.GetTempFileName ();
352 Assembly.LoadFrom (tempFile);
354 } catch (BadImageFormatException ex) {
355 Assert.IsNull (ex.InnerException, "#2");
357 File.Delete (tempFile);
362 public void LoadFrom_Invalid_Assembly ()
364 string tempFile = Path.GetTempFileName ();
365 using (StreamWriter sw = File.CreateText (tempFile)) {
366 sw.WriteLine ("foo");
371 Assembly.LoadFrom (tempFile);
373 } catch (BadImageFormatException ex) {
374 Assert.IsNull (ex.InnerException, "#2");
376 File.Delete (tempFile);
381 public void LoadFrom_NonExisting_Assembly ()
383 string tempFile = Path.GetTempFileName ();
384 File.Delete (tempFile);
387 Assembly.LoadFrom (tempFile);
389 } catch (FileNotFoundException ex) {
390 Assert.IsNull (ex.InnerException, "#2");
392 File.Delete (tempFile);
397 public void LoadWithPartialName ()
399 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" };
401 foreach (string s in names)
402 if (Assembly.LoadWithPartialName (s) != null)
404 Assert.Fail ("Was not able to load any corlib test");
408 public void GetObjectData_Info_Null ()
410 Assembly corlib = typeof (int).Assembly;
412 corlib.GetObjectData (null, new StreamingContext (
413 StreamingContextStates.All));
415 } catch (ArgumentNullException ex) {
416 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
417 Assert.IsNull (ex.InnerException, "#3");
418 Assert.IsNotNull (ex.Message, "#4");
419 Assert.IsNotNull (ex.ParamName, "#5");
420 Assert.AreEqual ("info", ex.ParamName, "#6");
425 public void GetReferencedAssemblies ()
427 Assembly corlib_test = Assembly.GetExecutingAssembly ();
428 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
429 foreach (AssemblyName an in names) {
430 Assert.IsNull (an.CodeBase, "CodeBase");
431 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
432 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
433 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
434 Assert.IsNotNull (an.FullName, "FullName");
435 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
436 Assert.IsNull (an.KeyPair, "KeyPair");
437 Assert.IsNotNull (an.Name, "Name");
438 Assert.IsNotNull (an.Version, "Version");
439 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
440 an.VersionCompatibility, "VersionCompatibility");
444 #if !MONOTOUCH // Reflection.Emit is not supported.
446 public void Location_Empty() {
447 string assemblyFileName = Path.Combine (
448 Path.GetTempPath (), "AssemblyLocation.dll");
451 AssemblyName assemblyName = new AssemblyName ();
452 assemblyName.Name = "AssemblyLocation";
454 AssemblyBuilder ab = AppDomain.CurrentDomain
455 .DefineDynamicAssembly (assemblyName,
456 AssemblyBuilderAccess.Save,
458 AppDomain.CurrentDomain.Evidence);
459 ab.Save (Path.GetFileName (assemblyFileName));
461 using (FileStream fs = File.OpenRead (assemblyFileName)) {
462 byte[] buffer = new byte[fs.Length];
463 fs.Read (buffer, 0, buffer.Length);
464 Assembly assembly = Assembly.Load (buffer);
465 Assert.AreEqual (string.Empty, assembly.Location);
469 File.Delete (assemblyFileName);
474 public void SateliteAssemblyForInMemoryAssembly ()
476 string assemblyFileName = Path.Combine (
477 Path.GetTempPath (), "AssemblyLocation1.dll");
480 AssemblyName assemblyName = new AssemblyName ();
481 assemblyName.Name = "AssemblyLocation1";
483 AssemblyBuilder ab = AppDomain.CurrentDomain
484 .DefineDynamicAssembly (assemblyName,
485 AssemblyBuilderAccess.Save,
486 Path.GetTempPath ());
488 ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
489 TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
491 MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
492 ILGenerator gen = methodBuilder.GetILGenerator ();
495 // var resourceManager = new ResourceManager (typeof (Program));
496 // resourceManager.GetString ("test");
498 gen.Emit (OpCodes.Ldtoken, typeBuilder);
499 gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
500 gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
501 gen.Emit (OpCodes.Ldstr, "test");
502 gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
503 gen.Emit (OpCodes.Pop);
504 gen.Emit (OpCodes.Ret);
506 typeBuilder.CreateType ();
508 ab.Save (Path.GetFileName (assemblyFileName));
510 using (FileStream fs = File.OpenRead (assemblyFileName)) {
511 byte[] buffer = new byte[fs.Length];
512 fs.Read (buffer, 0, buffer.Length);
513 Assembly assembly = Assembly.Load (buffer);
515 var mm = assembly.GetType ("Program").GetMethod ("TestCall");
517 mm.Invoke (null, null);
519 } catch (TargetInvocationException e) {
520 Assert.IsTrue (e.InnerException is MissingManifestResourceException);
526 File.Delete (assemblyFileName);
531 [Category ("NotWorking")]
532 public void bug78464 ()
534 string assemblyFileName = Path.Combine (
535 Path.GetTempPath (), "bug78464.dll");
538 // execute test in separate appdomain to allow assembly to be unloaded
539 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
540 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
542 crossDomainTester.bug78464 (assemblyFileName);
544 AppDomain.Unload (testDomain);
547 File.Delete (assemblyFileName);
552 public void bug78465 ()
554 string assemblyFileName = Path.Combine (
555 Path.GetTempPath (), "bug78465.dll");
558 AssemblyName assemblyName = new AssemblyName ();
559 assemblyName.Name = "bug78465";
561 AssemblyBuilder ab = AppDomain.CurrentDomain
562 .DefineDynamicAssembly (assemblyName,
563 AssemblyBuilderAccess.Save,
564 Path.GetDirectoryName (assemblyFileName),
565 AppDomain.CurrentDomain.Evidence);
566 ab.Save (Path.GetFileName (assemblyFileName));
568 using (FileStream fs = File.OpenRead (assemblyFileName)) {
569 byte[] buffer = new byte[fs.Length];
570 fs.Read (buffer, 0, buffer.Length);
571 Assembly assembly = Assembly.Load (buffer);
572 Assert.AreEqual (string.Empty, assembly.Location, "#1");
576 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
577 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
579 crossDomainTester.bug78465 (assemblyFileName);
581 AppDomain.Unload (testDomain);
584 File.Delete (assemblyFileName);
589 public void bug78468 ()
591 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
593 string resourceFileName = Path.Combine (Path.GetTempPath (),
596 using (StreamWriter sw = File.CreateText (resourceFileName)) {
597 sw.WriteLine ("FOO");
602 AssemblyName assemblyName = new AssemblyName ();
603 assemblyName.Name = "bug78468a";
605 AssemblyBuilder ab = AppDomain.CurrentDomain
606 .DefineDynamicAssembly (assemblyName,
607 AssemblyBuilderAccess.Save,
609 AppDomain.CurrentDomain.Evidence);
610 ab.AddResourceFile ("read", "readme.txt");
611 ab.Save (Path.GetFileName (assemblyFileNameA));
615 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
616 byte[] buffer = new byte[fs.Length];
617 fs.Read (buffer, 0, buffer.Length);
618 assembly = Assembly.Load (buffer);
622 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
623 string[] resNames = assembly.GetManifestResourceNames ();
624 Assert.IsNotNull (resNames, "#A2");
625 Assert.AreEqual (1, resNames.Length, "#A3");
626 Assert.AreEqual ("read", resNames[0], "#A4");
627 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
628 Assert.IsNotNull (resInfo, "#A5");
629 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
630 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
631 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
633 assembly.GetManifestResourceStream ("read");
635 } catch (FileNotFoundException) {
638 assembly.GetFile ("readme.txt");
639 Assert.Fail ("#A10");
640 } catch (FileNotFoundException) {
643 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
646 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
647 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
649 crossDomainTester.bug78468 (assemblyFileNameB);
651 AppDomain.Unload (testDomain);
652 File.Delete (assemblyFileNameB);
655 File.Delete (assemblyFileNameA);
656 File.Delete (resourceFileName);
661 [Category ("NotWorking")]
662 public void ReflectionOnlyLoad ()
664 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
666 Assert.IsNotNull (assembly);
667 Assert.IsTrue (assembly.ReflectionOnly);
671 public void ReflectionOnlyLoadFrom ()
673 string loc = typeof (AssemblyTest).Assembly.Location;
674 string filename = Path.GetFileName (loc);
675 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
677 Assert.IsNotNull (assembly);
678 Assert.IsTrue (assembly.ReflectionOnly);
682 [ExpectedException (typeof (ArgumentException))]
683 public void CreateInstanceOnRefOnly ()
685 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
686 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
690 [Category ("NotWorking")] // patch for bug #79720 must be committed first
691 public void Load_Culture ()
693 string tempDir = Path.Combine (Path.GetTempPath (),
694 "MonoTests.System.Reflection.AssemblyTest");
695 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
696 if (!Directory.Exists (cultureTempDir))
697 Directory.CreateDirectory (cultureTempDir);
698 cultureTempDir = Path.Combine (tempDir, "en-US");
699 if (!Directory.Exists (cultureTempDir))
700 Directory.CreateDirectory (cultureTempDir);
703 AppDomain ad = CreateTestDomain (tempDir, true);
705 CrossDomainTester cdt = CreateCrossDomainTester (ad);
709 AssemblyName aname = new AssemblyName ();
710 aname.Name = "culturea";
711 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
713 aname = new AssemblyName ();
714 aname.Name = "culturea";
715 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
717 aname = new AssemblyName ();
718 aname.Name = "culturea";
719 aname.CultureInfo = new CultureInfo ("nl-BE");
720 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
722 aname = new AssemblyName ();
723 aname.Name = "culturea";
724 aname.CultureInfo = CultureInfo.InvariantCulture;
725 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
729 aname = new AssemblyName ();
730 aname.Name = "cultureb";
731 aname.CultureInfo = new CultureInfo ("nl-BE");
732 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
734 aname = new AssemblyName ();
735 aname.Name = "cultureb";
736 aname.CultureInfo = new CultureInfo ("nl-BE");
737 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
739 aname = new AssemblyName ();
740 aname.Name = "cultureb";
741 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
743 aname = new AssemblyName ();
744 aname.Name = "cultureb";
745 aname.CultureInfo = new CultureInfo ("en-US");
746 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
750 aname = new AssemblyName ();
751 aname.Name = "culturec";
752 aname.CultureInfo = new CultureInfo ("nl-BE");
753 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
755 aname = new AssemblyName ();
756 aname.Name = "culturec";
757 aname.CultureInfo = new CultureInfo ("nl-BE");
758 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
760 aname = new AssemblyName ();
761 aname.Name = "culturec";
762 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
764 aname = new AssemblyName ();
765 aname.Name = "culturec";
766 aname.CultureInfo = CultureInfo.InvariantCulture;
767 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
771 aname = new AssemblyName ();
772 aname.Name = "cultured";
773 aname.CultureInfo = new CultureInfo ("nl-BE");
774 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
776 aname = new AssemblyName ();
777 aname.Name = "cultured";
778 aname.CultureInfo = new CultureInfo ("nl-BE");
779 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
781 aname = new AssemblyName ();
782 aname.Name = "cultured";
783 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
785 aname = new AssemblyName ();
786 aname.Name = "cultured";
787 aname.CultureInfo = CultureInfo.InvariantCulture;
788 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
790 AppDomain.Unload (ad);
791 if (Directory.Exists (tempDir))
792 Directory.Delete (tempDir, true);
797 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
798 public void Load_Culture_Mismatch ()
800 string tempDir = Path.Combine (Path.GetTempPath (),
801 "MonoTests.System.Reflection.AssemblyTest");
802 string cultureTempDir = Path.Combine (tempDir, "en-US");
803 if (!Directory.Exists (cultureTempDir))
804 Directory.CreateDirectory (cultureTempDir);
806 AppDomain ad = CreateTestDomain (tempDir, true);
808 CrossDomainTester cdt = CreateCrossDomainTester (ad);
812 AssemblyName aname = new AssemblyName ();
813 aname.Name = "bug79712a";
814 aname.CultureInfo = new CultureInfo ("nl-BE");
815 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
817 aname = new AssemblyName ();
818 aname.Name = "bug79712a";
819 aname.CultureInfo = CultureInfo.InvariantCulture;
820 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
824 aname = new AssemblyName ();
825 aname.Name = "bug79712b";
826 aname.CultureInfo = new CultureInfo ("nl-BE");
827 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
829 aname = new AssemblyName ();
830 aname.Name = "bug79712b";
831 aname.CultureInfo = new CultureInfo ("en-US");
832 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
834 AppDomain.Unload (ad);
835 if (Directory.Exists (tempDir))
836 Directory.Delete (tempDir, true);
842 public void Load_PartialVersion ()
844 string tempDir = Path.Combine (Path.GetTempPath (),
845 "MonoTests.System.Reflection.AssemblyTest");
846 if (!Directory.Exists (tempDir))
847 Directory.CreateDirectory (tempDir);
849 AppDomain ad = CreateTestDomain (tempDir, true);
851 CrossDomainTester cdt = CreateCrossDomainTester (ad);
853 AssemblyName aname = new AssemblyName ();
854 aname.Name = "bug79715";
855 aname.Version = new Version (1, 2, 3, 4);
856 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
858 aname = new AssemblyName ();
859 aname.Name = "bug79715";
860 aname.Version = new Version (1, 2);
861 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
862 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
864 aname = new AssemblyName ();
865 aname.Name = "bug79715";
866 aname.Version = new Version (1, 2, 3);
867 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
868 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
870 aname = new AssemblyName ();
871 aname.Name = "bug79715";
872 aname.Version = new Version (1, 2, 3, 4);
873 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
874 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
876 AppDomain.Unload (ad);
877 if (Directory.Exists (tempDir))
878 Directory.Delete (tempDir, true);
882 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
884 AppDomainSetup setup = new AppDomainSetup ();
885 setup.ApplicationBase = baseDirectory;
886 setup.ApplicationName = "testdomain";
888 AppDomain ad = AppDomain.CreateDomain ("testdomain",
889 AppDomain.CurrentDomain.Evidence, setup);
891 if (assemblyResolver) {
892 Assembly ea = Assembly.GetExecutingAssembly ();
893 ad.CreateInstanceFrom (ea.CodeBase,
894 typeof (AssemblyResolveHandler).FullName,
896 BindingFlags.Public | BindingFlags.Instance,
898 new object [] { ea.Location, ea.FullName },
899 CultureInfo.InvariantCulture,
907 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
909 Type testerType = typeof (CrossDomainTester);
910 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
911 testerType.Assembly.FullName, testerType.FullName, false,
912 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
913 CultureInfo.InvariantCulture, new object[0], null);
916 private class CrossDomainTester : MarshalByRefObject
918 public void GenerateAssembly (AssemblyName aname, string path)
920 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
921 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
922 ab.Save (Path.GetFileName (path));
925 public void Load (AssemblyName assemblyRef)
927 Assembly.Load (assemblyRef);
930 public void LoadFrom (string assemblyFile)
932 Assembly.LoadFrom (assemblyFile);
935 public bool AssertLoad (AssemblyName assemblyRef)
938 Assembly.Load (assemblyRef);
945 public bool AssertLoad (string assemblyString)
948 Assembly.Load (assemblyString);
955 public bool AssertFileLoadException (AssemblyName assemblyRef)
958 Assembly.Load (assemblyRef);
960 } catch (FileLoadException) {
965 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
968 Assembly.Load (assemblyRef);
970 } catch (FileNotFoundException) {
975 public void bug78464 (string assemblyFileName)
977 AssemblyName assemblyName = new AssemblyName ();
978 assemblyName.Name = "bug78464";
980 AssemblyBuilder ab = AppDomain.CurrentDomain
981 .DefineDynamicAssembly (assemblyName,
982 AssemblyBuilderAccess.Save,
983 Path.GetDirectoryName (assemblyFileName),
984 AppDomain.CurrentDomain.Evidence);
985 ab.Save (Path.GetFileName (assemblyFileName));
989 using (FileStream fs = File.OpenRead (assemblyFileName)) {
990 byte[] buffer = new byte[fs.Length];
991 fs.Read (buffer, 0, buffer.Length);
992 assembly = Assembly.Load (buffer);
996 Assert.AreEqual (string.Empty, assembly.Location, "#1");
998 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
999 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1000 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
1001 // note: we cannot check if directory names match, as MS.NET seems to
1002 // convert directory part of assembly location to lowercase
1003 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
1006 public void bug78465 (string assemblyFileName)
1008 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1009 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1010 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1011 // note: we cannot check if directory names match, as MS.NET seems to
1012 // convert directory part of assembly location to lowercase
1013 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1016 public void bug78468 (string assemblyFileName)
1018 AssemblyName assemblyName = new AssemblyName ();
1019 assemblyName.Name = "bug78468b";
1021 AssemblyBuilder ab = AppDomain.CurrentDomain
1022 .DefineDynamicAssembly (assemblyName,
1023 AssemblyBuilderAccess.Save,
1024 Path.GetDirectoryName (assemblyFileName),
1025 AppDomain.CurrentDomain.Evidence);
1026 ab.AddResourceFile ("read", "readme.txt");
1027 ab.Save (Path.GetFileName (assemblyFileName));
1029 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1030 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1031 string[] resNames = assembly.GetManifestResourceNames ();
1032 Assert.IsNotNull (resNames, "#B2");
1033 Assert.AreEqual (1, resNames.Length, "#B3");
1034 Assert.AreEqual ("read", resNames[0], "#B4");
1035 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1036 Assert.IsNotNull (resInfo, "#B5");
1037 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1038 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1039 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1040 Stream s = assembly.GetManifestResourceStream ("read");
1041 Assert.IsNotNull (s, "#B9");
1043 s = assembly.GetFile ("readme.txt");
1044 Assert.IsNotNull (s, "#B10");
1050 public void bug79872 ()
1052 Random rnd = new Random ();
1057 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1058 if (Directory.Exists (outdir)) {
1060 Directory.Delete (outdir, true);
1067 Directory.CreateDirectory (outdir);
1069 AssemblyName an = new AssemblyName ();
1070 an.Name = "bug79872";
1071 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1072 string dllname = "bug79872.dll";
1073 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1074 string netmodule = "bug79872.netmodule";
1075 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1076 TypeBuilder a1 = mb2.DefineType ("A");
1082 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1086 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1090 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1091 } catch (BadImageFormatException) {
1092 ok = true; // mono and .net 2.0 throw this
1093 } catch (FileLoadException) {
1094 ok = true; // .net 1.1 throws this
1098 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1100 Directory.Delete (outdir, true);
1105 public void ManifestModule ()
1107 Assembly assembly = typeof (int).Assembly;
1108 Module module = assembly.ManifestModule;
1109 Assert.IsNotNull (module, "#1");
1112 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1114 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1118 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1120 Assert.IsFalse (module.IsResource (), "#4");
1121 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1122 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1123 Assert.AreSame (module, assembly.ManifestModule, "#7");
1128 private class AssemblyResolveHandler
1130 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1132 _assemblyFile = assemblyFile;
1133 _assemblyName = assemblyName;
1135 AppDomain.CurrentDomain.AssemblyResolve +=
1136 new ResolveEventHandler (ResolveAssembly);
1139 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1141 if (args.Name == _assemblyName)
1142 return Assembly.LoadFrom (_assemblyFile);
1147 private readonly string _assemblyFile;
1148 private readonly string _assemblyName;
1151 protected internal class Bug328812_NestedFamORAssem { };
1154 public void bug328812 ()
1156 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1157 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1158 // Just a sanity check, in case the above passed for some other reason
1159 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1163 public void GetCustomAttributes_AttributeType_Null ()
1165 Assembly a = typeof (int).Assembly;
1167 a.GetCustomAttributes (null, false);
1169 } catch (ArgumentNullException ex) {
1170 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1171 Assert.IsNull (ex.InnerException, "#3");
1172 Assert.IsNotNull (ex.Message, "#4");
1173 Assert.IsNotNull (ex.ParamName, "#5");
1174 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1179 public void GetTypeWithEmptyStringShouldThrow ()
1182 typeof (string).Assembly.GetType ("");
1184 } catch (ArgumentException) {}
1189 public void DefinedTypes_Equality ()
1191 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1192 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1194 Assert.AreSame (x1, x2, "#1");
1199 public class TestDefinedTypes