ModuleTest.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Threading;
  11. using System.Reflection;
  12. #if !MONOTOUCH && !FULL_AOT_RUNTIME
  13. using System.Reflection.Emit;
  14. #endif
  15. using System.Runtime.Serialization;
  16. using System.IO;
  17. using System.Collections;
  18. using NUnit.Framework;
  19. namespace MonoTests.System.Reflection
  20. {
  21. [TestFixture]
  22. public class ModuleTest
  23. {
  24. static string BaseTempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.ModuleTest");
  25. static string TempFolder;
  26. [TestFixtureSetUp]
  27. public void FixtureSetUp ()
  28. {
  29. try {
  30. // Try to cleanup from any previous NUnit run.
  31. Directory.Delete (BaseTempFolder, true);
  32. } catch (Exception) {
  33. }
  34. }
  35. [SetUp]
  36. public void SetUp ()
  37. {
  38. int i = 0;
  39. do {
  40. TempFolder = Path.Combine (BaseTempFolder, (++i).ToString());
  41. } while (Directory.Exists (TempFolder));
  42. Directory.CreateDirectory (TempFolder);
  43. }
  44. [TearDown]
  45. public void TearDown ()
  46. {
  47. try {
  48. // This throws an exception under MS.NET and Mono on Windows,
  49. // since the directory contains loaded assemblies.
  50. Directory.Delete (TempFolder, true);
  51. } catch (Exception) {
  52. }
  53. }
  54. [Test]
  55. public void IsDefined_AttributeType_Null ()
  56. {
  57. Type t = typeof (ModuleTest);
  58. Module module = t.Module;
  59. try {
  60. module.IsDefined ((Type) null, false);
  61. Assert.Fail ("#1");
  62. } catch (ArgumentNullException ex) {
  63. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  64. Assert.IsNull (ex.InnerException, "#3");
  65. Assert.IsNotNull (ex.Message, "#4");
  66. Assert.IsNotNull (ex.ParamName, "#5");
  67. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  68. }
  69. }
  70. [Test]
  71. public void GetField_Name_Null ()
  72. {
  73. Type t = typeof (ModuleTest);
  74. Module module = t.Module;
  75. try {
  76. module.GetField (null);
  77. Assert.Fail ("#A1");
  78. } catch (ArgumentNullException ex) {
  79. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  80. Assert.IsNull (ex.InnerException, "#A3");
  81. Assert.IsNotNull (ex.Message, "#A4");
  82. Assert.IsNotNull (ex.ParamName, "#A5");
  83. Assert.AreEqual ("name", ex.ParamName, "#A6");
  84. }
  85. try {
  86. module.GetField (null, 0);
  87. Assert.Fail ("#B1");
  88. } catch (ArgumentNullException ex) {
  89. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  90. Assert.IsNull (ex.InnerException, "#B3");
  91. Assert.IsNotNull (ex.Message, "#B4");
  92. Assert.IsNotNull (ex.ParamName, "#B5");
  93. Assert.AreEqual ("name", ex.ParamName, "#B6");
  94. }
  95. }
  96. // Some of these tests overlap with the tests for ModuleBuilder
  97. #if !MONOTOUCH && !FULL_AOT_RUNTIME
  98. [Test]
  99. [Category("NotDotNet")] // path length can cause suprious failures
  100. public void TestGlobalData () {
  101. string name = "moduletest-assembly";
  102. string fileName = name + ".dll";
  103. AssemblyName assemblyName = new AssemblyName();
  104. assemblyName.Name = name;
  105. AssemblyBuilder ab
  106. = Thread.GetDomain().DefineDynamicAssembly(
  107. assemblyName, AssemblyBuilderAccess.RunAndSave, TempFolder);
  108. string resfile = Path.Combine (TempFolder, "res");
  109. using (StreamWriter sw = new StreamWriter (resfile)) {
  110. sw.WriteLine ("FOO");
  111. }
  112. ab.AddResourceFile ("res", "res");
  113. ModuleBuilder mb = ab.DefineDynamicModule(fileName, fileName);
  114. mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
  115. mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
  116. mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
  117. mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
  118. mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
  119. mb.CreateGlobalFunctions ();
  120. ab.Save (fileName);
  121. Assembly assembly = Assembly.LoadFrom (Path.Combine (TempFolder, fileName));
  122. Module module = assembly.GetLoadedModules ()[0];
  123. string[] expectedFieldNames = new string [] {
  124. "DATA", "DATA2", "DATA3", "DATA4"
  125. };
  126. ArrayList fieldNames = new ArrayList ();
  127. foreach (FieldInfo fi in module.GetFields ()) {
  128. fieldNames.Add (fi.Name);
  129. }
  130. AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));
  131. Assert.IsNotNull (module.GetField ("DATA"), "#A1");
  132. Assert.IsNotNull (module.GetField ("DATA2"), "#A2");
  133. Assert.IsNotNull (module.GetField ("DATA3"), "#A3");
  134. Assert.IsNotNull (module.GetField ("DATA4"), "#A4");
  135. Assert.IsNull (module.GetField ("DATA_PRIVATE"), "#A5");
  136. Assert.IsNotNull (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static), "#A6");
  137. // Check that these methods work correctly on resource modules
  138. Module m2 = assembly.GetModule ("res");
  139. Assert.IsNotNull (m2, "#B1");
  140. Assert.AreEqual (0, m2.GetFields ().Length, "#B2");
  141. Assert.IsNull (m2.GetField ("DATA"), "#B3");
  142. Assert.IsNull (m2.GetField ("DATA", BindingFlags.Public), "#B4");
  143. }
  144. #endif
  145. [Test]
  146. public void ResolveType ()
  147. {
  148. Type t = typeof (ModuleTest);
  149. Module module = t.Module;
  150. Assert.AreEqual (t, module.ResolveType (t.MetadataToken), "#1");
  151. /* We currently throw ArgumentException for this one */
  152. try {
  153. module.ResolveType (1234);
  154. Assert.Fail ("#2");
  155. } catch (ArgumentException) {
  156. }
  157. try {
  158. module.ResolveType (t.GetMethod ("ResolveType").MetadataToken);
  159. Assert.Fail ("#3");
  160. } catch (ArgumentException) {
  161. }
  162. try {
  163. module.ResolveType (t.MetadataToken + 10000);
  164. Assert.Fail ("#4");
  165. } catch (ArgumentOutOfRangeException) {
  166. }
  167. }
  168. [Test]
  169. public void ResolveMethod ()
  170. {
  171. Type t = typeof (ModuleTest);
  172. Module module = t.Module;
  173. Assert.AreEqual (t.GetMethod ("ResolveMethod"), module.ResolveMethod (t.GetMethod ("ResolveMethod").MetadataToken));
  174. try {
  175. module.ResolveMethod (1234);
  176. Assert.Fail ("1234");
  177. } catch (ArgumentException) {
  178. }
  179. try {
  180. module.ResolveMethod (t.MetadataToken);
  181. Assert.Fail ("MetadataToken");
  182. } catch (ArgumentException) {
  183. }
  184. try {
  185. module.ResolveMethod (t.GetMethod ("ResolveMethod").MetadataToken + 100000);
  186. Assert.Fail ("GetMethod");
  187. } catch (ArgumentOutOfRangeException) {
  188. }
  189. }
  190. public int aField;
  191. [Test]
  192. public void ResolveField ()
  193. {
  194. Type t = typeof (ModuleTest);
  195. Module module = t.Module;
  196. Assert.AreEqual (t.GetField ("aField"), module.ResolveField (t.GetField ("aField").MetadataToken));
  197. try {
  198. module.ResolveField (1234);
  199. Assert.Fail ();
  200. } catch (ArgumentException) {
  201. }
  202. try {
  203. module.ResolveField (t.MetadataToken);
  204. Assert.Fail ();
  205. } catch (ArgumentException) {
  206. }
  207. try {
  208. module.ResolveField (t.GetField ("aField").MetadataToken + 10000);
  209. Assert.Fail ();
  210. } catch (ArgumentOutOfRangeException) {
  211. }
  212. }
  213. [Ignore ("it breaks nunit-console.exe execution under .NET 2.0")]
  214. [Test]
  215. public void ResolveString ()
  216. {
  217. Type t = typeof (ModuleTest);
  218. Module module = t.Module;
  219. for (int i = 1; i < 10000; ++i) {
  220. try {
  221. module.ResolveString (0x70000000 + i);
  222. } catch (Exception) {
  223. }
  224. }
  225. try {
  226. module.ResolveString (1234);
  227. Assert.Fail ();
  228. } catch (ArgumentException) {
  229. }
  230. try {
  231. module.ResolveString (t.MetadataToken);
  232. Assert.Fail ();
  233. } catch (ArgumentException) {
  234. }
  235. try {
  236. module.ResolveString (0x70000000 | 10000);
  237. Assert.Fail ();
  238. } catch (ArgumentOutOfRangeException) {
  239. }
  240. }
  241. [Test]
  242. public void ResolveMember ()
  243. {
  244. Type t = typeof (ModuleTest);
  245. Module module = t.Module;
  246. Assert.AreEqual (t, module.ResolveMember (t.MetadataToken), "#1");
  247. Assert.AreEqual (t.GetField ("aField"), module.ResolveMember (t.GetField ("aField").MetadataToken), "#2");
  248. Assert.AreEqual (t.GetMethod ("ResolveMember"), module.ResolveMember (t.GetMethod ("ResolveMember").MetadataToken), "#3");
  249. try {
  250. module.ResolveMember (module.MetadataToken);
  251. Assert.Fail ("#4");
  252. } catch (ArgumentException) {
  253. }
  254. }
  255. public class Foo<T> {
  256. public void Bar(T t) {}
  257. }
  258. [Test]
  259. public void ResolveMethodOfGenericClass ()
  260. {
  261. Type type = typeof (Foo<>);
  262. Module mod = type.Module;
  263. MethodInfo method = type.GetMethod ("Bar");
  264. MethodBase res = mod.ResolveMethod (method.MetadataToken);
  265. Assert.AreEqual (method, res, "#1");
  266. }
  267. [Test]
  268. public void FindTypes ()
  269. {
  270. Module m = typeof (ModuleTest).Module;
  271. Type[] t;
  272. t = m.FindTypes (Module.FilterTypeName, "FindTypesTest*");
  273. Assert.AreEqual (2, t.Length, "#A1");
  274. Assert.AreEqual ("FindTypesTestFirstClass", t [0].Name, "#A2");
  275. Assert.AreEqual ("FindTypesTestSecondClass", t [1].Name, "#A3");
  276. t = m.FindTypes (Module.FilterTypeNameIgnoreCase, "findtypestest*");
  277. Assert.AreEqual (2, t.Length, "#B1");
  278. Assert.AreEqual ("FindTypesTestFirstClass", t [0].Name, "#B2");
  279. Assert.AreEqual ("FindTypesTestSecondClass", t [1].Name, "#B3");
  280. }
  281. [Test]
  282. [ExpectedException (typeof (ArgumentNullException))]
  283. public void GetObjectData_Null ()
  284. {
  285. Module m = typeof (ModuleTest).Module;
  286. m.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
  287. }
  288. #if !MONOTOUCH && !FULL_AOT_RUNTIME
  289. [Test]
  290. public void GetTypes ()
  291. {
  292. AssemblyName newName = new AssemblyName ();
  293. newName.Name = "ModuleTest";
  294. AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
  295. ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule" + ".dll", false);
  296. TypeBuilder tb = mb.DefineType ("Foo", TypeAttributes.Public);
  297. tb.CreateType ();
  298. ab.Save ("test_assembly.dll");
  299. Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
  300. ArrayList types = new ArrayList ();
  301. // The order of the modules is different between MS.NET and mono
  302. foreach (Module m in ass.GetModules ()) {
  303. Type[] t = m.GetTypes ();
  304. types.AddRange (t);
  305. }
  306. Assert.AreEqual (1, types.Count);
  307. Assert.AreEqual ("Foo", ((Type)(types [0])).Name);
  308. }
  309. #endif
  310. class FindTypesTestFirstClass {
  311. }
  312. class FindTypesTestSecondClass {
  313. }
  314. private static void AssertArrayEqualsSorted (Array o1, Array o2) {
  315. Array s1 = (Array)o1.Clone ();
  316. Array s2 = (Array)o2.Clone ();
  317. Array.Sort (s1);
  318. Array.Sort (s2);
  319. Assert.AreEqual (s1.Length, s2.Length);
  320. for (int i = 0; i < s1.Length; ++i)
  321. Assert.AreEqual (s1.GetValue (i), s2.GetValue (i));
  322. }
  323. }
  324. }