2 // AspNetHostingPermissionTest.cs -
\r
3 // NUnit Test Cases for AspNetHostingPermission
\r
6 // Sebastien Pouliot <sebastien@ximian.com>
\r
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
\r
10 // Permission is hereby granted, free of charge, to any person obtaining
\r
11 // a copy of this software and associated documentation files (the
\r
12 // "Software"), to deal in the Software without restriction, including
\r
13 // without limitation the rights to use, copy, modify, merge, publish,
\r
14 // distribute, sublicense, and/or sell copies of the Software, and to
\r
15 // permit persons to whom the Software is furnished to do so, subject to
\r
16 // the following conditions:
\r
18 // The above copyright notice and this permission notice shall be
\r
19 // included in all copies or substantial portions of the Software.
\r
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
30 using NUnit.Framework;
\r
32 using System.Security;
\r
33 using System.Security.Permissions;
\r
36 namespace MonoTests.System.Web {
\r
39 public class AspNetHostingPermissionTest {
\r
41 static AspNetHostingPermissionLevel[] AllLevel = {
\r
42 AspNetHostingPermissionLevel.None,
\r
43 AspNetHostingPermissionLevel.Minimal,
\r
44 AspNetHostingPermissionLevel.Low,
\r
45 AspNetHostingPermissionLevel.Medium,
\r
46 AspNetHostingPermissionLevel.High,
\r
47 AspNetHostingPermissionLevel.Unrestricted,
\r
50 static AspNetHostingPermissionLevel[] AllLevelExceptNone = {
\r
51 AspNetHostingPermissionLevel.Minimal,
\r
52 AspNetHostingPermissionLevel.Low,
\r
53 AspNetHostingPermissionLevel.Medium,
\r
54 AspNetHostingPermissionLevel.High,
\r
55 AspNetHostingPermissionLevel.Unrestricted,
\r
58 static AspNetHostingPermissionLevel[] AllLevelExceptUnrestricted = {
\r
59 AspNetHostingPermissionLevel.None,
\r
60 AspNetHostingPermissionLevel.Minimal,
\r
61 AspNetHostingPermissionLevel.Low,
\r
62 AspNetHostingPermissionLevel.Medium,
\r
63 AspNetHostingPermissionLevel.High,
\r
67 public void PermissionState_None ()
\r
69 PermissionState ps = PermissionState.None;
\r
70 AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
\r
71 Assert.AreEqual (AspNetHostingPermissionLevel.None, anhp.Level, "Level");
\r
72 Assert.IsFalse (anhp.IsUnrestricted (), "IsUnrestricted");
\r
74 SecurityElement se = anhp.ToXml ();
\r
75 // only class and version are present
\r
76 Assert.AreEqual ("None", se.Attribute ("Level"), "Xml-Level");
\r
77 Assert.IsNull (se.Children, "Xml-Children");
\r
79 AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
\r
80 Assert.IsFalse (Object.ReferenceEquals (anhp, copy), "ReferenceEquals");
\r
81 Assert.AreEqual (anhp.Level, copy.Level, "Level");
\r
82 Assert.AreEqual (anhp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
\r
86 public void PermissionState_Unrestricted ()
\r
88 PermissionState ps = PermissionState.Unrestricted;
\r
89 AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
\r
90 Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, anhp.Level, "Level");
\r
91 Assert.IsTrue (anhp.IsUnrestricted (), "IsUnrestricted");
\r
93 SecurityElement se = anhp.ToXml ();
96 Assert.IsNotNull (se.Attribute ("Unrestricted"), "Xml-Unrestricted");
98 Assert.IsNull (se.Attribute ("Unrestricted"), "Xml-Unrestricted");
100 Assert.AreEqual ("Unrestricted", se.Attribute ("Level"), "Xml-Level");
\r
101 Assert.IsNull (se.Children, "Xml-Children");
\r
103 AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
\r
104 Assert.IsFalse (Object.ReferenceEquals (anhp, copy), "ReferenceEquals");
\r
105 Assert.AreEqual (anhp.Level, copy.Level, "Level");
\r
106 Assert.AreEqual (anhp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
\r
110 [ExpectedException (typeof (ArgumentException))]
\r
111 public void PermissionState_Bad ()
\r
113 PermissionState ps = (PermissionState) Int32.MinValue;
\r
114 AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
\r
118 [ExpectedException (typeof (ArgumentException))]
\r
119 public void AspNetHostingPermissionLevels_Bad ()
\r
121 AspNetHostingPermissionLevel ppl = (AspNetHostingPermissionLevel) Int32.MinValue;
\r
122 AspNetHostingPermission anhp = new AspNetHostingPermission (ppl);
\r
126 [ExpectedException (typeof (ArgumentException))]
\r
127 public void Level_AspNetHostingPermissionLevels_Bad ()
\r
129 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
130 anhp.Level = (AspNetHostingPermissionLevel) Int32.MinValue;
\r
134 public void Copy ()
\r
136 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
137 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
139 AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
\r
140 Assert.AreEqual (ppl, copy.Level, ppl.ToString ());
\r
145 public void Intersect_Null ()
\r
147 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
148 // No intersection with null
\r
149 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
151 IPermission p = anhp.Intersect (null);
154 Assert.Ignore ("Behaviour changed in FX 1.1 SP1");
\r
156 Assert.IsNull (p, ppl.ToString ());
\r
161 public void Intersect_None ()
\r
163 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.None);
\r
164 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
165 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
167 // 1. Intersect None with ppl
\r
168 AspNetHostingPermission result = (AspNetHostingPermission)sp1.Intersect (sp2);
\r
169 Assert.AreEqual (AspNetHostingPermissionLevel.None, result.Level, "None N " + ppl.ToString ());
\r
170 // 2. Intersect ppl with None
\r
171 result = (AspNetHostingPermission)sp2.Intersect (sp1);
\r
172 Assert.AreEqual (AspNetHostingPermissionLevel.None, result.Level, ppl.ToString () + "N None");
\r
177 public void Intersect_Self ()
\r
179 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
180 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
182 AspNetHostingPermission result = (AspNetHostingPermission)anhp.Intersect (anhp);
\r
183 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
\r
188 public void Intersect_Unrestricted ()
\r
190 // Intersection with unrestricted == Copy
\r
191 // a. source (this) is unrestricted
\r
192 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
\r
193 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
194 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
196 AspNetHostingPermission result = (AspNetHostingPermission)sp1.Intersect (sp2);
\r
197 Assert.AreEqual (sp2.Level, result.Level, "target " + ppl.ToString ());
\r
199 // b. destination (target) is unrestricted
\r
200 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
202 AspNetHostingPermission result = (AspNetHostingPermission)sp2.Intersect (sp1);
\r
203 Assert.AreEqual (sp2.Level, result.Level, "source " + ppl.ToString ());
\r
208 public void IsSubset_Null ()
\r
210 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
211 Assert.IsTrue (anhp.IsSubsetOf (null), "NoLevel");
\r
212 foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptNone) {
\r
214 Assert.IsFalse (anhp.IsSubsetOf (null), ppl.ToString ());
\r
219 public void IsSubset_None ()
\r
221 // IsSubset with none
\r
222 // a. source (this) is none -> target is never a subset
\r
223 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.None);
\r
224 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
225 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
227 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
\r
229 // b. destination (target) is none -> target is always a subset
\r
230 foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptNone) {
\r
232 Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
\r
234 // exception of NoLevel
\r
235 sp2.Level = AspNetHostingPermissionLevel.None;
\r
236 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoLevel");
\r
240 public void IsSubset_Self ()
\r
242 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
243 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
245 AspNetHostingPermission result = (AspNetHostingPermission)anhp.Intersect (anhp);
\r
246 Assert.IsTrue (anhp.IsSubsetOf (anhp), ppl.ToString ());
\r
251 public void IsSubset_Unrestricted ()
\r
253 // IsSubset with unrestricted
\r
254 // a. source (this) is unrestricted -> target is never a subset
\r
255 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
\r
256 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
257 foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptUnrestricted) {
\r
259 Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
\r
261 // exception of AllLevel
\r
262 sp2.Level = AspNetHostingPermissionLevel.Unrestricted;
\r
263 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target AllLevel");
\r
264 // b. destination (target) is unrestricted -> target is always a subset
\r
265 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
267 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
\r
272 public void Union_Null ()
\r
274 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
275 // Union with null is a simple copy
\r
276 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
278 AspNetHostingPermission union = (AspNetHostingPermission)anhp.Union (null);
\r
279 Assert.AreEqual (ppl, union.Level, ppl.ToString ());
\r
284 public void Union_None ()
\r
286 // Union with none is same
\r
287 AspNetHostingPermission pp1 = new AspNetHostingPermission (PermissionState.None);
\r
288 AspNetHostingPermission pp2 = new AspNetHostingPermission (PermissionState.None);
\r
289 AspNetHostingPermission union = null;
\r
291 foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptUnrestricted) {
\r
294 union = (AspNetHostingPermission)pp1.Union (pp2);
\r
295 Assert.IsFalse (union.IsUnrestricted (), "target.Unrestricted " + ppl.ToString ());
\r
296 Assert.AreEqual (ppl, union.Level, "target.Level " + ppl.ToString ());
\r
298 union = (AspNetHostingPermission)pp2.Union (pp1);
\r
299 Assert.IsFalse (union.IsUnrestricted (), "source.Unrestricted " + ppl.ToString ());
\r
300 Assert.AreEqual (ppl, union.Level, "source.Level " + ppl.ToString ());
\r
303 pp2.Level = AspNetHostingPermissionLevel.Unrestricted;
\r
304 union = (AspNetHostingPermission)pp1.Union (pp2);
\r
305 Assert.IsTrue (union.IsUnrestricted (), "target.Unrestricted Unrestricted");
\r
306 Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, union.Level, "target.Level Unrestricted");
\r
308 union = (AspNetHostingPermission)pp2.Union (pp1);
\r
309 Assert.IsTrue (union.IsUnrestricted (), "source.Unrestricted Unrestricted");
\r
310 Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, union.Level, "source.Level Unrestricted");
\r
314 public void Union_Self ()
\r
316 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
317 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
319 AspNetHostingPermission result = (AspNetHostingPermission)anhp.Union (anhp);
\r
320 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
\r
325 public void Union_Unrestricted ()
\r
327 // Union with unrestricted is unrestricted
\r
328 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
\r
329 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
330 // a. source (this) is unrestricted
\r
331 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
333 AspNetHostingPermission union = (AspNetHostingPermission)sp1.Union (sp2);
\r
334 Assert.IsTrue (union.IsUnrestricted (), "target " + ppl.ToString ());
\r
336 // b. destination (target) is unrestricted
\r
337 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
339 AspNetHostingPermission union = (AspNetHostingPermission)sp2.Union (sp1);
\r
340 Assert.IsTrue (union.IsUnrestricted (), "source " + ppl.ToString ());
\r
345 [ExpectedException (typeof (ArgumentNullException))]
\r
346 public void FromXml_Null ()
\r
348 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
349 anhp.FromXml (null);
\r
353 [ExpectedException (typeof (ArgumentException))]
\r
354 public void FromXml_WrongTag ()
\r
356 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
357 SecurityElement se = anhp.ToXml ();
\r
360 // note: normally IPermission classes (in corlib) DO care about the
\r
365 [ExpectedException (typeof (ArgumentException))]
\r
366 public void FromXml_WrongTagCase ()
\r
368 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
369 SecurityElement se = anhp.ToXml ();
\r
370 se.Tag = "IPERMISSION"; // instead of IPermission
\r
372 // note: normally IPermission classes (in corlib) DO care about the
\r
377 public void FromXml_WrongClass ()
\r
379 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
380 SecurityElement se = anhp.ToXml ();
\r
382 SecurityElement w = new SecurityElement (se.Tag);
\r
383 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
\r
384 w.AddAttribute ("version", se.Attribute ("version"));
\r
386 // doesn't care of the class name at that stage
\r
387 // anyway the class has already be created so...
\r
391 [ExpectedException (typeof (ArgumentException))]
\r
392 public void FromXml_NoClass ()
\r
394 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
395 SecurityElement se = anhp.ToXml ();
\r
397 SecurityElement w = new SecurityElement (se.Tag);
\r
398 w.AddAttribute ("version", se.Attribute ("version"));
\r
400 // note: normally IPermission classes (in corlib) DO NOT care about
\r
401 // attribute "class" name presence in the XML
\r
405 [ExpectedException (typeof (ArgumentException))]
\r
406 public void FromXml_WrongVersion ()
\r
408 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
409 SecurityElement se = anhp.ToXml ();
\r
410 se.Attributes.Remove ("version");
\r
411 se.Attributes.Add ("version", "2");
\r
416 [ExpectedException (typeof (ArgumentException))]
\r
417 public void FromXml_NoVersion ()
\r
419 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
420 SecurityElement se = anhp.ToXml ();
\r
422 SecurityElement w = new SecurityElement (se.Tag);
\r
423 w.AddAttribute ("class", se.Attribute ("class"));
\r