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",
55 public void PermissionState_None ()
57 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
58 // that cause a NullReferenceException before 2.0
59 Assert.AreEqual (String.Empty, uip.Url, "Url");
60 SecurityElement se = uip.ToXml ();
61 // only class and version are present
62 Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
63 Assert.IsNull (se.Children, "Xml-Children");
64 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
65 Assert.IsFalse (Object.ReferenceEquals (uip, copy), "ReferenceEquals");
70 [Category ("NotWorking")]
71 public void PermissionStateUnrestricted ()
73 // In 2.0 Unrestricted are permitted for identity permissions
74 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
75 Assert.AreEqual (String.Empty, uip.Url, "Url");
76 SecurityElement se = uip.ToXml ();
77 // only class and version are present
78 Assert.AreEqual (3, se.Attributes.Count, "Xml-Attributes");
79 Assert.IsNull (se.Children, "Xml-Children");
80 // and they aren't equals to None
81 Assert.IsFalse (uip.Equals (new UrlIdentityPermission (PermissionState.None)));
84 [ExpectedException (typeof (ArgumentException))]
85 public void PermissionState_Bad ()
87 UrlIdentityPermission uip = new UrlIdentityPermission ((PermissionState)Int32.MinValue);
91 [ExpectedException (typeof (ArgumentNullException))]
92 public void UrlIdentityPermission_NullUrl ()
94 UrlIdentityPermission uip = new UrlIdentityPermission (null);
100 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
101 foreach (string s in GoodUrls) {
103 Assert.AreEqual (s, uip.Url, s);
108 public void Url_Null ()
110 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
112 Assert.AreEqual (String.Empty, uip.Url, "Url");
118 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
119 foreach (string s in GoodUrls) {
121 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
122 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
123 // so we only compare the start of the url
124 Assert.AreEqual (uip.Url, copy.Url, "Url");
129 public void Copy_None ()
131 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
132 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
136 public void Intersect_Null ()
138 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
139 // No intersection with null
140 foreach (string s in GoodUrls) {
142 Assert.IsNull (uip.Intersect (null), s);
145 [Category ("NotWorking")]
147 public void Intersect_None ()
149 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
150 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
151 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
152 Assert.IsNull (result, "None N None");
153 foreach (string s in GoodUrls) {
155 // 1. Intersect None with Url
156 result = (UrlIdentityPermission)uip1.Intersect (uip2);
157 Assert.IsNull (result, "None N " + s);
158 // 2. Intersect Url with None
159 result = (UrlIdentityPermission)uip2.Intersect (uip1);
160 Assert.IsNull (result, s + "N None");
165 public void Intersect_Self ()
167 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
168 foreach (string s in GoodUrls) {
170 UrlIdentityPermission result = (UrlIdentityPermission)uip.Intersect (uip);
171 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
172 // so we only compare the start of the url
173 Assert.IsTrue (result.Url.StartsWith (uip.Url), s);
178 public void Intersect_Different ()
180 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
181 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
182 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
183 Assert.IsNull (result, "Mono N Novell");
187 public void IsSubset_Null ()
189 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
190 Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
191 foreach (string s in GoodUrls) {
193 Assert.IsFalse (uip.IsSubsetOf (null), s);
197 [Category ("NotWorking")]
199 public void IsSubset_None ()
201 // IsSubset with none
202 // a. source (this) is none -> target is never a subset
203 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
204 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
205 foreach (string s in GoodUrls) {
207 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
209 uip1 = new UrlIdentityPermission (PermissionState.None);
210 // b. destination (target) is none -> target is always a subset
211 foreach (string s in GoodUrls) {
213 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
218 public void IsSubset_Self ()
220 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
221 Assert.IsTrue (uip.IsSubsetOf (uip), "None");
222 foreach (string s in GoodUrls) {
224 Assert.IsTrue (uip.IsSubsetOf (uip), s);
229 public void IsSubset_Different ()
231 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
232 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
233 Assert.IsFalse (uip1.IsSubsetOf (uip2), "Mono subset Novell");
234 Assert.IsFalse (uip2.IsSubsetOf (uip1), "Novell subset Mono");
238 public void Union_Null ()
240 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
241 // Union with null is a simple copy
242 foreach (string s in GoodUrls) {
244 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (null);
245 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
246 // so we only compare the start of the url
247 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
252 public void Union_None ()
254 // Union with none is same
255 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
256 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
257 // a. source (this) is none
258 foreach (string s in GoodUrls) {
260 UrlIdentityPermission union = (UrlIdentityPermission)uip1.Union (uip2);
261 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
262 // so we only compare the start of the url
263 Assert.IsTrue (union.Url.StartsWith (uip1.Url), s);
265 uip1 = new UrlIdentityPermission (PermissionState.None);
266 // b. destination (target) is none
267 foreach (string s in GoodUrls) {
269 UrlIdentityPermission union = (UrlIdentityPermission)uip2.Union (uip1);
270 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
271 // so we only compare the start of the url
272 Assert.IsTrue (union.Url.StartsWith (uip2.Url), s);
277 public void Union_Self ()
279 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
280 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (uip);
281 Assert.IsNull (union, "None U None");
282 foreach (string s in GoodUrls) {
284 union = (UrlIdentityPermission)uip.Union (uip);
285 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
286 // so we only compare the start of the url
287 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
290 [Category ("NotWorking")]
292 public void Union_Different ()
294 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
295 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
296 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
297 Assert.IsNotNull (result, "Mono U Novell");
298 // new XML format is used to contain more than one site
299 SecurityElement se = result.ToXml ();
300 Assert.AreEqual (2, se.Children.Count, "Childs");
301 Assert.AreEqual (GoodUrls [0], (se.Children [0] as SecurityElement).Attribute ("Url"), "Url#1");
302 Assert.AreEqual (GoodUrls [1], (se.Children [1] as SecurityElement).Attribute ("Url"), "Url#2");
303 // strangely it is still versioned as 'version="1"'.
304 Assert.AreEqual ("1", se.Attribute ("version"), "Version");
308 [ExpectedException (typeof (ArgumentNullException))]
309 public void FromXml_Null ()
311 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
316 [ExpectedException (typeof (ArgumentException))]
317 public void FromXml_WrongTag ()
319 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
320 SecurityElement se = uip.ToXml ();
326 [ExpectedException (typeof (ArgumentException))]
327 public void FromXml_WrongTagCase ()
329 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
330 SecurityElement se = uip.ToXml ();
331 se.Tag = "IPERMISSION"; // instead of IPermission
336 public void FromXml_WrongClass ()
338 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
339 SecurityElement se = uip.ToXml ();
341 SecurityElement w = new SecurityElement (se.Tag);
342 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
343 w.AddAttribute ("version", se.Attribute ("version"));
345 // doesn't care of the class name at that stage
346 // anyway the class has already be created so...
350 public void FromXml_NoClass ()
352 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
353 SecurityElement se = uip.ToXml ();
355 SecurityElement w = new SecurityElement (se.Tag);
356 w.AddAttribute ("version", se.Attribute ("version"));
358 // doesn't even care of the class attribute presence
362 [ExpectedException (typeof (ArgumentException))]
363 public void FromXml_WrongVersion ()
365 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
366 SecurityElement se = uip.ToXml ();
367 se.Attributes.Remove ("version");
368 se.Attributes.Add ("version", "2");
373 public void FromXml_NoVersion ()
375 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
376 SecurityElement se = uip.ToXml ();
378 SecurityElement w = new SecurityElement (se.Tag);
379 w.AddAttribute ("class", se.Attribute ("class"));