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