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