499c6b840962a7bcd4cc843079baad5580cae9c4
[mono.git] / mcs / class / corlib / Test / System.Reflection / AssemblyTest.cs
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)
11 //
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.
30 //
31
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 !MONOTOUCH
39 using System.Reflection.Emit;
40 #endif
41 using System.Threading;
42 using System.Runtime.Serialization;
43 using System.Security;
44 using System.Linq;
45
46 namespace MonoTests.System.Reflection
47 {
48         [TestFixture]
49         public class AssemblyTest
50         {
51                 static string TempFolder = Path.Combine (Path.GetTempPath (),
52                         "MonoTests.System.Reflection.AssemblyTest");
53
54                 [SetUp]
55                 public void SetUp ()
56                 {
57                         while (Directory.Exists (TempFolder))
58                                 TempFolder = Path.Combine (TempFolder, "2");
59                         Directory.CreateDirectory (TempFolder);
60                 }
61
62                 [TearDown]
63                 public void TearDown ()
64                 {
65                         try {
66                                 // This throws an exception under MS.NET, since the directory contains loaded
67                                 // assemblies.
68                                 Directory.Delete (TempFolder, true);
69                         } catch (Exception) {
70                         }
71                 }
72
73                 [Test] 
74                 public void CreateInstance() 
75                 {
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");
80                 }
81
82                 [Test] 
83                 public void CreateInvalidInstance() 
84                 {
85                         Type type = typeof (AssemblyTest);
86                         Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
87                         Assert.IsNull (obj, "#03");
88                 }
89
90                 [Test] // bug #49114
91                 [Category ("NotWorking")]
92                 [ExpectedException (typeof (ArgumentException))]
93                 public void GetType_TypeName_Invalid () 
94                 {
95                         typeof (int).Assembly.GetType ("&blabla", true, true);
96                 }
97
98                 [Test] // bug #334203
99                 public void GetType_TypeName_AssemblyName ()
100                 {
101                         Assembly a = typeof (int).Assembly;
102                         string typeName = typeof (string).AssemblyQualifiedName;
103                         try {
104                                 a.GetType (typeName, true, false);
105                                 Assert.Fail ("#A1");
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");
111                         }
112
113                         Type type = a.GetType (typeName, false);
114                         Assert.IsNull (type, "#B1");
115                         type = a.GetType (typeName, false, true);
116                         Assert.IsNull (type, "#B2");
117                 }
118
119                 [Test]
120                 public void GetEntryAssembly ()
121                 {
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");
129                         } else { // gnunit
130                                 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
131                                 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
132                         }
133                 }
134
135 #if !MONOTOUCH // Reflection.Emit is not supported.
136                 [Test]
137                 public void GetModules_MissingFile ()
138                 {
139                         AssemblyName newName = new AssemblyName ();
140                         newName.Name = "AssemblyTest";
141
142                         AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
143
144                         ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
145
146                         ab.Save ("test_assembly.dll");
147
148                         File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
149
150                         Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
151                         try {
152                                 ass.GetModules ();
153                                 Assert.Fail ();
154                         } catch (FileNotFoundException ex) {
155                                 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
156                         }
157                 }
158 #endif
159
160                 [Category ("NotWorking")]
161                 [Test]
162                 public void Corlib () 
163                 {
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");
170
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);
180                 }
181
182                 [Test]
183                 public void Corlib_test ()
184                 {
185                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
186 #if MOBILE
187                         Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
188                         Assert.IsNull (corlib_test.Evidence, "Evidence");
189 #else
190                         Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
191                         Assert.IsNotNull (corlib_test.Evidence, "Evidence");
192 #endif
193                         Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
194
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");
199 #else
200                         Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
201 #endif
202
203                         Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
204                         Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
205                 }
206
207                 [Test]
208                 public void GetAssembly ()
209                 {
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)");
212                 }
213
214                 [Test]
215                 public void GetFile_Null ()
216                 {
217                         try {
218                                 Assembly.GetExecutingAssembly ().GetFile (null);
219                                 Assert.Fail ("#1");
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");
225                         }
226                 }
227
228                 [Test]
229                 public void GetFile_Empty ()
230                 {
231                         try {
232                                 Assembly.GetExecutingAssembly ().GetFile (
233                                         String.Empty);
234                                 Assert.Fail ("#1");
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");
240                         }
241                 }
242
243                 [Test]
244                 public void GetFiles_False ()
245                 {
246                         Assembly corlib = typeof (int).Assembly;
247                         FileStream[] fss = corlib.GetFiles ();
248                         Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
249
250                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
251                         fss = corlib_test.GetFiles ();
252                         Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
253                 }
254
255                 [Test]
256                 public void GetFiles_True ()
257                 {
258                         Assembly corlib = typeof (int).Assembly;
259                         FileStream[] fss = corlib.GetFiles ();
260                         Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
261
262                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
263                         fss = corlib_test.GetFiles ();
264                         Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
265                 }
266
267                 [Test]
268                 public void GetManifestResourceStream_Name_Empty ()
269                 {
270                         Assembly corlib = typeof (int).Assembly;
271
272                         try {
273                                 corlib.GetManifestResourceStream (string.Empty);
274                                 Assert.Fail ("#A1");
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");
280                         }
281
282                         corlib.GetManifestResourceStream (typeof (int), string.Empty);
283
284                         try {
285                                 corlib.GetManifestResourceStream ((Type) null, string.Empty);
286                                 Assert.Fail ("#B1");
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");
292                         }
293                 }
294
295                 [Test]
296                 public void GetManifestResourceStream_Name_Null ()
297                 {
298                         Assembly corlib = typeof (int).Assembly;
299
300                         try {
301                                 corlib.GetManifestResourceStream ((string) null);
302                                 Assert.Fail ("#A1");
303                         } catch (ArgumentNullException ex) {
304                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
305                                 Assert.IsNull (ex.InnerException, "#A3");
306                                 Assert.IsNotNull (ex.Message, "#A4");
307                         }
308
309                         corlib.GetManifestResourceStream (typeof (int), (string) null);
310
311                         try {
312                                 corlib.GetManifestResourceStream ((Type) null, (string) null);
313                                 Assert.Fail ("#B1");
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");
320                         }
321                 }
322
323                 [Test]
324                 public void IsDefined_AttributeType_Null ()
325                 {
326                         Assembly corlib = typeof (int).Assembly;
327
328                         try {
329                                 corlib.IsDefined ((Type) null, false);
330                                 Assert.Fail ("#1");
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");
337                         }
338                 }
339
340                 [Test] // bug #78517
341                 public void LoadFrom_Empty_Assembly ()
342                 {
343                         string tempFile = Path.GetTempFileName ();
344
345                         try {
346                                 Assembly.LoadFrom (tempFile);
347                                 Assert.Fail ("#1");
348                         } catch (BadImageFormatException ex) {
349                                 Assert.IsNull (ex.InnerException, "#2");
350                         } finally {
351                                 File.Delete (tempFile);
352                         }
353                 }
354
355                 [Test] // bug #78517
356                 public void LoadFrom_Invalid_Assembly ()
357                 {
358                         string tempFile = Path.GetTempFileName ();
359                         using (StreamWriter sw = File.CreateText (tempFile)) {
360                                 sw.WriteLine ("foo");
361                                 sw.Close ();
362                         }
363
364                         try {
365                                 Assembly.LoadFrom (tempFile);
366                                 Assert.Fail ("#1");
367                         } catch (BadImageFormatException ex) {
368                                 Assert.IsNull (ex.InnerException, "#2");
369                         } finally {
370                                 File.Delete (tempFile);
371                         }
372                 }
373
374                 [Test]
375                 public void LoadFrom_NonExisting_Assembly ()
376                 {
377                         string tempFile = Path.GetTempFileName ();
378                         File.Delete (tempFile);
379
380                         try {
381                                 Assembly.LoadFrom (tempFile);
382                                 Assert.Fail ("#1");
383                         } catch (FileNotFoundException ex) {
384                                 Assert.IsNull (ex.InnerException, "#2");
385                         } finally {
386                                 File.Delete (tempFile);
387                         }
388                 }
389
390                 [Test]
391                 public void LoadWithPartialName ()
392                 {
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" };
394
395                         foreach (string s in names)
396                                 if (Assembly.LoadWithPartialName (s) != null)
397                                         return;
398                         Assert.Fail ("Was not able to load any corlib test");
399                 }
400
401                 [Test]
402                 public void GetObjectData_Info_Null ()
403                 {
404                         Assembly corlib = typeof (int).Assembly;
405                         try {
406                                 corlib.GetObjectData (null, new StreamingContext (
407                                         StreamingContextStates.All));
408                                 Assert.Fail ("#1");
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");
415                         }
416                 }
417
418                 [Test]
419                 public void GetReferencedAssemblies ()
420                 {
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");
435                         }
436                 }
437
438 #if !MONOTOUCH // Reflection.Emit is not supported.
439                 [Test]
440                 public void Location_Empty() {
441                         string assemblyFileName = Path.Combine (
442                                 Path.GetTempPath (), "AssemblyLocation.dll");
443
444                         try {
445                                 AssemblyName assemblyName = new AssemblyName ();
446                                 assemblyName.Name = "AssemblyLocation";
447
448                                 AssemblyBuilder ab = AppDomain.CurrentDomain
449                                         .DefineDynamicAssembly (assemblyName,
450                                         AssemblyBuilderAccess.Save,
451                                         Path.GetTempPath (),
452                                         AppDomain.CurrentDomain.Evidence);
453                                 ab.Save (Path.GetFileName (assemblyFileName));
454
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);
460                                         fs.Close ();
461                                 }
462                         } finally {
463                                 File.Delete (assemblyFileName);
464                         }
465                 }
466
467                 [Test]
468                 [Category ("NotWorking")]
469                 public void bug78464 ()
470                 {
471                         string assemblyFileName = Path.Combine (
472                                 Path.GetTempPath (), "bug78464.dll");
473
474                         try {
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);
478                                 try {
479                                         crossDomainTester.bug78464 (assemblyFileName);
480                                 } finally {
481                                         AppDomain.Unload (testDomain);
482                                 }
483                         } finally {
484                                 File.Delete (assemblyFileName);
485                         }
486                 }
487
488                 [Test]
489                 public void bug78465 ()
490                 {
491                         string assemblyFileName = Path.Combine (
492                                 Path.GetTempPath (), "bug78465.dll");
493
494                         try {
495                                 AssemblyName assemblyName = new AssemblyName ();
496                                 assemblyName.Name = "bug78465";
497
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));
504
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");
510                                         fs.Close ();
511                                 }
512
513                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
514                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
515                                 try {
516                                         crossDomainTester.bug78465 (assemblyFileName);
517                                 } finally {
518                                         AppDomain.Unload (testDomain);
519                                 }
520                         } finally {
521                                 File.Delete (assemblyFileName);
522                         }
523                 }
524
525                 [Test]
526                 public void bug78468 ()
527                 {
528                         string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
529                                 "bug78468a.dll");
530                         string resourceFileName = Path.Combine (Path.GetTempPath (),
531                                 "readme.txt");
532
533                         using (StreamWriter sw = File.CreateText (resourceFileName)) {
534                                 sw.WriteLine ("FOO");
535                                 sw.Close ();
536                         }
537
538                         try {
539                                 AssemblyName assemblyName = new AssemblyName ();
540                                 assemblyName.Name = "bug78468a";
541
542                                 AssemblyBuilder ab = AppDomain.CurrentDomain
543                                         .DefineDynamicAssembly (assemblyName,
544                                         AssemblyBuilderAccess.Save,
545                                         Path.GetTempPath (),
546                                         AppDomain.CurrentDomain.Evidence);
547                                 ab.AddResourceFile ("read", "readme.txt");
548                                 ab.Save (Path.GetFileName (assemblyFileNameA));
549
550                                 Assembly assembly;
551
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);
556                                         fs.Close ();
557                                 }
558
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");
569                                 try {
570                                         assembly.GetManifestResourceStream ("read");
571                                         Assert.Fail ("#A9");
572                                 } catch (FileNotFoundException) {
573                                 }
574                                 try {
575                                         assembly.GetFile ("readme.txt");
576                                         Assert.Fail ("#A10");
577                                 } catch (FileNotFoundException) {
578                                 }
579
580                                 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
581                                         "bug78468b.dll");
582
583                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
584                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
585                                 try {
586                                         crossDomainTester.bug78468 (assemblyFileNameB);
587                                 } finally {
588                                         AppDomain.Unload (testDomain);
589                                         File.Delete (assemblyFileNameB);
590                                 }
591                         } finally {
592                                 File.Delete (assemblyFileNameA);
593                                 File.Delete (resourceFileName);
594                         }
595                 }
596
597                 [Test]
598                 [Category ("NotWorking")]
599                 public void ReflectionOnlyLoad ()
600                 {
601                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
602                         
603                         Assert.IsNotNull (assembly);
604                         Assert.IsTrue (assembly.ReflectionOnly);
605                 }
606
607                 [Test]
608                 public void ReflectionOnlyLoadFrom ()
609                 {
610                         string loc = typeof (AssemblyTest).Assembly.Location;
611                         string filename = Path.GetFileName (loc);
612                         Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
613
614                         Assert.IsNotNull (assembly);
615                         Assert.IsTrue (assembly.ReflectionOnly);
616                 }
617
618                 [Test]
619                 [ExpectedException (typeof (ArgumentException))]
620                 public void CreateInstanceOnRefOnly ()
621                 {
622                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
623                         assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
624                 }
625
626                 [Test]
627                 [Category ("NotWorking")] // patch for bug #79720 must be committed first
628                 public void Load_Culture ()
629                 {
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);
638
639
640                         AppDomain ad = CreateTestDomain (tempDir, true);
641                         try {
642                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
643
644                                 // PART A
645
646                                 AssemblyName aname = new AssemblyName ();
647                                 aname.Name = "culturea";
648                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
649
650                                 aname = new AssemblyName ();
651                                 aname.Name = "culturea";
652                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
653
654                                 aname = new AssemblyName ();
655                                 aname.Name = "culturea";
656                                 aname.CultureInfo = new CultureInfo ("nl-BE");
657                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
658
659                                 aname = new AssemblyName ();
660                                 aname.Name = "culturea";
661                                 aname.CultureInfo = CultureInfo.InvariantCulture;
662                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
663
664                                 // PART B
665
666                                 aname = new AssemblyName ();
667                                 aname.Name = "cultureb";
668                                 aname.CultureInfo = new CultureInfo ("nl-BE");
669                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
670
671                                 aname = new AssemblyName ();
672                                 aname.Name = "cultureb";
673                                 aname.CultureInfo = new CultureInfo ("nl-BE");
674                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
675
676                                 aname = new AssemblyName ();
677                                 aname.Name = "cultureb";
678                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
679
680                                 aname = new AssemblyName ();
681                                 aname.Name = "cultureb";
682                                 aname.CultureInfo = new CultureInfo ("en-US");
683                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
684
685                                 // PART C
686
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"));
691
692                                 aname = new AssemblyName ();
693                                 aname.Name = "culturec";
694                                 aname.CultureInfo = new CultureInfo ("nl-BE");
695                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
696
697                                 aname = new AssemblyName ();
698                                 aname.Name = "culturec";
699                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
700
701                                 aname = new AssemblyName ();
702                                 aname.Name = "culturec";
703                                 aname.CultureInfo = CultureInfo.InvariantCulture;
704                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
705
706                                 // PART D
707
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"));
712
713                                 aname = new AssemblyName ();
714                                 aname.Name = "cultured";
715                                 aname.CultureInfo = new CultureInfo ("nl-BE");
716                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
717
718                                 aname = new AssemblyName ();
719                                 aname.Name = "cultured";
720                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
721
722                                 aname = new AssemblyName ();
723                                 aname.Name = "cultured";
724                                 aname.CultureInfo = CultureInfo.InvariantCulture;
725                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
726                         } finally {
727                                 AppDomain.Unload (ad);
728                                 if (Directory.Exists (tempDir))
729                                         Directory.Delete (tempDir, true);
730                         }
731                 }
732
733                 [Test] // bug #79712
734                 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
735                 public void Load_Culture_Mismatch ()
736                 {
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);
742
743                         AppDomain ad = CreateTestDomain (tempDir, true);
744                         try {
745                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
746
747                                 // PART A
748
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"));
753
754                                 aname = new AssemblyName ();
755                                 aname.Name = "bug79712a";
756                                 aname.CultureInfo = CultureInfo.InvariantCulture;
757                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
758
759                                 // PART B
760
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"));
765
766                                 aname = new AssemblyName ();
767                                 aname.Name = "bug79712b";
768                                 aname.CultureInfo = new CultureInfo ("en-US");
769                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
770                         } finally {
771                                 AppDomain.Unload (ad);
772                                 if (Directory.Exists (tempDir))
773                                         Directory.Delete (tempDir, true);
774                         }
775                 }
776
777
778                 [Test] // bug #79715
779                 public void Load_PartialVersion ()
780                 {
781                         string tempDir = Path.Combine (Path.GetTempPath (),
782                                 "MonoTests.System.Reflection.AssemblyTest");
783                         if (!Directory.Exists (tempDir))
784                                 Directory.CreateDirectory (tempDir);
785
786                         AppDomain ad = CreateTestDomain (tempDir, true);
787                         try {
788                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
789
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"));
794
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");
800
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");
806
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");
812                         } finally {
813                                 AppDomain.Unload (ad);
814                                 if (Directory.Exists (tempDir))
815                                         Directory.Delete (tempDir, true);
816                         }
817                 }
818
819                 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
820                 {
821                         AppDomainSetup setup = new AppDomainSetup ();
822                         setup.ApplicationBase = baseDirectory;
823                         setup.ApplicationName = "testdomain";
824
825                         AppDomain ad = AppDomain.CreateDomain ("testdomain", 
826                                 AppDomain.CurrentDomain.Evidence, setup);
827
828                         if (assemblyResolver) {
829                                 Assembly ea = Assembly.GetExecutingAssembly ();
830                                 ad.CreateInstanceFrom (ea.CodeBase,
831                                         typeof (AssemblyResolveHandler).FullName,
832                                         false,
833                                         BindingFlags.Public | BindingFlags.Instance,
834                                         null,
835                                         new object [] { ea.Location, ea.FullName },
836                                         CultureInfo.InvariantCulture,
837                                         null,
838                                         null);
839                         }
840
841                         return ad;
842                 }
843
844                 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
845                 {
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);
851                 }
852
853                 private class CrossDomainTester : MarshalByRefObject
854                 {
855                         public void GenerateAssembly (AssemblyName aname, string path)
856                         {
857                                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
858                                         aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
859                                 ab.Save (Path.GetFileName (path));
860                         }
861
862                         public void Load (AssemblyName assemblyRef)
863                         {
864                                 Assembly.Load (assemblyRef);
865                         }
866
867                         public void LoadFrom (string assemblyFile)
868                         {
869                                 Assembly.LoadFrom (assemblyFile);
870                         }
871
872                         public bool AssertLoad (AssemblyName assemblyRef)
873                         {
874                                 try {
875                                         Assembly.Load (assemblyRef);
876                                         return true;
877                                 } catch {
878                                         return false;
879                                 }
880                         }
881
882                         public bool AssertLoad (string assemblyString)
883                         {
884                                 try {
885                                         Assembly.Load (assemblyString);
886                                         return true;
887                                 } catch {
888                                         return false;
889                                 }
890                         }
891
892                         public bool AssertFileLoadException (AssemblyName assemblyRef)
893                         {
894                                 try {
895                                         Assembly.Load (assemblyRef);
896                                         return false;
897                                 } catch (FileLoadException) {
898                                         return true;
899                                 }
900                         }
901
902                         public bool AssertFileNotFoundException (AssemblyName assemblyRef)
903                         {
904                                 try {
905                                         Assembly.Load (assemblyRef);
906                                         return false;
907                                 } catch (FileNotFoundException) {
908                                         return true;
909                                 }
910                         }
911
912                         public void bug78464 (string assemblyFileName)
913                         {
914                                 AssemblyName assemblyName = new AssemblyName ();
915                                 assemblyName.Name = "bug78464";
916
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));
923
924                                 Assembly assembly;
925
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);
930                                         fs.Close ();
931                                 }
932
933                                 Assert.AreEqual (string.Empty, assembly.Location, "#1");
934
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");
941                         }
942
943                         public void bug78465 (string assemblyFileName)
944                         {
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");
951                         }
952
953                         public void bug78468 (string assemblyFileName)
954                         {
955                                 AssemblyName assemblyName = new AssemblyName ();
956                                 assemblyName.Name = "bug78468b";
957
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));
965
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");
979                                 s.Close ();
980                                 s = assembly.GetFile ("readme.txt");
981                                 Assert.IsNotNull (s, "#B10");
982                                 s.Close ();
983                         }
984                 }
985
986                 [Test]
987                 public void bug79872 ()
988                 {
989                         Random rnd = new Random ();
990                         string outdir;
991                         int tries = 0;
992
993                 retry:
994                         outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
995                         if (Directory.Exists (outdir)) {
996                                 try {
997                                         Directory.Delete (outdir, true);
998                                 } catch {
999                                         if (++tries <= 100)
1000                                                 goto retry;
1001                                 }
1002                         }
1003
1004                         Directory.CreateDirectory (outdir);
1005
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");
1014                         a1.CreateType ();
1015                         ab.Save (dllname);
1016
1017                         bool ok = true;
1018                         try {
1019                                 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1020                         } catch {
1021                                 ok = false;
1022                         }
1023                         Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1024
1025                         ok = false;
1026                         try {
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
1032                         } catch {
1033                                 // swallow the rest
1034                         }
1035                         Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1036
1037                         Directory.Delete (outdir, true);
1038                 }
1039 #endif
1040
1041                 [Test]
1042                 public void ManifestModule ()
1043                 {
1044                         Assembly assembly = typeof (int).Assembly;
1045                         Module module = assembly.ManifestModule;
1046                         Assert.IsNotNull (module, "#1");
1047
1048 #if NET_4_0
1049                         Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1050 #else
1051                         Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1052 #endif
1053
1054 #if !MONOTOUCH
1055                         Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1056 #endif
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");
1061                 }
1062
1063
1064                 [Serializable ()]
1065                 private class AssemblyResolveHandler
1066                 {
1067                         public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1068                         {
1069                                 _assemblyFile = assemblyFile;
1070                                 _assemblyName = assemblyName;
1071
1072                                 AppDomain.CurrentDomain.AssemblyResolve +=
1073                                         new ResolveEventHandler (ResolveAssembly);
1074                         }
1075
1076                         private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1077                         {
1078                                 if (args.Name == _assemblyName)
1079                                         return Assembly.LoadFrom (_assemblyFile);
1080
1081                                 return null;
1082                         }
1083
1084                         private readonly string _assemblyFile;
1085                         private readonly string _assemblyName;
1086                 }
1087
1088                 protected internal class Bug328812_NestedFamORAssem { };
1089
1090                 [Test]
1091                 public void bug328812 ()
1092                 {
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"));
1097                 }
1098                 
1099                 [Test]
1100                 public void GetCustomAttributes_AttributeType_Null ()
1101                 {
1102                         Assembly a = typeof (int).Assembly;
1103                         try {
1104                                 a.GetCustomAttributes (null, false);
1105                                 Assert.Fail ("#1");
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");
1112                         }
1113                 }
1114
1115                 [Test]
1116                 public void GetTypeWithEmptyStringShouldThrow ()
1117                 {
1118                         try {
1119                                 typeof (string).Assembly.GetType ("");
1120                                 Assert.Fail ("#1");
1121                         } catch (ArgumentException) {}
1122                 }
1123
1124 #if NET_4_5
1125                 [Test]
1126                 public void DefinedTypes_Equality ()
1127                 {
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 ();
1130
1131                         Assert.AreSame (x1, x2, "#1");
1132                 }
1133 #endif
1134         }
1135
1136         public class TestDefinedTypes
1137         {
1138         }
1139 }