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 Assert.AreEqual (String.Empty, uip.Url, "Url");
67 SecurityElement se = uip.ToXml ();
68 // only class and version are present
69 Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
70 Assert.IsNull (se.Children, "Xml-Children");
72 // UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
73 // Assert.IsFalse (Object.ReferenceEquals (uip, copy), "ReferenceEquals");
78 [ExpectedException (typeof (NullReferenceException))]
79 public void PermissionState_None_Url ()
81 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
82 Assert.IsNull (uip.Url, "Url");
87 [ExpectedException (typeof (ArgumentException))]
88 public void PermissionState_Unrestricted ()
90 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
94 [ExpectedException (typeof (ArgumentException))]
95 public void PermissionState_Bad ()
97 UrlIdentityPermission uip = new UrlIdentityPermission ((PermissionState)Int32.MinValue);
101 [ExpectedException (typeof (ArgumentNullException))]
102 public void UrlIdentityPermission_NullUrl ()
104 UrlIdentityPermission uip = new UrlIdentityPermission (null);
110 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
111 foreach (string s in GoodUrls) {
114 Assert.AreEqual (s, uip.Url, s);
116 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
117 // so we only compare the start of the url
123 [ExpectedException (typeof (ArgumentNullException))]
124 public void Url_Null ()
126 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
133 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
134 foreach (string s in GoodUrls) {
136 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
137 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
138 // so we only compare the start of the url
139 Assert.AreEqual (uip.Url, copy.Url, "Url");
144 public void Intersect_Null ()
146 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
147 // No intersection with null
148 foreach (string s in GoodUrls) {
150 Assert.IsNull (uip.Intersect (null), s);
155 public void Intersect_None ()
157 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
158 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
159 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
160 Assert.IsNull (result, "None N None");
161 foreach (string s in GoodUrls) {
163 // 1. Intersect None with Url
164 result = (UrlIdentityPermission)uip1.Intersect (uip2);
165 Assert.IsNull (result, "None N " + s);
166 // 2. Intersect Url with None
167 result = (UrlIdentityPermission)uip2.Intersect (uip1);
168 Assert.IsNull (result, s + "N None");
173 public void Intersect_Self ()
175 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
176 foreach (string s in GoodUrls) {
178 UrlIdentityPermission result = (UrlIdentityPermission)uip.Intersect (uip);
179 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
180 // so we only compare the start of the url
181 Assert.IsTrue (result.Url.StartsWith (uip.Url), s);
186 public void Intersect_Different ()
188 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
189 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
190 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
191 Assert.IsNull (result, "Mono N Novell");
195 public void IsSubset_Null ()
197 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
198 Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
199 foreach (string s in GoodUrls) {
201 Assert.IsFalse (uip.IsSubsetOf (null), s);
206 public void IsSubset_None ()
208 // IsSubset with none
209 // a. source (this) is none -> target is never a subset
210 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
211 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
212 foreach (string s in GoodUrls) {
214 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
216 uip1 = new UrlIdentityPermission (PermissionState.None);
217 // b. destination (target) is none -> target is always a subset
218 foreach (string s in GoodUrls) {
220 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
225 public void IsSubset_Self ()
227 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
228 Assert.IsTrue (uip.IsSubsetOf (uip), "None");
229 foreach (string s in GoodUrls) {
231 Assert.IsTrue (uip.IsSubsetOf (uip), s);
236 public void IsSubset_Different ()
238 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
239 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
240 Assert.IsFalse (uip1.IsSubsetOf (uip2), "Mono subset Novell");
241 Assert.IsFalse (uip2.IsSubsetOf (uip1), "Novell subset Mono");
245 public void Union_Null ()
247 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
248 // Union with null is a simple copy
249 foreach (string s in GoodUrls) {
251 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (null);
252 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
253 // so we only compare the start of the url
254 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
259 public void Union_None ()
261 // Union with none is same
262 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
263 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
264 // a. source (this) is none
265 foreach (string s in GoodUrls) {
267 UrlIdentityPermission union = (UrlIdentityPermission)uip1.Union (uip2);
268 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
269 // so we only compare the start of the url
270 Assert.IsTrue (union.Url.StartsWith (uip1.Url), s);
272 uip1 = new UrlIdentityPermission (PermissionState.None);
273 // b. destination (target) is none
274 foreach (string s in GoodUrls) {
276 UrlIdentityPermission union = (UrlIdentityPermission)uip2.Union (uip1);
277 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
278 // so we only compare the start of the url
279 Assert.IsTrue (union.Url.StartsWith (uip2.Url), s);
284 public void Union_Self ()
286 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
287 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (uip);
288 Assert.IsNull (union, "None U None");
289 foreach (string s in GoodUrls) {
291 union = (UrlIdentityPermission)uip.Union (uip);
292 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
293 // so we only compare the start of the url
294 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
300 [ExpectedException (typeof (ArgumentException))]
302 public void Union_Different ()
304 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
305 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
306 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
307 Assert.IsNull (result, "Mono U Novell");
311 [ExpectedException (typeof (ArgumentNullException))]
312 public void FromXml_Null ()
314 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
319 [ExpectedException (typeof (ArgumentException))]
320 public void FromXml_WrongTag ()
322 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
323 SecurityElement se = uip.ToXml ();
329 [ExpectedException (typeof (ArgumentException))]
330 public void FromXml_WrongTagCase ()
332 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
333 SecurityElement se = uip.ToXml ();
334 se.Tag = "IPERMISSION"; // instead of IPermission
339 public void FromXml_WrongClass ()
341 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
342 SecurityElement se = uip.ToXml ();
344 SecurityElement w = new SecurityElement (se.Tag);
345 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
346 w.AddAttribute ("version", se.Attribute ("version"));
348 // doesn't care of the class name at that stage
349 // anyway the class has already be created so...
353 public void FromXml_NoClass ()
355 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
356 SecurityElement se = uip.ToXml ();
358 SecurityElement w = new SecurityElement (se.Tag);
359 w.AddAttribute ("version", se.Attribute ("version"));
361 // doesn't even care of the class attribute presence
365 [ExpectedException (typeof (ArgumentException))]
366 public void FromXml_WrongVersion ()
368 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
369 SecurityElement se = uip.ToXml ();
370 se.Attributes.Remove ("version");
371 se.Attributes.Add ("version", "2");
376 public void FromXml_NoVersion ()
378 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
379 SecurityElement se = uip.ToXml ();
381 SecurityElement w = new SecurityElement (se.Tag);
382 w.AddAttribute ("class", se.Attribute ("class"));