2003-12-15 Zoltan Varga <vargaz@freemail.hu>
authorZoltan Varga <vargaz@gmail.com>
Mon, 15 Dec 2003 13:57:08 +0000 (13:57 -0000)
committerZoltan Varga <vargaz@gmail.com>
Mon, 15 Dec 2003 13:57:08 +0000 (13:57 -0000)
* MethodBuilderTest.cs ConstructorBuilderTest.cs: Add tests for
AddDeclarativeSecurity.

svn path=/trunk/mcs/; revision=21171

mcs/class/corlib/Test/System.Reflection.Emit/ChangeLog
mcs/class/corlib/Test/System.Reflection.Emit/ConstructorBuilderTest.cs
mcs/class/corlib/Test/System.Reflection.Emit/MethodBuilderTest.cs

index b1284b42092064cbeb3bf062107f98ca65147a09..684177fb316fa522d8974d014a449a4b9f92108f 100644 (file)
@@ -1,3 +1,8 @@
+2003-12-15  Zoltan Varga  <vargaz@freemail.hu>
+
+       * MethodBuilderTest.cs ConstructorBuilderTest.cs: Add tests for
+       AddDeclarativeSecurity.
+
 2003-11-17  Zoltan Varga  <vargaz@freemail.hu>
 
        * ConstructorBuilderTest.cs (TestAttributes): Make this test more
index 435a836a990657a51d6046a4cc7a1ac905f03657..413d22325e021a440dfb28a6866a4cd2904978dc 100644 (file)
@@ -8,12 +8,13 @@
 // TODO:\r
 //  - implement 'Signature' (what the hell it does???) and test it\r
 //  - implement Equals and test it\r
-//  - AddDeclarativeSecurity\r
 \r
 using System;\r
 using System.Threading;\r
 using System.Reflection;\r
 using System.Reflection.Emit;\r
+using System.Security;\r
+using System.Security.Permissions;\r
 \r
 using NUnit.Framework;\r
 \r
@@ -27,6 +28,8 @@ public class ConstructorBuilderTest : Assertion
 \r
        private ModuleBuilder module;\r
 \r
