[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / UrlIdentityPermissionTest.cs
1 //
2 // UrlIdentityPermissionTest.cs - NUnit Test Cases for UrlIdentityPermission
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 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.
27 //
28
29 using NUnit.Framework;
30 using System;
31 using System.Security;
32 using System.Security.Permissions;
33
34 using System.Diagnostics;
35
36 namespace MonoTests.System.Security.Permissions {
37
38         [TestFixture]
39         public class UrlIdentityPermissionTest {
40
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                         "*",
52                 };
53
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,
58                 };
59
60                 [Test]
61                 public void PermissionState_None ()
62                 {
63                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
64 #if NET_2_0
65                         // that cause a NullReferenceException before 2.0
66                         Assert.AreEqual (String.Empty, uip.Url, "Url");
67 #endif
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");
72 #if NET_2_0
73                         UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
74                         Assert.IsFalse (Object.ReferenceEquals (uip, copy), "ReferenceEquals");
75 #endif
76                 }
77
78 #if !NET_2_0
79                 [Test]
80                 [ExpectedException (typeof (NullReferenceException))]
81                 public void PermissionState_None_Url ()
82                 {
83                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
84                         Assert.IsNull (uip.Url, "Url");
85                 }
86 #endif
87
88 #if NET_2_0
89                 [Test]
90                 [Category ("NotWorking")]
91                 public void PermissionStateUnrestricted ()
92                 {
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)));
102                 }
103 #else
104                 [Test]
105                 [ExpectedException (typeof (ArgumentException))]
106                 public void PermissionState_Unrestricted ()
107                 {
108                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
109                 }
110 #endif
111                 [Test]
112                 [ExpectedException (typeof (ArgumentException))]
113                 public void PermissionState_Bad ()
114                 {
115                         UrlIdentityPermission uip = new UrlIdentityPermission ((PermissionState)Int32.MinValue);
116                 }
117
118                 [Test]
119                 [ExpectedException (typeof (ArgumentNullException))]
120                 public void UrlIdentityPermission_NullUrl ()
121                 {
122                         UrlIdentityPermission uip = new UrlIdentityPermission (null);
123                 }
124
125                 [Test]
126                 public void Url ()
127                 {
128                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
129                         foreach (string s in GoodUrls) {
130                                 uip.Url = s;
131 #if NET_2_0
132                                 Assert.AreEqual (s, uip.Url, s);
133 #else
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
136 #endif
137                         }
138                 }
139
140 #if NET_2_0
141                 [Test]
142                 public void Url_Null ()
143                 {
144                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
145                         uip.Url = null;
146                         Assert.AreEqual (String.Empty, uip.Url, "Url");
147                 }
148 #else
149                 [Test]
150                 [ExpectedException (typeof (ArgumentNullException))]
151                 public void Url_Null ()
152                 {
153                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
154                         uip.Url = null;
155                 }
156 #endif
157
158                 [Test]
159                 public void Copy ()
160                 {
161                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
162                         foreach (string s in GoodUrls) {
163                                 uip.Url = s;
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");
168                         }
169                 }
170
171                 [Test]
172 #if !NET_2_0
173                 [ExpectedException (typeof (NullReferenceException))]
174 #endif
175                 public void Copy_None ()
176                 {
177                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
178                         UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
179                 }
180
181                 [Test]
182                 public void Intersect_Null ()
183                 {
184                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
185                         // No intersection with null
186                         foreach (string s in GoodUrls) {
187                                 uip.Url = s;
188                                 Assert.IsNull (uip.Intersect (null), s);
189                         }
190                 }
191 #if NET_2_0
192                 [Category ("NotWorking")]
193 #endif
194                 [Test]
195                 public void Intersect_None ()
196                 {
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) {
202                                 uip1.Url = s;
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");
209                         }
210                 }
211
212                 [Test]
213                 public void Intersect_Self ()
214                 {
215                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
216                         foreach (string s in GoodUrls) {
217                                 uip.Url = s;
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);
222                         }
223                 }
224
225                 [Test]
226                 public void Intersect_Different ()
227                 {
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");
232                 }
233
234                 [Test]
235                 public void IsSubset_Null ()
236                 {
237                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
238                         Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
239                         foreach (string s in GoodUrls) {
240                                 uip.Url = s;
241                                 Assert.IsFalse (uip.IsSubsetOf (null), s);
242                         }
243                 }
244
245 #if NET_2_0
246                 [Category ("NotWorking")]
247 #endif
248                 [Test]
249                 public void IsSubset_None ()
250                 {
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) {
256                                 uip1.Url = s;
257                                 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
258                         }
259                         uip1 = new UrlIdentityPermission (PermissionState.None);
260                         // b. destination (target) is none -> target is always a subset
261                         foreach (string s in GoodUrls) {
262                                 uip2.Url = s;
263                                 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
264                         }
265                 }
266
267                 [Test]
268                 public void IsSubset_Self ()
269                 {
270                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
271                         Assert.IsTrue (uip.IsSubsetOf (uip), "None");
272                         foreach (string s in GoodUrls) {
273                                 uip.Url = s;
274                                 Assert.IsTrue (uip.IsSubsetOf (uip), s);
275                         }
276                 }
277
278                 [Test]
279                 public void IsSubset_Different ()
280                 {
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");
285                 }
286
287                 [Test]
288                 public void Union_Null ()
289                 {
290                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
291                         // Union with null is a simple copy
292                         foreach (string s in GoodUrls) {
293                                 uip.Url = s;
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);
298                         }
299                 }
300
301                 [Test]
302                 public void Union_None ()
303                 {
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) {
309                                 uip1.Url = s;
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);
314                         }
315                         uip1 = new UrlIdentityPermission (PermissionState.None);
316                         // b. destination (target) is none
317                         foreach (string s in GoodUrls) {
318                                 uip2.Url = s;
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);
323                         }
324                 }
325
326                 [Test]
327                 public void Union_Self ()
328                 {
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) {
333                                 uip.Url = s;
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);
338                         }
339                 }
340 #if NET_2_0
341                 [Category ("NotWorking")]
342 #endif
343                 [Test]
344                 public void Union_Different ()
345                 {
346                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
347                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
348                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
349 #if NET_2_0
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");
358 #else
359                         Assert.IsNull (result, "Mono U Novell");
360 #endif
361                 }
362
363                 [Test]
364                 [ExpectedException (typeof (ArgumentNullException))]
365                 public void FromXml_Null ()
366                 {
367                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
368                         uip.FromXml (null);
369                 }
370
371                 [Test]
372                 [ExpectedException (typeof (ArgumentException))]
373                 public void FromXml_WrongTag ()
374                 {
375                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
376                         SecurityElement se = uip.ToXml ();
377                         se.Tag = "IMono";
378                         uip.FromXml (se);
379                 }
380
381                 [Test]
382                 [ExpectedException (typeof (ArgumentException))]
383                 public void FromXml_WrongTagCase ()
384                 {
385                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
386                         SecurityElement se = uip.ToXml ();
387                         se.Tag = "IPERMISSION"; // instead of IPermission
388                         uip.FromXml (se);
389                 }
390
391                 [Test]
392                 public void FromXml_WrongClass ()
393                 {
394                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
395                         SecurityElement se = uip.ToXml ();
396
397                         SecurityElement w = new SecurityElement (se.Tag);
398                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
399                         w.AddAttribute ("version", se.Attribute ("version"));
400                         uip.FromXml (w);
401                         // doesn't care of the class name at that stage
402                         // anyway the class has already be created so...
403                 }
404
405                 [Test]
406                 public void FromXml_NoClass ()
407                 {
408                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
409                         SecurityElement se = uip.ToXml ();
410
411                         SecurityElement w = new SecurityElement (se.Tag);
412                         w.AddAttribute ("version", se.Attribute ("version"));
413                         uip.FromXml (w);
414                         // doesn't even care of the class attribute presence
415                 }
416
417                 [Test]
418                 [ExpectedException (typeof (ArgumentException))]
419                 public void FromXml_WrongVersion ()
420                 {
421                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
422                         SecurityElement se = uip.ToXml ();
423                         se.Attributes.Remove ("version");
424                         se.Attributes.Add ("version", "2");
425                         uip.FromXml (se);
426                 }
427
428                 [Test]
429                 public void FromXml_NoVersion ()
430                 {
431                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
432                         SecurityElement se = uip.ToXml ();
433
434                         SecurityElement w = new SecurityElement (se.Tag);
435                         w.AddAttribute ("class", se.Attribute ("class"));
436                         uip.FromXml (w);
437                 }
438         }
439 }