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                                 Assert.AreEqual (s, uip.Url, s);
114                         }
115                 }
116
117                 [Test]
118                 [ExpectedException (typeof (ArgumentNullException))]
119                 public void Url_Null ()
120                 {
121                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
122                         uip.Url = null;
123                 }
124
125                 [Test]
126                 public void Copy ()
127                 {
128                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
129                         foreach (string s in GoodUrls)
130                         {
131                                 uip.Url = s;
132                                 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
133                                 Assert.AreEqual (s, copy.Url, s);
134                         }
135                 }
136
137                 [Test]
138                 public void Intersect_Null ()
139                 {
140                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
141                         // No intersection with null
142                         foreach (string s in GoodUrls) {
143                                 uip.Url = s;
144                                 Assert.IsNull (uip.Intersect (null), s);
145                         }
146                 }
147
148                 [Test]
149                 public void Intersect_None ()
150                 {
151                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
152                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
153                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
154                         Assert.IsNull (result, "None N None");
155                         foreach (string s in GoodUrls) {
156                                 uip1.Url = s;
157                                 // 1. Intersect None with Url
158                                 result = (UrlIdentityPermission)uip1.Intersect (uip2);
159                                 Assert.IsNull (result, "None N " + s);
160                                 // 2. Intersect Url with None
161                                 result = (UrlIdentityPermission)uip2.Intersect (uip1);
162                                 Assert.IsNull (result, s + "N None");
163                         }
164                 }
165
166                 [Test]
167                 public void Intersect_Self ()
168                 {
169                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
170                         foreach (string s in GoodUrls) {
171                                 uip.Url = s;
172                                 UrlIdentityPermission result = (UrlIdentityPermission)uip.Intersect (uip);
173                                 Assert.AreEqual (s, result.Url, s);
174                         }
175                 }
176
177                 [Test]
178                 public void Intersect_Different ()
179                 {
180                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
181                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
182                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
183                         Assert.IsNull (result, "Mono N Novell");
184                 }
185
186                 [Test]
187                 public void IsSubset_Null ()
188                 {
189                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
190                         Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
191                         foreach (string s in GoodUrls) {
192                                 uip.Url = s;
193                                 Assert.IsFalse (uip.IsSubsetOf (null), s);
194                         }
195                 }
196
197                 [Test]
198                 public void IsSubset_None ()
199                 {
200                         // IsSubset with none
201                         // a. source (this) is none -> target is never a subset
202                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
203                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
204                         foreach (string s in GoodUrls) {
205                                 uip1.Url = s;
206                                 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
207                         }
208                         uip1 = new UrlIdentityPermission (PermissionState.None);
209                         // b. destination (target) is none -> target is always a subset
210                         foreach (string s in GoodUrls) {
211                                 uip2.Url = s;
212                                 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
213                         }
214                 }
215
216                 [Test]
217                 public void IsSubset_Self ()
218                 {
219                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
220                         Assert.IsTrue (uip.IsSubsetOf (uip), "None");
221                         foreach (string s in GoodUrls) {
222                                 uip.Url = s;
223                                 Assert.IsTrue (uip.IsSubsetOf (uip), s);
224                         }
225                 }
226
227                 [Test]
228                 public void IsSubset_Different ()
229                 {
230                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
231                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
232                         Assert.IsFalse (uip1.IsSubsetOf (uip2), "Mono subset Novell");
233                         Assert.IsFalse (uip2.IsSubsetOf (uip1), "Novell subset Mono");
234                 }
235
236                 [Test]
237                 public void Union_Null ()
238                 {
239                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
240                         // Union with null is a simple copy
241                         foreach (string s in GoodUrls) {
242                                 uip.Url = s;
243                                 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (null);
244                                 Assert.AreEqual (s, union.Url, s);
245                         }
246                 }
247
248                 [Test]
249                 public void Union_None ()
250                 {
251                         // Union with none is same
252                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
253                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
254                         // a. source (this) is none
255                         foreach (string s in GoodUrls) {
256                                 uip1.Url = s;
257                                 UrlIdentityPermission union = (UrlIdentityPermission)uip1.Union (uip2);
258                                 Assert.AreEqual (s, union.Url, s);
259                         }
260                         uip1 = new UrlIdentityPermission (PermissionState.None);
261                         // b. destination (target) is none
262                         foreach (string s in GoodUrls) {
263                                 uip2.Url = s;
264                                 UrlIdentityPermission union = (UrlIdentityPermission)uip2.Union (uip1);
265                                 Assert.AreEqual (s, union.Url, s);
266                         }
267                 }
268
269                 [Test]
270                 public void Union_Self ()
271                 {
272                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
273                         UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (uip);
274                         Assert.IsNull (union, "None U None"); 
275                         foreach (string s in GoodUrls) {
276                                 uip.Url = s;
277                                 union = (UrlIdentityPermission)uip.Union (uip);
278                                 Assert.AreEqual (s, union.Url, s);
279                         }
280                 }
281
282                 [Test]
283                 [ExpectedException (typeof (ArgumentException))]
284                 public void Union_Different ()
285                 {
286                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
287                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
288                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
289                         Assert.IsNull (result, "Mono U Novell");
290                 }
291
292                 [Test]
293                 [ExpectedException (typeof (ArgumentNullException))]
294                 public void FromXml_Null ()
295                 {
296                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
297                         uip.FromXml (null);
298                 }
299
300                 [Test]
301                 [ExpectedException (typeof (ArgumentException))]
302                 public void FromXml_WrongTag ()
303                 {
304                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
305                         SecurityElement se = uip.ToXml ();
306                         se.Tag = "IMono";
307                         uip.FromXml (se);
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (ArgumentException))]
312                 public void FromXml_WrongTagCase ()
313                 {
314                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
315                         SecurityElement se = uip.ToXml ();
316                         se.Tag = "IPERMISSION"; // instead of IPermission
317                         uip.FromXml (se);
318                 }
319
320                 [Test]
321                 public void FromXml_WrongClass ()
322                 {
323                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
324                         SecurityElement se = uip.ToXml ();
325
326                         SecurityElement w = new SecurityElement (se.Tag);
327                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
328                         w.AddAttribute ("version", se.Attribute ("version"));
329                         uip.FromXml (w);
330                         // doesn't care of the class name at that stage
331                         // anyway the class has already be created so...
332                 }
333
334                 [Test]
335                 public void FromXml_NoClass ()
336                 {
337                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
338                         SecurityElement se = uip.ToXml ();
339
340                         SecurityElement w = new SecurityElement (se.Tag);
341                         w.AddAttribute ("version", se.Attribute ("version"));
342                         uip.FromXml (w);
343                         // doesn't even care of the class attribute presence
344                 }
345
346                 [Test]
347                 [ExpectedException (typeof (ArgumentException))]
348                 public void FromXml_WrongVersion ()
349                 {
350                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
351                         SecurityElement se = uip.ToXml ();
352                         se.Attributes.Remove ("version");
353                         se.Attributes.Add ("version", "2");
354                         uip.FromXml (se);
355                 }
356
357                 [Test]
358                 public void FromXml_NoVersion ()
359                 {
360                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
361                         SecurityElement se = uip.ToXml ();
362
363                         SecurityElement w = new SecurityElement (se.Tag);
364                         w.AddAttribute ("class", se.Attribute ("class"));
365                         uip.FromXml (w);
366                 }
367         }
368 }