imported everything from my branch (which is slightly harmless).
[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                 [ExpectedException (typeof (ArgumentException))]
112                 public void Url_InvalidSite ()
113                 {
114                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.*");
115                 }
116
117                 [Test]
118                 public void Url_NoProtocol () 
119                 {
120                         UrlMembershipCondition umc = new UrlMembershipCondition ("www.go-mono.com");
121 #if NET_2_0
122                         Assert.AreEqual ("www.go-mono.com", umc.Url, "Url");
123                         Assert.AreEqual ("Url - www.go-mono.com", umc.ToString (), "ToString");
124 #else
125                         // note: no last slash here
126                         Assert.AreEqual ("file://WWW.GO-MONO.COM", umc.Url, "Url");
127                         Assert.AreEqual ("Url - file://WWW.GO-MONO.COM", umc.ToString (), "ToString");
128 #endif
129                 }
130
131                 [Test]
132                 public void Url_WellKnownProtocol () 
133                 {
134                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
135 #if NET_2_0
136                         Assert.AreEqual ("http://www.go-mono.com", umc.Url, "http-Url");
137                         Assert.AreEqual ("Url - http://www.go-mono.com", umc.ToString (), "http-ToString");
138 #else
139                         Assert.AreEqual ("http://www.go-mono.com/", umc.Url, "http-Url");
140                         Assert.AreEqual ("Url - http://www.go-mono.com/", umc.ToString (), "http-ToString");
141 #endif
142                         umc = new UrlMembershipCondition ("https://www.go-mono.com");
143 #if NET_2_0
144                         Assert.AreEqual ("https://www.go-mono.com", umc.Url, "https-Url");
145                         Assert.AreEqual ("Url - https://www.go-mono.com", umc.ToString (), "https-ToString");
146 #else
147                         Assert.AreEqual ("https://www.go-mono.com/", umc.Url, "https-Url");
148                         Assert.AreEqual ("Url - https://www.go-mono.com/", umc.ToString (), "https-ToString");
149 #endif
150
151                         umc = new UrlMembershipCondition ("ftp://www.go-mono.com");
152 #if NET_2_0
153                         Assert.AreEqual ("ftp://www.go-mono.com", umc.Url, "ftp-Url");
154                         Assert.AreEqual ("Url - ftp://www.go-mono.com", umc.ToString (), "ftp-ToString");
155 #else
156                         Assert.AreEqual ("ftp://www.go-mono.com/", umc.Url, "ftp-Url");
157                         Assert.AreEqual ("Url - ftp://www.go-mono.com/", umc.ToString (), "ftp-ToString");
158 #endif
159
160                         umc = new UrlMembershipCondition ("file://www.go-mono.com");
161 #if NET_2_0
162                         Assert.AreEqual ("file://www.go-mono.com", umc.Url, "file-Url");
163                         Assert.AreEqual ("Url - file://www.go-mono.com", umc.ToString (), "file-ToString");
164 #else
165                         Assert.AreEqual ("file://WWW.GO-MONO.COM", umc.Url, "file-Url");
166                         Assert.AreEqual ("Url - file://WWW.GO-MONO.COM", umc.ToString (), "file-ToString");
167 #endif
168                 }
169
170                 [Test]
171                 public void Url_UnknownProtocol () 
172                 {
173                         UrlMembershipCondition umc = new UrlMembershipCondition ("mono://www.go-mono.com");
174 #if NET_2_0
175                         Assert.AreEqual ("mono://www.go-mono.com", umc.Url, "Url");
176                         Assert.AreEqual ("Url - mono://www.go-mono.com", umc.ToString (), "ToString");
177 #else
178                         Assert.AreEqual ("mono://www.go-mono.com/", umc.Url, "Url");
179                         Assert.AreEqual ("Url - mono://www.go-mono.com/", umc.ToString (), "ToString");
180 #endif
181                 }
182
183                 [Test]
184                 public void Url_RelativePath () 
185                 {
186                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com/path/../newpath/index.html");
187                         Assert.AreEqual ("http://www.go-mono.com/path/../newpath/index.html", umc.Url, "Url");
188                         Assert.AreEqual ("Url - http://www.go-mono.com/path/../newpath/index.html", umc.ToString (), "ToString");
189                 }
190
191                 [Test]
192                 [ExpectedException (typeof (ArgumentNullException))]
193                 public void Url_Null () 
194                 {
195                         UrlMembershipCondition umc = new UrlMembershipCondition ("ftp://www.go-mono.com");
196                         umc.Url = null;
197                 }
198
199                 [Test]
200                 [ExpectedException (typeof (FormatException))]
201                 public void Url_Empty () 
202                 {
203                         UrlMembershipCondition umc = new UrlMembershipCondition ("ftp://www.go-mono.com");
204                         umc.Url = String.Empty;
205                 }
206
207                 [Test]
208                 public void Check () 
209                 {
210                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
211
212                         Evidence e = null;
213                         Assert.IsFalse (umc.Check (e), "Check(null)");
214
215                         e = new Evidence ();
216                         Assert.IsFalse (umc.Check (e), "Check(empty)");
217
218                         e.AddHost (new Zone (SecurityZone.MyComputer));
219                         Assert.IsFalse (umc.Check (e), "Check(zone)");
220
221                         Url u = new Url ("http://www.go-mono.com");
222                         e.AddAssembly (u);
223                         Assert.IsFalse (umc.Check (e), "Check(url-assembly)");
224                         e.AddHost (u);
225                         Assert.IsTrue (umc.Check (e), "Check(url-host)");
226                 }
227
228                 [Test]
229                 public void CheckPositive_Partial () 
230                 {
231                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com/*");
232                         Evidence e = new Evidence ();
233                         e.AddHost (new Url ("http://www.go-mono.com/index.html"));
234                         Assert.IsTrue (umc.Check (e), "Check(+-)");
235                 }
236
237                 [Test]
238                 public void CheckNegative () 
239                 {
240                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
241                         Evidence e = new Evidence ();
242                         e.AddHost (new Url ("http://www.go-mono.org"));
243                         Assert.IsFalse (umc.Check (e), "Check(-)");
244                 }
245
246                 [Test]
247                 public void CheckMultipleEvidences () 
248                 {
249                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
250                         Evidence e = new Evidence ();
251                         e.AddHost (new Url ("http://www.go-mono.org")); // the bad
252                         e.AddHost (new Url ("http://www.go-mono.com")); // the good
253                         e.AddHost (new Zone (SecurityZone.MyComputer)); // and the ugly (couldn't resist ;)
254                         Assert.IsTrue (umc.Check (e), "Check(n)");
255                         // check all Url evidence (i.e. do not stop at the first Url evidence)
256                 }
257
258                 [Test]
259                 public void EqualsCaseSensitive_Http () 
260                 {
261                         UrlMembershipCondition umc1 = new UrlMembershipCondition ("http://www.go-mono.com");
262                         UrlMembershipCondition umc2 = new UrlMembershipCondition ("http://www.Go-Mono.com");
263                         Assert.IsTrue (umc1.Equals (umc2), "CaseSensitive");
264                 }
265
266                 [Test]
267                 public void EqualsCaseSensitive_File () 
268                 {
269                         UrlMembershipCondition umc1 = new UrlMembershipCondition ("file://MONO");
270                         UrlMembershipCondition umc2 = new UrlMembershipCondition ("file://mono");
271                         Assert.IsTrue (umc1.Equals (umc2), "CaseSensitive");
272                 }
273
274                 [Test]
275                 public void EqualsNull () 
276                 {
277                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
278                         Assert.IsFalse (umc.Equals (null), "EqualsNull");
279                 }
280
281                 [Test]
282                 [ExpectedException (typeof (ArgumentNullException))]
283                 public void FromXml_Null () 
284                 {
285                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
286                         umc.FromXml (null);
287                 }
288
289                 [Test]
290                 [ExpectedException (typeof (ArgumentException))]
291                 public void FromXml_InvalidTag () 
292                 {
293                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
294                         SecurityElement se = umc.ToXml ();
295                         se.Tag = "IMonoship";
296                         umc.FromXml (se);
297                 }
298
299                 [Test]
300                 public void FromXml_InvalidClass ()
301                 {
302                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
303                         SecurityElement se = umc.ToXml ();
304                         se.Attributes ["class"] = "Hello world";
305                         umc.FromXml (se);
306                 }
307
308                 [Test]
309                 public void FromXml_NoClass ()
310                 {
311                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
312                         SecurityElement se = umc.ToXml ();
313
314                         SecurityElement w = new SecurityElement (se.Tag);
315                         w.AddAttribute ("version", se.Attribute ("version"));
316                         umc.FromXml (w);
317                         // doesn't even care of the class attribute presence
318                 }
319
320                 [Test]
321                 public void FromXml_InvalidVersion ()
322                 {
323                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
324                         SecurityElement se = umc.ToXml ();
325
326                         SecurityElement w = new SecurityElement (se.Tag);
327                         w.AddAttribute ("class", se.Attribute ("class"));
328                         w.AddAttribute ("version", "2");
329                         w.AddAttribute ("Url", se.Attribute ("Url"));
330                         umc.FromXml (w);
331                 }
332
333                 [Test]
334                 public void FromXml_NoVersion ()
335                 {
336                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
337                         SecurityElement se = umc.ToXml ();
338
339                         SecurityElement w = new SecurityElement (se.Tag);
340                         w.AddAttribute ("class", se.Attribute ("class"));
341                         umc.FromXml (w);
342                 }
343
344                 [Test]
345                 public void FromXml_PolicyLevel () 
346                 {
347                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
348                         SecurityElement se = umc.ToXml ();
349                         // is it accepted for all policy levels ?
350                         IEnumerator e = SecurityManager.PolicyHierarchy ();
351                         while (e.MoveNext ()) {
352                                 PolicyLevel pl = e.Current as PolicyLevel;
353                                 UrlMembershipCondition spl = new UrlMembershipCondition ("*");
354                                 spl.FromXml (se, pl);
355                                 Assert.IsTrue (spl.Equals (umc), "FromXml(PolicyLevel='" + pl.Label + "')");
356                         }
357                         // yes!
358                 }
359
360                 [Test]
361                 public void ToXml_Null () 
362                 {
363                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
364                         // no ArgumentNullException here
365                         SecurityElement se = umc.ToXml (null);
366                         Assert.IsNotNull (se, "ToXml(null)");
367                 }
368
369                 [Test]
370                 public void ToXml_PolicyLevel () 
371                 {
372                         UrlMembershipCondition umc = new UrlMembershipCondition ("http://www.go-mono.com");
373                         SecurityElement se = umc.ToXml ();
374                         string s = umc.ToXml ().ToString ();
375                         // is it accepted for all policy levels ?
376                         IEnumerator e = SecurityManager.PolicyHierarchy ();
377                         while (e.MoveNext ()) {
378                                 PolicyLevel pl = e.Current as PolicyLevel;
379                                 UrlMembershipCondition spl = new UrlMembershipCondition ("*");
380                                 spl.FromXml (se, pl);
381                                 Assert.AreEqual (s, spl.ToXml (pl).ToString (), "ToXml(PolicyLevel='" + pl.Label + "')");
382                         }
383                         // yes!
384                 }
385
386                 [Test]
387                 public void ToFromXmlRoundTrip () 
388                 {
389                         UrlMembershipCondition umc1 = new UrlMembershipCondition ("http://www.go-mono.com");
390                         SecurityElement se = umc1.ToXml ();
391
392                         UrlMembershipCondition umc2 = new UrlMembershipCondition ("*");
393                         umc2.FromXml (se);
394
395                         Assert.AreEqual (umc1.GetHashCode (), umc2.GetHashCode (), "ToFromXmlRoundTrip");
396                 }
397         }
398 }