ConstructorBuilderTest.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // ConstructorBuilderTest.cs - NUnit Test Cases for the ConstructorBuilder 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.Security;
  15. using System.Security.Permissions;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Reflection.Emit
  18. {
  19. [TestFixture]
  20. public class ConstructorBuilderTest : Assertion
  21. {
  22. private TypeBuilder genClass;
  23. private ModuleBuilder module;
  24. private static int typeIndexer = 0;
  25. [SetUp]
  26. protected void SetUp () {
  27. AssemblyName assemblyName = new AssemblyName();
  28. assemblyName.Name = "MonoTests.System.Reflection.Emit.ConstructorBuilderTest";
  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. // Return a unique type name
  37. private string genTypeName () {
  38. return "class" + (typeIndexer ++);
  39. }
  40. public void TestAttributes () {
  41. ConstructorBuilder cb = genClass.DefineConstructor (
  42. MethodAttributes.Public, 0, new Type [0]);
  43. Assert ("Attributes works",
  44. (cb.Attributes & MethodAttributes.Public) != 0);
  45. Assert ("Attributes works",
  46. (cb.Attributes & MethodAttributes.SpecialName) != 0);
  47. }
  48. public void TestCallingConvention () {
  49. /* This does not work under MS.NET
  50. ConstructorBuilder cb3 = genClass.DefineConstructor (
  51. 0, CallingConventions.VarArgs, new Type [0]);
  52. AssertEquals ("CallingConvetion works",
  53. CallingConventions.VarArgs | CallingConventions.HasThis,
  54. cb3.CallingConvention);
  55. */
  56. ConstructorBuilder cb4 = genClass.DefineConstructor (
  57. MethodAttributes.Static, CallingConventions.Standard, new Type [0]);
  58. AssertEquals ("Static implies !HasThis",
  59. cb4.CallingConvention,
  60. CallingConventions.Standard);
  61. }
  62. public void TestDeclaringType () {
  63. ConstructorBuilder cb = genClass.DefineConstructor (
  64. 0, 0, new Type[0]);
  65. AssertEquals ("DeclaringType works",
  66. cb.DeclaringType, genClass);
  67. }
  68. public void TestInitLocals () {
  69. ConstructorBuilder cb = genClass.DefineConstructor (
  70. 0, 0, new Type[0]);
  71. AssertEquals ("InitLocals defaults to true", cb.InitLocals, true);
  72. cb.InitLocals = false;
  73. AssertEquals ("InitLocals is settable", cb.InitLocals, false);
  74. }
  75. public void TestMethodHandle () {
  76. ConstructorBuilder cb = genClass.DefineConstructor (
  77. 0, 0, new Type [0]);
  78. try {
  79. RuntimeMethodHandle handle = cb.MethodHandle;
  80. Fail ();
  81. } catch (NotSupportedException) {
  82. }
  83. }
  84. public void TestName () {
  85. ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
  86. AssertEquals ("Name works", ".ctor", cb.Name);
  87. ConstructorBuilder cb2 = genClass.DefineConstructor (MethodAttributes.Static, 0, new Type [0]);
  88. AssertEquals ("Static constructors have the right name", ".cctor", cb2.Name);
  89. }
  90. public void TestReflectedType () {
  91. ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
  92. AssertEquals ("ReflectedType works",
  93. genClass, cb.ReflectedType);
  94. }
  95. public void TestReturnType () {
  96. ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
  97. AssertEquals ("ReturnType works",
  98. null, cb.ReturnType);
  99. }
  100. public void TestDefineParameter () {
  101. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  102. ConstructorBuilder cb = tb.DefineConstructor (
  103. 0, 0, new Type [2] { typeof(int), typeof(int) });
  104. // index out of range
  105. try {
  106. cb.DefineParameter (0, 0, "param1");
  107. Fail ();
  108. } catch (ArgumentOutOfRangeException) {
  109. }
  110. try {
  111. cb.DefineParameter (3, 0, "param1");
  112. Fail ();
  113. } catch (ArgumentOutOfRangeException) {
  114. }
  115. // Normal usage
  116. cb.DefineParameter (1, 0, "param1");
  117. cb.DefineParameter (1, 0, "param1");
  118. cb.DefineParameter (2, 0, null);
  119. // Can not be called on a created type
  120. cb.GetILGenerator ().Emit (OpCodes.Ret);
  121. tb.CreateType ();
  122. try {
  123. cb.DefineParameter (1, 0, "param1");
  124. Fail ();
  125. }
  126. catch (InvalidOperationException) {
  127. }
  128. }
  129. public void TestGetCustomAttributes () {
  130. ConstructorBuilder cb = genClass.DefineConstructor (
  131. 0, 0, new Type [1] {typeof(int)});
  132. try {
  133. cb.GetCustomAttributes (true);
  134. Fail ();
  135. } catch (NotSupportedException) {
  136. }
  137. try {
  138. cb.GetCustomAttributes (null, true);
  139. Fail ();
  140. } catch (NotSupportedException) {
  141. }
  142. }
  143. public void TestMethodImplementationFlags () {
  144. ConstructorBuilder cb = genClass.DefineConstructor (
  145. 0, 0, new Type [0]);
  146. AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
  147. cb.GetMethodImplementationFlags (),
  148. MethodImplAttributes.Managed | MethodImplAttributes.IL);
  149. cb.SetImplementationFlags (MethodImplAttributes.OPTIL);
  150. AssertEquals ("SetImplementationFlags works",
  151. cb.GetMethodImplementationFlags (),
  152. MethodImplAttributes.OPTIL);
  153. // Can not be called on a created type
  154. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  155. ConstructorBuilder cb2 = tb.DefineConstructor (
  156. 0, 0, new Type [0]);
  157. cb2.GetILGenerator ().Emit (OpCodes.Ret);
  158. cb2.SetImplementationFlags (MethodImplAttributes.Managed);
  159. tb.CreateType ();
  160. try {
  161. cb2.SetImplementationFlags (MethodImplAttributes.OPTIL);
  162. Fail ();
  163. }
  164. catch (InvalidOperationException) {
  165. }
  166. }
  167. public void TestGetModule () {
  168. ConstructorBuilder cb = genClass.DefineConstructor (
  169. 0, 0, new Type [0]);
  170. AssertEquals ("GetModule works",
  171. module, cb.GetModule ());
  172. }
  173. public void TestGetParameters () {
  174. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  175. ConstructorBuilder cb = tb.DefineConstructor (
  176. 0, 0, new Type [1] {typeof(int)});
  177. cb.GetILGenerator ().Emit (OpCodes.Ret);
  178. // Can't be called before CreateType ()
  179. /* This does not work under mono
  180. try {
  181. cb.GetParameters ();
  182. Fail ();
  183. } catch (InvalidOperationException) {
  184. }
  185. */
  186. tb.CreateType ();
  187. /* This does not work under MS.NET !
  188. cb.GetParameters ();
  189. */
  190. }
  191. public void TestGetToken () {
  192. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  193. ConstructorBuilder cb = tb.DefineConstructor (
  194. 0, 0, new Type [1] {typeof(void)});
  195. cb.GetToken ();
  196. }
  197. public void TestInvoke () {
  198. ConstructorBuilder cb = genClass.DefineConstructor (
  199. 0, 0,
  200. new Type [1] {typeof(int)});
  201. try {
  202. cb.Invoke (null, new object [1] { 42 });
  203. Fail ();
  204. } catch (NotSupportedException) {
  205. }
  206. try {
  207. cb.Invoke (null, 0, null, new object [1] { 42 }, null);
  208. Fail ();
  209. } catch (NotSupportedException) {
  210. }
  211. }
  212. public void TestIsDefined () {
  213. ConstructorBuilder cb = genClass.DefineConstructor (
  214. 0, 0,
  215. new Type [1] {typeof(int)});
  216. try {
  217. cb.IsDefined (null, true);
  218. Fail ();
  219. } catch (NotSupportedException) {
  220. }
  221. }
  222. public void TestSetCustomAttribute () {
  223. TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
  224. ConstructorBuilder cb = tb.DefineConstructor (
  225. 0, 0,
  226. new Type [1] {typeof(int)});
  227. cb.GetILGenerator ().Emit (OpCodes.Ret);
  228. // Null argument
  229. try {
  230. cb.SetCustomAttribute (null);
  231. Fail ();
  232. } catch (ArgumentNullException) {
  233. }
  234. byte[] custAttrData = { 1, 0, 0, 0, 0};
  235. Type attrType = Type.GetType
  236. ("System.Reflection.AssemblyKeyNameAttribute");
  237. Type[] paramTypes = new Type[1];
  238. paramTypes[0] = typeof(String);
  239. ConstructorInfo ctorInfo =
  240. attrType.GetConstructor(paramTypes);
  241. cb.SetCustomAttribute (ctorInfo, custAttrData);
  242. // Null arguments again
  243. try {
  244. cb.SetCustomAttribute (null, new byte[2]);
  245. Fail ();
  246. } catch (ArgumentNullException) {
  247. }
  248. try {
  249. cb.SetCustomAttribute (ctorInfo, null);
  250. Fail ();
  251. } catch (ArgumentNullException) {
  252. }
  253. }
  254. // Same as in MethodBuilderTest
  255. [Test]
  256. [ExpectedException (typeof (InvalidOperationException))]
  257. public void TestAddDeclarativeSecurityAlreadyCreated () {
  258. ConstructorBuilder cb = genClass.DefineConstructor (
  259. MethodAttributes.Public, 0, new Type [0]);
  260. ILGenerator ilgen = cb.GetILGenerator ();
  261. ilgen.Emit (OpCodes.Ret);
  262. genClass.CreateType ();
  263. PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
  264. cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
  265. }
  266. [Test]
  267. [ExpectedException (typeof (ArgumentNullException))]
  268. public void TestAddDeclarativeSecurityNullPermissionSet () {
  269. ConstructorBuilder cb = genClass.DefineConstructor (
  270. MethodAttributes.Public, 0, new Type [0]);
  271. cb.AddDeclarativeSecurity (SecurityAction.Demand, null);
  272. }
  273. [Test]
  274. public void TestAddDeclarativeSecurityInvalidAction () {
  275. ConstructorBuilder cb = genClass.DefineConstructor (
  276. MethodAttributes.Public, 0, new Type [0]);
  277. SecurityAction[] actions = new SecurityAction [] {
  278. SecurityAction.RequestMinimum,
  279. SecurityAction.RequestOptional,
  280. SecurityAction.RequestRefuse };
  281. PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
  282. foreach (SecurityAction action in actions) {
  283. try {
  284. cb.AddDeclarativeSecurity (action, set);
  285. Fail ();
  286. }
  287. catch (ArgumentException) {
  288. }
  289. }
  290. }
  291. [Test]
  292. [ExpectedException (typeof (InvalidOperationException))]
  293. public void TestAddDeclarativeSecurityDuplicateAction () {
  294. ConstructorBuilder cb = genClass.DefineConstructor (
  295. MethodAttributes.Public, 0, new Type [0]);
  296. PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
  297. cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
  298. cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
  299. }
  300. }
  301. }