2 // SiteTest.cs - NUnit Test Cases for Site
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
32 using System.Globalization;
33 using System.Security;
34 using System.Security.Permissions;
35 using System.Security.Policy;
37 namespace MonoTests.System.Security.Policy {
40 public class SiteTest : Assertion {
43 [ExpectedException (typeof (ArgumentNullException))]
44 public void Site_Null ()
46 Site s = new Site (null);
50 [ExpectedException (typeof (ArgumentException))]
51 public void Site_Empty ()
53 Site s = new Site (String.Empty);
57 [ExpectedException (typeof (ArgumentException))]
58 public void Site_FileUrl ()
60 Site s = new Site ("file://mono/index.html");
64 [ExpectedException (typeof (ArgumentException))]
65 public void Site_AllGoMono ()
67 Site s = new Site ("http://*.go-mono.com");
71 [ExpectedException (typeof (ArgumentException))]
72 public void Site_FullUrlWithPort ()
74 Site s = new Site ("http://www.go-mono.com:8080/index.html");
78 public void Site_GoMonoWebSite ()
80 Site s = new Site ("www.go-mono.com");
81 AssertEquals ("Name", "www.go-mono.com", s.Name);
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 ());
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 ());
87 Site s2 = (Site) s.Copy ();
88 AssertEquals ("Copy.Name", s.Name, s2.Name);
89 AssertEquals ("Copy.GetHashCode", s.GetHashCode (), s2.GetHashCode ());
91 SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
92 AssertEquals ("CreateIdentityPermission", s.Name, sip.Site);
94 Assert ("Equals", s.Equals (s2));
95 Site s3 = new Site ("go-mono.com");
96 Assert ("!Equals", !s.Equals (s3));
100 public void Site_AllGoMonoSite ()
102 Site s = new Site ("*.go-mono.com");
103 AssertEquals ("Name", "*.go-mono.com", s.Name);
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 ());
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 ());
109 Site s2 = (Site) s.Copy ();
110 AssertEquals ("Copy.Name", s.Name, s2.Name);
111 AssertEquals ("Copy.GetHashCode", s.GetHashCode (), s2.GetHashCode ());
113 SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
114 AssertEquals ("CreateIdentityPermission", s.Name, sip.Site);
116 Assert ("Equals", s.Equals (s2));
117 Site s3 = new Site ("go-mono.com");
118 Assert ("!Equals", !s.Equals (s3));
122 [ExpectedException (typeof (ArgumentException))]
123 public void Site_GoMonoAllTLD ()
125 Site s = new Site ("www.go-mono.*");
129 [ExpectedException (typeof (ArgumentException))]
130 public void Site_TwoStars ()
132 Site s = new Site ("*.*.go-mono.com");
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));
143 public void EqualsPartial ()
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));
152 public void EqualsNull ()
154 Site s1 = new Site ("*.go-mono.com");
155 Assert ("EqualsNull", !s1.Equals (null));
159 public void Site_LoneStar ()
161 Site s = new Site ("*");
162 AssertEquals ("Name", "*", s.Name);
164 AssertEquals ("ToString", "<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "<Name>*</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString ());
166 AssertEquals ("ToString", "<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + " <Name>*</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString ());
168 Site s2 = (Site) s.Copy ();
169 AssertEquals ("Copy.Name", s.Name, s2.Name);
170 AssertEquals ("Copy.GetHashCode", s.GetHashCode (), s2.GetHashCode ());
172 SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
173 AssertEquals ("CreateIdentityPermission", s.Name, sip.Site);
175 Assert ("Equals", s.Equals (s2));
176 Site s3 = new Site ("go-mono.com");
177 Assert ("!Equals", !s.Equals (s3));
181 public void AllChars ()
183 for (int i=1; i < 256; i++) {
185 char c = Convert.ToChar (i);
187 Site s = new Site (Convert.ToString (c));
189 // Console.WriteLine ("GOOD: {0} - {1}", i, c);
192 // Console.WriteLine ("FAIL: {0} - {1}", i, c);
194 bool result = ((i == 45) // -
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) // }~
204 || (i >= 47 && i <= 57) // /,0-9
206 || (i >= 97 && i <= 122) // a-z
208 || (i >= 64 && i <= 90)); // @,A-Z
209 Assert ("#"+i, (actual == result));
214 [ExpectedException (typeof (ArgumentNullException))]
215 public void CreateFromUrl_Null ()
217 Site.CreateFromUrl (null);
221 [ExpectedException (typeof (FormatException))]
222 public void CreateFromUrl_Empty ()
224 Site.CreateFromUrl (String.Empty);
227 string[] valid_urls = {
228 "http://www.go-mono.com",
229 "http://*.go-mono.com",
230 "http://www.go-mono.com:8080/index.html",
232 "file://mono/index.html", // file:// is supported as a site (1.0/1.1)
237 public void CreateFromUrl_Valid ()
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));
246 string[] invalid_urls = {
247 "file://mono/index.html", // file:// isn't supported as a site (2.0)
251 public void CreateFromUrl_Invalid ()
254 foreach (string url in invalid_urls) {
256 Site.CreateFromUrl (url);
257 msg = String.Format ("Expected ArgumentException for {0} but got none", url);
259 catch (ArgumentException) {
261 catch (Exception e) {
262 msg = String.Format ("Expected ArgumentException for {0} but got: {1}", url, e);