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