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