File: ModuleBuilderTest.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (489 lines) | stat: -rw-r--r-- 15,037 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//
// ModuleBuilderTest - NUnit Test Cases for the ModuleBuilder class
//
// Zoltan Varga (vargaz@freemail.hu)
//
// (C) Ximian, Inc.  http://www.ximian.com
//
//


using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.IO;
using System.Collections;
using System.Diagnostics.SymbolStore;
using System.Runtime.InteropServices;

using NUnit.Framework;

namespace MonoTests.System.Reflection.Emit
{
	[TestFixture]
	public class ModuleBuilderTest
	{
		static string tempDir = Path.Combine (Path.GetTempPath (), typeof (ModuleBuilderTest).FullName);
		static int nameIndex = 0;

		[SetUp]
		public void SetUp ()
		{
			Random AutoRand = new Random ();
			string basePath = tempDir;
			while (Directory.Exists (tempDir))
				tempDir = Path.Combine (basePath, AutoRand.Next ().ToString ());
			Directory.CreateDirectory (tempDir);
		}

		[TearDown]
		public void TearDown ()
		{
			try {
				// This throws an exception under MS.NET, since the directory contains loaded
				// assemblies.
				Directory.Delete (tempDir, true);
			} catch (Exception) {
			}
		}

		private AssemblyName genAssemblyName ()
		{
			AssemblyName assemblyName = new AssemblyName();
			assemblyName.Name = typeof (ModuleBuilderTest).FullName + (nameIndex ++);
			return assemblyName;
		}

		private AssemblyBuilder genAssembly ()
		{
			return Thread.GetDomain ().DefineDynamicAssembly (genAssemblyName (),
															  AssemblyBuilderAccess.RunAndSave,
															  tempDir);
		}

		[Test]
		public void TestIsTransient ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb1 = ab.DefineDynamicModule ("foo.dll");
			Assert.IsTrue (mb1.IsTransient (), "#1");
			ModuleBuilder mb2 = ab.DefineDynamicModule ("foo2.dll", "foo2.dll");
			Assert.IsFalse (mb2.IsTransient (), "#2");
		}

		// Some of these tests overlap with the tests for Module

		[Test]
		public void TestGlobalData ()
		{
			AssemblyBuilder ab = genAssembly ();

			string resfile = Path.Combine (tempDir, "res");
			using (StreamWriter sw = new StreamWriter (resfile)) {
				sw.WriteLine ("FOO");
			}

			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");

			mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
			mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
			mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
			mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
			mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
			mb.CreateGlobalFunctions ();

			ab.Save ("foo.dll");

			Assembly assembly = Assembly.LoadFrom (Path.Combine (tempDir, "foo.dll"));

			Module module = assembly.GetLoadedModules () [0];

			string [] expectedFieldNames = new string [] {
				"DATA", "DATA2", "DATA3", "DATA4" };
			ArrayList fieldNames = new ArrayList ();
			foreach (FieldInfo fi in module.GetFields ()) {
				fieldNames.Add (fi.Name);
			}
			AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));

			Assert.IsNotNull (module.GetField ("DATA"), "#1");
			Assert.IsNotNull (module.GetField ("DATA2"), "#2");
			Assert.IsNotNull (module.GetField ("DATA3"), "#3");
			Assert.IsNotNull (module.GetField ("DATA4"), "#4");
			Assert.IsNull (module.GetField ("DATA_PRIVATE"), "#5");
			Assert.IsNotNull (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static), "#6");
		}

		[Test]
		public void TestGlobalMethods ()
		{
			AssemblyBuilder builder = genAssembly ();
			ModuleBuilder module = builder.DefineDynamicModule ("MessageModule");
			MethodBuilder method = module.DefinePInvokeMethod ("printf", "libc.so",
															  MethodAttributes.PinvokeImpl | MethodAttributes.Static | MethodAttributes.Public,
															  CallingConventions.Standard, typeof (void), new Type [] { typeof (string) }, CallingConvention.Winapi,
															  CharSet.Auto);
			method.SetImplementationFlags (MethodImplAttributes.PreserveSig |
										   method.GetMethodImplementationFlags ());
			module.CreateGlobalFunctions ();

			Assert.IsNotNull (module.GetMethod ("printf"));
		}

		[Test]
		public void DefineType_Name_Null ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
			try {
				mb.DefineType ((string) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("fullname", ex.ParamName, "#5");
			}
		}

		[Test]
		public void DefineType_Name_Empty ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
			try {
				mb.DefineType (string.Empty);
				Assert.Fail ("#1");
			} catch (ArgumentException ex) {
				// Empty name is not legal
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("fullname", ex.ParamName, "#5");
			}
		}

		[Test]
		public void DefineType_Name_NullChar ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
			try {
				mb.DefineType ("\0test");
				Assert.Fail ("#1");
			} catch (ArgumentException ex) {
				// Illegal name
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("fullname", ex.ParamName, "#5");
			}

			mb.DefineType ("te\0st");
		}

		[Test]
		public void DefineType_InterfaceNotAbstract ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);

			try {
				mb.DefineType ("ITest1", TypeAttributes.Interface);
				Assert.Fail ("#A1");
			} catch (InvalidOperationException ex) {
				// Interface must be declared abstract
				Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
			}

			try {
				mb.DefineType ("ITest2", TypeAttributes.Interface, (Type) null);
				Assert.Fail ("#B1");
			} catch (InvalidOperationException ex) {
				// Interface must be declared abstract
				Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
			}

			// fail on MS .NET 1.1
