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