* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / Test / System.Security.Permissions / StorePermissionTest.cs
1 //
2 // StorePermissionTest.cs - NUnit Test Cases for StorePermissionTest
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using NUnit.Framework;
32 using System;
33 using System.Security;
34 using System.Security.Permissions;
35
36 namespace MonoTests.System.Security.Permissions {
37
38         [TestFixture]
39         public class StorePermissionTest {
40
41                 [Test]
42                 public void PermissionState_None ()
43                 {
44                         PermissionState ps = PermissionState.None;
45                         StorePermission sp = new StorePermission (ps);
46                         Assert.AreEqual (StorePermissionFlags.NoFlags, sp.Flags, "Flags");
47                         Assert.IsFalse (sp.IsUnrestricted (), "IsUnrestricted");
48
49                         SecurityElement se = sp.ToXml ();
50                         // only class and version are present
51                         Assert.AreEqual ("NoFlags", se.Attribute ("Flags"), "Xml-Flags");
52                         Assert.IsNull (se.Children, "Xml-Children");
53
54                         StorePermission copy = (StorePermission) sp.Copy ();
55                         Assert.IsNull (copy, "Copy");
56                 }
57
58                 [Test]
59                 [Ignore ("2.0 final bug reported as FDBK40928")]
60                 public void PermissionState_None_Copy ()
61                 {
62                         // both will return null under 2.0 final
63                         // StorePermission sp1 = new StorePermission (PermissionState.None).Copy();
64                         // StorePermission sp2 = new StorePermission (StorePermissionFlags.NoFlags).Copy ();
65                         StorePermission sp = new StorePermission (PermissionState.None);
66
67                         StorePermission copy = (StorePermission) sp.Copy ();
68                         Assert.IsFalse (Object.ReferenceEquals (sp, copy), "ReferenceEquals");
69                         Assert.AreEqual (sp.Flags, copy.Flags, "Copy Flags");
70                         Assert.AreEqual (sp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
71                 }
72
73                 [Test]
74                 public void PermissionState_Unrestricted ()
75                 {
76                         PermissionState ps = PermissionState.Unrestricted;
77                         StorePermission sp = new StorePermission (ps);
78                         Assert.AreEqual (StorePermissionFlags.AllFlags, sp.Flags, "Flags");
79                         Assert.IsTrue (sp.IsUnrestricted (), "IsUnrestricted");
80
81                         SecurityElement se = sp.ToXml ();
82                         Assert.IsNotNull (se.Attribute ("Unrestricted"), "Xml-Unrestricted");
83                         Assert.IsNull (se.Attribute ("Level"), "Xml-Flags");
84                         Assert.IsNull (se.Children, "Xml-Children");
85
86                         StorePermission copy = (StorePermission) sp.Copy ();
87                         Assert.IsFalse (Object.ReferenceEquals (sp, copy), "ReferenceEquals");
88                         Assert.AreEqual (sp.Flags, copy.Flags, "Copy Flags");
89                         Assert.AreEqual (sp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (ArgumentException))]
94                 public void PermissionState_Bad ()
95                 {
96                         PermissionState ps = (PermissionState) Int32.MinValue;
97                         StorePermission sp = new StorePermission (ps);
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentException))]
102                 public void StorePermissionFlags_Bad ()
103                 {
104                         StorePermissionFlags spf = (StorePermissionFlags) Int32.MinValue;
105                         StorePermission sp = new StorePermission (spf);
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentException))]
110                 public void StorePermissionFlags_BadEight ()
111                 {
112                         StorePermissionFlags spf = (StorePermissionFlags) 8; // unassigned
113                         StorePermission sp = new StorePermission (spf);
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentException))]
118                 public void Flags_StorePermissionFlags_Bad ()
119                 {
120                         StorePermission sp = new StorePermission (PermissionState.None);
121                         sp.Flags = (StorePermissionFlags) Int32.MinValue;
122                 }
123
124                 [Test]
125                 public void Copy ()
126                 {
127                         StorePermission sp = new StorePermission (PermissionState.None);
128                         // i==0 would return null for MS
129                         for (int i=1; i < (int) StorePermissionFlags.AllFlags; i++) {
130                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
131                                 if ((i & 8) == 8)
132                                         continue;
133                                 sp.Flags = (StorePermissionFlags) i;
134                                 StorePermission copy = (StorePermission) sp.Copy ();
135                                 Assert.AreEqual (i, (int) copy.Flags, sp.Flags.ToString ());
136                         }
137                 }
138
139                 [Test]
140                 public void Intersect_Null ()
141                 {
142                         StorePermission sp = new StorePermission (PermissionState.None);
143                         // No intersection with null
144                         for (int i = 0; i < (int) StorePermissionFlags.AllFlags; i++) {
145                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
146                                 if ((i & 8) == 8)
147                                         continue;
148                                 sp.Flags = (StorePermissionFlags) i;
149                                 Assert.IsNull (sp.Intersect (null), sp.Flags.ToString ());
150                         }
151                 }
152
153                 [Test]
154                 public void Intersect_None ()
155                 {
156                         StorePermission sp1 = new StorePermission (PermissionState.None);
157                         StorePermission sp2 = new StorePermission (PermissionState.None);
158                         for (int i = 0; i < (int) StorePermissionFlags.AllFlags; i++) {
159                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
160                                 if ((i & 8) == 8)
161                                         continue;
162                                 sp1.Flags = (StorePermissionFlags) i;
163                                 // 1. Intersect None with ppl
164                                 StorePermission result = (StorePermission) sp1.Intersect (sp2);
165                                 Assert.IsNull (result, "None N " + sp1.Flags.ToString ());
166                                 // 2. Intersect ppl with None
167                                 result = (StorePermission) sp2.Intersect (sp1);
168                                 Assert.IsNull (result, sp1.Flags.ToString () + "N None");
169                         }
170                 }
171
172                 [Test]
173                 public void Intersect_Self ()
174                 {
175                         StorePermission sp = new StorePermission (PermissionState.None);
176                         sp.Flags = StorePermissionFlags.NoFlags; // 0
177                         StorePermission result = (StorePermission) sp.Intersect (sp);
178                         Assert.IsNull (result, "NoFlags");
179                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
180                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
181                                 if ((i & 8) == 8)
182                                         continue;
183                                 sp.Flags = (StorePermissionFlags) i;
184                                 result = (StorePermission) sp.Intersect (sp);
185                                 Assert.AreEqual (sp.Flags, result.Flags, sp.Flags.ToString ());
186                         }
187                 }
188
189                 [Test]
190                 public void Intersect_Unrestricted ()
191                 {
192                         // Intersection with unrestricted == Copy
193                         // a. source (this) is unrestricted
194                         StorePermission sp1 = new StorePermission (PermissionState.Unrestricted);
195                         StorePermission sp2 = new StorePermission (PermissionState.None);
196                         StorePermission result = (StorePermission) sp1.Intersect (sp2);
197                         Assert.IsNull (result, "target NoFlags");
198                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
199                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
200                                 if ((i & 8) == 8)
201                                         continue;
202                                 sp2.Flags = (StorePermissionFlags) i;
203                                 result = (StorePermission) sp1.Intersect (sp2);
204                                 Assert.AreEqual (sp2.Flags, result.Flags, "target " + sp2.Flags.ToString ());
205                         }
206                         // b. destination (target) is unrestricted
207                         sp2.Flags = StorePermissionFlags.NoFlags;
208                         result = (StorePermission) sp2.Intersect (sp1);
209                         Assert.IsNull (result, "target NoFlags");
210                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
211                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
212                                 if ((i & 8) == 8)
213                                         continue;
214                                 sp2.Flags = (StorePermissionFlags) i;
215                                 result = (StorePermission) sp2.Intersect (sp1);
216                                 Assert.AreEqual (sp2.Flags, result.Flags, "source " + sp2.Flags.ToString ());
217                         }
218                 }
219
220                 [Test]
221                 public void IsSubset_Null ()
222                 {
223                         StorePermission sp = new StorePermission (PermissionState.None);
224                         Assert.IsTrue (sp.IsSubsetOf (null), "NoFlags"); // 0
225                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
226                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
227                                 if ((i & 8) == 8)
228                                         continue;
229                                 sp.Flags = (StorePermissionFlags) i;
230                                 Assert.IsFalse (sp.IsSubsetOf (null), sp.Flags.ToString ());
231                         }
232                 }
233
234                 [Test]
235                 public void IsSubset_None ()
236                 {
237                         // IsSubset with none
238                         // a. source (this) is none -> target is never a subset
239                         StorePermission sp1 = new StorePermission (PermissionState.None);
240                         StorePermission sp2 = new StorePermission (PermissionState.None);
241                         for (int i = 0; i < (int) StorePermissionFlags.AllFlags; i++) {
242                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
243                                 if ((i & 8) == 8)
244                                         continue;
245                                 sp2.Flags = (StorePermissionFlags) i;
246                                 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + sp2.Flags.ToString ());
247                         }
248                         // b. destination (target) is none -> target is always a subset
249                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
250                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
251                                 if ((i & 8) == 8)
252                                         continue;
253                                 sp2.Flags = (StorePermissionFlags) i;
254                                 Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + sp2.Flags.ToString ());
255                         }
256                         // exception of NoFlags
257                         sp2.Flags = StorePermissionFlags.NoFlags;
258                         Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoFlags");
259                 }
260
261                 [Test]
262                 public void IsSubset_Self ()
263                 {
264                         StorePermission sp = new StorePermission (PermissionState.None);
265                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
266                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
267                                 if ((i & 8) == 8)
268                                         continue;
269                                 sp.Flags = (StorePermissionFlags) i;
270                                 Assert.IsTrue (sp.IsSubsetOf (sp), sp.Flags.ToString ());
271                         }
272                 }
273
274                 [Test]
275                 public void IsSubset_Unrestricted ()
276                 {
277                         // IsSubset with unrestricted
278                         // a. source (this) is unrestricted -> target is never a subset
279                         StorePermission sp1 = new StorePermission (PermissionState.Unrestricted);
280                         StorePermission sp2 = new StorePermission (PermissionState.None);
281                         for (int i = 0; i < (int) StorePermissionFlags.AllFlags - 1; i++) {
282                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
283                                 if ((i & 8) == 8)
284                                         continue;
285                                 sp2.Flags = (StorePermissionFlags) i;
286                                 Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + sp2.Flags.ToString ());
287                         }
288                         // exception of AllLevel
289                         sp2.Flags = StorePermissionFlags.AllFlags;
290                         Assert.IsTrue (sp1.IsSubsetOf (sp2), "target AllLevel");
291                         // b. destination (target) is unrestricted -> target is always a subset
292                         for (int i = 0; i < (int) StorePermissionFlags.AllFlags; i++) {
293                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
294                                 if ((i & 8) == 8)
295                                         continue;
296                                 sp2.Flags = (StorePermissionFlags) i;
297                                 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + sp2.Flags.ToString ());
298                         }
299                 }
300
301                 [Test]
302                 public void Union_Null ()
303                 {
304                         StorePermission sp = new StorePermission (PermissionState.None);
305                         // Union with null is a simple copy
306                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
307                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
308                                 if ((i & 8) == 8)
309                                         continue;
310                                 sp.Flags = (StorePermissionFlags) i;
311                                 StorePermission union = (StorePermission) sp.Union (null);
312                                 Assert.AreEqual (sp.Flags, union.Flags, sp.Flags.ToString ());
313                         }
314                         // except fot NoFlags (which returns null)
315                         sp.Flags = StorePermissionFlags.NoFlags;
316                         Assert.IsNull (sp.Union (null), "NoFlags");
317                 }
318
319                 [Test]
320                 public void Union_None ()
321                 {
322                         // Union with none is same
323                         StorePermission sp1 = new StorePermission (PermissionState.None);
324                         StorePermission sp2 = new StorePermission (PermissionState.None);
325
326                         StorePermission union = (StorePermission) sp1.Union (sp1);
327                         Assert.IsNull (union, "NoFlags");
328
329                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
330                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
331                                 if ((i & 8) == 8)
332                                         continue;
333                                 sp2.Flags = (StorePermissionFlags) i;
334
335                                 union = (StorePermission) sp1.Union (sp2);
336                                 Assert.IsFalse (union.IsUnrestricted (), "target.Unrestricted " + sp2.Flags.ToString ());
337                                 Assert.AreEqual (sp2.Flags, union.Flags, "target.Level " + sp2.Flags.ToString ());
338
339                                 union = (StorePermission) sp2.Union (sp1);
340                                 Assert.IsFalse (union.IsUnrestricted (), "source.Unrestricted " + sp2.Flags.ToString ());
341                                 Assert.AreEqual (sp2.Flags, union.Flags, "source.Level " + sp2.Flags.ToString ());
342                         }
343
344                         sp2.Flags = StorePermissionFlags.AllFlags;
345                         union = (StorePermission) sp1.Union (sp2);
346                         Assert.IsTrue (union.IsUnrestricted (), "target.Unrestricted Unrestricted");
347                         Assert.AreEqual (StorePermissionFlags.AllFlags, union.Flags, "target.Level Unrestricted");
348
349                         union = (StorePermission) sp2.Union (sp1);
350                         Assert.IsTrue (union.IsUnrestricted (), "source.Unrestricted Unrestricted");
351                         Assert.AreEqual (StorePermissionFlags.AllFlags, union.Flags, "source.Level Unrestricted");
352                 }
353
354                 [Test]
355                 public void Union_Self ()
356                 {
357                         StorePermission sp = new StorePermission (PermissionState.None);
358                         StorePermission union = (StorePermission) sp.Union (sp);
359                         Assert.IsNull (union, "NoFlags");
360                         for (int i = 1; i < (int) StorePermissionFlags.AllFlags; i++) {
361                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
362                                 if ((i & 8) == 8)
363                                         continue;
364                                 sp.Flags = (StorePermissionFlags) i;
365                                 union = (StorePermission) sp.Union (sp);
366                                 Assert.AreEqual (sp.Flags, union.Flags, sp.Flags.ToString ());
367                         }
368                 }
369
370                 [Test]
371                 public void Union_Unrestricted ()
372                 {
373                         // Union with unrestricted is unrestricted
374                         StorePermission sp1 = new StorePermission (PermissionState.Unrestricted);
375                         StorePermission sp2 = new StorePermission (PermissionState.None);
376                         // a. source (this) is unrestricted
377                         for (int i = 0; i < (int) StorePermissionFlags.AllFlags; i++) {
378                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
379                                 if ((i & 8) == 8)
380                                         continue;
381                                 sp2.Flags = (StorePermissionFlags) i;
382                                 StorePermission union = (StorePermission) sp1.Union (sp2);
383                                 Assert.IsTrue (union.IsUnrestricted (), "target " + sp2.Flags.ToString ());
384                         }
385                         // b. destination (target) is unrestricted
386                         for (int i = 0; i < (int) StorePermissionFlags.AllFlags; i++) {
387                                 // 8 isn't a valid value (so we exclude it from the rest of the loop)
388                                 if ((i & 8) == 8)
389                                         continue;
390                                 sp2.Flags = (StorePermissionFlags) i;
391                                 StorePermission union = (StorePermission) sp2.Union (sp1);
392                                 Assert.IsTrue (union.IsUnrestricted (), "source " + sp2.Flags.ToString ());
393                         }
394                 }
395
396                 [Test]
397                 [ExpectedException (typeof (ArgumentNullException))]
398                 public void FromXml_Null ()
399                 {
400                         StorePermission sp = new StorePermission (PermissionState.None);
401                         sp.FromXml (null);
402                 }
403
404                 [Test]
405                 public void FromXml_WrongTag ()
406                 {
407                         StorePermission sp = new StorePermission (PermissionState.None);
408                         SecurityElement se = sp.ToXml ();
409                         se.Tag = "IMono";
410                         sp.FromXml (se);
411                         // note: normally IPermission classes (in corlib) DO care about the
412                         // IPermission tag
413                 }
414
415                 [Test]
416                 public void FromXml_WrongTagCase ()
417                 {
418                         StorePermission sp = new StorePermission (PermissionState.None);
419                         SecurityElement se = sp.ToXml ();
420                         se.Tag = "IPERMISSION"; // instead of IPermission
421                         sp.FromXml (se);
422                         // note: normally IPermission classes (in corlib) DO care about the
423                         // IPermission tag
424                 }
425
426                 [Test]
427                 public void FromXml_WrongClass ()
428                 {
429                         StorePermission sp = new StorePermission (PermissionState.None);
430                         SecurityElement se = sp.ToXml ();
431
432                         SecurityElement w = new SecurityElement (se.Tag);
433                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
434                         w.AddAttribute ("version", se.Attribute ("version"));
435                         sp.FromXml (w);
436                         // doesn't care of the class name at that stage
437                         // anyway the class has already be created so...
438                 }
439
440                 [Test]
441                 [ExpectedException (typeof (ArgumentException))]
442                 public void FromXml_NoClass ()
443                 {
444                         StorePermission sp = new StorePermission (PermissionState.None);
445                         SecurityElement se = sp.ToXml ();
446
447                         SecurityElement w = new SecurityElement (se.Tag);
448                         w.AddAttribute ("version", se.Attribute ("version"));
449                         sp.FromXml (w);
450                         // note: normally IPermission classes (in corlib) DO NOT care about
451                         // attribute "class" name presence in the XML
452                 }
453
454                 [Test]
455                 [ExpectedException (typeof (ArgumentException))]
456                 public void FromXml_WrongVersion ()
457                 {
458                         StorePermission sp = new StorePermission (PermissionState.None);
459                         SecurityElement se = sp.ToXml ();
460                         se.Attributes.Remove ("version");
461                         se.Attributes.Add ("version", "2");
462                         sp.FromXml (se);
463                 }
464
465                 [Test]
466                 public void FromXml_NoVersion ()
467                 {
468                         StorePermission sp = new StorePermission (PermissionState.None);
469                         SecurityElement se = sp.ToXml ();
470
471                         SecurityElement w = new SecurityElement (se.Tag);
472                         w.AddAttribute ("class", se.Attribute ("class"));
473                         sp.FromXml (w);
474                         // version is optional (in this case)
475                 }
476         }
477 }
478
479 #endif