ModuleBuilderTest.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // ModuleBuilderTest - NUnit Test Cases for the ModuleBuilder class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. //
  8. //
  9. using System;
  10. using System.Threading;
  11. using System.Reflection;
  12. using System.Reflection.Emit;
  13. using System.IO;
  14. using System.Collections;
  15. using System.Diagnostics.SymbolStore;
  16. using System.Runtime.InteropServices;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.Reflection.Emit
  19. {
  20. [TestFixture]
  21. public class ModuleBuilderTest
  22. {
  23. static string tempDir = Path.Combine (Path.GetTempPath (), typeof (ModuleBuilderTest).FullName);
  24. static int nameIndex = 0;
  25. [SetUp]
  26. public void SetUp ()
  27. {
  28. Random AutoRand = new Random ();
  29. string basePath = tempDir;
  30. while (Directory.Exists (tempDir))
  31. tempDir = Path.Combine (basePath, AutoRand.Next ().ToString ());
  32. Directory.CreateDirectory (tempDir);
  33. }
  34. [TearDown]
  35. public void TearDown ()
  36. {
  37. try {
  38. // This throws an exception under MS.NET, since the directory contains loaded
  39. // assemblies.
  40. Directory.Delete (tempDir, true);
  41. } catch (Exception) {
  42. }
  43. }
  44. private AssemblyName genAssemblyName ()
  45. {
  46. AssemblyName assemblyName = new AssemblyName();
  47. assemblyName.Name = typeof (ModuleBuilderTest).FullName + (nameIndex ++);
  48. return assemblyName;
  49. }
  50. private AssemblyBuilder genAssembly ()
  51. {
  52. return Thread.GetDomain ().DefineDynamicAssembly (genAssemblyName (),
  53. AssemblyBuilderAccess.RunAndSave,
  54. tempDir);
  55. }
  56. [Test]
  57. public void TestIsTransient ()
  58. {
  59. AssemblyBuilder ab = genAssembly ();
  60. ModuleBuilder mb1 = ab.DefineDynamicModule ("foo.dll");
  61. Assert.IsTrue (mb1.IsTransient (), "#1");
  62. ModuleBuilder mb2 = ab.DefineDynamicModule ("foo2.dll", "foo2.dll");
  63. Assert.IsFalse (mb2.IsTransient (), "#2");
  64. }
  65. // Some of these tests overlap with the tests for Module
  66. [Test]
  67. public void TestGlobalData ()
  68. {
  69. AssemblyBuilder ab = genAssembly ();
  70. string resfile = Path.Combine (tempDir, "res");
  71. using (StreamWriter sw = new StreamWriter (resfile)) {
  72. sw.WriteLine ("FOO");
  73. }
  74. ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
  75. mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
  76. mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
  77. mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
  78. mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
  79. mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
  80. mb.CreateGlobalFunctions ();
  81. ab.Save ("foo.dll");
  82. Assembly assembly = Assembly.LoadFrom (Path.Combine (tempDir, "foo.dll"));
  83. Module module = assembly.GetLoadedModules () [0];
  84. string [] expectedFieldNames = new string [] {
  85. "DATA", "DATA2", "DATA3", "DATA4" };
  86. ArrayList fieldNames = new ArrayList ();
  87. foreach (FieldInfo fi in module.GetFields ()) {
  88. fieldNames.Add (fi.Name);
  89. }
  90. AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));
  91. Assert.IsNotNull (module.GetField ("DATA"), "#1");
  92. Assert.IsNotNull (module.GetField ("DATA2"), "#2");
  93. Assert.IsNotNull (module.GetField ("DATA3"), "#3");
  94. Assert.IsNotNull (module.GetField ("DATA4"), "#4");
  95. Assert.IsNull (module.GetField ("DATA_PRIVATE"), "#5");
  96. Assert.IsNotNull (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static), "#6");
  97. }
  98. [Test]
  99. public void TestGlobalMethods ()
  100. {
  101. AssemblyBuilder builder = genAssembly ();
  102. ModuleBuilder module = builder.DefineDynamicModule ("MessageModule");
  103. MethodBuilder method = module.DefinePInvokeMethod ("printf", "libc.so",
  104. MethodAttributes.PinvokeImpl | MethodAttributes.Static | MethodAttributes.Public,
  105. CallingConventions.Standard, typeof (void), new Type [] { typeof (string) }, CallingConvention.Winapi,
  106. CharSet.Auto);
  107. method.SetImplementationFlags (MethodImplAttributes.PreserveSig |
  108. method.GetMethodImplementationFlags ());
  109. module.CreateGlobalFunctions ();
  110. Assert.IsNotNull (module.GetMethod ("printf"));
  111. }
  112. [Test]
  113. public void TestDefineType_InterfaceNotAbstract ()
  114. {
  115. AssemblyBuilder ab = genAssembly ();
  116. ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
  117. try {
  118. mb.DefineType ("ITest1", TypeAttributes.Interface);
  119. Assert.Fail ("#A1");
  120. } catch (InvalidOperationException ex) {
  121. // Interface must be declared abstract
  122. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  123. Assert.IsNull (ex.InnerException, "#A3");
  124. Assert.IsNotNull (ex.Message, "#A4");
  125. }
  126. try {
  127. mb.DefineType ("ITest2", TypeAttributes.Interface, (Type) null);
  128. Assert.Fail ("#B1");
  129. } catch (InvalidOperationException ex) {
  130. // Interface must be declared abstract
  131. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  132. Assert.IsNull (ex.InnerException, "#B3");
  133. Assert.IsNotNull (ex.Message, "#B4");
  134. }
  135. // fail on MS .NET 1.1
  136. #if NET_2_0
  137. TypeBuilder tb = mb.DefineType ("ITest2", TypeAttributes.Interface,
  138. typeof (object));
  139. Assert.AreEqual (typeof (object), tb.BaseType, "#C1");
  140. tb = mb.DefineType ("ITest3", TypeAttributes.Interface,
  141. typeof (IDisposable));
  142. Assert.AreEqual (typeof (IDisposable), tb.BaseType, "#D1");
  143. #endif
  144. }
  145. [Test]
  146. public void DuplicateSymbolDocument ()
  147. {
  148. AssemblyBuilder ab = genAssembly ();
  149. ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll", true);
  150. // Check that it is possible to redefine a symbol document
  151. ISymbolDocumentWriter doc1 =
  152. mb.DefineDocument ("foo.il", SymDocumentType.Text,
  153. SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
  154. ISymbolDocumentWriter doc2 =
  155. mb.DefineDocument ("foo.il", SymDocumentType.Text,
  156. SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
  157. }
  158. [Test] // Test case for #80435.
  159. public void GetArrayMethodToStringTest ()
  160. {
  161. AssemblyBuilder assembly = genAssembly ();
  162. ModuleBuilder module = assembly.DefineDynamicModule ("m", "test.dll");
  163. Type [] myArrayClass = new Type [1];
  164. Type [] parameterTypes = { typeof (Array) };
  165. MethodInfo myMethodInfo = module.GetArrayMethod (myArrayClass.GetType (), "Sort", CallingConventions.Standard, null, parameterTypes);
  166. string str = myMethodInfo.ToString ();
  167. Assert.IsNotNull (str);
  168. // Don't compare string, since MS returns System.Reflection.Emit.SymbolMethod here
  169. // (they do not provide an implementation of ToString).
  170. }
  171. private static void AssertArrayEqualsSorted (Array o1, Array o2)
  172. {
  173. Array s1 = (Array) o1.Clone ();
  174. Array s2 = (Array) o2.Clone ();
  175. Array.Sort (s1);
  176. Array.Sort (s2);
  177. Assert.AreEqual (s1.Length, s2.Length, "#1");
  178. for (int i = 0; i < s1.Length; ++i)
  179. Assert.AreEqual (s1.GetValue (i), s2.GetValue (i), "#2: " + i);
  180. }
  181. #if NET_2_0
  182. [Test]
  183. public void ResolveFieldTokenFieldBuilder ()
  184. {
  185. AssemblyBuilder ab = genAssembly ();
  186. ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
  187. TypeBuilder tb = mb.DefineType ("foo");
  188. FieldBuilder fb = tb.DefineField ("foo", typeof (int), 0);
  189. tb.CreateType ();
  190. FieldInfo fi = mb.ResolveField (fb.GetToken ().Token);
  191. Assert.IsNotNull (fi);
  192. Assert.AreEqual ("foo", fi.Name);
  193. }
  194. [Test]
  195. [ExpectedException (typeof (ArgumentException))]
  196. public void ResolveFieldTokenInvalidToken ()
  197. {
  198. AssemblyBuilder ab = genAssembly ();
  199. ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
  200. mb.ResolveField (0x4001234);
  201. }
  202. #endif
  203. [Test]
  204. public void GetTypes ()
  205. {
  206. AssemblyBuilder ab = genAssembly ();
  207. ModuleBuilder mb = ab.DefineDynamicModule ("foo.dll", "foo.dll");
  208. TypeBuilder tb1 = mb.DefineType("Foo", TypeAttributes.Public);
  209. Type[] types = mb.GetTypes ();
  210. Assert.AreEqual (1, types.Length);
  211. Assert.AreEqual (tb1, types [0]);
  212. // After the type is created, MS seems to return the created type
  213. tb1.CreateType ();
  214. types = mb.GetTypes ();
  215. Assert.AreEqual (tb1.CreateType (), types [0]);
  216. }
  217. }
  218. }