MethodBuilderTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. //
  2. // MethodBuilderTest.cs - NUnit Test Cases for the MethodBuilder class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. // TODO:
  8. // - implement 'Signature' (what the hell it does???) and test it
  9. // - implement Equals and test it
  10. using System;
  11. using System.Threading;
  12. using System.Reflection;
  13. using System.Reflection.Emit;
  14. using System.Runtime.CompilerServices;
  15. using System.Security;
  16. using System.Security.Permissions;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.Reflection.Emit
  19. {
  20. [TestFixture]
  21. public class MethodBuilderTest : Assertion
  22. {
  23. private TypeBuilder genClass;
  24. private ModuleBuilder module;
  25. [SetUp]
  26. protected void SetUp () {
  27. AssemblyName assemblyName = new AssemblyName();
  28. assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodBuilderTest";
  29. AssemblyBuilder assembly
  30. = Thread.GetDomain().DefineDynamicAssembly(
  31. assemblyName, AssemblyBuilderAccess.Run);
  32. module = assembly.DefineDynamicModule("module1");
  33. genClass = module.DefineType(genTypeName (),
  34. TypeAttributes.Public);
  35. }
  36. static int methodIndexer = 0;
  37. static int typeIndexer = 0;
  38. // Return a unique method name
  39. private string genMethodName () {
  40. return "m" + (methodIndexer ++);
  41. }
  42. // Return a unique type name
  43. private string genTypeName () {
  44. return "class" + (typeIndexer ++);
  45. }
  46. public void TestAttributes () {
  47. MethodBuilder mb = genClass.DefineMethod (
  48. genMethodName (), MethodAttributes.Public, typeof (void), new Type [0]);
  49. AssertEquals ("Attributes works",
  50. MethodAttributes.Public, mb.Attributes);
  51. }
  52. public void TestCallingConvention () {
  53. MethodBuilder mb = genClass.DefineMethod (
  54. genMethodName (), 0, typeof (void), new Type[0]);
  55. AssertEquals ("CallingConvetion defaults to Standard+HasThis",
  56. CallingConventions.Standard | CallingConventions.HasThis,
  57. mb.CallingConvention);
  58. MethodBuilder mb3 = genClass.DefineMethod (
  59. genMethodName (), 0, CallingConventions.VarArgs, typeof (void), new Type[0]);
  60. AssertEquals ("CallingConvetion works",
  61. CallingConventions.VarArgs | CallingConventions.HasThis,
  62. mb3.CallingConvention);
  63. MethodBuilder mb4 = genClass.DefineMethod (
  64. genMethodName (), MethodAttributes.Static, CallingConventions.Standard,
  65. typeof (void), new Type [0]);
  66. AssertEquals ("Static implies !HasThis",
  67. CallingConventions.Standard,
  68. mb4.CallingConvention);
  69. }
  70. public void TestDeclaringType () {
  71. MethodBuilder mb = genClass.DefineMethod (
  72. genMethodName (), 0, typeof (void), new Type[0]);
  73. AssertEquals ("DeclaringType works",
  74. genClass, mb.DeclaringType);
  75. }
  76. public void TestInitLocals () {
  77. MethodBuilder mb = genClass.DefineMethod (
  78. genMethodName (), 0, typeof (void), new Type[0]);
  79. Assert ("InitLocals defaults to true", mb.InitLocals);
  80. mb.InitLocals = false;
  81. Assert ("InitLocals is settable", !mb.InitLocals);
  82. }
  83. public void TestMethodHandle () {
  84. MethodBuilder mb = genClass.DefineMethod (
  85. genMethodName (), 0, typeof (void), new Type [0]);
  86. try {
  87. RuntimeMethodHandle handle = mb.MethodHandle;
  88. Fail ();
  89. } catch (NotSupportedException) {
  90. }
  91. }
  92. public void TestName () {
  93. string name = genMethodName ();
  94. MethodBuilder mb = genClass.DefineMethod (
  95. name, 0, typeof (void), new Type [0]);
  96. AssertEquals ("Name works", name, mb.Name);
  97. }
  98. public void TestReflectedType () {
  99. MethodBuilder mb = genClass.DefineMethod (
  100. genMethodName (), 0, typeof (void), new Type [0]);
  101. AssertEquals ("ReflectedType works",
  102. genClass, mb.ReflectedType);
  103. }
  104. public void TestReturnType () {
  105. MethodBuilder mb = genClass.DefineMethod (
  106. genMethodName (), 0, typeof (Console), new Type [0]);
  107. AssertEquals ("ReturnType works", typeof (Console),
  108. mb.ReturnType);
  109. MethodBuilder mb2 = genClass.DefineMethod (
  110. genMethodName (), 0, null, new Type [0]);
  111. Assert ("void ReturnType works", (mb2.ReturnType == null) || (mb2.ReturnType == typeof (void)));
  112. }
  113. public void TestReturnTypeCustomAttributes () {
  114. MethodBuilder mb = genClass.DefineMethod (
  115. genMethodName (), 0, typeof (Console), new Type [0]);
  116. AssertEquals ("ReturnTypeCustomAttributes must be null", null,
  117. mb.ReturnTypeCustomAttributes);
  118. }
  119. /*
  120. public void TestSignature () {
  121. MethodBuilder mb = genClass.DefineMethod (
  122. "m91", 0, typeof (Console), new Type [1] { typeof (Console) });
  123. Console.WriteLine (mb.Signature);
  124. }
  125. */
  126. public void TestCreateMethodBody () {
  127. MethodBuilder mb = genClass.DefineMethod (
  128. genMethodName (), 0, typeof (void), new Type [0]);
  129. // Clear body
  130. mb.CreateMethodBody (null, 999);
  131. // Check arguments 1.
  132. try {
  133. mb.CreateMethodBody (new byte[1], -1);
  134. Fail ();
  135. } catch (ArgumentException) {
  136. }
  137. // Check arguments 2.
  138. try {
  139. mb.CreateMethodBody (new byte[1], 2);
  140. Fail ();
  141. } catch (ArgumentException) {
  142. }
  143. mb.CreateMethodBody (new byte[2], 1);
  144. // Could only be called once
  145. try {
  146. mb.CreateMethodBody (new byte[2], 1);
  147. Fail ();
  148. } catch (InvalidOperationException) {
  149. }
  150. // Can not be called on a created type
  151. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  152. MethodBuilder mb2 = tb.DefineMethod (
  153. genMethodName (), 0, typeof (void), new Type [0]);
  154. ILGenerator ilgen = mb2.GetILGenerator ();
  155. ilgen.Emit (OpCodes.Ret);
  156. tb.CreateType ();
  157. try {
  158. mb2.CreateMethodBody (new byte[2], 1);
  159. Fail ();
  160. } catch (InvalidOperationException) {
  161. }
  162. }
  163. public void TestDefineParameter () {
  164. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  165. MethodBuilder mb = tb.DefineMethod (
  166. genMethodName (), 0, typeof (void),
  167. new Type [2] { typeof(int), typeof(int) });
  168. // index out of range
  169. // This fails on mono because the mono version accepts a 0 index
  170. /*
  171. try {
  172. mb.DefineParameter (0, 0, "param1");
  173. Fail ();
  174. } catch (ArgumentOutOfRangeException) {
  175. }
  176. */
  177. try {
  178. mb.DefineParameter (3, 0, "param1");
  179. Fail ();
  180. } catch (ArgumentOutOfRangeException) {
  181. }
  182. // Normal usage
  183. mb.DefineParameter (1, 0, "param1");
  184. mb.DefineParameter (1, 0, "param1");
  185. mb.DefineParameter (2, 0, null);
  186. // Can not be called on a created type
  187. mb.CreateMethodBody (new byte[2], 0);
  188. tb.CreateType ();
  189. try {
  190. mb.DefineParameter (1, 0, "param1");
  191. Fail ();
  192. }
  193. catch (InvalidOperationException) {
  194. }
  195. }
  196. public void TestGetBaseDefinition () {
  197. MethodBuilder mb = genClass.DefineMethod (
  198. genMethodName (), 0, typeof (void), new Type [0]);
  199. AssertEquals ("GetBaseDefinition works",
  200. mb.GetBaseDefinition (), mb);
  201. }
  202. public void TestGetILGenerator () {
  203. MethodBuilder mb = genClass.DefineMethod (
  204. genMethodName (), 0, typeof (void), new Type [0]);
  205. // The same instance is returned on the second call
  206. ILGenerator ilgen1 = mb.GetILGenerator ();
  207. ILGenerator ilgen2 = mb.GetILGenerator ();
  208. AssertEquals ("The same ilgen is returned on the second call",
  209. ilgen1, ilgen2);
  210. // Does not work on unmanaged code
  211. MethodBuilder mb2 = genClass.DefineMethod (
  212. genMethodName (), 0, typeof (void), new Type [0]);
  213. try {
  214. mb2.SetImplementationFlags (MethodImplAttributes.Unmanaged);
  215. mb2.GetILGenerator ();
  216. Fail ();
  217. } catch (InvalidOperationException) {
  218. }
  219. try {
  220. mb2.SetImplementationFlags (MethodImplAttributes.Native);
  221. mb2.GetILGenerator ();
  222. Fail ();
  223. } catch (InvalidOperationException) {
  224. }
  225. }
  226. public void TestMethodImplementationFlags () {
  227. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  228. MethodBuilder mb = tb.DefineMethod (
  229. genMethodName (), 0, typeof (void), new Type [0]);
  230. AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
  231. MethodImplAttributes.Managed | MethodImplAttributes.IL,
  232. mb.GetMethodImplementationFlags ());
  233. mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
  234. AssertEquals ("SetImplementationFlags works",
  235. MethodImplAttributes.OPTIL,
  236. mb.GetMethodImplementationFlags ());
  237. // Can not be called on a created type
  238. mb.CreateMethodBody (new byte[2], 0);
  239. mb.SetImplementationFlags (MethodImplAttributes.Managed);
  240. tb.CreateType ();
  241. try {
  242. mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
  243. Fail ();
  244. }
  245. catch (InvalidOperationException) {
  246. }
  247. }
  248. public void TestGetModule () {
  249. MethodBuilder mb = genClass.DefineMethod (
  250. genMethodName (), 0, typeof (void), new Type [0]);
  251. AssertEquals ("GetMethod works", module,
  252. mb.GetModule ());
  253. }
  254. public void TestGetParameters () {
  255. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  256. MethodBuilder mb = tb.DefineMethod (
  257. genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
  258. /*
  259. * According to the MSDN docs, this method should fail with a
  260. * NotSupportedException. In reality, it throws an
  261. * InvalidOperationException under MS .NET, and returns the
  262. * requested data under mono.
  263. */
  264. /*
  265. try {
  266. mb.GetParameters ();
  267. Fail ("#161");
  268. } catch (InvalidOperationException ex) {
  269. Console.WriteLine (ex);
  270. }
  271. */
  272. }
  273. public void TestGetToken () {
  274. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  275. MethodBuilder mb = tb.DefineMethod (
  276. genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
  277. mb.GetToken ();
  278. }
  279. public void TestInvoke () {
  280. MethodBuilder mb = genClass.DefineMethod (
  281. genMethodName (), 0, typeof (void),
  282. new Type [1] {typeof(int)});
  283. try {
  284. mb.Invoke (null, new object [1] { 42 });
  285. Fail ();
  286. } catch (NotSupportedException) {
  287. }
  288. try {
  289. mb.Invoke (null, 0, null, new object [1] { 42 }, null);
  290. Fail ();
  291. } catch (NotSupportedException) {
  292. }
  293. }
  294. public void TestIsDefined () {
  295. MethodBuilder mb = genClass.DefineMethod (
  296. genMethodName (), 0, typeof (void),
  297. new Type [1] {typeof(int)});
  298. try {
  299. mb.IsDefined (null, true);
  300. Fail ();
  301. } catch (NotSupportedException) {
  302. }
  303. }
  304. public void TestGetCustomAttributes () {
  305. MethodBuilder mb = genClass.DefineMethod (
  306. genMethodName (), 0, typeof (void),
  307. new Type [1] {typeof(int)});
  308. try {
  309. mb.GetCustomAttributes (true);
  310. Fail ();
  311. } catch (NotSupportedException) {
  312. }
  313. try {
  314. mb.GetCustomAttributes (null, true);
  315. Fail ();
  316. } catch (NotSupportedException) {
  317. }
  318. }
  319. public void TestSetCustomAttribute () {
  320. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  321. string name = genMethodName ();
  322. MethodBuilder mb = tb.DefineMethod (
  323. name, MethodAttributes.Public, typeof (void),
  324. new Type [1] {typeof(int)});
  325. // Null argument
  326. try {
  327. mb.SetCustomAttribute (null);
  328. Fail ();
  329. } catch (ArgumentNullException) {
  330. }
  331. byte[] custAttrData = { 1, 0, 0, 0, 0};
  332. Type attrType = Type.GetType
  333. ("System.Reflection.AssemblyKeyNameAttribute");
  334. Type[] paramTypes = new Type[1];
  335. paramTypes[0] = typeof(String);
  336. ConstructorInfo ctorInfo =
  337. attrType.GetConstructor(paramTypes);
  338. mb.SetCustomAttribute (ctorInfo, custAttrData);
  339. // Test MethodImplAttribute
  340. mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
  341. mb.GetILGenerator ().Emit (OpCodes.Ret);
  342. Type t = tb.CreateType ();
  343. AssertEquals ("Setting MethodImplAttributes works",
  344. t.GetMethod (name).GetMethodImplementationFlags (),
  345. MethodImplAttributes.Synchronized);
  346. // Null arguments again
  347. try {
  348. mb.SetCustomAttribute (null, new byte[2]);
  349. Fail ();
  350. } catch (ArgumentNullException) {
  351. }
  352. try {
  353. mb.SetCustomAttribute (ctorInfo, null);
  354. Fail ();
  355. } catch (ArgumentNullException) {
  356. }
  357. }
  358. [Test]
  359. [ExpectedException (typeof (InvalidOperationException))]
  360. public void TestAddDeclarativeSecurityAlreadyCreated () {
  361. MethodBuilder mb = genClass.DefineMethod (
  362. genMethodName (), MethodAttributes.Public, typeof (void),
  363. new Type [0]);
  364. ILGenerator ilgen = mb.GetILGenerator ();
  365. ilgen.Emit (OpCodes.Ret);
  366. genClass.CreateType ();
  367. PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
  368. mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
  369. }
  370. [Test]
  371. [ExpectedException (typeof (ArgumentNullException))]
  372. public void TestAddDeclarativeSecurityNullPermissionSet () {
  373. MethodBuilder mb = genClass.DefineMethod (
  374. genMethodName (), MethodAttributes.Public, typeof (void),
  375. new Type [0]);
  376. mb.AddDeclarativeSecurity (SecurityAction.Demand, null);
  377. }
  378. [Test]
  379. public void TestAddDeclarativeSecurityInvalidAction () {
  380. MethodBuilder mb = genClass.DefineMethod (
  381. genMethodName (), MethodAttributes.Public, typeof (void),
  382. new Type [0]);
  383. SecurityAction[] actions = new SecurityAction [] {
  384. SecurityAction.RequestMinimum,
  385. SecurityAction.RequestOptional,
  386. SecurityAction.RequestRefuse };
  387. PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
  388. foreach (SecurityAction action in actions) {
  389. try {
  390. mb.AddDeclarativeSecurity (action, set);
  391. Fail ();
  392. }
  393. catch (ArgumentException) {
  394. }
  395. }
  396. }
  397. [Test]
  398. [ExpectedException (typeof (InvalidOperationException))]
  399. public void TestAddDeclarativeSecurityDuplicateAction () {
  400. MethodBuilder mb = genClass.DefineMethod (
  401. genMethodName (), MethodAttributes.Public, typeof (void),
  402. new Type [0]);
  403. PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
  404. mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
  405. mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
  406. }
  407. [AttributeUsage (AttributeTargets.Parameter)]
  408. class ParamAttribute : Attribute {
  409. public ParamAttribute () {
  410. }
  411. }
  412. [Test]
  413. public void TestDynamicParams () {
  414. string mname = genMethodName ();
  415. MethodBuilder mb = genClass.DefineMethod (
  416. mname, MethodAttributes.Public, typeof (void),
  417. new Type [] { typeof (string) });
  418. ParameterBuilder pb = mb.DefineParameter (1, 0, "foo");
  419. pb.SetCustomAttribute (new CustomAttributeBuilder (typeof (ParamAttribute).GetConstructors () [0], new object [] { }));
  420. mb.GetILGenerator ().Emit (OpCodes.Ret);
  421. Type t = genClass.CreateType ();
  422. MethodInfo m = t.GetMethod (mname);
  423. ParameterInfo pi = m.GetParameters ()[0];
  424. AssertEquals ("foo", pi.Name);
  425. object[] cattrs = pi.GetCustomAttributes (true);
  426. /* This test does not run under MS.NET: */
  427. /*
  428. AssertEquals (1, cattrs.Length);
  429. AssertEquals (typeof (ParamAttribute), cattrs [0].GetType ());
  430. */
  431. }
  432. }
  433. }