update version number
[mcs.git] / class / corlib / Test / System.Reflection / AssemblyTest.cs
blob620057c24bfc931a3d43a970f80b7a9438cb69bd
1 //
2 // System.Reflection.Assembly Test Cases
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Philippe Lavoie (philippe.lavoie@cactus.ca)
7 // Sebastien Pouliot (sebastien@ximian.com)
8 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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;
33 using System;
34 using System.Configuration.Assemblies;
35 using System.Globalization;
36 using System.IO;
37 using System.Reflection;
38 #if !TARGET_JVM
39 using System.Reflection.Emit;
40 #endif
41 using System.Threading;
42 using System.Runtime.Serialization;
43 using System.Security;
45 namespace MonoTests.System.Reflection
47 [TestFixture]
48 public class AssemblyTest
50 static string TempFolder = Path.Combine (Path.GetTempPath (),
51 "MonoTests.System.Reflection.AssemblyTest");
53 [SetUp]
54 public void SetUp ()
56 while (Directory.Exists (TempFolder))
57 TempFolder = Path.Combine (TempFolder, "2");
58 Directory.CreateDirectory (TempFolder);
61 [TearDown]
62 public void TearDown ()
64 try {
65 // This throws an exception under MS.NET, since the directory contains loaded
66 // assemblies.
67 Directory.Delete (TempFolder, true);
68 } catch (Exception) {
72 [Test]
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");
81 [Test]
82 public void CreateInvalidInstance()
84 Type type = typeof (AssemblyTest);
85 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
86 Assert.IsNull (obj, "#03");
89 [Test] // bug #49114
90 #if NET_2_0
91 [Category ("NotWorking")]
92 [ExpectedException (typeof (ArgumentException))]
93 #else
94 [ExpectedException (typeof (TypeLoadException))]
95 #endif
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;
106 #if NET_2_0
107 try {
108 a.GetType (typeName, true, false);
109 Assert.Fail ("#A1");
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");
116 #else
117 try {
118 a.GetType (typeName, true, false);
119 Assert.Fail ("#A1");
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");
126 #endif
128 Type type = a.GetType (typeName, false);
129 Assert.IsNull (type, "#B1");
130 type = a.GetType (typeName, false, true);
131 Assert.IsNull (type, "#B2");
134 [Test]
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");
145 #endif
146 } else { // gnunit
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");
150 #endif
154 #if !TARGET_JVM // Reflection.Emit is not supported.
155 [Test]
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"));
170 try {
171 ass.GetModules ();
172 Assert.Fail ();
173 } catch (FileNotFoundException ex) {
174 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
177 #endif
179 #if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
180 #if NET_2_0
181 [Category ("NotWorking")]
182 #endif
183 [Test]
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");
195 #if NET_2_0
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);
203 #elif NET_1_1
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");
207 #endif
210 [Test]
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");
220 #if NET_4_0
221 Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
222 #else
223 Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
224 #endif
226 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
227 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
229 #endif
231 [Test]
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)");
238 [Test]
239 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
240 public void GetFile_Null ()
242 try {
243 Assembly.GetExecutingAssembly ().GetFile (null);
244 Assert.Fail ("#1");
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");
253 [Test]
254 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
255 public void GetFile_Empty ()
257 try {
258 Assembly.GetExecutingAssembly ().GetFile (
259 String.Empty);
260 Assert.Fail ("#1");
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");
269 [Test]
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)");
282 [Test]
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)");
295 [Test]
296 public void GetManifestResourceStream_Name_Empty ()
298 Assembly corlib = typeof (int).Assembly;
300 try {
301 corlib.GetManifestResourceStream (string.Empty);
302 Assert.Fail ("#A1");
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);
312 try {
313 corlib.GetManifestResourceStream ((Type) null, string.Empty);
314 Assert.Fail ("#B1");
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");
323 [Test]
324 public void GetManifestResourceStream_Name_Null ()
326 Assembly corlib = typeof (int).Assembly;
328 try {
329 corlib.GetManifestResourceStream ((string) null);
330 Assert.Fail ("#A1");
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);
339 try {
340 corlib.GetManifestResourceStream ((Type) null, (string) null);
341 Assert.Fail ("#B1");
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");
351 [Test]
352 public void IsDefined_AttributeType_Null ()
354 Assembly corlib = typeof (int).Assembly;
356 try {
357 corlib.IsDefined ((Type) null, false);
358 Assert.Fail ("#1");
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");
368 [Test] // bug #78517
369 #if ONLY_1_1
370 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
371 #endif
372 public void LoadFrom_Empty_Assembly ()
374 string tempFile = Path.GetTempFileName ();
376 try {
377 Assembly.LoadFrom (tempFile);
378 Assert.Fail ("#1");
379 } catch (BadImageFormatException ex) {
380 Assert.IsNull (ex.InnerException, "#2");
381 } finally {
382 File.Delete (tempFile);
386 [Test] // bug #78517
387 public void LoadFrom_Invalid_Assembly ()
389 string tempFile = Path.GetTempFileName ();
390 using (StreamWriter sw = File.CreateText (tempFile)) {
391 sw.WriteLine ("foo");
392 sw.Close ();
395 try {
396 Assembly.LoadFrom (tempFile);
397 Assert.Fail ("#1");
398 } catch (BadImageFormatException ex) {
399 Assert.IsNull (ex.InnerException, "#2");
400 } finally {
401 File.Delete (tempFile);
405 [Test]
406 public void LoadFrom_NonExisting_Assembly ()
408 string tempFile = Path.GetTempFileName ();
409 File.Delete (tempFile);
411 try {
412 Assembly.LoadFrom (tempFile);
413 Assert.Fail ("#1");
414 } catch (FileNotFoundException ex) {
415 Assert.IsNull (ex.InnerException, "#2");
416 } finally {
417 File.Delete (tempFile);
421 [Test]
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)
428 return;
429 Assert.Fail ("Was not able to load any corlib test");
432 #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
433 [Test]
434 public void GetObjectData_Info_Null ()
436 Assembly corlib = typeof (int).Assembly;
437 try {
438 corlib.GetObjectData (null, new StreamingContext (
439 StreamingContextStates.All));
440 Assert.Fail ("#1");
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");
449 #endif // TARGET_JVM
451 [Test]
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.
472 [Test]
473 public void Location_Empty() {
474 string assemblyFileName = Path.Combine (
475 Path.GetTempPath (), "AssemblyLocation.dll");
477 try {
478 AssemblyName assemblyName = new AssemblyName ();
479 assemblyName.Name = "AssemblyLocation";
481 AssemblyBuilder ab = AppDomain.CurrentDomain
482 .DefineDynamicAssembly (assemblyName,
483 AssemblyBuilderAccess.Save,
484 Path.GetTempPath (),
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);
493 fs.Close ();
495 } finally {
496 File.Delete (assemblyFileName);
500 [Test]
501 [Category ("NotWorking")]
502 public void bug78464 ()
504 string assemblyFileName = Path.Combine (
505 Path.GetTempPath (), "bug78464.dll");
507 try {
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);
511 try {
512 crossDomainTester.bug78464 (assemblyFileName);
513 } finally {
514 AppDomain.Unload (testDomain);
516 } finally {
517 File.Delete (assemblyFileName);
521 [Test]
522 public void bug78465 ()
524 string assemblyFileName = Path.Combine (
525 Path.GetTempPath (), "bug78465.dll");
527 try {
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");
543 fs.Close ();
546 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
547 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
548 try {
549 crossDomainTester.bug78465 (assemblyFileName);
550 } finally {
551 AppDomain.Unload (testDomain);
553 } finally {
554 File.Delete (assemblyFileName);
558 [Test]
559 public void bug78468 ()
561 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
562 "bug78468a.dll");
563 string resourceFileName = Path.Combine (Path.GetTempPath (),
564 "readme.txt");
566 using (StreamWriter sw = File.CreateText (resourceFileName)) {
567 sw.WriteLine ("FOO");
568 sw.Close ();
571 try {
572 AssemblyName assemblyName = new AssemblyName ();
573 assemblyName.Name = "bug78468a";
575 AssemblyBuilder ab = AppDomain.CurrentDomain
576 .DefineDynamicAssembly (assemblyName,
577 AssemblyBuilderAccess.Save,
578 Path.GetTempPath (),
579 AppDomain.CurrentDomain.Evidence);
580 ab.AddResourceFile ("read", "readme.txt");
581 ab.Save (Path.GetFileName (assemblyFileNameA));
583 Assembly assembly;
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);
589 fs.Close ();
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");
602 #if NET_2_0
603 try {
604 assembly.GetManifestResourceStream ("read");
605 Assert.Fail ("#A9");
606 } catch (FileNotFoundException) {
608 #else
609 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
610 #endif
611 try {
612 assembly.GetFile ("readme.txt");
613 Assert.Fail ("#A10");
614 } catch (FileNotFoundException) {
617 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
618 "bug78468b.dll");
620 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
621 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
622 try {
623 crossDomainTester.bug78468 (assemblyFileNameB);
624 } finally {
625 AppDomain.Unload (testDomain);
626 File.Delete (assemblyFileNameB);
628 } finally {
629 File.Delete (assemblyFileNameA);
630 File.Delete (resourceFileName);
634 #if NET_2_0
635 [Test]
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);
645 [Test]
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);
656 [Test]
657 [ExpectedException (typeof (ArgumentException))]
658 public void CreateInstanceOnRefOnly ()
660 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
661 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
663 #endif
665 [Test]
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);
680 try {
681 CrossDomainTester cdt = CreateCrossDomainTester (ad);
683 // PART A
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");
703 // PART B
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");
724 // PART C
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");
745 // PART D
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");
765 } finally {
766 AppDomain.Unload (ad);
767 if (Directory.Exists (tempDir))
768 Directory.Delete (tempDir, true);
772 [Test] // bug #79712
773 #if NET_2_0
774 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
775 #else
776 [Category ("NotWorking")]
777 #endif
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);
787 try {
788 CrossDomainTester cdt = CreateCrossDomainTester (ad);
790 // PART A
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;
800 #if NET_2_0
801 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
802 #else
803 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
804 #endif
806 // PART B
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");
816 #if NET_2_0
817 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
818 #else
819 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
820 #endif
821 } finally {
822 AppDomain.Unload (ad);
823 if (Directory.Exists (tempDir))
824 Directory.Delete (tempDir, true);
829 [Test] // bug #79715
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);
838 try {
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");
863 } finally {
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,
883 false,
884 BindingFlags.Public | BindingFlags.Instance,
885 null,
886 new object [] { ea.Location, ea.FullName },
887 CultureInfo.InvariantCulture,
888 null,
889 null);
892 return ad;
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)
925 try {
926 Assembly.Load (assemblyRef);
927 return true;
928 } catch {
929 return false;
933 public bool AssertLoad (string assemblyString)
935 try {
936 Assembly.Load (assemblyString);
937 return true;
938 } catch {
939 return false;
943 public bool AssertFileLoadException (AssemblyName assemblyRef)
945 try {
946 Assembly.Load (assemblyRef);
947 return false;
948 } catch (FileLoadException) {
949 return true;
953 public bool AssertFileNotFoundException (AssemblyName assemblyRef)
955 try {
956 Assembly.Load (assemblyRef);
957 return false;
958 } catch (FileNotFoundException) {
959 return true;
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));
975 Assembly assembly;
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);
981 fs.Close ();
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");
1030 s.Close ();
1031 s = assembly.GetFile ("readme.txt");
1032 Assert.IsNotNull (s, "#B10");
1033 s.Close ();
1037 [Test]
1038 public void bug79872 ()
1040 Random rnd = new Random ();
1041 string outdir;
1042 int tries = 0;
1044 retry:
1045 outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1046 if (Directory.Exists (outdir)) {
1047 try {
1048 Directory.Delete (outdir, true);
1049 } catch {
1050 if (++tries <= 100)
1051 goto retry;
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");
1065 a1.CreateType ();
1066 ab.Save (dllname);
1068 bool ok = true;
1069 try {
1070 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1071 } catch {
1072 ok = false;
1074 Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1076 ok = false;
1077 try {
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
1083 } catch {
1084 // swallow the rest
1086 Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1088 Directory.Delete (outdir, true);
1090 #endif // TARGET_JVM
1092 [Test]
1093 public void ManifestModule ()
1095 Assembly assembly = typeof (int).Assembly;
1096 Module module = assembly.ManifestModule;
1097 Assert.IsNotNull (module, "#1");
1099 #if NET_4_0
1100 Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1101 #else
1102 Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1103 #endif
1105 Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1106 Assert.IsFalse (module.IsResource (), "#4");
1107 Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1108 Assert.AreSame (module, assembly.GetModules () [0], "#6");
1109 Assert.AreSame (module, assembly.ManifestModule, "#7");
1113 [Serializable ()]
1114 private class AssemblyResolveHandler
1116 public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1118 _assemblyFile = assemblyFile;
1119 _assemblyName = assemblyName;
1121 AppDomain.CurrentDomain.AssemblyResolve +=
1122 new ResolveEventHandler (ResolveAssembly);
1125 private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1127 if (args.Name == _assemblyName)
1128 return Assembly.LoadFrom (_assemblyFile);
1130 return null;
1133 private readonly string _assemblyFile;
1134 private readonly string _assemblyName;
1137 protected internal class Bug328812_NestedFamORAssem { };
1139 [Test]
1140 public void bug328812 ()
1142 Assembly corlib_test = Assembly.GetExecutingAssembly ();
1143 Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1144 // Just a sanity check, in case the above passed for some other reason
1145 Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1148 [Test]
1149 public void GetCustomAttributes_AttributeType_Null ()
1151 Assembly a = typeof (int).Assembly;
1152 try {
1153 a.GetCustomAttributes (null, false);
1154 Assert.Fail ("#1");
1155 } catch (ArgumentNullException ex) {
1156 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1157 Assert.IsNull (ex.InnerException, "#3");
1158 Assert.IsNotNull (ex.Message, "#4");
1159 Assert.IsNotNull (ex.ParamName, "#5");
1160 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1164 [Test]
1165 public void GetTypeWithEmptyStringShouldThrow ()
1167 try {
1168 typeof (string).Assembly.GetType ("");
1169 Assert.Fail ("#1");
1170 } catch (ArgumentException) {}