d8bbbeb169c09ad09f4ef3272961113f79f53f62
[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                 [Category ("StaticLinkedAotNotWorking")] // Can't find .dll files when bundled in .exe
326                 public void GetFiles_False ()
327                 {
328                         Assembly corlib = typeof (int).Assembly;
329                         FileStream[] fss = corlib.GetFiles ();
330                         Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
331
332                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
333                         fss = corlib_test.GetFiles ();
334                         Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
335                 }
336
337                 [Test]
338                 [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
339                 [Category ("StaticLinkedAotNotWorking")] // Can't find .dll files when bundled in .exe
340                 public void GetFiles_True ()
341                 {
342                         Assembly corlib = typeof (int).Assembly;
343                         FileStream[] fss = corlib.GetFiles ();
344                         Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
345
346                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
347                         fss = corlib_test.GetFiles ();
348                         Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
349                 }
350
351                 [Test]
352                 public void GetManifestResourceStream_Name_Empty ()
353                 {
354                         Assembly corlib = typeof (int).Assembly;
355
356                         try {
357                                 corlib.GetManifestResourceStream (string.Empty);
358                                 Assert.Fail ("#A1");
359                         } catch (ArgumentException ex) {
360                                 // String cannot have zero length
361                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
362                                 Assert.IsNull (ex.InnerException, "#A3");
363                                 Assert.IsNotNull (ex.Message, "#A4");
364                         }
365
366                         corlib.GetManifestResourceStream (typeof (int), string.Empty);
367
368                         try {
369                                 corlib.GetManifestResourceStream ((Type) null, string.Empty);
370                                 Assert.Fail ("#B1");
371                         } catch (ArgumentException ex) {
372                                 // String cannot have zero length
373                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
374                                 Assert.IsNull (ex.InnerException, "#B3");
375                                 Assert.IsNotNull (ex.Message, "#B4");
376                         }
377                 }
378
379                 [Test]
380                 public void GetManifestResourceStream_Name_Null ()
381                 {
382                         Assembly corlib = typeof (int).Assembly;
383
384                         try {
385                                 corlib.GetManifestResourceStream ((string) null);
386                                 Assert.Fail ("#A1");
387                         } catch (ArgumentNullException ex) {
388                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
389                                 Assert.IsNull (ex.InnerException, "#A3");
390                                 Assert.IsNotNull (ex.Message, "#A4");
391                         }
392
393                         corlib.GetManifestResourceStream (typeof (int), (string) null);
394
395                         try {
396                                 corlib.GetManifestResourceStream ((Type) null, (string) null);
397                                 Assert.Fail ("#B1");
398                         } catch (ArgumentNullException ex) {
399                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
400                                 Assert.IsNull (ex.InnerException, "#B3");
401                                 Assert.IsNotNull (ex.Message, "#B4");
402                                 Assert.IsNotNull (ex.ParamName, "#B5");
403                                 Assert.AreEqual ("type", ex.ParamName, "#B6");
404                         }
405                 }
406
407                 [Test]
408                 public void IsDefined_AttributeType_Null ()
409                 {
410                         Assembly corlib = typeof (int).Assembly;
411
412                         try {
413                                 corlib.IsDefined ((Type) null, false);
414                                 Assert.Fail ("#1");
415                         } catch (ArgumentNullException ex) {
416                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
417                                 Assert.IsNull (ex.InnerException, "#3");
418                                 Assert.IsNotNull (ex.Message, "#4");
419                                 Assert.IsNotNull (ex.ParamName, "#5");
420                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
421                         }
422                 }
423
424                 [Test] // bug #78517
425                 public void LoadFrom_Empty_Assembly ()
426                 {
427                         string tempFile = Path.Combine (TempFolder, Path.GetRandomFileName ());
428                         File.CreateText (tempFile).Close ();
429
430                         try {
431                                 Assembly.LoadFrom (tempFile);
432                                 Assert.Fail ("#1");
433                         } catch (BadImageFormatException ex) {
434                                 Assert.IsNull (ex.InnerException, "#2");
435                         }
436                 }
437
438                 [Test] // bug #78517
439                 public void LoadFrom_Invalid_Assembly ()
440                 {
441                         string tempFile = Path.Combine (TempFolder, Path.GetRandomFileName ());
442                         using (StreamWriter sw = File.CreateText (tempFile)) {
443                                 sw.WriteLine ("foo");
444                                 sw.Close ();
445                         }
446
447                         try {
448                                 Assembly.LoadFrom (tempFile);
449                                 Assert.Fail ("#1");
450                         } catch (BadImageFormatException ex) {
451                                 Assert.IsNull (ex.InnerException, "#2");
452                         }
453                 }
454
455                 [Test]
456                 public void LoadFrom_NonExisting_Assembly ()
457                 {
458                         string tempFile = Path.Combine (TempFolder, Path.GetRandomFileName ());
459
460                         try {
461                                 Assembly.LoadFrom (tempFile);
462                                 Assert.Fail ("#1");
463                         } catch (FileNotFoundException ex) {
464                                 Assert.IsNull (ex.InnerException, "#2");
465                         }
466                 }
467
468                 [Test]
469                 public void LoadWithPartialName ()
470                 {
471 // FIXME?
472 // So the problem here is that if we load an assembly
473 // in a fully aot mode and then cannot load the
474 // dylib, we will assert. This test is incompatible
475 // with the semantics of aot'ed assembly loading, as
476 // aot may assert when loading. This assumes that it's
477 // safe to greedly load everything.
478                         var names = new string[] { Assembly.GetCallingAssembly ().GetName ().Name };
479
480                         foreach (string s in names)
481                                 if (Assembly.LoadWithPartialName (s) != null)
482                                         return;
483                         Assert.Fail ("Was not able to load any corlib test");
484                 }
485
486                 [Test]
487                 public void GetObjectData_Info_Null ()
488                 {
489                         Assembly corlib = typeof (int).Assembly;
490                         try {
491                                 corlib.GetObjectData (null, new StreamingContext (
492                                         StreamingContextStates.All));
493                                 Assert.Fail ("#1");
494                         } catch (ArgumentNullException ex) {
495                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
496                                 Assert.IsNull (ex.InnerException, "#3");
497                                 Assert.IsNotNull (ex.Message, "#4");
498                                 Assert.IsNotNull (ex.ParamName, "#5");
499                                 Assert.AreEqual ("info", ex.ParamName, "#6");
500                         }
501                 }
502
503                 [Test]
504                 public void GetReferencedAssemblies ()
505                 {
506                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
507                         AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
508                         foreach (AssemblyName an in names) {
509                                 Assert.IsNull (an.CodeBase, "CodeBase");
510                                 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
511                                 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
512                                 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
513                                 Assert.IsNotNull (an.FullName, "FullName");
514                                 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
515                                 Assert.IsNull (an.KeyPair, "KeyPair");
516                                 Assert.IsNotNull (an.Name, "Name");
517                                 Assert.IsNotNull (an.Version, "Version");
518                                 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, 
519                                         an.VersionCompatibility, "VersionCompatibility");
520                         }
521                 }
522
523 #if !MONOTOUCH && !FULL_AOT_RUNTIME // Reflection.Emit is not supported.
524                 [Test]
525                 public void Location_Empty() {
526                         string assemblyFileName = Path.Combine (
527                                 TempFolder, "AssemblyLocation.dll");
528
529                         AssemblyName assemblyName = new AssemblyName ();
530                         assemblyName.Name = "AssemblyLocation";
531
532                         AssemblyBuilder ab = AppDomain.CurrentDomain
533                                 .DefineDynamicAssembly (assemblyName,
534                                 AssemblyBuilderAccess.Save,
535                                 TempFolder,
536                                 AppDomain.CurrentDomain.Evidence);
537                         ab.Save (Path.GetFileName (assemblyFileName));
538
539                         using (FileStream fs = File.OpenRead (assemblyFileName)) {
540                                 byte[] buffer = new byte[fs.Length];
541                                 fs.Read (buffer, 0, buffer.Length);
542                                 Assembly assembly = Assembly.Load (buffer);
543                                 Assert.AreEqual (string.Empty, assembly.Location);
544                                 fs.Close ();
545                         }
546                 }
547
548                 [Test]
549                 public void SateliteAssemblyForInMemoryAssembly ()
550                 {
551                         string assemblyFileName = Path.Combine (
552                                 TempFolder, "AssemblyLocation1.dll");
553
554                         AssemblyName assemblyName = new AssemblyName ();
555                         assemblyName.Name = "AssemblyLocation1";
556
557                         AssemblyBuilder ab = AppDomain.CurrentDomain
558                                 .DefineDynamicAssembly (assemblyName,
559                                         AssemblyBuilderAccess.Save,
560                                         TempFolder);
561
562                         ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
563                         TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
564
565                         MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
566                         ILGenerator gen = methodBuilder.GetILGenerator ();
567
568                         //
569                         //      var resourceManager = new ResourceManager (typeof (Program));
570                         //      resourceManager.GetString ("test");
571                         //
572                         gen.Emit (OpCodes.Ldtoken, typeBuilder);
573                         gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
574                         gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
575                         gen.Emit (OpCodes.Ldstr, "test");
576                         gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
577                         gen.Emit (OpCodes.Pop);
578                         gen.Emit (OpCodes.Ret);
579
580                         typeBuilder.CreateType ();
581
582                         ab.Save (Path.GetFileName (assemblyFileName));
583
584                         using (FileStream fs = File.OpenRead (assemblyFileName)) {
585                                 byte[] buffer = new byte[fs.Length];
586                                 fs.Read (buffer, 0, buffer.Length);
587                                 Assembly assembly = Assembly.Load (buffer);
588
589                                 var mm = assembly.GetType ("Program").GetMethod ("TestCall");
590                                 try {
591                                         mm.Invoke (null, null);
592                                         Assert.Fail ();
593                                 } catch (TargetInvocationException e) {
594                                         Assert.IsTrue (e.InnerException is MissingManifestResourceException);
595                                 }
596
597                                 fs.Close ();
598                         }
599                 }
600
601                 [Test]
602                 [Category ("NotWorking")]
603                 public void bug78464 ()
604                 {
605                         string assemblyFileName = Path.Combine (
606                                 TempFolder, "bug78464.dll");
607
608                         // execute test in separate appdomain to allow assembly to be unloaded
609                         AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
610                         CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
611                         try {
612                                 crossDomainTester.bug78464 (assemblyFileName);
613                         } finally {
614                                 AppDomain.Unload (testDomain);
615                         }
616                 }
617
618                 [Test]
619                 [Category("MobileNotWorking")]
620                 public void bug78465 ()
621                 {
622                         string assemblyFileName = Path.Combine (
623                                 TempFolder, "bug78465.dll");
624
625                         AssemblyName assemblyName = new AssemblyName ();
626                         assemblyName.Name = "bug78465";
627
628                         AssemblyBuilder ab = AppDomain.CurrentDomain
629                                 .DefineDynamicAssembly (assemblyName,
630                                 AssemblyBuilderAccess.Save,
631                                 Path.GetDirectoryName (assemblyFileName),
632                                 AppDomain.CurrentDomain.Evidence);
633                         ab.Save (Path.GetFileName (assemblyFileName));
634
635                         using (FileStream fs = File.OpenRead (assemblyFileName)) {
636                                 byte[] buffer = new byte[fs.Length];
637                                 fs.Read (buffer, 0, buffer.Length);
638                                 Assembly assembly = Assembly.Load (buffer);
639                                 Assert.AreEqual (string.Empty, assembly.Location, "#1");
640                                 fs.Close ();
641                         }
642
643                         AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
644                         CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
645                         try {
646                                 crossDomainTester.bug78465 (assemblyFileName);
647                         } finally {
648                                 AppDomain.Unload (testDomain);
649                         }
650                 }
651
652                 [Test]
653                 [Category("MobileNotWorking")]
654                 public void bug78468 ()
655                 {
656                         string assemblyFileNameA = Path.Combine (TempFolder,
657                                 "bug78468a.dll");
658                         string resourceFileName = Path.Combine (TempFolder,
659                                 "readme.txt");
660
661                         using (StreamWriter sw = File.CreateText (resourceFileName)) {
662                                 sw.WriteLine ("FOO");
663                                 sw.Close ();
664                         }
665
666                         AssemblyName assemblyName = new AssemblyName ();
667                         assemblyName.Name = "bug78468a";
668
669                         AssemblyBuilder ab = AppDomain.CurrentDomain
670                                 .DefineDynamicAssembly (assemblyName,
671                                 AssemblyBuilderAccess.Save,
672                                 TempFolder,
673                                 AppDomain.CurrentDomain.Evidence);
674                         ab.AddResourceFile ("read", "readme.txt");
675                         ab.Save (Path.GetFileName (assemblyFileNameA));
676
677                         Assembly assembly;
678
679                         using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
680                                 byte[] buffer = new byte[fs.Length];
681                                 fs.Read (buffer, 0, buffer.Length);
682                                 assembly = Assembly.Load (buffer);
683                                 fs.Close ();
684                         }
685
686                         Assert.AreEqual (string.Empty, assembly.Location, "#A1");
687                         string[] resNames = assembly.GetManifestResourceNames ();
688                         Assert.IsNotNull (resNames, "#A2");
689                         Assert.AreEqual (1, resNames.Length, "#A3");
690                         Assert.AreEqual ("read", resNames[0], "#A4");
691                         ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
692                         Assert.IsNotNull (resInfo, "#A5");
693                         Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
694                         Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
695                         Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
696                         try {
697                                 assembly.GetManifestResourceStream ("read");
698                                 Assert.Fail ("#A9");
699                         } catch (FileNotFoundException) {
700                         }
701                         try {
702                                 assembly.GetFile ("readme.txt");
703                                 Assert.Fail ("#A10");
704                         } catch (FileNotFoundException) {
705                         }
706
707                         string assemblyFileNameB = Path.Combine (TempFolder,
708                                 "bug78468b.dll");
709
710                         AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
711                         CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
712                         try {
713                                 crossDomainTester.bug78468 (assemblyFileNameB);
714                         } finally {
715                                 AppDomain.Unload (testDomain);
716                         }
717                 }
718
719                 [Test]
720                 [Category ("NotWorking")]
721                 public void ReflectionOnlyLoad ()
722                 {
723                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
724                         
725                         Assert.IsNotNull (assembly);
726                         Assert.IsTrue (assembly.ReflectionOnly);
727                 }
728
729                 [Test]
730                 [Category ("AndroidNotWorking")] // Xamarin.Android assemblies are bundled so they don't exist in the file system.
731                 public void ReflectionOnlyLoadFrom ()
732                 {
733                         string loc = typeof (AssemblyTest).Assembly.Location;
734                         string filename = Path.GetFileName (loc);
735                         Assembly assembly = Assembly.ReflectionOnlyLoadFrom (loc);
736
737                         Assert.IsNotNull (assembly);
738                         Assert.IsTrue (assembly.ReflectionOnly);
739                 }
740
741                 [Test]
742                 [ExpectedException (typeof (ArgumentException))]
743                 public void CreateInstanceOnRefOnly ()
744                 {
745                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
746                         assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
747                 }
748
749                 [Test]
750                 [Category ("NotWorking")] // patch for bug #79720 must be committed first
751                 public void Load_Culture ()
752                 {
753                         string tempDir = TempFolder;
754                         string cultureTempDir = Path.Combine (tempDir, "nl-BE");
755                         if (!Directory.Exists (cultureTempDir))
756                                 Directory.CreateDirectory (cultureTempDir);
757                         cultureTempDir = Path.Combine (tempDir, "en-US");
758                         if (!Directory.Exists (cultureTempDir))
759                                 Directory.CreateDirectory (cultureTempDir);
760
761
762                         AppDomain ad = CreateTestDomain (tempDir, true);
763                         try {
764                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
765
766                                 // PART A
767
768                                 AssemblyName aname = new AssemblyName ();
769                                 aname.Name = "culturea";
770                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
771
772                                 aname = new AssemblyName ();
773                                 aname.Name = "culturea";
774                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
775
776                                 aname = new AssemblyName ();
777                                 aname.Name = "culturea";
778                                 aname.CultureInfo = new CultureInfo ("nl-BE");
779                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
780
781                                 aname = new AssemblyName ();
782                                 aname.Name = "culturea";
783                                 aname.CultureInfo = CultureInfo.InvariantCulture;
784                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
785
786                                 // PART B
787
788                                 aname = new AssemblyName ();
789                                 aname.Name = "cultureb";
790                                 aname.CultureInfo = new CultureInfo ("nl-BE");
791                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
792
793                                 aname = new AssemblyName ();
794                                 aname.Name = "cultureb";
795                                 aname.CultureInfo = new CultureInfo ("nl-BE");
796                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
797
798                                 aname = new AssemblyName ();
799                                 aname.Name = "cultureb";
800                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
801
802                                 aname = new AssemblyName ();
803                                 aname.Name = "cultureb";
804                                 aname.CultureInfo = new CultureInfo ("en-US");
805                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
806
807                                 // PART C
808
809                                 aname = new AssemblyName ();
810                                 aname.Name = "culturec";
811                                 aname.CultureInfo = new CultureInfo ("nl-BE");
812                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
813
814                                 aname = new AssemblyName ();
815                                 aname.Name = "culturec";
816                                 aname.CultureInfo = new CultureInfo ("nl-BE");
817                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
818
819                                 aname = new AssemblyName ();
820                                 aname.Name = "culturec";
821                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
822
823                                 aname = new AssemblyName ();
824                                 aname.Name = "culturec";
825                                 aname.CultureInfo = CultureInfo.InvariantCulture;
826                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
827
828                                 // PART D
829
830                                 aname = new AssemblyName ();
831                                 aname.Name = "cultured";
832                                 aname.CultureInfo = new CultureInfo ("nl-BE");
833                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
834
835                                 aname = new AssemblyName ();
836                                 aname.Name = "cultured";
837                                 aname.CultureInfo = new CultureInfo ("nl-BE");
838                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
839
840                                 aname = new AssemblyName ();
841                                 aname.Name = "cultured";
842                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
843
844                                 aname = new AssemblyName ();
845                                 aname.Name = "cultured";
846                                 aname.CultureInfo = CultureInfo.InvariantCulture;
847                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
848                         } finally {
849                                 AppDomain.Unload (ad);
850                         }
851                 }
852
853                 [Test] // bug #79712
854                 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
855                 public void Load_Culture_Mismatch ()
856                 {
857                         string tempDir = TempFolder;
858                         string cultureTempDir = Path.Combine (tempDir, "en-US");
859                         if (!Directory.Exists (cultureTempDir))
860                                 Directory.CreateDirectory (cultureTempDir);
861
862                         AppDomain ad = CreateTestDomain (tempDir, true);
863                         try {
864                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
865
866                                 // PART A
867
868                                 AssemblyName aname = new AssemblyName ();
869                                 aname.Name = "bug79712a";
870                                 aname.CultureInfo = new CultureInfo ("nl-BE");
871                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
872
873                                 aname = new AssemblyName ();
874                                 aname.Name = "bug79712a";
875                                 aname.CultureInfo = CultureInfo.InvariantCulture;
876                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
877
878                                 // PART B
879
880                                 aname = new AssemblyName ();
881                                 aname.Name = "bug79712b";
882                                 aname.CultureInfo = new CultureInfo ("nl-BE");
883                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
884
885                                 aname = new AssemblyName ();
886                                 aname.Name = "bug79712b";
887                                 aname.CultureInfo = new CultureInfo ("en-US");
888                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
889                         } finally {
890                                 AppDomain.Unload (ad);
891                         }
892                 }
893
894
895                 [Test] // bug #79715
896                 [Category("MobileNotWorking")]
897                 public void Load_PartialVersion ()
898                 {
899                         string tempDir = TempFolder;
900
901                         AppDomain ad = CreateTestDomain (tempDir, true);
902                         try {
903                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
904
905                                 AssemblyName aname = new AssemblyName ();
906                                 aname.Name = "bug79715";
907                                 aname.Version = new Version (1, 2, 3, 4);
908                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
909
910                                 aname = new AssemblyName ();
911                                 aname.Name = "bug79715";
912                                 aname.Version = new Version (1, 2);
913                                 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
914                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
915
916                                 aname = new AssemblyName ();
917                                 aname.Name = "bug79715";
918                                 aname.Version = new Version (1, 2, 3);
919                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
920                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
921
922                                 aname = new AssemblyName ();
923                                 aname.Name = "bug79715";
924                                 aname.Version = new Version (1, 2, 3, 4);
925                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
926                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
927                         } finally {
928                                 AppDomain.Unload (ad);
929                         }
930                 }
931
932                 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
933                 {
934                         AppDomainSetup setup = new AppDomainSetup ();
935                         setup.ApplicationBase = baseDirectory;
936                         setup.ApplicationName = "testdomain";
937
938                         AppDomain ad = AppDomain.CreateDomain ("testdomain", 
939                                 AppDomain.CurrentDomain.Evidence, setup);
940
941                         if (assemblyResolver) {
942                                 Assembly ea = Assembly.GetExecutingAssembly ();
943                                 ad.CreateInstanceFrom (ea.CodeBase,
944                                         typeof (AssemblyResolveHandler).FullName,
945                                         false,
946                                         BindingFlags.Public | BindingFlags.Instance,
947                                         null,
948                                         new object [] { ea.Location, ea.FullName },
949                                         CultureInfo.InvariantCulture,
950                                         null,
951                                         null);
952                         }
953
954                         return ad;
955                 }
956
957                 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
958                 {
959                         Type testerType = typeof (CrossDomainTester);
960                         return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
961                                 testerType.Assembly.FullName, testerType.FullName, false,
962                                 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
963                                 CultureInfo.InvariantCulture, new object[0], null);
964                 }
965
966                 private class CrossDomainTester : MarshalByRefObject
967                 {
968                         public void GenerateAssembly (AssemblyName aname, string path)
969                         {
970                                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
971                                         aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
972                                 ab.Save (Path.GetFileName (path));
973                         }
974
975                         public void Load (AssemblyName assemblyRef)
976                         {
977                                 Assembly.Load (assemblyRef);
978                         }
979
980                         public void LoadFrom (string assemblyFile)
981                         {
982                                 Assembly.LoadFrom (assemblyFile);
983                         }
984
985                         public bool AssertLoad (AssemblyName assemblyRef)
986                         {
987                                 try {
988                                         Assembly.Load (assemblyRef);
989                                         return true;
990                                 } catch {
991                                         return false;
992                                 }
993                         }
994
995                         public bool AssertLoad (string assemblyString)
996                         {
997                                 try {
998                                         Assembly.Load (assemblyString);
999                                         return true;
1000                                 } catch {
1001                                         return false;
1002                                 }
1003                         }
1004
1005                         public bool AssertFileLoadException (AssemblyName assemblyRef)
1006                         {
1007                                 try {
1008                                         Assembly.Load (assemblyRef);
1009                                         return false;
1010                                 } catch (FileLoadException) {
1011                                         return true;
1012                                 }
1013                         }
1014
1015                         public bool AssertFileNotFoundException (AssemblyName assemblyRef)
1016                         {
1017                                 try {
1018                                         Assembly.Load (assemblyRef);
1019                                         return false;
1020                                 } catch (FileNotFoundException) {
1021                                         return true;
1022                                 }
1023                         }
1024
1025                         public void bug78464 (string assemblyFileName)
1026                         {
1027                                 AssemblyName assemblyName = new AssemblyName ();
1028                                 assemblyName.Name = "bug78464";
1029
1030                                 AssemblyBuilder ab = AppDomain.CurrentDomain
1031                                         .DefineDynamicAssembly (assemblyName,
1032                                         AssemblyBuilderAccess.Save,
1033                                         Path.GetDirectoryName (assemblyFileName),
1034                                         AppDomain.CurrentDomain.Evidence);
1035                                 ab.Save (Path.GetFileName (assemblyFileName));
1036
1037                                 Assembly assembly;
1038
1039                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
1040                                         byte[] buffer = new byte[fs.Length];
1041                                         fs.Read (buffer, 0, buffer.Length);
1042                                         assembly = Assembly.Load (buffer);
1043                                         fs.Close ();
1044                                 }
1045
1046                                 Assert.AreEqual (string.Empty, assembly.Location, "#1");
1047
1048                                 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1049                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1050                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
1051                                 // note: we cannot check if directory names match, as MS.NET seems to 
1052                                 // convert directory part of assembly location to lowercase
1053                                 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
1054                         }
1055
1056                         public void bug78465 (string assemblyFileName)
1057                         {
1058                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1059                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
1060                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
1061                                 // note: we cannot check if directory names match, as MS.NET seems to 
1062                                 // convert directory part of assembly location to lowercase
1063                                 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
1064                         }
1065
1066                         public void bug78468 (string assemblyFileName)
1067                         {
1068                                 AssemblyName assemblyName = new AssemblyName ();
1069                                 assemblyName.Name = "bug78468b";
1070
1071                                 AssemblyBuilder ab = AppDomain.CurrentDomain
1072                                         .DefineDynamicAssembly (assemblyName,
1073                                         AssemblyBuilderAccess.Save,
1074                                         Path.GetDirectoryName (assemblyFileName),
1075                                         AppDomain.CurrentDomain.Evidence);
1076                                 ab.AddResourceFile ("read", "readme.txt");
1077                                 ab.Save (Path.GetFileName (assemblyFileName));
1078
1079                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
1080                                 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
1081                                 string[] resNames = assembly.GetManifestResourceNames ();
1082                                 Assert.IsNotNull (resNames, "#B2");
1083                                 Assert.AreEqual (1, resNames.Length, "#B3");
1084                                 Assert.AreEqual ("read", resNames[0], "#B4");
1085                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
1086                                 Assert.IsNotNull (resInfo, "#B5");
1087                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
1088                                 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
1089                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
1090                                 Stream s = assembly.GetManifestResourceStream ("read");
1091                                 Assert.IsNotNull (s, "#B9");
1092                                 s.Close ();
1093                                 s = assembly.GetFile ("readme.txt");
1094                                 Assert.IsNotNull (s, "#B10");
1095                                 s.Close ();
1096                         }
1097                 }
1098
1099                 [Test]
1100                 public void bug79872 ()
1101                 {
1102                         string outdir = TempFolder;
1103
1104                         AssemblyName an = new AssemblyName ();
1105                         an.Name = "bug79872";
1106                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
1107                         string dllname = "bug79872.dll";
1108                         ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
1109                         string netmodule = "bug79872.netmodule";
1110                         ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
1111                         TypeBuilder a1 = mb2.DefineType ("A");
1112                         a1.CreateType ();
1113                         ab.Save (dllname);
1114
1115                         bool ok = true;
1116                         try {
1117                                 Assembly.LoadFrom (Path.Combine (outdir, dllname));
1118                         } catch {
1119                                 ok = false;
1120                         }
1121                         Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
1122
1123                         ok = false;
1124                         try {
1125                                 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
1126                         } catch (BadImageFormatException) {
1127                                 ok = true; // mono and .net 2.0 throw this
1128                         } catch (FileLoadException) {
1129                                 ok = true; // .net 1.1 throws this
1130                         } catch {
1131                                 // swallow the rest
1132                         }
1133                         Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
1134                 }
1135 #endif
1136
1137                 [Test]
1138                 public void ManifestModule ()
1139                 {
1140                         Assembly assembly = typeof (int).Assembly;
1141                         Module module = assembly.ManifestModule;
1142                         Assert.IsNotNull (module, "#1");
1143
1144                         Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
1145
1146 #if !MONOTOUCH && !FULL_AOT_RUNTIME
1147                         Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
1148 #endif
1149                         Assert.IsFalse (module.IsResource (), "#4");
1150                         Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
1151                         Assert.AreSame (module, assembly.GetModules () [0], "#6");
1152                         Assert.AreSame (module, assembly.ManifestModule, "#7");
1153                 }
1154
1155
1156                 [Serializable ()]
1157                 private class AssemblyResolveHandler
1158                 {
1159                         public AssemblyResolveHandler (string assemblyFile, string assemblyName)
1160                         {
1161                                 _assemblyFile = assemblyFile;
1162                                 _assemblyName = assemblyName;
1163
1164                                 AppDomain.CurrentDomain.AssemblyResolve +=
1165                                         new ResolveEventHandler (ResolveAssembly);
1166                         }
1167
1168                         private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
1169                         {
1170                                 if (args.Name == _assemblyName)
1171                                         return Assembly.LoadFrom (_assemblyFile);
1172
1173                                 return null;
1174                         }
1175
1176                         private readonly string _assemblyFile;
1177                         private readonly string _assemblyName;
1178                 }
1179
1180                 protected internal class Bug328812_NestedFamORAssem { };
1181
1182                 [Test]
1183                 public void bug328812 ()
1184                 {
1185                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
1186                         Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
1187                         // Just a sanity check, in case the above passed for some other reason
1188                         Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
1189                 }
1190                 
1191                 [Test]
1192                 public void GetCustomAttributes_AttributeType_Null ()
1193                 {
1194                         Assembly a = typeof (int).Assembly;
1195                         try {
1196                                 a.GetCustomAttributes (null, false);
1197                                 Assert.Fail ("#1");
1198                         } catch (ArgumentNullException ex) {
1199                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1200                                 Assert.IsNull (ex.InnerException, "#3");
1201                                 Assert.IsNotNull (ex.Message, "#4");
1202                                 Assert.IsNotNull (ex.ParamName, "#5");
1203                                 Assert.AreEqual ("attributeType", ex.ParamName, "#6");
1204                         }
1205                 }
1206
1207                 [Test]
1208                 public void GetTypeWithEmptyStringShouldThrow ()
1209                 {
1210                         try {
1211                                 typeof (string).Assembly.GetType ("");
1212                                 Assert.Fail ("#1");
1213                         } catch (ArgumentException) {}
1214                 }
1215
1216                 class GetCallingAssemblyCallee {
1217                         static int _dummy;
1218
1219                         static void sideEffect () {
1220                                 _dummy++;
1221                         }
1222
1223                         // GetCallingAssembly may see an unpredictable
1224                         // view of the stack if it's called in tail
1225                         // position, or if its caller or the caller's
1226                         // caller is inlined.  So we put in a side
1227                         // effect to get out of tail position, and we
1228                         // tag the methods NoInlining to discourage
1229                         // the inliner.
1230                         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1231                         public static Assembly Leaf () {
1232                                 var a = Assembly.GetCallingAssembly ();
1233                                 sideEffect();
1234                                 return a;
1235                         }
1236
1237                         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1238                         public static Assembly DirectCall () {
1239                                 var a = Leaf();
1240                                 sideEffect();
1241                                 return a;
1242                         }
1243
1244                         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1245                         public static Assembly InvokeCall () {
1246                                 var ty = typeof (GetCallingAssemblyCallee);
1247                                 var mi = ty.GetMethod("Leaf");
1248                                 var o = mi.Invoke(null, null);
1249                                 sideEffect();
1250                                 return (Assembly)o;
1251                         }
1252                 }
1253
1254                 [Test]
1255                 public void GetCallingAssembly_Direct() {
1256                         var a = GetCallingAssemblyCallee.DirectCall ();
1257                         Assert.IsNotNull (a);
1258
1259                         Assert.AreEqual (GetType().Assembly, a);
1260                 }
1261
1262                 [Test]
1263                 public void GetCallingAssembly_SkipsReflection () {
1264                         // check that the calling assembly is this
1265                         // one, not mscorlib (aka, the reflection
1266                         // API).
1267                         var a = GetCallingAssemblyCallee.InvokeCall ();
1268                         Assert.IsNotNull (a);
1269
1270                         var invokeAssembly =
1271                                 typeof (MethodInfo).Assembly;
1272                         Assert.AreNotEqual (invokeAssembly, a);
1273
1274                         Assert.AreEqual (GetType().Assembly, a);
1275                 }
1276
1277                 [Test]
1278                 public void DefinedTypes_Equality ()
1279                 {
1280                         var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1281                         var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
1282
1283                         Assert.AreSame (x1, x2, "#1");
1284                 }
1285
1286                 class MyAssembly : Assembly { }
1287
1288                 [Test]
1289                 public void CustomAssemblyImplThrows ()
1290                 {
1291                         var ma = new MyAssembly();
1292                         try {
1293                                 ma.GetName ();
1294                                 Assert.Fail ("must throw");
1295                         } catch (NotImplementedException){
1296                         }
1297                 }
1298         }
1299
1300         public class TestDefinedTypes
1301         {
1302         }
1303 }