2 // MonoTests.System.Security.Policy.CodeGroupTest
\r
5 // Nick Drochak (ndrochak@gol.com)
\r
7 // (C) 2001 Nick Drochak, All rights reserved.
9 using NUnit.Framework;
\r
11 using System.Security;
\r
12 using System.Security.Policy;
\r
13 using System.Security.Permissions;
\r
15 namespace MonoTests.System.Security.Policy
\r
18 public class CodeGroupTest : TestCase
\r
21 public CodeGroupTest( string name ): base(name)
\r
25 public CodeGroupTest() : base("CodeGroupTest")
\r
29 public static ITest Suite
\r
33 return new TestSuite(typeof(CodeGroupTest));
\r
37 protected override void SetUp()
\r
41 public class MyCodeGroup : CodeGroup
\r
43 public MyCodeGroup(IMembershipCondition membershipCondition,
44 PolicyStatement policy) :base(membershipCondition, policy)
48 public override CodeGroup Copy()
\r
53 public override string MergeLogic
\r
61 public override PolicyStatement Resolve( Evidence evidence)
\r
63 return (PolicyStatement)null;
\r
66 public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
\r
72 public void TestConstructorExceptions()
\r
77 cg = new MyCodeGroup(null, null);
\r
78 Fail("Constructor should throw exception on null paramters");
\r
82 Assert("Should have caught an ArgumentNull Exception", e is ArgumentNullException);
\r
86 public void TestConstructor()
\r
88 MyCodeGroup cg = null;
\r
91 cg = new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
\r
92 Assert("PolicyStatement property not set correctly by constructor.", cg.PolicyStatement != null);
\r
93 Assert("MembershipCondition property not set correctly by constructor.", cg.MembershipCondition != null);
\r
97 Fail("Constructor failed. Exception caught was: " + e.ToString());
\r
101 public void TestDescriptionProperty()
\r
103 MyCodeGroup cg = null;
\r
104 const string description = "Test Description";
\r
105 cg = new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
\r
106 cg.Description = description;
\r
107 Assert("Description not the expected value", cg.Description == description);
\r
110 public void TestNameProperty()
\r
112 MyCodeGroup cg = null;
\r
113 const string name = "Test Name";
\r
114 cg = new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
\r
116 Assert("Description not the expected value", cg.Name == name);
\r
119 public void TestChildren()
\r
121 MyCodeGroup cg = null;
\r
122 cg = new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
\r
123 cg.AddChild(new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.Unrestricted))));
\r
124 Assert("Unexpected number of children", cg.Children.Count == 1);
\r
127 public void TestAttributeStringProperty()
\r
129 MyCodeGroup cg = null;
\r
130 PolicyStatementAttribute psa = PolicyStatementAttribute.LevelFinal;
\r
131 PolicyStatement ps = new PolicyStatement(new PermissionSet(PermissionState.None));
\r
132 ps.Attributes = psa;
\r
133 cg = new MyCodeGroup(new AllMembershipCondition(), ps);
\r
134 AssertEquals("AttributeString", psa.ToString(), cg.AttributeString);
\r
137 public void TestPermissionSetNameProperty()
\r
139 MyCodeGroup cg = null;
\r
140 const string ps_Name = "TestName";
\r
141 PolicyStatement ps = new PolicyStatement(new NamedPermissionSet(ps_Name));
\r
142 cg = new MyCodeGroup(new AllMembershipCondition(), ps);
\r
143 AssertEquals("AttributeString", ps_Name, cg.PermissionSetName);
\r
146 public void TestEquals()
\r
148 MyCodeGroup cg = null;
\r
149 MyCodeGroup cg2 = null;
\r
150 const string ps_Name = "TestName";
\r
151 PolicyStatement ps = new PolicyStatement(new NamedPermissionSet(ps_Name));
\r
152 cg = new MyCodeGroup(new AllMembershipCondition(), ps);
\r
153 cg.Name = "SomeName";
\r
154 cg.Description = "Some Description";
\r
156 isEquals = cg.Equals(cg);
\r
158 isEquals = cg.Equals("Not Equal to this");
\r
159 Assert("CodeGroup should not be equal to a non-CodeGroup type", !isEquals);
\r
161 cg2 = new MyCodeGroup(new AllMembershipCondition(), ps);
\r
162 cg2.Name = "SomeOtherName";
\r
163 cg2.Description = "Some Other Description";
\r
165 isEquals = cg.Equals(cg2);
\r
166 Assert("CodeGroup should not be equal when Name or Description is different", !isEquals);
\r
168 cg2 = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);
\r
169 cg2.Name = cg.Name;
\r
170 cg2.Description = cg.Description;
\r
171 isEquals = cg.Equals(cg2);
\r
172 Assert("CodeGroup should not be equal when Membership Condition is different", !isEquals);
\r
175 public void TestEqualsWithChildren()
\r
177 MyCodeGroup cg = null;
\r
178 MyCodeGroup cg2 = null;
\r
179 MyCodeGroup cgChild = null;
\r
180 const string ps_Name = "TestName";
\r
183 PolicyStatement ps = new PolicyStatement(new NamedPermissionSet(ps_Name));
\r
184 cg = new MyCodeGroup(new AllMembershipCondition(), ps);
\r
185 cg.Name = "SomeName";
\r
186 cg.Description = "Some Description";
\r
188 cgChild = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);
\r
189 cgChild.Name = "ChildName";
\r
190 cgChild.Description = "Child Descripiton";
\r
192 cg.AddChild(cgChild);
\r
194 cg2 = new MyCodeGroup(cg.MembershipCondition, cg.PolicyStatement);
\r
195 cg2.Name = cg.Name;
\r
196 cg2.Description = cg.Description;
\r
198 isEquals = cg.Equals(cg2);
\r
199 Assert("Should be equal when Children are ignored", isEquals);
\r
201 isEquals = cg.Equals(cg2, true);
\r
202 Assert("Should not be equal when Child count is different", !isEquals);
\r
204 cg2.AddChild(cgChild);
\r
205 isEquals = cg2.Equals(cg, true);
\r
206 Assert("Should be equal when children are equal", isEquals);
\r
210 public void TestRemoveChild()
\r
212 MyCodeGroup cg = null;
\r
213 MyCodeGroup cgChild = null;
\r
214 MyCodeGroup cgChild2 = null;
\r
215 const string ps_Name = "TestName";
\r
217 PolicyStatement ps = new PolicyStatement(new NamedPermissionSet(ps_Name));
\r
218 cg = new MyCodeGroup(new AllMembershipCondition(), ps);
\r
219 cg.Name = "SomeName";
\r
220 cg.Description = "Some Description";
\r
222 cgChild = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);
\r
223 cgChild.Name = "ChildName";
\r
224 cgChild.Description = "Child Descripiton";
\r
226 cg.AddChild(cgChild);
\r
228 cgChild2 = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);
\r
229 cgChild2.Name = "ChildName2";
\r
230 cgChild2.Description = "Child Descripiton 2";
\r
232 cg.AddChild(cgChild2);
\r
234 AssertEquals("Should be two children before the call to Remove()", 2, cg.Children.Count);
\r
236 cg.RemoveChild(cgChild);
\r
238 AssertEquals("Remaing child does not have correct name", "ChildName2", ((CodeGroup)cg.Children[0]).Name);
\r
241 cg.RemoveChild(cgChild);
\r
242 Fail("Should have throw error on trying to remove non-existant child");
\r
246 } // public class CodeGroupTest
\r
248 } // namespace MonoTests.System.Security.Policy