2002-02-07 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / CodeGroupTest.cs
1 //\r
2 // MonoTests.System.Security.Policy.CodeGroupTest\r
3 //\r
4 // Author(s):\r
5 //   Nick Drochak (ndrochak@gol.com)\r
6 //\r
7 // (C) 2001 Nick Drochak, All rights reserved.
8 \r
9 using NUnit.Framework;\r
10 using System;\r
11 using System.Security;\r
12 using System.Security.Policy;\r
13 using System.Security.Permissions;\r
14 \r
15 namespace MonoTests.System.Security.Policy\r
16 {\r
17 \r
18         public class CodeGroupTest : TestCase \r
19         {\r
20                 \r
21                 public CodeGroupTest( string name ): base(name)\r
22                 {\r
23                 }\r
24                 \r
25                 public CodeGroupTest() : base("CodeGroupTest")\r
26                 {\r
27                 }\r
28 \r
29                 public static ITest Suite\r
30                 {\r
31                         get\r
32                         {\r
33                                 return new TestSuite(typeof(CodeGroupTest));\r
34                         }\r
35                 }\r
36 \r
37                 protected override void SetUp()\r
38                 {\r
39                 }\r
40 \r
41                 public class MyCodeGroup : CodeGroup\r
42                 {\r
43                         public MyCodeGroup(IMembershipCondition membershipCondition,
44                                 PolicyStatement policy) :base(membershipCondition, policy)
45                         {
46                         }
47
48                         public override CodeGroup Copy()\r
49                         {\r
50                                 return this;\r
51                         }\r
52 \r
53                         public override string MergeLogic\r
54                         {\r
55                                 get\r
56                                 {\r
57                                         return "";\r
58                                 }\r
59                         }\r
60 \r
61                         public override PolicyStatement Resolve(        Evidence evidence)\r
62                         {\r
63                                 return (PolicyStatement)null;\r
64                         }\r
65 \r
66                         public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)\r
67                         {\r
68                                 return this;\r
69                         }\r
70                 }\r
71 \r
72                 public void TestConstructorExceptions()\r
73                 {\r
74                         MyCodeGroup cg;\r
75                         try\r
76                         {\r
77                                 cg = new MyCodeGroup(null, null);\r
78                                 Fail("Constructor should throw exception on null paramters");\r
79                         }\r
80                         catch(Exception e)\r
81                         {\r
82                                 Assert("Should have caught an ArgumentNull Exception", e is ArgumentNullException);\r
83                         }\r
84                 }\r
85 \r
86                 public void TestConstructor()\r
87                 {\r
88                         MyCodeGroup cg = null;\r
89                         try\r
90                         {\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
94                         }\r
95                         catch(Exception e)\r
96                         {\r
97                                 Fail("Constructor failed. Exception caught was: " + e.ToString());\r
98                         }\r
99                 }\r
100 \r
101                 public void TestDescriptionProperty()\r
102                 {\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
108                 }\r
109 \r
110                 public void TestNameProperty()\r
111                 {\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
115                         cg.Name = name;\r
116                         Assert("Description not the expected value", cg.Name == name);\r
117                 }\r
118 \r
119                 public void TestChildren()\r
120                 {\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
125                 }\r
126 \r
127                 public void TestAttributeStringProperty()\r
128                 {\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
135                 }\r
136 \r
137                 public void TestPermissionSetNameProperty()\r
138                 {\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
144                 }\r
145 \r
146                 public void TestEquals()\r
147                 {\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
155                         bool isEquals;\r
156                         isEquals = cg.Equals(cg);\r
157                         \r
158                         isEquals = cg.Equals("Not Equal to this");\r
159                         Assert("CodeGroup should not be equal to a non-CodeGroup type", !isEquals);\r
160 \r
161                         cg2 = new MyCodeGroup(new AllMembershipCondition(), ps);\r
162                         cg2.Name = "SomeOtherName";\r
163                         cg2.Description = "Some Other Description";\r
164 \r
165                         isEquals = cg.Equals(cg2);\r
166                         Assert("CodeGroup should not be equal when Name or Description is different", !isEquals);\r
167 \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
173                 }\r
174 \r
175                 public void TestEqualsWithChildren()\r
176                 {\r
177                         MyCodeGroup cg = null;\r
178                         MyCodeGroup cg2 = null;\r
179                         MyCodeGroup cgChild = null;\r
180                         const string ps_Name = "TestName";\r
181                         bool isEquals;\r
182 \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
187 \r
188                         cgChild = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);\r
189                         cgChild.Name = "ChildName";\r
190                         cgChild.Description = "Child Descripiton";\r
191 \r
192                         cg.AddChild(cgChild);\r
193 \r
194                         cg2 = new MyCodeGroup(cg.MembershipCondition, cg.PolicyStatement);\r
195                         cg2.Name = cg.Name;\r
196                         cg2.Description = cg.Description;\r
197 \r
198                         isEquals = cg.Equals(cg2);\r
199                         Assert("Should be equal when Children are ignored", isEquals);\r
200 \r
201                         isEquals = cg.Equals(cg2, true);\r
202                         Assert("Should not be equal when Child count is different", !isEquals);\r
203 \r
204                         cg2.AddChild(cgChild);\r
205                         isEquals = cg2.Equals(cg, true);\r
206                         Assert("Should be equal when children are equal", isEquals);\r
207                 }\r
208 \r
209         \r
210                 public void TestRemoveChild()\r
211                 {\r
212                         MyCodeGroup cg = null;\r
213                         MyCodeGroup cgChild = null;\r
214                         MyCodeGroup cgChild2 = null;\r
215                         const string ps_Name = "TestName";\r
216 \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
221 \r
222                         cgChild = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);\r
223                         cgChild.Name = "ChildName";\r
224                         cgChild.Description = "Child Descripiton";\r
225 \r
226                         cg.AddChild(cgChild);\r
227 \r
228                         cgChild2 = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);\r
229                         cgChild2.Name = "ChildName2";\r
230                         cgChild2.Description = "Child Descripiton 2";\r
231 \r
232                         cg.AddChild(cgChild2);\r
233 \r
234                         AssertEquals("Should be two children before the call to Remove()", 2, cg.Children.Count);\r
235 \r
236                         cg.RemoveChild(cgChild);\r
237 \r
238                         AssertEquals("Remaing child does not have correct name", "ChildName2", ((CodeGroup)cg.Children[0]).Name);\r
239                         try\r
240                         {\r
241                                 cg.RemoveChild(cgChild);\r
242                                 Fail("Should have throw error on trying to remove non-existant child");\r
243                         }\r
244                         catch{}\r
245                 }\r
246         }  // public class CodeGroupTest \r
247 \r
248 }  // namespace MonoTests.System.Security.Policy