New test + update.
[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                         module.CreateGlobalFunctions ();
130
131                         Assert.IsNotNull (module.GetMethod ("printf"));
132                 }
133
134                 [Test]
135                 public void TestDefineType_InterfaceNotAbstract ()
136                 {
137                         AssemblyBuilder ab = genAssembly ();
138                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
139
140                         try {
141                                 mb.DefineType ("ITest1", TypeAttributes.Interface);
142                                 Assert.Fail ("#A1");
143                         } catch (InvalidOperationException ex) {
144                                 // Interface must be declared abstract
145                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
146                                 Assert.IsNull (ex.InnerException, "#A3");
147                                 Assert.IsNotNull (ex.Message, "#A4");
148                         }
149
150                         try {
151                                 mb.DefineType ("ITest2", TypeAttributes.Interface, (Type) null);
152                                 Assert.Fail ("#B1");
153                         } catch (InvalidOperationException ex) {
154                                 // Interface must be declared abstract
155                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
156                                 Assert.IsNull (ex.InnerException, "#B3");
157                                 Assert.IsNotNull (ex.Message, "#B4");
158                         }
159
160                         // fail on MS .NET 1.1
161 #if NET_2_0
162                         TypeBuilder tb = mb.DefineType ("ITest2", TypeAttributes.Interface,
163                                 typeof (object));
164                         Assert.AreEqual (typeof (object), tb.BaseType, "#C1");
165
166                         tb = mb.DefineType ("ITest3", TypeAttributes.Interface,
167                                 typeof (IDisposable));
168                         Assert.AreEqual (typeof (IDisposable), tb.BaseType, "#D1");
169 #endif
170                 }
171
172                 [Test]
173                 public void DuplicateSymbolDocument ()
174                 {
175                         AssemblyBuilder ab = genAssembly ();
176                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
177
178                         // Check that it is possible to redefine a symbol document
179                         ISymbolDocumentWriter doc1 =
180                                 mb.DefineDocument ("foo.il", SymDocumentType.Text,
181                                                                   SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
182                         ISymbolDocumentWriter doc2 =
183                                 mb.DefineDocument ("foo.il", SymDocumentType.Text,
184                                                                   SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
185                 }
186
187                 [Test] // Test case for #80435.
188                 public void GetArrayMethodToStringTest ()
189                 {
190                         AssemblyBuilder assembly = genAssembly ();
191                         ModuleBuilder module = assembly.DefineDynamicModule ("m", "test.dll");
192
193                         Type [] myArrayClass = new Type [1];
194                         Type [] parameterTypes = { typeof (Array) };
195                         MethodInfo myMethodInfo = module.GetArrayMethod (myArrayClass.GetType (), "Sort", CallingConventions.Standard, null, parameterTypes);
196
197                         string str = myMethodInfo.ToString ();
198                         Assert.IsNotNull (str);
199                         // Don't compare string, since MS returns System.Reflection.Emit.SymbolMethod here 
200                         // (they do not provide an implementation of ToString).
201                 }
202
203                 private static void AssertArrayEqualsSorted (Array o1, Array o2)
204                 {
205                         Array s1 = (Array) o1.Clone ();
206                         Array s2 = (Array) o2.Clone ();
207
208                         Array.Sort (s1);
209                         Array.Sort (s2);
210
211                         Assert.AreEqual (s1.Length, s2.Length, "#1");
212                         for (int i = 0; i < s1.Length; ++i)
213                                 Assert.AreEqual (s1.GetValue (i), s2.GetValue (i), "#2: " + i);
214                 }
215
216 #if NET_2_0
217                 [Test]
218                 public void ResolveFieldTokenFieldBuilder ()
219                 {
220                         AssemblyBuilder ab = genAssembly ();
221                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
222
223                         TypeBuilder tb = mb.DefineType ("foo");
224                         FieldBuilder fb = tb.DefineField ("foo", typeof (int), 0);
225                         tb.CreateType ();
226
227                         FieldInfo fi = mb.ResolveField (fb.GetToken ().Token);
228                         Assert.IsNotNull (fi);
229                         Assert.AreEqual ("foo", fi.Name);
230                 }
231
232                 [Test]
233                 [ExpectedException (typeof (ArgumentException))]
234                 public void ResolveFieldTokenInvalidToken ()
235                 {
236                         AssemblyBuilder ab = genAssembly ();
237                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
238
239                         mb.ResolveField (0x4001234);
240                 }
241
242                 [Test]
243                 public void ResolveMethodTokenMethodBuilder ()
244                 {
245                         AssemblyBuilder ab = genAssembly ();
246                         ModuleBuilder moduleb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
247
248                         TypeBuilder tb = moduleb.DefineType ("foo");
249                         MethodBuilder mb = tb.DefineMethod("Frub", MethodAttributes.Static, null, new Type[] { typeof(IntPtr) });
250                         int tok = mb.GetToken().Token;
251                         mb.SetImplementationFlags(MethodImplAttributes.NoInlining);
252                         ILGenerator ilgen = mb.GetILGenerator();
253                         ilgen.Emit(OpCodes.Ret);
254
255                         tb.CreateType ();
256
257                         MethodBase mi = moduleb.ResolveMethod (tok);
258                         Assert.IsNotNull (mi);
259                         Assert.AreEqual ("Frub", mi.Name);
260                 }
261 #endif
262
263                 [Test]
264                 public void GetTypes ()
265                 {
266                         AssemblyBuilder ab = genAssembly ();
267                         ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
268                         
269             TypeBuilder tb1 = mb.DefineType("Foo", TypeAttributes.Public);
270                         
271                         Type[] types = mb.GetTypes ();
272                         Assert.AreEqual (1, types.Length);
273                         Assert.AreEqual (tb1, types [0]);
274
275                         // After the type is created, MS seems to return the created type
276                         tb1.CreateType ();
277
278                         types = mb.GetTypes ();
279                         Assert.AreEqual (tb1.CreateType (), types [0]);
280                 }
281         }
282 }