2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / AssemblyBuilderTest.cs
1 //\r
2 // AssemblyBuilderTest.cs - NUnit Test Cases for the AssemblyBuilder class\r
3 //\r
4 // Zoltan Varga (vargaz@freemail.hu)\r
5 //\r
6 // (C) Ximian, Inc.  http://www.ximian.com\r
7 //\r
8 //\r
9 \r
10 \r
11 using System;\r
12 using System.Threading;\r
13 using System.Reflection;\r
14 using System.Reflection.Emit;\r
15 using System.IO;\r
16 using System.Configuration.Assemblies;\r
17 \r
18 using NUnit.Framework;\r
19 \r
20 namespace MonoTests.System.Reflection.Emit\r
21 {\r
22 \r
23 [TestFixture]\r
24 public class AssemblyBuilderTest : Assertion\r
25 {       \r
26         [AttributeUsage (AttributeTargets.Assembly)]\r
27         public sealed class FooAttribute : Attribute\r
28         {\r
29                 public FooAttribute (string arg)\r
30                 {\r
31                 }\r
32 \r
33                 public FooAttribute ()\r
34                 {\r
35                 }\r
36         }\r
37 \r
38         static int nameIndex = 0;\r
39 \r
40         static AppDomain domain;\r
41 \r
42         static AssemblyBuilder ab;\r
43
44         static ModuleBuilder mb;
45 \r
46         string tempDir = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.Emit.AssemblyBuilderTest");\r
47 \r
48         [SetUp]\r
49         protected void SetUp () {\r
50                 if (Directory.Exists (tempDir))\r
51                         Directory.Delete (tempDir, true);\r
52 \r
53                 Directory.CreateDirectory (tempDir);\r
54 \r
55                 for (int i = 1; i < 3; ++i) {\r
56                         string resFile = Path.Combine (tempDir, "res" + i + ".txt");\r
57                         using (StreamWriter sw = new StreamWriter (resFile)) {\r
58                                 sw.WriteLine ("FOO");\r
59                         }\r
60                 }\r
61 \r
62                 domain = Thread.GetDomain ();\r
63                 ab = genAssembly ();\r
64                 mb = ab.DefineDynamicModule ("def_module");\r
65         }\r
66 \r
67         [TearDown]\r
68         protected void TearDown () {\r
69                 if (Directory.Exists (tempDir))\r
70                         Directory.Delete (tempDir, true);\r
71         }               \r
72 \r
73         private AssemblyName genAssemblyName () {\r
74                 AssemblyName assemblyName = new AssemblyName();\r
75                 assemblyName.Name = "MonoTests.System.Reflection.Emit.AssemblyBuilderTest" + (nameIndex ++);\r
76                 return assemblyName;\r
77         }\r
78 \r
79         private AssemblyBuilder genAssembly () {\r
80                 return domain.DefineDynamicAssembly (genAssemblyName (),\r
81                                                                                          AssemblyBuilderAccess.RunAndSave,\r
82                                                                                          tempDir);\r
83         }\r
84 \r
85         private MethodInfo genEntryFunction (AssemblyBuilder assembly) {\r
86                 ModuleBuilder module = assembly.DefineDynamicModule("module1");\r
87                 TypeBuilder tb = module.DefineType ("A");\r
88                 MethodBuilder mb = tb.DefineMethod ("A",\r
89                         MethodAttributes.Static, typeof (void), new Type [0]);\r
90                 mb.GetILGenerator ().Emit (OpCodes.Ret);\r
91                 return mb;\r
92         }\r
93 \r
94         [ExpectedException (typeof (NotSupportedException))]\r
95         public void TestCodeBase () {\r
96                 string codebase = ab.CodeBase;\r
97         }\r
98 \r
99         [ExpectedException (typeof (NotSupportedException))]\r
100         public void TestLocation () {\r
101                 string location = ab.Location;\r
102         }\r
103 \r
104         public void TestEntryPoint () {\r
105                 AssertEquals ("EntryPoint defaults to null",\r
106                                           null, ab.EntryPoint);\r
107 \r
108                 MethodInfo mi = genEntryFunction (ab);\r
109                 ab.SetEntryPoint (mi);\r
110 \r
111                 AssertEquals ("EntryPoint works", mi, ab.EntryPoint);\r
112         }\r
113 \r
114         public void TestSetEntryPoint () {\r
115                 // Check invalid arguments\r
116                 try {\r
117                         ab.SetEntryPoint (null);\r
118                         Fail ();\r
119                 }\r
120                 catch (ArgumentNullException) {\r
121                 }\r
122 \r
123                 // Check method from other assembly\r
124                 try {\r
125                         ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));\r
126                         Fail ();\r
127                 }\r
128                 catch (InvalidOperationException) {\r
129                 }\r
130         }\r
131 \r
132         public void TestIsDefined () {\r
133                 CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });\r
134                 ab.SetCustomAttribute (cab);\r
135 \r
136                 AssertEquals ("IsDefined works",\r
137                                           true, ab.IsDefined (typeof (FooAttribute), false));\r
138                 AssertEquals ("IsDefined works",\r
139                                           false, ab.IsDefined (typeof (AssemblyVersionAttribute), false));\r
140         }\r
141 \r
142         [ExpectedException (typeof (NotSupportedException))]\r
143         public void TestGetManifestResourceNames () {\r
144                 ab.GetManifestResourceNames ();\r
145         }\r
146 \r
147         [ExpectedException (typeof (NotSupportedException))]\r
148         public void TestGetManifestResourceInfo () {\r
149                 ab.GetManifestResourceInfo ("foo");\r
150         }\r
151 \r
152         [ExpectedException (typeof (NotSupportedException))]\r
153         public void TestGetManifestResourceStream1 () {\r
154                 ab.GetManifestResourceStream ("foo");\r
155         }\r
156 \r
157         [ExpectedException (typeof (NotSupportedException))]\r
158         public void TestGetManifestResourceStream2 () {\r
159                 ab.GetManifestResourceStream (typeof (int), "foo");\r
160         }\r
161 \r
162         [ExpectedException (typeof (NotSupportedException))]\r
163         public void TestGetFiles1 () {\r
164                 ab.GetFiles ();\r
165         }\r
166 \r
167         [ExpectedException (typeof (NotSupportedException))]\r
168         public void TestGetFiles2 () {\r
169                 ab.GetFiles (true);\r
170         }\r
171 \r
172         [ExpectedException (typeof (NotSupportedException))]\r
173         public void TestGetFile () {\r
174                 ab.GetFile ("foo");\r
175         }\r
176 \r
177         [ExpectedException (typeof (NotSupportedException))]\r
178         public void TestGetExportedTypes () {\r
179                 ab.GetExportedTypes ();\r
180         }\r
181 \r
182         [ExpectedException (typeof (ArgumentNullException))]\r
183         public void TestGetDynamicModule1 () {\r
184                 ab.GetDynamicModule (null);\r
185         }\r
186 \r
187         [ExpectedException (typeof (ArgumentException))]\r
188         public void TestGetDynamicModule2 () {\r
189                 ab.GetDynamicModule ("");\r
190         }\r
191 \r
192         public void TestGetDynamicModule3 () {\r
193                 AssertNull (ab.GetDynamicModule ("FOO2"));\r
194 \r
195                 ModuleBuilder mb = ab.DefineDynamicModule ("FOO");\r
196 \r
197                 AssertEquals (mb, ab.GetDynamicModule ("FOO"));\r
198 \r
199                 AssertNull (ab.GetDynamicModule ("FOO4"));\r
200         }\r
201 \r
202 #if NET_1_1\r
203         public void TestImageRuntimeVersion () {\r
204                 string version = ab.ImageRuntimeVersion;\r
205                 Assert (version.Length > 0);\r
206         }\r
207 #endif\r
208 \r
209         [ExpectedException (typeof (ArgumentNullException))]\r
210         public void TestAddResourceFileNullName () {\r
211                 ab.AddResourceFile (null, "foo.txt");\r
212         }\r
213 \r
214         [ExpectedException (typeof (ArgumentNullException))]\r
215         public void TestAddResourceFileNullFilename () {\r
216                 ab.AddResourceFile ("foo", null);\r
217         }\r
218 \r
219         [ExpectedException (typeof (ArgumentException))]\r
220         public void TestAddResourceFileEmptyName () {\r
221                 ab.AddResourceFile ("", "foo.txt");\r
222         }\r
223 \r
224         [ExpectedException (typeof (ArgumentException))]\r
225         public void TestAddResourceFileEmptyFilename () {\r
226                 ab.AddResourceFile ("foo", "");\r
227         }\r
228 \r
229         [ExpectedException (typeof (FileNotFoundException))]\r
230         public void TestAddResourceFileNonexistentFile () {\r
231                 ab.AddResourceFile ("foo", "not-existent.txt");\r
232         }\r
233 \r
234         [ExpectedException (typeof (ArgumentException))]\r
235         public void TestAddResourceFileDuplicateFileName () {\r
236                 ab.AddResourceFile ("foo", "res1.txt");\r
237                 ab.AddResourceFile ("foo2", "res1.txt");\r
238         }\r
239 \r
240         [ExpectedException (typeof (ArgumentException))]\r
241         public void TestAddResourceFileDuplicateName () {\r
242                 ab.AddResourceFile ("foo", "res1.txt");\r
243                 ab.AddResourceFile ("foo", "res2.txt");\r
244         }\r
245 \r
246         [ExpectedException (typeof (ArgumentException))]\r
247         public void TestAddResourceFileFilenameIncludesPath () {\r
248                 ab.AddResourceFile ("foo", "/tmp/res1.txt");\r
249         }\r
250 \r
251         public void TestAddResourceFile () {\r
252                 ab.AddResourceFile ("foo", "res2.txt", ResourceAttributes.Public);\r
253 \r
254                 ab.Save ("TestAddResourceFile.dll");\r
255 \r
256                 // TODO: Test reading back\r
257         }\r
258 \r
259         public void TestDefineResource () {\r
260                 ab.DefineResource ("foo", "FOO", "foo.txt", ResourceAttributes.Public);\r
261                 ab.DefineResource ("foo2", "FOO", "foo2.txt");\r
262 \r
263                 ab.Save ("TestDefineResource.dll");\r
264         }\r
265 \r
266         [ExpectedException (typeof (ArgumentNullException))]\r
267         public void TestDefineDynamicModuleNullName () {\r
268                 ab.DefineDynamicModule (null, "foo.txt");\r
269         }\r
270 \r
271         [ExpectedException (typeof (ArgumentNullException))]\r
272         public void TestDefineDynamicModuleNullFilename () {\r
273                 ab.DefineDynamicModule ("foo", null);\r
274         }\r
275 \r
276         [ExpectedException (typeof (ArgumentException))]\r
277         public void TestDefineDynamicModuleEmptyName () {\r
278                 ab.DefineDynamicModule ("", "foo.txt");\r
279         }\r
280 \r
281         [ExpectedException (typeof (ArgumentException))]\r
282         public void TestDefineDynamicModuleEmptyFilename () {\r
283                 ab.DefineDynamicModule ("foo", "");\r
284         }\r
285 \r
286         [ExpectedException (typeof (ArgumentException))]\r
287         public void TestDefineDynamicModuleDuplicateFileName () {\r
288                 ab.DefineDynamicModule ("foo", "res1.txt");\r
289                 ab.DefineDynamicModule ("foo2", "res1.txt");\r
290         }\r
291 \r
292         [ExpectedException (typeof (ArgumentException))]\r
293         public void TestDefineDynamicModuleDuplicateName () {\r
294                 ab.DefineDynamicModule ("foo", "res1.txt");\r
295                 ab.DefineDynamicModule ("foo", "res2.txt");\r
296         }\r
297 \r
298         [ExpectedException (typeof (ArgumentException))]\r
299         public void TestDefineDynamicModuleFilenameIncludesPath () {\r
300                 ab.DefineDynamicModule ("foo", "/tmp/res1.txt");\r
301         }\r
302 \r
303         [ExpectedException (typeof (ArgumentException))]\r
304         public void TestDefineDynamicModule5 () {\r
305                 // Filename without extension\r
306                 ab.DefineDynamicModule ("foo", "foo");\r
307         }\r
308 \r
309         /*\r
310         [ExpectedException (typeof (ArgumentException))]\r
311         public void TestDefineDynamicModule6 () {\r
312                 // Name too long\r
313                 string name = "";\r
314                 for (int i = 0; i < 259; ++i)\r
315                         name = name + "A";\r
316 \r
317                 try {\r
318                         ab.DefineDynamicModule (name);\r
319                 }\r
320                 catch (Exception) {\r
321                         Fail ();\r
322                 }\r
323 \r
324                 name = name + "A";\r
325                 // LAMESPEC: According to MSDN, this should throw an ArgumentException\r
326 \r
327                 ab.DefineDynamicModule (name);\r
328         }\r
329         */\r
330 \r
331         [ExpectedException (typeof (InvalidOperationException))]\r
332         public void TestDefineDynamicModule7 () {\r
333                 // Called when assembly was already saved\r
334                 ab.Save ("TestDefineDynamicModule7.dll");\r
335                 ab.DefineDynamicModule ("foo", "foo.dll");\r
336         }\r
337 \r
338         [ExpectedException (typeof (NotSupportedException))]\r
339         public void TestDefineDynamicModule8 () {\r
340                 // Called on an assembly defined with the Run attribute\r
341                 AssemblyBuilder ab = \r
342                         domain.DefineDynamicAssembly (genAssemblyName (),\r
343                                                                                   AssemblyBuilderAccess.Run,\r
344                                                                                   tempDir);\r
345                 ab.DefineDynamicModule ("foo", "foo.dll");\r
346         }\r
347 \r
348         public void TestDefineDynamicModule () {\r
349                 ab.DefineDynamicModule ("foo", "foo.dll");\r
350                 ab.DefineDynamicModule ("foo2", true);\r
351                 ab.DefineDynamicModule ("foo3", "foo3.dll");\r
352                 ab.DefineDynamicModule ("foo4", "foo4.dll", true);\r
353         }\r
354 \r
355         [ExpectedException (typeof (ArgumentNullException))]\r
356         public void TestDefineUnmanagedResource1 () {\r
357                 // Null argument\r
358                 ab.DefineUnmanagedResource ((byte[])null);\r
359         }\r
360 \r
361         [ExpectedException (typeof (ArgumentNullException))]\r
362         public void TestDefineUnmanagedResource2 () {\r
363                 // Null argument\r
364                 ab.DefineUnmanagedResource ((string)null);\r
365         }\r
366 \r
367         [ExpectedException (typeof (ArgumentException))]\r
368         public void TestDefineUnmanagedResource3 () {\r
369                 // Empty filename\r
370                 ab.DefineUnmanagedResource ("");\r
371         }\r
372 \r
373         [ExpectedException (typeof (FileNotFoundException))]\r
374         public void TestDefineUnmanagedResource4 () {\r
375                 // Nonexistent file\r
376                 ab.DefineUnmanagedResource ("not-exists.txt");\r
377         }\r
378 \r
379         [ExpectedException (typeof (ArgumentNullException))]\r
380         public void TestSetCustomAttribute1 () {\r
381                 // Null argument\r
382                 ab.SetCustomAttribute (null);\r
383         }\r
384 \r
385         [ExpectedException (typeof (ArgumentNullException))]\r
386         public void TestSetCustomAttribute2 () {\r
387                 // Null constructor\r
388                 ab.SetCustomAttribute (null, new byte [0]);\r
389         }\r
390 \r
391         [ExpectedException (typeof (ArgumentNullException))]\r
392         public void TestSetCustomAttribute3 () {\r
393                 // Null blob\r
394                 ab.SetCustomAttribute (typeof(AssemblyCompanyAttribute).GetConstructor (new Type [] { typeof (String) }), null);\r
395         }\r
396 \r
397         public void TestSetCustomAttribute () {\r
398                 // Test common custom attributes\r
399 \r
400                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4"}));\r
401 \r
402                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "bar"}));\r
403 \r
404                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }), new object [] { AssemblyHashAlgorithm.MD5 }));\r
405 \r
406                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint)0x0100 }));\r
407 \r
408                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));\r
409 \r
410                 ab.SetCustomAttribute (typeof (FooAttribute).GetConstructor (new Type [] {}), new byte [0]);\r
411 \r
412                 ab.Save ("TestSetCustomAttribute.dll");\r
413 \r
414                 /* We should read back the assembly and check the attributes ... */\r
415         }\r
416
417         // strongname generated using "sn -k unit.snk"
418         static byte[] strongName = { 
419                 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32, 
420                 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x7F, 0x7C, 0xEA, 0x4A, 
421                 0x28, 0x33, 0xD8, 0x3C, 0x86, 0x90, 0x86, 0x91, 0x11, 0xBB, 0x30, 0x0D, 
422                 0x3D, 0x69, 0x04, 0x4C, 0x48, 0xF5, 0x4F, 0xE7, 0x64, 0xA5, 0x82, 0x72, 
423                 0x5A, 0x92, 0xC4, 0x3D, 0xC5, 0x90, 0x93, 0x41, 0xC9, 0x1D, 0x34, 0x16, 
424                 0x72, 0x2B, 0x85, 0xC1, 0xF3, 0x99, 0x62, 0x07, 0x32, 0x98, 0xB7, 0xE4, 
425                 0xFA, 0x75, 0x81, 0x8D, 0x08, 0xB9, 0xFD, 0xDB, 0x00, 0x25, 0x30, 0xC4, 
426                 0x89, 0x13, 0xB6, 0x43, 0xE8, 0xCC, 0xBE, 0x03, 0x2E, 0x1A, 0x6A, 0x4D, 
427                 0x36, 0xB1, 0xEB, 0x49, 0x26, 0x6C, 0xAB, 0xC4, 0x29, 0xD7, 0x8F, 0x25, 
428                 0x11, 0xA4, 0x7C, 0x81, 0x61, 0x97, 0xCB, 0x44, 0x2D, 0x80, 0x49, 0x93, 
429                 0x48, 0xA7, 0xC9, 0xAB, 0xDB, 0xCF, 0xA3, 0x34, 0xCB, 0x6B, 0x86, 0xE0, 
430                 0x4D, 0x27, 0xFC, 0xA7, 0x4F, 0x36, 0xCA, 0x13, 0x42, 0xD3, 0x83, 0xC4, 
431                 0x06, 0x6E, 0x12, 0xE0, 0xA1, 0x3D, 0x9F, 0xA9, 0xEC, 0xD1, 0xC6, 0x08, 
432                 0x1B, 0x3D, 0xF5, 0xDB, 0x4C, 0xD4, 0xF0, 0x2C, 0xAA, 0xFC, 0xBA, 0x18, 
433                 0x6F, 0x48, 0x7E, 0xB9, 0x47, 0x68, 0x2E, 0xF6, 0x1E, 0x67, 0x1C, 0x7E, 
434                 0x0A, 0xCE, 0x10, 0x07, 0xC0, 0x0C, 0xAD, 0x5E, 0xC1, 0x53, 0x70, 0xD5, 
435                 0xE7, 0x25, 0xCA, 0x37, 0x5E, 0x49, 0x59, 0xD0, 0x67, 0x2A, 0xBE, 0x92, 
436                 0x36, 0x86, 0x8A, 0xBF, 0x3E, 0x17, 0x04, 0xFB, 0x1F, 0x46, 0xC8, 0x10, 
437                 0x5C, 0x93, 0x02, 0x43, 0x14, 0x96, 0x6A, 0xD9, 0x87, 0x17, 0x62, 0x7D, 
438                 0x3A, 0x45, 0xBE, 0x35, 0xDE, 0x75, 0x0B, 0x2A, 0xCE, 0x7D, 0xF3, 0x19, 
439                 0x85, 0x4B, 0x0D, 0x6F, 0x8D, 0x15, 0xA3, 0x60, 0x61, 0x28, 0x55, 0x46, 
440                 0xCE, 0x78, 0x31, 0x04, 0x18, 0x3C, 0x56, 0x4A, 0x3F, 0xA4, 0xC9, 0xB1, 
441                 0x41, 0xED, 0x22, 0x80, 0xA1, 0xB3, 0xE2, 0xC7, 0x1B, 0x62, 0x85, 0xE4, 
442                 0x81, 0x39, 0xCB, 0x1F, 0x95, 0xCC, 0x61, 0x61, 0xDF, 0xDE, 0xF3, 0x05, 
443                 0x68, 0xB9, 0x7D, 0x4F, 0xFF, 0xF3, 0xC0, 0x0A, 0x25, 0x62, 0xD9, 0x8A, 
444                 0x8A, 0x9E, 0x99, 0x0B, 0xFB, 0x85, 0x27, 0x8D, 0xF6, 0xD4, 0xE1, 0xB9, 
445                 0xDE, 0xB4, 0x16, 0xBD, 0xDF, 0x6A, 0x25, 0x9C, 0xAC, 0xCD, 0x91, 0xF7, 
446                 0xCB, 0xC1, 0x81, 0x22, 0x0D, 0xF4, 0x7E, 0xEC, 0x0C, 0x84, 0x13, 0x5A, 
447                 0x74, 0x59, 0x3F, 0x3E, 0x61, 0x00, 0xD6, 0xB5, 0x4A, 0xA1, 0x04, 0xB5, 
448                 0xA7, 0x1C, 0x29, 0xD0, 0xE1, 0x11, 0x19, 0xD7, 0x80, 0x5C, 0xEE, 0x08, 
449                 0x15, 0xEB, 0xC9, 0xA8, 0x98, 0xF5, 0xA0, 0xF0, 0x92, 0x2A, 0xB0, 0xD3, 
450                 0xC7, 0x8C, 0x8D, 0xBB, 0x88, 0x96, 0x4F, 0x18, 0xF0, 0x8A, 0xF9, 0x31, 
451                 0x9E, 0x44, 0x94, 0x75, 0x6F, 0x78, 0x04, 0x10, 0xEC, 0xF3, 0xB0, 0xCE, 
452                 0xA0, 0xBE, 0x7B, 0x25, 0xE1, 0xF7, 0x8A, 0xA8, 0xD4, 0x63, 0xC2, 0x65, 
453                 0x47, 0xCC, 0x5C, 0xED, 0x7D, 0x8B, 0x07, 0x4D, 0x76, 0x29, 0x53, 0xAC, 
454                 0x27, 0x8F, 0x5D, 0x78, 0x56, 0xFA, 0x99, 0x45, 0xA2, 0xCC, 0x65, 0xC4, 
455                 0x54, 0x13, 0x9F, 0x38, 0x41, 0x7A, 0x61, 0x0E, 0x0D, 0x34, 0xBC, 0x11, 
456                 0xAF, 0xE2, 0xF1, 0x8B, 0xFA, 0x2B, 0x54, 0x6C, 0xA3, 0x6C, 0x09, 0x1F, 
457                 0x0B, 0x43, 0x9B, 0x07, 0x95, 0x83, 0x3F, 0x97, 0x99, 0x89, 0xF5, 0x51, 
458                 0x41, 0xF6, 0x8E, 0x5D, 0xEF, 0x6D, 0x24, 0x71, 0x41, 0x7A, 0xAF, 0xBE, 
459                 0x81, 0x71, 0xAB, 0x76, 0x2F, 0x1A, 0x5A, 0xBA, 0xF3, 0xA6, 0x65, 0x7A, 
460                 0x80, 0x50, 0xCE, 0x23, 0xC3, 0xC7, 0x53, 0xB0, 0x7C, 0x97, 0x77, 0x27, 
461                 0x70, 0x98, 0xAE, 0xB5, 0x24, 0x66, 0xE1, 0x60, 0x39, 0x41, 0xDA, 0x54, 
462                 0x01, 0x64, 0xFB, 0x10, 0x33, 0xCE, 0x8B, 0xBE, 0x27, 0xD4, 0x21, 0x57, 
463                 0xCC, 0x0F, 0x1A, 0xC1, 0x3D, 0xF3, 0xCC, 0x39, 0xF0, 0x2F, 0xAE, 0xF1, 
464                 0xC0, 0xCD, 0x3B, 0x23, 0x87, 0x49, 0x7E, 0x40, 0x32, 0x6A, 0xD3, 0x96, 
465                 0x4A, 0xE5, 0x5E, 0x6E, 0x26, 0xFD, 0x8A, 0xCF, 0x7E, 0xFC, 0x37, 0xDE, 
466                 0x39, 0x0C, 0x53, 0x81, 0x75, 0x08, 0xAF, 0x6B, 0x39, 0x6C, 0xFB, 0xC9, 
467                 0x79, 0xC0, 0x9B, 0x5F, 0x34, 0x86, 0xB2, 0xDE, 0xC4, 0x19, 0x84, 0x5F, 
468                 0x0E, 0xED, 0x9B, 0xB8, 0xD3, 0x17, 0xDA, 0x78 };
469
470         [Test]
471         public void StrongName_MissingKeyFile_NoDelay () 
472         {
473                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "missing.snk" }));
474                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { false }));
475                 ab.Save ("StrongName_MissingKeyFile_NoDelay.dll");
476
477                 string filename = Path.Combine (tempDir, "StrongName_MissingKeyFile_NoDelay.dll");
478                 AssemblyName check = AssemblyName.GetAssemblyName (filename);
479                 // no exception is thrown (file not found)
480                 // because it's not AssemblyBuilder.Save job to do the signing :-/
481                 AssertNull ("Token", check.GetPublicKeyToken ());
482         }
483
484         [Test]
485         public void StrongName_KeyFile_Delay () 
486         {
487                 string strongfile = Path.Combine (tempDir, "strongname.snk");
488                 using (FileStream fs = File.OpenWrite (strongfile)) {
489                         fs.Write (strongName, 0, strongName.Length);
490                         fs.Close ();
491                 }
492                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { strongfile }));
493                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
494                 ab.Save ("StrongName_KeyFile_Delay.dll");
495
496                 string filename = Path.Combine (tempDir, "StrongName_KeyFile_Delay.dll");
497                 AssemblyName check = AssemblyName.GetAssemblyName (filename);
498                 // no public key is inserted into the assembly
499                 // because it's not AssemblyBuilder.Save job to do the signing :-/
500                 AssertNull ("Token", check.GetPublicKeyToken ());
501         }
502
503         [Test]
504         public void StrongName_WithoutAttributes () 
505         {
506                 // this demonstrate that AssemblyKeyFileAttribute (or AssemblyKeyNameAttribute)
507                 // aren't required to sign an assembly.
508                 AssemblyName an = genAssemblyName ();
509                 an.KeyPair = new StrongNameKeyPair (strongName);
510                 AssemblyBuilder ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.RunAndSave, tempDir);
511                 ab.Save ("StrongName_WithoutAttributes.dll");
512
513                 string filename = Path.Combine (tempDir, "StrongName_WithoutAttributes.dll");
514                 AssemblyName check = AssemblyName.GetAssemblyName (filename);
515                 AssertEquals ("Token", "0E-EA-7C-E6-5F-35-F2-D8", BitConverter.ToString (check.GetPublicKeyToken ()));
516         }
517
518         [Test]
519         [ExpectedException (typeof (NotSupportedException))]
520         public void SaveUnfinishedTypes ()
521         {
522                 TypeBuilder typeBuilder = mb.DefineType ("TestType", TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.AnsiClass | TypeAttributes.AutoClass, typeof(object));
523
524                 ab.Save ("def_module");
525         }
526
527 }\r
528 }\r
529 \r