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 {
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 ()
61 NamedPermissionSet nps = new NamedPermissionSet ("name");
62 Assert.AreEqual ("name", nps.Name, "Name");
63 Assert.IsNull (nps.Description, "Description");
64 Assert.IsTrue (nps.IsUnrestricted (), "IsUnrestricted");
65 Assert.IsTrue (!nps.IsEmpty (), "IsEmpty");
66 Assert.IsTrue (!nps.IsReadOnly, "IsReadOnly");
67 Assert.IsTrue (!nps.IsSynchronized, "IsSynchronized");
68 Assert.AreEqual (0, nps.Count, "Count");
72 public void ConstructorNameReserved ()
74 NamedPermissionSet nps = new NamedPermissionSet ("FullTrust");
75 Assert.AreEqual ("FullTrust", nps.Name, "Name");
76 Assert.IsNull (nps.Description, "Description");
77 Assert.IsTrue (nps.IsUnrestricted (), "IsUnrestricted");
78 Assert.IsTrue (!nps.IsEmpty (), "IsEmpty");
79 Assert.IsTrue (!nps.IsReadOnly, "IsReadOnly");
80 Assert.IsTrue (!nps.IsSynchronized, "IsSynchronized");
81 Assert.AreEqual (0, nps.Count, "Count");
85 [ExpectedException (typeof (NullReferenceException))]
86 public void ConstructorNamedPermissionSetNull ()
88 NamedPermissionSet nullps = null;
89 NamedPermissionSet nps = new NamedPermissionSet (nullps);
93 [ExpectedException (typeof (ArgumentException))]
94 public void ConstructorNameNullPermissionState ()
96 new NamedPermissionSet (null, PermissionState.None);
100 [ExpectedException (typeof (ArgumentException))]
101 public void ConstructorNameEmptyPermissionState ()
103 new NamedPermissionSet (String.Empty, PermissionState.None);
107 public void ConstructorNamePermissionStateNone ()
109 NamedPermissionSet nps = new NamedPermissionSet ("name", PermissionState.None);
110 Assert.AreEqual ("name", nps.Name, "Name");
111 Assert.IsNull (nps.Description, "Description");
112 Assert.IsTrue (!nps.IsUnrestricted (), "IsUnrestricted");
113 Assert.IsTrue (nps.IsEmpty (), "IsEmpty");
114 Assert.IsTrue (!nps.IsReadOnly, "IsReadOnly");
115 Assert.IsTrue (!nps.IsSynchronized, "IsSynchronized");
116 Assert.AreEqual (0, nps.Count, "Count");
120 public void ConstructorNamePermissionStateUnrestricted ()
122 NamedPermissionSet nps = new NamedPermissionSet ("name", PermissionState.Unrestricted);
123 Assert.AreEqual ("name", nps.Name, "Name");
124 Assert.IsNull (nps.Description, "Description");
125 Assert.IsTrue (nps.IsUnrestricted (), "IsUnrestricted");
126 Assert.IsTrue (!nps.IsEmpty (), "IsEmpty");
127 Assert.IsTrue (!nps.IsReadOnly, "IsReadOnly");
128 Assert.IsTrue (!nps.IsSynchronized, "IsSynchronized");
129 Assert.AreEqual (0, nps.Count, "Count");
133 [ExpectedException (typeof (ArgumentException))]
134 public void ConstructorNameNullPermissionSet ()
136 new NamedPermissionSet (null, new PermissionSet (PermissionState.None));
140 [ExpectedException (typeof (ArgumentException))]
141 public void ConstructorNameEmptyPermissionSet ()
143 new NamedPermissionSet (String.Empty, new PermissionSet (PermissionState.None));
147 public void ConstructorNamePermissionSetNull ()
149 NamedPermissionSet nps = new NamedPermissionSet ("name", null);
150 Assert.AreEqual ("name", nps.Name, "Name");
151 Assert.IsNull (nps.Description, "Description");
153 Assert.IsTrue (!nps.IsUnrestricted (), "IsUnrestricted");
154 Assert.IsTrue (nps.IsEmpty (), "IsEmpty");
156 Assert.IsTrue (nps.IsUnrestricted (), "IsUnrestricted");
157 Assert.IsTrue (!nps.IsEmpty (), "IsEmpty");
159 Assert.IsTrue (!nps.IsReadOnly, "IsReadOnly");
160 Assert.IsTrue (!nps.IsSynchronized, "IsSynchronized");
161 Assert.AreEqual (0, nps.Count, "Count");
165 public void Description ()
167 NamedPermissionSet nps = new NamedPermissionSet (name);
168 // null by default (not empty)
169 Assert.IsNull (nps.Description, "Description");
170 // is null-able (without exception)
171 nps.Description = null;
172 Assert.IsNull (nps.Description, "Description(null)");
173 nps.Description = sentinel;
174 Assert.AreEqual (sentinel, nps.Description, "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 Assert.AreEqual (sentinel, nps.Name, "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 Assert.AreEqual (nps.Name, copy.Name, "Name");
210 Assert.AreEqual (nps.Description, copy.Description, "Description");
211 Assert.AreEqual (nps.Count, copy.Count, "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 Assert.AreEqual ("Copy", copy.Name, "Name");
222 Assert.AreEqual (nps.Description, copy.Description, "Description");
223 Assert.AreEqual (nps.Count, copy.Count, "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 Assert.IsNull (nps.Name, "Name");
341 NamedPermissionSet copy = (NamedPermissionSet) nps.Copy ();
342 Assert.IsNull (copy.Name, "Copy.Name");
344 copy = nps.Copy ("name");
345 Assert.AreEqual ("name", copy.Name, "Copy(Name).Name");
348 Assert.IsNull (se.Attribute ("Name"), "Name attribute");
350 Assert.AreEqual (0, nps.GetHashCode (), "GetHashCode");
351 Assert.IsTrue (nps.Equals (nps), "Equals-self");
356 public void FromXml ()
358 NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
359 SecurityElement se = nps.ToXml ();
360 Assert.IsNotNull (se, "ToXml()");
362 NamedPermissionSet nps2 = (NamedPermissionSet) nps.Copy ();
364 Assert.AreEqual (name, nps2.Name, "FromXml-Copy.Name");
365 // strangely it's empty when converted from XML (but null when created)
366 Assert.AreEqual ("", nps2.Description, "FromXml-Copy.Description");
367 Assert.IsTrue (!nps2.IsUnrestricted () , "FromXml-Copy.IsUnrestricted");
369 se.AddAttribute ("Description", sentinel);
371 Assert.AreEqual (name, nps2.Name, "FromXml-Add1.Name");
372 Assert.AreEqual (sentinel, nps2.Description, "FromXml-Add1.Description");
373 Assert.IsTrue (!nps2.IsUnrestricted () , "FromXml-Add1.IsUnrestricted");
375 se.AddAttribute ("Unrestricted", "true");
377 Assert.AreEqual (name, nps2.Name, "FromXml-Add2.Name");
378 Assert.AreEqual (sentinel, nps2.Description, "FromXml-Add2.Description");
379 Assert.IsTrue (nps2.IsUnrestricted () , "FromXml-Add2.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.IsTrue (ps.ToString().StartsWith ("<PermissionSet"), "None.ToString().StartsWith");
389 Assert.AreEqual ("System.Security.NamedPermissionSet", (se.Attributes ["class"] as string), "None.class");
390 Assert.AreEqual ("1", (se.Attributes ["version"] as string), "None.version");
391 Assert.AreEqual (name, (se.Attributes ["Name"] as string), "None.Name");
392 Assert.AreEqual (sentinel, (se.Attributes ["Description"] as string), "None.Description");
393 Assert.IsNull ((se.Attributes ["Unrestricted"] as string), "None.Unrestricted");
397 public void ToXml_Unrestricted ()
399 NamedPermissionSet ps = new NamedPermissionSet (name, PermissionState.Unrestricted);
400 SecurityElement se = ps.ToXml ();
401 Assert.IsTrue (ps.ToString().StartsWith ("<PermissionSet"), "Unrestricted.ToString().StartsWith");
402 Assert.AreEqual ("System.Security.NamedPermissionSet", (se.Attributes ["class"] as string), "Unrestricted.class");
403 Assert.AreEqual ("1", (se.Attributes ["version"] as string), "Unrestricted.version");
404 Assert.AreEqual (name, (se.Attributes ["Name"] as string), "Unrestricted.Name");
405 Assert.IsNull ((se.Attributes ["Description"] as string), "Unrestricted.Description");
406 Assert.AreEqual ("true", (se.Attributes ["Unrestricted"] as string), "Unrestricted.Unrestricted");
410 public void Equals ()
412 NamedPermissionSet psn = new NamedPermissionSet (name, PermissionState.None);
413 NamedPermissionSet psu = new NamedPermissionSet (name, PermissionState.Unrestricted);
414 Assert.IsTrue (!psn.Equals (psu), "psn!=psu");
415 Assert.IsTrue (!psu.Equals (psn), "psu!=psn");
416 NamedPermissionSet cpsn = (NamedPermissionSet) psn.Copy ();
417 Assert.IsTrue (cpsn.Equals (psn), "cpsn==psn");
418 Assert.IsTrue (psn.Equals (cpsn), "psn==cpsn");
419 NamedPermissionSet cpsu = (NamedPermissionSet) psu.Copy ();
420 Assert.IsTrue (cpsu.Equals (psu), "cpsu==psu");
421 Assert.IsTrue (psu.Equals (cpsu), "psu==cpsu");
422 cpsn.Description = sentinel;
423 Assert.IsTrue (cpsn.Equals (psn), "cpsn+desc==psn");
424 Assert.IsTrue (psn.Equals (cpsn), "psn==cpsn+desc");
425 cpsn.Description = sentinel;
426 Assert.IsTrue (cpsu.Equals (psu), "cpsu+desc==psu");
427 Assert.IsTrue (psu.Equals (cpsu), "psu==cpsu+desc");
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.IsTrue (nhc != uhc, "GetHashCode-1");
438 psn.Description = sentinel;
439 Assert.IsTrue (psn.GetHashCode () == nhc, "GetHashCode-2");
440 psu.Description = sentinel;
441 Assert.IsTrue (psu.GetHashCode () == uhc, "GetHashCode-3");