remove TARGET_JVM
[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 //
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.Configuration.Assemblies;
35 using System.Globalization;
36 using System.IO;
37 using System.Reflection;
38 #if !TARGET_JVM
39 using System.Reflection.Emit;
40 #endif
41 using System.Runtime.Serialization;
42 using System.Security;
43
44 namespace MonoTests.System.Reflection
45 {
46         [TestFixture]
47         public class AssemblyTest
48         {
49                 [Test] 
50                 public void CreateInstance() 
51                 {
52                         Type type = typeof (AssemblyTest);
53                         Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
54                         Assert.IsNotNull (obj, "#01");
55                         Assert.AreEqual (GetType (), obj.GetType (), "#02");
56                 }
57
58                 [Test] 
59                 public void CreateInvalidInstance() 
60                 {
61                         Type type = typeof (AssemblyTest);
62                         Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
63                         Assert.IsNull (obj, "#03");
64                 }
65
66                 [Test]
67 #if NET_2_0
68                 [Category ("NotWorking")]
69                 [ExpectedException (typeof (ArgumentException))]
70 #else
71                 [ExpectedException (typeof (TypeLoadException))]
72 #endif
73                 public void TestGetType () 
74                 {
75                         // Bug #49114
76                         typeof (int).Assembly.GetType ("&blabla", true, true);
77                 }
78
79                 [Test]
80                 public void GetEntryAssembly ()
81                 {
82                         // note: only available in default appdomain
83                         // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
84                         // Not sure we should emulate this behavior.
85                         string fname = AppDomain.CurrentDomain.FriendlyName;
86                         if (fname.EndsWith (".dll")) { // nunit-console
87                                 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
88 #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
89                                 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
90 #endif
91                         } else { // gnunit
92                                 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
93 #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
94                                 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
95 #endif
96                         }
97                 }
98
99 #if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
100 #if NET_2_0
101                 [Category ("NotWorking")]
102 #endif
103                 [Test]
104                 public void Corlib () 
105                 {
106                         Assembly corlib = typeof (int).Assembly;
107                         Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
108                         Assert.IsNull (corlib.EntryPoint, "EntryPoint");
109                         Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
110                         Assert.IsNotNull (corlib.Evidence, "Evidence");
111                         Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
112
113                         // corlib doesn't reference anything
114                         Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
115 #if NET_2_0
116                         Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
117                         // not really "true" but it's even more trusted so...
118                         Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
119                         Assert.AreEqual (0, corlib.HostContext, "HostContext");
120                         Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
121                         Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
122                         Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
123 #elif NET_1_1
124                         Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
125                         Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
126                         Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
127 #endif
128                 }
129
130                 [Test]
131                 public void Corlib_test ()
132                 {
133                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
134                         Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
135                         Assert.IsNotNull (corlib_test.Evidence, "Evidence");
136                         Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
137
138                         Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
139 #if NET_2_0
140                         Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
141                         Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
142                         Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
143                         Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
144 #elif NET_1_1
145                         Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
146 #endif
147                 }
148 #endif
149
150                 [Test]
151                 public void GetAssembly ()
152                 {
153                         Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
154                         Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
155                 }
156
157                 [Test]
158                 [ExpectedException (typeof (ArgumentNullException))]
159                 public void GetFile_Null ()
160                 {
161                         Assembly.GetExecutingAssembly ().GetFile (null);
162                 }
163
164                 [Test]
165                 [ExpectedException (typeof (ArgumentException))]
166                 public void GetFile_Empty ()
167                 {
168                         Assembly.GetExecutingAssembly ().GetFile (String.Empty);
169                 }
170
171                 [Test]
172                 public void GetFiles_False ()
173                 {
174                         Assembly corlib = typeof (int).Assembly;
175                         FileStream[] fss = corlib.GetFiles ();
176                         Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
177
178                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
179                         fss = corlib_test.GetFiles ();
180                         Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
181                 }
182
183                 [Test]
184                 public void GetFiles_True ()
185                 {
186                         Assembly corlib = typeof (int).Assembly;
187                         FileStream[] fss = corlib.GetFiles ();
188                         Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
189
190                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
191                         fss = corlib_test.GetFiles ();
192                         Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
193                 }
194
195                 [Test] // bug #78517
196 #if ONLY_1_1
197                 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
198 #endif
199                 public void LoadFrom_Empty_Assembly ()
200                 {
201                         string tempFile = Path.GetTempFileName ();
202
203                         try {
204                                 Assembly.LoadFrom (tempFile);
205                                 Assert.Fail ("#1");
206                         } catch (BadImageFormatException ex) {
207                                 Assert.IsNull (ex.InnerException, "#2");
208                         } finally {
209                                 File.Delete (tempFile);
210                         }
211                 }
212
213                 [Test] // bug #78517
214                 public void LoadFrom_Invalid_Assembly ()
215                 {
216                         string tempFile = Path.GetTempFileName ();
217                         using (StreamWriter sw = File.CreateText (tempFile)) {
218                                 sw.WriteLine ("foo");
219                                 sw.Close ();
220                         }
221
222                         try {
223                                 Assembly.LoadFrom (tempFile);
224                                 Assert.Fail ("#1");
225                         } catch (BadImageFormatException ex) {
226                                 Assert.IsNull (ex.InnerException, "#2");
227                         } finally {
228                                 File.Delete (tempFile);
229                         }
230                 }
231
232                 [Test]
233                 public void LoadFrom_NonExisting_Assembly ()
234                 {
235                         string tempFile = Path.GetTempFileName ();
236                         File.Delete (tempFile);
237
238                         try {
239                                 Assembly.LoadFrom (tempFile);
240                                 Assert.Fail ("#1");
241                         } catch (FileNotFoundException ex) {
242                                 Assert.IsNull (ex.InnerException, "#2");
243                         } finally {
244                                 File.Delete (tempFile);
245                         }
246                 }
247
248                 [Test]
249                 public void LoadWithPartialName ()
250                 {
251                         string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
252
253                         foreach (string s in names)
254                                 if (Assembly.LoadWithPartialName (s) != null)
255                                         return;
256                         Assertion.Fail ("Was not able to load any corlib test");
257                 }
258
259 #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
260                 [Test]
261                 [ExpectedException (typeof (ArgumentNullException))]
262                 public void GetObjectData_Null ()
263                 {
264                         Assembly corlib = typeof (int).Assembly;
265                         corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
266                 }
267 #endif // TARGET_JVM
268
269                 [Test]
270                 public void GetReferencedAssemblies ()
271                 {
272                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
273                         AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
274                         foreach (AssemblyName an in names) {
275                                 Assert.IsNull (an.CodeBase, "CodeBase");
276                                 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
277                                 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
278                                 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
279                                 Assert.IsNotNull (an.FullName, "FullName");
280                                 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
281                                 Assert.IsNull (an.KeyPair, "KeyPair");
282                                 Assert.IsNotNull (an.Name, "Name");
283                                 Assert.IsNotNull (an.Version, "Version");
284                                 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, 
285                                         an.VersionCompatibility, "VersionCompatibility");
286                         }
287                 }
288
289 #if !TARGET_JVM // Reflection.Emit is not supported.
290                 [Test]
291                 public void Location_Empty() {
292                         string assemblyFileName = Path.Combine (
293                                 Path.GetTempPath (), "AssemblyLocation.dll");
294
295                         try {
296                                 AssemblyName assemblyName = new AssemblyName ();
297                                 assemblyName.Name = "AssemblyLocation";
298
299                                 AssemblyBuilder ab = AppDomain.CurrentDomain
300                                         .DefineDynamicAssembly (assemblyName,
301                                         AssemblyBuilderAccess.Save,
302                                         Path.GetTempPath (),
303                                         AppDomain.CurrentDomain.Evidence);
304                                 ab.Save (Path.GetFileName (assemblyFileName));
305
306                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
307                                         byte[] buffer = new byte[fs.Length];
308                                         fs.Read (buffer, 0, buffer.Length);
309                                         Assembly assembly = Assembly.Load (buffer);
310                                         Assert.AreEqual (string.Empty, assembly.Location);
311                                         fs.Close ();
312                                 }
313                         } finally {
314                                 File.Delete (assemblyFileName);
315                         }
316                 }
317
318                 [Test]
319                 [Category ("NotWorking")]
320                 public void bug78464 ()
321                 {
322                         string assemblyFileName = Path.Combine (
323                                 Path.GetTempPath (), "bug78464.dll");
324
325                         try {
326                                 // execute test in separate appdomain to allow assembly to be unloaded
327                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
328                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
329                                 try {
330                                         crossDomainTester.bug78464 (assemblyFileName);
331                                 } finally {
332                                         AppDomain.Unload (testDomain);
333                                 }
334                         } finally {
335                                 File.Delete (assemblyFileName);
336                         }
337                 }
338
339                 [Test]
340                 [Category ("NotWorking")]
341                 public void bug78465 ()
342                 {
343                         string assemblyFileName = Path.Combine (
344                                 Path.GetTempPath (), "bug78465.dll");
345
346                         try {
347                                 AssemblyName assemblyName = new AssemblyName ();
348                                 assemblyName.Name = "bug78465";
349
350                                 AssemblyBuilder ab = AppDomain.CurrentDomain
351                                         .DefineDynamicAssembly (assemblyName,
352                                         AssemblyBuilderAccess.Save,
353                                         Path.GetDirectoryName (assemblyFileName),
354                                         AppDomain.CurrentDomain.Evidence);
355                                 ab.Save (Path.GetFileName (assemblyFileName));
356
357                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
358                                         byte[] buffer = new byte[fs.Length];
359                                         fs.Read (buffer, 0, buffer.Length);
360                                         Assembly assembly = Assembly.Load (buffer);
361                                         Assert.AreEqual (string.Empty, assembly.Location, "#1");
362                                         fs.Close ();
363                                 }
364
365                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
366                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
367                                 try {
368                                         crossDomainTester.bug78465 (assemblyFileName);
369                                 } finally {
370                                         AppDomain.Unload (testDomain);
371                                 }
372                         } finally {
373                                 File.Delete (assemblyFileName);
374                         }
375                 }
376
377                 [Test]
378                 public void bug78468 ()
379                 {
380                         string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
381                                 "bug78468a.dll");
382                         string resourceFileName = Path.Combine (Path.GetTempPath (),
383                                 "readme.txt");
384
385                         using (StreamWriter sw = File.CreateText (resourceFileName)) {
386                                 sw.WriteLine ("FOO");
387                                 sw.Close ();
388                         }
389
390                         try {
391                                 AssemblyName assemblyName = new AssemblyName ();
392                                 assemblyName.Name = "bug78468a";
393
394                                 AssemblyBuilder ab = AppDomain.CurrentDomain
395                                         .DefineDynamicAssembly (assemblyName,
396                                         AssemblyBuilderAccess.Save,
397                                         Path.GetTempPath (),
398                                         AppDomain.CurrentDomain.Evidence);
399                                 ab.AddResourceFile ("read", "readme.txt");
400                                 ab.Save (Path.GetFileName (assemblyFileNameA));
401
402                                 Assembly assembly;
403
404                                 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
405                                         byte[] buffer = new byte[fs.Length];
406                                         fs.Read (buffer, 0, buffer.Length);
407                                         assembly = Assembly.Load (buffer);
408                                         fs.Close ();
409                                 }
410
411                                 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
412                                 string[] resNames = assembly.GetManifestResourceNames ();
413                                 Assert.IsNotNull (resNames, "#A2");
414                                 Assert.AreEqual (1, resNames.Length, "#A3");
415                                 Assert.AreEqual ("read", resNames[0], "#A4");
416                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
417                                 Assert.IsNotNull (resInfo, "#A5");
418                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
419                                 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
420                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
421 #if NET_2_0
422                                 try {
423                                         assembly.GetManifestResourceStream ("read");
424                                         Assert.Fail ("#A9");
425                                 } catch (FileNotFoundException) {
426                                 }
427 #else
428                                 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
429 #endif
430                                 try {
431                                         assembly.GetFile ("readme.txt");
432                                         Assert.Fail ("#A10");
433                                 } catch (FileNotFoundException) {
434                                 }
435
436                                 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
437                                         "bug78468b.dll");
438
439                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
440                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
441                                 try {
442                                         crossDomainTester.bug78468 (assemblyFileNameB);
443                                 } finally {
444                                         AppDomain.Unload (testDomain);
445                                         File.Delete (assemblyFileNameB);
446                                 }
447                         } finally {
448                                 File.Delete (assemblyFileNameA);
449                                 File.Delete (resourceFileName);
450                         }
451                 }
452
453 #if NET_2_0
454                 [Test]
455                 [Category ("NotWorking")]
456                 public void ReflectionOnlyLoad ()
457                 {
458                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
459                         
460                         Assert.IsNotNull (assembly);
461                         Assert.IsTrue (assembly.ReflectionOnly);
462                 }
463
464                 [Test]
465                 public void ReflectionOnlyLoadFrom ()
466                 {
467                         string loc = typeof (AssemblyTest).Assembly.Location;
468                         string filename = Path.GetFileName (loc);
469                         Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
470
471                         Assert.IsNotNull (assembly);
472                         Assert.IsTrue (assembly.ReflectionOnly);
473                 }
474
475                 [Test]
476                 [ExpectedException (typeof (ArgumentException))]
477                 public void CreateInstanceOnRefOnly ()
478                 {
479                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
480                         assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
481                 }
482 #endif
483
484                 [Test]
485                 [Category ("NotWorking")] // patch for bug #79720 must be committed first
486                 public void Load_Culture ()
487                 {
488                         string tempDir = Path.Combine (Path.GetTempPath (),
489                                 "MonoTests.System.Reflection.AssemblyTest");
490                         string cultureTempDir = Path.Combine (tempDir, "nl-BE");
491                         if (!Directory.Exists (cultureTempDir))
492                                 Directory.CreateDirectory (cultureTempDir);
493                         cultureTempDir = Path.Combine (tempDir, "en-US");
494                         if (!Directory.Exists (cultureTempDir))
495                                 Directory.CreateDirectory (cultureTempDir);
496
497
498                         AppDomain ad = CreateTestDomain (tempDir, true);
499                         try {
500                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
501
502                                 // PART A
503
504                                 AssemblyName aname = new AssemblyName ();
505                                 aname.Name = "culturea";
506                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
507
508                                 aname = new AssemblyName ();
509                                 aname.Name = "culturea";
510                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
511
512                                 aname = new AssemblyName ();
513                                 aname.Name = "culturea";
514                                 aname.CultureInfo = new CultureInfo ("nl-BE");
515                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
516
517                                 aname = new AssemblyName ();
518                                 aname.Name = "culturea";
519                                 aname.CultureInfo = CultureInfo.InvariantCulture;
520                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
521
522                                 // PART B
523
524                                 aname = new AssemblyName ();
525                                 aname.Name = "cultureb";
526                                 aname.CultureInfo = new CultureInfo ("nl-BE");
527                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
528
529                                 aname = new AssemblyName ();
530                                 aname.Name = "cultureb";
531                                 aname.CultureInfo = new CultureInfo ("nl-BE");
532                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
533
534                                 aname = new AssemblyName ();
535                                 aname.Name = "cultureb";
536                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
537
538                                 aname = new AssemblyName ();
539                                 aname.Name = "cultureb";
540                                 aname.CultureInfo = new CultureInfo ("en-US");
541                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
542
543                                 // PART C
544
545                                 aname = new AssemblyName ();
546                                 aname.Name = "culturec";
547                                 aname.CultureInfo = new CultureInfo ("nl-BE");
548                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
549
550                                 aname = new AssemblyName ();
551                                 aname.Name = "culturec";
552                                 aname.CultureInfo = new CultureInfo ("nl-BE");
553                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
554
555                                 aname = new AssemblyName ();
556                                 aname.Name = "culturec";
557                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
558
559                                 aname = new AssemblyName ();
560                                 aname.Name = "culturec";
561                                 aname.CultureInfo = CultureInfo.InvariantCulture;
562                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
563
564                                 // PART D
565
566                                 aname = new AssemblyName ();
567                                 aname.Name = "cultured";
568                                 aname.CultureInfo = new CultureInfo ("nl-BE");
569                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
570
571                                 aname = new AssemblyName ();
572                                 aname.Name = "cultured";
573                                 aname.CultureInfo = new CultureInfo ("nl-BE");
574                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
575
576                                 aname = new AssemblyName ();
577                                 aname.Name = "cultured";
578                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
579
580                                 aname = new AssemblyName ();
581                                 aname.Name = "cultured";
582                                 aname.CultureInfo = CultureInfo.InvariantCulture;
583                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
584                         } finally {
585                                 AppDomain.Unload (ad);
586                                 if (Directory.Exists (tempDir))
587                                         Directory.Delete (tempDir, true);
588                         }
589                 }
590
591                 [Test] // bug #79712
592 #if NET_2_0
593                 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
594 #else
595                 [Category ("NotWorking")]
596 #endif
597                 public void Load_Culture_Mismatch ()
598                 {
599                         string tempDir = Path.Combine (Path.GetTempPath (),
600                                 "MonoTests.System.Reflection.AssemblyTest");
601                         string cultureTempDir = Path.Combine (tempDir, "en-US");
602                         if (!Directory.Exists (cultureTempDir))
603                                 Directory.CreateDirectory (cultureTempDir);
604
605                         AppDomain ad = CreateTestDomain (tempDir, true);
606                         try {
607                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
608
609                                 // PART A
610
611                                 AssemblyName aname = new AssemblyName ();
612                                 aname.Name = "bug79712a";
613                                 aname.CultureInfo = new CultureInfo ("nl-BE");
614                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
615
616                                 aname = new AssemblyName ();
617                                 aname.Name = "bug79712a";
618                                 aname.CultureInfo = CultureInfo.InvariantCulture;
619 #if NET_2_0
620                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
621 #else
622                                 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
623 #endif
624
625                                 // PART B
626
627                                 aname = new AssemblyName ();
628                                 aname.Name = "bug79712b";
629                                 aname.CultureInfo = new CultureInfo ("nl-BE");
630                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
631
632                                 aname = new AssemblyName ();
633                                 aname.Name = "bug79712b";
634                                 aname.CultureInfo = new CultureInfo ("en-US");
635 #if NET_2_0
636                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
637 #else
638                                 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
639 #endif
640                         } finally {
641                                 AppDomain.Unload (ad);
642                                 if (Directory.Exists (tempDir))
643                                         Directory.Delete (tempDir, true);
644                         }
645                 }
646
647                 [Test] // bug #79715
648                 public void Load_PartialVersion ()
649                 {
650                         string tempDir = Path.Combine (Path.GetTempPath (),
651                                 "MonoTests.System.Reflection.AssemblyTest");
652                         if (!Directory.Exists (tempDir))
653                                 Directory.CreateDirectory (tempDir);
654
655                         AppDomain ad = CreateTestDomain (tempDir, true);
656                         try {
657                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
658
659                                 AssemblyName aname = new AssemblyName ();
660                                 aname.Name = "bug79715";
661                                 aname.Version = new Version (1, 2, 3, 4);
662                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
663
664                                 aname = new AssemblyName ();
665                                 aname.Name = "bug79715";
666                                 aname.Version = new Version (1, 2);
667                                 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
668                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
669
670                                 aname = new AssemblyName ();
671                                 aname.Name = "bug79715";
672                                 aname.Version = new Version (1, 2, 3);
673                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
674                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
675
676                                 aname = new AssemblyName ();
677                                 aname.Name = "bug79715";
678                                 aname.Version = new Version (1, 2, 3, 4);
679                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
680                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
681                         } finally {
682                                 AppDomain.Unload (ad);
683                                 if (Directory.Exists (tempDir))
684                                         Directory.Delete (tempDir, true);
685                         }
686                 }
687
688                 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
689                 {
690                         AppDomainSetup setup = new AppDomainSetup ();
691                         setup.ApplicationBase = baseDirectory;
692                         setup.ApplicationName = "testdomain";
693
694                         AppDomain ad = AppDomain.CreateDomain ("testdomain", 
695                                 AppDomain.CurrentDomain.Evidence, setup);
696
697                         if (assemblyResolver) {
698                                 Assembly ea = Assembly.GetExecutingAssembly ();
699                                 ad.CreateInstanceFrom (ea.CodeBase,
700                                         typeof (AssemblyResolveHandler).FullName,
701                                         false,
702                                         BindingFlags.Public | BindingFlags.Instance,
703                                         null,
704                                         new object [] { ea.Location, ea.FullName },
705                                         CultureInfo.InvariantCulture,
706                                         null,
707                                         null);
708                         }
709
710                         return ad;
711                 }
712
713                 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
714                 {
715                         Type testerType = typeof (CrossDomainTester);
716                         return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
717                                 testerType.Assembly.FullName, testerType.FullName, false,
718                                 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
719                                 CultureInfo.InvariantCulture, new object[0], null);
720                 }
721
722                 private class CrossDomainTester : MarshalByRefObject
723                 {
724                         public void GenerateAssembly (AssemblyName aname, string path)
725                         {
726                                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
727                                         aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
728                                 ab.Save (Path.GetFileName (path));
729                         }
730
731                         public void Load (AssemblyName assemblyRef)
732                         {
733                                 Assembly.Load (assemblyRef);
734                         }
735
736                         public void LoadFrom (string assemblyFile)
737                         {
738                                 Assembly.LoadFrom (assemblyFile);
739                         }
740
741                         public bool AssertLoad (AssemblyName assemblyRef)
742                         {
743                                 try {
744                                         Assembly.Load (assemblyRef);
745                                         return true;
746                                 } catch {
747                                         return false;
748                                 }
749                         }
750
751                         public bool AssertLoad (string assemblyString)
752                         {
753                                 try {
754                                         Assembly.Load (assemblyString);
755                                         return true;
756                                 } catch {
757                                         return false;
758                                 }
759                         }
760
761                         public bool AssertFileLoadException (AssemblyName assemblyRef)
762                         {
763                                 try {
764                                         Assembly.Load (assemblyRef);
765                                         return false;
766                                 } catch (FileLoadException) {
767                                         return true;
768                                 }
769                         }
770
771                         public bool AssertFileNotFoundException (AssemblyName assemblyRef)
772                         {
773                                 try {
774                                         Assembly.Load (assemblyRef);
775                                         return false;
776                                 } catch (FileNotFoundException) {
777                                         return true;
778                                 }
779                         }
780
781                         public void bug78464 (string assemblyFileName)
782                         {
783                                 AssemblyName assemblyName = new AssemblyName ();
784                                 assemblyName.Name = "bug78464";
785
786                                 AssemblyBuilder ab = AppDomain.CurrentDomain
787                                         .DefineDynamicAssembly (assemblyName,
788                                         AssemblyBuilderAccess.Save,
789                                         Path.GetDirectoryName (assemblyFileName),
790                                         AppDomain.CurrentDomain.Evidence);
791                                 ab.Save (Path.GetFileName (assemblyFileName));
792
793                                 Assembly assembly;
794
795                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
796                                         byte[] buffer = new byte[fs.Length];
797                                         fs.Read (buffer, 0, buffer.Length);
798                                         assembly = Assembly.Load (buffer);
799                                         fs.Close ();
800                                 }
801
802                                 Assert.AreEqual (string.Empty, assembly.Location, "#1");
803
804                                 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
805                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
806                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
807                                 // note: we cannot check if directory names match, as MS.NET seems to 
808                                 // convert directory part of assembly location to lowercase
809                                 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
810                         }
811
812                         public void bug78465 (string assemblyFileName)
813                         {
814                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
815                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
816                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
817                                 // note: we cannot check if directory names match, as MS.NET seems to 
818                                 // convert directory part of assembly location to lowercase
819                                 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
820                         }
821
822                         public void bug78468 (string assemblyFileName)
823                         {
824                                 AssemblyName assemblyName = new AssemblyName ();
825                                 assemblyName.Name = "bug78468b";
826
827                                 AssemblyBuilder ab = AppDomain.CurrentDomain
828                                         .DefineDynamicAssembly (assemblyName,
829                                         AssemblyBuilderAccess.Save,
830                                         Path.GetDirectoryName (assemblyFileName),
831                                         AppDomain.CurrentDomain.Evidence);
832                                 ab.AddResourceFile ("read", "readme.txt");
833                                 ab.Save (Path.GetFileName (assemblyFileName));
834
835                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
836                                 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
837                                 string[] resNames = assembly.GetManifestResourceNames ();
838                                 Assert.IsNotNull (resNames, "#B2");
839                                 Assert.AreEqual (1, resNames.Length, "#B3");
840                                 Assert.AreEqual ("read", resNames[0], "#B4");
841                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
842                                 Assert.IsNotNull (resInfo, "#B5");
843                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
844                                 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
845                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
846                                 Stream s = assembly.GetManifestResourceStream ("read");
847                                 Assert.IsNotNull (s, "#B9");
848                                 s.Close ();
849                                 s = assembly.GetFile ("readme.txt");
850                                 Assert.IsNotNull (s, "#B10");
851                                 s.Close ();
852                         }
853                 }
854
855                 [Test]
856                 public void bug79872 ()
857                 {
858                         Random rnd = new Random ();
859                         string outdir;
860                         int tries = 0;
861
862                 retry:
863                         outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
864                         if (Directory.Exists (outdir)) {
865                                 try {
866                                         Directory.Delete (outdir, true);
867                                 } catch {
868                                         if (++tries <= 100)
869                                                 goto retry;
870                                 }
871                         }
872
873                         Directory.CreateDirectory (outdir);
874
875                         AssemblyName an = new AssemblyName ();
876                         an.Name = "bug79872";
877                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
878                         string dllname = "bug79872.dll";
879                         ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
880                         string netmodule = "bug79872.netmodule";
881                         ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
882                         TypeBuilder a1 = mb2.DefineType ("A");
883                         a1.CreateType ();
884                         ab.Save (dllname);
885
886                         bool ok = true;
887                         try {
888                                 Assembly.LoadFrom (Path.Combine (outdir, dllname));
889                         } catch {
890                                 ok = false;
891                         }
892                         Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
893
894                         ok = false;
895                         try {
896                                 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
897                         } catch (BadImageFormatException) {
898                                 ok = true; // mono and .net 2.0 throw this
899                         } catch (FileLoadException) {
900                                 ok = true; // .net 1.1 throws this
901                         } catch {
902                                 // swallow the rest
903                         }
904                         Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
905
906                         Directory.Delete (outdir, true);
907                 }
908 #endif // TARGET_JVM
909
910                 [Serializable ()]
911                 private class AssemblyResolveHandler
912                 {
913                         public AssemblyResolveHandler (string assemblyFile, string assemblyName)
914                         {
915                                 _assemblyFile = assemblyFile;
916                                 _assemblyName = assemblyName;
917
918                                 AppDomain.CurrentDomain.AssemblyResolve +=
919                                         new ResolveEventHandler (ResolveAssembly);
920                         }
921
922                         private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
923                         {
924                                 if (args.Name == _assemblyName)
925                                         return Assembly.LoadFrom (_assemblyFile);
926
927                                 return null;
928                         }
929
930                         private readonly string _assemblyFile;
931                         private readonly string _assemblyName;
932                 }
933         }
934 }