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