Remove generated file dependency to make fresh checkout tests work out of box
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / AssemblyBuilderTest.cs
1 //
2 // AssemblyBuilderTest.cs - NUnit Test Cases for the AssemblyBuilder class
3 //
4 // Zoltan Varga (vargaz@freemail.hu)
5 // Andres G. Aragoneses (andres@7digital.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // (C) 7digital Media, Ltd. http://www.7digital.com
9 //
10 //
11
12
13 using System;
14 using System.Globalization;
15 using System.Threading;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.IO;
19 using System.Configuration.Assemblies;
20
21 using NUnit.Framework;
22
23 namespace MonoTests.System.Reflection.Emit
24 {
25
26 [TestFixture]
27 public class AssemblyBuilderTest
28 {       
29         [AttributeUsage (AttributeTargets.Assembly)]
30         public sealed class FooAttribute : Attribute
31         {
32                 public FooAttribute (string arg)
33                 {
34                 }
35
36                 public FooAttribute ()
37                 {
38                 }
39         }
40
41         static int nameIndex = 0;
42         static AppDomain domain;
43         static AssemblyBuilder ab;
44         static ModuleBuilder mb;
45         string tempDir = Path.Combine (Path.GetTempPath (), typeof (AssemblyBuilderTest).FullName);
46
47         [SetUp]
48         protected void SetUp ()
49         {
50                 if (Directory.Exists (tempDir))
51                         Directory.Delete (tempDir, true);
52
53                 Directory.CreateDirectory (tempDir);
54
55                 for (int i = 1; i < 3; ++i) {
56                         string resFile = Path.Combine (tempDir, "res" + i + ".txt");
57                         using (StreamWriter sw = new StreamWriter (resFile)) {
58                                 sw.WriteLine ("FOO");
59                         }
60                 }
61
62                 domain = Thread.GetDomain ();
63                 ab = genAssembly ();
64                 mb = ab.DefineDynamicModule ("def_module");
65         }
66
67         [TearDown]
68         protected void TearDown ()
69         {
70                 if (Directory.Exists (tempDir))
71                         Directory.Delete (tempDir, true);
72         }
73
74         private AssemblyName genAssemblyName ()
75         {
76                 AssemblyName assemblyName = new AssemblyName();
77                 assemblyName.Name = typeof (AssemblyBuilderTest).FullName + (nameIndex ++);
78                 return assemblyName;
79         }
80
81         private AssemblyBuilder genAssembly ()
82         {
83                 return domain.DefineDynamicAssembly (genAssemblyName (),
84                                                      AssemblyBuilderAccess.RunAndSave,
85                                                      tempDir);
86         }
87
88         private MethodInfo genEntryFunction (AssemblyBuilder assembly)
89         {
90                 ModuleBuilder module = assembly.DefineDynamicModule("module1");
91                 TypeBuilder tb = module.DefineType ("A");
92                 MethodBuilder mb = tb.DefineMethod ("A",
93                         MethodAttributes.Static, typeof (void), new Type [0]);
94                 mb.GetILGenerator ().Emit (OpCodes.Ret);
95                 return mb;
96         }
97
98 #if NET_2_0
99         [Test]
100         [Category ("NotWorking")]
101         public void ManifestModule ()
102         {
103                 AssemblyName aname = new AssemblyName ("ManifestModule1");
104                 ab = domain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.RunAndSave,
105                         tempDir);
106                 Assert.IsNotNull (ab.ManifestModule, "#A1");
107                 Assert.AreEqual (1, ab.GetModules ().Length, "#A2");
108                 Assert.AreEqual (typeof (ModuleBuilder), ab.ManifestModule.GetType (), "#A3");
109
110                 ModuleBuilder mb1 = (ModuleBuilder) ab.ManifestModule;
111                 Assert.AreSame (mb1, ab.GetModules () [0], "#B1");
112                 Assert.IsFalse (mb1.IsResource (), "#B2");
113                 Assert.AreSame (mb1, ab.ManifestModule, "#B3");
114
115                 ab.Save ("ManifestModule.dll");
116
117                 ModuleBuilder mb2 = (ModuleBuilder) ab.ManifestModule;
118                 Assert.AreSame (mb2, ab.GetModules () [0], "#C1");
119                 Assert.IsFalse (mb2.IsResource (), "#C2");
120                 Assert.AreSame (mb2, ab.ManifestModule, "#C3");
121                 Assert.AreSame (mb1, mb2, "#C4");
122         }
123 #endif
124
125         [Test]
126         [ExpectedException (typeof (NotSupportedException))]
127         public void TestCodeBase ()
128         {
129                 string codebase = ab.CodeBase;
130         }
131
132         [Test]
133         [ExpectedException (typeof (NotSupportedException))]
134         public void TestLocation ()
135         {
136                 string location = ab.Location;
137         }
138
139         [Test]
140         public void TestEntryPoint ()
141         {
142                 Assert.AreEqual (null, ab.EntryPoint, "EntryPoint defaults to null");
143
144                 MethodInfo mi = genEntryFunction (ab);
145                 ab.SetEntryPoint (mi);
146
147                 Assert.AreEqual (mi, ab.EntryPoint, "EntryPoint works");
148         }
149
150         [Test]
151         public void TestSetEntryPoint ()
152         {
153                 // Check invalid arguments
154                 try {
155                         ab.SetEntryPoint (null);
156                         Assert.Fail ("#A1");
157                 } catch (ArgumentNullException ex) {
158                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
159                         Assert.IsNull (ex.InnerException, "#A3");
160                         Assert.IsNotNull (ex.Message, "#A4");
161                         Assert.IsNotNull (ex.ParamName, "#A5");
162                         Assert.AreEqual ("entryMethod", ex.ParamName, "#A6");
163                 }
164
165                 // Check method from other assembly
166                 try {
167                         ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));
168                         Assert.Fail ("#B");
169                 } catch (InvalidOperationException) {
170                 }
171         }
172
173         [Test]
174         public void TestIsDefined ()
175         {
176                 CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });
177                 ab.SetCustomAttribute (cab);
178
179                 Assert.IsTrue (ab.IsDefined (typeof (FooAttribute), false),
180                         "IsDefined(FooAttribute) works");
181                 Assert.IsFalse (ab.IsDefined (typeof (AssemblyVersionAttribute), false),
182                         "!IsDefined(AssemblyVersionAttribute) works");
183         }
184
185         [Test]
186         [ExpectedException (typeof (NotSupportedException))]
187         public void TestGetManifestResourceNames ()
188         {
189                 ab.GetManifestResourceNames ();
190         }
191
192         [Test]
193         [ExpectedException (typeof (NotSupportedException))]
194         public void TestGetManifestResourceInfo ()
195         {
196                 ab.GetManifestResourceInfo ("foo");
197         }
198
199         [Test]
200         [ExpectedException (typeof (NotSupportedException))]
201         public void TestGetManifestResourceStream1 ()
202         {
203                 ab.GetManifestResourceStream ("foo");
204         }
205
206         [Test]
207         [ExpectedException (typeof (NotSupportedException))]
208         public void TestGetManifestResourceStream2 ()
209         {
210                 ab.GetManifestResourceStream (typeof (int), "foo");
211         }
212
213         [Test]
214         [ExpectedException (typeof (NotSupportedException))]
215         public void TestGetFiles1 ()
216         {
217                 ab.GetFiles ();
218         }
219
220         [Test]
221         [ExpectedException (typeof (NotSupportedException))]
222         public void TestGetFiles2 ()
223         {
224                 ab.GetFiles (true);
225         }
226
227         [Test]
228         [ExpectedException (typeof (NotSupportedException))]
229         public void TestGetFile ()
230         {
231                 ab.GetFile ("foo");
232         }
233
234         [Test]
235         [ExpectedException (typeof (NotSupportedException))]
236         public void TestGetExportedTypes ()
237         {
238                 ab.GetExportedTypes ();
239         }
240
241         [Test]
242         public void TestGetDynamicModule_Name_Null ()
243         {
244                 try {
245                         ab.GetDynamicModule (null);
246                         Assert.Fail ("#1");
247                 } catch (ArgumentNullException ex) {
248                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
249                         Assert.IsNull (ex.InnerException, "#3");
250                         Assert.IsNotNull (ex.Message, "#4");
251                         Assert.IsNotNull (ex.ParamName, "#5");
252                         Assert.AreEqual ("name", ex.ParamName, "#6");
253                 }
254         }
255
256         [Test]
257         public void TestGetDynamicModule_Name_Empty ()
258         {
259                 try {
260                         ab.GetDynamicModule (string.Empty);
261                         Assert.Fail ("#1");
262                 } catch (ArgumentException ex) {
263                         // Empty name is not legal
264                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
265                         Assert.IsNull (ex.InnerException, "#3");
266                         Assert.IsNotNull (ex.Message, "#4");
267                         Assert.IsNotNull (ex.ParamName, "#5");
268                         Assert.AreEqual ("name", ex.ParamName, "#6");
269                 }
270         }
271
272         [Test]
273         public void TestGetDynamicModule3 ()
274         {
275                 Assert.IsNull (ab.GetDynamicModule ("FOO2"));
276                 ModuleBuilder mb = ab.DefineDynamicModule ("FOO");
277                 Assert.AreEqual (mb, ab.GetDynamicModule ("FOO"));
278                 Assert.IsNull (ab.GetDynamicModule ("FOO4"));
279         }
280
281         [Test]
282         public void TestImageRuntimeVersion ()
283         {
284                 string version = ab.ImageRuntimeVersion;
285                 Assert.IsTrue (version.Length > 0);
286         }
287
288         [Test]
289         public void TestAddResourceFile_Name_Null ()
290         {
291                 try {
292                         ab.AddResourceFile (null, "foo.txt");
293                         Assert.Fail ("#1");
294                 } catch (ArgumentNullException ex) {
295                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
296                         Assert.IsNull (ex.InnerException, "#3");
297                         Assert.IsNotNull (ex.Message, "#4");
298                         Assert.IsNotNull (ex.ParamName, "#5");
299                         Assert.AreEqual ("name", ex.ParamName, "#6");
300                 }
301         }
302
303         [Test]
304         public void TestAddResourceFile_Filename_Null ()
305         {
306                 try {
307                         ab.AddResourceFile ("foo", null);
308                         Assert.Fail ("#1");
309                 } catch (ArgumentNullException ex) {
310                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
311                         Assert.IsNull (ex.InnerException, "#3");
312                         Assert.IsNotNull (ex.Message, "#4");
313                         Assert.IsNotNull (ex.ParamName, "#5");
314                         Assert.AreEqual ("fileName", ex.ParamName, "#6");
315                 }
316         }
317
318         [Test]
319         public void TestAddResourceFile_Name_Empty ()
320         {
321                 try {
322                         ab.AddResourceFile (string.Empty, "foo.txt");
323                         Assert.Fail ("#1");
324                 } catch (ArgumentException ex) {
325                         // Empty name is not legal
326                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
327                         Assert.IsNull (ex.InnerException, "#3");
328                         Assert.IsNotNull (ex.Message, "#4");
329                 }
330         }
331
332         [Test]
333         public void TestAddResourceFile_Filename_Empty ()
334         {
335                 try {
336                         ab.AddResourceFile ("foo", string.Empty);
337                         Assert.Fail ("#1");
338                 } catch (ArgumentException ex) {
339                         // Empty file name is not legal
340                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
341                         Assert.IsNull (ex.InnerException, "#3");
342                         Assert.IsNotNull (ex.Message, "#4");
343                 }
344         }
345
346         [Test]
347         [ExpectedException (typeof (FileNotFoundException))]
348         public void TestAddResourceFile_FileName_DoesNotExist ()
349         {
350                 ab.AddResourceFile ("foo", "not-existent.txt");
351         }
352
353         [Test]
354         public void TestAddResourceFile_FileName_Duplicate ()
355         {
356                 ab.AddResourceFile ("foo", "res1.txt");
357                 try {
358                         ab.AddResourceFile ("foo2", "res1.txt");
359                         Assert.Fail ("#1");
360                 } catch (ArgumentException ex) {
361                         // Duplicate file names
362                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
363                         Assert.IsNull (ex.InnerException, "#3");
364                         Assert.IsNotNull (ex.Message, "#4");
365                         Assert.IsNull (ex.ParamName, "#5");
366                 }
367         }
368
369         [Test]
370         public void TestAddResourceFile_Name_Duplicate ()
371         {
372                 ab.AddResourceFile ("foo", "res1.txt");
373                 try {
374                         ab.AddResourceFile ("foo", "res2.txt");
375                         Assert.Fail ("#1");
376                 } catch (ArgumentException ex) {
377                         // Duplicate resource name within an assembly
378                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
379                         Assert.IsNull (ex.InnerException, "#3");
380                         Assert.IsNotNull (ex.Message, "#4");
381                         Assert.IsNull (ex.ParamName, "#5");
382                 }
383         }
384
385         [Test]
386         public void TestAddResourceFile_Filename_IncludesPath ()
387         {
388                 try {
389                         ab.AddResourceFile ("foo", "/tmp/res1.txt");
390                         Assert.Fail ("#1");
391                 } catch (ArgumentException ex) {
392                         // The filename must not include a path specification
393                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
394                         Assert.IsNull (ex.InnerException, "#3");
395                         Assert.IsNotNull (ex.Message, "#4");
396                         Assert.IsNotNull (ex.ParamName, "#5");
397                         Assert.AreEqual ("fileName", ex.ParamName, "#6");
398                 }
399         }
400
401         [Test]
402         public void TestAddResourceFile ()
403         {
404                 ab.AddResourceFile ("foo", "res2.txt", ResourceAttributes.Public);
405                 ab.Save ("TestAddResourceFile.dll");
406
407                 // TODO: Test reading back
408         }
409
410         [Test]
411         public void TestDefineResource ()
412         {
413                 ab.DefineResource ("foo", "FOO", "foo.txt", ResourceAttributes.Public);
414                 ab.DefineResource ("foo2", "FOO", "foo2.txt");
415                 ab.Save ("TestDefineResource.dll");
416         }
417
418         [Test]
419         public void TestDefineDynamicModule_Name_Null ()
420         {
421                 try {
422                         ab.DefineDynamicModule (null, "foo.txt");
423                         Assert.Fail ("#1");
424                 } catch (ArgumentNullException ex) {
425                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
426                         Assert.IsNull (ex.InnerException, "#3");
427                         Assert.IsNotNull (ex.Message, "#4");
428                         Assert.IsNotNull (ex.ParamName, "#5");
429                         Assert.AreEqual ("name", ex.ParamName, "#6");
430                 }
431         }
432
433         [Test]
434         public void TestDefineDynamicModule_FileName_Null ()
435         {
436                 try {
437                         ab.DefineDynamicModule ("foo", null);
438                         Assert.Fail ("#1");
439                 } catch (ArgumentNullException ex) {
440                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
441                         Assert.IsNull (ex.InnerException, "#3");
442                         Assert.IsNotNull (ex.Message, "#4");
443                         Assert.IsNotNull (ex.ParamName, "#5");
444                         Assert.AreEqual ("fileName", ex.ParamName, "#6");
445                 }
446         }
447
448         [Test]
449         public void TestDefineDynamicModule_Name_Empty ()
450         {
451                 try {
452                         ab.DefineDynamicModule (string.Empty, "foo.txt");
453                         Assert.Fail ("#1");
454                 } catch (ArgumentException ex) {
455                         // Empty name is not legal
456                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
457                         Assert.IsNull (ex.InnerException, "#3");
458                         Assert.IsNotNull (ex.Message, "#4");
459                         Assert.IsNotNull (ex.ParamName, "#5");
460                         Assert.AreEqual ("name", ex.ParamName, "#6");
461                 }
462         }
463
464         [Test]
465         public void TestDefineDynamicModule_Filename_Empty ()
466         {
467                 try {
468                         ab.DefineDynamicModule ("foo", string.Empty);
469                         Assert.Fail ("#1");
470                 } catch (ArgumentException ex) {
471                         // Empty file name is not legal
472                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
473                         Assert.IsNull (ex.InnerException, "#3");
474                         Assert.IsNotNull (ex.Message, "#4");
475                         Assert.IsNotNull (ex.ParamName, "#5");
476                         Assert.AreEqual ("fileName", ex.ParamName, "#6");
477                 }
478         }
479
480         [Test]
481         public void TestDefineDynamicModule_FileName_Duplicate ()
482         {
483                 ab.DefineDynamicModule ("foo", "res1.txt");
484                 try {
485                         ab.DefineDynamicModule ("foo2", "res1.txt");
486                         Assert.Fail ("#1");
487                 } catch (ArgumentException ex) {
488                         // Duplicate file names
489                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
490                         Assert.IsNull (ex.InnerException, "#3");
491                         Assert.IsNotNull (ex.Message, "#4");
492                         Assert.IsNull (ex.ParamName, "#5");
493                 }
494         }
495
496         [Test]
497         public void TestDefineDynamicModule_Name_Duplicate ()
498         {
499                 ab.DefineDynamicModule ("foo", "res1.txt");
500                 try {
501                         ab.DefineDynamicModule ("foo", "res2.txt");
502                         Assert.Fail ("#1");
503                 } catch (ArgumentException ex) {
504                         // Duplicate dynamic module name within an assembly
505                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
506                         Assert.IsNull (ex.InnerException, "#3");
507                         Assert.IsNotNull (ex.Message, "#4");
508                         Assert.IsNull (ex.ParamName, "#5");
509                 }
510         }
511
512         [Test]
513         public void TestDefineDynamicModule_Filename_IncludesPath ()
514         {
515                 try {
516                         ab.DefineDynamicModule ("foo", "/tmp/res1.txt");
517                         Assert.Fail ("#1");
518                 } catch (ArgumentException ex) {
519                         // The filename must not include a path specification
520                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
521                         Assert.IsNull (ex.InnerException, "#3");
522                         Assert.IsNotNull (ex.Message, "#4");
523                         Assert.IsNotNull (ex.ParamName, "#5");
524                         Assert.AreEqual ("fileName", ex.ParamName, "#6");
525                 }
526         }
527
528         [Test]
529         public void TestDefineDynamicModule5_FileName_NoExtension ()
530         {
531                 try {
532                         ab.DefineDynamicModule ("foo", "bar");
533                         Assert.Fail ("#1");
534                 } catch (ArgumentException ex) {
535                         // Module file name 'bar' must have file extension
536                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
537                         Assert.IsNull (ex.InnerException, "#3");
538                         Assert.IsNotNull (ex.Message, "#4");
539                         Assert.IsTrue (ex.Message.IndexOf ("bar") != -1, "#5");
540                         Assert.IsNull (ex.ParamName, "#6");
541                 }
542         }
543
544         [Test]
545         [Category ("NotWorking")]
546         public void TestDefineDynamicModule_Name_MaxLength () {
547                 string name = string.Empty;
548                 for (int i = 0; i < 259; ++i)
549                         name = name + "A";
550                 ab.DefineDynamicModule (name);
551
552                 name = name + "A";
553                 try {
554                         ab.DefineDynamicModule (name);
555                         Assert.Fail ("#1");
556                 } catch (ArgumentException ex) {
557                         // Value does not fall within expected range
558                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
559                         Assert.IsNull (ex.InnerException, "#3");
560                         Assert.IsNotNull (ex.Message, "#4");
561                         Assert.IsNull (ex.ParamName, "#5");
562                 }
563         }
564
565         [Test]
566         [ExpectedException (typeof (InvalidOperationException))]
567         public void TestDefineDynamicModule_Assembly_Saved ()
568         {
569                 // Called when assembly was already saved
570                 ab.Save ("TestDefineDynamicModule7.dll");
571                 ab.DefineDynamicModule ("foo", "foo.dll");
572         }
573
574         [Test]
575         [ExpectedException (typeof (NotSupportedException))]
576         public void TestDefineDynamicModule_Access_Run ()
577         {
578                 // Called on an assembly defined with the Run attribute
579                 AssemblyBuilder ab = 
580                         domain.DefineDynamicAssembly (genAssemblyName (),
581                                                                                   AssemblyBuilderAccess.Run,
582                                                                                   tempDir);
583                 ab.DefineDynamicModule ("foo", "foo.dll");
584         }
585
586         [Test]
587         public void TestDefineDynamicModule ()
588         {
589                 ab.DefineDynamicModule ("foo", "foo.dll");
590                 ab.DefineDynamicModule ("foo2", true);
591                 ab.DefineDynamicModule ("foo3", "foo3.dll");
592                 ab.DefineDynamicModule ("foo4", "foo4.dll", true);
593         }
594
595         [Test] // DefineUnmanagedResource (byte [])
596         [Category ("NotWorking")]
597         public void TestDefineUnmanagedResource1_ResourceAlreadyDefined ()
598         {
599                 string version_res = Path.Combine (tempDir, "version.res");
600                 using (FileStream fs = File.OpenWrite (version_res)) {
601                         fs.WriteByte (0x0a);
602                 }
603
604                 ab.DefineUnmanagedResource (new byte [0]);
605
606                 try {
607                         ab.DefineUnmanagedResource (new byte [0]);
608                         Assert.Fail ("#A1");
609                 } catch (ArgumentException ex) {
610                         // Native resource has already been defined
611                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
612                         Assert.IsNull (ex.InnerException, "#A3");
613                         Assert.IsNotNull (ex.Message, "#A4");
614                         Assert.IsNull (ex.ParamName, "#A5");
615                 }
616
617                 try {
618                         ab.DefineUnmanagedResource (version_res);
619                         Assert.Fail ("#B1");
620                 } catch (ArgumentException ex) {
621                         // Native resource has already been defined
622                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
623                         Assert.IsNull (ex.InnerException, "#B3");
624                         Assert.IsNotNull (ex.Message, "#B4");
625                         Assert.IsNull (ex.ParamName, "#B5");
626                 }
627
628                 try {
629                         ab.DefineVersionInfoResource ();
630                         Assert.Fail ("#C1");
631                 } catch (ArgumentException ex) {
632                         // Native resource has already been defined
633                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
634                         Assert.IsNull (ex.InnerException, "#C3");
635                         Assert.IsNotNull (ex.Message, "#C4");
636                         Assert.IsNull (ex.ParamName, "#C5");
637                 }
638
639                 try {
640                         ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
641                         Assert.Fail ("#D1");
642                 } catch (ArgumentException ex) {
643                         // Native resource has already been defined
644                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
645                         Assert.IsNull (ex.InnerException, "#D3");
646                         Assert.IsNotNull (ex.Message, "#D4");
647                         Assert.IsNull (ex.ParamName, "#D5");
648                 }
649         }
650
651         [Test] // DefineUnmanagedResource (byte [])
652         public void TestDefineUnmanagedResource1_Resource_Null ()
653         {
654                 try {
655                         ab.DefineUnmanagedResource ((byte []) null);
656                         Assert.Fail ("#1");
657                 } catch (ArgumentNullException ex) {
658                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
659                         Assert.IsNull (ex.InnerException, "#3");
660                         Assert.IsNotNull (ex.Message, "#4");
661                         Assert.IsNotNull (ex.ParamName, "#5");
662                         Assert.AreEqual ("resource", ex.ParamName, "#6");
663                 }
664         }
665
666         [Test] // DefineUnmanagedResource (String)
667         public void TestDefineUnmanagedResource2_ResourceAlreadyDefined ()
668         {
669                 string version_res = Path.Combine (tempDir, "version.res");
670                 using (FileStream fs = File.OpenWrite (version_res)) {
671                         fs.WriteByte (0x0a);
672                 }
673
674                 ab.DefineUnmanagedResource (version_res);
675
676                 try {
677                         ab.DefineUnmanagedResource (new byte [0]);
678                         Assert.Fail ("#A1");
679                 } catch (ArgumentException ex) {
680                         // Native resource has already been defined
681                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
682                         Assert.IsNull (ex.InnerException, "#A3");
683                         Assert.IsNotNull (ex.Message, "#A4");
684                         Assert.IsNull (ex.ParamName, "#A5");
685                 }
686
687                 try {
688                         ab.DefineUnmanagedResource (version_res);
689                         Assert.Fail ("#B1");
690                 } catch (ArgumentException ex) {
691                         // Native resource has already been defined
692                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
693                         Assert.IsNull (ex.InnerException, "#B3");
694                         Assert.IsNotNull (ex.Message, "#B4");
695                         Assert.IsNull (ex.ParamName, "#B5");
696                 }
697
698                 try {
699                         ab.DefineVersionInfoResource ();
700                         Assert.Fail ("#C1");
701                 } catch (ArgumentException ex) {
702                         // Native resource has already been defined
703                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
704                         Assert.IsNull (ex.InnerException, "#C3");
705                         Assert.IsNotNull (ex.Message, "#C4");
706                         Assert.IsNull (ex.ParamName, "#C5");
707                 }
708
709                 try {
710                         ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
711                         Assert.Fail ("#D1");
712                 } catch (ArgumentException ex) {
713                         // Native resource has already been defined
714                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
715                         Assert.IsNull (ex.InnerException, "#D3");
716                         Assert.IsNotNull (ex.Message, "#D4");
717                         Assert.IsNull (ex.ParamName, "#D5");
718                 }
719         }
720
721         [Test] // DefinedUnmanagedResource (String)
722         [ExpectedException (typeof (FileNotFoundException))]
723         public void TestDefineUnmanagedResource2_ResourceFile_DoesNotExist ()
724         {
725                 ab.DefineUnmanagedResource ("not-exists.txt");
726         }
727
728         [Test] // DefinedUnmanagedResource (String)
729         public void TestDefineUnmanagedResource2_ResourceFileName_Empty ()
730         {
731                 try {
732                         ab.DefineUnmanagedResource (string.Empty);
733                         Assert.Fail ("#1");
734                 } catch (ArgumentException ex) {
735                         // The path is not of a legal form
736                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
737                         Assert.IsNull (ex.InnerException, "#3");
738                         Assert.IsNotNull (ex.Message, "#4");
739                         Assert.IsNull (ex.ParamName, "#5");
740                 }
741         }
742
743         [Test] // DefinedUnmanagedResource (String)
744         public void TestDefineUnmanagedResource2_ResourceFileName_Null ()
745         {
746                 try {
747                         ab.DefineUnmanagedResource ((string) null);
748                         Assert.Fail ("#1");
749                 } catch (ArgumentNullException ex) {
750                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
751                         Assert.IsNull (ex.InnerException, "#3");
752                         Assert.IsNotNull (ex.Message, "#4");
753                         Assert.IsNotNull (ex.ParamName, "#5");
754                         Assert.AreEqual ("resourceFileName", ex.ParamName, "#6");
755                 }
756         }
757
758         [Test] // DefineVersionInfoResource ()
759         public void TestDefineVersionInfoResource1_Culture_NotSupported ()
760         {
761                 AssemblyName aname = new AssemblyName ();
762                 aname.CultureInfo = new CultureInfo ("nl-BE");
763                 aname.Name = "lib";
764                 aname.Version = new Version (3, 5, 7);
765
766                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
767                         aname, AssemblyBuilderAccess.RunAndSave,
768                         tempDir);
769
770                 // AssemblyCulture
771                 Type attrType = typeof (AssemblyCultureAttribute);
772                 ConstructorInfo ci = attrType.GetConstructor (new Type [] { typeof (String) });
773                 CustomAttributeBuilder cab = new CustomAttributeBuilder (
774                         ci, new object [1] { "doesnotexist" });
775                 ab.SetCustomAttribute (cab);
776
777                 ab.DefineVersionInfoResource ();
778
779                 try {
780                         ab.Save ("lib.dll");
781                         Assert.Fail ("#A1");
782 #if NET_4_0
783                 } catch (CultureNotFoundException ex) {
784                 }
785 #else
786                 } catch (ArgumentException ex) {
787                         // Culture name doesnotexist is not supported
788                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
789                         Assert.IsNull (ex.InnerException, "#A3");
790                         Assert.IsNotNull (ex.Message, "#A4");
791                         Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#A5");
792                         Assert.AreEqual ("name", ex.ParamName, "#A6");
793                 }
794 #endif
795
796                 ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname,
797                         AssemblyBuilderAccess.RunAndSave, tempDir);
798
799                 // AssemblyCulture
800                 attrType = typeof (AssemblyCultureAttribute);
801                 ci = attrType.GetConstructor (new Type [] { typeof (String) });
802                 cab = new CustomAttributeBuilder (ci, new object [1] { "neutral" });
803                 ab.SetCustomAttribute (cab);
804
805                 ab.DefineVersionInfoResource ();
806
807                 try {
808                         ab.Save ("lib.dll");
809                         Assert.Fail ("#B1");
810 #if NET_4_0
811                 } catch (CultureNotFoundException ex) {
812                 }
813 #else
814                 } catch (ArgumentException ex) {
815                         // Culture name neutral is not supported
816                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
817                         Assert.IsNull (ex.InnerException, "#B3");
818                         Assert.IsNotNull (ex.Message, "#B4");
819                         Assert.IsTrue (ex.Message.IndexOf ("neutral") != -1, "#B5");
820                         Assert.AreEqual ("name", ex.ParamName, "#B6");
821                 }
822 #endif
823         }
824
825         [Test] // DefineVersionInfoResource ()
826         public void TestDefineVersionInfoResource1_ResourceAlreadyDefined ()
827         {
828                 string version_res = Path.Combine (tempDir, "version.res");
829                 using (FileStream fs = File.OpenWrite (version_res)) {
830                         fs.WriteByte (0x0a);
831                 }
832
833                 ab.DefineVersionInfoResource ();
834
835                 try {
836                         ab.DefineUnmanagedResource (new byte [0]);
837                         Assert.Fail ("#A1");
838                 } catch (ArgumentException ex) {
839                         // Native resource has already been defined
840                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
841                         Assert.IsNull (ex.InnerException, "#A3");
842                         Assert.IsNotNull (ex.Message, "#A4");
843                         Assert.IsNull (ex.ParamName, "#A5");
844                 }
845
846                 try {
847                         ab.DefineUnmanagedResource (version_res);
848                         Assert.Fail ("#B1");
849                 } catch (ArgumentException ex) {
850                         // Native resource has already been defined
851                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
852                         Assert.IsNull (ex.InnerException, "#B3");
853                         Assert.IsNotNull (ex.Message, "#B4");
854                         Assert.IsNull (ex.ParamName, "#B5");
855                 }
856
857                 try {
858                         ab.DefineVersionInfoResource ();
859                         Assert.Fail ("#C1");
860                 } catch (ArgumentException ex) {
861                         // Native resource has already been defined
862                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
863                         Assert.IsNull (ex.InnerException, "#C3");
864                         Assert.IsNotNull (ex.Message, "#C4");
865                         Assert.IsNull (ex.ParamName, "#C5");
866                 }
867
868                 try {
869                         ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
870                         Assert.Fail ("#D1");
871                 } catch (ArgumentException ex) {
872                         // Native resource has already been defined
873                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
874                         Assert.IsNull (ex.InnerException, "#D3");
875                         Assert.IsNotNull (ex.Message, "#D4");
876                         Assert.IsNull (ex.ParamName, "#D5");
877                 }
878         }
879
880         [Test] // DefineVersionInfoResource (String, String, String, String, String)
881         public void TestDefineVersionInfoResource2_Culture_NotSupported ()
882         {
883                 AssemblyName aname = new AssemblyName ();
884                 aname.CultureInfo = new CultureInfo ("nl-BE");
885                 aname.Name = "lib";
886                 aname.Version = new Version (3, 5, 7);
887
888                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
889                         aname, AssemblyBuilderAccess.RunAndSave,
890                         tempDir);
891
892                 // AssemblyCulture
893                 Type attrType = typeof (AssemblyCultureAttribute);
894                 ConstructorInfo ci = attrType.GetConstructor (new Type [] { typeof (String) });
895                 CustomAttributeBuilder cab = new CustomAttributeBuilder (
896                         ci, new object [1] { "doesnotexist" });
897                 ab.SetCustomAttribute (cab);
898
899                 ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
900
901                 try {
902                         ab.Save ("lib.dll");
903                         Assert.Fail ("#A1");
904 #if NET_4_0
905                 } catch (CultureNotFoundException ex) {
906                 }
907 #else
908                 } catch (ArgumentException ex) {
909                         // Culture name doesnotexist is not supported
910                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
911                         Assert.IsNull (ex.InnerException, "#A3");
912                         Assert.IsNotNull (ex.Message, "#A4");
913                         Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#A5");
914                         Assert.AreEqual ("name", ex.ParamName, "#A6");
915                 }
916 #endif
917
918                 ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname,
919                         AssemblyBuilderAccess.RunAndSave, tempDir);
920
921                 // AssemblyCulture
922                 attrType = typeof (AssemblyCultureAttribute);
923                 ci = attrType.GetConstructor (new Type [] { typeof (String) });
924                 cab = new CustomAttributeBuilder (ci, new object [1] { "neutral" });
925                 ab.SetCustomAttribute (cab);
926
927                 ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
928
929                 try {
930                         ab.Save ("lib.dll");
931                         Assert.Fail ("#B1");
932 #if NET_4_0
933                 } catch (CultureNotFoundException ex) {
934                 }
935 #else
936                 } catch (ArgumentException ex) {
937                         // Culture name neutral is not supported
938                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
939                         Assert.IsNull (ex.InnerException, "#B3");
940                         Assert.IsNotNull (ex.Message, "#B4");
941                         Assert.IsTrue (ex.Message.IndexOf ("neutral") != -1, "#B5");
942                         Assert.AreEqual ("name", ex.ParamName, "#B6");
943                 }
944 #endif
945         }
946
947         [Test] // DefineVersionInfoResource (String, String, String, String, String)
948         public void TestDefineVersionInfoResource2_ResourceAlreadyDefined ()
949         {
950                 string version_res = Path.Combine (tempDir, "version.res");
951                 using (FileStream fs = File.OpenWrite (version_res)) {
952                         fs.WriteByte (0x0a);
953                 }
954
955                 ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
956
957                 try {
958                         ab.DefineUnmanagedResource (new byte [0]);
959                         Assert.Fail ("#A1");
960                 } catch (ArgumentException ex) {
961                         // Native resource has already been defined
962                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
963                         Assert.IsNull (ex.InnerException, "#A3");
964                         Assert.IsNotNull (ex.Message, "#A4");
965                         Assert.IsNull (ex.ParamName, "#A5");
966                 }
967
968                 try {
969                         ab.DefineUnmanagedResource (version_res);
970                         Assert.Fail ("#B1");
971                 } catch (ArgumentException ex) {
972                         // Native resource has already been defined
973                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
974                         Assert.IsNull (ex.InnerException, "#B3");
975                         Assert.IsNotNull (ex.Message, "#B4");
976                         Assert.IsNull (ex.ParamName, "#B5");
977                 }
978
979                 try {
980                         ab.DefineVersionInfoResource ();
981                         Assert.Fail ("#C1");
982                 } catch (ArgumentException ex) {
983                         // Native resource has already been defined
984                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
985                         Assert.IsNull (ex.InnerException, "#C3");
986                         Assert.IsNotNull (ex.Message, "#C4");
987                         Assert.IsNull (ex.ParamName, "#C5");
988                 }
989
990                 try {
991                         ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
992                         Assert.Fail ("#D1");
993                 } catch (ArgumentException ex) {
994                         // Native resource has already been defined
995                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
996                         Assert.IsNull (ex.InnerException, "#D3");
997                         Assert.IsNotNull (ex.Message, "#D4");
998                         Assert.IsNull (ex.ParamName, "#D5");
999                 }
1000         }
1001
1002         [Test]
1003         public void TestSetCustomAttribute1_CustomBuilder_Null ()
1004         {
1005                 try {
1006                         ab.SetCustomAttribute (null);
1007                         Assert.Fail ("#1");
1008                 } catch (ArgumentNullException ex) {
1009                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1010                         Assert.IsNull (ex.InnerException, "#3");
1011                         Assert.IsNotNull (ex.Message, "#4");
1012                         Assert.IsNotNull (ex.ParamName, "#5");
1013                         Assert.AreEqual ("customBuilder", ex.ParamName, "#6");
1014                 }
1015         }
1016
1017         [Test]
1018         public void TestSetCustomAttribute2_ConstructorInfo_Null ()
1019         {
1020                 try {
1021                         ab.SetCustomAttribute (null, new byte [0]);
1022                         Assert.Fail ("#1");
1023                 } catch (ArgumentNullException ex) {
1024                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1025                         Assert.IsNull (ex.InnerException, "#3");
1026                         Assert.IsNotNull (ex.Message, "#4");
1027                         Assert.IsNotNull (ex.ParamName, "#5");
1028                         Assert.AreEqual ("con", ex.ParamName, "#6");
1029                 }
1030         }
1031
1032         [Test]
1033         public void TestSetCustomAttribute2_BinaryAttribute_Null ()
1034         {
1035                 try {
1036                         ab.SetCustomAttribute (typeof (AssemblyCompanyAttribute).GetConstructor (
1037                                 new Type [] { typeof (String) }), null);
1038                         Assert.Fail ("#1");
1039                 } catch (ArgumentNullException ex) {
1040                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1041                         Assert.IsNull (ex.InnerException, "#3");
1042                         Assert.IsNotNull (ex.Message, "#4");
1043                         Assert.IsNotNull (ex.ParamName, "#5");
1044                         Assert.AreEqual ("binaryAttribute", ex.ParamName, "#6");
1045                 }
1046         }
1047
1048         [Test] // SetCustomAttribute (CustomAttributeBuilder)
1049         public void TestSetCustomAttribute1 ()
1050         {
1051                 Assembly a;
1052                 AssemblyName an;
1053                 AssemblyName check;
1054                 Attribute attr;
1055                 string filename;
1056                 
1057                 an = new AssemblyName ();
1058                 an.Name = "TestSetCustomAttributeA";
1059
1060                 ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, tempDir);
1061                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).
1062                         GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4"}));
1063                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).
1064                         GetConstructor (new Type [] { typeof (string) }), new object [] { "bar"}));
1065                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).
1066                         GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }),
1067                         new object [] { AssemblyHashAlgorithm.MD5 }));
1068                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).
1069                         GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint)0xff }));
1070                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).
1071                         GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
1072                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (FooAttribute).
1073                         GetConstructor (Type.EmptyTypes), new object [0]));
1074                 ab.Save ("TestSetCustomAttributeA.dll");
1075
1076                 filename = Path.Combine (tempDir, "TestSetCustomAttributeA.dll");
1077                 check = AssemblyName.GetAssemblyName (filename);
1078                 Assert.AreEqual (CultureInfo.InvariantCulture, check.CultureInfo, "#A1");
1079 #if NET_2_0
1080                 Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#A2");
1081 #else
1082                 Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#A2");
1083 #endif
1084                 Assert.AreEqual ("TestSetCustomAttributeA, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", check.FullName, "#A3");
1085 #if NET_2_0
1086                 Assert.IsNull (check.GetPublicKey (), "#A4");
1087 #else
1088                 Assert.AreEqual (new byte [0], check.GetPublicKey (), "#A4");
1089 #endif
1090 #if NET_2_0
1091                 Assert.AreEqual (new byte [0], check.GetPublicKeyToken (), "#A5");
1092 #else
1093                 Assert.IsNull (check.GetPublicKeyToken (), "#A5");
1094 #endif
1095                 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, check.HashAlgorithm, "#A6");
1096                 Assert.IsNull (check.KeyPair, "#A7");
1097                 Assert.AreEqual ("TestSetCustomAttributeA", check.Name, "#A8");
1098 #if NET_2_0
1099                 //Assert.AreEqual (ProcessorArchitecture.MSIL, check.ProcessorArchitecture, "#A9");
1100 #endif
1101                 Assert.AreEqual (new Version (0, 0, 0, 0), check.Version, "#A10");
1102                 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, check.VersionCompatibility, "#A11");
1103
1104                 using (FileStream fs = File.OpenRead (filename)) {
1105                         byte [] buffer = new byte [fs.Length];
1106                         fs.Read (buffer, 0, buffer.Length);
1107                         a = Assembly.Load (buffer);
1108                 }
1109
1110                 attr = Attribute.GetCustomAttribute (a, typeof (AssemblyVersionAttribute));
1111                 Assert.IsNotNull (attr, "#A12a");
1112                 Assert.AreEqual ("1.2.3.4", ((AssemblyVersionAttribute) attr).Version, "#A12b");
1113                 attr = Attribute.GetCustomAttribute (a, typeof (AssemblyCultureAttribute));
1114                 Assert.IsNotNull (attr, "#A13a");
1115                 Assert.AreEqual ("bar", ((AssemblyCultureAttribute) attr).Culture, "#A13b");
1116                 attr = Attribute.GetCustomAttribute (a, typeof (AssemblyAlgorithmIdAttribute));
1117                 Assert.IsNotNull (attr, "#A14a");
1118                 Assert.AreEqual ((uint) AssemblyHashAlgorithm.MD5, ((AssemblyAlgorithmIdAttribute) attr).AlgorithmId, "#A14b");
1119                 attr = Attribute.GetCustomAttribute (a, typeof (AssemblyFlagsAttribute));
1120                 Assert.IsNotNull (attr, "#A15a");
1121                 Assert.AreEqual ((uint) 0xff, ((AssemblyFlagsAttribute) attr).Flags, "#A15b");
1122                 attr = Attribute.GetCustomAttribute (a, typeof (FooAttribute));
1123                 Assert.IsNotNull (attr, "#A16");
1124
1125                 an = new AssemblyName ();
1126                 an.CultureInfo = new CultureInfo ("nl-BE");
1127                 an.Flags = AssemblyNameFlags.Retargetable;
1128                 an.Name = "TestSetCustomAttributeB";
1129 #if NET_2_0
1130                 an.ProcessorArchitecture = ProcessorArchitecture.IA64;
1131 #endif
1132                 an.Version = new Version (1, 3, 5, 7);
1133                 an.VersionCompatibility = AssemblyVersionCompatibility.SameDomain;
1134
1135                 ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, tempDir);
1136                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).
1137                         GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4" }));
1138                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).
1139                         GetConstructor (new Type [] { typeof (string) }), new object [] { "en-US" }));
1140                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).
1141                         GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }),
1142                         new object [] { AssemblyHashAlgorithm.MD5 }));
1143                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).
1144                         GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint) 0x0100 }));
1145                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).
1146                         GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
1147                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (FooAttribute).
1148                         GetConstructor (Type.EmptyTypes), new object [0]));
1149                 ab.Save ("TestSetCustomAttributeB.dll");
1150
1151                 filename = Path.Combine (tempDir, "TestSetCustomAttributeB.dll");
1152                 check = AssemblyName.GetAssemblyName (filename);
1153                 Assert.AreEqual ("nl-BE", check.CultureInfo.Name, "#B1");
1154 #if NET_2_0
1155                 Assert.AreEqual (AssemblyNameFlags.Retargetable, check.Flags, "#B2");
1156 #else
1157                 Assert.AreEqual (AssemblyNameFlags.PublicKey | AssemblyNameFlags.Retargetable, check.Flags, "#B2");
1158 #endif
1159                 Assert.AreEqual ("TestSetCustomAttributeB, Version=1.3.5.7, Culture=nl-BE, PublicKeyToken=null, Retargetable=Yes", check.FullName, "#B3");
1160 #if NET_2_0
1161                 Assert.IsNull (check.GetPublicKey (), "#B4");
1162 #else
1163                 Assert.AreEqual (new byte [0], check.GetPublicKey (), "#B4");
1164 #endif
1165 #if NET_2_0
1166                 Assert.AreEqual (new byte [0], check.GetPublicKeyToken (), "#B5");
1167 #else
1168                 Assert.IsNull (check.GetPublicKeyToken (), "#B5");
1169 #endif
1170                 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, check.HashAlgorithm, "#B6");
1171                 Assert.IsNull (check.KeyPair, "#B7");
1172                 Assert.AreEqual ("TestSetCustomAttributeB", check.Name, "#B8");
1173 #if NET_2_0
1174                 //Assert.AreEqual (ProcessorArchitecture.MSIL, check.ProcessorArchitecture, "#B9");
1175 #endif
1176                 Assert.AreEqual (new Version (1, 3, 5, 7), check.Version, "#B10");
1177                 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, check.VersionCompatibility, "#B11");
1178
1179                 using (FileStream fs = File.OpenRead (filename)) {
1180                         byte [] buffer = new byte [fs.Length];
1181                         fs.Read (buffer, 0, buffer.Length);
1182                         a = Assembly.Load (buffer);
1183                 }
1184
1185                 attr = Attribute.GetCustomAttribute (a, typeof (AssemblyVersionAttribute));
1186                 Assert.IsNotNull (attr, "#B12a");
1187                 Assert.AreEqual ("1.2.3.4", ((AssemblyVersionAttribute) attr).Version, "#B12b");
1188                 attr = Attribute.GetCustomAttribute (a, typeof (AssemblyCultureAttribute));
1189                 Assert.IsNotNull (attr, "#B13a");
1190                 Assert.AreEqual ("en-US", ((AssemblyCultureAttribute) attr).Culture, "#B13b");
1191                 attr = Attribute.GetCustomAttribute (a, typeof (AssemblyAlgorithmIdAttribute));
1192                 Assert.IsNotNull (attr, "#B14a");
1193                 Assert.AreEqual ((uint) AssemblyHashAlgorithm.MD5, ((AssemblyAlgorithmIdAttribute) attr).AlgorithmId, "#B14b");
1194                 attr = Attribute.GetCustomAttribute (a, typeof (AssemblyFlagsAttribute));
1195                 Assert.IsNotNull (attr, "#B15a");
1196                 Assert.AreEqual ((uint) 0x0100, ((AssemblyFlagsAttribute) attr).Flags, "#B15b");
1197                 attr = Attribute.GetCustomAttribute (a, typeof (FooAttribute));
1198                 Assert.IsNotNull (attr, "#B16");
1199         }
1200
1201         // strongname generated using "sn -k unit.snk"
1202         static byte[] strongName = { 
1203                 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32, 
1204                 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x7F, 0x7C, 0xEA, 0x4A, 
1205                 0x28, 0x33, 0xD8, 0x3C, 0x86, 0x90, 0x86, 0x91, 0x11, 0xBB, 0x30, 0x0D, 
1206                 0x3D, 0x69, 0x04, 0x4C, 0x48, 0xF5, 0x4F, 0xE7, 0x64, 0xA5, 0x82, 0x72, 
1207                 0x5A, 0x92, 0xC4, 0x3D, 0xC5, 0x90, 0x93, 0x41, 0xC9, 0x1D, 0x34, 0x16, 
1208                 0x72, 0x2B, 0x85, 0xC1, 0xF3, 0x99, 0x62, 0x07, 0x32, 0x98, 0xB7, 0xE4, 
1209                 0xFA, 0x75, 0x81, 0x8D, 0x08, 0xB9, 0xFD, 0xDB, 0x00, 0x25, 0x30, 0xC4, 
1210                 0x89, 0x13, 0xB6, 0x43, 0xE8, 0xCC, 0xBE, 0x03, 0x2E, 0x1A, 0x6A, 0x4D, 
1211                 0x36, 0xB1, 0xEB, 0x49, 0x26, 0x6C, 0xAB, 0xC4, 0x29, 0xD7, 0x8F, 0x25, 
1212                 0x11, 0xA4, 0x7C, 0x81, 0x61, 0x97, 0xCB, 0x44, 0x2D, 0x80, 0x49, 0x93, 
1213                 0x48, 0xA7, 0xC9, 0xAB, 0xDB, 0xCF, 0xA3, 0x34, 0xCB, 0x6B, 0x86, 0xE0, 
1214                 0x4D, 0x27, 0xFC, 0xA7, 0x4F, 0x36, 0xCA, 0x13, 0x42, 0xD3, 0x83, 0xC4, 
1215                 0x06, 0x6E, 0x12, 0xE0, 0xA1, 0x3D, 0x9F, 0xA9, 0xEC, 0xD1, 0xC6, 0x08, 
1216                 0x1B, 0x3D, 0xF5, 0xDB, 0x4C, 0xD4, 0xF0, 0x2C, 0xAA, 0xFC, 0xBA, 0x18, 
1217                 0x6F, 0x48, 0x7E, 0xB9, 0x47, 0x68, 0x2E, 0xF6, 0x1E, 0x67, 0x1C, 0x7E, 
1218                 0x0A, 0xCE, 0x10, 0x07, 0xC0, 0x0C, 0xAD, 0x5E, 0xC1, 0x53, 0x70, 0xD5, 
1219                 0xE7, 0x25, 0xCA, 0x37, 0x5E, 0x49, 0x59, 0xD0, 0x67, 0x2A, 0xBE, 0x92, 
1220                 0x36, 0x86, 0x8A, 0xBF, 0x3E, 0x17, 0x04, 0xFB, 0x1F, 0x46, 0xC8, 0x10, 
1221                 0x5C, 0x93, 0x02, 0x43, 0x14, 0x96, 0x6A, 0xD9, 0x87, 0x17, 0x62, 0x7D, 
1222                 0x3A, 0x45, 0xBE, 0x35, 0xDE, 0x75, 0x0B, 0x2A, 0xCE, 0x7D, 0xF3, 0x19, 
1223                 0x85, 0x4B, 0x0D, 0x6F, 0x8D, 0x15, 0xA3, 0x60, 0x61, 0x28, 0x55, 0x46, 
1224                 0xCE, 0x78, 0x31, 0x04, 0x18, 0x3C, 0x56, 0x4A, 0x3F, 0xA4, 0xC9, 0xB1, 
1225                 0x41, 0xED, 0x22, 0x80, 0xA1, 0xB3, 0xE2, 0xC7, 0x1B, 0x62, 0x85, 0xE4, 
1226                 0x81, 0x39, 0xCB, 0x1F, 0x95, 0xCC, 0x61, 0x61, 0xDF, 0xDE, 0xF3, 0x05, 
1227                 0x68, 0xB9, 0x7D, 0x4F, 0xFF, 0xF3, 0xC0, 0x0A, 0x25, 0x62, 0xD9, 0x8A, 
1228                 0x8A, 0x9E, 0x99, 0x0B, 0xFB, 0x85, 0x27, 0x8D, 0xF6, 0xD4, 0xE1, 0xB9, 
1229                 0xDE, 0xB4, 0x16, 0xBD, 0xDF, 0x6A, 0x25, 0x9C, 0xAC, 0xCD, 0x91, 0xF7, 
1230                 0xCB, 0xC1, 0x81, 0x22, 0x0D, 0xF4, 0x7E, 0xEC, 0x0C, 0x84, 0x13, 0x5A, 
1231                 0x74, 0x59, 0x3F, 0x3E, 0x61, 0x00, 0xD6, 0xB5, 0x4A, 0xA1, 0x04, 0xB5, 
1232                 0xA7, 0x1C, 0x29, 0xD0, 0xE1, 0x11, 0x19, 0xD7, 0x80, 0x5C, 0xEE, 0x08, 
1233                 0x15, 0xEB, 0xC9, 0xA8, 0x98, 0xF5, 0xA0, 0xF0, 0x92, 0x2A, 0xB0, 0xD3, 
1234                 0xC7, 0x8C, 0x8D, 0xBB, 0x88, 0x96, 0x4F, 0x18, 0xF0, 0x8A, 0xF9, 0x31, 
1235                 0x9E, 0x44, 0x94, 0x75, 0x6F, 0x78, 0x04, 0x10, 0xEC, 0xF3, 0xB0, 0xCE, 
1236                 0xA0, 0xBE, 0x7B, 0x25, 0xE1, 0xF7, 0x8A, 0xA8, 0xD4, 0x63, 0xC2, 0x65, 
1237                 0x47, 0xCC, 0x5C, 0xED, 0x7D, 0x8B, 0x07, 0x4D, 0x76, 0x29, 0x53, 0xAC, 
1238                 0x27, 0x8F, 0x5D, 0x78, 0x56, 0xFA, 0x99, 0x45, 0xA2, 0xCC, 0x65, 0xC4, 
1239                 0x54, 0x13, 0x9F, 0x38, 0x41, 0x7A, 0x61, 0x0E, 0x0D, 0x34, 0xBC, 0x11, 
1240                 0xAF, 0xE2, 0xF1, 0x8B, 0xFA, 0x2B, 0x54, 0x6C, 0xA3, 0x6C, 0x09, 0x1F, 
1241                 0x0B, 0x43, 0x9B, 0x07, 0x95, 0x83, 0x3F, 0x97, 0x99, 0x89, 0xF5, 0x51, 
1242                 0x41, 0xF6, 0x8E, 0x5D, 0xEF, 0x6D, 0x24, 0x71, 0x41, 0x7A, 0xAF, 0xBE, 
1243                 0x81, 0x71, 0xAB, 0x76, 0x2F, 0x1A, 0x5A, 0xBA, 0xF3, 0xA6, 0x65, 0x7A, 
1244                 0x80, 0x50, 0xCE, 0x23, 0xC3, 0xC7, 0x53, 0xB0, 0x7C, 0x97, 0x77, 0x27, 
1245                 0x70, 0x98, 0xAE, 0xB5, 0x24, 0x66, 0xE1, 0x60, 0x39, 0x41, 0xDA, 0x54, 
1246                 0x01, 0x64, 0xFB, 0x10, 0x33, 0xCE, 0x8B, 0xBE, 0x27, 0xD4, 0x21, 0x57, 
1247                 0xCC, 0x0F, 0x1A, 0xC1, 0x3D, 0xF3, 0xCC, 0x39, 0xF0, 0x2F, 0xAE, 0xF1, 
1248                 0xC0, 0xCD, 0x3B, 0x23, 0x87, 0x49, 0x7E, 0x40, 0x32, 0x6A, 0xD3, 0x96, 
1249                 0x4A, 0xE5, 0x5E, 0x6E, 0x26, 0xFD, 0x8A, 0xCF, 0x7E, 0xFC, 0x37, 0xDE, 
1250                 0x39, 0x0C, 0x53, 0x81, 0x75, 0x08, 0xAF, 0x6B, 0x39, 0x6C, 0xFB, 0xC9, 
1251                 0x79, 0xC0, 0x9B, 0x5F, 0x34, 0x86, 0xB2, 0xDE, 0xC4, 0x19, 0x84, 0x5F, 
1252                 0x0E, 0xED, 0x9B, 0xB8, 0xD3, 0x17, 0xDA, 0x78 };
1253
1254         static byte [] token = { 0x0e, 0xea, 0x7c, 0xe6, 0x5f, 0x35, 0xf2, 0xd8 };
1255
1256         [Test]
1257         public void StrongName_MissingKeyFile_NoDelay ()
1258         {
1259                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "missing.snk" }));
1260                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { false }));
1261                 ab.Save ("StrongName_MissingKeyFile_NoDelay.dll");
1262
1263                 string filename = Path.Combine (tempDir, "StrongName_MissingKeyFile_NoDelay.dll");
1264                 AssemblyName check = AssemblyName.GetAssemblyName (filename);
1265                 // no exception is thrown (file not found)
1266                 // because it's not AssemblyBuilder.Save job to do the signing :-/
1267 #if NET_2_0
1268                 Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#1");
1269                 Assert.IsNull (check.GetPublicKey (), "#2");
1270                 Assert.IsNotNull (check.GetPublicKeyToken (), "#3a");
1271                 Assert.AreEqual (0, check.GetPublicKeyToken ().Length, "#3b");
1272 #else
1273                 Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#1");
1274                 Assert.IsNotNull (check.GetPublicKey (), "#2a");
1275                 Assert.AreEqual (0, check.GetPublicKey ().Length, "#2b");
1276                 Assert.IsNull (check.GetPublicKeyToken (), "#3");
1277 #endif
1278                 Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#4");
1279                 Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#5");
1280                 Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=null") != -1, "#6");
1281         }
1282
1283         [Test]
1284         public void StrongName_KeyFile_Delay ()
1285         {
1286                 string strongfile = Path.Combine (tempDir, "strongname.snk");
1287                 using (FileStream fs = File.OpenWrite (strongfile)) {
1288                         fs.Write (strongName, 0, strongName.Length);
1289                         fs.Close ();
1290                 }
1291                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { strongfile }));
1292                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
1293                 ab.Save ("StrongName_KeyFile_Delay.dll");
1294
1295                 string filename = Path.Combine (tempDir, "StrongName_KeyFile_Delay.dll");
1296                 AssemblyName check = AssemblyName.GetAssemblyName (filename);
1297                 // no public key is inserted into the assembly
1298                 // because it's not AssemblyBuilder.Save job to do the signing :-/
1299 #if NET_2_0
1300                 Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#1");
1301                 Assert.IsNull (check.GetPublicKey (), "#2");
1302                 Assert.IsNotNull (check.GetPublicKeyToken (), "#3a");
1303                 Assert.AreEqual (0, check.GetPublicKeyToken ().Length, "#3b");
1304 #else
1305                 Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#1");
1306                 Assert.IsNotNull (check.GetPublicKey (), "#2a");
1307                 Assert.AreEqual (0, check.GetPublicKey ().Length, "#2b");
1308                 Assert.IsNull (check.GetPublicKeyToken (), "#3");
1309 #endif
1310                 Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#4");
1311                 Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#5");
1312                 Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=null") != -1, "#6");
1313         }
1314
1315         [Test]
1316         public void StrongName_WithoutAttributes ()
1317         {
1318                 // this demonstrate that AssemblyKeyFileAttribute (or AssemblyKeyNameAttribute)
1319                 // aren't required to sign an assembly.
1320                 AssemblyName an = genAssemblyName ();
1321                 an.KeyPair = new StrongNameKeyPair (strongName);
1322                 AssemblyBuilder ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.RunAndSave, tempDir);
1323                 ab.Save ("StrongName_WithoutAttributes.dll");
1324
1325                 string filename = Path.Combine (tempDir, "StrongName_WithoutAttributes.dll");
1326                 AssemblyName check = AssemblyName.GetAssemblyName (filename);
1327                 Assert.IsNotNull (check.GetPublicKey (), "#1a");
1328                 Assert.IsTrue (check.GetPublicKey ().Length > 0, "#1b");
1329                 Assert.AreEqual ("0E-EA-7C-E6-5F-35-F2-D8", BitConverter.ToString (check.GetPublicKeyToken ()), "#2");
1330
1331                 Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#3");
1332                 Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#4");
1333                 Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=0eea7ce65f35f2d8") != -1, "#5");
1334                 Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#6");
1335         }
1336
1337         [Test]
1338         public void SaveUnfinishedTypes ()
1339         {
1340                 mb.DefineType ("TestType", TypeAttributes.Class |
1341                         TypeAttributes.Public | TypeAttributes.Sealed |
1342                         TypeAttributes.AnsiClass | TypeAttributes.AutoClass,
1343                         typeof(object));
1344                 try {
1345                         ab.Save ("def_module");
1346                         Assert.Fail ("#1");
1347                 } catch (NotSupportedException ex) {
1348                         // Type 'TestType' was not completed
1349                         Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
1350                         Assert.IsNull (ex.InnerException, "#3");
1351                         Assert.IsNotNull (ex.Message, "#4");
1352                         Assert.IsTrue (ex.Message.IndexOf ("TestType") != -1, "#5");
1353                 }
1354         }
1355
1356         [Test]
1357         public void GetModules ()
1358         {
1359                 Module[] arr;
1360
1361                 arr = ab.GetModules ();
1362                 Assert.IsNotNull (arr, "#A1");
1363                 // FIXME: This doesn't work on mono
1364                 //Assert.IsTrue (arr.Length >= 2, "#A2");
1365                 foreach (Module m in arr)
1366                         Assert.AreEqual (typeof (ModuleBuilder), m.GetType (), "#A3");
1367
1368                 // Test with no modules
1369                 AssemblyBuilder ab2 = genAssembly ();
1370                 arr = ab2.GetModules ();
1371                 Assert.IsNotNull (arr, "#B1");
1372                 Assert.AreEqual (0, arr.Length, "#B2");
1373         }
1374
1375         [Test]
1376         [Category ("NotWorking")] // bug #351932
1377         public void GetReferencedAssemblies ()
1378         {
1379                 AssemblyBuilder ab1;
1380                 AssemblyBuilder ab2;
1381                 AssemblyBuilder ab3;
1382                 AssemblyName [] refs;
1383                 TypeBuilder tb1;
1384                 TypeBuilder tb2;
1385                 TypeBuilder tb3;
1386                 TypeBuilder tb4;
1387                 ModuleBuilder mb1;
1388                 ModuleBuilder mb2;
1389                 ModuleBuilder mb3;
1390                 AssemblyName an1 = genAssemblyName ();
1391                 an1.Version = new Version (3, 0);
1392                 AssemblyName an2 = genAssemblyName ();
1393                 an2.Version = new Version ("1.2.3.4");
1394                 an2.KeyPair = new StrongNameKeyPair (strongName);
1395                 AssemblyName an3 = genAssemblyName ();
1396
1397                 ab1 = domain.DefineDynamicAssembly (an1,
1398                         AssemblyBuilderAccess.RunAndSave,
1399                         tempDir);
1400                 ab2 = domain.DefineDynamicAssembly (an2,
1401                         AssemblyBuilderAccess.RunAndSave,
1402                         tempDir);
1403                 ab3 = domain.DefineDynamicAssembly (an3,
1404                         AssemblyBuilderAccess.RunAndSave,
1405                         tempDir);
1406
1407                 refs = ab1.GetReferencedAssemblies ();
1408                 Assert.AreEqual (0, refs.Length, "#A1");
1409                 refs = ab2.GetReferencedAssemblies ();
1410                 Assert.AreEqual (0, refs.Length, "#A2");
1411                 refs = ab3.GetReferencedAssemblies ();
1412                 Assert.AreEqual (0, refs.Length, "#A3");
1413
1414                 mb1 = ab1.DefineDynamicModule (an1.Name + ".dll");
1415                 tb1 = mb1.DefineType ("TestType1", TypeAttributes.Class |
1416                         TypeAttributes.Public, typeof (Attribute));
1417                 tb1.CreateType ();
1418
1419                 mb2 = ab2.DefineDynamicModule (an2.Name + ".dll");
1420                 tb2 = mb2.DefineType ("TestType2", TypeAttributes.Class |
1421                         TypeAttributes.Public, tb1);
1422                 tb2.CreateType ();
1423
1424                 mb3 = ab3.DefineDynamicModule (an3.Name + ".dll");
1425                 tb3 = mb3.DefineType ("TestType3", TypeAttributes.Class |
1426                         TypeAttributes.Public, tb1);
1427                 tb3.CreateType ();
1428                 tb4 = mb3.DefineType ("TestType4", TypeAttributes.Class |
1429                         TypeAttributes.Public, tb2);
1430                 tb4.CreateType ();
1431
1432                 refs = ab1.GetReferencedAssemblies ();
1433                 Assert.AreEqual (0, refs.Length, "#B1");
1434                 refs = ab2.GetReferencedAssemblies ();
1435                 Assert.AreEqual (0, refs.Length, "#B2");
1436                 refs = ab3.GetReferencedAssemblies ();
1437                 Assert.AreEqual (0, refs.Length, "#B3");
1438
1439                 ab1.Save (an1.Name + ".dll");
1440                 ab2.Save (an2.Name + ".dll");
1441                 ab3.Save (an3.Name + ".dll");
1442
1443                 refs = ab1.GetReferencedAssemblies ();
1444                 Assert.AreEqual (0, refs.Length, "#C1");
1445                 refs = ab2.GetReferencedAssemblies ();
1446                 Assert.AreEqual (0, refs.Length, "#C2");
1447                 refs = ab3.GetReferencedAssemblies ();
1448                 Assert.AreEqual (0, refs.Length, "#C3");
1449
1450                 string assemblyFile = Path.Combine (tempDir, an1.Name + ".dll");
1451
1452                 using (FileStream fs = File.OpenRead (assemblyFile)) {
1453                         byte [] buffer = new byte [fs.Length];
1454                         fs.Read (buffer, 0, buffer.Length);
1455                         Assembly a = Assembly.Load (buffer);
1456                         refs = a.GetReferencedAssemblies ();
1457                         Assert.AreEqual (1, refs.Length, "#D1");
1458
1459                         Assert.IsNull (refs [0].CodeBase, "#D2:CodeBase");
1460                         Assert.IsNotNull (refs [0].CultureInfo, "#D2:CultureInfo");
1461                         Assert.IsNull (refs [0].EscapedCodeBase, "#D2:EscapedCodeBase");
1462                         Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#D2:Flags");
1463                         Assert.AreEqual (typeof (object).FullName, refs [0].FullName, "#D2:FullName");
1464                         Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#D2:HashAlgorithm");
1465                         Assert.IsNull (refs [0].KeyPair, "#D2:KeyPair");
1466                         Assert.AreEqual ("mscorlib", refs [0].Name, "#D2:Name");
1467 #if NET_2_0
1468                         Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#D2:PA");
1469 #endif
1470
1471                         string FxVersion;
1472 #if MOBILE
1473                         FxVersion = "2.0.5.0;";
1474 #elif NET_4_0
1475                         FxVersion = "4.0.0.0;";
1476 #else
1477                         FxVersion = "2.0.0.0;";
1478 #endif
1479                         Assert.AreEqual (new Version (FxVersion), refs [0].Version, "#D2:Version");
1480                         Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
1481                                 refs [0].VersionCompatibility, "#D2:VersionCompatibility");
1482                         Assert.IsNull (refs [0].GetPublicKey (), "#D2:GetPublicKey");
1483                         Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#D2:GetPublicKeyToken(a)");
1484                         Assert.AreEqual (8, refs [0].GetPublicKeyToken ().Length, "#D2:GetPublicKeyToken(b)");
1485                         Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#D2:ToString");
1486                 }
1487
1488                 assemblyFile = Path.Combine (tempDir, an2.Name + ".dll");
1489
1490                 using (FileStream fs = File.OpenRead (assemblyFile)) {
1491                         byte [] buffer = new byte [fs.Length];
1492                         fs.Read (buffer, 0, buffer.Length);
1493                         Assembly a = Assembly.Load (buffer);
1494                         refs = a.GetReferencedAssemblies ();
1495                         Assert.AreEqual (1, refs.Length, "#E1");
1496
1497                         Assert.IsNull (refs [0].CodeBase, "#E2:CodeBase");
1498                         Assert.IsNotNull (refs [0].CultureInfo, "#E2:CultureInfo(a)");
1499                         Assert.AreEqual (CultureInfo.InvariantCulture, refs [0].CultureInfo, "#E2:CultureInfo(b)");
1500                         Assert.IsNull (refs [0].EscapedCodeBase, "#E2:EscapedCodeBase");
1501                         Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#E2:Flags");
1502                         Assert.AreEqual (an1.Name + ", Version=3.0.0.0, Culture=neutral, PublicKeyToken=null", refs [0].FullName, "#E2:FullName");
1503                         Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#E2:HashAlgorithm");
1504                         Assert.IsNull (refs [0].KeyPair, "#E2:KeyPair");
1505                         Assert.AreEqual (an1.Name, refs [0].Name, "#E2:Name");
1506 #if NET_2_0
1507                         Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#E2:PA");
1508 #endif
1509                         Assert.AreEqual (new Version (3, 0, 0, 0), refs [0].Version, "#E2:Version");
1510                         Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
1511                                 refs [0].VersionCompatibility, "#E2:VersionCompatibility");
1512                         Assert.IsNull (refs [0].GetPublicKey (), "#E2:GetPublicKey");
1513                         Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#E2:GetPublicKeyToken(a)");
1514                         Assert.AreEqual (0, refs [0].GetPublicKeyToken ().Length, "#E2:GetPublicKeyToken(b)");
1515                         Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#E2:ToString");
1516                 }
1517
1518                 assemblyFile = Path.Combine (tempDir, an3.Name + ".dll");
1519
1520                 using (FileStream fs = File.OpenRead (assemblyFile)) {
1521                         byte [] buffer = new byte [fs.Length];
1522                         fs.Read (buffer, 0, buffer.Length);
1523                         Assembly a = Assembly.Load (buffer);
1524                         refs = a.GetReferencedAssemblies ();
1525                         Assert.AreEqual (2, refs.Length, "#F1");
1526
1527                         Assert.IsNull (refs [0].CodeBase, "#F2:CodeBase");
1528                         Assert.IsNotNull (refs [0].CultureInfo, "#F2:CultureInfo(a)");
1529                         Assert.AreEqual (CultureInfo.InvariantCulture, refs [0].CultureInfo, "#F2:CultureInfo(b)");
1530                         Assert.IsNull (refs [0].EscapedCodeBase, "#F2:EscapedCodeBase");
1531                         Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#F2:Flags");
1532                         Assert.AreEqual (an1.Name + ", Version=3.0.0.0, Culture=neutral, PublicKeyToken=null", refs [0].FullName, "#F2:FullName");
1533                         Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#F2:HashAlgorithm");
1534                         Assert.IsNull (refs [0].KeyPair, "#F2:KeyPair");
1535                         Assert.AreEqual (an1.Name, refs [0].Name, "#F2:Name");
1536 #if NET_2_0
1537                         Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#F2:PA");
1538 #endif
1539                         Assert.AreEqual (new Version (3, 0, 0, 0), refs [0].Version, "#F2:Version");
1540                         Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
1541                                 refs [0].VersionCompatibility, "#F2:VersionCompatibility");
1542                         Assert.IsNull (refs [0].GetPublicKey (), "#F2:GetPublicKey");
1543                         Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#F2:GetPublicKeyToken(a)");
1544                         Assert.AreEqual (0, refs [0].GetPublicKeyToken ().Length, "#F2:GetPublicKeyToken(b)");
1545                         Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#F2:ToString");
1546
1547                         Assert.IsNull (refs [1].CodeBase, "#F3:CodeBase");
1548                         Assert.IsNotNull (refs [1].CultureInfo, "#F3:CultureInfo(a)");
1549                         Assert.AreEqual (CultureInfo.InvariantCulture, refs [1].CultureInfo, "#F3:CultureInfo(b)");
1550                         Assert.IsNull (refs [1].EscapedCodeBase, "#F3:EscapedCodeBase");
1551                         Assert.AreEqual (AssemblyNameFlags.None, refs [1].Flags, "#F3:Flags");
1552                         Assert.AreEqual (an2.Name + ", Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8", refs [1].FullName, "#F3:FullName");
1553                         Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [1].HashAlgorithm, "#F3:HashAlgorithm");
1554                         Assert.IsNull (refs [1].KeyPair, "#F3:KeyPair");
1555                         Assert.AreEqual (an2.Name, refs [1].Name, "#F3:Name");
1556 #if NET_2_0
1557                         Assert.AreEqual (ProcessorArchitecture.None, refs [1].ProcessorArchitecture, "#F3:PA");
1558 #endif
1559                         Assert.AreEqual (new Version (1, 2, 3, 4), refs [1].Version, "#F3:Version");
1560                         Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
1561                                 refs [1].VersionCompatibility, "#F3:VersionCompatibility");
1562                         Assert.IsNull (refs [1].GetPublicKey (), "#F3:GetPublicKey");
1563                         Assert.AreEqual (token, refs [1].GetPublicKeyToken (), "#F3:GetPublicKeyToken");
1564                         Assert.AreEqual (refs [1].FullName, refs [1].ToString (), "#F3:ToString");
1565                 }
1566         }
1567
1568         [Test] // bug #78724
1569         public void GetTypes ()
1570         {
1571                 TypeBuilder tb = mb.DefineType ("sometype");
1572                 tb.CreateType ();
1573
1574                 Type[] types = ab.GetTypes ();
1575                 Assert.AreEqual (1, types.Length, "#1");
1576                 Assert.AreEqual ("sometype", types[0].Name, "#2");
1577         }
1578
1579         [Test]
1580         public void AssemblyName_Culture ()
1581         {
1582                 AssemblyName assemblyName = new AssemblyName ();
1583                 assemblyName.Name = "AssemblyNameTest";
1584                 assemblyName.Version = new Version ("1.0.0.0");
1585                 assemblyName.CultureInfo = new CultureInfo ("en-US");
1586
1587                 const string fullName = "AssemblyNameTest, Version=1.0.0.0, Culture=en-US, PublicKeyToken=null";
1588                 const string abName = "AssemblyNameTest, Version=1.0.0.0, Culture=en-US";
1589
1590                 AssertAssemblyName (tempDir, assemblyName, abName, fullName);
1591         }
1592
1593         [Test]
1594         public void AssemblyName_PublicKey ()
1595         {
1596                 AssemblyName assemblyName = new AssemblyName ();
1597                 assemblyName.Name = "AssemblyNameTest_PublicKey";
1598                 assemblyName.Version = new Version ("1.2.3.4");
1599                 assemblyName.KeyPair = new StrongNameKeyPair (strongName);
1600
1601                 Assert.AreEqual ("AssemblyNameTest_PublicKey, Version=1.2.3.4", assemblyName.FullName, "#A1");
1602
1603                 const string fullName = "AssemblyNameTest_PublicKey, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8";
1604
1605                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
1606                         assemblyName, AssemblyBuilderAccess.Save, tempDir);
1607
1608                 AssemblyName abName = ab.GetName ();
1609                 Assert.AreEqual (CultureInfo.InvariantCulture, abName.CultureInfo, "#B1");
1610                 Assert.AreEqual (AssemblyNameFlags.PublicKey, abName.Flags, "#B2");
1611                 Assert.IsNotNull (abName.GetPublicKey () != null, "#B3a");
1612                 Assert.IsTrue (abName.GetPublicKey ().Length > 0, "#B3b");
1613                 Assert.IsNotNull (abName.GetPublicKeyToken (), "#B4a");
1614                 Assert.IsTrue (abName.GetPublicKeyToken ().Length > 0, "#B4b");
1615 #if NET_2_0
1616                 Assert.AreEqual (fullName, abName.FullName, "#B5");
1617 #else
1618                 //Assert.AreEqual ("AssemblyNameTest_PublicKey, Version=1.2.3.4, PublicKeyToken=0eea7ce65f35f2d8", abName.FullName, "#B5");
1619                 Assert.IsTrue (abName.FullName.IndexOf ("AssemblyNameTest_PublicKey, Version=1.2.3.4") != -1, "#B5a");
1620                 Assert.IsTrue (abName.FullName.IndexOf ("PublicKeyToken=0eea7ce65f35f2d8") != -1, "#B5b");
1621 #endif
1622
1623                 ab.Save ("AssemblyNameTest_PublicKey.dll");
1624                 AssemblyName bakedName = AssemblyName.GetAssemblyName (Path.Combine(
1625                         tempDir, "AssemblyNameTest_PublicKey.dll"));
1626
1627                 Assert.AreEqual (CultureInfo.InvariantCulture, bakedName.CultureInfo, "#C1");
1628                 Assert.AreEqual (AssemblyNameFlags.PublicKey, bakedName.Flags, "#C2");
1629                 Assert.IsNotNull (bakedName.GetPublicKeyToken (), "#C3");
1630                 Assert.IsNotNull (bakedName.GetPublicKey (), "#C4");
1631                 Assert.AreEqual (fullName, bakedName.FullName, "#C5");
1632         }
1633
1634         [Test]
1635         public void AssemblyName_MoreCultureInfo ()
1636         {
1637                 AssemblyName assemblyName = new AssemblyName ();
1638                 assemblyName.Name = "AssemblyNameTest_MoreCultureInfo";
1639                 assemblyName.Version = new Version ("1.2.3.4");
1640                 assemblyName.KeyPair = new StrongNameKeyPair (strongName);
1641
1642                 Assert.IsNull (assemblyName.CultureInfo, "#A1");
1643                 Assert.AreEqual ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4", assemblyName.FullName, "#A2");
1644
1645                 const string fullName = "AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8";
1646
1647                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
1648                         assemblyName, AssemblyBuilderAccess.Save, tempDir);
1649
1650                 AssemblyName abName = ab.GetName ();
1651                 Assert.IsNotNull (abName.CultureInfo != null, "#B1");
1652 #if NET_2_0
1653                 Assert.IsTrue (abName.CultureInfo != CultureInfo.InvariantCulture, "#B2a");
1654                 Assert.AreEqual (CultureInfo.InvariantCulture.LCID, abName.CultureInfo.LCID, "#B2a");
1655                 Assert.AreEqual (AssemblyNameFlags.PublicKey, abName.Flags, "#B3");
1656                 Assert.AreEqual (fullName, abName.FullName, "#B4");
1657 #else
1658                 Assert.AreEqual (CultureInfo.InvariantCulture, abName.CultureInfo, "#B2");
1659                 Assert.AreEqual (AssemblyNameFlags.PublicKey, abName.Flags, "#B3");
1660                 //Assert.AreEqual ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4, PublicKeyToken=0eea7ce65f35f2d8", abName.FullName, "#B4");
1661                 Assert.IsTrue (abName.FullName.IndexOf ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4") != -1, "#B4a");
1662                 Assert.IsTrue (abName.FullName.IndexOf ("PublicKeyToken=0eea7ce65f35f2d8") != -1, "#B4b");
1663 #endif
1664
1665                 ab.Save ("AssemblyNameTest_MoreCultureInfo.dll");
1666
1667                 AssemblyName bakedName = AssemblyName.GetAssemblyName (Path.Combine(
1668                         tempDir, "AssemblyNameTest_MoreCultureInfo.dll"));
1669
1670                 Assert.IsNotNull (bakedName.CultureInfo, "#C1");
1671
1672 #if NET_2_0
1673                 Assert.IsTrue (abName.CultureInfo != CultureInfo.InvariantCulture, "#C2a");
1674                 Assert.AreEqual (CultureInfo.InvariantCulture.LCID, abName.CultureInfo.LCID, "#C2b");
1675 #else
1676                 Assert.AreEqual (CultureInfo.InvariantCulture, bakedName.CultureInfo, "#C2");
1677 #endif
1678                 Assert.AreEqual (fullName, bakedName.FullName, "#C3");
1679         }
1680
1681         [Test]
1682         public void AssemblyName_NoVersion ()
1683         {
1684                 AssemblyName assemblyName = new AssemblyName ();
1685                 assemblyName.Name = "AssemblyNameTest";
1686
1687                 const string fullName = "AssemblyNameTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
1688                 const string abName = "AssemblyNameTest, Version=0.0.0.0";
1689
1690                 AssertAssemblyName (tempDir, assemblyName, abName, fullName);
1691         }
1692
1693         [Test]
1694         public void AssemblyName_Version ()
1695         {
1696                 AssemblyName assemblyName = new AssemblyName ();
1697                 assemblyName.Name = "AssemblyNameTest";
1698                 assemblyName.Version = new Version (1, 2, 3, 4);
1699
1700                 string fullName = "AssemblyNameTest, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null";
1701                 string abName = "AssemblyNameTest, Version=1.2.3.4";
1702
1703                 AssertAssemblyName (tempDir, assemblyName, abName, fullName);
1704
1705                 assemblyName = new AssemblyName ();
1706                 assemblyName.Name = "AssemblyNameTest";
1707                 assemblyName.Version = new Version (1, 2);
1708
1709                 fullName = "AssemblyNameTest, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null";
1710                 abName = "AssemblyNameTest, Version=1.2.0.0";
1711
1712                 AssertAssemblyName (tempDir, assemblyName, abName, fullName);
1713         }
1714
1715         [Test]
1716         public void GetType_IgnoreCase ()
1717         {
1718                 TypeBuilder tb = mb.DefineType ("Foo.Test2", TypeAttributes.Public, typeof (object));
1719                 tb.CreateType ();
1720
1721                 Type t;
1722
1723                 t = ab.GetType ("foo.Test2", true, true);
1724                 Assert.AreEqual ("Test2", t.Name, "#1");
1725
1726                 t = ab.GetType ("foo.test2", true, true);
1727                 Assert.AreEqual ("Test2", t.Name, "#2");
1728
1729                 t = ab.GetType ("Foo.test2", true, true);
1730                 Assert.AreEqual ("Test2", t.Name, "#3");
1731         }
1732
1733
1734         [Test]
1735         public void TestGetType ()
1736         {
1737                 TypeBuilder tb = mb.DefineType ("Test", TypeAttributes.Public);
1738
1739                 Assert.IsNull (ab.GetType ("Test", false, true), "#1");
1740                 try {
1741                         ab.GetType ("Test", true, true);
1742                         Assert.Fail ("#2");
1743                 } catch (TypeLoadException) { }
1744
1745                 var res = tb.CreateType ();
1746
1747                 Assert.AreSame (res, ab.GetType ("Test", false, true), "#3");
1748         }
1749
1750         [Test]
1751         public void GetModule ()
1752         {
1753                 var ab = genAssembly ();
1754                 Assert.IsNull (ab.GetModule ("Foo"), "#1");
1755
1756                 var modA = ab.DefineDynamicModule ("Foo");
1757                 var modB = ab.DefineDynamicModule ("Bar");
1758
1759                 Assert.AreSame (modA, ab.GetModule ("Foo"), "#2"); 
1760                 Assert.AreSame (modB, ab.GetModule ("Bar"), "#3"); 
1761                 Assert.IsNull (ab.GetModule ("FooBar"), "#4");
1762         }
1763         
1764         [Test]
1765         public void GetModules2 ()
1766         {
1767                 //XXX this is not the v4 behavior since it returns
1768                 //the manifest module in the place of the first one
1769                 var ab = genAssembly ();
1770                 var modA = ab.DefineDynamicModule ("Foo");
1771                 var modB = ab.DefineDynamicModule ("Bar");
1772                 Assert.AreEqual (2, ab.GetModules ().Length, "#1");
1773                 Assert.AreSame (modA, ab.GetModules () [0], "#2");
1774                 Assert.AreSame (modB, ab.GetModules () [1], "#3");
1775         }
1776
1777         [Test]
1778         [Category ("NotDotNet")] // MS returns the real deal
1779         public void GetReferencedAssemblies_Trivial ()
1780         {
1781                 Assert.IsNotNull (ab.GetReferencedAssemblies (), "#1");
1782         }
1783         
1784         [Test]
1785         public void GetLoadedModules ()
1786         {
1787                 var res = ab.GetLoadedModules (true);
1788                 Assert.IsNotNull (res, "#1");
1789                 Assert.AreEqual (1, res.Length, "#2");
1790                 Assert.AreEqual (mb, res [0], "#3");
1791         }
1792
1793         [ExpectedException (typeof (TypeLoadException))]
1794         public void GetCustomAttributes_NotCreated ()
1795         {
1796                 AssemblyBuilder ab = genAssembly ();
1797                 ModuleBuilder mb = ab.DefineDynamicModule("tester", "tester.dll", false);
1798                 TypeBuilder tb = mb.DefineType ("T");
1799                 tb.SetParent (typeof (Attribute));
1800                 ConstructorBuilder ctor = tb.DefineDefaultConstructor (MethodAttributes.Public);
1801                 object [] o = new object [0];
1802                 CustomAttributeBuilder cab = new CustomAttributeBuilder (ctor, o);
1803                 ab.SetCustomAttribute (cab);
1804
1805                 ab.GetCustomAttributes (true);
1806         }
1807
1808
1809         [Test]
1810         public void GetTypesWithUnfinishedTypeBuilder ()
1811         {
1812                 AssemblyBuilder ab = genAssembly ();
1813                 ModuleBuilder mb = ab.DefineDynamicModule("tester", "tester.dll", false);
1814                 mb.DefineType ("K").CreateType ();
1815                 var tb = mb.DefineType ("T");
1816
1817                 try {
1818                         ab.GetTypes ();
1819                         Assert.Fail ("#1");
1820                 } catch (ReflectionTypeLoadException ex) {
1821                         Assert.AreEqual (1, ex.Types.Length, "#2");
1822                         Assert.AreEqual (1, ex.LoaderExceptions.Length, "#3");
1823                         Assert.IsNull (ex.Types [0], "#4");
1824                         Assert.IsTrue (ex.LoaderExceptions [0] is TypeLoadException, "#5");
1825                 }
1826
1827                 tb.CreateType ();
1828                 var types = ab.GetTypes ();
1829                 Assert.AreEqual (2, types.Length, "#5");
1830                 foreach (var t in types)
1831                         Assert.IsFalse (t is TypeBuilder, "#6_" + t.Name);
1832         }
1833
1834         [Test]
1835         public void DynamicAssemblyGenerationInCurrentDomainShouldNotChangeTheOrderOfCurrentDomainGetAssemblies ()
1836         {
1837                 var initialPosition = GetAssemblyPositionForType (GetType ());
1838                 DefineDynamicAssembly (AppDomain.CurrentDomain);
1839
1840                 var currentPosition = GetAssemblyPositionForType (GetType ());
1841                 Assert.AreEqual (initialPosition, currentPosition);
1842         }
1843
1844         static void DefineDynamicAssembly (AppDomain domain)
1845         {
1846                 AssemblyName assemblyName = new AssemblyName ();
1847                 assemblyName.Name = "MyDynamicAssembly";
1848
1849                 AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.Run);
1850                 ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule ("MyDynamicModule");
1851                 TypeBuilder typeBuilder = moduleBuilder.DefineType ("MyDynamicType", TypeAttributes.Public);
1852                 ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, null);
1853                 ILGenerator ilGenerator = constructorBuilder.GetILGenerator ();
1854                 ilGenerator.EmitWriteLine ("MyDynamicType instantiated!");
1855                 ilGenerator.Emit (OpCodes.Ret);
1856                 typeBuilder.CreateType ();
1857         }
1858
1859         static int GetAssemblyPositionForType (Type type)
1860         {
1861                 var assemblies = AppDomain.CurrentDomain.GetAssemblies ();
1862                 for (int i = 0; i < assemblies.Length; i++)
1863                         if (type.Assembly == assemblies [i])
1864                                 return i;
1865                 return -1;
1866         }
1867
1868
1869         private static void AssertAssemblyName (string tempDir, AssemblyName assemblyName, string abName, string fullName)
1870         {
1871                 AppDomain currentDomain = AppDomain.CurrentDomain;
1872                 AppDomain newDomain = null;
1873
1874                 try {
1875                         AssemblyBuilder ab = currentDomain.DefineDynamicAssembly (
1876                                 assemblyName, AssemblyBuilderAccess.Save, tempDir);
1877                         ab.Save (assemblyName.Name + ".dll");
1878
1879 #if NET_2_0
1880                         // on .NET 2.0, the full name of the AssemblyBuilder matches the
1881                         // fully qualified assembly name
1882                         Assert.AreEqual (fullName, ab.FullName, "#1");
1883 #else
1884                         //Assert.AreEqual (abName, ab.FullName, "#1");
1885 #endif
1886
1887                         AssemblyName an = ab.GetName ();
1888
1889                         Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#2");
1890                         Assert.IsNotNull (an.GetPublicKey (), "#3a");
1891                         Assert.AreEqual (0, an.GetPublicKey ().Length, "#3b");
1892 #if NET_2_0
1893                         Assert.IsNotNull (an.GetPublicKeyToken (), "#4a");
1894                         Assert.AreEqual (0, an.GetPublicKeyToken ().Length, "#4b");
1895 #else
1896                         Assert.IsNull (an.GetPublicKeyToken (), "#4");
1897 #endif
1898
1899                         // load assembly in separate domain, so we can clean-up after the 
1900                         // test
1901                         newDomain = AppDomain.CreateDomain ("test2", currentDomain.Evidence,
1902                                 currentDomain.SetupInformation);
1903
1904                         Helper helper = new Helper (Path.Combine (tempDir, assemblyName.Name + ".dll"),
1905                                 fullName);
1906                         newDomain.DoCallBack (new CrossAppDomainDelegate (helper.Test));
1907                 } finally {
1908                         if (newDomain != null) {
1909                                 AppDomain.Unload (newDomain);
1910                         }
1911                 }
1912         }
1913
1914         [Serializable ()]
1915         private class Helper
1916         {
1917                 private readonly string _assemblyPath;
1918                 private readonly string _assemblyName;
1919
1920                 public Helper (string assemblyPath, string assemblyName)
1921                 {
1922                         _assemblyPath = assemblyPath;
1923                         _assemblyName = assemblyName;
1924                 }
1925
1926                 public void Test ()
1927                 {
1928                         AssemblyName assemblyName = AssemblyName.GetAssemblyName (_assemblyPath);
1929                         Assert.AreEqual (_assemblyName, assemblyName.ToString ());
1930                 }
1931         }
1932 }
1933 }