2 // NamedPermissionSetTest.cs - NUnit Test Cases for NamedPermissionSet
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-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.
30 using NUnit.Framework;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace MonoTests.System.Security {
38 public class NamedPermissionSetTest : Assertion {
40 private static string name = "mono";
41 private static string sentinel = "go mono!";
44 [ExpectedException (typeof (ArgumentException))]
45 public void ConstructorNameNull ()
47 string s = null; // we don't want to confuse the compiler
48 NamedPermissionSet nps = new NamedPermissionSet (s);
52 [ExpectedException (typeof (ArgumentException))]
53 public void ConstructorNameEmpty ()
55 NamedPermissionSet nps = new NamedPermissionSet ("");
59 public void ConstructorName ()
\r
61 NamedPermissionSet nps = new NamedPermissionSet ("name");
\r
62 AssertEquals ("Name", "name", nps.Name);
\r
63 AssertNull ("Description", nps.Description);
\r
64 Assert ("IsUnrestricted", nps.IsUnrestricted ());
\r
65 Assert ("IsEmpty", !nps.IsEmpty ());
\r
66 Assert ("IsReadOnly", !nps.IsReadOnly);
\r
67 Assert ("IsSynchronized", !nps.IsSynchronized);
\r
68 AssertEquals ("Count", 0, nps.Count);
\r
72 public void ConstructorNameReserved ()
\r
74 NamedPermissionSet nps = new NamedPermissionSet ("FullTrust");
\r
75 AssertEquals ("Name", "FullTrust", nps.Name);
\r
76 AssertNull ("Description", nps.Description);
\r
77 Assert ("IsUnrestricted", nps.IsUnrestricted ());
\r
78 Assert ("IsEmpty", !nps.IsEmpty ());
\r
79 Assert ("IsReadOnly", !nps.IsReadOnly);
\r
80 Assert ("IsSynchronized", !nps.IsSynchronized);
\r
81 AssertEquals ("Count", 0, nps.Count);
\r
85 [ExpectedException (typeof (NullReferenceException))]
\r
86 public void ConstructorNamedPermissionSetNull ()
\r
88 NamedPermissionSet nullps = null;
\r
89 NamedPermissionSet nps = new NamedPermissionSet (nullps);
\r
93 [ExpectedException (typeof (ArgumentException))]
\r
94 public void ConstructorNameNullPermissionState ()
\r
96 new NamedPermissionSet (null, PermissionState.None);
\r
100 [ExpectedException (typeof (ArgumentException))]
\r
101 public void ConstructorNameEmptyPermissionState ()
\r
103 new NamedPermissionSet (String.Empty, PermissionState.None);
\r
107 public void ConstructorNamePermissionStateNone ()
\r
109 NamedPermissionSet nps = new NamedPermissionSet ("name", PermissionState.None);
\r
110 AssertEquals ("Name", "name", nps.Name);
\r
111 AssertNull ("Description", nps.Description);
\r
112 Assert ("IsUnrestricted", !nps.IsUnrestricted ());
\r
113 Assert ("IsEmpty", nps.IsEmpty ());
\r
114 Assert ("IsReadOnly", !nps.IsReadOnly);
\r
115 Assert ("IsSynchronized", !nps.IsSynchronized);
\r
116 AssertEquals ("Count", 0, nps.Count);
\r
120 public void ConstructorNamePermissionStateUnrestricted ()
\r
122 NamedPermissionSet nps = new NamedPermissionSet ("name", PermissionState.Unrestricted);
\r
123 AssertEquals ("Name", "name", nps.Name);
\r
124 AssertNull ("Description", nps.Description);
\r
125 Assert ("IsUnrestricted", nps.IsUnrestricted ());
\r
126 Assert ("IsEmpty", !nps.IsEmpty ());
\r
127 Assert ("IsReadOnly", !nps.IsReadOnly);
\r
128 Assert ("IsSynchronized", !nps.IsSynchronized);
\r
129 AssertEquals ("Count", 0, nps.Count);
\r
133 [ExpectedException (typeof (ArgumentException))]
\r
134 public void ConstructorNameNullPermissionSet ()
\r
136 new NamedPermissionSet (null, new PermissionSet (PermissionState.None));
\r
140 [ExpectedException (typeof (ArgumentException))]
\r
141 public void ConstructorNameEmptyPermissionSet ()
\r
143 new NamedPermissionSet (String.Empty, new PermissionSet (PermissionState.None));
\r
147 public void ConstructorNamePermissionSetNull ()
\r
149 NamedPermissionSet nps = new NamedPermissionSet ("name", null);
\r
150 AssertEquals ("Name", "name", nps.Name);
\r
151 AssertNull ("Description", nps.Description);
\r
153 Assert ("IsUnrestricted", !nps.IsUnrestricted ());
\r
154 Assert ("IsEmpty", nps.IsEmpty ());
\r
156 Assert ("IsUnrestricted", nps.IsUnrestricted ());
\r
157 Assert ("IsEmpty", !nps.IsEmpty ());
\r
159 Assert ("IsReadOnly", !nps.IsReadOnly);
\r
160 Assert ("IsSynchronized", !nps.IsSynchronized);
\r
161 AssertEquals ("Count", 0, nps.Count);
\r
165 public void Description ()
167 NamedPermissionSet nps = new NamedPermissionSet (name);
168 // null by default (not empty)
169 AssertNull ("Description", nps.Description);
170 // is null-able (without exception)
171 nps.Description = null;
172 AssertNull ("Description(null)", nps.Description);
173 nps.Description = sentinel;
174 AssertEquals ("Description", sentinel, nps.Description);
178 [ExpectedException (typeof (ArgumentException))]
179 public void NameNull ()
181 NamedPermissionSet nps = new NamedPermissionSet (name);
183 // strangely this isn't a ArgumentNullException (but so says the doc)
187 [ExpectedException (typeof (ArgumentException))]
188 public void NameEmpty ()
190 NamedPermissionSet nps = new NamedPermissionSet (name);
197 NamedPermissionSet nps = new NamedPermissionSet (name);
199 AssertEquals ("Name", sentinel, nps.Name);
205 NamedPermissionSet nps = new NamedPermissionSet (name);
206 nps.Description = sentinel;
207 nps.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
208 NamedPermissionSet copy = (NamedPermissionSet)nps.Copy ();
209 AssertEquals ("Name", nps.Name, copy.Name);
210 AssertEquals ("Description", nps.Description, copy.Description);
211 AssertEquals ("Count", nps.Count, copy.Count);
215 public void Copy_Name ()
217 NamedPermissionSet nps = new NamedPermissionSet (name);
218 nps.Description = sentinel;
219 nps.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
220 NamedPermissionSet copy = (NamedPermissionSet)nps.Copy ("Copy");
221 AssertEquals ("Name", "Copy", copy.Name);
222 AssertEquals ("Description", nps.Description, copy.Description);
223 AssertEquals ("Count", nps.Count, copy.Count);
227 [ExpectedException (typeof (ArgumentException))]
228 public void Copy_Name_Null ()
230 NamedPermissionSet nps = new NamedPermissionSet (name);
231 NamedPermissionSet copy = (NamedPermissionSet)nps.Copy (null);
235 [ExpectedException (typeof (ArgumentException))]
236 public void Copy_Name_Empty ()
238 NamedPermissionSet nps = new NamedPermissionSet (name);
239 NamedPermissionSet copy = (NamedPermissionSet)nps.Copy (String.Empty);
243 [ExpectedException (typeof (ArgumentNullException))]
244 public void FromXml_Null ()
246 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
251 [ExpectedException (typeof (ArgumentException))]
252 public void FromXml_InvalidPermission ()
254 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
255 SecurityElement se = nps.ToXml ();
256 // can't modify - so we create our own
257 SecurityElement se2 = new SecurityElement ("InvalidPermissionSet", se.Text);
258 se2.AddAttribute ("class", se.Attribute ("class"));
259 se2.AddAttribute ("version", se.Attribute ("version"));
260 se2.AddAttribute ("Name", se.Attribute ("Name"));
265 [ExpectedException (typeof (ArgumentException))]
266 public void FromXml_WrongTagCase ()
268 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
269 SecurityElement se = nps.ToXml ();
270 se.Tag = se.Tag.ToUpper (); // instead of PermissionSet
275 public void FromXml_WrongClass ()
277 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
278 SecurityElement se = nps.ToXml ();
280 SecurityElement w = new SecurityElement (se.Tag);
281 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
282 w.AddAttribute ("version", se.Attribute ("version"));
283 w.AddAttribute ("Name", se.Attribute ("Name"));
285 // doesn't care of the class name at that stage
286 // anyway the class has already be created so...
290 public void FromXml_NoClass ()
292 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
293 SecurityElement se = nps.ToXml ();
295 SecurityElement w = new SecurityElement (se.Tag);
296 w.AddAttribute ("version", se.Attribute ("version"));
298 // doesn't even care of the class attribute presence
302 // [ExpectedException (typeof (ArgumentException))]
303 public void FromXml_WrongVersion ()
305 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
306 SecurityElement se = nps.ToXml ();
307 // can't modify - so we create our own
308 SecurityElement se2 = new SecurityElement (se.Tag, se.Text);
309 se2.AddAttribute ("class", se.Attribute ("class"));
310 se2.AddAttribute ("version", "2");
311 se2.AddAttribute ("Name", se.Attribute ("Name"));
313 // wow - here we accept a version 2 !!!
317 public void FromXml_NoVersion ()
319 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
320 SecurityElement se = nps.ToXml ();
322 SecurityElement w = new SecurityElement (se.Tag);
323 w.AddAttribute ("class", se.Attribute ("class"));
324 w.AddAttribute ("Name", se.Attribute ("Name"));
329 public void FromXml_NoName ()
331 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
332 SecurityElement se = nps.ToXml ();
334 SecurityElement w = new SecurityElement (se.Tag);
335 w.AddAttribute ("class", se.Attribute ("class"));
336 w.AddAttribute ("version", "1");
339 // having a null name can badly influence the rest of the class code
340 AssertNull ("Name", nps.Name);
341 NamedPermissionSet copy = (NamedPermissionSet) nps.Copy ();
342 AssertNull ("Copy.Name", copy.Name);
344 copy = nps.Copy ("name");
345 AssertEquals ("Copy(Name).Name", "name", copy.Name);
348 AssertNull ("Name attribute", se.Attribute ("Name"));
350 AssertEquals ("GetHashCode", 0, nps.GetHashCode ());
351 Assert ("Equals-self", nps.Equals (nps));
356 public void FromXml ()
358 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
359 SecurityElement se = nps.ToXml ();
360 AssertNotNull ("ToXml()", se);
362 NamedPermissionSet nps2 = (NamedPermissionSet) nps.Copy ();
364 AssertEquals ("FromXml-Copy.Name", name, nps2.Name);
365 // strangely it's empty when converted from XML (but null when created)
366 AssertEquals ("FromXml-Copy.Description", "", nps2.Description);
367 Assert ("FromXml-Copy.IsUnrestricted", !nps2.IsUnrestricted ());
369 se.AddAttribute ("Description", sentinel);
371 AssertEquals ("FromXml-Add1.Name", name, nps2.Name);
372 AssertEquals ("FromXml-Add1.Description", sentinel, nps2.Description);
373 Assert ("FromXml-Add1.IsUnrestricted", !nps2.IsUnrestricted ());
375 se.AddAttribute ("Unrestricted", "true");
377 AssertEquals ("FromXml-Add2.Name", name, nps2.Name);
378 AssertEquals ("FromXml-Add2.Description", sentinel, nps2.Description);
379 Assert ("FromXml-Add2.IsUnrestricted", nps2.IsUnrestricted ());
383 public void ToXml_None ()
385 NamedPermissionSet ps = new NamedPermissionSet (name, PermissionState.None);
386 ps.Description = sentinel;
387 SecurityElement se = ps.ToXml ();
388 Assert ("None.ToString().StartsWith", ps.ToString().StartsWith ("<PermissionSet"));
389 AssertEquals ("None.class", "System.Security.NamedPermissionSet", (se.Attributes ["class"] as string));
390 AssertEquals ("None.version", "1", (se.Attributes ["version"] as string));
391 AssertEquals ("None.Name", name, (se.Attributes ["Name"] as string));
392 AssertEquals ("None.Description", sentinel, (se.Attributes ["Description"] as string));
393 AssertNull ("None.Unrestricted", (se.Attributes ["Unrestricted"] as string));
397 public void ToXml_Unrestricted ()
399 NamedPermissionSet ps = new NamedPermissionSet (name, PermissionState.Unrestricted);
400 SecurityElement se = ps.ToXml ();
401 Assert ("Unrestricted.ToString().StartsWith", ps.ToString().StartsWith ("<PermissionSet"));
402 AssertEquals ("Unrestricted.class", "System.Security.NamedPermissionSet", (se.Attributes ["class"] as string));
403 AssertEquals ("Unrestricted.version", "1", (se.Attributes ["version"] as string));
404 AssertEquals ("Unrestricted.Name", name, (se.Attributes ["Name"] as string));
405 AssertNull ("Unrestricted.Description", (se.Attributes ["Description"] as string));
406 AssertEquals ("Unrestricted.Unrestricted", "true", (se.Attributes ["Unrestricted"] as string));
410 public void Equals ()
412 NamedPermissionSet psn = new NamedPermissionSet (name, PermissionState.None);
413 NamedPermissionSet psu = new NamedPermissionSet (name, PermissionState.Unrestricted);
414 Assert ("psn!=psu", !psn.Equals (psu));
415 Assert ("psu!=psn", !psu.Equals (psn));
416 NamedPermissionSet cpsn = (NamedPermissionSet) psn.Copy ();
417 Assert ("cpsn==psn", cpsn.Equals (psn));
418 Assert ("psn==cpsn", psn.Equals (cpsn));
419 NamedPermissionSet cpsu = (NamedPermissionSet) psu.Copy ();
420 Assert ("cpsu==psu", cpsu.Equals (psu));
421 Assert ("psu==cpsu", psu.Equals (cpsu));
422 cpsn.Description = sentinel;
423 Assert ("cpsn+desc==psn", cpsn.Equals (psn));
424 Assert ("psn==cpsn+desc", psn.Equals (cpsn));
425 cpsn.Description = sentinel;
426 Assert ("cpsu+desc==psu", cpsu.Equals (psu));
427 Assert ("psu==cpsu+desc", psu.Equals (cpsu));
431 public void GetHashCode_ ()
433 NamedPermissionSet psn = new NamedPermissionSet (name, PermissionState.None);
434 int nhc = psn.GetHashCode ();
435 NamedPermissionSet psu = new NamedPermissionSet (name, PermissionState.Unrestricted);
436 int uhc = psu.GetHashCode ();
437 Assert ("GetHashCode-1", nhc != uhc);
438 psn.Description = sentinel;
439 Assert ("GetHashCode-2", psn.GetHashCode () == nhc);
440 psu.Description = sentinel;
441 Assert ("GetHashCode-3", psu.GetHashCode () == uhc);