| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- //
- // ModuleBuilderTest - NUnit Test Cases for the ModuleBuilder class
- //
- // Zoltan Varga ([email protected])
- //
- // (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 TestDefineType_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]
- 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);
- }
- #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]);
- }
- }
- }
|