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;
46 namespace MonoTests.System.Reflection
49 public class AssemblyTest
51 static string TempFolder = Path.Combine (Path.GetTempPath (),
52 "MonoTests.System.Reflection.AssemblyTest");
57 while (Directory.Exists (TempFolder))
58 TempFolder = Path.Combine (TempFolder, "2");
59 Directory.CreateDirectory (TempFolder);
63 public void TearDown ()
66 // This throws an exception under MS.NET, since the directory contains loaded
68 Directory.Delete (TempFolder, true);
74 public void CreateInstance()
76 Type type = typeof (AssemblyTest);
77 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
78 Assert.IsNotNull (obj, "#01");
79 Assert.AreEqual (GetType (), obj.GetType (), "#02");
83 public void CreateInvalidInstance()
85 Type type = typeof (AssemblyTest);
86 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
87 Assert.IsNull (obj, "#03");
91 [Category ("NotWorking")]
92 [ExpectedException (typeof (ArgumentException))]
93 public void GetType_TypeName_Invalid ()
95 typeof (int).Assembly.GetType ("&blabla", true, true);
99 public void GetType_TypeName_AssemblyName ()
101 Assembly a = typeof (int).Assembly;
102 string typeName = typeof (string).AssemblyQualifiedName;
104 a.GetType (typeName, true, false);
106 } catch (ArgumentException ex) {
107 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
108 Assert.IsNull (ex.InnerException, "#A3");
109 Assert.IsNotNull (ex.Message, "#A4");
110 Assert.IsNull (ex.ParamName, "#A5");
113 Type type = a.GetType (typeName, false);
114 Assert.IsNull (type, "#B1");
115 type = a.GetType (typeName, false, true);
116 Assert.IsNull (type, "#B2");
120 public void GetEntryAssembly ()
122 // note: only available in default appdomain
123 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
124 // Not sure we should emulate this behavior.
125 string fname = AppDomain.CurrentDomain.FriendlyName;
126 if (fname.EndsWith (".dll")) { // nunit-console
127 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
128 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
130 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
131 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
135 #if !MONOTOUCH // Reflection.Emit is not supported.
137 public void GetModules_MissingFile ()
139 AssemblyName newName = new AssemblyName ();
140 newName.Name = "AssemblyTest";
142 AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
144 ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
146 ab.Save ("test_assembly.dll");
148 File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
150 Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
154 } catch (FileNotFoundException ex) {
155 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
160 [Category ("NotWorking")]
162 public void Corlib ()
164 Assembly corlib = typeof (int).Assembly;
165 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
166 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
167 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
168 Assert.IsNotNull (corlib.Evidence, "Evidence");
169 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
171 // corlib doesn't reference anything
172 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
173 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
174 // not really "true" but it's even more trusted so...
175 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
176 Assert.AreEqual (0, corlib.HostContext, "HostContext");
177 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
178 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
179 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
183 public void Corlib_test ()
185 Assembly corlib_test = Assembly.GetExecutingAssembly ();
187 Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
188 Assert.IsNull (corlib_test.Evidence, "Evidence");
190 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
191 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
193 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
195 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
196 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
197 #if NET_4_0 && !MOBILE
198 Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
200 Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
203 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
204 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
208 public void GetAssembly ()
210 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
211 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
215 public void GetFile_Null ()
218 Assembly.GetExecutingAssembly ().GetFile (null);
220 } catch (ArgumentNullException ex) {
221 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
222 Assert.IsNull (ex.InnerException, "#3");
223 Assert.IsNotNull (ex.Message, "#4");
224 Assert.IsNull (ex.ParamName, "#5");
229 public void GetFile_Empty ()
232 Assembly.GetExecutingAssembly ().GetFile (
235 } catch (ArgumentException ex) {
236 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
237 Assert.IsNull (ex.InnerException, "#3");
238 Assert.IsNotNull (ex.Message, "#4");
239 Assert.IsNull (ex.ParamName, "#5");
244 public void GetFiles_False ()
246 Assembly corlib = typeof (int).Assembly;
247 FileStream[] fss = corlib.GetFiles ();
248 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
250 Assembly corlib_test = Assembly.GetExecutingAssembly ();
251 fss = corlib_test.GetFiles ();
252 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
256 public void GetFiles_True ()
258 Assembly corlib = typeof (int).Assembly;
259 FileStream[] fss = corlib.GetFiles ();
260 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
262 Assembly corlib_test = Assembly.GetExecutingAssembly ();
263 fss = corlib_test.GetFiles ();
264 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
268 public void GetManifestResourceStream_Name_Empty ()
270 Assembly corlib = typeof (int).Assembly;
273 corlib.GetManifestResourceStream (string.Empty);
275 } catch (ArgumentException ex) {
276 // String cannot have zero length
277 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
278 Assert.IsNull (ex.InnerException, "#A3");
279 Assert.IsNotNull (ex.Message, "#A4");
282 corlib.GetManifestResourceStream (typeof (int), string.Empty);
285 corlib.GetManifestResourceStream ((Type) null, string.Empty);
287 } catch (ArgumentException ex) {
288 // String cannot have zero length
289 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
290 Assert.IsNull (ex.InnerException, "#B3");
291 Assert.IsNotNull (ex.Message, "#B4");
296 public void GetManifestResourceStream_Name_Null ()
298 Assembly corlib = typeof (int).Assembly;
301 corlib.GetManifestResourceStream ((string) null);
303 } catch (ArgumentNullException ex) {
304 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
305 Assert.IsNull (ex.InnerException, "#A3");
306 Assert.IsNotNull (ex.Message, "#A4");
309 corlib.GetManifestResourceStream (typeof (int), (string) null);
312 corlib.GetManifestResourceStream ((Type) null, (string) null);
314 } catch (ArgumentNullException ex) {
315 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
316 Assert.IsNull (ex.InnerException, "#B3");
317 Assert.IsNotNull (ex.Message, "#B4");
318 Assert.IsNotNull (ex.ParamName, "#B5");
319 Assert.AreEqual ("type", ex.ParamName, "#B6");
324 public void IsDefined_AttributeType_Null ()
326 Assembly corlib = typeof (int).Assembly;
329 corlib.IsDefined ((Type) null, false);
331 } catch (ArgumentNullException ex) {
332 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
333 Assert.IsNull (ex.InnerException, "#3");
334 Assert.IsNotNull (ex.Message, "#4");
335 Assert.IsNotNull (ex.ParamName, "#5");
336 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
341 public void LoadFrom_Empty_Assembly ()
343 string tempFile = Path.GetTempFileName ();
346 Assembly.LoadFrom (tempFile);
348 } catch (BadImageFormatException ex) {
349 Assert.IsNull (ex.InnerException, "#2");
351 File.Delete (tempFile);
356 public void LoadFrom_Invalid_Assembly ()
358 string tempFile = Path.GetTempFileName ();
359 using (StreamWriter sw = File.CreateText (tempFile)) {
360 sw.WriteLine ("foo");
365 Assembly.LoadFrom (tempFile);
367 } catch (BadImageFormatException ex) {
368 Assert.IsNull (ex.InnerException, "#2");
370 File.Delete (tempFile);
375 public void LoadFrom_NonExisting_Assembly ()
377 string tempFile = Path.GetTempFileName ();
378 File.Delete (tempFile);
381 Assembly.LoadFrom (tempFile);
383 } catch (FileNotFoundException ex) {
384 Assert.IsNull (ex.InnerException, "#2");
386 File.Delete (tempFile);
391 public void LoadWithPartialName ()
393 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" };
395 foreach (string s in names)
396 if (Assembly.LoadWithPartialName (s) != null)
398 Assert.Fail ("Was not able to load any corlib test");
402 public void GetObjectData_Info_Null ()
404 Assembly corlib = typeof (int).Assembly;
406 corlib.GetObjectData (null, new StreamingContext (
407 StreamingContextStates.All));
409 } catch (ArgumentNullException ex) {
410 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
411 Assert.IsNull (ex.InnerException, "#3");
412 Assert.IsNotNull (ex.Message, "#4");
413 Assert.IsNotNull (ex.ParamName, "#5");
414 Assert.AreEqual ("info", ex.ParamName, "#6");
419 public void GetReferencedAssemblies ()
421 Assembly corlib_test = Assembly.GetExecutingAssembly ();
422 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
423 foreach (AssemblyName an in names) {
424 Assert.IsNull (an.CodeBase, "CodeBase");
425 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
426 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
427 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
428 Assert.IsNotNull (an.FullName, "FullName");
429 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
430 Assert.IsNull (an.KeyPair, "KeyPair");
431 Assert.IsNotNull (an.Name, "Name");
432 Assert.IsNotNull (an.Version, "Version");
433 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
434 an.VersionCompatibility, "VersionCompatibility");
438 #if !MONOTOUCH // Reflection.Emit is not supported.
440 public void Location_Empty() {
441 string assemblyFileName = Path.Combine (
442 Path.GetTempPath (), "AssemblyLocation.dll");
445 AssemblyName assemblyName = new AssemblyName ();
446 assemblyName.Name = "AssemblyLocation";
448 AssemblyBuilder ab = AppDomain.CurrentDomain
449 .DefineDynamicAssembly (assemblyName,
450 AssemblyBuilderAccess.Save,
452 AppDomain.CurrentDomain.Evidence);
453 ab.Save (Path.GetFileName (assemblyFileName));
455 using (FileStream fs = File.OpenRead (assemblyFileName)) {
456 byte[] buffer = new byte[fs.Length];
457 fs.Read (buffer, 0, buffer.Length);
458 Assembly assembly = Assembly.Load (buffer);
459 Assert.AreEqual (string.Empty, assembly.Location);
463 File.Delete (assemblyFileName);
468 [Category ("NotWorking")]
469 public void bug78464 ()
471 string assemblyFileName = Path.Combine (
472 Path.GetTempPath (), "bug78464.dll");
475 // execute test in separate appdomain to allow assembly to be unloaded
476 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
477 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
479 crossDomainTester.bug78464 (assemblyFileName);
481 AppDomain.Unload (testDomain);
484 File.Delete (assemblyFileName);
489 public void bug78465 ()
491 string assemblyFileName = Path.Combine (
492 Path.GetTempPath (), "bug78465.dll");
495 AssemblyName assemblyName = new AssemblyName ();
496 assemblyName.Name = "bug78465";
498 AssemblyBuilder ab = AppDomain.CurrentDomain
499 .DefineDynamicAssembly (assemblyName,
500 AssemblyBuilderAccess.Save,
501 Path.GetDirectoryName (assemblyFileName),
502 AppDomain.CurrentDomain.Evidence);
503 ab.Save (Path.GetFileName (assemblyFileName));
505 using (FileStream fs = File.OpenRead (assemblyFileName)) {
506 byte[] buffer = new byte[fs.Length];
507 fs.Read (buffer, 0, buffer.Length);
508 Assembly assembly = Assembly.Load (buffer);
509 Assert.AreEqual (string.Empty, assembly.Location, "#1");
513 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
514 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
516 crossDomainTester.bug78465 (assemblyFileName);
518 AppDomain.Unload (testDomain);
521 File.Delete (assemblyFileName);
526 public void bug78468 ()
528 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
530 string resourceFileName = Path.Combine (Path.GetTempPath (),
533 using (StreamWriter sw = File.CreateText (resourceFileName)) {
534 sw.WriteLine ("FOO");
539 AssemblyName assemblyName = new AssemblyName ();
540 assemblyName.Name = "bug78468a";
542 AssemblyBuilder ab = AppDomain.CurrentDomain
543 .DefineDynamicAssembly (assemblyName,
544 AssemblyBuilderAccess.Save,
546 AppDomain.CurrentDomain.Evidence);
547 ab.AddResourceFile ("read", "readme.txt");
548 ab.Save (Path.GetFileName (assemblyFileNameA));
552 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
553 byte[] buffer = new byte[fs.Length];
554 fs.Read (buffer, 0, buffer.Length);
555 assembly = Assembly.Load (buffer);
559 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
560 string[] resNames = assembly.GetManifestResourceNames ();
561 Assert.IsNotNull (resNames, "#A2");
562 Assert.AreEqual (1, resNames.Length, "#A3");
563 Assert.AreEqual ("read", resNames[0], "#A4");
564 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
565 Assert.IsNotNull (resInfo, "#A5");
566 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
567 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
568 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
570 assembly.GetManifestResourceStream ("read");
572 } catch (FileNotFoundException) {
575 assembly.GetFile ("readme.txt");
576 Assert.Fail ("#A10");
577 } catch (FileNotFoundException) {
580 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
583 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
584 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
586 crossDomainTester.bug78468 (assemblyFileNameB);
588 AppDomain.Unload (testDomain);
589 File.Delete (assemblyFileNameB);
592 File.Delete (assemblyFileNameA);
593 File.Delete (resourceFileName);
598 [Category ("NotWorking")]
599 public void ReflectionOnlyLoad ()
601 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
603 Assert.IsNotNull (assembly);
604 Assert.IsTrue (assembly.ReflectionOnly);
608 public void ReflectionOnlyLoadFrom ()
610 string loc = typeof (AssemblyTest).Assembly.Location;
611 string filename = Path.GetFileName (loc);
612 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
614 Assert.IsNotNull (assembly);
615 Assert.IsTrue (assembly.ReflectionOnly);
619 [ExpectedException (typeof (ArgumentException))]
620 public void CreateInstanceOnRefOnly ()
622 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
623 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
627 [Category ("NotWorking")] // patch for bug #79720 must be committed first
628 public void Load_Culture ()
630 string tempDir = Path.Combine (Path.GetTempPath (),
631 "MonoTests.System.Reflection.AssemblyTest");
632 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
633 if (!Directory.Exists (cultureTempDir))
634 Directory.CreateDirectory (cultureTempDir);
635 cultureTempDir = Path.Combine (tempDir, "en-US");
636 if (!Directory.Exists (cultureTempDir))
637 Directory.CreateDirectory (cultureTempDir);
640 AppDomain ad = CreateTestDomain (tempDir, true);
642 CrossDomainTester cdt = CreateCrossDomainTester (ad);
646 AssemblyName aname = new AssemblyName ();
647 aname.Name = "culturea";
648 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
650 aname = new AssemblyName ();
651 aname.Name = "culturea";
652 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
654 aname = new AssemblyName ();
655 aname.Name = "culturea";
656 aname.CultureInfo = new CultureInfo ("nl-BE");
657 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
659 aname = new AssemblyName ();
660 aname.Name = "culturea";
661 aname.CultureInfo = CultureInfo.InvariantCulture;
662 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
666 aname = new AssemblyName ();
667 aname.Name = "cultureb";
668 aname.CultureInfo = new CultureInfo ("nl-BE");
669 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
671 aname = new AssemblyName ();
672 aname.Name = "cultureb";
673 aname.CultureInfo = new CultureInfo ("nl-BE");
674 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
676 aname = new AssemblyName ();
677 aname.Name = "cultureb";
678 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
680 aname = new AssemblyName ();
681 aname.Name = "cultureb";
682 aname.CultureInfo = new CultureInfo ("en-US");
683 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
687 aname = new AssemblyName ();
688 aname.Name = "culturec";
689 aname.CultureInfo = new CultureInfo ("nl-BE");
690 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
692 aname = new AssemblyName ();
693 aname.Name = "culturec";
694 aname.CultureInfo = new CultureInfo ("nl-BE");
695 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
697 aname = new AssemblyName ();
698 aname.Name = "culturec";
699 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
701 aname = new AssemblyName ();
702 aname.Name = "culturec";
703 aname.CultureInfo = CultureInfo.InvariantCulture;
704 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
708 aname = new AssemblyName ();
709 aname.Name = "cultured";
710 aname.CultureInfo = new CultureInfo ("nl-BE");
711 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
713 aname = new AssemblyName ();
714 aname.Name = "cultured";
715 aname.CultureInfo = new CultureInfo ("nl-BE");
716 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
718 aname = new AssemblyName ();
719 aname.Name = "cultured";
720 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
722 aname = new AssemblyName ();
723 aname.Name = "cultured";
724 aname.CultureInfo = CultureInfo.InvariantCulture;
725 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
727 AppDomain.Unload (ad);
728 if (Directory.Exists (tempDir))
729 Directory.Delete (tempDir, true);
734 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
735 public void Load_Culture_Mismatch ()
737 string tempDir = Path.Combine (Path.GetTempPath (),
738 "MonoTests.System.Reflection.AssemblyTest");
739 string cultureTempDir = Path.Combine (tempDir, "en-US");
740 if (!Directory.Exists (cultureTempDir))
741 Directory.CreateDirectory (cultureTempDir);
743 AppDomain ad = CreateTestDomain (tempDir, true);
745 CrossDomainTester cdt = CreateCrossDomainTester (ad);
749 AssemblyName aname = new AssemblyName ();
750 aname.Name = "bug79712a";
751 aname.CultureInfo = new CultureInfo ("nl-BE");
752 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
754 aname = new AssemblyName ();
755 aname.Name = "bug79712a";
756 aname.CultureInfo = CultureInfo.InvariantCulture;
757 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
761 aname = new AssemblyName ();
762 aname.Name = "bug79712b";
763 aname.CultureInfo = new CultureInfo ("nl-BE");
764 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
766 aname = new AssemblyName ();
767 aname.Name = "bug79712b";
768 aname.CultureInfo = new CultureInfo ("en-US");
769 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
771 AppDomain.Unload (ad);
772 if (Directory.Exists (tempDir))
773 Directory.Delete (tempDir, true);
779 public void Load_PartialVersion ()
781 string tempDir = Path.Combine (Path.GetTempPath (),
782 "MonoTests.System.Reflection.AssemblyTest");
783 if (!Directory.Exists (tempDir))
784 Directory.CreateDirectory (tempDir);
786 AppDomain ad = CreateTestDomain (tempDir, true);
788 CrossDomainTester cdt = CreateCrossDomainTester (ad);
790 AssemblyName aname = new AssemblyName ();
791 aname.Name = "bug79715";
792 aname.Version = new Version (1, 2, 3, 4);
793 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
795 aname = new AssemblyName ();
796 aname.Name = "bug79715";
797 aname.Version = new Version (1, 2);
798 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
799 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
801 aname = new AssemblyName ();
802 aname.Name = "bug79715";
803 aname.Version = new Version (1, 2, 3);
804 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
805 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
807 aname = new AssemblyName ();
808 aname.Name = "bug79715";
809 aname.Version = new Version (1, 2, 3, 4);
810 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
811 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
813 AppDomain.Unload (ad);
814 if (Directory.Exists (tempDir))
815 Directory.Delete (tempDir, true);
819 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
821 AppDomainSetup setup = new AppDomainSetup ();
822 setup.ApplicationBase = baseDirectory;
823 setup.ApplicationName = "testdomain";
825 AppDomain ad = AppDomain.CreateDomain ("testdomain",
826 AppDomain.CurrentDomain.Evidence, setup);
828 if (assemblyResolver) {
829 Assembly ea = Assembly.GetExecutingAssembly ();
830 ad.CreateInstanceFrom (ea.CodeBase,
831 typeof (AssemblyResolveHandler).FullName,
833 BindingFlags.Public | BindingFlags.Instance,
835 new object [] { ea.Location, ea.FullName },
836 CultureInfo.InvariantCulture,
844 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
846 Type testerType = typeof (CrossDomainTester);
847 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
848 testerType.Assembly.FullName, testerType.FullName, false,
849 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
850 CultureInfo.InvariantCulture, new object[0], null);
853 private class CrossDomainTester : MarshalByRefObject
855 public void GenerateAssembly (AssemblyName aname, string path)
857 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
858 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
859 ab.Save (Path.GetFileName (path));
862 public void Load (AssemblyName assemblyRef)
864 Assembly.Load (assemblyRef);
867 public void LoadFrom (string assemblyFile)
869 Assembly.LoadFrom (assemblyFile);
872 public bool AssertLoad (AssemblyName assemblyRef)
875 Assembly.Load (assemblyRef);
882 public bool AssertLoad (string assemblyString)
885 Assembly.Load (assemblyString);
892 public bool AssertFileLoadException (AssemblyName assemblyRef)
895 Assembly.Load (assemblyRef);
897 } catch (FileLoadException) {
902 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
905 Assembly.Load (assemblyRef);
907 } catch (FileNotFoundException) {
912 public void bug78464 (string assemblyFileName)
914 AssemblyName assemblyName = new AssemblyName ();
915 assemblyName.Name = "bug78464";
917 AssemblyBuilder ab = AppDomain.CurrentDomain
918 .DefineDynamicAssembly (assemblyName,
919 AssemblyBuilderAccess.Save,
920 Path.GetDirectoryName (assemblyFileName),
921 AppDomain.CurrentDomain.Evidence);
922 ab.Save (Path.GetFileName (assemblyFileName));
926 using (FileStream fs = File.OpenRead (assemblyFileName)) {
927 byte[] buffer = new byte[fs.Length];
928 fs.Read (buffer, 0, buffer.Length);
929 assembly = Assembly.Load (buffer);
933 Assert.AreEqual (string.Empty, assembly.Location, "#1");
935 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
936 Assert.IsFalse (assembly.Location == string.Empty, "#2");
937 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
938 // note: we cannot check if directory names match, as MS.NET seems to
939 // convert directory part of assembly location to lowercase
940 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
943 public void bug78465 (string assemblyFileName)
945 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
946 Assert.IsFalse (assembly.Location == string.Empty, "#2");
947 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
948 // note: we cannot check if directory names match, as MS.NET seems to
949 // convert directory part of assembly location to lowercase
950 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
953 public void bug78468 (string assemblyFileName)
955 AssemblyName assemblyName = new AssemblyName ();
956 assemblyName.Name = "bug78468b";
958 AssemblyBuilder ab = AppDomain.CurrentDomain
959 .DefineDynamicAssembly (assemblyName,
960 AssemblyBuilderAccess.Save,
961 Path.GetDirectoryName (assemblyFileName),
962 AppDomain.CurrentDomain.Evidence);
963 ab.AddResourceFile ("read", "readme.txt");
964 ab.Save (Path.GetFileName (assemblyFileName));
966 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
967 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
968 string[] resNames = assembly.GetManifestResourceNames ();
969 Assert.IsNotNull (resNames, "#B2");
970 Assert.AreEqual (1, resNames.Length, "#B3");
971 Assert.AreEqual ("read", resNames[0], "#B4");
972 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
973 Assert.IsNotNull (resInfo, "#B5");
974 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
975 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
976 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
977 Stream s = assembly.GetManifestResourceStream ("read");
978 Assert.IsNotNull (s, "#B9");
980 s = assembly.GetFile ("readme.txt");
981 Assert.IsNotNull (s, "#B10");
987 public void bug79872 ()
989 Random rnd = new Random ();
994 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
995 if (Directory.Exists (outdir)) {
997 Directory.Delete (outdir, true);
1004 Directory.CreateDirectory (outdir);
1006 AssemblyName an = new AssemblyName ();
1007 an.Name = "bug79872";
1008 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1009 string dllname = "bug79872.dll";
1010 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1011 string netmodule = "bug79872.netmodule";
1012 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1013 TypeBuilder a1 = mb2.DefineType ("A");
1019 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1023 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1027 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1028 } catch (BadImageFormatException) {
1029 ok = true; // mono and .net 2.0 throw this
1030 } catch (FileLoadException) {
1031 ok = true; // .net 1.1 throws this
1035 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1037 Directory.Delete (outdir, true);
1042 public void ManifestModule ()
1044 Assembly assembly = typeof (int).Assembly;
1045 Module module = assembly.ManifestModule;
1046 Assert.IsNotNull (module, "#1");
1049 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1051 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1055 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1057 Assert.IsFalse (module.IsResource (), "#4");
1058 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1059 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1060 Assert.AreSame (module, assembly.ManifestModule, "#7");
1065 private class AssemblyResolveHandler
1067 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1069 _assemblyFile = assemblyFile;
1070 _assemblyName = assemblyName;
1072 AppDomain.CurrentDomain.AssemblyResolve +=
1073 new ResolveEventHandler (ResolveAssembly);
1076 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1078 if (args.Name == _assemblyName)
1079 return Assembly.LoadFrom (_assemblyFile);
1084 private readonly string _assemblyFile;
1085 private readonly string _assemblyName;
1088 protected internal class Bug328812_NestedFamORAssem { };
1091 public void bug328812 ()
1093 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1094 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1095 // Just a sanity check, in case the above passed for some other reason
1096 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1100 public void GetCustomAttributes_AttributeType_Null ()
1102 Assembly a = typeof (int).Assembly;
1104 a.GetCustomAttributes (null, false);
1106 } catch (ArgumentNullException ex) {
1107 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1108 Assert.IsNull (ex.InnerException, "#3");
1109 Assert.IsNotNull (ex.Message, "#4");
1110 Assert.IsNotNull (ex.ParamName, "#5");
1111 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1116 public void GetTypeWithEmptyStringShouldThrow ()
1119 typeof (string).Assembly.GetType ("");
1121 } catch (ArgumentException) {}
1126 public void DefinedTypes_Equality ()
1128 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1129 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1131 Assert.AreSame (x1, x2, "#1");
1136 public class TestDefinedTypes