Merge pull request #1921 from mattzink/master
[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 using System.Resources;
46
47 namespace MonoTests.System.Reflection
48 {
49         [TestFixture]
50         public class AssemblyTest
51         {
52                 static string TempFolder = Path.Combine (Path.GetTempPath (),
53                         "MonoTests.System.Reflection.AssemblyTest");
54
55                 [SetUp]
56                 public void SetUp ()
57                 {
58                         while (Directory.Exists (TempFolder))
59                                 TempFolder = Path.Combine (TempFolder, "2");
60                         Directory.CreateDirectory (TempFolder);
61                 }
62
63                 [TearDown]
64                 public void TearDown ()
65                 {
66                         try {
67                                 // This throws an exception under MS.NET, since the directory contains loaded
68                                 // assemblies.
69                                 Directory.Delete (TempFolder, true);
70                         } catch (Exception) {
71                         }
72                 }
73
74                 [Test] 
75                 public void CreateInstance() 
76                 {
77                         Type type = typeof (AssemblyTest);
78                         Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
79                         Assert.IsNotNull (obj, "#01");
80                         Assert.AreEqual (GetType (), obj.GetType (), "#02");
81                 }
82
83                 [Test] 
84                 public void CreateInvalidInstance() 
85                 {
86                         Type type = typeof (AssemblyTest);
87                         Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
88                         Assert.IsNull (obj, "#03");
89                 }
90
91                 [Test] // bug #49114
92                 [Category ("NotWorking")]
93                 [ExpectedException (typeof (ArgumentException))]
94                 public void GetType_TypeName_Invalid () 
95                 {
96                         typeof (int).Assembly.GetType ("&blabla", true, true);
97                 }
98
99                 [Test] // bug #334203
100                 public void GetType_TypeName_AssemblyName ()
101                 {
102                         Assembly a = typeof (int).Assembly;
103                         string typeName = typeof (string).AssemblyQualifiedName;
104                         try {
105                                 a.GetType (typeName, true, false);
106                                 Assert.Fail ("#A1");
107                         } catch (ArgumentException ex) {
108                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
109                                 Assert.IsNull (ex.InnerException, "#A3");
110                                 Assert.IsNotNull (ex.Message, "#A4");
111                                 Assert.IsNull (ex.ParamName, "#A5");
112                         }
113
114                         Type type = a.GetType (typeName, false);
115                         Assert.IsNull (type, "#B1");
116                         type = a.GetType (typeName, false, true);
117                         Assert.IsNull (type, "#B2");
118                 }
119
120                 [Test]
121                 public void GetEntryAssembly ()
122                 {
123                         // note: only available in default appdomain
124                         // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
125                         // Not sure we should emulate this behavior.
126 #if !MONODROID
127                         string fname = AppDomain.CurrentDomain.FriendlyName;
128                         if (fname.EndsWith (".dll")) { // nunit-console
129                                 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
130                                 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
131                         } else { // gnunit
132                                 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
133                                 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
134                         }
135 #else
136                         Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
137                         Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
138 #endif
139                 }
140
141 #if !MONOTOUCH // Reflection.Emit is not supported.
142                 [Test]
143                 [Category("AndroidNotWorking")] // Missing Mono.CompilerServices.SymbolWriter
144                 public void GetModules_MissingFile ()
145                 {
146                         AssemblyName newName = new AssemblyName ();
147                         newName.Name = "AssemblyTest";
148
149                         AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
150
151                         ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
152
153                         ab.Save ("test_assembly.dll");
154
155                         File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
156
157                         Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
158                         try {
159                                 ass.GetModules ();
160                                 Assert.Fail ();
161                         } catch (FileNotFoundException ex) {
162                                 Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
163                         }
164                 }
165 #endif
166
167                 [Category ("NotWorking")]
168                 [Test]
169                 public void Corlib () 
170                 {
171                         Assembly corlib = typeof (int).Assembly;
172                         Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
173                         Assert.IsNull (corlib.EntryPoint, "EntryPoint");
174                         Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
175                         Assert.IsNotNull (corlib.Evidence, "Evidence");
176                         Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
177
178                         // corlib doesn't reference anything
179                         Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
180                         Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
181                         // not really "true" but it's even more trusted so...
182                         Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
183                         Assert.AreEqual (0, corlib.HostContext, "HostContext");
184                         Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
185                         Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
186                         Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
187                 }
188
189                 [Test]
190                 public void Corlib_test ()
191                 {
192                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
193 #if MONODROID
194                         Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
195                         Assert.IsNull (corlib_test.Evidence, "Evidence");
196 #elif MOBILE
197                         Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
198                         Assert.IsNull (corlib_test.Evidence, "Evidence");
199 #else
200                         Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
201                         Assert.IsNotNull (corlib_test.Evidence, "Evidence");
202 #endif
203                         Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
204
205                         Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
206                         Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
207                         Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
208
209                         Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
210                         Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
211                 }
212
213                 [Test]
214                 public void GetAssembly ()
215                 {
216                         Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
217                         Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
218                 }
219
220                 [Test]
221                 public void GetFile_Null ()
222                 {
223                         try {
224                                 Assembly.GetExecutingAssembly ().GetFile (null);
225                                 Assert.Fail ("#1");
226                         } catch (ArgumentNullException ex) {
227                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
228                                 Assert.IsNull (ex.InnerException, "#3");
229                                 Assert.IsNotNull (ex.Message, "#4");
230                                 Assert.IsNull (ex.ParamName, "#5");
231                         }
232                 }
233
234                 [Test]
235                 public void GetFile_Empty ()
236                 {
237                         try {
238                                 Assembly.GetExecutingAssembly ().GetFile (
239                                         String.Empty);
240                                 Assert.Fail ("#1");
241                         } catch (ArgumentException ex) {
242                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
243                                 Assert.IsNull (ex.InnerException, "#3");
244                                 Assert.IsNotNull (ex.Message, "#4");
245                                 Assert.IsNull (ex.ParamName, "#5");
246                         }
247                 }
248
249                 [Test]
250                 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
251                 public void GetFiles_False ()
252                 {
253                         Assembly corlib = typeof (int).Assembly;
254                         FileStream[] fss = corlib.GetFiles ();
255                         Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
256
257                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
258                         fss = corlib_test.GetFiles ();
259                         Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
260                 }
261
262                 [Test]
263                 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
264                 public void GetFiles_True ()
265                 {
266                         Assembly corlib = typeof (int).Assembly;
267                         FileStream[] fss = corlib.GetFiles ();
268                         Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
269
270                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
271                         fss = corlib_test.GetFiles ();
272                         Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
273                 }
274
275                 [Test]
276                 public void GetManifestResourceStream_Name_Empty ()
277                 {
278                         Assembly corlib = typeof (int).Assembly;
279
280                         try {
281                                 corlib.GetManifestResourceStream (string.Empty);
282                                 Assert.Fail ("#A1");
283                         } catch (ArgumentException ex) {
284                                 // String cannot have zero length
285                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
286                                 Assert.IsNull (ex.InnerException, "#A3");
287                                 Assert.IsNotNull (ex.Message, "#A4");
288                         }
289
290                         corlib.GetManifestResourceStream (typeof (int), string.Empty);
291
292                         try {
293                                 corlib.GetManifestResourceStream ((Type) null, string.Empty);
294                                 Assert.Fail ("#B1");
295                         } catch (ArgumentException ex) {
296                                 // String cannot have zero length
297                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
298                                 Assert.IsNull (ex.InnerException, "#B3");
299                                 Assert.IsNotNull (ex.Message, "#B4");
300                         }
301                 }
302
303                 [Test]
304                 public void GetManifestResourceStream_Name_Null ()
305                 {
306                         Assembly corlib = typeof (int).Assembly;
307
308                         try {
309                                 corlib.GetManifestResourceStream ((string) null);
310                                 Assert.Fail ("#A1");
311                         } catch (ArgumentNullException ex) {
312                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
313                                 Assert.IsNull (ex.InnerException, "#A3");
314                                 Assert.IsNotNull (ex.Message, "#A4");
315                         }
316
317                         corlib.GetManifestResourceStream (typeof (int), (string) null);
318
319                         try {
320                                 corlib.GetManifestResourceStream ((Type) null, (string) null);
321                                 Assert.Fail ("#B1");
322                         } catch (ArgumentNullException ex) {
323                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
324                                 Assert.IsNull (ex.InnerException, "#B3");
325                                 Assert.IsNotNull (ex.Message, "#B4");
326                                 Assert.IsNotNull (ex.ParamName, "#B5");
327                                 Assert.AreEqual ("type", ex.ParamName, "#B6");
328                         }
329                 }
330
331                 [Test]
332                 public void IsDefined_AttributeType_Null ()
333                 {
334                         Assembly corlib = typeof (int).Assembly;
335
336                         try {
337                                 corlib.IsDefined ((Type) null, false);
338                                 Assert.Fail ("#1");
339                         } catch (ArgumentNullException ex) {
340                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
341                                 Assert.IsNull (ex.InnerException, "#3");
342                                 Assert.IsNotNull (ex.Message, "#4");
343                                 Assert.IsNotNull (ex.ParamName, "#5");
344                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
345                         }
346                 }
347
348                 [Test] // bug #78517
349                 public void LoadFrom_Empty_Assembly ()
350                 {
351                         string tempFile = Path.GetTempFileName ();
352
353                         try {
354                                 Assembly.LoadFrom (tempFile);
355                                 Assert.Fail ("#1");
356                         } catch (BadImageFormatException ex) {
357                                 Assert.IsNull (ex.InnerException, "#2");
358                         } finally {
359                                 File.Delete (tempFile);
360                         }
361                 }
362
363                 [Test] // bug #78517
364                 public void LoadFrom_Invalid_Assembly ()
365                 {
366                         string tempFile = Path.GetTempFileName ();
367                         using (StreamWriter sw = File.CreateText (tempFile)) {
368                                 sw.WriteLine ("foo");
369                                 sw.Close ();
370                         }
371
372                         try {
373                                 Assembly.LoadFrom (tempFile);
374                                 Assert.Fail ("#1");
375                         } catch (BadImageFormatException ex) {
376                                 Assert.IsNull (ex.InnerException, "#2");
377                         } finally {
378                                 File.Delete (tempFile);
379                         }
380                 }
381
382                 [Test]
383                 public void LoadFrom_NonExisting_Assembly ()
384                 {
385                         string tempFile = Path.GetTempFileName ();
386                         File.Delete (tempFile);
387
388                         try {
389                                 Assembly.LoadFrom (tempFile);
390                                 Assert.Fail ("#1");
391                         } catch (FileNotFoundException ex) {
392                                 Assert.IsNull (ex.InnerException, "#2");
393                         } finally {
394                                 File.Delete (tempFile);
395                         }
396                 }
397
398                 [Test]
399                 public void LoadWithPartialName ()
400                 {
401                         string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_test_net_4_0", "corlib_test_net_4_5", "corlib_test_net_4_x", "corlib_plattest", "mscorlibtests", "BclTests" };
402
403                         foreach (string s in names)
404                                 if (Assembly.LoadWithPartialName (s) != null)
405                                         return;
406                         Assert.Fail ("Was not able to load any corlib test");
407                 }
408
409                 [Test]
410                 public void GetObjectData_Info_Null ()
411                 {
412                         Assembly corlib = typeof (int).Assembly;
413                         try {
414                                 corlib.GetObjectData (null, new StreamingContext (
415                                         StreamingContextStates.All));
416                                 Assert.Fail ("#1");
417                         } catch (ArgumentNullException ex) {
418                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
419                                 Assert.IsNull (ex.InnerException, "#3");
420                                 Assert.IsNotNull (ex.Message, "#4");
421                                 Assert.IsNotNull (ex.ParamName, "#5");
422                                 Assert.AreEqual ("info", ex.ParamName, "#6");
423                         }
424                 }
425
426                 [Test]
427                 public void GetReferencedAssemblies ()
428                 {
429                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
430                         AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
431                         foreach (AssemblyName an in names) {
432                                 Assert.IsNull (an.CodeBase, "CodeBase");
433                                 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
434                                 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
435                                 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
436                                 Assert.IsNotNull (an.FullName, "FullName");
437                                 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
438                                 Assert.IsNull (an.KeyPair, "KeyPair");
439                                 Assert.IsNotNull (an.Name, "Name");
440                                 Assert.IsNotNull (an.Version, "Version");
441                                 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, 
442                                         an.VersionCompatibility, "VersionCompatibility");
443                         }
444                 }
445
446 #if !MONOTOUCH // Reflection.Emit is not supported.
447                 [Test]
448                 public void Location_Empty() {
449                         string assemblyFileName = Path.Combine (
450                                 Path.GetTempPath (), "AssemblyLocation.dll");
451
452                         try {
453                                 AssemblyName assemblyName = new AssemblyName ();
454                                 assemblyName.Name = "AssemblyLocation";
455
456                                 AssemblyBuilder ab = AppDomain.CurrentDomain
457                                         .DefineDynamicAssembly (assemblyName,
458                                         AssemblyBuilderAccess.Save,
459                                         Path.GetTempPath (),
460                                         AppDomain.CurrentDomain.Evidence);
461                                 ab.Save (Path.GetFileName (assemblyFileName));
462
463                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
464                                         byte[] buffer = new byte[fs.Length];
465                                         fs.Read (buffer, 0, buffer.Length);
466                                         Assembly assembly = Assembly.Load (buffer);
467                                         Assert.AreEqual (string.Empty, assembly.Location);
468                                         fs.Close ();
469                                 }
470                         } finally {
471                                 File.Delete (assemblyFileName);
472                         }
473                 }
474
475                 [Test]
476                 public void SateliteAssemblyForInMemoryAssembly ()
477                 {
478                         string assemblyFileName = Path.Combine (
479                                 Path.GetTempPath (), "AssemblyLocation1.dll");
480
481                         try {
482                                 AssemblyName assemblyName = new AssemblyName ();
483                                 assemblyName.Name = "AssemblyLocation1";
484
485                                 AssemblyBuilder ab = AppDomain.CurrentDomain
486                                         .DefineDynamicAssembly (assemblyName,
487                                                 AssemblyBuilderAccess.Save,
488                                                 Path.GetTempPath ());
489
490                                 ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
491                                 TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
492
493                                 MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
494                                 ILGenerator gen = methodBuilder.GetILGenerator ();
495
496                                 //
497                                 //      var resourceManager = new ResourceManager (typeof (Program));
498                                 //      resourceManager.GetString ("test");
499                                 //
500                                 gen.Emit (OpCodes.Ldtoken, typeBuilder);
501                                 gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
502                                 gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
503                                 gen.Emit (OpCodes.Ldstr, "test");
504                                 gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
505                                 gen.Emit (OpCodes.Pop);
506                                 gen.Emit (OpCodes.Ret);
507
508                                 typeBuilder.CreateType ();
509
510                                 ab.Save (Path.GetFileName (assemblyFileName));
511
512                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
513                                         byte[] buffer = new byte[fs.Length];
514                                         fs.Read (buffer, 0, buffer.Length);
515                                         Assembly assembly = Assembly.Load (buffer);
516
517                                         var mm = assembly.GetType ("Program").GetMethod ("TestCall");
518                                         try {
519                                                 mm.Invoke (null, null);
520                                                 Assert.Fail ();
521                                         } catch (TargetInvocationException e) {
522                                                 Assert.IsTrue (e.InnerException is MissingManifestResourceException);
523                                         }
524
525                                         fs.Close ();
526                                 }
527                         } finally {
528                                 File.Delete (assemblyFileName);
529                         }
530                 }
531
532                 [Test]
533                 [Category ("NotWorking")]
534                 public void bug78464 ()
535                 {
536                         string assemblyFileName = Path.Combine (
537                                 Path.GetTempPath (), "bug78464.dll");
538
539                         try {
540                                 // execute test in separate appdomain to allow assembly to be unloaded
541                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
542                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
543                                 try {
544                                         crossDomainTester.bug78464 (assemblyFileName);
545                                 } finally {
546                                         AppDomain.Unload (testDomain);
547                                 }
548                         } finally {
549                                 File.Delete (assemblyFileName);
550                         }
551                 }
552
553                 [Test]
554                 [Category("MobileNotWorking")]
555                 public void bug78465 ()
556                 {
557                         string assemblyFileName = Path.Combine (
558                                 Path.GetTempPath (), "bug78465.dll");
559
560                         try {
561                                 AssemblyName assemblyName = new AssemblyName ();
562                                 assemblyName.Name = "bug78465";
563
564                                 AssemblyBuilder ab = AppDomain.CurrentDomain
565                                         .DefineDynamicAssembly (assemblyName,
566                                         AssemblyBuilderAccess.Save,
567                                         Path.GetDirectoryName (assemblyFileName),
568                                         AppDomain.CurrentDomain.Evidence);
569                                 ab.Save (Path.GetFileName (assemblyFileName));
570
571                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
572                                         byte[] buffer = new byte[fs.Length];
573                                         fs.Read (buffer, 0, buffer.Length);
574                                         Assembly assembly = Assembly.Load (buffer);
575                                         Assert.AreEqual (string.Empty, assembly.Location, "#1");
576                                         fs.Close ();
577                                 }
578
579                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
580                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
581                                 try {
582                                         crossDomainTester.bug78465 (assemblyFileName);
583                                 } finally {
584                                         AppDomain.Unload (testDomain);
585                                 }
586                         } finally {
587                                 File.Delete (assemblyFileName);
588                         }
589                 }
590
591                 [Test]
592                 [Category("MobileNotWorking")]
593                 public void bug78468 ()
594                 {
595                         string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
596                                 "bug78468a.dll");
597                         string resourceFileName = Path.Combine (Path.GetTempPath (),
598                                 "readme.txt");
599
600                         using (StreamWriter sw = File.CreateText (resourceFileName)) {
601                                 sw.WriteLine ("FOO");
602                                 sw.Close ();
603                         }
604
605                         try {
606                                 AssemblyName assemblyName = new AssemblyName ();
607                                 assemblyName.Name = "bug78468a";
608
609                                 AssemblyBuilder ab = AppDomain.CurrentDomain
610                                         .DefineDynamicAssembly (assemblyName,
611                                         AssemblyBuilderAccess.Save,
612                                         Path.GetTempPath (),
613                                         AppDomain.CurrentDomain.Evidence);
614                                 ab.AddResourceFile ("read", "readme.txt");
615                                 ab.Save (Path.GetFileName (assemblyFileNameA));
616
617                                 Assembly assembly;
618
619                                 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
620                                         byte[] buffer = new byte[fs.Length];
621                                         fs.Read (buffer, 0, buffer.Length);
622                                         assembly = Assembly.Load (buffer);
623                                         fs.Close ();
624                                 }
625
626                                 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
627                                 string[] resNames = assembly.GetManifestResourceNames ();
628                                 Assert.IsNotNull (resNames, "#A2");
629                                 Assert.AreEqual (1, resNames.Length, "#A3");
630                                 Assert.AreEqual ("read", resNames[0], "#A4");
631                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
632                                 Assert.IsNotNull (resInfo, "#A5");
633                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
634                                 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
635                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
636                                 try {
637                                         assembly.GetManifestResourceStream ("read");
638                                         Assert.Fail ("#A9");
639                                 } catch (FileNotFoundException) {
640                                 }
641                                 try {
642                                         assembly.GetFile ("readme.txt");
643                                         Assert.Fail ("#A10");
644                                 } catch (FileNotFoundException) {
645                                 }
646
647                                 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
648                                         "bug78468b.dll");
649
650                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
651                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
652                                 try {
653                                         crossDomainTester.bug78468 (assemblyFileNameB);
654                                 } finally {
655                                         AppDomain.Unload (testDomain);
656                                         File.Delete (assemblyFileNameB);
657                                 }
658                         } finally {
659                                 File.Delete (assemblyFileNameA);
660                                 File.Delete (resourceFileName);
661                         }
662                 }
663
664                 [Test]
665                 [Category ("NotWorking")]
666                 public void ReflectionOnlyLoad ()
667                 {
668                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
669                         
670                         Assert.IsNotNull (assembly);
671                         Assert.IsTrue (assembly.ReflectionOnly);
672                 }
673
674                 [Test]
675                 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be directly as files
676                 public void ReflectionOnlyLoadFrom ()
677                 {
678                         string loc = typeof (AssemblyTest).Assembly.Location;
679                         string filename = Path.GetFileName (loc);
680                         Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
681
682                         Assert.IsNotNull (assembly);
683                         Assert.IsTrue (assembly.ReflectionOnly);
684                 }
685
686                 [Test]
687                 [ExpectedException (typeof (ArgumentException))]
688                 public void CreateInstanceOnRefOnly ()
689                 {
690                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
691                         assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
692                 }
693
694                 [Test]
695                 [Category ("NotWorking")] // patch for bug #79720 must be committed first
696                 public void Load_Culture ()
697                 {
698                         string tempDir = Path.Combine (Path.GetTempPath (),
699                                 "MonoTests.System.Reflection.AssemblyTest");
700                         string cultureTempDir = Path.Combine (tempDir, "nl-BE");
701                         if (!Directory.Exists (cultureTempDir))
702                                 Directory.CreateDirectory (cultureTempDir);
703                         cultureTempDir = Path.Combine (tempDir, "en-US");
704                         if (!Directory.Exists (cultureTempDir))
705                                 Directory.CreateDirectory (cultureTempDir);
706
707
708                         AppDomain ad = CreateTestDomain (tempDir, true);
709                         try {
710                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
711
712                                 // PART A
713
714                                 AssemblyName aname = new AssemblyName ();
715                                 aname.Name = "culturea";
716                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
717
718                                 aname = new AssemblyName ();
719                                 aname.Name = "culturea";
720                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
721
722                                 aname = new AssemblyName ();
723                                 aname.Name = "culturea";
724                                 aname.CultureInfo = new CultureInfo ("nl-BE");
725                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
726
727                                 aname = new AssemblyName ();
728                                 aname.Name = "culturea";
729                                 aname.CultureInfo = CultureInfo.InvariantCulture;
730                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
731
732                                 // PART B
733
734                                 aname = new AssemblyName ();
735                                 aname.Name = "cultureb";
736                                 aname.CultureInfo = new CultureInfo ("nl-BE");
737                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
738
739                                 aname = new AssemblyName ();
740                                 aname.Name = "cultureb";
741                                 aname.CultureInfo = new CultureInfo ("nl-BE");
742                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
743
744                                 aname = new AssemblyName ();
745                                 aname.Name = "cultureb";
746                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
747
748                                 aname = new AssemblyName ();
749                                 aname.Name = "cultureb";
750                                 aname.CultureInfo = new CultureInfo ("en-US");
751                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
752
753                                 // PART C
754
755                                 aname = new AssemblyName ();
756                                 aname.Name = "culturec";
757                                 aname.CultureInfo = new CultureInfo ("nl-BE");
758                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
759
760                                 aname = new AssemblyName ();
761                                 aname.Name = "culturec";
762                                 aname.CultureInfo = new CultureInfo ("nl-BE");
763                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
764
765                                 aname = new AssemblyName ();
766                                 aname.Name = "culturec";
767                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
768
769                                 aname = new AssemblyName ();
770                                 aname.Name = "culturec";
771                                 aname.CultureInfo = CultureInfo.InvariantCulture;
772                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
773
774                                 // PART D
775
776                                 aname = new AssemblyName ();
777                                 aname.Name = "cultured";
778                                 aname.CultureInfo = new CultureInfo ("nl-BE");
779                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
780
781                                 aname = new AssemblyName ();
782                                 aname.Name = "cultured";
783                                 aname.CultureInfo = new CultureInfo ("nl-BE");
784                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
785
786                                 aname = new AssemblyName ();
787                                 aname.Name = "cultured";
788                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
789
790                                 aname = new AssemblyName ();
791                                 aname.Name = "cultured";
792                                 aname.CultureInfo = CultureInfo.InvariantCulture;
793                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
794                         } finally {
795                                 AppDomain.Unload (ad);
796                                 if (Directory.Exists (tempDir))
797                                         Directory.Delete (tempDir, true);
798                         }
799                 }
800
801                 [Test] // bug #79712
802                 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
803                 public void Load_Culture_Mismatch ()
804                 {
805                         string tempDir = Path.Combine (Path.GetTempPath (),
806                                 "MonoTests.System.Reflection.AssemblyTest");
807                         string cultureTempDir = Path.Combine (tempDir, "en-US");
808                         if (!Directory.Exists (cultureTempDir))
809                                 Directory.CreateDirectory (cultureTempDir);
810
811                         AppDomain ad = CreateTestDomain (tempDir, true);
812                         try {
813                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
814
815                                 // PART A
816
817                                 AssemblyName aname = new AssemblyName ();
818                                 aname.Name = "bug79712a";
819                                 aname.CultureInfo = new CultureInfo ("nl-BE");
820                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
821
822                                 aname = new AssemblyName ();
823                                 aname.Name = "bug79712a";
824                                 aname.CultureInfo = CultureInfo.InvariantCulture;
825                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
826
827                                 // PART B
828
829                                 aname = new AssemblyName ();
830                                 aname.Name = "bug79712b";
831                                 aname.CultureInfo = new CultureInfo ("nl-BE");
832                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
833
834                                 aname = new AssemblyName ();
835                                 aname.Name = "bug79712b";
836                                 aname.CultureInfo = new CultureInfo ("en-US");
837                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
838                         } finally {
839                                 AppDomain.Unload (ad);
840                                 if (Directory.Exists (tempDir))
841                                         Directory.Delete (tempDir, true);
842                         }
843                 }
844
845
846                 [Test] // bug #79715
847                 [Category("MobileNotWorking")]
848                 public void Load_PartialVersion ()
849                 {
850                         string tempDir = Path.Combine (Path.GetTempPath (),
851                                 "MonoTests.System.Reflection.AssemblyTest");
852                         if (!Directory.Exists (tempDir))
853                                 Directory.CreateDirectory (tempDir);
854
855                         AppDomain ad = CreateTestDomain (tempDir, true);
856                         try {
857                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
858
859                                 AssemblyName aname = new AssemblyName ();
860                                 aname.Name = "bug79715";
861                                 aname.Version = new Version (1, 2, 3, 4);
862                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
863
864                                 aname = new AssemblyName ();
865                                 aname.Name = "bug79715";
866                                 aname.Version = new Version (1, 2);
867                                 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
868                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
869
870                                 aname = new AssemblyName ();
871                                 aname.Name = "bug79715";
872                                 aname.Version = new Version (1, 2, 3);
873                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
874                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
875
876                                 aname = new AssemblyName ();
877                                 aname.Name = "bug79715";
878                                 aname.Version = new Version (1, 2, 3, 4);
879                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
880                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
881                         } finally {
882                                 AppDomain.Unload (ad);
883                                 if (Directory.Exists (tempDir))
884                                         Directory.Delete (tempDir, true);
885                         }
886                 }
887
888                 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
889                 {
890                         AppDomainSetup setup = new AppDomainSetup ();
891                         setup.ApplicationBase = baseDirectory;
892                         setup.ApplicationName = "testdomain";
893
894                         AppDomain ad = AppDomain.CreateDomain ("testdomain", 
895                                 AppDomain.CurrentDomain.Evidence, setup);
896
897                         if (assemblyResolver) {
898                                 Assembly ea = Assembly.GetExecutingAssembly ();
899                                 ad.CreateInstanceFrom (ea.CodeBase,
900                                         typeof (AssemblyResolveHandler).FullName,
901                                         false,
902                                         BindingFlags.Public | BindingFlags.Instance,
903                                         null,
904                                         new object [] { ea.Location, ea.FullName },
905                                         CultureInfo.InvariantCulture,
906                                         null,
907                                         null);
908                         }
909
910                         return ad;
911                 }
912
913                 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
914                 {
915                         Type testerType = typeof (CrossDomainTester);
916                         return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
917                                 testerType.Assembly.FullName, testerType.FullName, false,
918                                 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
919                                 CultureInfo.InvariantCulture, new object[0], null);
920                 }
921
922                 private class CrossDomainTester : MarshalByRefObject
923                 {
924                         public void GenerateAssembly (AssemblyName aname, string path)
925                         {
926                                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
927                                         aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
928                                 ab.Save (Path.GetFileName (path));
929                         }
930
931                         public void Load (AssemblyName assemblyRef)
932                         {
933                                 Assembly.Load (assemblyRef);
934                         }
935
936                         public void LoadFrom (string assemblyFile)
937                         {
938                                 Assembly.LoadFrom (assemblyFile);
939                         }
940
941                         public bool AssertLoad (AssemblyName assemblyRef)
942                         {
943                                 try {
944                                         Assembly.Load (assemblyRef);
945                                         return true;
946                                 } catch {
947                                         return false;
948                                 }
949                         }
950
951                         public bool AssertLoad (string assemblyString)
952                         {
953                                 try {
954                                         Assembly.Load (assemblyString);
955                                         return true;
956                                 } catch {
957                                         return false;
958                                 }
959                         }
960
961                         public bool AssertFileLoadException (AssemblyName assemblyRef)
962                         {
963                                 try {
964                                         Assembly.Load (assemblyRef);
965                                         return false;
966                                 } catch (FileLoadException) {
967                                         return true;
968                                 }
969                         }
970
971                         public bool AssertFileNotFoundException (AssemblyName assemblyRef)
972                         {
973                                 try {
974                                         Assembly.Load (assemblyRef);
975                                         return false;
976                                 } catch (FileNotFoundException) {
977                                         return true;
978                                 }
979                         }
980
981                         public void bug78464 (string assemblyFileName)
982                         {
983                                 AssemblyName assemblyName = new AssemblyName ();
984                                 assemblyName.Name = "bug78464";
985
986                                 AssemblyBuilder ab = AppDomain.CurrentDomain
987                                         .DefineDynamicAssembly (assemblyName,
988                                         AssemblyBuilderAccess.Save,
989                                         Path.GetDirectoryName (assemblyFileName),
990                                         AppDomain.CurrentDomain.Evidence);
991                                 ab.Save (Path.GetFileName (assemblyFileName));
992
993                                 Assembly assembly;
994
995                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
996                                         byte[] buffer = new byte[fs.Length];
997                                         fs.Read (buffer, 0, buffer.Length);
998                                         assembly = Assembly.Load (buffer);
999                                         fs.Close ();
1000                                 }
1001
1002                                 Assert.AreEqual (string.Empty, assembly.Location, "#1");
1003
1004                                 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1005                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1006                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
1007                                 // note: we cannot check if directory names match, as MS.NET seems to 
1008                                 // convert directory part of assembly location to lowercase
1009                                 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
1010                         }
1011
1012                         public void bug78465 (string assemblyFileName)
1013                         {
1014                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1015                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1016                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1017                                 // note: we cannot check if directory names match, as MS.NET seems to 
1018                                 // convert directory part of assembly location to lowercase
1019                                 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1020                         }
1021
1022                         public void bug78468 (string assemblyFileName)
1023                         {
1024                                 AssemblyName assemblyName = new AssemblyName ();
1025                                 assemblyName.Name = "bug78468b";
1026
1027                                 AssemblyBuilder ab = AppDomain.CurrentDomain
1028                                         .DefineDynamicAssembly (assemblyName,
1029                                         AssemblyBuilderAccess.Save,
1030                                         Path.GetDirectoryName (assemblyFileName),
1031                                         AppDomain.CurrentDomain.Evidence);
1032                                 ab.AddResourceFile ("read", "readme.txt");
1033                                 ab.Save (Path.GetFileName (assemblyFileName));
1034
1035                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1036                                 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1037                                 string[] resNames = assembly.GetManifestResourceNames ();
1038                                 Assert.IsNotNull (resNames, "#B2");
1039                                 Assert.AreEqual (1, resNames.Length, "#B3");
1040                                 Assert.AreEqual ("read", resNames[0], "#B4");
1041                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1042                                 Assert.IsNotNull (resInfo, "#B5");
1043                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1044                                 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1045                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1046                                 Stream s = assembly.GetManifestResourceStream ("read");
1047                                 Assert.IsNotNull (s, "#B9");
1048                                 s.Close ();
1049                                 s = assembly.GetFile ("readme.txt");
1050                                 Assert.IsNotNull (s, "#B10");
1051                                 s.Close ();
1052                         }
1053                 }
1054
1055                 [Test]
1056                 public void bug79872 ()
1057                 {
1058                         Random rnd = new Random ();
1059                         string outdir;
1060                         int tries = 0;
1061
1062                 retry:
1063                         outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1064                         if (Directory.Exists (outdir)) {
1065                                 try {
1066                                         Directory.Delete (outdir, true);
1067                                 } catch {
1068                                         if (++tries <= 100)
1069                                                 goto retry;
1070                                 }
1071                         }
1072
1073                         Directory.CreateDirectory (outdir);
1074
1075                         AssemblyName an = new AssemblyName ();
1076                         an.Name = "bug79872";
1077                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1078                         string dllname = "bug79872.dll";
1079                         ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1080                         string netmodule = "bug79872.netmodule";
1081                         ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1082                         TypeBuilder a1 = mb2.DefineType ("A");
1083                         a1.CreateType ();
1084                         ab.Save (dllname);
1085
1086                         bool ok = true;
1087                         try {
1088                                 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1089                         } catch {
1090                                 ok = false;
1091                         }
1092                         Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1093
1094                         ok = false;
1095                         try {
1096                                 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1097                         } catch (BadImageFormatException) {
1098                                 ok = true; // mono and .net 2.0 throw this
1099                         } catch (FileLoadException) {
1100                                 ok = true; // .net 1.1 throws this
1101                         } catch {
1102                                 // swallow the rest
1103                         }
1104                         Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1105
1106                         Directory.Delete (outdir, true);
1107                 }
1108 #endif
1109
1110                 [Test]
1111                 public void ManifestModule ()
1112                 {
1113                         Assembly assembly = typeof (int).Assembly;
1114                         Module module = assembly.ManifestModule;
1115                         Assert.IsNotNull (module, "#1");
1116
1117                         Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1118
1119 #if !MONOTOUCH
1120                         Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1121 #endif
1122                         Assert.IsFalse (module.IsResource (), "#4");
1123                         Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1124                         Assert.AreSame (module, assembly.GetModules () [0], "#6");
1125                         Assert.AreSame (module, assembly.ManifestModule, "#7");
1126                 }
1127
1128
1129                 [Serializable ()]
1130                 private class AssemblyResolveHandler
1131                 {
1132                         public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1133                         {
1134                                 _assemblyFile = assemblyFile;
1135                                 _assemblyName = assemblyName;
1136
1137                                 AppDomain.CurrentDomain.AssemblyResolve +=
1138                                         new ResolveEventHandler (ResolveAssembly);
1139                         }
1140
1141                         private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1142                         {
1143                                 if (args.Name == _assemblyName)
1144                                         return Assembly.LoadFrom (_assemblyFile);
1145
1146                                 return null;
1147                         }
1148
1149                         private readonly string _assemblyFile;
1150                         private readonly string _assemblyName;
1151                 }
1152
1153                 protected internal class Bug328812_NestedFamORAssem { };
1154
1155                 [Test]
1156                 public void bug328812 ()
1157                 {
1158                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
1159                         Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1160                         // Just a sanity check, in case the above passed for some other reason
1161                         Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1162                 }
1163                 
1164                 [Test]
1165                 public void GetCustomAttributes_AttributeType_Null ()
1166                 {
1167                         Assembly a = typeof (int).Assembly;
1168                         try {
1169                                 a.GetCustomAttributes (null, false);
1170                                 Assert.Fail ("#1");
1171                         } catch (ArgumentNullException ex) {
1172                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1173                                 Assert.IsNull (ex.InnerException, "#3");
1174                                 Assert.IsNotNull (ex.Message, "#4");
1175                                 Assert.IsNotNull (ex.ParamName, "#5");
1176                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1177                         }
1178                 }
1179
1180                 [Test]
1181                 public void GetTypeWithEmptyStringShouldThrow ()
1182                 {
1183                         try {
1184                                 typeof (string).Assembly.GetType ("");
1185                                 Assert.Fail ("#1");
1186                         } catch (ArgumentException) {}
1187                 }
1188
1189 #if NET_4_5
1190                 [Test]
1191                 public void DefinedTypes_Equality ()
1192                 {
1193                         var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1194                         var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1195
1196                         Assert.AreSame (x1, x2, "#1");
1197                 }
1198 #endif
1199         }
1200
1201         public class TestDefinedTypes
1202         {
1203         }
1204 }