0 failures here. YAY
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / UrlMembershipConditionTest.cs
1 //
2 // UrlMembershipConditionTest.cs - NUnit Test Cases for UrlMembershipCondition
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Collections;
33 using System.Security;
34 using System.Security.Policy;
35
36 namespace MonoTests.System.Security.Policy {
37
38         [TestFixture]
39         public class UrlMembershipConditionTest {
40
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void UrlMembershipCondition_Null () 
44                 {
45                         UrlMembershipCondition umc = new UrlMembershipCondition (null);
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (FormatException))]
50                 public void UrlMembershipCondition_Empty () 
51                 {
52                         UrlMembershipCondition umc = new UrlMembershipCondition (String.Empty);
53                 }
54
55                 [Test]
56                 public void UrlMembershipCondition_FileUrl () 
57                 {
58                         UrlMembershipCondition umc = new UrlMembershipCondition ("file://mono/index.html");
59                 }
60
61                 [Test]
62                 public void UrlMembershipCondition_FullUrlWithPort () 
63                 {
64                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com:8080/index.html");
65                 }
66
67                 [Test]
68                 public void UrlMembershipCondition_GoMonoWebUrl () 
69                 {
70                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com/");
71                         Assert.AreEqual ("http://www.go-mono.com/", umc.Url, "Url");
72                         Assert.AreEqual ("Url - http://www.go-mono.com/", umc.ToString (), "ToString");
73
74                         UrlMembershipCondition umc2 = (UrlMembershipCondition) umc.Copy ();
75                         Assert.AreEqual (umc.Url, umc2.Url, "Copy.Url");
76                         Assert.AreEqual (umc.GetHashCode (), umc2.GetHashCode (), "Copy.GetHashCode");
77
78                         SecurityElement se = umc2.ToXml ();
79                         UrlMembershipCondition umc3 = new UrlMembershipCondition ("*");
80                         umc3.FromXml (se);
81                         Assert.AreEqual (umc.Url, umc3.Url, "ToXml/FromXml");
82
83                         Assert.IsTrue (umc.Equals (umc2), "Equals");
84                         UrlMembershipCondition umc4 = new UrlMembershipCondition ("http://www.go-mono.com");
85                         // note that a last slash is added to Url - so it's equal
86                         Assert.IsTrue (umc.Equals (umc4), "Equals-AutoAddedLastSlash");
87                 }
88
89                 [Test]
90                 public void Url_AllGoMonoUrl () 
91                 {
92                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com/*");
93                         Assert.AreEqual ("http://www.go-mono.com/*", umc.Url, "Url");
94                         Assert.AreEqual ("Url - http://www.go-mono.com/*", umc.ToString (), "ToString");
95
96                         UrlMembershipCondition umc2 = (UrlMembershipCondition) umc.Copy ();
97                         Assert.AreEqual (umc.Url, umc2.Url, "Copy.Url");
98                         Assert.AreEqual (umc.GetHashCode (), umc2.GetHashCode (), "Copy.GetHashCode");
99
100                         SecurityElement se = umc2.ToXml ();
101                         UrlMembershipCondition umc3 = new UrlMembershipCondition ("*");
102                         umc3.FromXml (se);
103                         Assert.AreEqual (umc.Url, umc3.Url, "ToXml/FromXml");
104
105                         Assert.IsTrue (umc.Equals (umc2), "Equals");
106                         UrlMembershipCondition umc4 = new UrlMembershipCondition ("http://www.go-mono.com/");
107                         Assert.IsTrue (umc.Equals (umc4), "Equals-*");
108                 }
109
110                 [Test]
111 #if !NET_2_0
112                 [ExpectedException (typeof (ArgumentException))]
113 #else
114                 [Category ("NotWorking")]
115 #endif
116                 public void Url_InvalidSite ()
117                 {
118                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.*");
119 #if NET_2_0
120                         Assert.AreEqual ("http://www.go-mono.*", umc.Url, "Url");
121                         Assert.AreEqual ("Url - http://www.go-mono.*", umc.ToString (), "ToString");
122 #endif
123                 }
124
125 #if NET_2_0
126                 [Category ("NotWorking")]
127 #endif
128                 [Test]
129                 public void Url_NoProtocol () 
130                 {
131                         UrlMembershipCondition umc = new UrlMembershipCondition ("www.go-mono.com");
132 #if NET_2_0
133                         Assert.AreEqual ("www.go-mono.com", umc.Url, "Url");
134                         Assert.AreEqual ("Url - www.go-mono.com", umc.ToString (), "ToString");
135 #else
136                         // note: no last slash here
137                         Assert.AreEqual ("file://WWW.GO-MONO.COM", umc.Url, "Url");
138                         Assert.AreEqual ("Url - file://WWW.GO-MONO.COM", umc.ToString (), "ToString");
139 #endif
140                 }
141 #if NET_2_0
142                 [Category ("NotWorking")]
143 #endif
144                 [Test]
145                 public void Url_WellKnownProtocol () 
146                 {
147                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
148 #if NET_2_0
149                         Assert.AreEqual ("http://www.go-mono.com", umc.Url, "http-Url");
150                         Assert.AreEqual ("Url - http://www.go-mono.com", umc.ToString (), "http-ToString");
151 #else
152                         Assert.AreEqual ("http://www.go-mono.com/", umc.Url, "http-Url");
153                         Assert.AreEqual ("Url - http://www.go-mono.com/", umc.ToString (), "http-ToString");
154 #endif
155                         umc = new UrlMembershipCondition ("https://www.go-mono.com");
156 #if NET_2_0
157                         Assert.AreEqual ("https://www.go-mono.com", umc.Url, "https-Url");
158                         Assert.AreEqual ("Url - https://www.go-mono.com", umc.ToString (), "https-ToString");
159 #else
160                         Assert.AreEqual ("https://www.go-mono.com/", umc.Url, "https-Url");
161                         Assert.AreEqual ("Url - https://www.go-mono.com/", umc.ToString (), "https-ToString");
162 #endif
163
164                         umc = new UrlMembershipCondition ("ftp://www.go-mono.com");
165 #if NET_2_0
166                         Assert.AreEqual ("ftp://www.go-mono.com", umc.Url, "ftp-Url");
167                         Assert.AreEqual ("Url - ftp://www.go-mono.com", umc.ToString (), "ftp-ToString");
168 #else
169                         Assert.AreEqual ("ftp://www.go-mono.com/", umc.Url, "ftp-Url");
170                         Assert.AreEqual ("Url - ftp://www.go-mono.com/", umc.ToString (), "ftp-ToString");
171 #endif
172
173                         umc = new UrlMembershipCondition ("file://www.go-mono.com");
174 #if NET_2_0
175                         Assert.AreEqual ("file://www.go-mono.com", umc.Url, "file-Url");
176                         Assert.AreEqual ("Url - file://www.go-mono.com", umc.ToString (), "file-ToString");
177 #else
178                         Assert.AreEqual ("file://WWW.GO-MONO.COM", umc.Url, "file-Url");
179                         Assert.AreEqual ("Url - file://WWW.GO-MONO.COM", umc.ToString (), "file-ToString");
180 #endif
181                 }
182 #if NET_2_0
183                 [Category ("NotWorking")]
184 #endif
185                 [Test]
186                 public void Url_UnknownProtocol () 
187                 {
188                         UrlMembershipCondition umc = new UrlMembershipCondition ("mono://www.go-mono.com");
189 #if NET_2_0
190                         Assert.AreEqual ("mono://www.go-mono.com", umc.Url, "Url");
191                         Assert.AreEqual ("Url - mono://www.go-mono.com", umc.ToString (), "ToString");
192 #else
193                         Assert.AreEqual ("mono://www.go-mono.com/", umc.Url, "Url");
194                         Assert.AreEqual ("Url - mono://www.go-mono.com/", umc.ToString (), "ToString");
195 #endif
196                 }
197
198                 [Test]
199                 public void Url_RelativePath () 
200                 {
201                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com/path/../newpath/index.html");
202                         Assert.AreEqual ("http://www.go-mono.com/path/../newpath/index.html", umc.Url, "Url");
203                         Assert.AreEqual ("Url - http://www.go-mono.com/path/../newpath/index.html", umc.ToString (), "ToString");
204                 }
205
206                 [Test]
207                 [ExpectedException (typeof (ArgumentNullException))]
208                 public void Url_Null () 
209                 {
210                         UrlMembershipCondition umc = new UrlMembershipCondition ("ftp://www.go-mono.com");
211                         umc.Url = null;
212                 }
213
214                 [Test]
215                 [ExpectedException (typeof (FormatException))]
216                 public void Url_Empty () 
217                 {
218                         UrlMembershipCondition umc = new UrlMembershipCondition ("ftp://www.go-mono.com");
219                         umc.Url = String.Empty;
220                 }
221
222                 [Test]
223                 public void Check () 
224                 {
225                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
226
227                         Evidence e = null;
228                         Assert.IsFalse (umc.Check (e), "Check(null)");
229
230                         e = new Evidence ();
231                         Assert.IsFalse (umc.Check (e), "Check(empty)");
232
233                         e.AddHost (new Zone (SecurityZone.MyComputer));
234                         Assert.IsFalse (umc.Check (e), "Check(zone)");
235
236                         Url u = new Url ("http://www.go-mono.com");
237                         e.AddAssembly (u);
238                         Assert.IsFalse (umc.Check (e), "Check(url-assembly)");
239                         e.AddHost (u);
240                         Assert.IsTrue (umc.Check (e), "Check(url-host)");
241                 }
242
243                 [Test]
244                 public void CheckPositive_Partial () 
245                 {
246                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com/*");
247                         Evidence e = new Evidence ();
248                         e.AddHost (new Url ("http://www.go-mono.com/index.html"));
249                         Assert.IsTrue (umc.Check (e), "Check(+-)");
250                 }
251
252                 [Test]
253                 public void CheckNegative () 
254                 {
255                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
256                         Evidence e = new Evidence ();
257                         e.AddHost (new Url ("http://www.go-mono.org"));
258                         Assert.IsFalse (umc.Check (e), "Check(-)");
259                 }
260
261                 [Test]
262                 public void CheckMultipleEvidences () 
263                 {
264                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
265                         Evidence e = new Evidence ();
266                         e.AddHost (new Url ("http://www.go-mono.org")); // the bad
267                         e.AddHost (new Url ("http://www.go-mono.com")); // the good
268                         e.AddHost (new Zone (SecurityZone.MyComputer)); // and the ugly (couldn't resist ;)
269                         Assert.IsTrue (umc.Check (e), "Check(n)");
270                         // check all Url evidence (i.e. do not stop at the first Url evidence)
271                 }
272
273                 [Test]
274                 public void EqualsCaseSensitive_Http () 
275                 {
276                         UrlMembershipCondition umc1 = new UrlMembershipCondition ("http://www.go-mono.com");
277                         UrlMembershipCondition umc2 = new UrlMembershipCondition ("http://www.Go-Mono.com");
278                         Assert.IsTrue (umc1.Equals (umc2), "CaseSensitive");
279                 }
280
281                 [Test]
282                 public void EqualsCaseSensitive_File () 
283                 {
284                         UrlMembershipCondition umc1 = new UrlMembershipCondition ("file://MONO");
285                         UrlMembershipCondition umc2 = new UrlMembershipCondition ("file://mono");
286                         Assert.IsTrue (umc1.Equals (umc2), "CaseSensitive");
287                 }
288
289                 [Test]
290                 public void EqualsNull () 
291                 {
292                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
293                         Assert.IsFalse (umc.Equals (null), "EqualsNull");
294                 }
295
296                 [Test]
297                 [ExpectedException (typeof (ArgumentNullException))]
298                 public void FromXml_Null () 
299                 {
300                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
301                         umc.FromXml (null);
302                 }
303
304                 [Test]
305                 [ExpectedException (typeof (ArgumentException))]
306                 public void FromXml_InvalidTag () 
307                 {
308                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
309                         SecurityElement se = umc.ToXml ();
310                         se.Tag = "IMonoship";
311                         umc.FromXml (se);
312                 }
313
314                 [Test]
315                 public void FromXml_InvalidClass ()
316                 {
317                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
318                         SecurityElement se = umc.ToXml ();
319                         se.Attributes ["class"] = "Hello world";
320                         umc.FromXml (se);
321                 }
322
323                 [Test]
324                 public void FromXml_NoClass ()
325                 {
326                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
327                         SecurityElement se = umc.ToXml ();
328
329                         SecurityElement w = new SecurityElement (se.Tag);
330                         w.AddAttribute ("version", se.Attribute ("version"));
331                         umc.FromXml (w);
332                         // doesn't even care of the class attribute presence
333                 }
334
335                 [Test]
336                 public void FromXml_InvalidVersion ()
337                 {
338                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
339                         SecurityElement se = umc.ToXml ();
340
341                         SecurityElement w = new SecurityElement (se.Tag);
342                         w.AddAttribute ("class", se.Attribute ("class"));
343                         w.AddAttribute ("version", "2");
344                         w.AddAttribute ("Url", se.Attribute ("Url"));
345                         umc.FromXml (w);
346                 }
347
348                 [Test]
349                 public void FromXml_NoVersion ()
350                 {
351                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
352                         SecurityElement se = umc.ToXml ();
353
354                         SecurityElement w = new SecurityElement (se.Tag);
355                         w.AddAttribute ("class", se.Attribute ("class"));
356                         umc.FromXml (w);
357                 }
358
359                 [Test]
360                 public void FromXml_PolicyLevel () 
361                 {
362                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
363                         SecurityElement se = umc.ToXml ();
364                         // is it accepted for all policy levels ?
365                         IEnumerator e = SecurityManager.PolicyHierarchy ();
366                         while (e.MoveNext ()) {
367                                 PolicyLevel pl = e.Current as PolicyLevel;
368                                 UrlMembershipCondition spl = new UrlMembershipCondition ("*");
369                                 spl.FromXml (se, pl);
370                                 Assert.IsTrue (spl.Equals (umc), "FromXml(PolicyLevel='" + pl.Label + "')");
371                         }
372                         // yes!
373                 }
374
375                 [Test]
376                 public void ToXml_Null () 
377                 {
378                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
379                         // no ArgumentNullException here
380                         SecurityElement se = umc.ToXml (null);
381                         Assert.IsNotNull (se, "ToXml(null)");
382                 }
383
384                 [Test]
385                 public void ToXml_PolicyLevel () 
386                 {
387                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
388                         SecurityElement se = umc.ToXml ();
389                         string s = umc.ToXml ().ToString ();
390                         // is it accepted for all policy levels ?
391                         IEnumerator e = SecurityManager.PolicyHierarchy ();
392                         while (e.MoveNext ()) {
393                                 PolicyLevel pl = e.Current as PolicyLevel;
394                                 UrlMembershipCondition spl = new UrlMembershipCondition ("*");
395                                 spl.FromXml (se, pl);
396                                 Assert.AreEqual (s, spl.ToXml (pl).ToString (), "ToXml(PolicyLevel='" + pl.Label + "')");
397                         }
398                         // yes!
399                 }
400
401                 [Test]
402                 public void ToFromXmlRoundTrip () 
403                 {
404                         UrlMembershipCondition umc1 = new UrlMembershipCondition ("http://www.go-mono.com");
405                         SecurityElement se = umc1.ToXml ();
406
407                         UrlMembershipCondition umc2 = new UrlMembershipCondition ("*");
408                         umc2.FromXml (se);
409
410                         Assert.AreEqual (umc1.GetHashCode (), umc2.GetHashCode (), "ToFromXmlRoundTrip");
411                 }
412         }
413 }