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