// 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
\r
private ModuleBuilder module;\r
\r
+ private static int typeIndexer = 0;\r
+\r
[SetUp]\r
protected void SetUp () {\r
AssemblyName assemblyName = new AssemblyName();\r
\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
}\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
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
}\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
}\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
}\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
} 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
// 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
\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
}\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
}\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
}\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
}\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
}\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
}\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
} 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