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