New tests.
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / ModuleBuilderTest.cs
1 //
2 // ModuleBuilderTest - NUnit Test Cases for the ModuleBuilder 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.Threading;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.IO;
16 using System.Collections;
17 using System.Diagnostics.SymbolStore;
18 using System.Runtime.InteropServices;
19
20 using NUnit.Framework;
21
22 namespace MonoTests.System.Reflection.Emit
23 {
24         [TestFixture]
25         public class ModuleBuilderTest
26         {
27                 static string tempDir = Path.Combine (Path.GetTempPath (), typeof (ModuleBuilderTest).FullName);
28                 static int nameIndex = 0;
29
30                 [SetUp]
31                 public void SetUp ()
32                 {
33                         Random AutoRand = new Random ();
34                         string basePath = tempDir;
35                         while (Directory.Exists (tempDir))
36                                 tempDir = Path.Combine (basePath, AutoRand.Next ().ToString ());
37                         Directory.CreateDirectory (tempDir);
38                 }
39
40                 [TearDown]
41                 public void TearDown ()
42                 {
43                         try {
44                                 // This throws an exception under MS.NET, since the directory contains loaded
45                                 // assemblies.
46                                 Directory.Delete (tempDir, true);
47                         } catch (Exception) {
48                         }
49                 }
50
51                 private AssemblyName genAssemblyName ()
52                 {
53                         AssemblyName assemblyName = new AssemblyName();
54                         assemblyName.Name = typeof (ModuleBuilderTest).FullName + (nameIndex ++);
55                         return assemblyName;
56                 }
57
58                 private AssemblyBuilder genAssembly ()
59                 {
60                         return Thread.GetDomain ().DefineDynamicAssembly (genAssemblyName (),
61                                                                                                                           AssemblyBuilderAccess.RunAndSave,
62                                                                                                                           tempDir);
63                 }
64
65                 [Test]
66                 public void TestIsTransient ()
67                 {
68                         AssemblyBuilder ab = genAssembly ();
69                         ModuleBuilder mb1 = ab.DefineDynamicModule ("foo.dll");
70                         Assert.IsTrue (mb1.IsTransient (), "#1");
71                         ModuleBuilder mb2 = ab.DefineDynamicModule ("foo2.dll", "foo2.dll");
72                         Assert.IsFalse (mb2.IsTransient (), "#2");
73                 }
74
75                 // Some of these tests overlap with the tests for Module
76
77                 [Test]
78                 public void TestGlobalData ()
79                 {
80                         AssemblyBuilder ab = genAssembly ();
81
82                         string resfile = Path.Combine (tempDir, "res");
83                         using (StreamWriter sw = new StreamWriter (resfile)) {
84                                 sw.WriteLine ("FOO");
85                         }
86
87                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
88
89                         mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
90                         mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
91                         mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
92                         mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
93                         mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
94                         mb.CreateGlobalFunctions ();
95
96                         ab.Save ("foo.dll");
97
98                         Assembly assembly = Assembly.LoadFrom (Path.Combine (tempDir, "foo.dll"));
99
100                         Module module = assembly.GetLoadedModules () [0];
101
102                         string [] expectedFieldNames = new string [] {
103                                 "DATA", "DATA2", "DATA3", "DATA4" };
104                         ArrayList fieldNames = new ArrayList ();
105                         foreach (FieldInfo fi in module.GetFields ()) {
106                                 fieldNames.Add (fi.Name);
107                         }
108                         AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));
109
110                         Assert.IsNotNull (module.GetField ("DATA"), "#1");
111                         Assert.IsNotNull (module.GetField ("DATA2"), "#2");
112                         Assert.IsNotNull (module.GetField ("DATA3"), "#3");
113                         Assert.IsNotNull (module.GetField ("DATA4"), "#4");
114                         Assert.IsNull (module.GetField ("DATA_PRIVATE"), "#5");
115                         Assert.IsNotNull (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static), "#6");
116                 }
117
118                 [Test]
119                 public void TestGlobalMethods ()
120                 {
121                         AssemblyBuilder builder = genAssembly ();
122                         ModuleBuilder module = builder.DefineDynamicModule ("MessageModule");
123                         MethodBuilder method = module.DefinePInvokeMethod ("printf", "libc.so",
124                                                                                                                           MethodAttributes.PinvokeImpl | MethodAttributes.Static | MethodAttributes.Public,
125                                                                                                                           CallingConventions.Standard, typeof (void), new Type [] { typeof (string) }, CallingConvention.Winapi,
126                                                                                                                           CharSet.Auto);
127                         method.SetImplementationFlags (MethodImplAttributes.PreserveSig |
128                                                                                    method.GetMethodImplementationFlags ());
129
130                         Assert.IsNull (module.GetMethod ("printf"), "#1");
131
132                         module.CreateGlobalFunctions ();
133
134                         Assert.IsNotNull (module.GetMethod ("printf"), "#2");
135                 }
136
137                 [Test]
138                 public void DefineType_Name_Null ()
139                 {
140                         AssemblyBuilder ab = genAssembly ();
141                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
142                         try {
143                                 mb.DefineType ((string) null);
144                                 Assert.Fail ("#1");
145                         } catch (ArgumentNullException ex) {
146                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
147                                 Assert.IsNull (ex.InnerException, "#3");
148                                 Assert.IsNotNull (ex.Message, "#4");
149                                 Assert.AreEqual ("fullname", ex.ParamName, "#5");
150                         }
151                 }
152
153                 [Test]
154                 public void DefineType_Name_Empty ()
155                 {
156                         AssemblyBuilder ab = genAssembly ();
157                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
158                         try {
159                                 mb.DefineType (string.Empty);
160                                 Assert.Fail ("#1");
161                         } catch (ArgumentException ex) {
162                                 // Empty name is not legal
163                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
164                                 Assert.IsNull (ex.InnerException, "#3");
165                                 Assert.IsNotNull (ex.Message, "#4");
166                                 Assert.AreEqual ("fullname", ex.ParamName, "#5");
167                         }
168                 }
169
170                 [Test]
171                 public void DefineType_Name_NullChar ()
172                 {
173                         AssemblyBuilder ab = genAssembly ();
174                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
175                         try {
176                                 mb.DefineType ("\0test");
177                                 Assert.Fail ("#1");
178                         } catch (ArgumentException ex) {
179                                 // Illegal name
180                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
181                                 Assert.IsNull (ex.InnerException, "#3");
182                                 Assert.IsNotNull (ex.Message, "#4");
183                                 Assert.AreEqual ("fullname", ex.ParamName, "#5");
184                         }
185
186                         mb.DefineType ("te\0st");
187                 }
188
189                 [Test]
190                 public void DefineType_InterfaceNotAbstract ()
191                 {
192                         AssemblyBuilder ab = genAssembly ();
193                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
194
195                         try {
196                                 mb.DefineType ("ITest1", TypeAttributes.Interface);
197                                 Assert.Fail ("#A1");
198                         } catch (InvalidOperationException ex) {
199                                 // Interface must be declared abstract
200                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
201                                 Assert.IsNull (ex.InnerException, "#A3");
202                                 Assert.IsNotNull (ex.Message, "#A4");
203                         }
204
205                         try {
206                                 mb.DefineType ("ITest2", TypeAttributes.Interface, (Type) null);
207                                 Assert.Fail ("#B1");
208                         } catch (InvalidOperationException ex) {
209                                 // Interface must be declared abstract
210                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
211                                 Assert.IsNull (ex.InnerException, "#B3");
212                                 Assert.IsNotNull (ex.Message, "#B4");
213                         }
214
215                         // fail on MS .NET 1.1
216 #if NET_2_0
217                         TypeBuilder tb = mb.DefineType ("ITest2", TypeAttributes.Interface,
218                                 typeof (object));
219                         Assert.AreEqual (typeof (object), tb.BaseType, "#C1");
220
221                         tb = mb.DefineType ("ITest3", TypeAttributes.Interface,
222                                 typeof (IDisposable));
223                         Assert.AreEqual (typeof (IDisposable), tb.BaseType, "#D1");
224 #endif
225                 }
226
227                 [Test]
228 #if ONLY_1_1
229                 [Category ("NotDotNet")] // Parent type was not extensible by the given type
230 #endif
231                 public void DefineType_Parent_Interface ()
232                 {
233                         TypeBuilder tb;
234
235                         AssemblyBuilder ab = genAssembly ();
236                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
237
238                         tb = mb.DefineType ("Foo", TypeAttributes.Class,
239                                 typeof (ICollection));
240                         Assert.AreEqual (typeof (ICollection), tb.BaseType, "#1");
241
242                         tb = mb.DefineType ("Bar", TypeAttributes.Interface,
243                                 typeof (ICollection));
244                         Assert.AreEqual (typeof (ICollection), tb.BaseType, "#2");
245                 }
246
247                 [Test]
248                 [ExpectedException (typeof (ArgumentException))]
249                 public void DuplicateTypeName () {
250                         AssemblyBuilder ab = genAssembly ();
251                         ModuleBuilder module = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
252
253                         var itb = module.DefineType ("TBase", TypeAttributes.Public);
254
255                         itb.SetParent (typeof(ValueType));        
256
257                         var ptb = module.DefineType ("TBase", TypeAttributes.Public);
258
259                         ptb.SetParent (typeof(Enum));
260                 }
261
262                 [Test]
263                 public void DuplicateSymbolDocument ()
264                 {
265                         AssemblyBuilder ab = genAssembly ();
266                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
267
268                         // Check that it is possible to redefine a symbol document
269                         ISymbolDocumentWriter doc1 =
270                                 mb.DefineDocument ("foo.il", SymDocumentType.Text,
271                                                                   SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
272                         ISymbolDocumentWriter doc2 =
273                                 mb.DefineDocument ("foo.il", SymDocumentType.Text,
274                                                                   SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
275                 }
276
277                 [Test] // Test case for #80435.
278                 public void GetArrayMethodToStringTest ()
279                 {
280                         AssemblyBuilder assembly = genAssembly ();
281                         ModuleBuilder module = assembly.DefineDynamicModule ("m", "test.dll");
282
283                         Type [] myArrayClass = new Type [1];
284                         Type [] parameterTypes = { typeof (Array) };
285                         MethodInfo myMethodInfo = module.GetArrayMethod (myArrayClass.GetType (), "Sort", CallingConventions.Standard, null, parameterTypes);
286
287                         string str = myMethodInfo.ToString ();
288                         Assert.IsNotNull (str);
289                         // Don't compare string, since MS returns System.Reflection.Emit.SymbolMethod here 
290                         // (they do not provide an implementation of ToString).
291                 }
292
293                 private static void AssertArrayEqualsSorted (Array o1, Array o2)
294                 {
295                         Array s1 = (Array) o1.Clone ();
296                         Array s2 = (Array) o2.Clone ();
297
298                         Array.Sort (s1);
299                         Array.Sort (s2);
300
301                         Assert.AreEqual (s1.Length, s2.Length, "#1");
302                         for (int i = 0; i < s1.Length; ++i)
303                                 Assert.AreEqual (s1.GetValue (i), s2.GetValue (i), "#2: " + i);
304                 }
305
306 #if NET_2_0
307                 [Test]
308                 public void ResolveFieldTokenFieldBuilder ()
309                 {
310                         AssemblyBuilder ab = genAssembly ();
311                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
312
313                         TypeBuilder tb = mb.DefineType ("foo");
314                         FieldBuilder fb = tb.DefineField ("foo", typeof (int), 0);
315                         tb.CreateType ();
316
317                         FieldInfo fi = mb.ResolveField (fb.GetToken ().Token);
318                         Assert.IsNotNull (fi);
319                         Assert.AreEqual ("foo", fi.Name);
320                 }
321
322                 [Test]
323                 [ExpectedException (typeof (ArgumentException))]
324                 public void ResolveFieldTokenInvalidToken ()
325                 {
326                         AssemblyBuilder ab = genAssembly ();
327                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
328
329                         mb.ResolveField (0x4001234);
330                 }
331
332                 [Test]
333                 public void ResolveMethodTokenMethodBuilder ()
334                 {
335                         AssemblyBuilder ab = genAssembly ();
336                         ModuleBuilder moduleb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
337
338                         TypeBuilder tb = moduleb.DefineType ("foo");
339                         MethodBuilder mb = tb.DefineMethod("Frub", MethodAttributes.Static, null, new Type[] { typeof(IntPtr) });
340                         int tok = mb.GetToken().Token;
341                         mb.SetImplementationFlags(MethodImplAttributes.NoInlining);
342                         ILGenerator ilgen = mb.GetILGenerator();
343                         ilgen.Emit(OpCodes.Ret);
344
345                         tb.CreateType ();
346
347                         MethodBase mi = moduleb.ResolveMethod (tok);
348                         Assert.IsNotNull (mi);
349                         Assert.AreEqual ("Frub", mi.Name);
350                 }
351 #endif
352
353                 [Test]
354                 public void GetTypes ()
355                 {
356                         AssemblyBuilder ab = genAssembly ();
357                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
358
359                         TypeBuilder tb1 = mb.DefineType("Foo", TypeAttributes.Public);
360
361                         Type[] types = mb.GetTypes ();
362                         Assert.AreEqual (1, types.Length);
363                         Assert.AreEqual (tb1, types [0]);
364
365                         // After the type is created, MS seems to return the created type
366                         tb1.CreateType ();
367
368                         types = mb.GetTypes ();
369                         Assert.AreEqual (tb1.CreateType (), types [0]);
370                 }
371
372                 [Test] // GetTypeToken (Type)
373 #if NET_2_0
374                 [Category ("NotDotNet")] // http://support.microsoft.com/kb/950986
375 #endif
376                 public void GetTypeToken2_Type_Array ()
377                 {
378                         Type type;
379                         TypeToken typeToken;
380                         Type resolved_type;
381
382                         AssemblyName aname = genAssemblyName ();
383                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
384                                 aname, AssemblyBuilderAccess.RunAndSave);
385                         ModuleBuilder mb = ab.DefineDynamicModule ("MyModule");
386
387                         type = typeof (object []);
388                         typeToken = mb.GetTypeToken (type);
389 #if NET_2_0
390                         Assert.IsFalse (typeToken == TypeToken.Empty, "#A1");
391                         resolved_type = mb.ResolveType (typeToken.Token);
392                         Assert.AreEqual (type, resolved_type, "#A2");
393 #else
394                         Assert.IsFalse (typeToken.Token == TypeToken.Empty.Token, "#A1");
395 #endif
396
397 #if NET_2_0
398                         type = typeof (object).MakeArrayType ();
399                         typeToken = mb.GetTypeToken (type);
400                         Assert.IsFalse (typeToken == TypeToken.Empty, "#B1");
401                         resolved_type = mb.ResolveType (typeToken.Token);
402                         Assert.AreEqual (type, resolved_type, "#B2");
403 #endif
404                 }
405
406                 [Test] // GetTypeToken (Type)
407                 public void GetTypeToken2_Type_String ()
408                 {
409                         AssemblyName aname = genAssemblyName ();
410                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
411                                 aname, AssemblyBuilderAccess.RunAndSave);
412                         ModuleBuilder mb = ab.DefineDynamicModule ("MyModule");
413                         Type type = typeof (string);
414                         TypeToken typeToken = mb.GetTypeToken (type);
415 #if NET_2_0
416                         Assert.IsFalse (typeToken == TypeToken.Empty, "#1");
417                         Type resolved_type = mb.ResolveType (typeToken.Token);
418                         Assert.AreEqual (type, resolved_type, "#2");
419 #else
420                         Assert.IsFalse (typeToken.Token == TypeToken.Empty.Token, "#1");
421 #endif
422                 }
423
424 #if NET_2_0
425                 [Test] // bug #471302
426                 public void ModuleBuilder_ModuleVersionId ()
427                 {
428                         var name = new AssemblyName () { Name = "Foo" };
429                         var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
430                                 name, AssemblyBuilderAccess.Run);
431
432                         var module = assembly.DefineDynamicModule ("Foo");
433
434                         Assert.AreNotEqual (new Guid (), module.ModuleVersionId);
435                 }
436 #endif
437
438                 [Test]
439                 public void GetType_String_Null ()
440                 {
441                         AssemblyName an = genAssemblyName ();
442                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Run);
443                         ModuleBuilder module = ab.DefineDynamicModule ("GetTypeNullCheck");
444
445                         try {
446                                 module.GetType (null);
447                                 Assert.Fail ("Expected ArgumentNullException for GetType(string)");
448                         }
449                         catch (ArgumentNullException) {
450                         }
451                         try {
452                                 module.GetType (null, true); // ignoreCase
453                                 Assert.Fail ("Expected ArgumentNullException for GetType(string,bool)");
454                         }
455                         catch (ArgumentNullException) {
456                         }
457                         try {
458                                 module.GetType (null, true, true); // throwOnError, ignoreCase
459                                 Assert.Fail ("Expected ArgumentNullException for GetType(string,bool,bool)");
460                         }
461                         catch (ArgumentNullException) {
462                         }
463                 }
464
465                 [Test]
466                 public void GetType_String_Empty ()
467                 {
468                         AssemblyName an = genAssemblyName ();
469                         AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Run);
470                         ModuleBuilder module = ab.DefineDynamicModule ("GetTypeEmptyCheck");
471
472                         try {
473                                 module.GetType (String.Empty);
474                                 Assert.Fail ("Expected ArgumentNullException for GetType(string)");
475                         }
476                         catch (ArgumentException) {
477                         }
478                         try {
479                                 module.GetType (String.Empty, true); // ignoreCase
480                                 Assert.Fail ("Expected ArgumentNullException for GetType(string,bool)");
481                         }
482                         catch (ArgumentException) {
483                         }
484                         try {
485                                 module.GetType (String.Empty, true, true); // throwOnError, ignoreCase
486                                 Assert.Fail ("Expected ArgumentNullException for GetType(string,bool,bool)");
487                         }
488                         catch (ArgumentException) {
489                         }
490                 }
491         }
492 }