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