[tests] Add Xamarin.Android assembly name to list of probes
[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_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                 public void bug78465 ()
555                 {
556                         string assemblyFileName = Path.Combine (
557                                 Path.GetTempPath (), "bug78465.dll");
558
559                         try {
560                                 AssemblyName assemblyName = new AssemblyName ();
561                                 assemblyName.Name = "bug78465";
562
563                                 AssemblyBuilder ab = AppDomain.CurrentDomain
564                                         .DefineDynamicAssembly (assemblyName,
565                                         AssemblyBuilderAccess.Save,
566                                         Path.GetDirectoryName (assemblyFileName),
567                                         AppDomain.CurrentDomain.Evidence);
568                                 ab.Save (Path.GetFileName (assemblyFileName));
569
570                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
571                                         byte[] buffer = new byte[fs.Length];
572                                         fs.Read (buffer, 0, buffer.Length);
573                                         Assembly assembly = Assembly.Load (buffer);
574                                         Assert.AreEqual (string.Empty, assembly.Location, "#1");
575                                         fs.Close ();
576                                 }
577
578                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
579                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
580                                 try {
581                                         crossDomainTester.bug78465 (assemblyFileName);
582                                 } finally {
583                                         AppDomain.Unload (testDomain);
584                                 }
585                         } finally {
586                                 File.Delete (assemblyFileName);
587                         }
588                 }
589
590                 [Test]
591                 public void bug78468 ()
592                 {
593                         string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
594                                 "bug78468a.dll");
595                         string resourceFileName = Path.Combine (Path.GetTempPath (),
596                                 "readme.txt");
597
598                         using (StreamWriter sw = File.CreateText (resourceFileName)) {
599                                 sw.WriteLine ("FOO");
600                                 sw.Close ();
601                         }
602
603                         try {
604                                 AssemblyName assemblyName = new AssemblyName ();
605                                 assemblyName.Name = "bug78468a";
606
607                                 AssemblyBuilder ab = AppDomain.CurrentDomain
608                                         .DefineDynamicAssembly (assemblyName,
609                                         AssemblyBuilderAccess.Save,
610                                         Path.GetTempPath (),
611                                         AppDomain.CurrentDomain.Evidence);
612                                 ab.AddResourceFile ("read", "readme.txt");
613                                 ab.Save (Path.GetFileName (assemblyFileNameA));
614
615                                 Assembly assembly;
616
617                                 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
618                                         byte[] buffer = new byte[fs.Length];
619                                         fs.Read (buffer, 0, buffer.Length);
620                                         assembly = Assembly.Load (buffer);
621                                         fs.Close ();
622                                 }
623
624                                 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
625                                 string[] resNames = assembly.GetManifestResourceNames ();
626                                 Assert.IsNotNull (resNames, "#A2");
627                                 Assert.AreEqual (1, resNames.Length, "#A3");
628                                 Assert.AreEqual ("read", resNames[0], "#A4");
629                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
630                                 Assert.IsNotNull (resInfo, "#A5");
631                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
632                                 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
633                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
634                                 try {
635                                         assembly.GetManifestResourceStream ("read");
636                                         Assert.Fail ("#A9");
637                                 } catch (FileNotFoundException) {
638                                 }
639                                 try {
640                                         assembly.GetFile ("readme.txt");
641                                         Assert.Fail ("#A10");
642                                 } catch (FileNotFoundException) {
643                                 }
644
645                                 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
646                                         "bug78468b.dll");
647
648                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
649                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
650                                 try {
651                                         crossDomainTester.bug78468 (assemblyFileNameB);
652                                 } finally {
653                                         AppDomain.Unload (testDomain);
654                                         File.Delete (assemblyFileNameB);
655                                 }
656                         } finally {
657                                 File.Delete (assemblyFileNameA);
658                                 File.Delete (resourceFileName);
659                         }
660                 }
661
662                 [Test]
663                 [Category ("NotWorking")]
664                 public void ReflectionOnlyLoad ()
665                 {
666                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
667                         
668                         Assert.IsNotNull (assembly);
669                         Assert.IsTrue (assembly.ReflectionOnly);
670                 }
671
672                 [Test]
673                 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be directly as files
674                 public void ReflectionOnlyLoadFrom ()
675                 {
676                         string loc = typeof (AssemblyTest).Assembly.Location;
677                         string filename = Path.GetFileName (loc);
678                         Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
679
680                         Assert.IsNotNull (assembly);
681                         Assert.IsTrue (assembly.ReflectionOnly);
682                 }
683
684                 [Test]
685                 [ExpectedException (typeof (ArgumentException))]
686                 public void CreateInstanceOnRefOnly ()
687                 {
688                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
689                         assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
690                 }
691
692                 [Test]
693                 [Category ("NotWorking")] // patch for bug #79720 must be committed first
694                 public void Load_Culture ()
695                 {
696                         string tempDir = Path.Combine (Path.GetTempPath (),
697                                 "MonoTests.System.Reflection.AssemblyTest");
698                         string cultureTempDir = Path.Combine (tempDir, "nl-BE");
699                         if (!Directory.Exists (cultureTempDir))
700                                 Directory.CreateDirectory (cultureTempDir);
701                         cultureTempDir = Path.Combine (tempDir, "en-US");
702                         if (!Directory.Exists (cultureTempDir))
703                                 Directory.CreateDirectory (cultureTempDir);
704
705
706                         AppDomain ad = CreateTestDomain (tempDir, true);
707                         try {
708                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
709
710                                 // PART A
711
712                                 AssemblyName aname = new AssemblyName ();
713                                 aname.Name = "culturea";
714                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
715
716                                 aname = new AssemblyName ();
717                                 aname.Name = "culturea";
718                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
719
720                                 aname = new AssemblyName ();
721                                 aname.Name = "culturea";
722                                 aname.CultureInfo = new CultureInfo ("nl-BE");
723                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
724
725                                 aname = new AssemblyName ();
726                                 aname.Name = "culturea";
727                                 aname.CultureInfo = CultureInfo.InvariantCulture;
728                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
729
730                                 // PART B
731
732                                 aname = new AssemblyName ();
733                                 aname.Name = "cultureb";
734                                 aname.CultureInfo = new CultureInfo ("nl-BE");
735                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
736
737                                 aname = new AssemblyName ();
738                                 aname.Name = "cultureb";
739                                 aname.CultureInfo = new CultureInfo ("nl-BE");
740                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
741
742                                 aname = new AssemblyName ();
743                                 aname.Name = "cultureb";
744                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
745
746                                 aname = new AssemblyName ();
747                                 aname.Name = "cultureb";
748                                 aname.CultureInfo = new CultureInfo ("en-US");
749                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
750
751                                 // PART C
752
753                                 aname = new AssemblyName ();
754                                 aname.Name = "culturec";
755                                 aname.CultureInfo = new CultureInfo ("nl-BE");
756                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
757
758                                 aname = new AssemblyName ();
759                                 aname.Name = "culturec";
760                                 aname.CultureInfo = new CultureInfo ("nl-BE");
761                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
762
763                                 aname = new AssemblyName ();
764                                 aname.Name = "culturec";
765                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
766
767                                 aname = new AssemblyName ();
768                                 aname.Name = "culturec";
769                                 aname.CultureInfo = CultureInfo.InvariantCulture;
770                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
771
772                                 // PART D
773
774                                 aname = new AssemblyName ();
775                                 aname.Name = "cultured";
776                                 aname.CultureInfo = new CultureInfo ("nl-BE");
777                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
778
779                                 aname = new AssemblyName ();
780                                 aname.Name = "cultured";
781                                 aname.CultureInfo = new CultureInfo ("nl-BE");
782                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
783
784                                 aname = new AssemblyName ();
785                                 aname.Name = "cultured";
786                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
787
788                                 aname = new AssemblyName ();
789                                 aname.Name = "cultured";
790                                 aname.CultureInfo = CultureInfo.InvariantCulture;
791                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
792                         } finally {
793                                 AppDomain.Unload (ad);
794                                 if (Directory.Exists (tempDir))
795                                         Directory.Delete (tempDir, true);
796                         }
797                 }
798
799                 [Test] // bug #79712
800                 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
801                 public void Load_Culture_Mismatch ()
802                 {
803                         string tempDir = Path.Combine (Path.GetTempPath (),
804                                 "MonoTests.System.Reflection.AssemblyTest");
805                         string cultureTempDir = Path.Combine (tempDir, "en-US");
806                         if (!Directory.Exists (cultureTempDir))
807                                 Directory.CreateDirectory (cultureTempDir);
808
809                         AppDomain ad = CreateTestDomain (tempDir, true);
810                         try {
811                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
812
813                                 // PART A
814
815                                 AssemblyName aname = new AssemblyName ();
816                                 aname.Name = "bug79712a";
817                                 aname.CultureInfo = new CultureInfo ("nl-BE");
818                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
819
820                                 aname = new AssemblyName ();
821                                 aname.Name = "bug79712a";
822                                 aname.CultureInfo = CultureInfo.InvariantCulture;
823                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
824
825                                 // PART B
826
827                                 aname = new AssemblyName ();
828                                 aname.Name = "bug79712b";
829                                 aname.CultureInfo = new CultureInfo ("nl-BE");
830                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
831
832                                 aname = new AssemblyName ();
833                                 aname.Name = "bug79712b";
834                                 aname.CultureInfo = new CultureInfo ("en-US");
835                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
836                         } finally {
837                                 AppDomain.Unload (ad);
838                                 if (Directory.Exists (tempDir))
839                                         Directory.Delete (tempDir, true);
840                         }
841                 }
842
843
844                 [Test] // bug #79715
845                 public void Load_PartialVersion ()
846                 {
847                         string tempDir = Path.Combine (Path.GetTempPath (),
848                                 "MonoTests.System.Reflection.AssemblyTest");
849                         if (!Directory.Exists (tempDir))
850                                 Directory.CreateDirectory (tempDir);
851
852                         AppDomain ad = CreateTestDomain (tempDir, true);
853                         try {
854                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
855
856                                 AssemblyName aname = new AssemblyName ();
857                                 aname.Name = "bug79715";
858                                 aname.Version = new Version (1, 2, 3, 4);
859                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
860
861                                 aname = new AssemblyName ();
862                                 aname.Name = "bug79715";
863                                 aname.Version = new Version (1, 2);
864                                 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
865                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
866
867                                 aname = new AssemblyName ();
868                                 aname.Name = "bug79715";
869                                 aname.Version = new Version (1, 2, 3);
870                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
871                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
872
873                                 aname = new AssemblyName ();
874                                 aname.Name = "bug79715";
875                                 aname.Version = new Version (1, 2, 3, 4);
876                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
877                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
878                         } finally {
879                                 AppDomain.Unload (ad);
880                                 if (Directory.Exists (tempDir))
881                                         Directory.Delete (tempDir, true);
882                         }
883                 }
884
885                 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
886                 {
887                         AppDomainSetup setup = new AppDomainSetup ();
888                         setup.ApplicationBase = baseDirectory;
889                         setup.ApplicationName = "testdomain";
890
891                         AppDomain ad = AppDomain.CreateDomain ("testdomain", 
892                                 AppDomain.CurrentDomain.Evidence, setup);
893
894                         if (assemblyResolver) {
895                                 Assembly ea = Assembly.GetExecutingAssembly ();
896                                 ad.CreateInstanceFrom (ea.CodeBase,
897                                         typeof (AssemblyResolveHandler).FullName,
898                                         false,
899                                         BindingFlags.Public | BindingFlags.Instance,
900                                         null,
901                                         new object [] { ea.Location, ea.FullName },
902                                         CultureInfo.InvariantCulture,
903                                         null,
904                                         null);
905                         }
906
907                         return ad;
908                 }
909
910                 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
911                 {
912                         Type testerType = typeof (CrossDomainTester);
913                         return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
914                                 testerType.Assembly.FullName, testerType.FullName, false,
915                                 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
916                                 CultureInfo.InvariantCulture, new object[0], null);
917                 }
918
919                 private class CrossDomainTester : MarshalByRefObject
920                 {
921                         public void GenerateAssembly (AssemblyName aname, string path)
922                         {
923                                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
924                                         aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
925                                 ab.Save (Path.GetFileName (path));
926                         }
927
928                         public void Load (AssemblyName assemblyRef)
929                         {
930                                 Assembly.Load (assemblyRef);
931                         }
932
933                         public void LoadFrom (string assemblyFile)
934                         {
935                                 Assembly.LoadFrom (assemblyFile);
936                         }
937
938                         public bool AssertLoad (AssemblyName assemblyRef)
939                         {
940                                 try {
941                                         Assembly.Load (assemblyRef);
942                                         return true;
943                                 } catch {
944                                         return false;
945                                 }
946                         }
947
948                         public bool AssertLoad (string assemblyString)
949                         {
950                                 try {
951                                         Assembly.Load (assemblyString);
952                                         return true;
953                                 } catch {
954                                         return false;
955                                 }
956                         }
957
958                         public bool AssertFileLoadException (AssemblyName assemblyRef)
959                         {
960                                 try {
961                                         Assembly.Load (assemblyRef);
962                                         return false;
963                                 } catch (FileLoadException) {
964                                         return true;
965                                 }
966                         }
967
968                         public bool AssertFileNotFoundException (AssemblyName assemblyRef)
969                         {
970                                 try {
971                                         Assembly.Load (assemblyRef);
972                                         return false;
973                                 } catch (FileNotFoundException) {
974                                         return true;
975                                 }
976                         }
977
978                         public void bug78464 (string assemblyFileName)
979                         {
980                                 AssemblyName assemblyName = new AssemblyName ();
981                                 assemblyName.Name = "bug78464";
982
983                                 AssemblyBuilder ab = AppDomain.CurrentDomain
984                                         .DefineDynamicAssembly (assemblyName,
985                                         AssemblyBuilderAccess.Save,
986                                         Path.GetDirectoryName (assemblyFileName),
987                                         AppDomain.CurrentDomain.Evidence);
988                                 ab.Save (Path.GetFileName (assemblyFileName));
989
990                                 Assembly assembly;
991
992                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
993                                         byte[] buffer = new byte[fs.Length];
994                                         fs.Read (buffer, 0, buffer.Length);
995                                         assembly = Assembly.Load (buffer);
996                                         fs.Close ();
997                                 }
998
999                                 Assert.AreEqual (string.Empty, assembly.Location, "#1");
1000
1001                                 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1002                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1003                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
1004                                 // note: we cannot check if directory names match, as MS.NET seems to 
1005                                 // convert directory part of assembly location to lowercase
1006                                 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
1007                         }
1008
1009                         public void bug78465 (string assemblyFileName)
1010                         {
1011                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1012                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1013                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1014                                 // note: we cannot check if directory names match, as MS.NET seems to 
1015                                 // convert directory part of assembly location to lowercase
1016                                 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1017                         }
1018
1019                         public void bug78468 (string assemblyFileName)
1020                         {
1021                                 AssemblyName assemblyName = new AssemblyName ();
1022                                 assemblyName.Name = "bug78468b";
1023
1024                                 AssemblyBuilder ab = AppDomain.CurrentDomain
1025                                         .DefineDynamicAssembly (assemblyName,
1026                                         AssemblyBuilderAccess.Save,
1027                                         Path.GetDirectoryName (assemblyFileName),
1028                                         AppDomain.CurrentDomain.Evidence);
1029                                 ab.AddResourceFile ("read", "readme.txt");
1030                                 ab.Save (Path.GetFileName (assemblyFileName));
1031
1032                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1033                                 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1034                                 string[] resNames = assembly.GetManifestResourceNames ();
1035                                 Assert.IsNotNull (resNames, "#B2");
1036                                 Assert.AreEqual (1, resNames.Length, "#B3");
1037                                 Assert.AreEqual ("read", resNames[0], "#B4");
1038                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1039                                 Assert.IsNotNull (resInfo, "#B5");
1040                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1041                                 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1042                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1043                                 Stream s = assembly.GetManifestResourceStream ("read");
1044                                 Assert.IsNotNull (s, "#B9");
1045                                 s.Close ();
1046                                 s = assembly.GetFile ("readme.txt");
1047                                 Assert.IsNotNull (s, "#B10");
1048                                 s.Close ();
1049                         }
1050                 }
1051
1052                 [Test]
1053                 public void bug79872 ()
1054                 {
1055                         Random rnd = new Random ();
1056                         string outdir;
1057                         int tries = 0;
1058
1059                 retry:
1060                         outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
1061                         if (Directory.Exists (outdir)) {
1062                                 try {
1063                                         Directory.Delete (outdir, true);
1064                                 } catch {
1065                                         if (++tries <= 100)
1066                                                 goto retry;
1067                                 }
1068                         }
1069
1070                         Directory.CreateDirectory (outdir);
1071
1072                         AssemblyName an = new AssemblyName ();
1073                         an.Name = "bug79872";
1074                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1075                         string dllname = "bug79872.dll";
1076                         ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1077                         string netmodule = "bug79872.netmodule";
1078                         ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1079                         TypeBuilder a1 = mb2.DefineType ("A");
1080                         a1.CreateType ();
1081                         ab.Save (dllname);
1082
1083                         bool ok = true;
1084                         try {
1085                                 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1086                         } catch {
1087                                 ok = false;
1088                         }
1089                         Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1090
1091                         ok = false;
1092                         try {
1093                                 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1094                         } catch (BadImageFormatException) {
1095                                 ok = true; // mono and .net 2.0 throw this
1096                         } catch (FileLoadException) {
1097                                 ok = true; // .net 1.1 throws this
1098                         } catch {
1099                                 // swallow the rest
1100                         }
1101                         Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1102
1103                         Directory.Delete (outdir, true);
1104                 }
1105 #endif
1106
1107                 [Test]
1108                 public void ManifestModule ()
1109                 {
1110                         Assembly assembly = typeof (int).Assembly;
1111                         Module module = assembly.ManifestModule;
1112                         Assert.IsNotNull (module, "#1");
1113
1114 #if NET_4_0
1115                         Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1116 #else
1117                         Assert.AreEqual (typeof (Module), module.GetType (), "#2");
1118 #endif
1119
1120 #if !MONOTOUCH
1121                         Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1122 #endif
1123                         Assert.IsFalse (module.IsResource (), "#4");
1124                         Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1125                         Assert.AreSame (module, assembly.GetModules () [0], "#6");
1126                         Assert.AreSame (module, assembly.ManifestModule, "#7");
1127                 }
1128
1129
1130                 [Serializable ()]
1131                 private class AssemblyResolveHandler
1132                 {
1133                         public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1134                         {
1135                                 _assemblyFile = assemblyFile;
1136                                 _assemblyName = assemblyName;
1137
1138                                 AppDomain.CurrentDomain.AssemblyResolve +=
1139                                         new ResolveEventHandler (ResolveAssembly);
1140                         }
1141
1142                         private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1143                         {
1144                                 if (args.Name == _assemblyName)
1145                                         return Assembly.LoadFrom (_assemblyFile);
1146
1147                                 return null;
1148                         }
1149
1150                         private readonly string _assemblyFile;
1151                         private readonly string _assemblyName;
1152                 }
1153
1154                 protected internal class Bug328812_NestedFamORAssem { };
1155
1156                 [Test]
1157                 public void bug328812 ()
1158                 {
1159                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
1160                         Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1161                         // Just a sanity check, in case the above passed for some other reason
1162                         Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1163                 }
1164                 
1165                 [Test]
1166                 public void GetCustomAttributes_AttributeType_Null ()
1167                 {
1168                         Assembly a = typeof (int).Assembly;
1169                         try {
1170                                 a.GetCustomAttributes (null, false);
1171                                 Assert.Fail ("#1");
1172                         } catch (ArgumentNullException ex) {
1173                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1174                                 Assert.IsNull (ex.InnerException, "#3");
1175                                 Assert.IsNotNull (ex.Message, "#4");
1176                                 Assert.IsNotNull (ex.ParamName, "#5");
1177                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1178                         }
1179                 }
1180
1181                 [Test]
1182                 public void GetTypeWithEmptyStringShouldThrow ()
1183                 {
1184                         try {
1185                                 typeof (string).Assembly.GetType ("");
1186                                 Assert.Fail ("#1");
1187                         } catch (ArgumentException) {}
1188                 }
1189
1190 #if NET_4_5
1191                 [Test]
1192                 public void DefinedTypes_Equality ()
1193                 {
1194                         var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1195                         var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1196
1197                         Assert.AreSame (x1, x2, "#1");
1198                 }
1199 #endif
1200         }
1201
1202         public class TestDefinedTypes
1203         {
1204         }
1205 }