2 // UrlIdentityPermissionTest.cs - NUnit Test Cases for UrlIdentityPermission
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 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.
29 using NUnit.Framework;
31 using System.Security;
32 using System.Security.Permissions;
34 using System.Diagnostics;
36 namespace MonoTests.System.Security.Permissions {
39 public class UrlIdentityPermissionTest {
41 static string[] GoodUrls = {
42 "http://www.mono-project.com:80/",
43 "http://www.mono-project.com/",
44 "http://www.mono-project.com",
45 "www.mono-project.com",
48 "*www.mono-project.com",
54 // accepted as Url but fails to work (as expected) in some methods
55 static string[] SemiBadUrls = {
56 "www.mono-project.com:80",
61 public void PermissionState_None ()
63 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
65 // that cause a NullReferenceException before 2.0
66 Assert.AreEqual (String.Empty, uip.Url, "Url");
68 SecurityElement se = uip.ToXml ();
69 // only class and version are present
70 Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
71 Assert.IsNull (se.Children, "Xml-Children");
73 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
74 Assert.IsFalse (Object.ReferenceEquals (uip, copy), "ReferenceEquals");
80 [ExpectedException (typeof (NullReferenceException))]
81 public void PermissionState_None_Url ()
83 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
84 Assert.IsNull (uip.Url, "Url");
90 [Category ("NotWorking")]
91 public void PermissionStateUnrestricted ()
93 // In 2.0 Unrestricted are permitted for identity permissions
94 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
95 Assert.AreEqual (String.Empty, uip.Url, "Url");
96 SecurityElement se = uip.ToXml ();
97 // only class and version are present
98 Assert.AreEqual (3, se.Attributes.Count, "Xml-Attributes");
99 Assert.IsNull (se.Children, "Xml-Children");
100 // and they aren't equals to None
101 Assert.IsFalse (uip.Equals (new UrlIdentityPermission (PermissionState.None)));
105 [ExpectedException (typeof (ArgumentException))]
106 public void PermissionState_Unrestricted ()
108 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
112 [ExpectedException (typeof (ArgumentException))]
113 public void PermissionState_Bad ()
115 UrlIdentityPermission uip = new UrlIdentityPermission ((PermissionState)Int32.MinValue);
119 [ExpectedException (typeof (ArgumentNullException))]
120 public void UrlIdentityPermission_NullUrl ()
122 UrlIdentityPermission uip = new UrlIdentityPermission (null);
128 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
129 foreach (string s in GoodUrls) {
132 Assert.AreEqual (s, uip.Url, s);
134 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
135 // so we only compare the start of the url
142 // this was working in beta1 but is broken in Nov CTP
143 [ExpectedException (typeof (NullReferenceException))]
144 [Category ("NotWorking")]
146 [ExpectedException (typeof (ArgumentNullException))]
148 public void Url_Null ()
150 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
157 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
158 foreach (string s in GoodUrls) {
160 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
161 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
162 // so we only compare the start of the url
163 Assert.AreEqual (uip.Url, copy.Url, "Url");
169 [ExpectedException (typeof (NullReferenceException))]
171 public void Copy_None ()
173 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
174 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
178 public void Intersect_Null ()
180 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
181 // No intersection with null
182 foreach (string s in GoodUrls) {
184 Assert.IsNull (uip.Intersect (null), s);
188 [Category ("NotWorking")]
191 public void Intersect_None ()
193 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
194 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
195 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
196 Assert.IsNull (result, "None N None");
197 foreach (string s in GoodUrls) {
199 // 1. Intersect None with Url
200 result = (UrlIdentityPermission)uip1.Intersect (uip2);
201 Assert.IsNull (result, "None N " + s);
202 // 2. Intersect Url with None
203 result = (UrlIdentityPermission)uip2.Intersect (uip1);
204 Assert.IsNull (result, s + "N None");
209 public void Intersect_Self ()
211 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
212 foreach (string s in GoodUrls) {
214 UrlIdentityPermission result = (UrlIdentityPermission)uip.Intersect (uip);
215 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
216 // so we only compare the start of the url
217 Assert.IsTrue (result.Url.StartsWith (uip.Url), s);
222 public void Intersect_Different ()
224 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
225 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
226 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
227 Assert.IsNull (result, "Mono N Novell");
231 public void IsSubset_Null ()
233 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
234 Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
235 foreach (string s in GoodUrls) {
237 Assert.IsFalse (uip.IsSubsetOf (null), s);
242 [Category ("NotWorking")]
245 public void IsSubset_None ()
247 // IsSubset with none
248 // a. source (this) is none -> target is never a subset
249 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
250 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
251 foreach (string s in GoodUrls) {
253 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
255 uip1 = new UrlIdentityPermission (PermissionState.None);
256 // b. destination (target) is none -> target is always a subset
257 foreach (string s in GoodUrls) {
259 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
264 public void IsSubset_Self ()
266 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
267 Assert.IsTrue (uip.IsSubsetOf (uip), "None");
268 foreach (string s in GoodUrls) {
270 Assert.IsTrue (uip.IsSubsetOf (uip), s);
275 public void IsSubset_Different ()
277 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
278 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
279 Assert.IsFalse (uip1.IsSubsetOf (uip2), "Mono subset Novell");
280 Assert.IsFalse (uip2.IsSubsetOf (uip1), "Novell subset Mono");
284 public void Union_Null ()
286 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
287 // Union with null is a simple copy
288 foreach (string s in GoodUrls) {
290 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (null);
291 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
292 // so we only compare the start of the url
293 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
298 public void Union_None ()
300 // Union with none is same
301 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
302 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
303 // a. source (this) is none
304 foreach (string s in GoodUrls) {
306 UrlIdentityPermission union = (UrlIdentityPermission)uip1.Union (uip2);
307 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
308 // so we only compare the start of the url
309 Assert.IsTrue (union.Url.StartsWith (uip1.Url), s);
311 uip1 = new UrlIdentityPermission (PermissionState.None);
312 // b. destination (target) is none
313 foreach (string s in GoodUrls) {
315 UrlIdentityPermission union = (UrlIdentityPermission)uip2.Union (uip1);
316 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
317 // so we only compare the start of the url
318 Assert.IsTrue (union.Url.StartsWith (uip2.Url), s);
323 public void Union_Self ()
325 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
326 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (uip);
327 Assert.IsNull (union, "None U None");
328 foreach (string s in GoodUrls) {
330 union = (UrlIdentityPermission)uip.Union (uip);
331 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
332 // so we only compare the start of the url
333 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
337 [Category ("NotWorking")]
340 public void Union_Different ()
342 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
343 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
344 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
346 Assert.IsNotNull (result, "Mono U Novell");
347 // new XML format is used to contain more than one site
348 SecurityElement se = result.ToXml ();
349 Assert.AreEqual (2, se.Children.Count, "Childs");
350 Assert.AreEqual (GoodUrls [0], (se.Children [0] as SecurityElement).Attribute ("Url"), "Url#1");
351 Assert.AreEqual (GoodUrls [1], (se.Children [1] as SecurityElement).Attribute ("Url"), "Url#2");
352 // strangely it is still versioned as 'version="1"'.
353 Assert.AreEqual ("1", se.Attribute ("version"), "Version");
355 Assert.IsNull (result, "Mono U Novell");
360 [ExpectedException (typeof (ArgumentNullException))]
361 public void FromXml_Null ()
363 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
368 [ExpectedException (typeof (ArgumentException))]
369 public void FromXml_WrongTag ()
371 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
372 SecurityElement se = uip.ToXml ();
378 [ExpectedException (typeof (ArgumentException))]
379 public void FromXml_WrongTagCase ()
381 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
382 SecurityElement se = uip.ToXml ();
383 se.Tag = "IPERMISSION"; // instead of IPermission
388 public void FromXml_WrongClass ()
390 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
391 SecurityElement se = uip.ToXml ();
393 SecurityElement w = new SecurityElement (se.Tag);
394 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
395 w.AddAttribute ("version", se.Attribute ("version"));
397 // doesn't care of the class name at that stage
398 // anyway the class has already be created so...
402 public void FromXml_NoClass ()
404 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
405 SecurityElement se = uip.ToXml ();
407 SecurityElement w = new SecurityElement (se.Tag);
408 w.AddAttribute ("version", se.Attribute ("version"));
410 // doesn't even care of the class attribute presence
414 [ExpectedException (typeof (ArgumentException))]
415 public void FromXml_WrongVersion ()
417 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
418 SecurityElement se = uip.ToXml ();
419 se.Attributes.Remove ("version");
420 se.Attributes.Add ("version", "2");
425 public void FromXml_NoVersion ()
427 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
428 SecurityElement se = uip.ToXml ();
430 SecurityElement w = new SecurityElement (se.Tag);
431 w.AddAttribute ("class", se.Attribute ("class"));