* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / SiteTest.cs
1 //
2 // SiteTest.cs - NUnit Test Cases for Site
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.Globalization;
33 using System.Security;
34 using System.Security.Permissions;
35 using System.Security.Policy;
36
37 namespace MonoTests.System.Security.Policy {
38
39         [TestFixture]
40         public class SiteTest : Assertion {
41
42                 [Test]
43                 [ExpectedException (typeof (ArgumentNullException))]
44                 public void Site_Null () 
45                 {
46                         Site s = new Site (null);
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentException))]
51                 public void Site_Empty () 
52                 {
53                         Site s = new Site (String.Empty);
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentException))]
58                 public void Site_FileUrl () 
59                 {
60                         Site s = new Site ("file://mono/index.html");
61                 }
62
63                 [Test]
64                 [ExpectedException (typeof (ArgumentException))]
65                 public void Site_AllGoMono () 
66                 {
67                         Site s = new Site ("http://*.go-mono.com");
68                 }
69
70                 [Test]
71                 [ExpectedException (typeof (ArgumentException))]
72                 public void Site_FullUrlWithPort () 
73                 {
74                         Site s = new Site ("http://www.go-mono.com:8080/index.html");
75                 }
76
77                 [Test]
78                 public void Site_GoMonoWebSite () 
79                 {
80                         Site s = new Site ("www.go-mono.com");
81                         AssertEquals ("Name", "www.go-mono.com", s.Name);
82 #if NET_2_0
83                         AssertEquals ("ToString", "<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "<Name>www.go-mono.com</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString ());
84 #else
85                         AssertEquals ("ToString", "<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "   <Name>www.go-mono.com</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString ());
86 #endif
87                         Site s2 = (Site) s.Copy ();
88                         AssertEquals ("Copy.Name", s.Name, s2.Name);
89                         AssertEquals ("Copy.GetHashCode", s.GetHashCode (), s2.GetHashCode ());
90
91                         SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
92                         AssertEquals ("CreateIdentityPermission", s.Name, sip.Site);
93
94                         Assert ("Equals", s.Equals (s2));
95                         Site s3 = new Site ("go-mono.com");
96                         Assert ("!Equals", !s.Equals (s3));
97                 }
98
99                 [Test]
100                 public void Site_AllGoMonoSite () 
101                 {
102                         Site s = new Site ("*.go-mono.com");
103                         AssertEquals ("Name", "*.go-mono.com", s.Name);
104 #if NET_2_0
105                         AssertEquals ("ToString", "<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "<Name>*.go-mono.com</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString ());
106 #else
107                         AssertEquals ("ToString", "<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "   <Name>*.go-mono.com</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString ());
108 #endif
109                         Site s2 = (Site) s.Copy ();
110                         AssertEquals ("Copy.Name", s.Name, s2.Name);
111                         AssertEquals ("Copy.GetHashCode", s.GetHashCode (), s2.GetHashCode ());
112
113                         SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
114                         AssertEquals ("CreateIdentityPermission", s.Name, sip.Site);
115
116                         Assert ("Equals", s.Equals (s2));
117                         Site s3 = new Site ("go-mono.com");
118                         Assert ("!Equals", !s.Equals (s3));
119                 }
120
121                 [Test]
122                 [ExpectedException (typeof (ArgumentException))]
123                 public void Site_GoMonoAllTLD () 
124                 {
125                         Site s = new Site ("www.go-mono.*");
126                 }
127
128                 [Test]
129                 [ExpectedException (typeof (ArgumentException))]
130                 public void Site_TwoStars () 
131                 {
132                         Site s = new Site ("*.*.go-mono.com");
133                 }
134
135                 [Test]
136                 public void EqualsCaseSensitive () {
137                         Site s1 = new Site ("*.go-mono.com");
138                         Site s2 = new Site ("*.Go-Mono.com");
139                         Assert ("CaseSensitive", s1.Equals (s2));
140                 }
141
142                 [Test]
143                 public void EqualsPartial () 
144                 {
145                         Site s1 = new Site ("www.go-mono.com");
146                         Site s2 = new Site ("*.go-mono.com");
147                         Assert ("Partial:1-2", !s1.Equals (s2));
148                         Assert ("Partial:2-1", !s2.Equals (s1));
149                 }
150
151                 [Test]
152                 public void EqualsNull () 
153                 {
154                         Site s1 = new Site ("*.go-mono.com");
155                         Assert ("EqualsNull", !s1.Equals (null));
156                 }
157
158                 [Test]
159                 public void Site_LoneStar () 
160                 {
161                         Site s = new Site ("*");
162                         AssertEquals ("Name", "*", s.Name);
163 #if NET_2_0
164                         AssertEquals ("ToString", "<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "<Name>*</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString ());
165 #else
166                         AssertEquals ("ToString", "<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "   <Name>*</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString ());
167 #endif
168                         Site s2 = (Site) s.Copy ();
169                         AssertEquals ("Copy.Name", s.Name, s2.Name);
170                         AssertEquals ("Copy.GetHashCode", s.GetHashCode (), s2.GetHashCode ());
171
172                         SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
173                         AssertEquals ("CreateIdentityPermission", s.Name, sip.Site);
174
175                         Assert ("Equals", s.Equals (s2));
176                         Site s3 = new Site ("go-mono.com");
177                         Assert ("!Equals", !s.Equals (s3));
178                 }
179
180                 [Test]
181                 public void AllChars () 
182                 {
183                         for (int i=1; i < 256; i++) {
184                                 bool actual = false;
185                                 char c = Convert.ToChar (i);
186                                 try {
187                                         Site s = new Site (Convert.ToString (c));
188                                         actual = true;
189                                         // Console.WriteLine ("GOOD: {0} - {1}", i, c);
190                                 }
191                                 catch {
192                                         // Console.WriteLine ("FAIL: {0} - {1}", i, c);
193                                 }
194                                 bool result = ((i == 45)                // -
195 #if NET_2_0
196                                         || (i == 33)                    // !
197                                         || (i >= 35 && i <= 42)         // #$%&'()*
198                                         || (i >= 48 && i <= 57)         // 0-9
199                                         || (i >= 94 && i <= 95)         // ^_
200                                         || (i >= 97 && i <= 123)        // a-z{
201                                         || (i >= 125 && i <= 126)       // }~
202 #else
203                                         || (i == 42)                    // *
204                                         || (i >= 47 && i <= 57)         // /,0-9
205                                         || (i == 95)                    // _
206                                         || (i >= 97 && i <= 122)        // a-z
207 #endif
208                                         || (i >= 64 && i <= 90));       // @,A-Z
209                                 Assert ("#"+i, (actual == result));
210                         }
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (ArgumentNullException))]
215                 public void CreateFromUrl_Null ()
216                 {
217                         Site.CreateFromUrl (null);
218                 }
219
220                 [Test]
221                 [ExpectedException (typeof (FormatException))]
222                 public void CreateFromUrl_Empty ()
223                 {
224                         Site.CreateFromUrl (String.Empty);
225                 }
226
227                 string[] valid_urls = {
228                         "http://www.go-mono.com",
229                         "http://*.go-mono.com",
230                         "http://www.go-mono.com:8080/index.html",
231 #if !NET_2_0
232                         "file://mono/index.html",       // file:// is supported as a site (1.0/1.1)
233 #endif
234                 };
235
236                 [Test]
237                 public void CreateFromUrl_Valid () 
238                 {
239                         foreach (string url in valid_urls) {
240                                 Site s = Site.CreateFromUrl (url);
241                                 Assert (s.Name, (s.Name.ToUpper (CultureInfo.InvariantCulture).IndexOf ("MONO") != -1));
242                         }
243                 }
244
245 #if NET_2_0
246                 string[] invalid_urls = {
247                         "file://mono/index.html",       // file:// isn't supported as a site (2.0)
248                 };
249
250                 [Test]
251                 public void CreateFromUrl_Invalid ()
252                 {
253                         string msg = null;
254                         foreach (string url in invalid_urls) {
255                                 try {
256                                         Site.CreateFromUrl (url);
257                                         msg = String.Format ("Expected ArgumentException for {0} but got none", url);
258                                 }
259                                 catch (ArgumentException) {
260                                 }
261                                 catch (Exception e) {
262                                         msg = String.Format ("Expected ArgumentException for {0} but got: {1}", url, e);
263                                 }
264                                 finally {
265                                         if (msg != null) {
266                                                 Fail (msg);
267                                                 msg = null;
268                                         }
269                                 }
270                         }
271                 }
272 #endif
273         }
274 }