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