ModuleTest.cs 9.6 KB

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