+       private static int typeIndexer = 0;\r
+\r
        [SetUp]\r
        protected void SetUp () {\r
                AssemblyName assemblyName = new AssemblyName();\r
@@ -38,10 +41,15 @@ public class ConstructorBuilderTest : Assertion
 \r
                module = assembly.DefineDynamicModule("module1");\r
                \r
-               genClass = module.DefineType("class1"\r
+               genClass = module.DefineType(genTypeName ()\r
                                                                         TypeAttributes.Public);\r
        }\r
 \r
+       // Return a unique type name\r
+       private string genTypeName () {\r
+               return "class" + (typeIndexer ++);\r
+       }\r
+\r
        public void TestAttributes () {\r
                ConstructorBuilder cb = genClass.DefineConstructor (\r
                         MethodAttributes.Public, 0, new Type [0]);\r
@@ -120,7 +128,7 @@ public class ConstructorBuilderTest : Assertion
        }\r
 \r
        public void TestDefineParameter () {\r
-               TypeBuilder tb = module.DefineType ("class7", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                ConstructorBuilder cb = tb.DefineConstructor (\r
                         0, 0, new Type [2] { typeof(int), typeof(int) });\r
 \r
@@ -184,7 +192,7 @@ public class ConstructorBuilderTest : Assertion
                                          MethodImplAttributes.OPTIL);\r
 \r
                // Can not be called on a created type\r
-               TypeBuilder tb = module.DefineType ("class14", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                ConstructorBuilder cb2 = tb.DefineConstructor (\r
                         0, 0, new Type [0]);\r
 \r
@@ -208,7 +216,7 @@ public class ConstructorBuilderTest : Assertion
        }\r
 \r
        public void TestGetParameters () {\r
-               TypeBuilder tb = module.DefineType ("class16", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                ConstructorBuilder cb = tb.DefineConstructor (\r
                         0, 0, new Type [1] {typeof(int)});\r
                cb.GetILGenerator ().Emit (OpCodes.Ret);\r
@@ -230,7 +238,7 @@ public class ConstructorBuilderTest : Assertion
        }\r
 \r
        public void TestGetToken () {\r
-               TypeBuilder tb = module.DefineType ("class17", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                ConstructorBuilder cb = tb.DefineConstructor (\r
                         0, 0, new Type [1] {typeof(void)});\r
 \r
@@ -268,7 +276,7 @@ public class ConstructorBuilderTest : Assertion
        }\r
 \r
        public void TestSetCustomAttribute () {\r
-               TypeBuilder tb = module.DefineType ("class21", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                ConstructorBuilder cb = tb.DefineConstructor (\r
                         0, 0, \r
                        new Type [1] {typeof(int)});\r
@@ -304,5 +312,58 @@ public class ConstructorBuilderTest : Assertion
                } catch (ArgumentNullException) {\r
                }\r
        }\r
+\r
+       // Same as in MethodBuilderTest\r
+       [Test]\r
+       [ExpectedException (typeof (InvalidOperationException))]\r
+       public void TestAddDeclarativeSecurityAlreadyCreated () {\r
+               ConstructorBuilder cb = genClass.DefineConstructor (\r
+                        MethodAttributes.Public, 0, new Type [0]);\r
+               ILGenerator ilgen = cb.GetILGenerator ();\r
+               ilgen.Emit (OpCodes.Ret);\r
+               genClass.CreateType ();\r
+\r
+               PermissionSet set = new PermissionSet (PermissionState.Unrestricted);\r
+               cb.AddDeclarativeSecurity (SecurityAction.Demand, set);\r
+       }\r
+\r
+       [Test]\r
+       [ExpectedException (typeof (ArgumentNullException))]\r
+       public void TestAddDeclarativeSecurityNullPermissionSet () {\r
+               ConstructorBuilder cb = genClass.DefineConstructor (\r
+                        MethodAttributes.Public, 0, new Type [0]);\r
+               cb.AddDeclarativeSecurity (SecurityAction.Demand, null);\r
+       }\r
+\r
+       [Test]\r
+       public void TestAddDeclarativeSecurityInvalidAction () {\r
+               ConstructorBuilder cb = genClass.DefineConstructor (\r
+                        MethodAttributes.Public, 0, new Type [0]);\r
+\r
+               SecurityAction[] actions = new SecurityAction [] { \r
+                       SecurityAction.RequestMinimum,\r
+                       SecurityAction.RequestOptional,\r
+                       SecurityAction.RequestRefuse };\r
+               PermissionSet set = new PermissionSet (PermissionState.Unrestricted);\r
+\r
+               foreach (SecurityAction action in actions) {\r
+                       try {\r
+                               cb.AddDeclarativeSecurity (action, set);\r
+                               Fail ();\r
+                       }\r
+                       catch (ArgumentException) {\r
+                       }\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       [ExpectedException (typeof (InvalidOperationException))]\r
+       public void TestAddDeclarativeSecurityDuplicateAction () {\r
+               ConstructorBuilder cb = genClass.DefineConstructor (\r
+                        MethodAttributes.Public, 0, new Type [0]);\r
+               PermissionSet set = new PermissionSet (PermissionState.Unrestricted);\r
+               cb.AddDeclarativeSecurity (SecurityAction.Demand, set);\r
+               cb.AddDeclarativeSecurity (SecurityAction.Demand, set);\r
+       }\r
 }\r
 }\r
index ec38154fec5a8d6a180d07df03da3bd49f74bb07..87d6d52ec42feb3f0439d5baa37d339abdbb22ab 100644 (file)
@@ -8,13 +8,14 @@
 // TODO:\r
 //  - implement 'Signature' (what the hell it does???) and test it\r
 //  - implement Equals and test it\r
-//  - AddDeclarativeSecurity\r
 \r
 using System;\r
 using System.Threading;\r
 using System.Reflection;\r
 using System.Reflection.Emit;\r
 using System.Runtime.CompilerServices;\r
+using System.Security;\r
+using System.Security.Permissions;\r
 \r
 using NUnit.Framework;\r
 \r
@@ -39,17 +40,24 @@ public class MethodBuilderTest : Assertion
 \r
                module = assembly.DefineDynamicModule("module1");\r
                \r
-               genClass = module.DefineType("class1"\r
+               genClass = module.DefineType(genTypeName ()\r
                                                                         TypeAttributes.Public);\r
        }\r
 \r
        static int methodIndexer = 0;\r
 \r
+       static int typeIndexer = 0;\r
+\r
        // Return a unique method name\r
        private string genMethodName () {\r
                return "m" + (methodIndexer ++);\r
        }\r
 \r
+       // Return a unique type name\r
+       private string genTypeName () {\r
+               return "class" + (typeIndexer ++);\r
+       }\r
+\r
        public void TestAttributes () {\r
                MethodBuilder mb = genClass.DefineMethod (\r
                        genMethodName (), MethodAttributes.Public, typeof (void), new Type [0]);\r
@@ -185,7 +193,7 @@ public class MethodBuilderTest : Assertion
                }\r
 \r
                // Can not be called on a created type\r
-               TypeBuilder tb = module.DefineType ("class6", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                MethodBuilder mb2 = tb.DefineMethod (\r
                        genMethodName (), 0, typeof (void), new Type [0]);\r
                ILGenerator ilgen = mb2.GetILGenerator ();\r
@@ -200,7 +208,7 @@ public class MethodBuilderTest : Assertion
        }\r
 \r
        public void TestDefineParameter () {\r
-               TypeBuilder tb = module.DefineType ("class7", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                MethodBuilder mb = tb.DefineMethod (\r
                        genMethodName (), 0, typeof (void), \r
                        new Type [2] { typeof(int), typeof(int) });\r
@@ -275,7 +283,7 @@ public class MethodBuilderTest : Assertion
        }\r
 \r
        public void TestMethodImplementationFlags () {\r
-               TypeBuilder tb = module.DefineType ("class14", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                MethodBuilder mb = tb.DefineMethod (\r
                        genMethodName (), 0, typeof (void), new Type [0]);\r
 \r
@@ -310,7 +318,7 @@ public class MethodBuilderTest : Assertion
        }\r
 \r
        public void TestGetParameters () {\r
-               TypeBuilder tb = module.DefineType ("class16", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                MethodBuilder mb = tb.DefineMethod (\r
                        genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});\r
 \r
@@ -331,7 +339,7 @@ public class MethodBuilderTest : Assertion
        }\r
 \r
        public void TestGetToken () {\r
-               TypeBuilder tb = module.DefineType ("class17", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                MethodBuilder mb = tb.DefineMethod (\r
                        genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});\r
 \r
@@ -387,7 +395,7 @@ public class MethodBuilderTest : Assertion
        }\r
 \r
        public void TestSetCustomAttribute () {\r
-               TypeBuilder tb = module.DefineType ("class21", TypeAttributes.Public);\r
+               TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);\r
                string name = genMethodName ();\r
                MethodBuilder mb = tb.DefineMethod (\r
                        name, MethodAttributes.Public, typeof (void), \r
@@ -433,5 +441,61 @@ public class MethodBuilderTest : Assertion
                } catch (ArgumentNullException) {\r
                }\r
        }\r
+\r
+       [Test]\r
+       [ExpectedException (typeof (InvalidOperationException))]\r
+       public void TestAddDeclarativeSecurityAlreadyCreated () {\r
+               MethodBuilder mb = genClass.DefineMethod (\r
+                       genMethodName (), MethodAttributes.Public, typeof (void),\r
+                       new Type [0]);\r
+               ILGenerator ilgen = mb.GetILGenerator ();\r
+               ilgen.Emit (OpCodes.Ret);\r
+               genClass.CreateType ();\r
+\r
+               PermissionSet set = new PermissionSet (PermissionState.Unrestricted);\r
+               mb.AddDeclarativeSecurity (SecurityAction.Demand, set);\r
+       }\r
+\r
+       [Test]\r
+       [ExpectedException (typeof (ArgumentNullException))]\r
+       public void TestAddDeclarativeSecurityNullPermissionSet () {\r
+               MethodBuilder mb = genClass.DefineMethod (\r
+                       genMethodName (), MethodAttributes.Public, typeof (void), \r
+                       new Type [0]);\r
+               mb.AddDeclarativeSecurity (SecurityAction.Demand, null);\r
+       }\r
+\r
+       [Test]\r
+       public void TestAddDeclarativeSecurityInvalidAction () {\r
+               MethodBuilder mb = genClass.DefineMethod (\r
+                       genMethodName (), MethodAttributes.Public, typeof (void), \r
+                       new Type [0]);\r
+\r
+               SecurityAction[] actions = new SecurityAction [] { \r
+                       SecurityAction.RequestMinimum,\r
+                       SecurityAction.RequestOptional,\r
+                       SecurityAction.RequestRefuse };\r
+               PermissionSet set = new PermissionSet (PermissionState.Unrestricted);\r
+\r
+               foreach (SecurityAction action in actions) {\r
+                       try {\r
+                               mb.AddDeclarativeSecurity (action, set);\r
+                               Fail ();\r
+                       }\r
+                       catch (ArgumentException) {\r
+                       }\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       [ExpectedException (typeof (InvalidOperationException))]\r
+       public void TestAddDeclarativeSecurityDuplicateAction () {\r
+               MethodBuilder mb = genClass.DefineMethod (\r
+                       genMethodName (), MethodAttributes.Public, typeof (void), \r
+                       new Type [0]);\r
+               PermissionSet set = new PermissionSet (PermissionState.Unrestricted);\r
+               mb.AddDeclarativeSecurity (SecurityAction.Demand, set);\r
+               mb.AddDeclarativeSecurity (SecurityAction.Demand, set);\r
+       }\r
 }\r
 }\r