2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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 {
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                 }
57
58                 [Test]
59                 public void ConstructorName ()
60                 {
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");
69                 }
70
71                 [Test]
72                 public void ConstructorNameReserved ()
73                 {
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");
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (NullReferenceException))]
86                 public void ConstructorNamedPermissionSetNull ()
87                 {
88                         NamedPermissionSet nullps = null;
89                         NamedPermissionSet nps = new NamedPermissionSet (nullps);
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (ArgumentException))]
94                 public void ConstructorNameNullPermissionState ()
95                 {
96                         new NamedPermissionSet (null, PermissionState.None);
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (ArgumentException))]
101                 public void ConstructorNameEmptyPermissionState ()
102                 {
103                         new NamedPermissionSet (String.Empty, PermissionState.None);
104                 }
105
106                 [Test]
107                 public void ConstructorNamePermissionStateNone ()
108                 {
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");
117                 }
118
119                 [Test]
120                 public void ConstructorNamePermissionStateUnrestricted ()
121                 {
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");
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof (ArgumentException))]
134                 public void ConstructorNameNullPermissionSet ()
135                 {
136                         new NamedPermissionSet (null, new PermissionSet (PermissionState.None));
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (ArgumentException))]
141                 public void ConstructorNameEmptyPermissionSet ()
142                 {
143                         new NamedPermissionSet (String.Empty, new PermissionSet (PermissionState.None));
144                 }
145
146                 [Test]
147                 public void ConstructorNamePermissionSetNull ()
148                 {
149                         NamedPermissionSet nps = new NamedPermissionSet ("name", null);
150                         Assert.AreEqual ("name", nps.Name, "Name");
151                         Assert.IsNull (nps.Description, "Description");
152 #if NET_2_0
153                         Assert.IsTrue (!nps.IsUnrestricted (), "IsUnrestricted");
154                         Assert.IsTrue (nps.IsEmpty (), "IsEmpty");
155 #else
156                         Assert.IsTrue (nps.IsUnrestricted (), "IsUnrestricted");
157                         Assert.IsTrue (!nps.IsEmpty (), "IsEmpty");
158 #endif
159                         Assert.IsTrue (!nps.IsReadOnly, "IsReadOnly");
160                         Assert.IsTrue (!nps.IsSynchronized, "IsSynchronized");
161                         Assert.AreEqual (0, nps.Count, "Count");
162                 }
163
164                 [Test]
165                 public void Description () 
166                 {
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");
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                         Assert.AreEqual (sentinel, nps.Name, "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                         Assert.AreEqual (nps.Name, copy.Name, "Name");
210                         Assert.AreEqual (nps.Description, copy.Description, "Description");
211                         Assert.AreEqual (nps.Count, copy.Count, "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                         Assert.AreEqual ("Copy", copy.Name, "Name");
222                         Assert.AreEqual (nps.Description, copy.Description, "Description");
223                         Assert.AreEqual (nps.Count, copy.Count, "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                         Assert.IsNull (nps.Name, "Name");
341                         NamedPermissionSet copy = (NamedPermissionSet) nps.Copy ();
342                         Assert.IsNull (copy.Name, "Copy.Name");
343
344                         copy = nps.Copy ("name");
345                         Assert.AreEqual ("name", copy.Name, "Copy(Name).Name");
346
347                         se = nps.ToXml ();
348                         Assert.IsNull (se.Attribute ("Name"), "Name attribute");
349 #if NET_2_0
350                         Assert.AreEqual (0, nps.GetHashCode (), "GetHashCode");
351                         Assert.IsTrue (nps.Equals (nps), "Equals-self");
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                         Assert.IsNotNull (se, "ToXml()");
361
362                         NamedPermissionSet nps2 = (NamedPermissionSet) nps.Copy ();
363                         nps2.FromXml (se);
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");
368
369                         se.AddAttribute ("Description", sentinel);
370                         nps2.FromXml (se);
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");
374
375                         se.AddAttribute ("Unrestricted", "true");
376                         nps2.FromXml (se);
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");
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.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");
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.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");
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.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");
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.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");
442                 }
443 #endif
444         }
445 }