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");
342 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
344 public void LoadFrom_Empty_Assembly ()
346 string tempFile = Path.GetTempFileName ();
349 Assembly.LoadFrom (tempFile);
351 } catch (BadImageFormatException ex) {
352 Assert.IsNull (ex.InnerException, "#2");
354 File.Delete (tempFile);
359 public void LoadFrom_Invalid_Assembly ()
361 string tempFile = Path.GetTempFileName ();
362 using (StreamWriter sw = File.CreateText (tempFile)) {
363 sw.WriteLine ("foo");
368 Assembly.LoadFrom (tempFile);
370 } catch (BadImageFormatException ex) {
371 Assert.IsNull (ex.InnerException, "#2");
373 File.Delete (tempFile);
378 public void LoadFrom_NonExisting_Assembly ()
380 string tempFile = Path.GetTempFileName ();
381 File.Delete (tempFile);
384 Assembly.LoadFrom (tempFile);
386 } catch (FileNotFoundException ex) {
387 Assert.IsNull (ex.InnerException, "#2");
389 File.Delete (tempFile);
394 public void LoadWithPartialName ()
396 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" };
398 foreach (string s in names)
399 if (Assembly.LoadWithPartialName (s) != null)
401 Assert.Fail ("Was not able to load any corlib test");
405 public void GetObjectData_Info_Null ()
407 Assembly corlib = typeof (int).Assembly;
409 corlib.GetObjectData (null, new StreamingContext (
410 StreamingContextStates.All));
412 } catch (ArgumentNullException ex) {
413 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
414 Assert.IsNull (ex.InnerException, "#3");
415 Assert.IsNotNull (ex.Message, "#4");
416 Assert.IsNotNull (ex.ParamName, "#5");
417 Assert.AreEqual ("info", ex.ParamName, "#6");
422 public void GetReferencedAssemblies ()
424 Assembly corlib_test = Assembly.GetExecutingAssembly ();
425 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
426 foreach (AssemblyName an in names) {
427 Assert.IsNull (an.CodeBase, "CodeBase");
428 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
429 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
430 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
431 Assert.IsNotNull (an.FullName, "FullName");
432 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
433 Assert.IsNull (an.KeyPair, "KeyPair");
434 Assert.IsNotNull (an.Name, "Name");
435 Assert.IsNotNull (an.Version, "Version");
436 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
437 an.VersionCompatibility, "VersionCompatibility");
441 #if !MONOTOUCH // Reflection.Emit is not supported.
443 public void Location_Empty() {
444 string assemblyFileName = Path.Combine (
445 Path.GetTempPath (), "AssemblyLocation.dll");
448 AssemblyName assemblyName = new AssemblyName ();
449 assemblyName.Name = "AssemblyLocation";
451 AssemblyBuilder ab = AppDomain.CurrentDomain
452 .DefineDynamicAssembly (assemblyName,
453 AssemblyBuilderAccess.Save,
455 AppDomain.CurrentDomain.Evidence);
456 ab.Save (Path.GetFileName (assemblyFileName));
458 using (FileStream fs = File.OpenRead (assemblyFileName)) {
459 byte[] buffer = new byte[fs.Length];
460 fs.Read (buffer, 0, buffer.Length);
461 Assembly assembly = Assembly.Load (buffer);
462 Assert.AreEqual (string.Empty, assembly.Location);
466 File.Delete (assemblyFileName);
471 [Category ("NotWorking")]
472 public void bug78464 ()
474 string assemblyFileName = Path.Combine (
475 Path.GetTempPath (), "bug78464.dll");
478 // execute test in separate appdomain to allow assembly to be unloaded
479 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
480 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
482 crossDomainTester.bug78464 (assemblyFileName);
484 AppDomain.Unload (testDomain);
487 File.Delete (assemblyFileName);
492 public void bug78465 ()
494 string assemblyFileName = Path.Combine (
495 Path.GetTempPath (), "bug78465.dll");
498 AssemblyName assemblyName = new AssemblyName ();
499 assemblyName.Name = "bug78465";
501 AssemblyBuilder ab = AppDomain.CurrentDomain
502 .DefineDynamicAssembly (assemblyName,
503 AssemblyBuilderAccess.Save,
504 Path.GetDirectoryName (assemblyFileName),
505 AppDomain.CurrentDomain.Evidence);
506 ab.Save (Path.GetFileName (assemblyFileName));
508 using (FileStream fs = File.OpenRead (assemblyFileName)) {
509 byte[] buffer = new byte[fs.Length];
510 fs.Read (buffer, 0, buffer.Length);
511 Assembly assembly = Assembly.Load (buffer);
512 Assert.AreEqual (string.Empty, assembly.Location, "#1");
516 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
517 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
519 crossDomainTester.bug78465 (assemblyFileName);
521 AppDomain.Unload (testDomain);
524 File.Delete (assemblyFileName);
529 public void bug78468 ()
531 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
533 string resourceFileName = Path.Combine (Path.GetTempPath (),
536 using (StreamWriter sw = File.CreateText (resourceFileName)) {
537 sw.WriteLine ("FOO");
542 AssemblyName assemblyName = new AssemblyName ();
543 assemblyName.Name = "bug78468a";
545 AssemblyBuilder ab = AppDomain.CurrentDomain
546 .DefineDynamicAssembly (assemblyName,
547 AssemblyBuilderAccess.Save,
549 AppDomain.CurrentDomain.Evidence);
550 ab.AddResourceFile ("read", "readme.txt");
551 ab.Save (Path.GetFileName (assemblyFileNameA));
555 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
556 byte[] buffer = new byte[fs.Length];
557 fs.Read (buffer, 0, buffer.Length);
558 assembly = Assembly.Load (buffer);
562 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
563 string[] resNames = assembly.GetManifestResourceNames ();
564 Assert.IsNotNull (resNames, "#A2");
565 Assert.AreEqual (1, resNames.Length, "#A3");
566 Assert.AreEqual ("read", resNames[0], "#A4");
567 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
568 Assert.IsNotNull (resInfo, "#A5");
569 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
570 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
571 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
573 assembly.GetManifestResourceStream ("read");
575 } catch (FileNotFoundException) {
578 assembly.GetFile ("readme.txt");
579 Assert.Fail ("#A10");
580 } catch (FileNotFoundException) {
583 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
586 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
587 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
589 crossDomainTester.bug78468 (assemblyFileNameB);
591 AppDomain.Unload (testDomain);
592 File.Delete (assemblyFileNameB);
595 File.Delete (assemblyFileNameA);
596 File.Delete (resourceFileName);
601 [Category ("NotWorking")]
602 public void ReflectionOnlyLoad ()
604 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
606 Assert.IsNotNull (assembly);
607 Assert.IsTrue (assembly.ReflectionOnly);
611 public void ReflectionOnlyLoadFrom ()
613 string loc = typeof (AssemblyTest).Assembly.Location;
614 string filename = Path.GetFileName (loc);
615 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
617 Assert.IsNotNull (assembly);
618 Assert.IsTrue (assembly.ReflectionOnly);
622 [ExpectedException (typeof (ArgumentException))]
623 public void CreateInstanceOnRefOnly ()
625 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
626 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
630 [Category ("NotWorking")] // patch for bug #79720 must be committed first
631 public void Load_Culture ()
633 string tempDir = Path.Combine (Path.GetTempPath (),
634 "MonoTests.System.Reflection.AssemblyTest");
635 string cultureTempDir = Path.Combine (tempDir, "nl-BE");
636 if (!Directory.Exists (cultureTempDir))
637 Directory.CreateDirectory (cultureTempDir);
638 cultureTempDir = Path.Combine (tempDir, "en-US");
639 if (!Directory.Exists (cultureTempDir))
640 Directory.CreateDirectory (cultureTempDir);
643 AppDomain ad = CreateTestDomain (tempDir, true);
645 CrossDomainTester cdt = CreateCrossDomainTester (ad);
649 AssemblyName aname = new AssemblyName ();
650 aname.Name = "culturea";
651 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
653 aname = new AssemblyName ();
654 aname.Name = "culturea";
655 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
657 aname = new AssemblyName ();
658 aname.Name = "culturea";
659 aname.CultureInfo = new CultureInfo ("nl-BE");
660 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
662 aname = new AssemblyName ();
663 aname.Name = "culturea";
664 aname.CultureInfo = CultureInfo.InvariantCulture;
665 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
669 aname = new AssemblyName ();
670 aname.Name = "cultureb";
671 aname.CultureInfo = new CultureInfo ("nl-BE");
672 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
674 aname = new AssemblyName ();
675 aname.Name = "cultureb";
676 aname.CultureInfo = new CultureInfo ("nl-BE");
677 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
679 aname = new AssemblyName ();
680 aname.Name = "cultureb";
681 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
683 aname = new AssemblyName ();
684 aname.Name = "cultureb";
685 aname.CultureInfo = new CultureInfo ("en-US");
686 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
690 aname = new AssemblyName ();
691 aname.Name = "culturec";
692 aname.CultureInfo = new CultureInfo ("nl-BE");
693 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
695 aname = new AssemblyName ();
696 aname.Name = "culturec";
697 aname.CultureInfo = new CultureInfo ("nl-BE");
698 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
700 aname = new AssemblyName ();
701 aname.Name = "culturec";
702 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
704 aname = new AssemblyName ();
705 aname.Name = "culturec";
706 aname.CultureInfo = CultureInfo.InvariantCulture;
707 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
711 aname = new AssemblyName ();
712 aname.Name = "cultured";
713 aname.CultureInfo = new CultureInfo ("nl-BE");
714 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
716 aname = new AssemblyName ();
717 aname.Name = "cultured";
718 aname.CultureInfo = new CultureInfo ("nl-BE");
719 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
721 aname = new AssemblyName ();
722 aname.Name = "cultured";
723 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
725 aname = new AssemblyName ();
726 aname.Name = "cultured";
727 aname.CultureInfo = CultureInfo.InvariantCulture;
728 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
730 AppDomain.Unload (ad);
731 if (Directory.Exists (tempDir))
732 Directory.Delete (tempDir, true);
737 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
738 public void Load_Culture_Mismatch ()
740 string tempDir = Path.Combine (Path.GetTempPath (),
741 "MonoTests.System.Reflection.AssemblyTest");
742 string cultureTempDir = Path.Combine (tempDir, "en-US");
743 if (!Directory.Exists (cultureTempDir))
744 Directory.CreateDirectory (cultureTempDir);
746 AppDomain ad = CreateTestDomain (tempDir, true);
748 CrossDomainTester cdt = CreateCrossDomainTester (ad);
752 AssemblyName aname = new AssemblyName ();
753 aname.Name = "bug79712a";
754 aname.CultureInfo = new CultureInfo ("nl-BE");
755 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
757 aname = new AssemblyName ();
758 aname.Name = "bug79712a";
759 aname.CultureInfo = CultureInfo.InvariantCulture;
760 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
764 aname = new AssemblyName ();
765 aname.Name = "bug79712b";
766 aname.CultureInfo = new CultureInfo ("nl-BE");
767 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
769 aname = new AssemblyName ();
770 aname.Name = "bug79712b";
771 aname.CultureInfo = new CultureInfo ("en-US");
772 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
774 AppDomain.Unload (ad);
775 if (Directory.Exists (tempDir))
776 Directory.Delete (tempDir, true);
782 public void Load_PartialVersion ()
784 string tempDir = Path.Combine (Path.GetTempPath (),
785 "MonoTests.System.Reflection.AssemblyTest");
786 if (!Directory.Exists (tempDir))
787 Directory.CreateDirectory (tempDir);
789 AppDomain ad = CreateTestDomain (tempDir, true);
791 CrossDomainTester cdt = CreateCrossDomainTester (ad);
793 AssemblyName aname = new AssemblyName ();
794 aname.Name = "bug79715";
795 aname.Version = new Version (1, 2, 3, 4);
796 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
798 aname = new AssemblyName ();
799 aname.Name = "bug79715";
800 aname.Version = new Version (1, 2);
801 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
802 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
804 aname = new AssemblyName ();
805 aname.Name = "bug79715";
806 aname.Version = new Version (1, 2, 3);
807 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
808 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
810 aname = new AssemblyName ();
811 aname.Name = "bug79715";
812 aname.Version = new Version (1, 2, 3, 4);
813 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
814 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
816 AppDomain.Unload (ad);
817 if (Directory.Exists (tempDir))
818 Directory.Delete (tempDir, true);
822 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
824 AppDomainSetup setup = new AppDomainSetup ();
825 setup.ApplicationBase = baseDirectory;
826 setup.ApplicationName = "testdomain";
828 AppDomain ad = AppDomain.CreateDomain ("testdomain",
829 AppDomain.CurrentDomain.Evidence, setup);
831 if (assemblyResolver) {
832 Assembly ea = Assembly.GetExecutingAssembly ();
833 ad.CreateInstanceFrom (ea.CodeBase,
834 typeof (AssemblyResolveHandler).FullName,
836 BindingFlags.Public | BindingFlags.Instance,
838 new object [] { ea.Location, ea.FullName },
839 CultureInfo.InvariantCulture,
847 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
849 Type testerType = typeof (CrossDomainTester);
850 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
851 testerType.Assembly.FullName, testerType.FullName, false,
852 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
853 CultureInfo.InvariantCulture, new object[0], null);
856 private class CrossDomainTester : MarshalByRefObject
858 public void GenerateAssembly (AssemblyName aname, string path)
860 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
861 aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
862 ab.Save (Path.GetFileName (path));
865 public void Load (AssemblyName assemblyRef)
867 Assembly.Load (assemblyRef);
870 public void LoadFrom (string assemblyFile)
872 Assembly.LoadFrom (assemblyFile);
875 public bool AssertLoad (AssemblyName assemblyRef)
878 Assembly.Load (assemblyRef);
885 public bool AssertLoad (string assemblyString)
888 Assembly.Load (assemblyString);
895 public bool AssertFileLoadException (AssemblyName assemblyRef)
898 Assembly.Load (assemblyRef);
900 } catch (FileLoadException) {
905 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
908 Assembly.Load (assemblyRef);
910 } catch (FileNotFoundException) {
915 public void bug78464 (string assemblyFileName)
917 AssemblyName assemblyName = new AssemblyName ();
918 assemblyName.Name = "bug78464";
920 AssemblyBuilder ab = AppDomain.CurrentDomain
921 .DefineDynamicAssembly (assemblyName,
922 AssemblyBuilderAccess.Save,
923 Path.GetDirectoryName (assemblyFileName),
924 AppDomain.CurrentDomain.Evidence);
925 ab.Save (Path.GetFileName (assemblyFileName));
929 using (FileStream fs = File.OpenRead (assemblyFileName)) {
930 byte[] buffer = new byte[fs.Length];
931 fs.Read (buffer, 0, buffer.Length);
932 assembly = Assembly.Load (buffer);
936 Assert.AreEqual (string.Empty, assembly.Location, "#1");
938 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
939 Assert.IsFalse (assembly.Location == string.Empty, "#2");
940 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
941 // note: we cannot check if directory names match, as MS.NET seems to
942 // convert directory part of assembly location to lowercase
943 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
946 public void bug78465 (string assemblyFileName)
948 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
949 Assert.IsFalse (assembly.Location == string.Empty, "#2");
950 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
951 // note: we cannot check if directory names match, as MS.NET seems to
952 // convert directory part of assembly location to lowercase
953 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
956 public void bug78468 (string assemblyFileName)
958 AssemblyName assemblyName = new AssemblyName ();
959 assemblyName.Name = "bug78468b";
961 AssemblyBuilder ab = AppDomain.CurrentDomain
962 .DefineDynamicAssembly (assemblyName,
963 AssemblyBuilderAccess.Save,
964 Path.GetDirectoryName (assemblyFileName),
965 AppDomain.CurrentDomain.Evidence);
966 ab.AddResourceFile ("read", "readme.txt");
967 ab.Save (Path.GetFileName (assemblyFileName));
969 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
970 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
971 string[] resNames = assembly.GetManifestResourceNames ();
972 Assert.IsNotNull (resNames, "#B2");
973 Assert.AreEqual (1, resNames.Length, "#B3");
974 Assert.AreEqual ("read", resNames[0], "#B4");
975 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
976 Assert.IsNotNull (resInfo, "#B5");
977 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
978 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
979 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
980 Stream s = assembly.GetManifestResourceStream ("read");
981 Assert.IsNotNull (s, "#B9");
983 s = assembly.GetFile ("readme.txt");
984 Assert.IsNotNull (s, "#B10");
990 public void bug79872 ()
992 Random rnd = new Random ();
997 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
998 if (Directory.Exists (outdir)) {
1000 Directory.Delete (outdir, true);
1007 Directory.CreateDirectory (outdir);
1009 AssemblyName an = new AssemblyName ();
1010 an.Name = "bug79872";
1011 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1012 string dllname = "bug79872.dll";
1013 ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1014 string netmodule = "bug79872.netmodule";
1015 ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1016 TypeBuilder a1 = mb2.DefineType ("A");
1022 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1026 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1030 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1031 } catch (BadImageFormatException) {
1032 ok = true; // mono and .net 2.0 throw this
1033 } catch (FileLoadException) {
1034 ok = true; // .net 1.1 throws this
1038 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1040 Directory.Delete (outdir, true);
1045 public void ManifestModule ()
1047 Assembly assembly = typeof (int).Assembly;
1048 Module module = assembly.ManifestModule;
1049 Assert.IsNotNull (module, "#1");
1052 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1054 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1058 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1060 Assert.IsFalse (module.IsResource (), "#4");
1061 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1062 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1063 Assert.AreSame (module, assembly.ManifestModule, "#7");
1068 private class AssemblyResolveHandler
1070 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1072 _assemblyFile = assemblyFile;
1073 _assemblyName = assemblyName;
1075 AppDomain.CurrentDomain.AssemblyResolve +=
1076 new ResolveEventHandler (ResolveAssembly);
1079 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1081 if (args.Name == _assemblyName)
1082 return Assembly.LoadFrom (_assemblyFile);
1087 private readonly string _assemblyFile;
1088 private readonly string _assemblyName;
1091 protected internal class Bug328812_NestedFamORAssem { };
1094 public void bug328812 ()
1096 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1097 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1098 // Just a sanity check, in case the above passed for some other reason
1099 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1103 public void GetCustomAttributes_AttributeType_Null ()
1105 Assembly a = typeof (int).Assembly;
1107 a.GetCustomAttributes (null, false);
1109 } catch (ArgumentNullException ex) {
1110 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1111 Assert.IsNull (ex.InnerException, "#3");
1112 Assert.IsNotNull (ex.Message, "#4");
1113 Assert.IsNotNull (ex.ParamName, "#5");
1114 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1119 public void GetTypeWithEmptyStringShouldThrow ()
1122 typeof (string).Assembly.GetType ("");
1124 } catch (ArgumentException) {}
1129 public void DefinedTypes_Equality ()
1131 var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1132 var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1134 Assert.AreSame (x1, x2, "#1");
1139 public class TestDefinedTypes