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");
145 [ExpectedException (typeof (NullReferenceException
))]
147 public void Copy_None ()
149 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
150 UrlIdentityPermission copy
= (UrlIdentityPermission
)uip
.Copy ();
154 public void Intersect_Null ()
156 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
157 // No intersection with null
158 foreach (string s
in GoodUrls
) {
160 Assert
.IsNull (uip
.Intersect (null), s
);
165 public void Intersect_None ()
167 UrlIdentityPermission uip1
= new UrlIdentityPermission (PermissionState
.None
);
168 UrlIdentityPermission uip2
= new UrlIdentityPermission (PermissionState
.None
);
169 UrlIdentityPermission result
= (UrlIdentityPermission
)uip1
.Intersect (uip2
);
170 Assert
.IsNull (result
, "None N None");
171 foreach (string s
in GoodUrls
) {
173 // 1. Intersect None with Url
174 result
= (UrlIdentityPermission
)uip1
.Intersect (uip2
);
175 Assert
.IsNull (result
, "None N " + s
);
176 // 2. Intersect Url with None
177 result
= (UrlIdentityPermission
)uip2
.Intersect (uip1
);
178 Assert
.IsNull (result
, s
+ "N None");
183 public void Intersect_Self ()
185 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
186 foreach (string s
in GoodUrls
) {
188 UrlIdentityPermission result
= (UrlIdentityPermission
)uip
.Intersect (uip
);
189 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
190 // so we only compare the start of the url
191 Assert
.IsTrue (result
.Url
.StartsWith (uip
.Url
), s
);
196 public void Intersect_Different ()
198 UrlIdentityPermission uip1
= new UrlIdentityPermission (GoodUrls
[0]);
199 UrlIdentityPermission uip2
= new UrlIdentityPermission (GoodUrls
[1]);
200 UrlIdentityPermission result
= (UrlIdentityPermission
)uip1
.Intersect (uip2
);
201 Assert
.IsNull (result
, "Mono N Novell");
205 public void IsSubset_Null ()
207 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
208 Assert
.IsTrue (uip
.IsSubsetOf (null), "Empty");
209 foreach (string s
in GoodUrls
) {
211 Assert
.IsFalse (uip
.IsSubsetOf (null), s
);
216 public void IsSubset_None ()
218 // IsSubset with none
219 // a. source (this) is none -> target is never a subset
220 UrlIdentityPermission uip1
= new UrlIdentityPermission (PermissionState
.None
);
221 UrlIdentityPermission uip2
= new UrlIdentityPermission (PermissionState
.None
);
222 foreach (string s
in GoodUrls
) {
224 Assert
.IsFalse (uip1
.IsSubsetOf (uip2
), "target " + s
);
226 uip1
= new UrlIdentityPermission (PermissionState
.None
);
227 // b. destination (target) is none -> target is always a subset
228 foreach (string s
in GoodUrls
) {
230 Assert
.IsFalse (uip2
.IsSubsetOf (uip1
), "source " + s
);
235 public void IsSubset_Self ()
237 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
238 Assert
.IsTrue (uip
.IsSubsetOf (uip
), "None");
239 foreach (string s
in GoodUrls
) {
241 Assert
.IsTrue (uip
.IsSubsetOf (uip
), s
);
246 public void IsSubset_Different ()
248 UrlIdentityPermission uip1
= new UrlIdentityPermission (GoodUrls
[0]);
249 UrlIdentityPermission uip2
= new UrlIdentityPermission (GoodUrls
[1]);
250 Assert
.IsFalse (uip1
.IsSubsetOf (uip2
), "Mono subset Novell");
251 Assert
.IsFalse (uip2
.IsSubsetOf (uip1
), "Novell subset Mono");
255 public void Union_Null ()
257 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
258 // Union with null is a simple copy
259 foreach (string s
in GoodUrls
) {
261 UrlIdentityPermission union
= (UrlIdentityPermission
)uip
.Union (null);
262 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
263 // so we only compare the start of the url
264 Assert
.IsTrue (union
.Url
.StartsWith (uip
.Url
), s
);
269 public void Union_None ()
271 // Union with none is same
272 UrlIdentityPermission uip1
= new UrlIdentityPermission (PermissionState
.None
);
273 UrlIdentityPermission uip2
= new UrlIdentityPermission (PermissionState
.None
);
274 // a. source (this) is none
275 foreach (string s
in GoodUrls
) {
277 UrlIdentityPermission union
= (UrlIdentityPermission
)uip1
.Union (uip2
);
278 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
279 // so we only compare the start of the url
280 Assert
.IsTrue (union
.Url
.StartsWith (uip1
.Url
), s
);
282 uip1
= new UrlIdentityPermission (PermissionState
.None
);
283 // b. destination (target) is none
284 foreach (string s
in GoodUrls
) {
286 UrlIdentityPermission union
= (UrlIdentityPermission
)uip2
.Union (uip1
);
287 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
288 // so we only compare the start of the url
289 Assert
.IsTrue (union
.Url
.StartsWith (uip2
.Url
), s
);
294 public void Union_Self ()
296 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
297 UrlIdentityPermission union
= (UrlIdentityPermission
)uip
.Union (uip
);
298 Assert
.IsNull (union
, "None U None");
299 foreach (string s
in GoodUrls
) {
301 union
= (UrlIdentityPermission
)uip
.Union (uip
);
302 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
303 // so we only compare the start of the url
304 Assert
.IsTrue (union
.Url
.StartsWith (uip
.Url
), s
);
310 [ExpectedException (typeof (ArgumentException
))]
312 public void Union_Different ()
314 UrlIdentityPermission uip1
= new UrlIdentityPermission (GoodUrls
[0]);
315 UrlIdentityPermission uip2
= new UrlIdentityPermission (GoodUrls
[1]);
316 UrlIdentityPermission result
= (UrlIdentityPermission
)uip1
.Union (uip2
);
317 Assert
.IsNull (result
, "Mono U Novell");
321 [ExpectedException (typeof (ArgumentNullException
))]
322 public void FromXml_Null ()
324 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
329 [ExpectedException (typeof (ArgumentException
))]
330 public void FromXml_WrongTag ()
332 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
333 SecurityElement se
= uip
.ToXml ();
339 [ExpectedException (typeof (ArgumentException
))]
340 public void FromXml_WrongTagCase ()
342 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
343 SecurityElement se
= uip
.ToXml ();
344 se
.Tag
= "IPERMISSION"; // instead of IPermission
349 public void FromXml_WrongClass ()
351 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
352 SecurityElement se
= uip
.ToXml ();
354 SecurityElement w
= new SecurityElement (se
.Tag
);
355 w
.AddAttribute ("class", "Wrong" + se
.Attribute ("class"));
356 w
.AddAttribute ("version", se
.Attribute ("version"));
358 // doesn't care of the class name at that stage
359 // anyway the class has already be created so...
363 public void FromXml_NoClass ()
365 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
366 SecurityElement se
= uip
.ToXml ();
368 SecurityElement w
= new SecurityElement (se
.Tag
);
369 w
.AddAttribute ("version", se
.Attribute ("version"));
371 // doesn't even care of the class attribute presence
375 [ExpectedException (typeof (ArgumentException
))]
376 public void FromXml_WrongVersion ()
378 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
379 SecurityElement se
= uip
.ToXml ();
380 se
.Attributes
.Remove ("version");
381 se
.Attributes
.Add ("version", "2");
386 public void FromXml_NoVersion ()
388 UrlIdentityPermission uip
= new UrlIdentityPermission (PermissionState
.None
);
389 SecurityElement se
= uip
.ToXml ();
391 SecurityElement w
= new SecurityElement (se
.Tag
);
392 w
.AddAttribute ("class", se
.Attribute ("class"));