85972ebafcb80edf5ed0f070e21f8c2975c0bee7
[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                 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
159                 [ExpectedException (typeof (ArgumentNullException))]
160                 public void GetFile_Null ()
161                 {
162                         Assembly.GetExecutingAssembly ().GetFile (null);
163                 }
164
165                 [Test]
166                 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
167                 [ExpectedException (typeof (ArgumentException))]
168                 public void GetFile_Empty ()
169                 {
170                         Assembly.GetExecutingAssembly ().GetFile (String.Empty);
171                 }
172
173                 [Test]
174                 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
175                 public void GetFiles_False ()
176                 {
177                         Assembly corlib = typeof (int).Assembly;
178                         FileStream[] fss = corlib.GetFiles ();
179                         Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
180
181                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
182                         fss = corlib_test.GetFiles ();
183                         Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
184                 }
185
186                 [Test]
187                 [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
188                 public void GetFiles_True ()
189                 {
190                         Assembly corlib = typeof (int).Assembly;
191                         FileStream[] fss = corlib.GetFiles ();
192                         Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
193
194                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
195                         fss = corlib_test.GetFiles ();
196                         Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
197                 }
198
199                 [Test] // bug #78517
200 #if ONLY_1_1
201                 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
202 #endif
203                 public void LoadFrom_Empty_Assembly ()
204                 {
205                         string tempFile = Path.GetTempFileName ();
206
207                         try {
208                                 Assembly.LoadFrom (tempFile);
209                                 Assert.Fail ("#1");
210                         } catch (BadImageFormatException ex) {
211                                 Assert.IsNull (ex.InnerException, "#2");
212                         } finally {
213                                 File.Delete (tempFile);
214                         }
215                 }
216
217                 [Test] // bug #78517
218                 public void LoadFrom_Invalid_Assembly ()
219                 {
220                         string tempFile = Path.GetTempFileName ();
221                         using (StreamWriter sw = File.CreateText (tempFile)) {
222                                 sw.WriteLine ("foo");
223                                 sw.Close ();
224                         }
225
226                         try {
227                                 Assembly.LoadFrom (tempFile);
228                                 Assert.Fail ("#1");
229                         } catch (BadImageFormatException ex) {
230                                 Assert.IsNull (ex.InnerException, "#2");
231                         } finally {
232                                 File.Delete (tempFile);
233                         }
234                 }
235
236                 [Test]
237                 public void LoadFrom_NonExisting_Assembly ()
238                 {
239                         string tempFile = Path.GetTempFileName ();
240                         File.Delete (tempFile);
241
242                         try {
243                                 Assembly.LoadFrom (tempFile);
244                                 Assert.Fail ("#1");
245                         } catch (FileNotFoundException ex) {
246                                 Assert.IsNull (ex.InnerException, "#2");
247                         } finally {
248                                 File.Delete (tempFile);
249                         }
250                 }
251
252                 [Test]
253                 public void LoadWithPartialName ()
254                 {
255                         string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
256
257                         foreach (string s in names)
258                                 if (Assembly.LoadWithPartialName (s) != null)
259                                         return;
260                         Assertion.Fail ("Was not able to load any corlib test");
261                 }
262
263 #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
264                 [Test]
265                 [ExpectedException (typeof (ArgumentNullException))]
266                 public void GetObjectData_Null ()
267                 {
268                         Assembly corlib = typeof (int).Assembly;
269                         corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
270                 }
271 #endif // TARGET_JVM
272
273                 [Test]
274                 public void GetReferencedAssemblies ()
275                 {
276                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
277                         AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
278                         foreach (AssemblyName an in names) {
279                                 Assert.IsNull (an.CodeBase, "CodeBase");
280                                 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
281                                 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
282                                 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
283                                 Assert.IsNotNull (an.FullName, "FullName");
284                                 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
285                                 Assert.IsNull (an.KeyPair, "KeyPair");
286                                 Assert.IsNotNull (an.Name, "Name");
287                                 Assert.IsNotNull (an.Version, "Version");
288                                 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, 
289                                         an.VersionCompatibility, "VersionCompatibility");
290                         }
291                 }
292
293 #if !TARGET_JVM // Reflection.Emit is not supported.
294                 [Test]
295                 public void Location_Empty() {
296                         string assemblyFileName = Path.Combine (
297                                 Path.GetTempPath (), "AssemblyLocation.dll");
298
299                         try {
300                                 AssemblyName assemblyName = new AssemblyName ();
301                                 assemblyName.Name = "AssemblyLocation";
302
303                                 AssemblyBuilder ab = AppDomain.CurrentDomain
304                                         .DefineDynamicAssembly (assemblyName,
305                                         AssemblyBuilderAccess.Save,
306                                         Path.GetTempPath (),
307                                         AppDomain.CurrentDomain.Evidence);
308                                 ab.Save (Path.GetFileName (assemblyFileName));
309
310                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
311                                         byte[] buffer = new byte[fs.Length];
312                                         fs.Read (buffer, 0, buffer.Length);
313                                         Assembly assembly = Assembly.Load (buffer);
314                                         Assert.AreEqual (string.Empty, assembly.Location);
315                                         fs.Close ();
316                                 }
317                         } finally {
318                                 File.Delete (assemblyFileName);
319                         }
320                 }
321
322                 [Test]
323                 [Category ("NotWorking")]
324                 public void bug78464 ()
325                 {
326                         string assemblyFileName = Path.Combine (
327                                 Path.GetTempPath (), "bug78464.dll");
328
329                         try {
330                                 // execute test in separate appdomain to allow assembly to be unloaded
331                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
332                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
333                                 try {
334                                         crossDomainTester.bug78464 (assemblyFileName);
335                                 } finally {
336                                         AppDomain.Unload (testDomain);
337                                 }
338                         } finally {
339                                 File.Delete (assemblyFileName);
340                         }
341                 }
342
343                 [Test]
344                 [Category ("NotWorking")]
345                 public void bug78465 ()
346                 {
347                         string assemblyFileName = Path.Combine (
348                                 Path.GetTempPath (), "bug78465.dll");
349
350                         try {
351                                 AssemblyName assemblyName = new AssemblyName ();
352                                 assemblyName.Name = "bug78465";
353
354                                 AssemblyBuilder ab = AppDomain.CurrentDomain
355                                         .DefineDynamicAssembly (assemblyName,
356                                         AssemblyBuilderAccess.Save,
357                                         Path.GetDirectoryName (assemblyFileName),
358                                         AppDomain.CurrentDomain.Evidence);
359                                 ab.Save (Path.GetFileName (assemblyFileName));
360
361                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
362                                         byte[] buffer = new byte[fs.Length];
363                                         fs.Read (buffer, 0, buffer.Length);
364                                         Assembly assembly = Assembly.Load (buffer);
365                                         Assert.AreEqual (string.Empty, assembly.Location, "#1");
366                                         fs.Close ();
367                                 }
368
369                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
370                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
371                                 try {
372                                         crossDomainTester.bug78465 (assemblyFileName);
373                                 } finally {
374                                         AppDomain.Unload (testDomain);
375                                 }
376                         } finally {
377                                 File.Delete (assemblyFileName);
378                         }
379                 }
380
381                 [Test]
382                 public void bug78468 ()
383                 {
384                         string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
385                                 "bug78468a.dll");
386                         string resourceFileName = Path.Combine (Path.GetTempPath (),
387                                 "readme.txt");
388
389                         using (StreamWriter sw = File.CreateText (resourceFileName)) {
390                                 sw.WriteLine ("FOO");
391                                 sw.Close ();
392                         }
393
394                         try {
395                                 AssemblyName assemblyName = new AssemblyName ();
396                                 assemblyName.Name = "bug78468a";
397
398                                 AssemblyBuilder ab = AppDomain.CurrentDomain
399                                         .DefineDynamicAssembly (assemblyName,
400                                         AssemblyBuilderAccess.Save,
401                                         Path.GetTempPath (),
402                                         AppDomain.CurrentDomain.Evidence);
403                                 ab.AddResourceFile ("read", "readme.txt");
404                                 ab.Save (Path.GetFileName (assemblyFileNameA));
405
406                                 Assembly assembly;
407
408                                 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
409                                         byte[] buffer = new byte[fs.Length];
410                                         fs.Read (buffer, 0, buffer.Length);
411                                         assembly = Assembly.Load (buffer);
412                                         fs.Close ();
413                                 }
414
415                                 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
416                                 string[] resNames = assembly.GetManifestResourceNames ();
417                                 Assert.IsNotNull (resNames, "#A2");
418                                 Assert.AreEqual (1, resNames.Length, "#A3");
419                                 Assert.AreEqual ("read", resNames[0], "#A4");
420                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
421                                 Assert.IsNotNull (resInfo, "#A5");
422                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
423                                 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
424                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
425 #if NET_2_0
426                                 try {
427                                         assembly.GetManifestResourceStream ("read");
428                                         Assert.Fail ("#A9");
429                                 } catch (FileNotFoundException) {
430                                 }
431 #else
432                                 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
433 #endif
434                                 try {
435                                         assembly.GetFile ("readme.txt");
436                                         Assert.Fail ("#A10");
437                                 } catch (FileNotFoundException) {
438                                 }
439
440                                 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
441                                         "bug78468b.dll");
442
443                                 AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
444                                 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
445                                 try {
446                                         crossDomainTester.bug78468 (assemblyFileNameB);
447                                 } finally {
448                                         AppDomain.Unload (testDomain);
449                                         File.Delete (assemblyFileNameB);
450                                 }
451                         } finally {
452                                 File.Delete (assemblyFileNameA);
453                                 File.Delete (resourceFileName);
454                         }
455                 }
456
457 #if NET_2_0
458                 [Test]
459                 [Category ("NotWorking")]
460                 public void ReflectionOnlyLoad ()
461                 {
462                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
463                         
464                         Assert.IsNotNull (assembly);
465                         Assert.IsTrue (assembly.ReflectionOnly);
466                 }
467
468                 [Test]
469                 public void ReflectionOnlyLoadFrom ()
470                 {
471                         string loc = typeof (AssemblyTest).Assembly.Location;
472                         string filename = Path.GetFileName (loc);
473                         Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
474
475                         Assert.IsNotNull (assembly);
476                         Assert.IsTrue (assembly.ReflectionOnly);
477                 }
478
479                 [Test]
480                 [ExpectedException (typeof (ArgumentException))]
481                 public void CreateInstanceOnRefOnly ()
482                 {
483                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
484                         assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
485                 }
486 #endif
487
488                 [Test]
489                 [Category ("NotWorking")] // patch for bug #79720 must be committed first
490                 public void Load_Culture ()
491                 {
492                         string tempDir = Path.Combine (Path.GetTempPath (),
493                                 "MonoTests.System.Reflection.AssemblyTest");
494                         string cultureTempDir = Path.Combine (tempDir, "nl-BE");
495                         if (!Directory.Exists (cultureTempDir))
496                                 Directory.CreateDirectory (cultureTempDir);
497                         cultureTempDir = Path.Combine (tempDir, "en-US");
498                         if (!Directory.Exists (cultureTempDir))
499                                 Directory.CreateDirectory (cultureTempDir);
500
501
502                         AppDomain ad = CreateTestDomain (tempDir, true);
503                         try {
504                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
505
506                                 // PART A
507
508                                 AssemblyName aname = new AssemblyName ();
509                                 aname.Name = "culturea";
510                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
511
512                                 aname = new AssemblyName ();
513                                 aname.Name = "culturea";
514                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
515
516                                 aname = new AssemblyName ();
517                                 aname.Name = "culturea";
518                                 aname.CultureInfo = new CultureInfo ("nl-BE");
519                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
520
521                                 aname = new AssemblyName ();
522                                 aname.Name = "culturea";
523                                 aname.CultureInfo = CultureInfo.InvariantCulture;
524                                 Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
525
526                                 // PART B
527
528                                 aname = new AssemblyName ();
529                                 aname.Name = "cultureb";
530                                 aname.CultureInfo = new CultureInfo ("nl-BE");
531                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
532
533                                 aname = new AssemblyName ();
534                                 aname.Name = "cultureb";
535                                 aname.CultureInfo = new CultureInfo ("nl-BE");
536                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
537
538                                 aname = new AssemblyName ();
539                                 aname.Name = "cultureb";
540                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
541
542                                 aname = new AssemblyName ();
543                                 aname.Name = "cultureb";
544                                 aname.CultureInfo = new CultureInfo ("en-US");
545                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
546
547                                 // PART C
548
549                                 aname = new AssemblyName ();
550                                 aname.Name = "culturec";
551                                 aname.CultureInfo = new CultureInfo ("nl-BE");
552                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
553
554                                 aname = new AssemblyName ();
555                                 aname.Name = "culturec";
556                                 aname.CultureInfo = new CultureInfo ("nl-BE");
557                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
558
559                                 aname = new AssemblyName ();
560                                 aname.Name = "culturec";
561                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
562
563                                 aname = new AssemblyName ();
564                                 aname.Name = "culturec";
565                                 aname.CultureInfo = CultureInfo.InvariantCulture;
566                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
567
568                                 // PART D
569
570                                 aname = new AssemblyName ();
571                                 aname.Name = "cultured";
572                                 aname.CultureInfo = new CultureInfo ("nl-BE");
573                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
574
575                                 aname = new AssemblyName ();
576                                 aname.Name = "cultured";
577                                 aname.CultureInfo = new CultureInfo ("nl-BE");
578                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
579
580                                 aname = new AssemblyName ();
581                                 aname.Name = "cultured";
582                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
583
584                                 aname = new AssemblyName ();
585                                 aname.Name = "cultured";
586                                 aname.CultureInfo = CultureInfo.InvariantCulture;
587                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
588                         } finally {
589                                 AppDomain.Unload (ad);
590                                 if (Directory.Exists (tempDir))
591                                         Directory.Delete (tempDir, true);
592                         }
593                 }
594
595                 [Test] // bug #79712
596 #if NET_2_0
597                 [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
598 #else
599                 [Category ("NotWorking")]
600 #endif
601                 public void Load_Culture_Mismatch ()
602                 {
603                         string tempDir = Path.Combine (Path.GetTempPath (),
604                                 "MonoTests.System.Reflection.AssemblyTest");
605                         string cultureTempDir = Path.Combine (tempDir, "en-US");
606                         if (!Directory.Exists (cultureTempDir))
607                                 Directory.CreateDirectory (cultureTempDir);
608
609                         AppDomain ad = CreateTestDomain (tempDir, true);
610                         try {
611                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
612
613                                 // PART A
614
615                                 AssemblyName aname = new AssemblyName ();
616                                 aname.Name = "bug79712a";
617                                 aname.CultureInfo = new CultureInfo ("nl-BE");
618                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
619
620                                 aname = new AssemblyName ();
621                                 aname.Name = "bug79712a";
622                                 aname.CultureInfo = CultureInfo.InvariantCulture;
623 #if NET_2_0
624                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
625 #else
626                                 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
627 #endif
628
629                                 // PART B
630
631                                 aname = new AssemblyName ();
632                                 aname.Name = "bug79712b";
633                                 aname.CultureInfo = new CultureInfo ("nl-BE");
634                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
635
636                                 aname = new AssemblyName ();
637                                 aname.Name = "bug79712b";
638                                 aname.CultureInfo = new CultureInfo ("en-US");
639 #if NET_2_0
640                                 Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
641 #else
642                                 Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
643 #endif
644                         } finally {
645                                 AppDomain.Unload (ad);
646                                 if (Directory.Exists (tempDir))
647                                         Directory.Delete (tempDir, true);
648                         }
649                 }
650
651                 [Test] // bug #79715
652                 public void Load_PartialVersion ()
653                 {
654                         string tempDir = Path.Combine (Path.GetTempPath (),
655                                 "MonoTests.System.Reflection.AssemblyTest");
656                         if (!Directory.Exists (tempDir))
657                                 Directory.CreateDirectory (tempDir);
658
659                         AppDomain ad = CreateTestDomain (tempDir, true);
660                         try {
661                                 CrossDomainTester cdt = CreateCrossDomainTester (ad);
662
663                                 AssemblyName aname = new AssemblyName ();
664                                 aname.Name = "bug79715";
665                                 aname.Version = new Version (1, 2, 3, 4);
666                                 cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
667
668                                 aname = new AssemblyName ();
669                                 aname.Name = "bug79715";
670                                 aname.Version = new Version (1, 2);
671                                 Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
672                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
673
674                                 aname = new AssemblyName ();
675                                 aname.Name = "bug79715";
676                                 aname.Version = new Version (1, 2, 3);
677                                 Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
678                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
679
680                                 aname = new AssemblyName ();
681                                 aname.Name = "bug79715";
682                                 aname.Version = new Version (1, 2, 3, 4);
683                                 Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
684                                 Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
685                         } finally {
686                                 AppDomain.Unload (ad);
687                                 if (Directory.Exists (tempDir))
688                                         Directory.Delete (tempDir, true);
689                         }
690                 }
691
692                 private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
693                 {
694                         AppDomainSetup setup = new AppDomainSetup ();
695                         setup.ApplicationBase = baseDirectory;
696                         setup.ApplicationName = "testdomain";
697
698                         AppDomain ad = AppDomain.CreateDomain ("testdomain", 
699                                 AppDomain.CurrentDomain.Evidence, setup);
700
701                         if (assemblyResolver) {
702                                 Assembly ea = Assembly.GetExecutingAssembly ();
703                                 ad.CreateInstanceFrom (ea.CodeBase,
704                                         typeof (AssemblyResolveHandler).FullName,
705                                         false,
706                                         BindingFlags.Public | BindingFlags.Instance,
707                                         null,
708                                         new object [] { ea.Location, ea.FullName },
709                                         CultureInfo.InvariantCulture,
710                                         null,
711                                         null);
712                         }
713
714                         return ad;
715                 }
716
717                 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
718                 {
719                         Type testerType = typeof (CrossDomainTester);
720                         return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
721                                 testerType.Assembly.FullName, testerType.FullName, false,
722                                 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
723                                 CultureInfo.InvariantCulture, new object[0], null);
724                 }
725
726                 private class CrossDomainTester : MarshalByRefObject
727                 {
728                         public void GenerateAssembly (AssemblyName aname, string path)
729                         {
730                                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
731                                         aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
732                                 ab.Save (Path.GetFileName (path));
733                         }
734
735                         public void Load (AssemblyName assemblyRef)
736                         {
737                                 Assembly.Load (assemblyRef);
738                         }
739
740                         public void LoadFrom (string assemblyFile)
741                         {
742                                 Assembly.LoadFrom (assemblyFile);
743                         }
744
745                         public bool AssertLoad (AssemblyName assemblyRef)
746                         {
747                                 try {
748                                         Assembly.Load (assemblyRef);
749                                         return true;
750                                 } catch {
751                                         return false;
752                                 }
753                         }
754
755                         public bool AssertLoad (string assemblyString)
756                         {
757                                 try {
758                                         Assembly.Load (assemblyString);
759                                         return true;
760                                 } catch {
761                                         return false;
762                                 }
763                         }
764
765                         public bool AssertFileLoadException (AssemblyName assemblyRef)
766                         {
767                                 try {
768                                         Assembly.Load (assemblyRef);
769                                         return false;
770                                 } catch (FileLoadException) {
771                                         return true;
772                                 }
773                         }
774
775                         public bool AssertFileNotFoundException (AssemblyName assemblyRef)
776                         {
777                                 try {
778                                         Assembly.Load (assemblyRef);
779                                         return false;
780                                 } catch (FileNotFoundException) {
781                                         return true;
782                                 }
783                         }
784
785                         public void bug78464 (string assemblyFileName)
786                         {
787                                 AssemblyName assemblyName = new AssemblyName ();
788                                 assemblyName.Name = "bug78464";
789
790                                 AssemblyBuilder ab = AppDomain.CurrentDomain
791                                         .DefineDynamicAssembly (assemblyName,
792                                         AssemblyBuilderAccess.Save,
793                                         Path.GetDirectoryName (assemblyFileName),
794                                         AppDomain.CurrentDomain.Evidence);
795                                 ab.Save (Path.GetFileName (assemblyFileName));
796
797                                 Assembly assembly;
798
799                                 using (FileStream fs = File.OpenRead (assemblyFileName)) {
800                                         byte[] buffer = new byte[fs.Length];
801                                         fs.Read (buffer, 0, buffer.Length);
802                                         assembly = Assembly.Load (buffer);
803                                         fs.Close ();
804                                 }
805
806                                 Assert.AreEqual (string.Empty, assembly.Location, "#1");
807
808                                 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
809                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
810                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
811                                 // note: we cannot check if directory names match, as MS.NET seems to 
812                                 // convert directory part of assembly location to lowercase
813                                 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
814                         }
815
816                         public void bug78465 (string assemblyFileName)
817                         {
818                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
819                                 Assert.IsFalse (assembly.Location == string.Empty, "#2");
820                                 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
821                                 // note: we cannot check if directory names match, as MS.NET seems to 
822                                 // convert directory part of assembly location to lowercase
823                                 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
824                         }
825
826                         public void bug78468 (string assemblyFileName)
827                         {
828                                 AssemblyName assemblyName = new AssemblyName ();
829                                 assemblyName.Name = "bug78468b";
830
831                                 AssemblyBuilder ab = AppDomain.CurrentDomain
832                                         .DefineDynamicAssembly (assemblyName,
833                                         AssemblyBuilderAccess.Save,
834                                         Path.GetDirectoryName (assemblyFileName),
835                                         AppDomain.CurrentDomain.Evidence);
836                                 ab.AddResourceFile ("read", "readme.txt");
837                                 ab.Save (Path.GetFileName (assemblyFileName));
838
839                                 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
840                                 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
841                                 string[] resNames = assembly.GetManifestResourceNames ();
842                                 Assert.IsNotNull (resNames, "#B2");
843                                 Assert.AreEqual (1, resNames.Length, "#B3");
844                                 Assert.AreEqual ("read", resNames[0], "#B4");
845                                 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
846                                 Assert.IsNotNull (resInfo, "#B5");
847                                 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
848                                 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
849                                 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
850                                 Stream s = assembly.GetManifestResourceStream ("read");
851                                 Assert.IsNotNull (s, "#B9");
852                                 s.Close ();
853                                 s = assembly.GetFile ("readme.txt");
854                                 Assert.IsNotNull (s, "#B10");
855                                 s.Close ();
856                         }
857                 }
858
859                 [Test]
860                 public void bug79872 ()
861                 {
862                         Random rnd = new Random ();
863                         string outdir;
864                         int tries = 0;
865
866                 retry:
867                         outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
868                         if (Directory.Exists (outdir)) {
869                                 try {
870                                         Directory.Delete (outdir, true);
871                                 } catch {
872                                         if (++tries <= 100)
873                                                 goto retry;
874                                 }
875                         }
876
877                         Directory.CreateDirectory (outdir);
878
879                         AssemblyName an = new AssemblyName ();
880                         an.Name = "bug79872";
881                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
882                         string dllname = "bug79872.dll";
883                         ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
884                         string netmodule = "bug79872.netmodule";
885                         ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
886                         TypeBuilder a1 = mb2.DefineType ("A");
887                         a1.CreateType ();
888                         ab.Save (dllname);
889
890                         bool ok = true;
891                         try {
892                                 Assembly.LoadFrom (Path.Combine (outdir, dllname));
893                         } catch {
894                                 ok = false;
895                         }
896                         Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
897
898                         ok = false;
899                         try {
900                                 Assembly.LoadFrom (Path.Combine (outdir, netmodule));
901                         } catch (BadImageFormatException) {
902                                 ok = true; // mono and .net 2.0 throw this
903                         } catch (FileLoadException) {
904                                 ok = true; // .net 1.1 throws this
905                         } catch {
906                                 // swallow the rest
907                         }
908                         Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
909
910                         Directory.Delete (outdir, true);
911                 }
912 #endif // TARGET_JVM
913
914                 [Serializable ()]
915                 private class AssemblyResolveHandler
916                 {
917                         public AssemblyResolveHandler (string assemblyFile, string assemblyName)
918                         {
919                                 _assemblyFile = assemblyFile;
920                                 _assemblyName = assemblyName;
921
922                                 AppDomain.CurrentDomain.AssemblyResolve +=
923                                         new ResolveEventHandler (ResolveAssembly);
924                         }
925
926                         private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
927                         {
928                                 if (args.Name == _assemblyName)
929                                         return Assembly.LoadFrom (_assemblyFile);
930
931                                 return null;
932                         }
933
934                         private readonly string _assemblyFile;
935                         private readonly string _assemblyName;
936                 }
937         }
938 }