#if NET_2_0
			TypeBuilder tb = mb.DefineType ("ITest2", TypeAttributes.Interface,
				typeof (object));
			Assert.AreEqual (typeof (object), tb.BaseType, "#C1");

			tb = mb.DefineType ("ITest3", TypeAttributes.Interface,
				typeof (IDisposable));
			Assert.AreEqual (typeof (IDisposable), tb.BaseType, "#D1");
#endif
		}

		[Test]
#if ONLY_1_1
		[Category ("NotDotNet")] // Parent type was not extensible by the given type
#endif
		public void DefineType_Parent_Interface ()
		{
			TypeBuilder tb;

			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);

			tb = mb.DefineType ("Foo", TypeAttributes.Class,
				typeof (ICollection));
			Assert.AreEqual (typeof (ICollection), tb.BaseType, "#1");

			tb = mb.DefineType ("Bar", TypeAttributes.Interface,
				typeof (ICollection));
			Assert.AreEqual (typeof (ICollection), tb.BaseType, "#2");
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void DuplicateTypeName () {
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder module = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);

			var itb = module.DefineType ("TBase", TypeAttributes.Public);

			itb.SetParent (typeof(ValueType));        

			var ptb = module.DefineType ("TBase", TypeAttributes.Public);

			ptb.SetParent (typeof(Enum));
		}

		[Test]
		public void DuplicateSymbolDocument ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);

			// Check that it is possible to redefine a symbol document
			ISymbolDocumentWriter doc1 =
				mb.DefineDocument ("foo.il", SymDocumentType.Text,
								  SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
			ISymbolDocumentWriter doc2 =
				mb.DefineDocument ("foo.il", SymDocumentType.Text,
								  SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
		}

		[Test] // Test case for #80435.
		public void GetArrayMethodToStringTest ()
		{
			AssemblyBuilder assembly = genAssembly ();
			ModuleBuilder module = assembly.DefineDynamicModule ("m", "test.dll");

			Type [] myArrayClass = new Type [1];
			Type [] parameterTypes = { typeof (Array) };
			MethodInfo myMethodInfo = module.GetArrayMethod (myArrayClass.GetType (), "Sort", CallingConventions.Standard, null, parameterTypes);

			string str = myMethodInfo.ToString ();
			Assert.IsNotNull (str);
			// Don't compare string, since MS returns System.Reflection.Emit.SymbolMethod here 
			// (they do not provide an implementation of ToString).
		}

		private static void AssertArrayEqualsSorted (Array o1, Array o2)
		{
			Array s1 = (Array) o1.Clone ();
			Array s2 = (Array) o2.Clone ();

			Array.Sort (s1);
			Array.Sort (s2);

			Assert.AreEqual (s1.Length, s2.Length, "#1");
			for (int i = 0; i < s1.Length; ++i)
				Assert.AreEqual (s1.GetValue (i), s2.GetValue (i), "#2: " + i);
		}

#if NET_2_0
		[Test]
		public void ResolveFieldTokenFieldBuilder ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");

			TypeBuilder tb = mb.DefineType ("foo");
			FieldBuilder fb = tb.DefineField ("foo", typeof (int), 0);
			tb.CreateType ();

			FieldInfo fi = mb.ResolveField (fb.GetToken ().Token);
			Assert.IsNotNull (fi);
			Assert.AreEqual ("foo", fi.Name);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void ResolveFieldTokenInvalidToken ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");

			mb.ResolveField (0x4001234);
		}

		[Test]
		public void ResolveMethodTokenMethodBuilder ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder moduleb = ab.DefineDynamicModule ("foo.dll", "foo.dll");

			TypeBuilder tb = moduleb.DefineType ("foo");
			MethodBuilder mb = tb.DefineMethod("Frub", MethodAttributes.Static, null, new Type[] { typeof(IntPtr) });
			int tok = mb.GetToken().Token;
			mb.SetImplementationFlags(MethodImplAttributes.NoInlining);
			ILGenerator ilgen = mb.GetILGenerator();
			ilgen.Emit(OpCodes.Ret);

			tb.CreateType ();

			MethodBase mi = moduleb.ResolveMethod (tok);
			Assert.IsNotNull (mi);
			Assert.AreEqual ("Frub", mi.Name);
		}
