2004-09-01 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                         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");
71
72 //                      UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
73 //                      Assert.IsFalse (Object.ReferenceEquals (uip, copy), "ReferenceEquals");
74                 }
75
76 #if !NET_2_0
77                 [Test]
78                 [ExpectedException (typeof (NullReferenceException))]
79                 public void PermissionState_None_Url ()
80                 {
81                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
82                         Assert.IsNull (uip.Url, "Url");
83                 }
84 #endif
85
86                 [Test]
87                 [ExpectedException (typeof (ArgumentException))]
88                 public void PermissionState_Unrestricted ()
89                 {
90                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof (ArgumentException))]
95                 public void PermissionState_Bad ()
96                 {
97                         UrlIdentityPermission uip = new UrlIdentityPermission ((PermissionState)Int32.MinValue);
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentNullException))]
102                 public void UrlIdentityPermission_NullUrl ()
103                 {
104                         UrlIdentityPermission uip = new UrlIdentityPermission (null);
105                 }
106
107                 [Test]
108                 public void Url ()
109                 {
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
119                         }
120                 }
121
122                 [Test]
123                 [ExpectedException (typeof (ArgumentNullException))]
124                 public void Url_Null ()
125                 {
126                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
127                         uip.Url = null;
128                 }
129
130                 [Test]
131                 public void Copy ()
132                 {
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");
140                         }
141                 }
142
143                 [Test]
144                 public void Intersect_Null ()
145                 {
146                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
147                         // No intersection with null
148                         foreach (string s in GoodUrls) {
149                                 uip.Url = s;
150                                 Assert.IsNull (uip.Intersect (null), s);
151                         }
152                 }
153
154                 [Test]
155                 public void Intersect_None ()
156                 {
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) {
162                                 uip1.Url = s;
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");
169                         }
170                 }
171
172                 [Test]
173                 public void Intersect_Self ()
174                 {
175                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
176                         foreach (string s in GoodUrls) {
177                                 uip.Url = s;
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);
182                         }
183                 }
184
185                 [Test]
186                 public void Intersect_Different ()
187                 {
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");
192                 }
193
194                 [Test]
195                 public void IsSubset_Null ()
196                 {
197                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
198                         Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
199                         foreach (string s in GoodUrls) {
200                                 uip.Url = s;
201                                 Assert.IsFalse (uip.IsSubsetOf (null), s);
202                         }
203                 }
204
205                 [Test]
206                 public void IsSubset_None ()
207                 {
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) {
213                                 uip1.Url = s;
214                                 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
215                         }
216                         uip1 = new UrlIdentityPermission (PermissionState.None);
217                         // b. destination (target) is none -> target is always a subset
218                         foreach (string s in GoodUrls) {
219                                 uip2.Url = s;
220                                 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
221                         }
222                 }
223
224                 [Test]
225                 public void IsSubset_Self ()
226                 {
227                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
228                         Assert.IsTrue (uip.IsSubsetOf (uip), "None");
229                         foreach (string s in GoodUrls) {
230                                 uip.Url = s;
231                                 Assert.IsTrue (uip.IsSubsetOf (uip), s);
232                         }
233                 }
234
235                 [Test]
236                 public void IsSubset_Different ()
237                 {
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");
242                 }
243
244                 [Test]
245                 public void Union_Null ()
246                 {
247                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
248                         // Union with null is a simple copy
249                         foreach (string s in GoodUrls) {
250                                 uip.Url = s;
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);
255                         }
256                 }
257
258                 [Test]
259                 public void Union_None ()
260                 {
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) {
266                                 uip1.Url = s;
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);
271                         }
272                         uip1 = new UrlIdentityPermission (PermissionState.None);
273                         // b. destination (target) is none
274                         foreach (string s in GoodUrls) {
275                                 uip2.Url = s;
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);
280                         }
281                 }
282
283                 [Test]
284                 public void Union_Self ()
285                 {
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) {
290                                 uip.Url = s;
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);
295                         }
296                 }
297
298                 [Test]
299 #if NET_2_0
300                 [ExpectedException (typeof (ArgumentException))]
301 #endif
302                 public void Union_Different ()
303                 {
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");
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (ArgumentNullException))]
312                 public void FromXml_Null ()
313                 {
314                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
315                         uip.FromXml (null);
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (ArgumentException))]
320                 public void FromXml_WrongTag ()
321                 {
322                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
323                         SecurityElement se = uip.ToXml ();
324                         se.Tag = "IMono";
325                         uip.FromXml (se);
326                 }
327
328                 [Test]
329                 [ExpectedException (typeof (ArgumentException))]
330                 public void FromXml_WrongTagCase ()
331                 {
332                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
333                         SecurityElement se = uip.ToXml ();
334                         se.Tag = "IPERMISSION"; // instead of IPermission
335                         uip.FromXml (se);
336                 }
337
338                 [Test]
339                 public void FromXml_WrongClass ()
340                 {
341                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
342                         SecurityElement se = uip.ToXml ();
343
344                         SecurityElement w = new SecurityElement (se.Tag);
345                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
346                         w.AddAttribute ("version", se.Attribute ("version"));
347                         uip.FromXml (w);
348                         // doesn't care of the class name at that stage
349                         // anyway the class has already be created so...
350                 }
351
352                 [Test]
353                 public void FromXml_NoClass ()
354                 {
355                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
356                         SecurityElement se = uip.ToXml ();
357
358                         SecurityElement w = new SecurityElement (se.Tag);
359                         w.AddAttribute ("version", se.Attribute ("version"));
360                         uip.FromXml (w);
361                         // doesn't even care of the class attribute presence
362                 }
363
364                 [Test]
365                 [ExpectedException (typeof (ArgumentException))]
366                 public void FromXml_WrongVersion ()
367                 {
368                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
369                         SecurityElement se = uip.ToXml ();
370                         se.Attributes.Remove ("version");
371                         se.Attributes.Add ("version", "2");
372                         uip.FromXml (se);
373                 }
374
375                 [Test]
376                 public void FromXml_NoVersion ()
377                 {
378                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
379                         SecurityElement se = uip.ToXml ();
380
381                         SecurityElement w = new SecurityElement (se.Tag);
382                         w.AddAttribute ("class", se.Attribute ("class"));
383                         uip.FromXml (w);
384                 }
385         }
386 }