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