2004-02-16 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 \r
44         string tempDir = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.Emit.AssemblyBuilderTest");\r
45 \r
46         [SetUp]\r
47         protected void SetUp () {\r
48                 if (Directory.Exists (tempDir))\r
49                         Directory.Delete (tempDir, true);\r
50 \r
51                 Directory.CreateDirectory (tempDir);\r
52 \r
53                 for (int i = 1; i < 3; ++i) {\r
54                         string resFile = Path.Combine (tempDir, "res" + i + ".txt");\r
55                         using (StreamWriter sw = new StreamWriter (resFile)) {\r
56                                 sw.WriteLine ("FOO");\r
57                         }\r
58                 }\r
59 \r
60                 domain = Thread.GetDomain ();\r
61                 ab = genAssembly ();\r
62                 ab.DefineDynamicModule ("def_module");\r
63         }\r
64 \r
65         [TearDown]\r
66         protected void TearDown () {\r
67                 if (Directory.Exists (tempDir))\r
68                         Directory.Delete (tempDir, true);\r
69         }               \r
70 \r
71         private AssemblyName genAssemblyName () {\r
72                 AssemblyName assemblyName = new AssemblyName();\r
73                 assemblyName.Name = "MonoTests.System.Reflection.Emit.AssemblyBuilderTest" + (nameIndex ++);\r
74                 return assemblyName;\r
75         }\r
76 \r
77         private AssemblyBuilder genAssembly () {\r
78                 return domain.DefineDynamicAssembly (genAssemblyName (),\r
79                                                                                          AssemblyBuilderAccess.RunAndSave,\r
80                                                                                          tempDir);\r
81         }\r
82 \r
83         private MethodInfo genEntryFunction (AssemblyBuilder assembly) {\r
84                 ModuleBuilder module = assembly.DefineDynamicModule("module1");\r
85                 TypeBuilder tb = module.DefineType ("A");\r
86                 MethodBuilder mb = tb.DefineMethod ("A",\r
87                         MethodAttributes.Static, typeof (void), new Type [0]);\r
88                 mb.GetILGenerator ().Emit (OpCodes.Ret);\r
89                 return mb;\r
90         }\r
91 \r
92         [ExpectedException (typeof (NotSupportedException))]\r
93         public void TestCodeBase () {\r
94                 string codebase = ab.CodeBase;\r
95         }\r
96 \r
97         [ExpectedException (typeof (NotSupportedException))]\r
98         public void TestLocation () {\r
99                 string location = ab.Location;\r
100         }\r
101 \r
102         public void TestEntryPoint () {\r
103                 AssertEquals ("EntryPoint defaults to null",\r
104                                           null, ab.EntryPoint);\r
105 \r
106                 MethodInfo mi = genEntryFunction (ab);\r
107                 ab.SetEntryPoint (mi);\r
108 \r
109                 AssertEquals ("EntryPoint works", mi, ab.EntryPoint);\r
110         }\r
111 \r
112         public void TestSetEntryPoint () {\r
113                 // Check invalid arguments\r
114                 try {\r
115                         ab.SetEntryPoint (null);\r
116                         Fail ();\r
117                 }\r
118                 catch (ArgumentNullException) {\r
119                 }\r
120 \r
121                 // Check method from other assembly\r
122                 try {\r
123                         ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));\r
124                         Fail ();\r
125                 }\r
126                 catch (InvalidOperationException) {\r
127                 }\r
128         }\r
129 \r
130         public void TestIsDefined () {\r
131                 CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });\r
132                 ab.SetCustomAttribute (cab);\r
133 \r
134                 AssertEquals ("IsDefined works",\r
135                                           true, ab.IsDefined (typeof (FooAttribute), false));\r
136                 AssertEquals ("IsDefined works",\r
137                                           false, ab.IsDefined (typeof (AssemblyVersionAttribute), false));\r
138         }\r
139 \r
140         [ExpectedException (typeof (NotSupportedException))]\r
141         public void TestGetManifestResourceNames () {\r
142                 ab.GetManifestResourceNames ();\r
143         }\r
144 \r
145         [ExpectedException (typeof (NotSupportedException))]\r
146         public void TestGetManifestResourceInfo () {\r
147                 ab.GetManifestResourceInfo ("foo");\r
148         }\r
149 \r
150         [ExpectedException (typeof (NotSupportedException))]\r
151         public void TestGetManifestResourceStream1 () {\r
152                 ab.GetManifestResourceStream ("foo");\r
153         }\r
154 \r
155         [ExpectedException (typeof (NotSupportedException))]\r
156         public void TestGetManifestResourceStream2 () {\r
157                 ab.GetManifestResourceStream (typeof (int), "foo");\r
158         }\r
159 \r
160         [ExpectedException (typeof (NotSupportedException))]\r
161         public void TestGetFiles1 () {\r
162                 ab.GetFiles ();\r
163         }\r
164 \r
165         [ExpectedException (typeof (NotSupportedException))]\r
166         public void TestGetFiles2 () {\r
167                 ab.GetFiles (true);\r
168         }\r
169 \r
170         [ExpectedException (typeof (NotSupportedException))]\r
171         public void TestGetFile () {\r
172                 ab.GetFile ("foo");\r
173         }\r
174 \r
175         [ExpectedException (typeof (NotSupportedException))]\r
176         public void TestGetExportedTypes () {\r
177                 ab.GetExportedTypes ();\r
178         }\r
179 \r
180         [ExpectedException (typeof (ArgumentNullException))]\r
181         public void TestGetDynamicModule1 () {\r
182                 ab.GetDynamicModule (null);\r
183         }\r
184 \r
185         [ExpectedException (typeof (ArgumentException))]\r
186         public void TestGetDynamicModule2 () {\r
187                 ab.GetDynamicModule ("");\r
188         }\r
189 \r
190         public void TestGetDynamicModule3 () {\r
191                 AssertNull (ab.GetDynamicModule ("FOO2"));\r
192 \r
193                 ModuleBuilder mb = ab.DefineDynamicModule ("FOO");\r
194 \r
195                 AssertEquals (mb, ab.GetDynamicModule ("FOO"));\r
196 \r
197                 AssertNull (ab.GetDynamicModule ("FOO4"));\r
198         }\r
199 \r
200 #if NET_1_1\r
201         public void TestImageRuntimeVersion () {\r
202                 string version = ab.ImageRuntimeVersion;\r
203                 Assert (version.Length > 0);\r
204         }\r
205 #endif\r
206 \r
207         [ExpectedException (typeof (ArgumentNullException))]\r
208         public void TestAddResourceFileNullName () {\r
209                 ab.AddResourceFile (null, "foo.txt");\r
210         }\r
211 \r
212         [ExpectedException (typeof (ArgumentNullException))]\r
213         public void TestAddResourceFileNullFilename () {\r
214                 ab.AddResourceFile ("foo", null);\r
215         }\r
216 \r
217         [ExpectedException (typeof (ArgumentException))]\r
218         public void TestAddResourceFileEmptyName () {\r
219                 ab.AddResourceFile ("", "foo.txt");\r
220         }\r
221 \r
222         [ExpectedException (typeof (ArgumentException))]\r
223         public void TestAddResourceFileEmptyFilename () {\r
224                 ab.AddResourceFile ("foo", "");\r
225         }\r
226 \r
227         [ExpectedException (typeof (FileNotFoundException))]\r
228         public void TestAddResourceFileNonexistentFile () {\r
229                 ab.AddResourceFile ("foo", "not-existent.txt");\r
230         }\r
231 \r
232         [ExpectedException (typeof (ArgumentException))]\r
233         public void TestAddResourceFileDuplicateFileName () {\r
234                 ab.AddResourceFile ("foo", "res1.txt");\r
235                 ab.AddResourceFile ("foo2", "res1.txt");\r
236         }\r
237 \r
238         [ExpectedException (typeof (ArgumentException))]\r
239         public void TestAddResourceFileDuplicateName () {\r
240                 ab.AddResourceFile ("foo", "res1.txt");\r
241                 ab.AddResourceFile ("foo", "res2.txt");\r
242         }\r
243 \r
244         [ExpectedException (typeof (ArgumentException))]\r
245         public void TestAddResourceFileFilenameIncludesPath () {\r
246                 ab.AddResourceFile ("foo", "/tmp/res1.txt");\r
247         }\r
248 \r
249         public void TestAddResourceFile () {\r
250                 ab.AddResourceFile ("foo", "res2.txt", ResourceAttributes.Public);\r
251 \r
252                 ab.Save ("TestAddResourceFile.dll");\r
253 \r
254                 // TODO: Test reading back\r
255         }\r
256 \r
257         public void TestDefineResource () {\r
258                 ab.DefineResource ("foo", "FOO", "foo.txt", ResourceAttributes.Public);\r
259                 ab.DefineResource ("foo2", "FOO", "foo2.txt");\r
260 \r
261                 ab.Save ("TestDefineResource.dll");\r
262         }\r
263 \r
264         [ExpectedException (typeof (ArgumentNullException))]\r
265         public void TestDefineDynamicModuleNullName () {\r
266                 ab.DefineDynamicModule (null, "foo.txt");\r
267         }\r
268 \r
269         [ExpectedException (typeof (ArgumentNullException))]\r
270         public void TestDefineDynamicModuleNullFilename () {\r
271                 ab.DefineDynamicModule ("foo", null);\r
272         }\r
273 \r
274         [ExpectedException (typeof (ArgumentException))]\r
275         public void TestDefineDynamicModuleEmptyName () {\r
276                 ab.DefineDynamicModule ("", "foo.txt");\r
277         }\r
278 \r
279         [ExpectedException (typeof (ArgumentException))]\r
280         public void TestDefineDynamicModuleEmptyFilename () {\r
281                 ab.DefineDynamicModule ("foo", "");\r
282         }\r
283 \r
284         [ExpectedException (typeof (ArgumentException))]\r
285         public void TestDefineDynamicModuleDuplicateFileName () {\r
286                 ab.DefineDynamicModule ("foo", "res1.txt");\r
287                 ab.DefineDynamicModule ("foo2", "res1.txt");\r
288         }\r
289 \r
290         [ExpectedException (typeof (ArgumentException))]\r
291         public void TestDefineDynamicModuleDuplicateName () {\r
292                 ab.DefineDynamicModule ("foo", "res1.txt");\r
293                 ab.DefineDynamicModule ("foo", "res2.txt");\r
294         }\r
295 \r
296         [ExpectedException (typeof (ArgumentException))]\r
297         public void TestDefineDynamicModuleFilenameIncludesPath () {\r
298                 ab.DefineDynamicModule ("foo", "/tmp/res1.txt");\r
299         }\r
300 \r
301         [ExpectedException (typeof (ArgumentException))]\r
302         public void TestDefineDynamicModule5 () {\r
303                 // Filename without extension\r
304                 ab.DefineDynamicModule ("foo", "foo");\r
305         }\r
306 \r
307         /*\r
308         [ExpectedException (typeof (ArgumentException))]\r
309         public void TestDefineDynamicModule6 () {\r
310                 // Name too long\r
311                 string name = "";\r
312                 for (int i = 0; i < 259; ++i)\r
313                         name = name + "A";\r
314 \r
315                 try {\r
316                         ab.DefineDynamicModule (name);\r
317                 }\r
318                 catch (Exception) {\r
319                         Fail ();\r
320                 }\r
321 \r
322                 name = name + "A";\r
323                 // LAMESPEC: According to MSDN, this should throw an ArgumentException\r
324 \r
325                 ab.DefineDynamicModule (name);\r
326         }\r
327         */\r
328 \r
329         [ExpectedException (typeof (InvalidOperationException))]\r
330         public void TestDefineDynamicModule7 () {\r
331                 // Called when assembly was already saved\r
332                 ab.Save ("TestDefineDynamicModule7.dll");\r
333                 ab.DefineDynamicModule ("foo", "foo.dll");\r
334         }\r
335 \r
336         [ExpectedException (typeof (NotSupportedException))]\r
337         public void TestDefineDynamicModule8 () {\r
338                 // Called on an assembly defined with the Run attribute\r
339                 AssemblyBuilder ab = \r
340                         domain.DefineDynamicAssembly (genAssemblyName (),\r
341                                                                                   AssemblyBuilderAccess.Run,\r
342                                                                                   tempDir);\r
343                 ab.DefineDynamicModule ("foo", "foo.dll");\r
344         }\r
345 \r
346         public void TestDefineDynamicModule () {\r
347                 ab.DefineDynamicModule ("foo", "foo.dll");\r
348                 ab.DefineDynamicModule ("foo2", true);\r
349                 ab.DefineDynamicModule ("foo3", "foo3.dll");\r
350                 ab.DefineDynamicModule ("foo4", "foo4.dll", true);\r
351         }\r
352 \r
353         [ExpectedException (typeof (ArgumentNullException))]\r
354         public void TestDefineUnmanagedResource1 () {\r
355                 // Null argument\r
356                 ab.DefineUnmanagedResource ((byte[])null);\r
357         }\r
358 \r
359         [ExpectedException (typeof (ArgumentNullException))]\r
360         public void TestDefineUnmanagedResource2 () {\r
361                 // Null argument\r
362                 ab.DefineUnmanagedResource ((string)null);\r
363         }\r
364 \r
365         [ExpectedException (typeof (ArgumentException))]\r
366         public void TestDefineUnmanagedResource3 () {\r
367                 // Empty filename\r
368                 ab.DefineUnmanagedResource ("");\r
369         }\r
370 \r
371         [ExpectedException (typeof (FileNotFoundException))]\r
372         public void TestDefineUnmanagedResource4 () {\r
373                 // Nonexistent file\r
374                 ab.DefineUnmanagedResource ("not-exists.txt");\r
375         }\r
376 \r
377         [ExpectedException (typeof (ArgumentNullException))]\r
378         public void TestSetCustomAttribute1 () {\r
379                 // Null argument\r
380                 ab.SetCustomAttribute (null);\r
381         }\r
382 \r
383         [ExpectedException (typeof (ArgumentNullException))]\r
384         public void TestSetCustomAttribute2 () {\r
385                 // Null constructor\r
386                 ab.SetCustomAttribute (null, new byte [0]);\r
387         }\r
388 \r
389         [ExpectedException (typeof (ArgumentNullException))]\r
390         public void TestSetCustomAttribute3 () {\r
391                 // Null blob\r
392                 ab.SetCustomAttribute (typeof(AssemblyCompanyAttribute).GetConstructor (new Type [] { typeof (String) }), null);\r
393         }\r
394 \r
395         public void TestSetCustomAttribute () {\r
396                 // Test common custom attributes\r
397 \r
398                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4"}));\r
399 \r
400                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "bar"}));\r
401 \r
402                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }), new object [] { AssemblyHashAlgorithm.MD5 }));\r
403 \r
404                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint)0x0100 }));\r
405 \r
406                 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));\r
407 \r
408                 ab.SetCustomAttribute (typeof (FooAttribute).GetConstructor (new Type [] {}), new byte [0]);\r
409 \r
410                 ab.Save ("TestSetCustomAttribute.dll");\r
411 \r
412                 /* We should read back the assembly and check the attributes ... */\r
413         }\r
414 \r
415 }\r
416 }\r
417 \r