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