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