2 // PermissionSetCollectionTest.cs
3 // - NUnit Test Cases for PermissionSetCollection
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
34 using System.Collections;
35 using System.Security;
36 using System.Security.Permissions;
38 namespace MonoTests.System.Security {
40 // "alternate" IList implementation
41 class TestList : IList {
47 l = (IList) new ArrayList ();
50 public int Add (object value)
60 public bool Contains (object value)
62 return l.Contains (value);
65 public int IndexOf (object value)
67 return l.IndexOf (value);
70 public void Insert (int index, object value)
72 l.Insert (index, value);
75 public bool IsFixedSize {
76 get { return l.IsFixedSize; }
79 public bool IsReadOnly {
80 get { return l.IsReadOnly; }
83 public void Remove (object value)
88 public void RemoveAt (int index)
93 public object this [int index] {
94 get { return l [index]; }
95 set { l [index] = value; }
98 public void CopyTo (Array array, int index)
100 l.CopyTo (array, index);
104 get { return l.Count; }
107 public bool IsSynchronized {
108 get { return l.IsSynchronized; }
111 public object SyncRoot {
112 get { return l.SyncRoot; }
115 public IEnumerator GetEnumerator ()
117 return l.GetEnumerator ();
122 public class PermissionSetCollectionTest {
125 public void Constructor ()
127 PermissionSetCollection psc = new PermissionSetCollection ();
128 Assert.AreEqual (0, psc.Count, "Count");
129 Assert.IsFalse (psc.IsSynchronized, "IsSynchronized");
130 Assert.AreEqual (0, psc.PermissionSets.Count, "PermissionSets.Count");
131 Assert.AreEqual (psc.ToXml ().ToString (), psc.ToString (), "ToXml().ToString()==ToString()");
132 Assert.IsNotNull (psc.GetEnumerator (), "GetEnumerator");
136 [ExpectedException (typeof (NotSupportedException))]
137 public void SyncRoot ()
139 PermissionSetCollection psc = new PermissionSetCollection ();
140 Assert.IsNull (psc.SyncRoot, "SyncRoot");
144 [ExpectedException (typeof (ArgumentNullException))]
145 public void Add_Null ()
147 PermissionSetCollection psc = new PermissionSetCollection ();
154 PermissionSet none = new PermissionSet (PermissionState.None);
155 PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
156 PermissionSetCollection psc = new PermissionSetCollection ();
157 Assert.AreEqual (0, psc.PermissionSets.Count, "Count-0");
158 Assert.AreEqual (0, psc.Count, "Count-0");
160 Assert.AreEqual (1, psc.PermissionSets.Count, "Count-1");
161 // re-add same permissionset
163 Assert.AreEqual (2, psc.PermissionSets.Count, "Count-2");
165 Assert.AreEqual (3, psc.PermissionSets.Count, "Count-3");
166 Assert.AreEqual (3, psc.Count, "Count-3");
172 PermissionSet none = new PermissionSet (PermissionState.None);
173 PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
174 PermissionSetCollection psc = new PermissionSetCollection ();
175 PermissionSetCollection copy = psc.Copy ();
176 Assert.AreEqual (0, copy.PermissionSets.Count, "Count-0");
178 Assert.AreEqual (0, copy.PermissionSets.Count, "Count-0b");
180 Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1");
181 psc.Add (none); // re-add same permissionset
182 Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1b");
184 Assert.AreEqual (2, copy.PermissionSets.Count, "Count-2");
186 Assert.AreEqual (2, copy.PermissionSets.Count, "Count-2b");
188 Assert.AreEqual (3, copy.PermissionSets.Count, "Count-3");
189 Assert.AreEqual (3, copy.Count, "Count-3");
193 public void Copy_References ()
195 PermissionSet none = new PermissionSet (PermissionState.None);
196 PermissionSetCollection psc = new PermissionSetCollection ();
198 PermissionSetCollection copy = psc.Copy ();
199 Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1");
201 string before = psc.ToString ();
202 none.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
204 Assert.AreEqual (none.ToString (), psc.PermissionSets[0].ToString (), "psc");
205 Assert.AreEqual (before, copy.ToString (), "copy");
209 [ExpectedException (typeof (NotSupportedException))]
210 public void CopyTo ()
212 PermissionSetCollection psc = new PermissionSetCollection ();
213 psc.CopyTo (null, 0);
217 [ExpectedException (typeof (NotSupportedException))]
218 public void CopyTo_ICollection ()
220 PermissionSetCollection psc = new PermissionSetCollection ();
221 ICollection c = (psc as ICollection);
226 [ExpectedException (typeof (ArgumentNullException))]
227 public void FromXml_Null ()
229 PermissionSetCollection psc = new PermissionSetCollection ();
234 [ExpectedException (typeof (ArgumentException))]
235 public void FromXml_BadName ()
237 PermissionSetCollection psc = new PermissionSetCollection ();
238 SecurityElement se = new SecurityElement ("PermissionZetCollection");
243 public void FromXml_Roundtrip ()
245 PermissionSetCollection psc = new PermissionSetCollection ();
246 string expected = psc.ToString ();
247 SecurityElement se = psc.ToXml ();
249 string actual = psc.ToString ();
250 Assert.AreEqual (expected, actual, "Empty");
252 PermissionSet none = new PermissionSet (PermissionState.None);
254 expected = psc.ToString ();
257 actual = psc.ToString ();
258 Assert.AreEqual (expected, actual, "1-None");
260 none.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
261 expected = psc.ToString ();
264 actual = psc.ToString ();
265 Assert.AreEqual (expected, actual, "1-Assertion");
266 Assert.AreEqual (1, psc.Count, "1");
268 PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
270 expected = psc.ToString ();
273 actual = psc.ToString ();
274 Assert.AreEqual (expected, actual, "2-Assertion+Unrestricted");
275 Assert.AreEqual (2, psc.Count, "2");
279 [ExpectedException (typeof (ArgumentOutOfRangeException))]
280 public void GetSet_Negative ()
282 PermissionSetCollection psc = new PermissionSetCollection ();
287 [ExpectedException (typeof (ArgumentOutOfRangeException))]
288 public void GetSet_Zero_Empty ()
290 PermissionSetCollection psc = new PermissionSetCollection ();
295 [ExpectedException (typeof (ArgumentOutOfRangeException))]
296 public void GetSet_MaxInt ()
298 PermissionSetCollection psc = new PermissionSetCollection ();
299 psc.GetSet (Int32.MaxValue);
303 public void GetSet ()
305 PermissionSetCollection psc = new PermissionSetCollection ();
306 PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
308 PermissionSet ps = psc.GetSet (0);
309 Assert.AreEqual (unr.ToString (), ps.ToString (), "Same XML");
310 Assert.IsTrue (Object.ReferenceEquals (unr, ps), "Same Object Reference");
314 [ExpectedException (typeof (ArgumentOutOfRangeException))]
315 public void RemoveSet_Negative ()
317 PermissionSetCollection psc = new PermissionSetCollection ();
322 [ExpectedException (typeof (ArgumentOutOfRangeException))]
323 public void RemoveSet_Zero_Empty ()
325 PermissionSetCollection psc = new PermissionSetCollection ();
330 [ExpectedException (typeof (ArgumentOutOfRangeException))]
331 public void RemoveSet_MaxInt ()
333 PermissionSetCollection psc = new PermissionSetCollection ();
334 psc.RemoveSet (Int32.MaxValue);
338 public void RemoveSet ()
340 PermissionSetCollection psc = new PermissionSetCollection ();
341 PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
344 Assert.AreEqual (0, psc.Count, "Count");
350 PermissionSetCollection psc = new PermissionSetCollection ();
351 SecurityElement se = psc.ToXml ();
352 Assert.IsNull (se.Children, "Children==null for 0");
353 PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
356 Assert.AreEqual (1, se.Children.Count, "Children==1");
357 Assert.AreEqual (unr.ToString (), se.Children[0].ToString (), "XML");