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