#endif

		[Test]
		public void GetTypes ()
		{
			AssemblyBuilder ab = genAssembly ();
			ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");

			TypeBuilder tb1 = mb.DefineType("Foo", TypeAttributes.Public);

			Type[] types = mb.GetTypes ();
			Assert.AreEqual (1, types.Length);
			Assert.AreEqual (tb1, types [0]);

			// After the type is created, MS seems to return the created type
			tb1.CreateType ();

			types = mb.GetTypes ();
			Assert.AreEqual (tb1.CreateType (), types [0]);
		}

		[Test] // GetTypeToken (Type)
#if NET_2_0
		[Category ("NotDotNet")] // http://support.microsoft.com/kb/950986
#endif
		public void GetTypeToken2_Type_Array ()
		{
			Type type;
			TypeToken typeToken;
			Type resolved_type;

			AssemblyName aname = genAssemblyName ();
			AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
				aname, AssemblyBuilderAccess.RunAndSave);
			ModuleBuilder mb = ab.DefineDynamicModule ("MyModule");

			type = typeof (object []);
			typeToken = mb.GetTypeToken (type);
#if NET_2_0
			Assert.IsFalse (typeToken == TypeToken.Empty, "#A1");
			resolved_type = mb.ResolveType (typeToken.Token);
			Assert.AreEqual (type, resolved_type, "#A2");
#else
			Assert.IsFalse (typeToken.Token == TypeToken.Empty.Token, "#A1");
#endif

#if NET_2_0
			type = typeof (object).MakeArrayType ();
			typeToken = mb.GetTypeToken (type);
			Assert.IsFalse (typeToken == TypeToken.Empty, "#B1");
			resolved_type = mb.ResolveType (typeToken.Token);
			Assert.AreEqual (type, resolved_type, "#B2");
#endif
		}

		[Test] // GetTypeToken (Type)
		public void GetTypeToken2_Type_String ()
		{
			AssemblyName aname = genAssemblyName ();
			AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
				aname, AssemblyBuilderAccess.RunAndSave);
			ModuleBuilder mb = ab.DefineDynamicModule ("MyModule");
			Type type = typeof (string);
			TypeToken typeToken = mb.GetTypeToken (type);
#if NET_2_0
			Assert.IsFalse (typeToken == TypeToken.Empty, "#1");
			Type resolved_type = mb.ResolveType (typeToken.Token);
			Assert.AreEqual (type, resolved_type, "#2");
#else
			Assert.IsFalse (typeToken.Token == TypeToken.Empty.Token, "#1");
#endif
		}

#if NET_2_0
		[Test] // bug #471302
		public void ModuleBuilder_ModuleVersionId ()
		{
			var name = new AssemblyName () { Name = "Foo" };
			var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
				name, AssemblyBuilderAccess.Run);

			var module = assembly.DefineDynamicModule ("Foo");

			Assert.AreNotEqual (new Guid (), module.ModuleVersionId);
		}
#endif

		[Test]
		public void GetType_String_Null ()
		{
			AssemblyName an = genAssemblyName ();
			AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Run);
			ModuleBuilder module = ab.DefineDynamicModule ("GetTypeNullCheck");

			try {
				module.GetType (null);
				Assert.Fail ("Expected ArgumentNullException for GetType(string)");
			}
			catch (ArgumentNullException) {
			}
			try {
				module.GetType (null, true); // ignoreCase
				Assert.Fail ("Expected ArgumentNullException for GetType(string,bool)");
			}
			catch (ArgumentNullException) {
			}
			try {
				module.GetType (null, true, true); // throwOnError, ignoreCase
				Assert.Fail ("Expected ArgumentNullException for GetType(string,bool,bool)");
			}
			catch (ArgumentNullException) {
			}
		}

		[Test]
		public void GetType_String_Empty ()
		{
			AssemblyName an = genAssemblyName ();
			AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Run);
			ModuleBuilder module = ab.DefineDynamicModule ("GetTypeEmptyCheck");

			try {
				module.GetType (String.Empty);
				Assert.Fail ("Expected ArgumentNullException for GetType(string)");
			}
			catch (ArgumentException) {
			}
			try {
				module.GetType (String.Empty, true); // ignoreCase
				Assert.Fail ("Expected ArgumentNullException for GetType(string,bool)");
			}
			catch (ArgumentException) {
			}
			try {
				module.GetType (String.Empty, true, true); // throwOnError, ignoreCase
				Assert.Fail ("Expected ArgumentNullException for GetType(string,bool,bool)");
			}
			catch (ArgumentException) {
			}
		}
	}
}