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 namespace MonoTests.System.Reflection
48 public class AssemblyTest
50 static string TempFolder = Path.Combine (Path.GetTempPath (),
51 "MonoTests.System.Reflection.AssemblyTest");
56 while (Directory.Exists (TempFolder))
57 TempFolder = Path.Combine (TempFolder, "2");
58 Directory.CreateDirectory (TempFolder);
62 public void TearDown ()
65 // This throws an exception under MS.NET, since the directory contains loaded
67 Directory.Delete (TempFolder, true);
73 public void CreateInstance()
75 Type type = typeof (AssemblyTest);
76 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
77 Assert.IsNotNull (obj, "#01");
78 Assert.AreEqual (GetType (), obj.GetType (), "#02");
82 public void CreateInvalidInstance()
84 Type type = typeof (AssemblyTest);
85 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
86 Assert.IsNull (obj, "#03");
91 [Category ("NotWorking")]
92 [ExpectedException (typeof (ArgumentException))]
94 [ExpectedException (typeof (TypeLoadException))]
96 public void GetType_TypeName_Invalid ()
98 typeof (int).Assembly.GetType ("&blabla", true, true);
101 [Test] // bug #334203
102 public void GetType_TypeName_AssemblyName ()
104 Assembly a = typeof (int).Assembly;
105 string typeName = typeof (string).AssemblyQualifiedName;
108 a.GetType (typeName, true, false);
110 } catch (ArgumentException ex) {
111 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
112 Assert.IsNull (ex.InnerException, "#A3");
113 Assert.IsNotNull (ex.Message, "#A4");
114 Assert.IsNull (ex.ParamName, "#A5");
118 a.GetType (typeName, true, false);
120 } catch (TypeLoadException ex) {
121 Assert.AreEqual (typeof (TypeLoadException), ex.GetType (), "#A2");
122 Assert.IsNull (ex.InnerException, "#A3");
123 Assert.IsNotNull (ex.Message, "#A4");
124 Assert.IsTrue (ex.Message.IndexOf (typeName) != -1, "#A5");
128 Type type = a.GetType (typeName, false);
129 Assert.IsNull (type, "#B1");
130 type = a.GetType (typeName, false, true);
131 Assert.IsNull (type, "#B2");
135 public void GetEntryAssembly ()
137 // note: only available in default appdomain
138 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
139 // Not sure we should emulate this behavior.
140 string fname = AppDomain.CurrentDomain.FriendlyName;
141 if (fname.EndsWith (".dll")) { // nunit-console
142 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
143 #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
144 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
147 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
148 #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
149 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
154 #if !TARGET_JVM // Reflection.Emit is not supported.
156 public void GetModules_MissingFile ()
158 AssemblyName newName = new AssemblyName ();
159 newName.Name = "AssemblyTest";
161 AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
163 ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
165 ab.Save ("test_assembly.dll");
167 File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
169 Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
173 } catch (FileNotFoundException ex) {
174 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
179 #if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
181 [Category ("NotWorking")]
184 public void Corlib ()
186 Assembly corlib = typeof (int).Assembly;
187 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
188 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
189 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
190 Assert.IsNotNull (corlib.Evidence, "Evidence");
191 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
193 // corlib doesn't reference anything
194 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
196 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
197 // not really "true" but it's even more trusted so...
198 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
199 Assert.AreEqual (0, corlib.HostContext, "HostContext");
200 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
201 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
202 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
204 Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
205 Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
206 Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
211 public void Corlib_test ()
213 Assembly corlib_test = Assembly.GetExecutingAssembly ();
214 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
215 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
216 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
218 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
219 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
221 Assert.AreEqual ("v4.0.30128", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
223 Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
226 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
227 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
232 public void GetAssembly ()
234 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
235 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
239 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
240 public void GetFile_Null ()
243 Assembly.GetExecutingAssembly ().GetFile (null);
245 } catch (ArgumentNullException ex) {
246 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
247 Assert.IsNull (ex.InnerException, "#3");
248 Assert.IsNotNull (ex.Message, "#4");
249 Assert.IsNull (ex.ParamName, "#5");
254 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
255 public void GetFile_Empty ()
258 Assembly.GetExecutingAssembly ().GetFile (
261 } catch (ArgumentException ex) {
262 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
263 Assert.IsNull (ex.InnerException, "#3");
264 Assert.IsNotNull (ex.Message, "#4");
265 Assert.IsNull (ex.ParamName, "#5");
270 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
271 public void GetFiles_False ()
273 Assembly corlib = typeof (int).Assembly;
274 FileStream[] fss = corlib.GetFiles ();
275 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
277 Assembly corlib_test = Assembly.GetExecutingAssembly ();
278 fss = corlib_test.GetFiles ();
279 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
283 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
284 public void GetFiles_True ()
286 Assembly corlib = typeof (int).Assembly;
287 FileStream[] fss = corlib.GetFiles ();
288 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
290 Assembly corlib_test = Assembly.GetExecutingAssembly ();
291 fss = corlib_test.GetFiles ();
292 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
296 public void GetManifestResourceStream_Name_Empty ()
298 Assembly corlib = typeof (int).Assembly;
301 corlib.GetManifestResourceStream (string.Empty);
303 } catch (ArgumentException ex) {
304 // String cannot have zero length
305 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
306 Assert.IsNull (ex.InnerException, "#A3");
307 Assert.IsNotNull (ex.Message, "#A4");
310 corlib.GetManifestResourceStream (typeof (int), string.Empty);
313 corlib.GetManifestResourceStream ((Type) null, string.Empty);
315 } catch (ArgumentException ex) {
316 // String cannot have zero length
317 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
318 Assert.IsNull (ex.InnerException, "#B3");
319 Assert.IsNotNull (ex.Message, "#B4");
324 public void GetManifestResourceStream_Name_Null ()
326 Assembly corlib = typeof (int).Assembly;
329 corlib.GetManifestResourceStream ((string) null);
331 } catch (ArgumentNullException ex) {
332 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
333 Assert.IsNull (ex.InnerException, "#A3");
334 Assert.IsNotNull (ex.Message, "#A4");
337 corlib.GetManifestResourceStream (typeof (int), (string) null);
340 corlib.GetManifestResourceStream ((Type) null, (string) null);
342 } catch (ArgumentNullException ex) {
343 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
344 Assert.IsNull (ex.InnerException, "#B3");
345 Assert.IsNotNull (ex.Message, "#B4");
346 Assert.IsNotNull (ex.ParamName, "#B5");
347 Assert.AreEqual ("type", ex.ParamName, "#B6");
352 public void IsDefined_AttributeType_Null ()
354 Assembly corlib = typeof (int).Assembly;
357 corlib.IsDefined ((Type) null, false);
359 } catch (ArgumentNullException ex) {
360 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
361 Assert.IsNull (ex.InnerException, "#3");
362 Assert.IsNotNull (ex.Message, "#4");
363 Assert.IsNotNull (ex.ParamName, "#5");
364 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
370 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
372 public void LoadFrom_Empty_Assembly ()
374 string tempFile = Path.GetTempFileName ();
377 Assembly.LoadFrom (tempFile);
379 } catch (BadImageFormatException ex) {
380 Assert.IsNull (ex.InnerException, "#2");
382 File.Delete (tempFile);
387 public void LoadFrom_Invalid_Assembly ()
389 string tempFile = Path.GetTempFileName ();
390 using (StreamWriter sw = File.CreateText (tempFile)) {
391 sw.WriteLine ("foo");
396 Assembly.LoadFrom (tempFile);
398 } catch (BadImageFormatException ex) {
399 Assert.IsNull (ex.InnerException, "#2");
401 File.Delete (tempFile);
406 public void LoadFrom_NonExisting_Assembly ()
408 string tempFile = Path.GetTempFileName ();
409 File.Delete (tempFile);
412 Assembly.LoadFrom (tempFile);
414 } catch (FileNotFoundException ex) {
415 Assert.IsNull (ex.InnerException, "#2");
417 File.Delete (tempFile);
422 public void LoadWithPartialName ()
424 string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_test_net_4_0", "corlib_plattest" };
426 foreach (string s in names)
427 if (Assembly.LoadWithPartialName (s) != null)
429 Assert.Fail ("Was not able to load any corlib test");
432 #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
434 public void GetObjectData_Info_Null ()
436 Assembly corlib = typeof (int).Assembly;
438 corlib.GetObjectData (null, new StreamingContext (
439 StreamingContextStates.All));
441 } catch (ArgumentNullException ex) {
442 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
443 Assert.IsNull (ex.InnerException, "#3");
444 Assert.IsNotNull (ex.Message, "#4");
445 Assert.IsNotNull (ex.ParamName, "#5");
446 Assert.AreEqual ("info", ex.ParamName, "#6");
452 public void GetReferencedAssemblies ()
454 Assembly corlib_test = Assembly.GetExecutingAssembly ();
455 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
456 foreach (AssemblyName an in names) {
457 Assert.IsNull (an.CodeBase, "CodeBase");
458 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
459 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
460 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
461 Assert.IsNotNull (an.FullName, "FullName");
462 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
463 Assert.IsNull (an.KeyPair, "KeyPair");
464 Assert.IsNotNull (an.Name, "Name");
465 Assert.IsNotNull (an.Version, "Version");
466 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
467 an.VersionCompatibility, "VersionCompatibility");
471 #if !TARGET_JVM // Reflection.Emit is not supported.
473 public void Location_Empty() {
474 string assemblyFileName = Path.Combine (
475 Path.GetTempPath (), "AssemblyLocation.dll");
478 AssemblyName assemblyName = new AssemblyName ();
479 assemblyName.Name = "AssemblyLocation";
481 AssemblyBuilder ab = AppDomain.CurrentDomain
482 .DefineDynamicAssembly (assemblyName,
483 AssemblyBuilderAccess.Save,
485 AppDomain.CurrentDomain.Evidence);
486 ab.Save (Path.GetFileName (assemblyFileName));
488 using (FileStream fs = File.OpenRead (assemblyFileName)) {
489 byte[] buffer = new byte[fs.Length];
490 fs.Read (buffer, 0, buffer.Length);
491 Assembly assembly = Assembly.Load (buffer);
492 Assert.AreEqual (string.Empty, assembly.Location);
496 File.Delete (assemblyFileName);
501 [Category ("NotWorking")]
502 public void bug78464 ()
504 string assemblyFileName = Path.Combine (
505 Path.GetTempPath (), "bug78464.dll");
508 // execute test in separate appdomain to allow assembly to be unloaded
509 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
510 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
512 crossDomainTester.bug78464 (assemblyFileName);
514 AppDomain.Unload (testDomain);
517 File.Delete (assemblyFileName);
522 public void bug78465 ()
524 string assemblyFileName = Path.Combine (
525 Path.GetTempPath (), "bug78465.dll");
528 AssemblyName assemblyName = new AssemblyName ();
529 assemblyName.Name = "bug78465";
531 AssemblyBuilder ab = AppDomain.CurrentDomain
532 .DefineDynamicAssembly (assemblyName,
533 AssemblyBuilderAccess.Save,
534 Path.GetDirectoryName (assemblyFileName),
535 AppDomain.CurrentDomain.Evidence);
536 ab.Save (Path.GetFileName (assemblyFileName));
538 using (FileStream fs = File.OpenRead (assemblyFileName)) {
539 byte[] buffer = new byte[fs.Length];
540 fs.Read (buffer, 0, buffer.Length);
541 Assembly assembly = Assembly.Load (buffer);
542 Assert.AreEqual (string.Empty, assembly.Location, "#1");
546 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
547 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
549 crossDomainTester.bug78465 (assemblyFileName);
551 AppDomain.Unload (testDomain);
554 File.Delete (assemblyFileName);
559 public void bug78468 ()
561 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
563 string resourceFileName = Path.Combine (Path.GetTempPath (),
566 using (StreamWriter sw = File.CreateText (resourceFileName)) {
567 sw.WriteLine ("FOO");
572 AssemblyName assemblyName = new AssemblyName ();
573 assemblyName.Name = "bug78468a";
575 AssemblyBuilder ab = AppDomain.CurrentDomain
576 .DefineDynamicAssembly (assemblyName,
577 AssemblyBuilderAccess.Save,
579 AppDomain.CurrentDomain.Evidence);
580 ab.AddResourceFile ("read", "readme.txt");
581 ab.Save (Path.GetFileName (assemblyFileNameA));
585 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
586 byte[] buffer = new byte[fs.Length];
587 fs.Read (buffer, 0, buffer.Length);
588 assembly = Assembly.Load (buffer);
592 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
593 string[] resNames = assembly.GetManifestResourceNames ();
594 Assert.IsNotNull (resNames, "#A2");
595 Assert.AreEqual (1, resNames.Length, "#A3");
596 Assert.AreEqual ("read", resNames[0], "#A4");
597 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
598 Assert.IsNotNull (resInfo, "#A5");
599 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
600 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
601 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
604 assembly.GetManifestResourceStream ("read");
606 } catch (FileNotFoundException) {
609 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
612 assembly.GetFile ("readme.txt");
613 Assert.Fail ("#A10");
614 } catch (FileNotFoundException) {
617 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
620 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
621 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
623 crossDomainTester.bug78468 (assemblyFileNameB);
625 AppDomain.Unload (testDomain);
626 File.Delete (assemblyFileNameB);
629 File.Delete (assemblyFileNameA);
630 File.Delete (resourceFileName);
636 [Category ("NotWorking")]
637 public void ReflectionOnlyLoad ()
639 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
641 Assert.IsNotNull (assembly);
642 Assert.IsTrue (assembly.ReflectionOnly);
646 public void ReflectionOnlyLoadFrom ()
648 string loc = typeof (AssemblyTest).Assembly.Location;
649 string filename = Path.GetFileName (loc);
650 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
652 Assert.IsNotNull (assembly);
653 Assert.IsTrue (assembly.ReflectionOnly);
657 [ExpectedException (typeof (ArgumentException))]
658 public void CreateInstanceOnRefOnly ()
660 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
661 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
666 [Category ("NotWorking")] // patch for bug #79720 must be committed first
667 public void Load_Culture ()
669 string tempDir = Path.Combine (Path.GetTempPath (),
670 "MonoTests.System.Reflection.AssemblyTest");
671 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
672 if (!Directory.Exists (cultureTempDir))
673 Directory.CreateDirectory (cultureTempDir);
674 cultureTempDir = Path.Combine (tempDir, "en-US");
675 if (!Directory.Exists (cultureTempDir))
676 Directory.CreateDirectory (cultureTempDir);
679 AppDomain ad = CreateTestDomain (tempDir, true);
681 CrossDomainTester cdt = CreateCrossDomainTester (ad);
685 AssemblyName aname = new AssemblyName ();
686 aname.Name = "culturea";
687 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
689 aname = new AssemblyName ();
690 aname.Name = "culturea";
691 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
693 aname = new AssemblyName ();
694 aname.Name = "culturea";
695 aname.CultureInfo = new CultureInfo ("nl-BE");
696 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
698 aname = new AssemblyName ();
699 aname.Name = "culturea";
700 aname.CultureInfo = CultureInfo.InvariantCulture;
701 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
705 aname = new AssemblyName ();
706 aname.Name = "cultureb";
707 aname.CultureInfo = new CultureInfo ("nl-BE");
708 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
710 aname = new AssemblyName ();
711 aname.Name = "cultureb";
712 aname.CultureInfo = new CultureInfo ("nl-BE");
713 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
715 aname = new AssemblyName ();
716 aname.Name = "cultureb";
717 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
719 aname = new AssemblyName ();
720 aname.Name = "cultureb";
721 aname.CultureInfo = new CultureInfo ("en-US");
722 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
726 aname = new AssemblyName ();
727 aname.Name = "culturec";
728 aname.CultureInfo = new CultureInfo ("nl-BE");
729 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
731 aname = new AssemblyName ();
732 aname.Name = "culturec";
733 aname.CultureInfo = new CultureInfo ("nl-BE");
734 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
736 aname = new AssemblyName ();
737 aname.Name = "culturec";
738 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
740 aname = new AssemblyName ();
741 aname.Name = "culturec";
742 aname.CultureInfo = CultureInfo.InvariantCulture;
743 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
747 aname = new AssemblyName ();
748 aname.Name = "cultured";
749 aname.CultureInfo = new CultureInfo ("nl-BE");
750 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
752 aname = new AssemblyName ();
753 aname.Name = "cultured";
754 aname.CultureInfo = new CultureInfo ("nl-BE");
755 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
757 aname = new AssemblyName ();
758 aname.Name = "cultured";
759 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
761 aname = new AssemblyName ();
762 aname.Name = "cultured";
763 aname.CultureInfo = CultureInfo.InvariantCulture;
764 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
766 AppDomain.Unload (ad);
767 if (Directory.Exists (tempDir))
768 Directory.Delete (tempDir, true);
774 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
776 [Category ("NotWorking")]
778 public void Load_Culture_Mismatch ()
780 string tempDir = Path.Combine (Path.GetTempPath (),
781 "MonoTests.System.Reflection.AssemblyTest");
782 string cultureTempDir = Path.Combine (tempDir, "en-US");
783 if (!Directory.Exists (cultureTempDir))
784 Directory.CreateDirectory (cultureTempDir);
786 AppDomain ad = CreateTestDomain (tempDir, true);
788 CrossDomainTester cdt = CreateCrossDomainTester (ad);
792 AssemblyName aname = new AssemblyName ();
793 aname.Name = "bug79712a";
794 aname.CultureInfo = new CultureInfo ("nl-BE");
795 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
797 aname = new AssemblyName ();
798 aname.Name = "bug79712a";
799 aname.CultureInfo = CultureInfo.InvariantCulture;
801 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
803 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
808 aname = new AssemblyName ();
809 aname.Name = "bug79712b";
810 aname.CultureInfo = new CultureInfo ("nl-BE");
811 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
813 aname = new AssemblyName ();
814 aname.Name = "bug79712b";
815 aname.CultureInfo = new CultureInfo ("en-US");
817 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
819 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
822 AppDomain.Unload (ad);
823 if (Directory.Exists (tempDir))
824 Directory.Delete (tempDir, true);
830 public void Load_PartialVersion ()
832 string tempDir = Path.Combine (Path.GetTempPath (),
833 "MonoTests.System.Reflection.AssemblyTest");
834 if (!Directory.Exists (tempDir))
835 Directory.CreateDirectory (tempDir);
837 AppDomain ad = CreateTestDomain (tempDir, true);
839 CrossDomainTester cdt = CreateCrossDomainTester (ad);
841 AssemblyName aname = new AssemblyName ();
842 aname.Name = "bug79715";
843 aname.Version = new Version (1, 2, 3, 4);
844 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
846 aname = new AssemblyName ();
847 aname.Name = "bug79715";
848 aname.Version = new Version (1, 2);
849 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
850 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
852 aname = new AssemblyName ();
853 aname.Name = "bug79715";
854 aname.Version = new Version (1, 2, 3);
855 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
856 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
858 aname = new AssemblyName ();
859 aname.Name = "bug79715";
860 aname.Version = new Version (1, 2, 3, 4);
861 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
862 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
864 AppDomain.Unload (ad);
865 if (Directory.Exists (tempDir))
866 Directory.Delete (tempDir, true);
870 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
872 AppDomainSetup setup = new AppDomainSetup ();
873 setup.ApplicationBase = baseDirectory;
874 setup.ApplicationName = "testdomain";
876 AppDomain ad = AppDomain.CreateDomain ("testdomain",
877 AppDomain.CurrentDomain.Evidence, setup);
879 if (assemblyResolver) {
880 Assembly ea = Assembly.GetExecutingAssembly ();
881 ad.CreateInstanceFrom (ea.CodeBase,
882 typeof (AssemblyResolveHandler).FullName,
884 BindingFlags.Public | BindingFlags.Instance,
886 new object [] { ea.Location, ea.FullName },
887 CultureInfo.InvariantCulture,
895 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
897 Type testerType = typeof (CrossDomainTester);
898 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
899 testerType.Assembly.FullName, testerType.FullName, false,
900 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
901 CultureInfo.InvariantCulture, new object[0], null);
904 private class CrossDomainTester : MarshalByRefObject
906 public void GenerateAssembly (AssemblyName aname, string path)
908 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
909 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
910 ab.Save (Path.GetFileName (path));
913 public void Load (AssemblyName assemblyRef)
915 Assembly.Load (assemblyRef);
918 public void LoadFrom (string assemblyFile)
920 Assembly.LoadFrom (assemblyFile);
923 public bool AssertLoad (AssemblyName assemblyRef)
926 Assembly.Load (assemblyRef);
933 public bool AssertLoad (string assemblyString)
936 Assembly.Load (assemblyString);
943 public bool AssertFileLoadException (AssemblyName assemblyRef)
946 Assembly.Load (assemblyRef);
948 } catch (FileLoadException) {
953 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
956 Assembly.Load (assemblyRef);
958 } catch (FileNotFoundException) {
963 public void bug78464 (string assemblyFileName)
965 AssemblyName assemblyName = new AssemblyName ();
966 assemblyName.Name = "bug78464";
968 AssemblyBuilder ab = AppDomain.CurrentDomain
969 .DefineDynamicAssembly (assemblyName,
970 AssemblyBuilderAccess.Save,
971 Path.GetDirectoryName (assemblyFileName),
972 AppDomain.CurrentDomain.Evidence);
973 ab.Save (Path.GetFileName (assemblyFileName));
977 using (FileStream fs = File.OpenRead (assemblyFileName)) {
978 byte[] buffer = new byte[fs.Length];
979 fs.Read (buffer, 0, buffer.Length);
980 assembly = Assembly.Load (buffer);
984 Assert.AreEqual (string.Empty, assembly.Location, "#1");
986 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
987 Assert.IsFalse (assembly.Location == string.Empty, "#2");
988 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
989 // note: we cannot check if directory names match, as MS.NET seems to
990 // convert directory part of assembly location to lowercase
991 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
994 public void bug78465 (string assemblyFileName)
996 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
997 Assert.IsFalse (assembly.Location == string.Empty, "#2");
998 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
999 // note: we cannot check if directory names match, as MS.NET seems to
1000 // convert directory part of assembly location to lowercase
1001 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1004 public void bug78468 (string assemblyFileName)
1006 AssemblyName assemblyName = new AssemblyName ();
1007 assemblyName.Name = "bug78468b";
1009 AssemblyBuilder ab = AppDomain.CurrentDomain
1010 .DefineDynamicAssembly (assemblyName,
1011 AssemblyBuilderAccess.Save,
1012 Path.GetDirectoryName (assemblyFileName),
1013 AppDomain.CurrentDomain.Evidence);
1014 ab.AddResourceFile ("read", "readme.txt");
1015 ab.Save (Path.GetFileName (assemblyFileName));
1017 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1018 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1019 string[] resNames = assembly.GetManifestResourceNames ();
1020 Assert.IsNotNull (resNames, "#B2");
1021 Assert.AreEqual (1, resNames.Length, "#B3");
1022 Assert.AreEqual ("read", resNames[0], "#B4");
1023 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1024 Assert.IsNotNull (resInfo, "#B5");
1025 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1026 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1027 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1028 Stream s = assembly.GetManifestResourceStream ("read");
1029 Assert.IsNotNull (s, "#B9");
1031 s = assembly.GetFile ("readme.txt");
1032 Assert.IsNotNull (s, "#B10");
1038 public void bug79872 ()
1040 Random rnd = new Random ();
1045 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1046 if (Directory.Exists (outdir)) {
1048 Directory.Delete (outdir, true);
1055 Directory.CreateDirectory (outdir);
1057 AssemblyName an = new AssemblyName ();
1058 an.Name = "bug79872";
1059 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1060 string dllname = "bug79872.dll";
1061 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1062 string netmodule = "bug79872.netmodule";
1063 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1064 TypeBuilder a1 = mb2.DefineType ("A");
1070 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1074 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1078 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1079 } catch (BadImageFormatException) {
1080 ok = true; // mono and .net 2.0 throw this
1081 } catch (FileLoadException) {
1082 ok = true; // .net 1.1 throws this
1086 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1088 Directory.Delete (outdir, true);
1090 #endif // TARGET_JVM
1094 public void ManifestModule ()
1096 Assembly assembly = typeof (int).Assembly;
1097 Module module = assembly.ManifestModule;
1098 Assert.IsNotNull (module, "#1");
1099 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1100 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1101 Assert.IsFalse (module.IsResource (), "#4");
1102 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1103 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1104 Assert.AreSame (module, assembly.ManifestModule, "#7");
1109 private class AssemblyResolveHandler
1111 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1113 _assemblyFile = assemblyFile;
1114 _assemblyName = assemblyName;
1116 AppDomain.CurrentDomain.AssemblyResolve +=
1117 new ResolveEventHandler (ResolveAssembly);
1120 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1122 if (args.Name == _assemblyName)
1123 return Assembly.LoadFrom (_assemblyFile);
1128 private readonly string _assemblyFile;
1129 private readonly string _assemblyName;
1132 protected internal class Bug328812_NestedFamORAssem { };
1135 public void bug328812 ()
1137 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1138 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1139 // Just a sanity check, in case the above passed for some other reason
1140 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1144 public void GetCustomAttributes_AttributeType_Null ()
1146 Assembly a = typeof (int).Assembly;
1148 a.GetCustomAttributes (null, false);
1150 } catch (ArgumentNullException ex) {
1151 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1152 Assert.IsNull (ex.InnerException, "#3");
1153 Assert.IsNotNull (ex.Message, "#4");
1154 Assert.IsNotNull (ex.ParamName, "#5");
1155 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1160 public void GetTypeWithEmptyStringShouldThrow ()
1163 typeof (string).Assembly.GetType ("");
1165 } catch (ArgumentException) {}