(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.Security.Permissions / UrlIdentityPermissionTest.cs
blobd5b1cc8e573fd101e53c53666013b8a32dfa0dff
1 //
2 // UrlIdentityPermissionTest.cs - NUnit Test Cases for UrlIdentityPermission
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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;
30 using System;
31 using System.Security;
32 using System.Security.Permissions;
34 using System.Diagnostics;
36 namespace MonoTests.System.Security.Permissions {
38 [TestFixture]
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",
46 "www.novell.com",
47 "*.mono-project.com",
48 "*www.mono-project.com",
49 "*-project.com",
50 "*.com",
51 "*",
54 // accepted as Url but fails to work (as expected) in some methods
55 static string[] SemiBadUrls = {
56 "www.mono-project.com:80",
57 String.Empty,
60 [Test]
61 public void PermissionState_None ()
63 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
64 #if NET_2_0
65 Assert.AreEqual (String.Empty, uip.Url, "Url");
66 #endif
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");
76 #if !NET_2_0
77 [Test]
78 [ExpectedException (typeof (NullReferenceException))]
79 public void PermissionState_None_Url ()
81 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
82 Assert.IsNull (uip.Url, "Url");
84 #endif
86 [Test]
87 [ExpectedException (typeof (ArgumentException))]
88 public void PermissionState_Unrestricted ()
90 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
93 [Test]
94 [ExpectedException (typeof (ArgumentException))]
95 public void PermissionState_Bad ()
97 UrlIdentityPermission uip = new UrlIdentityPermission ((PermissionState)Int32.MinValue);
100 [Test]
101 [ExpectedException (typeof (ArgumentNullException))]
102 public void UrlIdentityPermission_NullUrl ()
104 UrlIdentityPermission uip = new UrlIdentityPermission (null);
107 [Test]
108 public void Url ()
110 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
111 foreach (string s in GoodUrls) {
112 uip.Url = s;
113 #if NET_2_0
114 Assert.AreEqual (s, uip.Url, s);
115 #else
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
118 #endif
122 [Test]
123 [ExpectedException (typeof (ArgumentNullException))]
124 public void Url_Null ()
126 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
127 uip.Url = null;
130 [Test]
131 public void Copy ()
133 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
134 foreach (string s in GoodUrls) {
135 uip.Url = s;
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");
143 [Test]
144 #if !NET_2_0
145 [ExpectedException (typeof (NullReferenceException))]
146 #endif
147 public void Copy_None ()
149 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
150 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
153 [Test]
154 public void Intersect_Null ()
156 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
157 // No intersection with null
158 foreach (string s in GoodUrls) {
159 uip.Url = s;
160 Assert.IsNull (uip.Intersect (null), s);
164 [Test]
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) {
172 uip1.Url = s;
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");
182 [Test]
183 public void Intersect_Self ()
185 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
186 foreach (string s in GoodUrls) {
187 uip.Url = s;
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);
195 [Test]
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");
204 [Test]
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) {
210 uip.Url = s;
211 Assert.IsFalse (uip.IsSubsetOf (null), s);
215 [Test]
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) {
223 uip1.Url = s;
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) {
229 uip2.Url = s;
230 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
234 [Test]
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) {
240 uip.Url = s;
241 Assert.IsTrue (uip.IsSubsetOf (uip), s);
245 [Test]
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");
254 [Test]
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) {
260 uip.Url = s;
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);
268 [Test]
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) {
276 uip1.Url = s;
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) {
285 uip2.Url = s;
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);
293 [Test]
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) {
300 uip.Url = s;
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);
308 [Test]
309 #if NET_2_0
310 [ExpectedException (typeof (ArgumentException))]
311 #endif
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");
320 [Test]
321 [ExpectedException (typeof (ArgumentNullException))]
322 public void FromXml_Null ()
324 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
325 uip.FromXml (null);
328 [Test]
329 [ExpectedException (typeof (ArgumentException))]
330 public void FromXml_WrongTag ()
332 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
333 SecurityElement se = uip.ToXml ();
334 se.Tag = "IMono";
335 uip.FromXml (se);
338 [Test]
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
345 uip.FromXml (se);
348 [Test]
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"));
357 uip.FromXml (w);
358 // doesn't care of the class name at that stage
359 // anyway the class has already be created so...
362 [Test]
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"));
370 uip.FromXml (w);
371 // doesn't even care of the class attribute presence
374 [Test]
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");
382 uip.FromXml (se);
385 [Test]
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"));
393 uip.FromXml (w);