2005-12-23 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security / NamedPermissionSetTest.cs
1 //
2 // NamedPermissionSetTest.cs - NUnit Test Cases for NamedPermissionSet
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-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 using NUnit.Framework;
31 using System;
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace MonoTests.System.Security {
36
37         [TestFixture]
38         public class NamedPermissionSetTest : Assertion {
39
40                 private static string name = "mono";
41                 private static string sentinel = "go mono!";
42
43                 [Test]
44                 [ExpectedException (typeof (ArgumentException))]
45                 public void ConstructorNameNull () 
46                 {
47                         string s = null; // we don't want to confuse the compiler
48                         NamedPermissionSet nps = new NamedPermissionSet (s);
49                 }
50
51                 [Test]
52                 [ExpectedException (typeof (ArgumentException))]
53                 public void ConstructorNameEmpty () 
54                 {
55                         NamedPermissionSet nps = new NamedPermissionSet ("");
56                 }\r
57 \r
58                 [Test]\r
59                 public void ConstructorName ()\r
60                 {\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
69                 }\r
70 \r
71                 [Test]\r
72                 public void ConstructorNameReserved ()\r
73                 {\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
82                 }\r
83 \r
84                 [Test]\r
85                 [ExpectedException (typeof (NullReferenceException))]\r
86                 public void ConstructorNamedPermissionSetNull ()\r
87                 {\r
88                         NamedPermissionSet nullps = null;\r
89                         NamedPermissionSet nps = new NamedPermissionSet (nullps);\r
90                 }\r
91 \r
92                 [Test]\r
93                 [ExpectedException (typeof (ArgumentException))]\r
94                 public void ConstructorNameNullPermissionState ()\r
95                 {\r
96                         new NamedPermissionSet (null, PermissionState.None);\r
97                 }\r
98 \r
99                 [Test]\r
100                 [ExpectedException (typeof (ArgumentException))]\r
101                 public void ConstructorNameEmptyPermissionState ()\r
102                 {\r
103                         new NamedPermissionSet (String.Empty, PermissionState.None);\r
104                 }
105 \r
106                 [Test]\r
107                 public void ConstructorNamePermissionStateNone ()\r
108                 {\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
117                 }\r
118 \r
119                 [Test]\r
120                 public void ConstructorNamePermissionStateUnrestricted ()\r
121                 {\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
130                 }\r
131 \r
132                 [Test]\r
133                 [ExpectedException (typeof (ArgumentException))]\r
134                 public void ConstructorNameNullPermissionSet ()\r
135                 {\r
136                         new NamedPermissionSet (null, new PermissionSet (PermissionState.None));\r
137                 }\r
138 \r
139                 [Test]\r
140                 [ExpectedException (typeof (ArgumentException))]\r
141                 public void ConstructorNameEmptyPermissionSet ()\r
142                 {\r
143                         new NamedPermissionSet (String.Empty, new PermissionSet (PermissionState.None));\r
144                 }
145 \r
146                 [Test]\r
147                 public void ConstructorNamePermissionSetNull ()\r
148                 {\r
149                         NamedPermissionSet nps = new NamedPermissionSet ("name", null);\r
150                         AssertEquals ("Name", "name", nps.Name);\r
151                         AssertNull ("Description", nps.Description);\r
152 #if NET_2_0\r
153                         Assert ("IsUnrestricted", !nps.IsUnrestricted ());\r
154                         Assert ("IsEmpty", nps.IsEmpty ());\r
155 #else\r
156                         Assert ("IsUnrestricted", nps.IsUnrestricted ());\r
157                         Assert ("IsEmpty", !nps.IsEmpty ());\r
158 #endif\r
159                         Assert ("IsReadOnly", !nps.IsReadOnly);\r
160                         Assert ("IsSynchronized", !nps.IsSynchronized);\r
161                         AssertEquals ("Count", 0, nps.Count);\r
162                 }\r
163 \r
164                 [Test]
165                 public void Description () 
166                 {
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);
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (ArgumentException))]
179                 public void NameNull () 
180                 {
181                         NamedPermissionSet nps = new NamedPermissionSet (name);
182                         nps.Name = null;
183                         // strangely this isn't a ArgumentNullException (but so says the doc)
184                 }
185
186                 [Test]
187                 [ExpectedException (typeof (ArgumentException))]
188                 public void NameEmpty () 
189                 {
190                         NamedPermissionSet nps = new NamedPermissionSet (name);
191                         nps.Name = "";
192                 }
193
194                 [Test]
195                 public void Name () 
196                 {
197                         NamedPermissionSet nps = new NamedPermissionSet (name);
198                         nps.Name = sentinel;
199                         AssertEquals ("Name", sentinel, nps.Name);
200                 }
201
202                 [Test]
203                 public void Copy ()
204                 {
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);
212                 }
213
214                 [Test]
215                 public void Copy_Name ()
216                 {
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);
224                 }
225
226                 [Test]
227                 [ExpectedException (typeof (ArgumentException))]
228                 public void Copy_Name_Null ()
229                 {
230                         NamedPermissionSet nps = new NamedPermissionSet (name);
231                         NamedPermissionSet copy = (NamedPermissionSet)nps.Copy (null);
232                 }
233
234                 [Test]
235                 [ExpectedException (typeof (ArgumentException))]
236                 public void Copy_Name_Empty ()
237                 {
238                         NamedPermissionSet nps = new NamedPermissionSet (name);
239                         NamedPermissionSet copy = (NamedPermissionSet)nps.Copy (String.Empty);
240                 }
241
242                 [Test]
243                 [ExpectedException (typeof (ArgumentNullException))]
244                 public void FromXml_Null () 
245                 {
246                         NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
247                         nps.FromXml (null);
248                 }
249
250                 [Test]
251                 [ExpectedException (typeof (ArgumentException))]
252                 public void FromXml_InvalidPermission () 
253                 {
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"));
261                         nps.FromXml (se2);
262                 }
263
264                 [Test]
265                 [ExpectedException (typeof (ArgumentException))]
266                 public void FromXml_WrongTagCase ()
267                 {
268                         NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
269                         SecurityElement se = nps.ToXml ();
270                         se.Tag = se.Tag.ToUpper (); // instead of PermissionSet
271                         nps.FromXml (se);
272                 }
273
274                 [Test]
275                 public void FromXml_WrongClass ()
276                 {
277                         NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
278                         SecurityElement se = nps.ToXml ();
279
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"));
284                         nps.FromXml (w);
285                         // doesn't care of the class name at that stage
286                         // anyway the class has already be created so...
287                 }
288
289                 [Test]
290                 public void FromXml_NoClass ()
291                 {
292                         NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
293                         SecurityElement se = nps.ToXml ();
294
295                         SecurityElement w = new SecurityElement (se.Tag);
296                         w.AddAttribute ("version", se.Attribute ("version"));
297                         nps.FromXml (w);
298                         // doesn't even care of the class attribute presence
299                 }
300
301                 [Test]
302                 // [ExpectedException (typeof (ArgumentException))]
303                 public void FromXml_WrongVersion () 
304                 {
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"));
312                         nps.FromXml (se2);
313                         // wow - here we accept a version 2 !!!
314                 }
315
316                 [Test]
317                 public void FromXml_NoVersion ()
318                 {
319                         NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
320                         SecurityElement se = nps.ToXml ();
321
322                         SecurityElement w = new SecurityElement (se.Tag);
323                         w.AddAttribute ("class", se.Attribute ("class"));
324                         w.AddAttribute ("Name", se.Attribute ("Name"));
325                         nps.FromXml (w);
326                 }
327
328                 [Test]
329                 public void FromXml_NoName ()
330                 {
331                         NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
332                         SecurityElement se = nps.ToXml ();
333
334                         SecurityElement w = new SecurityElement (se.Tag);
335                         w.AddAttribute ("class", se.Attribute ("class"));
336                         w.AddAttribute ("version", "1");
337                         nps.FromXml (w);
338
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);
343
344                         copy = nps.Copy ("name");
345                         AssertEquals ("Copy(Name).Name", "name", copy.Name);
346
347                         se = nps.ToXml ();
348                         AssertNull ("Name attribute", se.Attribute ("Name"));
349 #if NET_2_0
350                         AssertEquals ("GetHashCode", 0, nps.GetHashCode ());
351                         Assert ("Equals-self", nps.Equals (nps));
352 #endif
353                 }
354
355                 [Test]
356                 public void FromXml () 
357                 {
358                         NamedPermissionSet nps = new NamedPermissionSet (name, PermissionState.None);
359                         SecurityElement se = nps.ToXml ();
360                         AssertNotNull ("ToXml()", se);
361
362                         NamedPermissionSet nps2 = (NamedPermissionSet) nps.Copy ();
363                         nps2.FromXml (se);
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 ()); 
368
369                         se.AddAttribute ("Description", sentinel);
370                         nps2.FromXml (se);
371                         AssertEquals ("FromXml-Add1.Name", name, nps2.Name);
372                         AssertEquals ("FromXml-Add1.Description", sentinel, nps2.Description);
373                         Assert ("FromXml-Add1.IsUnrestricted", !nps2.IsUnrestricted ()); 
374
375                         se.AddAttribute ("Unrestricted", "true");
376                         nps2.FromXml (se);
377                         AssertEquals ("FromXml-Add2.Name", name, nps2.Name);
378                         AssertEquals ("FromXml-Add2.Description", sentinel, nps2.Description);
379                         Assert ("FromXml-Add2.IsUnrestricted", nps2.IsUnrestricted ()); 
380                 }
381
382                 [Test]
383                 public void ToXml_None () 
384                 {
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));
394                 }
395
396                 [Test]
397                 public void ToXml_Unrestricted () 
398                 {
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));
407                 }
408 #if NET_2_0
409                 [Test]
410                 public void Equals () 
411                 {
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));
428                 }
429
430                 [Test]
431                 public void GetHashCode_ () 
432                 {
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);
442                 }
443 #endif
444         }
445 }