2005-03-31 Sebastien Pouliot <sebastien@ximian.com>
[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 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                 [Test]
141 #if NET_2_0
142                 // this was working in beta1 but is broken in Nov CTP
143                 [ExpectedException (typeof (NullReferenceException))]
144                 [Category ("NotWorking")]
145 #else
146                 [ExpectedException (typeof (ArgumentNullException))]
147 #endif
148                 public void Url_Null ()
149                 {
150                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
151                         uip.Url = null;
152                 }
153
154                 [Test]
155                 public void Copy ()
156                 {
157                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
158                         foreach (string s in GoodUrls) {
159                                 uip.Url = s;
160                                 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
161                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
162                                 // so we only compare the start of the url
163                                 Assert.AreEqual (uip.Url, copy.Url, "Url");
164                         }
165                 }
166
167                 [Test]
168 #if !NET_2_0
169                 [ExpectedException (typeof (NullReferenceException))]
170 #endif
171                 public void Copy_None ()
172                 {
173                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
174                         UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
175                 }
176
177                 [Test]
178                 public void Intersect_Null ()
179                 {
180                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
181                         // No intersection with null
182                         foreach (string s in GoodUrls) {
183                                 uip.Url = s;
184                                 Assert.IsNull (uip.Intersect (null), s);
185                         }
186                 }
187 #if NET_2_0
188                 [Category ("NotWorking")]
189 #endif
190                 [Test]
191                 public void Intersect_None ()
192                 {
193                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
194                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
195                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
196                         Assert.IsNull (result, "None N None");
197                         foreach (string s in GoodUrls) {
198                                 uip1.Url = s;
199                                 // 1. Intersect None with Url
200                                 result = (UrlIdentityPermission)uip1.Intersect (uip2);
201                                 Assert.IsNull (result, "None N " + s);
202                                 // 2. Intersect Url with None
203                                 result = (UrlIdentityPermission)uip2.Intersect (uip1);
204                                 Assert.IsNull (result, s + "N None");
205                         }
206                 }
207
208                 [Test]
209                 public void Intersect_Self ()
210                 {
211                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
212                         foreach (string s in GoodUrls) {
213                                 uip.Url = s;
214                                 UrlIdentityPermission result = (UrlIdentityPermission)uip.Intersect (uip);
215                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
216                                 // so we only compare the start of the url
217                                 Assert.IsTrue (result.Url.StartsWith (uip.Url), s);
218                         }
219                 }
220
221                 [Test]
222                 public void Intersect_Different ()
223                 {
224                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
225                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
226                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
227                         Assert.IsNull (result, "Mono N Novell");
228                 }
229
230                 [Test]
231                 public void IsSubset_Null ()
232                 {
233                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
234                         Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
235                         foreach (string s in GoodUrls) {
236                                 uip.Url = s;
237                                 Assert.IsFalse (uip.IsSubsetOf (null), s);
238                         }
239                 }
240
241 #if NET_2_0
242                 [Category ("NotWorking")]
243 #endif
244                 [Test]
245                 public void IsSubset_None ()
246                 {
247                         // IsSubset with none
248                         // a. source (this) is none -> target is never a subset
249                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
250                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
251                         foreach (string s in GoodUrls) {
252                                 uip1.Url = s;
253                                 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
254                         }
255                         uip1 = new UrlIdentityPermission (PermissionState.None);
256                         // b. destination (target) is none -> target is always a subset
257                         foreach (string s in GoodUrls) {
258                                 uip2.Url = s;
259                                 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
260                         }
261                 }
262
263                 [Test]
264                 public void IsSubset_Self ()
265                 {
266                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
267                         Assert.IsTrue (uip.IsSubsetOf (uip), "None");
268                         foreach (string s in GoodUrls) {
269                                 uip.Url = s;
270                                 Assert.IsTrue (uip.IsSubsetOf (uip), s);
271                         }
272                 }
273
274                 [Test]
275                 public void IsSubset_Different ()
276                 {
277                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
278                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
279                         Assert.IsFalse (uip1.IsSubsetOf (uip2), "Mono subset Novell");
280                         Assert.IsFalse (uip2.IsSubsetOf (uip1), "Novell subset Mono");
281                 }
282
283                 [Test]
284                 public void Union_Null ()
285                 {
286                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
287                         // Union with null is a simple copy
288                         foreach (string s in GoodUrls) {
289                                 uip.Url = s;
290                                 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (null);
291                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
292                                 // so we only compare the start of the url
293                                 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
294                         }
295                 }
296
297                 [Test]
298                 public void Union_None ()
299                 {
300                         // Union with none is same
301                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
302                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
303                         // a. source (this) is none
304                         foreach (string s in GoodUrls) {
305                                 uip1.Url = s;
306                                 UrlIdentityPermission union = (UrlIdentityPermission)uip1.Union (uip2);
307                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
308                                 // so we only compare the start of the url
309                                 Assert.IsTrue (union.Url.StartsWith (uip1.Url), s);
310                         }
311                         uip1 = new UrlIdentityPermission (PermissionState.None);
312                         // b. destination (target) is none
313                         foreach (string s in GoodUrls) {
314                                 uip2.Url = s;
315                                 UrlIdentityPermission union = (UrlIdentityPermission)uip2.Union (uip1);
316                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
317                                 // so we only compare the start of the url
318                                 Assert.IsTrue (union.Url.StartsWith (uip2.Url), s);
319                         }
320                 }
321
322                 [Test]
323                 public void Union_Self ()
324                 {
325                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
326                         UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (uip);
327                         Assert.IsNull (union, "None U None"); 
328                         foreach (string s in GoodUrls) {
329                                 uip.Url = s;
330                                 union = (UrlIdentityPermission)uip.Union (uip);
331                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
332                                 // so we only compare the start of the url
333                                 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
334                         }
335                 }
336 #if NET_2_0
337                 [Category ("NotWorking")]
338 #endif
339                 [Test]
340                 public void Union_Different ()
341                 {
342                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
343                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
344                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
345 #if NET_2_0
346                         Assert.IsNotNull (result, "Mono U Novell");
347                         // new XML format is used to contain more than one site
348                         SecurityElement se = result.ToXml ();
349                         Assert.AreEqual (2, se.Children.Count, "Childs");
350                         Assert.AreEqual (GoodUrls [0], (se.Children [0] as SecurityElement).Attribute ("Url"), "Url#1");
351                         Assert.AreEqual (GoodUrls [1], (se.Children [1] as SecurityElement).Attribute ("Url"), "Url#2");
352                         // strangely it is still versioned as 'version="1"'.
353                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
354 #else
355                         Assert.IsNull (result, "Mono U Novell");
356 #endif
357                 }
358
359                 [Test]
360                 [ExpectedException (typeof (ArgumentNullException))]
361                 public void FromXml_Null ()
362                 {
363                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
364                         uip.FromXml (null);
365                 }
366
367                 [Test]
368                 [ExpectedException (typeof (ArgumentException))]
369                 public void FromXml_WrongTag ()
370                 {
371                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
372                         SecurityElement se = uip.ToXml ();
373                         se.Tag = "IMono";
374                         uip.FromXml (se);
375                 }
376
377                 [Test]
378                 [ExpectedException (typeof (ArgumentException))]
379                 public void FromXml_WrongTagCase ()
380                 {
381                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
382                         SecurityElement se = uip.ToXml ();
383                         se.Tag = "IPERMISSION"; // instead of IPermission
384                         uip.FromXml (se);
385                 }
386
387                 [Test]
388                 public void FromXml_WrongClass ()
389                 {
390                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
391                         SecurityElement se = uip.ToXml ();
392
393                         SecurityElement w = new SecurityElement (se.Tag);
394                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
395                         w.AddAttribute ("version", se.Attribute ("version"));
396                         uip.FromXml (w);
397                         // doesn't care of the class name at that stage
398                         // anyway the class has already be created so...
399                 }
400
401                 [Test]
402                 public void FromXml_NoClass ()
403                 {
404                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
405                         SecurityElement se = uip.ToXml ();
406
407                         SecurityElement w = new SecurityElement (se.Tag);
408                         w.AddAttribute ("version", se.Attribute ("version"));
409                         uip.FromXml (w);
410                         // doesn't even care of the class attribute presence
411                 }
412
413                 [Test]
414                 [ExpectedException (typeof (ArgumentException))]
415                 public void FromXml_WrongVersion ()
416                 {
417                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
418                         SecurityElement se = uip.ToXml ();
419                         se.Attributes.Remove ("version");
420                         se.Attributes.Add ("version", "2");
421                         uip.FromXml (se);
422                 }
423
424                 [Test]
425                 public void FromXml_NoVersion ()
426                 {
427                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
428                         SecurityElement se = uip.ToXml ();
429
430                         SecurityElement w = new SecurityElement (se.Tag);
431                         w.AddAttribute ("class", se.Attribute ("class"));
432                         uip.FromXml (w);
433                 }
434         }
435 }