ModuleTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // ModuleTest - NUnit Test Cases for the Module 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 NUnit.Framework;
  16. namespace MonoTests.System.Reflection
  17. {
  18. [TestFixture]
  19. public class ModuleTest : Assertion
  20. {
  21. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.ModuleTest");
  22. [SetUp]
  23. public void SetUp () {
  24. while (Directory.Exists (TempFolder))
  25. TempFolder = Path.Combine (TempFolder, "2");
  26. Directory.CreateDirectory (TempFolder);
  27. }
  28. [TearDown]
  29. public void TearDown () {
  30. try {
  31. // This throws an exception under MS.NET, since the directory contains loaded
  32. // assemblies.
  33. Directory.Delete (TempFolder, true);
  34. }
  35. catch (Exception) {
  36. }
  37. }
  38. // Some of these tests overlap with the tests for ModuleBuilder
  39. [Test]
  40. public void TestGlobalData () {
  41. string name = "moduletest-assembly";
  42. string fileName = name + ".dll";
  43. AssemblyName assemblyName = new AssemblyName();
  44. assemblyName.Name = name;
  45. AssemblyBuilder ab
  46. = Thread.GetDomain().DefineDynamicAssembly(
  47. assemblyName, AssemblyBuilderAccess.RunAndSave, TempFolder);
  48. string resfile = Path.Combine (TempFolder, "res");
  49. using (StreamWriter sw = new StreamWriter (resfile)) {
  50. sw.WriteLine ("FOO");
  51. }
  52. ab.AddResourceFile ("res", "res");
  53. ModuleBuilder mb = ab.DefineDynamicModule(fileName, fileName);
  54. mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
  55. mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
  56. mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
  57. mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
  58. mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
  59. mb.CreateGlobalFunctions ();
  60. ab.Save (fileName);
  61. Assembly assembly = Assembly.LoadFrom (Path.Combine (TempFolder, fileName));
  62. Module module = assembly.GetLoadedModules ()[0];
  63. string[] expectedFieldNames = new string [] {
  64. "DATA", "DATA2", "DATA3", "DATA4"
  65. };
  66. ArrayList fieldNames = new ArrayList ();
  67. foreach (FieldInfo fi in module.GetFields ()) {
  68. fieldNames.Add (fi.Name);
  69. }
  70. AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));
  71. try {
  72. module.GetField (null);
  73. Fail ();
  74. }
  75. catch (ArgumentNullException) {
  76. }
  77. try {
  78. module.GetField (null, 0);
  79. Fail ();
  80. }
  81. catch (ArgumentNullException) {
  82. }
  83. AssertEquals (module.GetField ("DATA") != null, true);
  84. AssertEquals (module.GetField ("DATA2") != null, true);
  85. AssertEquals (module.GetField ("DATA3") != null, true);
  86. AssertEquals (module.GetField ("DATA4") != null, true);
  87. AssertEquals (module.GetField ("DATA_PRIVATE"), null);
  88. AssertEquals (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static) != null, true);
  89. // Check that these methods work correctly on resource modules
  90. Module m2 = assembly.GetModule ("res");
  91. AssertEquals (m2 != null, true);
  92. AssertEquals (m2.GetFields ().Length, 0);
  93. AssertEquals (m2.GetField ("DATA"), null);
  94. AssertEquals (m2.GetField ("DATA", BindingFlags.Public), null);
  95. }
  96. #if NET_2_0
  97. [Test]
  98. public void ResolveType () {
  99. Type t = typeof (ModuleTest);
  100. Module module = t.Module;
  101. AssertEquals (t, module.ResolveType (t.MetadataToken));
  102. /* We currently throw ArgumentException for this one */
  103. try {
  104. module.ResolveType (1234);
  105. Fail ();
  106. }
  107. catch (ArgumentException) {
  108. }
  109. try {
  110. module.ResolveType (t.GetMethod ("ResolveType").MetadataToken);
  111. Fail ();
  112. }
  113. catch (ArgumentException) {
  114. }
  115. try {
  116. module.ResolveType (t.MetadataToken + 10000);
  117. Fail ();
  118. }
  119. catch (ArgumentOutOfRangeException) {
  120. }
  121. }
  122. [Test]
  123. public void ResolveMethod () {
  124. Type t = typeof (ModuleTest);
  125. Module module = t.Module;
  126. AssertEquals (t.GetMethod ("ResolveMethod"), module.ResolveMethod (t.GetMethod ("ResolveMethod").MetadataToken));
  127. try {
  128. module.ResolveMethod (1234);
  129. Fail ();
  130. }
  131. catch (ArgumentException) {
  132. }
  133. try {
  134. module.ResolveMethod (t.MetadataToken);
  135. Fail ();
  136. }
  137. catch (ArgumentException) {
  138. }
  139. try {
  140. module.ResolveMethod (t.GetMethod ("ResolveMethod").MetadataToken + 10000);
  141. Fail ();
  142. }
  143. catch (ArgumentOutOfRangeException) {
  144. }
  145. }
  146. public int aField;
  147. [Test]
  148. public void ResolveField () {
  149. Type t = typeof (ModuleTest);
  150. Module module = t.Module;
  151. AssertEquals (t.GetField ("aField"), module.ResolveField (t.GetField ("aField").MetadataToken));
  152. try {
  153. module.ResolveField (1234);
  154. Fail ();
  155. }
  156. catch (ArgumentException) {
  157. }
  158. try {
  159. module.ResolveField (t.MetadataToken);
  160. Fail ();
  161. }
  162. catch (ArgumentException) {
  163. }
  164. try {
  165. module.ResolveField (t.GetField ("aField").MetadataToken + 10000);
  166. Fail ();
  167. }
  168. catch (ArgumentOutOfRangeException) {
  169. }
  170. }
  171. [Test]
  172. public void ResolveString () {
  173. Type t = typeof (ModuleTest);
  174. Module module = t.Module;
  175. for (int i = 1; i < 10000; ++i) {
  176. try {
  177. module.ResolveString (0x70000000 + i);
  178. }
  179. catch (Exception) {
  180. }
  181. }
  182. try {
  183. module.ResolveString (1234);
  184. Fail ();
  185. }
  186. catch (ArgumentException) {
  187. }
  188. try {
  189. module.ResolveString (t.MetadataToken);
  190. Fail ();
  191. }
  192. catch (ArgumentException) {
  193. }
  194. try {
  195. module.ResolveString (0x70000000 | 10000);
  196. Fail ();
  197. }
  198. catch (ArgumentOutOfRangeException) {
  199. }
  200. }
  201. [Test]
  202. public void ResolveMember () {
  203. Type t = typeof (ModuleTest);
  204. Module module = t.Module;
  205. AssertEquals (t, module.ResolveMember (t.MetadataToken));
  206. AssertEquals (t.GetField ("aField"), module.ResolveMember (t.GetField ("aField").MetadataToken));
  207. AssertEquals (t.GetMethod ("ResolveMember"), module.ResolveMember (t.GetMethod ("ResolveMember").MetadataToken));
  208. try {
  209. module.ResolveMember (module.MetadataToken);
  210. }
  211. catch (ArgumentException) {
  212. }
  213. }
  214. #endif
  215. [Test]
  216. public void FindTypes () {
  217. Module m = typeof (ModuleTest).Module;
  218. Type[] t;
  219. t = m.FindTypes (Module.FilterTypeName, "FindTypesTest*");
  220. AssertEquals (2, t.Length);
  221. AssertEquals ("FindTypesTestFirstClass", t [0].Name);
  222. AssertEquals ("FindTypesTestSecondClass", t [1].Name);
  223. t = m.FindTypes (Module.FilterTypeNameIgnoreCase, "findtypestest*");
  224. AssertEquals (2, t.Length);
  225. AssertEquals ("FindTypesTestFirstClass", t [0].Name);
  226. AssertEquals ("FindTypesTestSecondClass", t [1].Name);
  227. }
  228. class FindTypesTestFirstClass {
  229. }
  230. class FindTypesTestSecondClass {
  231. }
  232. private static void AssertArrayEqualsSorted (Array o1, Array o2) {
  233. Array s1 = (Array)o1.Clone ();
  234. Array s2 = (Array)o2.Clone ();
  235. Array.Sort (s1);
  236. Array.Sort (s2);
  237. AssertEquals (s1.Length, s2.Length);
  238. for (int i = 0; i < s1.Length; ++i)
  239. AssertEquals (s1.GetValue (i), s2.GetValue (i));
  240. }
  241. }
  242. }