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