2004-12-03 Lluis Sanchez Gual <lluis@novell.com>
[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                 };
57
58                 [Test]
59                 public void PermissionState_None ()
60                 {
61                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
62 //                      Assert.IsNull (sip.Site, "Site");
63
64                         SecurityElement se = sip.ToXml ();
65                         // only class and version are present
66                         Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
67                         Assert.IsNull (se.Children, "Xml-Children");
68
69                         SiteIdentityPermission copy = (SiteIdentityPermission)sip.Copy ();
70                         Assert.IsFalse (Object.ReferenceEquals (sip, copy), "ReferenceEquals");
71                 }
72
73                 [Test]
74 // MS BUG
75                 [ExpectedException (typeof (NullReferenceException))]
76                 public void PermissionState_None_Site ()
77                 {
78                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
79                         Assert.IsNull (sip.Site, "Site");
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof (ArgumentException))]
84                 public void PermissionState_Unrestricted ()
85                 {
86                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.Unrestricted);
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentException))]
91                 public void PermissionState_Bad ()
92                 {
93                         SiteIdentityPermission sip = new SiteIdentityPermission ((PermissionState)Int32.MinValue);
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ArgumentException))]
98                 public void SiteIdentityPermission_NullSite ()
99                 {
100                         SiteIdentityPermission sip = new SiteIdentityPermission (null);
101                 }
102
103                 [Test]
104                 public void Site ()
105                 {
106                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
107                         foreach (string s in GoodSites) {
108                                 sip.Site = s;
109                                 Assert.AreEqual (s, sip.Site, s);
110                         }
111                 }
112
113                 [Test]
114                 public void Site_Bad ()
115                 {
116                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
117                         foreach (string s in BadSites) {
118                                 try {
119                                         sip.Site = s;
120                                         Assert.Fail (s + " isn't a bad site!");
121                                 }
122                                 catch (ArgumentException) {
123                                         // expected, continue looping
124                                 }
125                         }
126                 }
127
128 /*              [Test]
129                 public void Site_InvalidChars ()
130                 {
131                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
132                         for (int i=0; i < 256; i++) {
133                                 try {
134                                         sip.Site = String.Empty + (char) i;
135                                 }
136                                 catch (ArgumentException) {
137                                         Console.WriteLine ("{0} is bad", i);
138                                 }
139                         }
140                 }*/
141
142                 [Test]
143                 [ExpectedException (typeof (ArgumentException))]
144                 public void Site_Null ()
145                 {
146                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
147                         sip.Site = null;
148                 }
149
150                 [Test]
151                 public void Copy ()
152                 {
153                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
154                         foreach (string s in GoodSites) {
155                                 sip.Site = s;
156                                 SiteIdentityPermission copy = (SiteIdentityPermission)sip.Copy ();
157                                 Assert.AreEqual (s, copy.Site, s);
158                         }
159                 }
160
161                 [Test]
162                 public void Intersect_Null ()
163                 {
164                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
165                         // No intersection with null
166                         foreach (string s in GoodSites) {
167                                 sip.Site = s;
168                                 Assert.IsNull (sip.Intersect (null), s);
169                         }
170                 }
171
172                 [Test]
173                 public void Intersect_None ()
174                 {
175                         SiteIdentityPermission sip1 = new SiteIdentityPermission (PermissionState.None);
176                         SiteIdentityPermission sip2 = new SiteIdentityPermission (PermissionState.None);
177                         SiteIdentityPermission result = (SiteIdentityPermission)sip1.Intersect (sip2);
178                         Assert.IsNull (result, "None N None");
179                         foreach (string s in GoodSites) {
180                                 sip1.Site = s;
181                                 // 1. Intersect None with site
182                                 result = (SiteIdentityPermission)sip1.Intersect (sip2);
183                                 Assert.IsNull (result, "None N " + s);
184                                 // 2. Intersect site with None
185                                 result = (SiteIdentityPermission)sip2.Intersect (sip1);
186                                 Assert.IsNull (result, s + "N None");
187                         }
188                 }
189
190                 [Test]
191                 public void Intersect_Self ()
192                 {
193                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
194                         foreach (string s in GoodSites) {
195                                 sip.Site = s;
196                                 SiteIdentityPermission result = (SiteIdentityPermission)sip.Intersect (sip);
197                                 Assert.AreEqual (s, result.Site, s);
198                         }
199                 }
200
201                 [Test]
202                 public void Intersect_Different ()
203                 {
204                         SiteIdentityPermission sip1 = new SiteIdentityPermission (GoodSites [0]);
205                         SiteIdentityPermission sip2 = new SiteIdentityPermission (GoodSites [1]);
206                         SiteIdentityPermission result = (SiteIdentityPermission)sip1.Intersect (sip2);
207                         Assert.IsNull (result, "Mono N Novell");
208                 }
209
210                 [Test]
211                 public void IsSubset_Null ()
212                 {
213                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
214                         Assert.IsTrue (sip.IsSubsetOf (null), "Empty");
215                         foreach (string s in GoodSites) {
216                                 sip.Site = s;
217                                 Assert.IsFalse (sip.IsSubsetOf (null), s);
218                         }
219                 }
220
221                 [Test]
222                 public void IsSubset_None ()
223                 {
224                         // IsSubset with none
225                         // a. source (this) is none -> target is never a subset
226                         SiteIdentityPermission sip1 = new SiteIdentityPermission (PermissionState.None);
227                         SiteIdentityPermission sip2 = new SiteIdentityPermission (PermissionState.None);
228                         foreach (string s in GoodSites) {
229                                 sip1.Site = s;
230                                 Assert.IsFalse (sip1.IsSubsetOf (sip2), "target " + s);
231                         }
232                         sip1 = new SiteIdentityPermission (PermissionState.None);
233                         // b. destination (target) is none -> target is always a subset
234                         foreach (string s in GoodSites) {
235                                 sip2.Site = s;
236                                 Assert.IsFalse (sip2.IsSubsetOf (sip1), "source " + s);
237                         }
238                 }
239
240                 [Test]
241                 public void IsSubset_Self ()
242                 {
243                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
244                         Assert.IsTrue (sip.IsSubsetOf (sip), "None");
245                         foreach (string s in GoodSites) {
246                                 sip.Site = s;
247                                 Assert.IsTrue (sip.IsSubsetOf (sip), s);
248                         }
249                 }
250
251                 [Test]
252                 public void IsSubset_Different ()
253                 {
254                         SiteIdentityPermission sip1 = new SiteIdentityPermission (GoodSites [0]);
255                         SiteIdentityPermission sip2 = new SiteIdentityPermission (GoodSites [1]);
256                         Assert.IsFalse (sip1.IsSubsetOf (sip2), "Mono subset Novell");
257                         Assert.IsFalse (sip2.IsSubsetOf (sip1), "Novell subset Mono");
258                 }
259
260                 [Test]
261                 public void Union_Null ()
262                 {
263                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
264                         // Union with null is a simple copy
265                         foreach (string s in GoodSites) {
266                                 sip.Site = s;
267                                 SiteIdentityPermission union = (SiteIdentityPermission)sip.Union (null);
268                                 Assert.AreEqual (s, union.Site, s);
269                         }
270                 }
271
272                 [Test]
273                 public void Union_None ()
274                 {
275                         // Union with none is same
276                         SiteIdentityPermission sip1 = new SiteIdentityPermission (PermissionState.None);
277                         SiteIdentityPermission sip2 = new SiteIdentityPermission (PermissionState.None);
278                         // a. source (this) is none
279                         foreach (string s in GoodSites) {
280                                 sip1.Site = s;
281                                 SiteIdentityPermission union = (SiteIdentityPermission)sip1.Union (sip2);
282                                 Assert.AreEqual (s, union.Site, s);
283                         }
284                         sip1 = new SiteIdentityPermission (PermissionState.None);
285                         // b. destination (target) is none
286                         foreach (string s in GoodSites) {
287                                 sip2.Site = s;
288                                 SiteIdentityPermission union = (SiteIdentityPermission)sip2.Union (sip1);
289                                 Assert.AreEqual (s, union.Site, s);
290                         }
291                 }
292
293                 [Test]
294                 public void Union_Self ()
295                 {
296                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
297                         SiteIdentityPermission union = (SiteIdentityPermission)sip.Union (sip);
298                         Assert.IsNotNull (union, "None U None"); // can't get null Site property
299                         foreach (string s in GoodSites) {
300                                 sip.Site = s;
301                                 union = (SiteIdentityPermission)sip.Union (sip);
302                                 Assert.AreEqual (s, union.Site, s);
303                         }
304                 }
305
306                 [Test]
307 #if NET_2_0
308                 [ExpectedException (typeof (ArgumentException))]
309 #endif
310                 public void Union_Different ()
311                 {
312                         SiteIdentityPermission sip1 = new SiteIdentityPermission (GoodSites [0]);
313                         SiteIdentityPermission sip2 = new SiteIdentityPermission (GoodSites [1]);
314                         SiteIdentityPermission result = (SiteIdentityPermission)sip1.Union (sip2);
315                         Assert.IsNull (result, "Mono U Novell");
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (ArgumentNullException))]
320                 public void FromXml_Null ()
321                 {
322                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
323                         sip.FromXml (null);
324                 }
325
326                 [Test]
327                 [ExpectedException (typeof (ArgumentException))]
328                 public void FromXml_WrongTag ()
329                 {
330                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
331                         SecurityElement se = sip.ToXml ();
332                         se.Tag = "IMono";
333                         sip.FromXml (se);
334                 }
335
336                 [Test]
337                 [ExpectedException (typeof (ArgumentException))]
338                 public void FromXml_WrongTagCase ()
339                 {
340                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
341                         SecurityElement se = sip.ToXml ();
342                         se.Tag = "IPERMISSION"; // instead of IPermission
343                         sip.FromXml (se);
344                 }
345
346                 [Test]
347                 public void FromXml_WrongClass ()
348                 {
349                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
350                         SecurityElement se = sip.ToXml ();
351
352                         SecurityElement w = new SecurityElement (se.Tag);
353                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
354                         w.AddAttribute ("version", se.Attribute ("version"));
355                         sip.FromXml (w);
356                         // doesn't care of the class name at that stage
357                         // anyway the class has already be created so...
358                 }
359
360                 [Test]
361                 public void FromXml_NoClass ()
362                 {
363                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
364                         SecurityElement se = sip.ToXml ();
365
366                         SecurityElement w = new SecurityElement (se.Tag);
367                         w.AddAttribute ("version", se.Attribute ("version"));
368                         sip.FromXml (w);
369                         // doesn't even care of the class attribute presence
370                 }
371
372                 [Test]
373                 [ExpectedException (typeof (ArgumentException))]
374                 public void FromXml_WrongVersion ()
375                 {
376                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
377                         SecurityElement se = sip.ToXml ();
378                         se.Attributes.Remove ("version");
379                         se.Attributes.Add ("version", "2");
380                         sip.FromXml (se);
381                 }
382
383                 [Test]
384                 public void FromXml_NoVersion ()
385                 {
386                         SiteIdentityPermission sip = new SiteIdentityPermission (PermissionState.None);
387                         SecurityElement se = sip.ToXml ();
388
389                         SecurityElement w = new SecurityElement (se.Tag);
390                         w.AddAttribute ("class", se.Attribute ("class"));
391                         sip.FromXml (w);
392                 }
393         }
394 }