2 // UrlIdentityPermissionTest.cs - NUnit Test Cases for UrlIdentityPermission
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004-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.
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 public void Url_Null ()
144 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
146 Assert.AreEqual (String.Empty, uip.Url, "Url");
150 [ExpectedException (typeof (ArgumentNullException))]
151 public void Url_Null ()
153 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
161 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
162 foreach (string s in GoodUrls) {
164 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
165 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
166 // so we only compare the start of the url
167 Assert.AreEqual (uip.Url, copy.Url, "Url");
173 [ExpectedException (typeof (NullReferenceException))]
175 public void Copy_None ()
177 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
178 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
182 public void Intersect_Null ()
184 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
185 // No intersection with null
186 foreach (string s in GoodUrls) {
188 Assert.IsNull (uip.Intersect (null), s);
192 [Category ("NotWorking")]
195 public void Intersect_None ()
197 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
198 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
199 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
200 Assert.IsNull (result, "None N None");
201 foreach (string s in GoodUrls) {
203 // 1. Intersect None with Url
204 result = (UrlIdentityPermission)uip1.Intersect (uip2);
205 Assert.IsNull (result, "None N " + s);
206 // 2. Intersect Url with None
207 result = (UrlIdentityPermission)uip2.Intersect (uip1);
208 Assert.IsNull (result, s + "N None");
213 public void Intersect_Self ()
215 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
216 foreach (string s in GoodUrls) {
218 UrlIdentityPermission result = (UrlIdentityPermission)uip.Intersect (uip);
219 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
220 // so we only compare the start of the url
221 Assert.IsTrue (result.Url.StartsWith (uip.Url), s);
226 public void Intersect_Different ()
228 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
229 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
230 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
231 Assert.IsNull (result, "Mono N Novell");
235 public void IsSubset_Null ()
237 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
238 Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
239 foreach (string s in GoodUrls) {
241 Assert.IsFalse (uip.IsSubsetOf (null), s);
246 [Category ("NotWorking")]
249 public void IsSubset_None ()
251 // IsSubset with none
252 // a. source (this) is none -> target is never a subset
253 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
254 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
255 foreach (string s in GoodUrls) {
257 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
259 uip1 = new UrlIdentityPermission (PermissionState.None);
260 // b. destination (target) is none -> target is always a subset
261 foreach (string s in GoodUrls) {
263 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
268 public void IsSubset_Self ()
270 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
271 Assert.IsTrue (uip.IsSubsetOf (uip), "None");
272 foreach (string s in GoodUrls) {
274 Assert.IsTrue (uip.IsSubsetOf (uip), s);
279 public void IsSubset_Different ()
281 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
282 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
283 Assert.IsFalse (uip1.IsSubsetOf (uip2), "Mono subset Novell");
284 Assert.IsFalse (uip2.IsSubsetOf (uip1), "Novell subset Mono");
288 public void Union_Null ()
290 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
291 // Union with null is a simple copy
292 foreach (string s in GoodUrls) {
294 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (null);
295 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
296 // so we only compare the start of the url
297 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
302 public void Union_None ()
304 // Union with none is same
305 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
306 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
307 // a. source (this) is none
308 foreach (string s in GoodUrls) {
310 UrlIdentityPermission union = (UrlIdentityPermission)uip1.Union (uip2);
311 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
312 // so we only compare the start of the url
313 Assert.IsTrue (union.Url.StartsWith (uip1.Url), s);
315 uip1 = new UrlIdentityPermission (PermissionState.None);
316 // b. destination (target) is none
317 foreach (string s in GoodUrls) {
319 UrlIdentityPermission union = (UrlIdentityPermission)uip2.Union (uip1);
320 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
321 // so we only compare the start of the url
322 Assert.IsTrue (union.Url.StartsWith (uip2.Url), s);
327 public void Union_Self ()
329 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
330 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (uip);
331 Assert.IsNull (union, "None U None");
332 foreach (string s in GoodUrls) {
334 union = (UrlIdentityPermission)uip.Union (uip);
335 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
336 // so we only compare the start of the url
337 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
341 [Category ("NotWorking")]
344 public void Union_Different ()
346 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
347 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
348 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
350 Assert.IsNotNull (result, "Mono U Novell");
351 // new XML format is used to contain more than one site
352 SecurityElement se = result.ToXml ();
353 Assert.AreEqual (2, se.Children.Count, "Childs");
354 Assert.AreEqual (GoodUrls [0], (se.Children [0] as SecurityElement).Attribute ("Url"), "Url#1");
355 Assert.AreEqual (GoodUrls [1], (se.Children [1] as SecurityElement).Attribute ("Url"), "Url#2");
356 // strangely it is still versioned as 'version="1"'.
357 Assert.AreEqual ("1", se.Attribute ("version"), "Version");
359 Assert.IsNull (result, "Mono U Novell");
364 [ExpectedException (typeof (ArgumentNullException))]
365 public void FromXml_Null ()
367 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
372 [ExpectedException (typeof (ArgumentException))]
373 public void FromXml_WrongTag ()
375 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
376 SecurityElement se = uip.ToXml ();
382 [ExpectedException (typeof (ArgumentException))]
383 public void FromXml_WrongTagCase ()
385 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
386 SecurityElement se = uip.ToXml ();
387 se.Tag = "IPERMISSION"; // instead of IPermission
392 public void FromXml_WrongClass ()
394 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
395 SecurityElement se = uip.ToXml ();
397 SecurityElement w = new SecurityElement (se.Tag);
398 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
399 w.AddAttribute ("version", se.Attribute ("version"));
401 // doesn't care of the class name at that stage
402 // anyway the class has already be created so...
406 public void FromXml_NoClass ()
408 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
409 SecurityElement se = uip.ToXml ();
411 SecurityElement w = new SecurityElement (se.Tag);
412 w.AddAttribute ("version", se.Attribute ("version"));
414 // doesn't even care of the class attribute presence
418 [ExpectedException (typeof (ArgumentException))]
419 public void FromXml_WrongVersion ()
421 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
422 SecurityElement se = uip.ToXml ();
423 se.Attributes.Remove ("version");
424 se.Attributes.Add ("version", "2");
429 public void FromXml_NoVersion ()
431 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
432 SecurityElement se = uip.ToXml ();
434 SecurityElement w = new SecurityElement (se.Tag);
435 w.AddAttribute ("class", se.Attribute ("class"));