2 // StorePermissionTest.cs - NUnit Test Cases for StorePermissionTest
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
33 using System.Security;
34 using System.Security.Permissions;
36 namespace MonoTests.System.Security.Permissions {
39 public class StorePermissionTest {
42 public void PermissionState_None ()
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");
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");
54 StorePermission copy = (StorePermission) sp.Copy ();
55 Assert.IsNull (copy, "Copy");
59 [Ignore ("2.0 final bug reported as FDBK40928")]
60 public void PermissionState_None_Copy ()
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);
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 ()");
74 public void PermissionState_Unrestricted ()
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");
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");
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 ()");
93 [ExpectedException (typeof (ArgumentException))]
94 public void PermissionState_Bad ()
96 PermissionState ps = (PermissionState) Int32.MinValue;
97 StorePermission sp = new StorePermission (ps);
101 [ExpectedException (typeof (ArgumentException))]
102 public void StorePermissionFlags_Bad ()
104 StorePermissionFlags spf = (StorePermissionFlags) Int32.MinValue;
105 StorePermission sp = new StorePermission (spf);
109 [ExpectedException (typeof (ArgumentException))]
110 public void StorePermissionFlags_BadEight ()
112 StorePermissionFlags spf = (StorePermissionFlags) 8; // unassigned
113 StorePermission sp = new StorePermission (spf);
117 [ExpectedException (typeof (ArgumentException))]
118 public void Flags_StorePermissionFlags_Bad ()
120 StorePermission sp = new StorePermission (PermissionState.None);
121 sp.Flags = (StorePermissionFlags) Int32.MinValue;
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)
133 sp.Flags = (StorePermissionFlags) i;
134 StorePermission copy = (StorePermission) sp.Copy ();
135 Assert.AreEqual (i, (int) copy.Flags, sp.Flags.ToString ());
140 public void Intersect_Null ()
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)
148 sp.Flags = (StorePermissionFlags) i;
149 Assert.IsNull (sp.Intersect (null), sp.Flags.ToString ());
154 public void Intersect_None ()
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)
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");
173 public void Intersect_Self ()
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)
183 sp.Flags = (StorePermissionFlags) i;
184 result = (StorePermission) sp.Intersect (sp);
185 Assert.AreEqual (sp.Flags, result.Flags, sp.Flags.ToString ());
190 public void Intersect_Unrestricted ()
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)
202 sp2.Flags = (StorePermissionFlags) i;
203 result = (StorePermission) sp1.Intersect (sp2);
204 Assert.AreEqual (sp2.Flags, result.Flags, "target " + sp2.Flags.ToString ());
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)
214 sp2.Flags = (StorePermissionFlags) i;
215 result = (StorePermission) sp2.Intersect (sp1);
216 Assert.AreEqual (sp2.Flags, result.Flags, "source " + sp2.Flags.ToString ());
221 public void IsSubset_Null ()
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)
229 sp.Flags = (StorePermissionFlags) i;
230 Assert.IsFalse (sp.IsSubsetOf (null), sp.Flags.ToString ());
235 public void IsSubset_None ()
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)
245 sp2.Flags = (StorePermissionFlags) i;
246 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + sp2.Flags.ToString ());
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)
253 sp2.Flags = (StorePermissionFlags) i;
254 Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + sp2.Flags.ToString ());
256 // exception of NoFlags
257 sp2.Flags = StorePermissionFlags.NoFlags;
258 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoFlags");
262 public void IsSubset_Self ()
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)
269 sp.Flags = (StorePermissionFlags) i;
270 Assert.IsTrue (sp.IsSubsetOf (sp), sp.Flags.ToString ());
275 public void IsSubset_Unrestricted ()
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)
285 sp2.Flags = (StorePermissionFlags) i;
286 Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + sp2.Flags.ToString ());
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)
296 sp2.Flags = (StorePermissionFlags) i;
297 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + sp2.Flags.ToString ());
302 public void Union_Null ()
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)
310 sp.Flags = (StorePermissionFlags) i;
311 StorePermission union = (StorePermission) sp.Union (null);
312 Assert.AreEqual (sp.Flags, union.Flags, sp.Flags.ToString ());
314 // except fot NoFlags (which returns null)
315 sp.Flags = StorePermissionFlags.NoFlags;
316 Assert.IsNull (sp.Union (null), "NoFlags");
320 public void Union_None ()
322 // Union with none is same
323 StorePermission sp1 = new StorePermission (PermissionState.None);
324 StorePermission sp2 = new StorePermission (PermissionState.None);
326 StorePermission union = (StorePermission) sp1.Union (sp1);
327 Assert.IsNull (union, "NoFlags");
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)
333 sp2.Flags = (StorePermissionFlags) i;
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 ());
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 ());
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");
349 union = (StorePermission) sp2.Union (sp1);
350 Assert.IsTrue (union.IsUnrestricted (), "source.Unrestricted Unrestricted");
351 Assert.AreEqual (StorePermissionFlags.AllFlags, union.Flags, "source.Level Unrestricted");
355 public void Union_Self ()
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)
364 sp.Flags = (StorePermissionFlags) i;
365 union = (StorePermission) sp.Union (sp);
366 Assert.AreEqual (sp.Flags, union.Flags, sp.Flags.ToString ());
371 public void Union_Unrestricted ()
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)
381 sp2.Flags = (StorePermissionFlags) i;
382 StorePermission union = (StorePermission) sp1.Union (sp2);
383 Assert.IsTrue (union.IsUnrestricted (), "target " + sp2.Flags.ToString ());
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)
390 sp2.Flags = (StorePermissionFlags) i;
391 StorePermission union = (StorePermission) sp2.Union (sp1);
392 Assert.IsTrue (union.IsUnrestricted (), "source " + sp2.Flags.ToString ());
397 [ExpectedException (typeof (ArgumentNullException))]
398 public void FromXml_Null ()
400 StorePermission sp = new StorePermission (PermissionState.None);
405 public void FromXml_WrongTag ()
407 StorePermission sp = new StorePermission (PermissionState.None);
408 SecurityElement se = sp.ToXml ();
411 // note: normally IPermission classes (in corlib) DO care about the
416 public void FromXml_WrongTagCase ()
418 StorePermission sp = new StorePermission (PermissionState.None);
419 SecurityElement se = sp.ToXml ();
420 se.Tag = "IPERMISSION"; // instead of IPermission
422 // note: normally IPermission classes (in corlib) DO care about the
427 public void FromXml_WrongClass ()
429 StorePermission sp = new StorePermission (PermissionState.None);
430 SecurityElement se = sp.ToXml ();
432 SecurityElement w = new SecurityElement (se.Tag);
433 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
434 w.AddAttribute ("version", se.Attribute ("version"));
436 // doesn't care of the class name at that stage
437 // anyway the class has already be created so...
441 [ExpectedException (typeof (ArgumentException))]
442 public void FromXml_NoClass ()
444 StorePermission sp = new StorePermission (PermissionState.None);
445 SecurityElement se = sp.ToXml ();
447 SecurityElement w = new SecurityElement (se.Tag);
448 w.AddAttribute ("version", se.Attribute ("version"));
450 // note: normally IPermission classes (in corlib) DO NOT care about
451 // attribute "class" name presence in the XML
455 [ExpectedException (typeof (ArgumentException))]
456 public void FromXml_WrongVersion ()
458 StorePermission sp = new StorePermission (PermissionState.None);
459 SecurityElement se = sp.ToXml ();
460 se.Attributes.Remove ("version");
461 se.Attributes.Add ("version", "2");
466 public void FromXml_NoVersion ()
468 StorePermission sp = new StorePermission (PermissionState.None);
469 SecurityElement se = sp.ToXml ();
471 SecurityElement w = new SecurityElement (se.Tag);
472 w.AddAttribute ("class", se.Attribute ("class"));
474 // version is optional (in this case)