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 ();
\r
95 Assert.IsNotNull (se.Attribute ("Unrestricted"), "Xml-Unrestricted");
\r
96 Assert.AreEqual ("Unrestricted", se.Attribute ("Level"), "Xml-Level");
\r
97 Assert.IsNull (se.Children, "Xml-Children");
\r
99 AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
\r
100 Assert.IsFalse (Object.ReferenceEquals (anhp, copy), "ReferenceEquals");
\r
101 Assert.AreEqual (anhp.Level, copy.Level, "Level");
\r
102 Assert.AreEqual (anhp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
\r
106 [ExpectedException (typeof (ArgumentException))]
\r
107 public void PermissionState_Bad ()
\r
109 PermissionState ps = (PermissionState) Int32.MinValue;
\r
110 AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
\r
114 [ExpectedException (typeof (ArgumentException))]
\r
115 public void AspNetHostingPermissionLevels_Bad ()
\r
117 AspNetHostingPermissionLevel ppl = (AspNetHostingPermissionLevel) Int32.MinValue;
\r
118 AspNetHostingPermission anhp = new AspNetHostingPermission (ppl);
\r
122 [ExpectedException (typeof (ArgumentException))]
\r
123 public void Level_AspNetHostingPermissionLevels_Bad ()
\r
125 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
126 anhp.Level = (AspNetHostingPermissionLevel) Int32.MinValue;
\r
130 public void Copy ()
\r
132 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
133 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
135 AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
\r
136 Assert.AreEqual (ppl, copy.Level, ppl.ToString ());
\r
141 public void Intersect_Null ()
\r
143 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
144 // No intersection with null
\r
145 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
147 IPermission p = anhp.Intersect (null);
\r
148 Assert.IsNull (p, ppl.ToString ());
\r
153 public void Intersect_None ()
\r
155 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.None);
\r
156 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
157 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
159 // 1. Intersect None with ppl
\r
160 AspNetHostingPermission result = (AspNetHostingPermission)sp1.Intersect (sp2);
\r
161 Assert.AreEqual (AspNetHostingPermissionLevel.None, result.Level, "None N " + ppl.ToString ());
\r
162 // 2. Intersect ppl with None
\r
163 result = (AspNetHostingPermission)sp2.Intersect (sp1);
\r
164 Assert.AreEqual (AspNetHostingPermissionLevel.None, result.Level, ppl.ToString () + "N None");
\r
169 public void Intersect_Self ()
\r
171 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
172 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
174 AspNetHostingPermission result = (AspNetHostingPermission)anhp.Intersect (anhp);
\r
175 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
\r
180 public void Intersect_Unrestricted ()
\r
182 // Intersection with unrestricted == Copy
\r
183 // a. source (this) is unrestricted
\r
184 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
\r
185 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
186 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
188 AspNetHostingPermission result = (AspNetHostingPermission)sp1.Intersect (sp2);
\r
189 Assert.AreEqual (sp2.Level, result.Level, "target " + ppl.ToString ());
\r
191 // b. destination (target) is unrestricted
\r
192 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
194 AspNetHostingPermission result = (AspNetHostingPermission)sp2.Intersect (sp1);
\r
195 Assert.AreEqual (sp2.Level, result.Level, "source " + ppl.ToString ());
\r
200 public void IsSubset_Null ()
\r
202 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
203 Assert.IsTrue (anhp.IsSubsetOf (null), "NoLevel");
\r
204 foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptNone) {
\r
206 Assert.IsFalse (anhp.IsSubsetOf (null), ppl.ToString ());
\r
211 public void IsSubset_None ()
\r
213 // IsSubset with none
\r
214 // a. source (this) is none -> target is never a subset
\r
215 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.None);
\r
216 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
217 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
219 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
\r
221 // b. destination (target) is none -> target is always a subset
\r
222 foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptNone) {
\r
224 Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
\r
226 // exception of NoLevel
\r
227 sp2.Level = AspNetHostingPermissionLevel.None;
\r
228 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoLevel");
\r
232 public void IsSubset_Self ()
\r
234 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
235 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
237 AspNetHostingPermission result = (AspNetHostingPermission)anhp.Intersect (anhp);
\r
238 Assert.IsTrue (anhp.IsSubsetOf (anhp), ppl.ToString ());
\r
243 public void IsSubset_Unrestricted ()
\r
245 // IsSubset with unrestricted
\r
246 // a. source (this) is unrestricted -> target is never a subset
\r
247 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
\r
248 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
249 foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptUnrestricted) {
\r
251 Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
\r
253 // exception of AllLevel
\r
254 sp2.Level = AspNetHostingPermissionLevel.Unrestricted;
\r
255 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target AllLevel");
\r
256 // b. destination (target) is unrestricted -> target is always a subset
\r
257 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
259 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
\r
264 public void Union_Null ()
\r
266 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
267 // Union with null is a simple copy
\r
268 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
270 AspNetHostingPermission union = (AspNetHostingPermission)anhp.Union (null);
\r
271 Assert.AreEqual (ppl, union.Level, ppl.ToString ());
\r
276 public void Union_None ()
\r
278 // Union with none is same
\r
279 AspNetHostingPermission pp1 = new AspNetHostingPermission (PermissionState.None);
\r
280 AspNetHostingPermission pp2 = new AspNetHostingPermission (PermissionState.None);
\r
281 AspNetHostingPermission union = null;
\r
283 foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptUnrestricted) {
\r
286 union = (AspNetHostingPermission)pp1.Union (pp2);
\r
287 Assert.IsFalse (union.IsUnrestricted (), "target.Unrestricted " + ppl.ToString ());
\r
288 Assert.AreEqual (ppl, union.Level, "target.Level " + ppl.ToString ());
\r
290 union = (AspNetHostingPermission)pp2.Union (pp1);
\r
291 Assert.IsFalse (union.IsUnrestricted (), "source.Unrestricted " + ppl.ToString ());
\r
292 Assert.AreEqual (ppl, union.Level, "source.Level " + ppl.ToString ());
\r
295 pp2.Level = AspNetHostingPermissionLevel.Unrestricted;
\r
296 union = (AspNetHostingPermission)pp1.Union (pp2);
\r
297 Assert.IsTrue (union.IsUnrestricted (), "target.Unrestricted Unrestricted");
\r
298 Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, union.Level, "target.Level Unrestricted");
\r
300 union = (AspNetHostingPermission)pp2.Union (pp1);
\r
301 Assert.IsTrue (union.IsUnrestricted (), "source.Unrestricted Unrestricted");
\r
302 Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, union.Level, "source.Level Unrestricted");
\r
306 public void Union_Self ()
\r
308 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
309 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
311 AspNetHostingPermission result = (AspNetHostingPermission)anhp.Union (anhp);
\r
312 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
\r
317 public void Union_Unrestricted ()
\r
319 // Union with unrestricted is unrestricted
\r
320 AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
\r
321 AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
\r
322 // a. source (this) is unrestricted
\r
323 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
325 AspNetHostingPermission union = (AspNetHostingPermission)sp1.Union (sp2);
\r
326 Assert.IsTrue (union.IsUnrestricted (), "target " + ppl.ToString ());
\r
328 // b. destination (target) is unrestricted
\r
329 foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
\r
331 AspNetHostingPermission union = (AspNetHostingPermission)sp2.Union (sp1);
\r
332 Assert.IsTrue (union.IsUnrestricted (), "source " + ppl.ToString ());
\r
337 [ExpectedException (typeof (ArgumentNullException))]
\r
338 public void FromXml_Null ()
\r
340 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
341 anhp.FromXml (null);
\r
345 [ExpectedException (typeof (ArgumentException))]
\r
346 public void FromXml_WrongTag ()
\r
348 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
349 SecurityElement se = anhp.ToXml ();
\r
352 // note: normally IPermission classes (in corlib) DO care about the
\r
357 [ExpectedException (typeof (ArgumentException))]
\r
358 public void FromXml_WrongTagCase ()
\r
360 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
361 SecurityElement se = anhp.ToXml ();
\r
362 se.Tag = "IPERMISSION"; // instead of IPermission
\r
364 // note: normally IPermission classes (in corlib) DO care about the
\r
369 public void FromXml_WrongClass ()
\r
371 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
372 SecurityElement se = anhp.ToXml ();
\r
374 SecurityElement w = new SecurityElement (se.Tag);
\r
375 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
\r
376 w.AddAttribute ("version", se.Attribute ("version"));
\r
378 // doesn't care of the class name at that stage
\r
379 // anyway the class has already be created so...
\r
383 [ExpectedException (typeof (ArgumentException))]
\r
384 public void FromXml_NoClass ()
\r
386 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
387 SecurityElement se = anhp.ToXml ();
\r
389 SecurityElement w = new SecurityElement (se.Tag);
\r
390 w.AddAttribute ("version", se.Attribute ("version"));
\r
392 // note: normally IPermission classes (in corlib) DO NOT care about
\r
393 // attribute "class" name presence in the XML
\r
397 [ExpectedException (typeof (ArgumentException))]
\r
398 public void FromXml_WrongVersion ()
\r
400 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
401 SecurityElement se = anhp.ToXml ();
\r
402 se.Attributes.Remove ("version");
\r
403 se.Attributes.Add ("version", "2");
\r
408 [ExpectedException (typeof (ArgumentException))]
\r
409 public void FromXml_NoVersion ()
\r
411 AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
\r
412 SecurityElement se = anhp.ToXml ();
\r
414 SecurityElement w = new SecurityElement (se.Tag);
\r
415 w.AddAttribute ("class", se.Attribute ("class"));
\r