[bcl] Remove more NET_2_0 checks from class libs
[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                 [Test]
55                 public void PermissionState_None ()
56                 {
57                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
58                         // that cause a NullReferenceException before 2.0
59                         Assert.AreEqual (String.Empty, uip.Url, "Url");
60                         SecurityElement se = uip.ToXml ();
61                         // only class and version are present
62                         Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
63                         Assert.IsNull (se.Children, "Xml-Children");
64                         UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
65                         Assert.IsFalse (Object.ReferenceEquals (uip, copy), "ReferenceEquals");
66                 }
67
68
69                 [Test]
70                 [Category ("NotWorking")]
71                 public void PermissionStateUnrestricted ()
72                 {
73                         // In 2.0 Unrestricted are permitted for identity permissions
74                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
75                         Assert.AreEqual (String.Empty, uip.Url, "Url");
76                         SecurityElement se = uip.ToXml ();
77                         // only class and version are present
78                         Assert.AreEqual (3, se.Attributes.Count, "Xml-Attributes");
79                         Assert.IsNull (se.Children, "Xml-Children");
80                         // and they aren't equals to None
81                         Assert.IsFalse (uip.Equals (new UrlIdentityPermission (PermissionState.None)));
82                 }
83                 [Test]
84                 [ExpectedException (typeof (ArgumentException))]
85                 public void PermissionState_Bad ()
86                 {
87                         UrlIdentityPermission uip = new UrlIdentityPermission ((PermissionState)Int32.MinValue);
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (ArgumentNullException))]
92                 public void UrlIdentityPermission_NullUrl ()
93                 {
94                         UrlIdentityPermission uip = new UrlIdentityPermission (null);
95                 }
96
97                 [Test]
98                 public void Url ()
99                 {
100                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
101                         foreach (string s in GoodUrls) {
102                                 uip.Url = s;
103                                 Assert.AreEqual (s, uip.Url, s);
104                         }
105                 }
106
107                 [Test]
108                 public void Url_Null ()
109                 {
110                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
111                         uip.Url = null;
112                         Assert.AreEqual (String.Empty, uip.Url, "Url");
113                 }
114
115                 [Test]
116                 public void Copy ()
117                 {
118                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
119                         foreach (string s in GoodUrls) {
120                                 uip.Url = s;
121                                 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
122                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
123                                 // so we only compare the start of the url
124                                 Assert.AreEqual (uip.Url, copy.Url, "Url");
125                         }
126                 }
127
128                 [Test]
129                 public void Copy_None ()
130                 {
131                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
132                         UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
133                 }
134
135                 [Test]
136                 public void Intersect_Null ()
137                 {
138                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
139                         // No intersection with null
140                         foreach (string s in GoodUrls) {
141                                 uip.Url = s;
142                                 Assert.IsNull (uip.Intersect (null), s);
143                         }
144                 }
145                 [Category ("NotWorking")]
146                 [Test]
147                 public void Intersect_None ()
148                 {
149                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
150                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
151                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
152                         Assert.IsNull (result, "None N None");
153                         foreach (string s in GoodUrls) {
154                                 uip1.Url = s;
155                                 // 1. Intersect None with Url
156                                 result = (UrlIdentityPermission)uip1.Intersect (uip2);
157                                 Assert.IsNull (result, "None N " + s);
158                                 // 2. Intersect Url with None
159                                 result = (UrlIdentityPermission)uip2.Intersect (uip1);
160                                 Assert.IsNull (result, s + "N None");
161                         }
162                 }
163
164                 [Test]
165                 public void Intersect_Self ()
166                 {
167                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
168                         foreach (string s in GoodUrls) {
169                                 uip.Url = s;
170                                 UrlIdentityPermission result = (UrlIdentityPermission)uip.Intersect (uip);
171                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
172                                 // so we only compare the start of the url
173                                 Assert.IsTrue (result.Url.StartsWith (uip.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                 [Category ("NotWorking")]
198                 [Test]
199                 public void IsSubset_None ()
200                 {
201                         // IsSubset with none
202                         // a. source (this) is none -> target is never a subset
203                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
204                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
205                         foreach (string s in GoodUrls) {
206                                 uip1.Url = s;
207                                 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
208                         }
209                         uip1 = new UrlIdentityPermission (PermissionState.None);
210                         // b. destination (target) is none -> target is always a subset
211                         foreach (string s in GoodUrls) {
212                                 uip2.Url = s;
213                                 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
214                         }
215                 }
216
217                 [Test]
218                 public void IsSubset_Self ()
219                 {
220                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
221                         Assert.IsTrue (uip.IsSubsetOf (uip), "None");
222                         foreach (string s in GoodUrls) {
223                                 uip.Url = s;
224                                 Assert.IsTrue (uip.IsSubsetOf (uip), s);
225                         }
226                 }
227
228                 [Test]
229                 public void IsSubset_Different ()
230                 {
231                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
232                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
233                         Assert.IsFalse (uip1.IsSubsetOf (uip2), "Mono subset Novell");
234                         Assert.IsFalse (uip2.IsSubsetOf (uip1), "Novell subset Mono");
235                 }
236
237                 [Test]
238                 public void Union_Null ()
239                 {
240                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
241                         // Union with null is a simple copy
242                         foreach (string s in GoodUrls) {
243                                 uip.Url = s;
244                                 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (null);
245                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
246                                 // so we only compare the start of the url
247                                 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
248                         }
249                 }
250
251                 [Test]
252                 public void Union_None ()
253                 {
254                         // Union with none is same
255                         UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
256                         UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
257                         // a. source (this) is none
258                         foreach (string s in GoodUrls) {
259                                 uip1.Url = s;
260                                 UrlIdentityPermission union = (UrlIdentityPermission)uip1.Union (uip2);
261                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
262                                 // so we only compare the start of the url
263                                 Assert.IsTrue (union.Url.StartsWith (uip1.Url), s);
264                         }
265                         uip1 = new UrlIdentityPermission (PermissionState.None);
266                         // b. destination (target) is none
267                         foreach (string s in GoodUrls) {
268                                 uip2.Url = s;
269                                 UrlIdentityPermission union = (UrlIdentityPermission)uip2.Union (uip1);
270                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
271                                 // so we only compare the start of the url
272                                 Assert.IsTrue (union.Url.StartsWith (uip2.Url), s);
273                         }
274                 }
275
276                 [Test]
277                 public void Union_Self ()
278                 {
279                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
280                         UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (uip);
281                         Assert.IsNull (union, "None U None"); 
282                         foreach (string s in GoodUrls) {
283                                 uip.Url = s;
284                                 union = (UrlIdentityPermission)uip.Union (uip);
285                                 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
286                                 // so we only compare the start of the url
287                                 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
288                         }
289                 }
290                 [Category ("NotWorking")]
291                 [Test]
292                 public void Union_Different ()
293                 {
294                         UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
295                         UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
296                         UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
297                         Assert.IsNotNull (result, "Mono U Novell");
298                         // new XML format is used to contain more than one site
299                         SecurityElement se = result.ToXml ();
300                         Assert.AreEqual (2, se.Children.Count, "Childs");
301                         Assert.AreEqual (GoodUrls [0], (se.Children [0] as SecurityElement).Attribute ("Url"), "Url#1");
302                         Assert.AreEqual (GoodUrls [1], (se.Children [1] as SecurityElement).Attribute ("Url"), "Url#2");
303                         // strangely it is still versioned as 'version="1"'.
304                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
305                 }
306
307                 [Test]
308                 [ExpectedException (typeof (ArgumentNullException))]
309                 public void FromXml_Null ()
310                 {
311                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
312                         uip.FromXml (null);
313                 }
314
315                 [Test]
316                 [ExpectedException (typeof (ArgumentException))]
317                 public void FromXml_WrongTag ()
318                 {
319                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
320                         SecurityElement se = uip.ToXml ();
321                         se.Tag = "IMono";
322                         uip.FromXml (se);
323                 }
324
325                 [Test]
326                 [ExpectedException (typeof (ArgumentException))]
327                 public void FromXml_WrongTagCase ()
328                 {
329                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
330                         SecurityElement se = uip.ToXml ();
331                         se.Tag = "IPERMISSION"; // instead of IPermission
332                         uip.FromXml (se);
333                 }
334
335                 [Test]
336                 public void FromXml_WrongClass ()
337                 {
338                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
339                         SecurityElement se = uip.ToXml ();
340
341                         SecurityElement w = new SecurityElement (se.Tag);
342                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
343                         w.AddAttribute ("version", se.Attribute ("version"));
344                         uip.FromXml (w);
345                         // doesn't care of the class name at that stage
346                         // anyway the class has already be created so...
347                 }
348
349                 [Test]
350                 public void FromXml_NoClass ()
351                 {
352                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
353                         SecurityElement se = uip.ToXml ();
354
355                         SecurityElement w = new SecurityElement (se.Tag);
356                         w.AddAttribute ("version", se.Attribute ("version"));
357                         uip.FromXml (w);
358                         // doesn't even care of the class attribute presence
359                 }
360
361                 [Test]
362                 [ExpectedException (typeof (ArgumentException))]
363                 public void FromXml_WrongVersion ()
364                 {
365                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
366                         SecurityElement se = uip.ToXml ();
367                         se.Attributes.Remove ("version");
368                         se.Attributes.Add ("version", "2");
369                         uip.FromXml (se);
370                 }
371
372                 [Test]
373                 public void FromXml_NoVersion ()
374                 {
375                         UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
376                         SecurityElement se = uip.ToXml ();
377
378                         SecurityElement w = new SecurityElement (se.Tag);
379                         w.AddAttribute ("class", se.Attribute ("class"));
380                         uip.FromXml (w);
381                 }
382         }
383 }