New test.
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / SiteIdentityPermissionTest.cs
1 //
2 // SiteIdentityPermissionTest.cs - NUnit Test Cases for SiteIdentityPermission
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 SiteIdentityPermissionTest {
40
41                 static string[] GoodSites = {
42                         "www.mono-project.com",
43                         "www.novell.com",
44                         "*.mono-project.com",
45                         "*.com",
46                         "*",
47                 };
48
49                 static string[] BadSites = {
50                         "http://www.mono-project.com:80/",
51                         "http://www.mono-project.com/",
52                         "http://www.mono-project.com",
53                         "www.mono-project.com:80",
54                         "*www.mono-project.com",
55                         "*-project.com",
56                         "www.*.com",
57                 };
58
59 #if NET_2_0
60                 [Category ("NotWorking")]
61 #endif
62                 [Test]
63                 public void PermissionState_None ()
64                 {
65                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
66 #if NET_2_0
67                         Assert.AreEqual (String.Empty, sip.Site, "Site");
68 #endif
69                         SecurityElement se = sip.ToXml ();
70                         // only class and version are present
71                         Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
72                         Assert.IsNull (se.Children, "Xml-Children");
73                         SiteIdentityPermission copy = (SiteIdentityPermission)sip.Copy ();
74                         Assert.IsFalse (Object.ReferenceEquals (sip, copy), "ReferenceEquals");
75                 }
76
77 #if NET_2_0
78                 [Category ("NotWorking")]
79                 [Test]
80                 public void PermissionStateUnrestricted ()
81                 {
82                         // In 2.0 Unrestricted are permitted for identity permissions
83                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.Unrestricted);
84                         Assert.AreEqual (String.Empty, sip.Site, "Site");
85                         SecurityElement se = sip.ToXml ();
86                         Assert.AreEqual (3, se.Attributes.Count, "Xml-Attributes");
87                         Assert.IsNull (se.Children, "Xml-Children");
88                         SiteIdentityPermission copy = (SiteIdentityPermission)sip.Copy ();
89                         Assert.IsFalse (Object.ReferenceEquals (sip, copy), "ReferenceEquals");
90                         // and they aren't equals to None
91                         Assert.IsFalse (sip.Equals (new SiteIdentityPermission (PermissionState.None)));
92                 }
93 #else
94                 [Test]
95                 [ExpectedException (typeof (ArgumentException))]
96                 public void PermissionState_Unrestricted ()
97                 {
98                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.Unrestricted);
99                 }
100 #endif
101                 [Test]
102                 [ExpectedException (typeof (ArgumentException))]
103                 public void PermissionState_Bad ()
104                 {
105                         SiteIdentityPermission sip = new SiteIdentityPermission ((PermissionState)Int32.MinValue);
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentException))]
110                 public void SiteIdentityPermission_NullSite ()
111                 {
112                         SiteIdentityPermission sip = new SiteIdentityPermission (null);
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (ArgumentException))]
117                 public void SiteIdentityPermission_EmptySite ()
118                 {
119                         SiteIdentityPermission sip = new SiteIdentityPermission (String.Empty);
120                 }
121
122                 [Test]
123                 public void Site ()
124                 {
125                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
126                         foreach (string s in GoodSites) {
127                                 sip.Site = s;
128                                 Assert.AreEqual (s, sip.Site, s);
129                         }
130                 }
131
132                 [Test]
133                 public void Site_Bad ()
134                 {
135                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
136                         foreach (string s in BadSites) {
137                                 try {
138                                         sip.Site = s;
139                                         Assert.Fail (s + " isn't a bad site!");
140                                 }
141                                 catch (ArgumentException) {
142                                         // expected, continue looping
143                                 }
144                         }
145                 }
146
147 /*              [Test]
148                 public void Site_InvalidChars ()
149                 {
150                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
151                         for (int i=0; i < 256; i++) {
152                                 try {
153                                         sip.Site = String.Empty + (char) i;
154                                 }
155                                 catch (ArgumentException) {
156                                         Console.WriteLine ("{0} is bad", i);
157                                 }
158                         }
159                 }*/
160
161                 [Test]
162                 [ExpectedException (typeof (ArgumentException))]
163                 public void Site_Null ()
164                 {
165                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
166                         sip.Site = null;
167                 }
168
169                 [Test]
170                 public void Copy ()
171                 {
172                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
173                         foreach (string s in GoodSites) {
174                                 sip.Site = s;
175                                 SiteIdentityPermission copy = (SiteIdentityPermission)sip.Copy ();
176                                 Assert.AreEqual (s, copy.Site, s);
177                         }
178                 }
179
180                 [Test]
181                 public void Intersect_Null ()
182                 {
183                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
184                         // No intersection with null
185                         foreach (string s in GoodSites) {
186                                 sip.Site = s;
187                                 Assert.IsNull (sip.Intersect (null), s);
188                         }
189                 }
190
191                 [Test]
192                 public void Intersect_None ()
193                 {
194                         SiteIdentityPermission sip1 = new SiteIdentityPermission (PermissionState.None);
195                         SiteIdentityPermission sip2 = new SiteIdentityPermission (PermissionState.None);
196                         SiteIdentityPermission result = (SiteIdentityPermission)sip1.Intersect (sip2);
197                         Assert.IsNull (result, "None N None");
198                         foreach (string s in GoodSites) {
199                                 sip1.Site = s;
200                                 // 1. Intersect None with site
201                                 result = (SiteIdentityPermission)sip1.Intersect (sip2);
202                                 Assert.IsNull (result, "None N " + s);
203                                 // 2. Intersect site with None
204                                 result = (SiteIdentityPermission)sip2.Intersect (sip1);
205                                 Assert.IsNull (result, s + "N None");
206                         }
207                 }
208
209                 [Test]
210                 public void Intersect_Self ()
211                 {
212                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
213                         foreach (string s in GoodSites) {
214                                 sip.Site = s;
215                                 SiteIdentityPermission result = (SiteIdentityPermission)sip.Intersect (sip);
216                                 Assert.AreEqual (s, result.Site, s);
217                         }
218                 }
219
220                 [Test]
221                 public void Intersect_Different ()
222                 {
223                         SiteIdentityPermission sip1 = new SiteIdentityPermission (GoodSites [0]);
224                         SiteIdentityPermission sip2 = new SiteIdentityPermission (GoodSites [1]);
225                         SiteIdentityPermission result = (SiteIdentityPermission)sip1.Intersect (sip2);
226                         Assert.IsNull (result, "Mono N Novell");
227                 }
228
229                 [Test]
230                 public void IsSubset_Null ()
231                 {
232                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
233                         Assert.IsTrue (sip.IsSubsetOf (null), "Empty");
234                         foreach (string s in GoodSites) {
235                                 sip.Site = s;
236                                 Assert.IsFalse (sip.IsSubsetOf (null), s);
237                         }
238                 }
239
240                 [Test]
241                 public void IsSubset_None ()
242                 {
243                         // IsSubset with none
244                         // a. source (this) is none -> target is never a subset
245                         SiteIdentityPermission sip1 = new SiteIdentityPermission (PermissionState.None);
246                         SiteIdentityPermission sip2 = new SiteIdentityPermission (PermissionState.None);
247                         foreach (string s in GoodSites) {
248                                 sip1.Site = s;
249                                 Assert.IsFalse (sip1.IsSubsetOf (sip2), "target " + s);
250                         }
251                         sip1 = new SiteIdentityPermission (PermissionState.None);
252                         // b. destination (target) is none -> target is always a subset
253                         foreach (string s in GoodSites) {
254                                 sip2.Site = s;
255                                 Assert.IsFalse (sip2.IsSubsetOf (sip1), "source " + s);
256                         }
257                 }
258
259                 [Test]
260                 public void IsSubset_Self ()
261                 {
262                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
263                         Assert.IsTrue (sip.IsSubsetOf (sip), "None");
264                         foreach (string s in GoodSites) {
265                                 sip.Site = s;
266                                 Assert.IsTrue (sip.IsSubsetOf (sip), s);
267                         }
268                 }
269
270                 [Test]
271                 public void IsSubset_Different ()
272                 {
273                         SiteIdentityPermission sip1 = new SiteIdentityPermission (GoodSites [0]);
274                         SiteIdentityPermission sip2 = new SiteIdentityPermission (GoodSites [1]);
275                         Assert.IsFalse (sip1.IsSubsetOf (sip2), "Mono subset Novell");
276                         Assert.IsFalse (sip2.IsSubsetOf (sip1), "Novell subset Mono");
277                 }
278
279                 [Test]
280                 public void IsSubset_Wildcard ()
281                 {
282                         SiteIdentityPermission sip1 = new SiteIdentityPermission (GoodSites [0]);
283                         SiteIdentityPermission sip2 = new SiteIdentityPermission ("*.mono-project.com");
284                         Assert.IsTrue (sip1.IsSubsetOf (sip2), "www.mono-project.com subset *.mono-project.com");
285                         Assert.IsFalse (sip2.IsSubsetOf (sip1), "*.mono-project.com subset www.mono-project.com");
286                 }
287
288                 [Test]
289                 public void Union_Null ()
290                 {
291                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
292                         // Union with null is a simple copy
293                         foreach (string s in GoodSites) {
294                                 sip.Site = s;
295                                 SiteIdentityPermission union = (SiteIdentityPermission)sip.Union (null);
296                                 Assert.AreEqual (s, union.Site, s);
297                         }
298                 }
299
300                 [Test]
301                 public void Union_None ()
302                 {
303                         // Union with none is same
304                         SiteIdentityPermission sip1 = new SiteIdentityPermission (PermissionState.None);
305                         SiteIdentityPermission sip2 = new SiteIdentityPermission (PermissionState.None);
306                         // a. source (this) is none
307                         foreach (string s in GoodSites) {
308                                 sip1.Site = s;
309                                 SiteIdentityPermission union = (SiteIdentityPermission)sip1.Union (sip2);
310                                 Assert.AreEqual (s, union.Site, s);
311                         }
312                         sip1 = new SiteIdentityPermission (PermissionState.None);
313                         // b. destination (target) is none
314                         foreach (string s in GoodSites) {
315                                 sip2.Site = s;
316                                 SiteIdentityPermission union = (SiteIdentityPermission)sip2.Union (sip1);
317                                 Assert.AreEqual (s, union.Site, s);
318                         }
319                 }
320 #if NET_2_0
321                 [Category ("NotWorking")]
322 #endif
323                 [Test]
324                 public void Union_Self ()
325                 {
326                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
327                         SiteIdentityPermission union = (SiteIdentityPermission)sip.Union (sip);
328 #if NET_2_0
329                         Assert.IsNull (union, "None U None");
330 #else
331                         Assert.IsNotNull (union, "None U None"); // can't get null Site property
332                         foreach (string s in GoodSites) {
333                                 sip.Site = s;
334                                 union = (SiteIdentityPermission)sip.Union (sip);
335                                 Assert.AreEqual (s, union.Site, s);
336                         }
337 #endif
338                 }
339 #if NET_2_0
340                 [Category ("NotWorking")]
341 #endif
342                 [Test]
343                 public void Union_Different ()
344                 {
345                         SiteIdentityPermission sip1 = new SiteIdentityPermission (GoodSites [0]);
346                         SiteIdentityPermission sip2 = new SiteIdentityPermission (GoodSites [1]);
347                         SiteIdentityPermission result = (SiteIdentityPermission)sip1.Union (sip2);
348 #if NET_2_0
349                         Assert.IsNotNull (result, "Mono U Novell");
350                         // new XML format is used to contain more than one site
351                         SecurityElement se = result.ToXml ();
352                         Assert.AreEqual (2, se.Children.Count, "Childs");
353                         Assert.AreEqual ((se.Children [0] as SecurityElement).Attribute ("Site"), sip1.Site, "Site#1");
354                         Assert.AreEqual ((se.Children [1] as SecurityElement).Attribute ("Site"), sip2.Site, "Site#2");
355                         // strangely it is still versioned as 'version="1"'.
356                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
357 #else
358                         // It's null for FX 1.0 and 1.1
359                         Assert.IsNull (result, "Mono U Novell");
360 #endif
361                 }
362 #if NET_2_0
363                 [Test]
364                 [Category ("NotWorking")]
365                 [ExpectedException (typeof (NotSupportedException))]
366                 public void Union_Different_Site ()
367                 {
368                         SiteIdentityPermission sip1 = new SiteIdentityPermission (GoodSites [0]);
369                         SiteIdentityPermission sip2 = new SiteIdentityPermission (GoodSites [1]);
370                         SiteIdentityPermission result = (SiteIdentityPermission)sip1.Union (sip2);
371                         // it's not possible to return many sites using the Site property so it throws
372                         Assert.IsNull (result.Site);
373                 }
374 #endif
375                 [Test]
376                 [ExpectedException (typeof (ArgumentNullException))]
377                 public void FromXml_Null ()
378                 {
379                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
380                         sip.FromXml (null);
381                 }
382
383                 [Test]
384                 [ExpectedException (typeof (ArgumentException))]
385                 public void FromXml_WrongTag ()
386                 {
387                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
388                         SecurityElement se = sip.ToXml ();
389                         se.Tag = "IMono";
390                         sip.FromXml (se);
391                 }
392
393                 [Test]
394                 [ExpectedException (typeof (ArgumentException))]
395                 public void FromXml_WrongTagCase ()
396                 {
397                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
398                         SecurityElement se = sip.ToXml ();
399                         se.Tag = "IPERMISSION"; // instead of IPermission
400                         sip.FromXml (se);
401                 }
402
403                 [Test]
404                 public void FromXml_WrongClass ()
405                 {
406                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
407                         SecurityElement se = sip.ToXml ();
408
409                         SecurityElement w = new SecurityElement (se.Tag);
410                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
411                         w.AddAttribute ("version", se.Attribute ("version"));
412                         sip.FromXml (w);
413                         // doesn't care of the class name at that stage
414                         // anyway the class has already be created so...
415                 }
416
417                 [Test]
418                 public void FromXml_NoClass ()
419                 {
420                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
421                         SecurityElement se = sip.ToXml ();
422
423                         SecurityElement w = new SecurityElement (se.Tag);
424                         w.AddAttribute ("version", se.Attribute ("version"));
425                         sip.FromXml (w);
426                         // doesn't even care of the class attribute presence
427                 }
428
429                 [Test]
430                 [ExpectedException (typeof (ArgumentException))]
431                 public void FromXml_WrongVersion ()
432                 {
433                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
434                         SecurityElement se = sip.ToXml ();
435                         se.Attributes.Remove ("version");
436                         se.Attributes.Add ("version", "2");
437                         sip.FromXml (se);
438                 }
439
440                 [Test]
441                 public void FromXml_NoVersion ()
442                 {
443                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
444                         SecurityElement se = sip.ToXml ();
445
446                         SecurityElement w = new SecurityElement (se.Tag);
447                         w.AddAttribute ("class", se.Attribute ("class"));
448                         sip.FromXml (w);
449                 }
450         }
451 }