0e5dbded301543186b7901747b01e51b64f6d6d9
[mono.git] / mcs / class / corlib / Test / System.Security / PermissionSetCollectionTest.cs
1 //
2 // PermissionSetCollectionTest.cs 
3 //      - NUnit Test Cases for PermissionSetCollection
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 #if NET_2_0 && !TARGET_JVM
31
32 using NUnit.Framework;
33 using System;
34 using System.Collections;
35 using System.Security;
36 using System.Security.Permissions;
37
38 namespace MonoTests.System.Security {
39
40         // "alternate" IList implementation
41         class TestList : IList {
42
43                 private IList l;
44
45                 public TestList ()
46                 {
47                         l = (IList) new ArrayList ();
48                 }
49
50                 public int Add (object value)
51                 {
52                         return l.Add (value);
53                 }
54
55                 public void Clear ()
56                 {
57                         l.Clear ();
58                 }
59
60                 public bool Contains (object value)
61                 {
62                         return l.Contains (value);
63                 }
64
65                 public int IndexOf (object value)
66                 {
67                         return l.IndexOf (value);
68                 }
69
70                 public void Insert (int index, object value)
71                 {
72                         l.Insert (index, value);
73                 }
74
75                 public bool IsFixedSize {
76                         get { return l.IsFixedSize; }
77                 }
78
79                 public bool IsReadOnly {
80                         get { return l.IsReadOnly; }
81                 }
82
83                 public void Remove (object value)
84                 {
85                         l.Remove (value);
86                 }
87
88                 public void RemoveAt (int index)
89                 {
90                         l.RemoveAt (index);
91                 }
92
93                 public object this [int index] {
94                         get { return l [index]; }
95                         set { l [index] = value; }
96                 }
97
98                 public void CopyTo (Array array, int index)
99                 {
100                         l.CopyTo (array, index);
101                 }
102
103                 public int Count {
104                         get { return l.Count; }
105                 }
106
107                 public bool IsSynchronized {
108                         get { return l.IsSynchronized; }
109                 }
110
111                 public object SyncRoot {
112                         get { return l.SyncRoot; }
113                 }
114
115                 public IEnumerator GetEnumerator ()
116                 {
117                         return l.GetEnumerator ();
118                 }
119         }
120
121         [TestFixture]
122         public class PermissionSetCollectionTest {
123
124                 [Test]
125                 public void Constructor ()
126                 {
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");
133                 }
134
135                 [Test]
136                 [ExpectedException (typeof (NotSupportedException))]
137                 public void SyncRoot ()
138                 {
139                         PermissionSetCollection psc = new PermissionSetCollection ();
140                         Assert.IsNull (psc.SyncRoot, "SyncRoot");
141                 }
142
143                 [Test]
144                 [ExpectedException (typeof (ArgumentNullException))]
145                 public void Add_Null ()
146                 {
147                         PermissionSetCollection psc = new PermissionSetCollection ();
148                         psc.Add (null);
149                 }
150
151                 [Test]
152                 public void Add ()
153                 {
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");
159                         psc.Add (none);
160                         Assert.AreEqual (1, psc.PermissionSets.Count, "Count-1");
161                         // re-add same permissionset
162                         psc.Add (none);
163                         Assert.AreEqual (2, psc.PermissionSets.Count, "Count-2");
164                         psc.Add (unr);
165                         Assert.AreEqual (3, psc.PermissionSets.Count, "Count-3");
166                         Assert.AreEqual (3, psc.Count, "Count-3");
167                 }
168
169                 [Test]
170                 public void Copy ()
171                 {
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");
177                         psc.Add (none);
178                         Assert.AreEqual (0, copy.PermissionSets.Count, "Count-0b");
179                         copy = psc.Copy ();
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");
183                         copy = psc.Copy ();
184                         Assert.AreEqual (2, copy.PermissionSets.Count, "Count-2");
185                         psc.Add (unr);
186                         Assert.AreEqual (2, copy.PermissionSets.Count, "Count-2b");
187                         copy = psc.Copy ();
188                         Assert.AreEqual (3, copy.PermissionSets.Count, "Count-3");
189                         Assert.AreEqual (3, copy.Count, "Count-3");
190                 }
191
192                 [Test]
193                 public void Copy_References ()
194                 {
195                         PermissionSet none = new PermissionSet (PermissionState.None);
196                         PermissionSetCollection psc = new PermissionSetCollection ();
197                         psc.Add (none);
198                         PermissionSetCollection copy = psc.Copy ();
199                         Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1");
200
201                         string before = psc.ToString ();
202                         none.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
203
204                         Assert.AreEqual (none.ToString (), psc.PermissionSets[0].ToString (), "psc");
205                         Assert.AreEqual (before, copy.ToString (), "copy");
206                 }
207
208                 [Test]
209                 [ExpectedException (typeof (NotSupportedException))]
210                 public void CopyTo ()
211                 {
212                         PermissionSetCollection psc = new PermissionSetCollection ();
213                         psc.CopyTo (null, 0);
214                 }
215
216                 [Test]
217                 [ExpectedException (typeof (NotSupportedException))]
218                 public void CopyTo_ICollection ()
219                 {
220                         PermissionSetCollection psc = new PermissionSetCollection ();
221                         ICollection c = (psc as ICollection);
222                         c.CopyTo (null, 0);
223                 }
224
225                 [Test]
226                 [ExpectedException (typeof (ArgumentNullException))]
227                 public void FromXml_Null ()
228                 {
229                         PermissionSetCollection psc = new PermissionSetCollection ();
230                         psc.FromXml (null);
231                 }
232
233                 [Test]
234                 [ExpectedException (typeof (ArgumentException))]
235                 public void FromXml_BadName ()
236                 {
237                         PermissionSetCollection psc = new PermissionSetCollection ();
238                         SecurityElement se = new SecurityElement ("PermissionZetCollection");
239                         psc.FromXml (se);
240                 }
241
242                 [Test]
243                 public void FromXml_Roundtrip ()
244                 {
245                         PermissionSetCollection psc = new PermissionSetCollection ();
246                         string expected = psc.ToString ();
247                         SecurityElement se = psc.ToXml ();
248                         psc.FromXml (se);
249                         string actual = psc.ToString ();
250                         Assert.AreEqual (expected, actual, "Empty");
251
252                         PermissionSet none = new PermissionSet (PermissionState.None);
253                         psc.Add (none);
254                         expected = psc.ToString ();
255                         se = psc.ToXml ();
256                         psc.FromXml (se);
257                         actual = psc.ToString ();
258                         Assert.AreEqual (expected, actual, "1-None");
259
260                         none.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
261                         expected = psc.ToString ();
262                         se = psc.ToXml ();
263                         psc.FromXml (se);
264                         actual = psc.ToString ();
265                         Assert.AreEqual (expected, actual, "1-Assertion");
266                         Assert.AreEqual (1, psc.Count, "1");
267
268                         PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
269                         psc.Add (unr);
270                         expected = psc.ToString ();
271                         se = psc.ToXml ();
272                         psc.FromXml (se);
273                         actual = psc.ToString ();
274                         Assert.AreEqual (expected, actual, "2-Assertion+Unrestricted");
275                         Assert.AreEqual (2, psc.Count, "2");
276                 }
277
278                 [Test]
279                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
280                 public void GetSet_Negative ()
281                 {
282                         PermissionSetCollection psc = new PermissionSetCollection ();
283                         psc.GetSet (-1);
284                 }
285
286                 [Test]
287                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
288                 public void GetSet_Zero_Empty ()
289                 {
290                         PermissionSetCollection psc = new PermissionSetCollection ();
291                         psc.GetSet (0);
292                 }
293
294                 [Test]
295                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
296                 public void GetSet_MaxInt ()
297                 {
298                         PermissionSetCollection psc = new PermissionSetCollection ();
299                         psc.GetSet (Int32.MaxValue);
300                 }
301
302                 [Test]
303                 public void GetSet ()
304                 {
305                         PermissionSetCollection psc = new PermissionSetCollection ();
306                         PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
307                         psc.Add (unr);
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");
311                 }
312
313                 [Test]
314                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
315                 public void RemoveSet_Negative ()
316                 {
317                         PermissionSetCollection psc = new PermissionSetCollection ();
318                         psc.RemoveSet (-1);
319                 }
320
321                 [Test]
322                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
323                 public void RemoveSet_Zero_Empty ()
324                 {
325                         PermissionSetCollection psc = new PermissionSetCollection ();
326                         psc.RemoveSet (0);
327                 }
328
329                 [Test]
330                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
331                 public void RemoveSet_MaxInt ()
332                 {
333                         PermissionSetCollection psc = new PermissionSetCollection ();
334                         psc.RemoveSet (Int32.MaxValue);
335                 }
336
337                 [Test]
338                 public void RemoveSet ()
339                 {
340                         PermissionSetCollection psc = new PermissionSetCollection ();
341                         PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
342                         psc.Add (unr);
343                         psc.RemoveSet (0);
344                         Assert.AreEqual (0, psc.Count, "Count");
345                 }
346
347                 [Test]
348                 public void ToXml ()
349                 {
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);
354                         psc.Add (unr);
355                         se = psc.ToXml ();
356                         Assert.AreEqual (1, se.Children.Count, "Children==1");
357                         Assert.AreEqual (unr.ToString (), se.Children[0].ToString (), "XML");
358                 }
359         }
360 }
361
362 #endif