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 {
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 Assert.AreEqual ("www.go-mono.com", s.Name, "Name");
82 Assert.AreEqual ("<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "<Name>www.go-mono.com</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString (), "ToString");
83 Site s2 = (Site) s.Copy ();
84 Assert.AreEqual (s.Name, s2.Name, "Copy.Name");
85 Assert.AreEqual (s.GetHashCode (), s2.GetHashCode (), "Copy.GetHashCode");
87 SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
88 Assert.AreEqual (s.Name, sip.Site, "CreateIdentityPermission");
90 Assert.IsTrue (s.Equals (s2), "Equals");
91 Site s3 = new Site ("go-mono.com");
92 Assert.IsTrue (!s.Equals (s3), "!Equals");
96 public void Site_AllGoMonoSite ()
98 Site s = new Site ("*.go-mono.com");
99 Assert.AreEqual ("*.go-mono.com", s.Name, "Name");
100 Assert.AreEqual ("<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "<Name>*.go-mono.com</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString (), "ToString");
101 Site s2 = (Site) s.Copy ();
102 Assert.AreEqual (s.Name, s2.Name, "Copy.Name");
103 Assert.AreEqual (s.GetHashCode (), s2.GetHashCode (), "Copy.GetHashCode");
105 SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
106 Assert.AreEqual (s.Name, sip.Site, "CreateIdentityPermission");
108 Assert.IsTrue (s.Equals (s2), "Equals");
109 Site s3 = new Site ("go-mono.com");
110 Assert.IsTrue (!s.Equals (s3), "!Equals");
114 [ExpectedException (typeof (ArgumentException))]
115 public void Site_GoMonoAllTLD ()
117 Site s = new Site ("www.go-mono.*");
121 [ExpectedException (typeof (ArgumentException))]
122 public void Site_TwoStars ()
124 Site s = new Site ("*.*.go-mono.com");
128 public void EqualsCaseSensitive () {
129 Site s1 = new Site ("*.go-mono.com");
130 Site s2 = new Site ("*.Go-Mono.com");
131 Assert.IsTrue (s1.Equals (s2), "CaseSensitive");
135 public void EqualsPartial ()
137 Site s1 = new Site ("www.go-mono.com");
138 Site s2 = new Site ("*.go-mono.com");
139 Assert.IsTrue (!s1.Equals (s2), "Partial:1-2");
140 Assert.IsTrue (!s2.Equals (s1), "Partial:2-1");
144 public void EqualsNull ()
146 Site s1 = new Site ("*.go-mono.com");
147 Assert.IsTrue (!s1.Equals (null), "EqualsNull");
151 public void Site_LoneStar ()
153 Site s = new Site ("*");
154 Assert.AreEqual ("*", s.Name, "Name");
155 Assert.AreEqual ("<System.Security.Policy.Site version=\"1\">" + Environment.NewLine + "<Name>*</Name>" + Environment.NewLine + "</System.Security.Policy.Site>" + Environment.NewLine, s.ToString (), "ToString");
156 Site s2 = (Site) s.Copy ();
157 Assert.AreEqual (s.Name, s2.Name, "Copy.Name");
158 Assert.AreEqual (s.GetHashCode (), s2.GetHashCode (), "Copy.GetHashCode");
160 SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
161 Assert.AreEqual (s.Name, sip.Site, "CreateIdentityPermission");
163 Assert.IsTrue (s.Equals (s2), "Equals");
164 Site s3 = new Site ("go-mono.com");
165 Assert.IsTrue (!s.Equals (s3), "!Equals");
169 public void AllChars ()
171 for (int i=1; i < 256; i++) {
173 char c = Convert.ToChar (i);
175 Site s = new Site (Convert.ToString (c));
177 // Console.WriteLine ("GOOD: {0} - {1}", i, c);
180 // Console.WriteLine ("FAIL: {0} - {1}", i, c);
182 bool result = ((i == 45) // -
184 || (i >= 35 && i <= 42) // #$%&'()*
185 || (i >= 48 && i <= 57) // 0-9
186 || (i >= 94 && i <= 95) // ^_
187 || (i >= 97 && i <= 123) // a-z{
188 || (i >= 125 && i <= 126) // }~
189 || (i >= 64 && i <= 90)); // @,A-Z
190 Assert.IsTrue ((actual == result), "#"+i);
195 [ExpectedException (typeof (ArgumentNullException))]
196 public void CreateFromUrl_Null ()
198 Site.CreateFromUrl (null);
202 [ExpectedException (typeof (FormatException))]
203 public void CreateFromUrl_Empty ()
205 Site.CreateFromUrl (String.Empty);
208 string[] valid_urls = {
209 "http://www.go-mono.com",
210 "http://*.go-mono.com",
211 "http://www.go-mono.com:8080/index.html",
215 public void CreateFromUrl_Valid ()
217 foreach (string url in valid_urls) {
218 Site s = Site.CreateFromUrl (url);
219 Assert.IsTrue ((s.Name.ToUpper (CultureInfo.InvariantCulture).IndexOf ("MONO") != -1), s.Name);
223 string[] invalid_urls = {
224 "file://mono/index.html", // file:// isn't supported as a site (2.0)
228 public void CreateFromUrl_Invalid ()
231 foreach (string url in invalid_urls) {
233 Site.CreateFromUrl (url);
234 msg = String.Format ("Expected ArgumentException for {0} but got none", url);
236 catch (ArgumentException) {
238 catch (Exception e) {
239 msg = String.Format ("Expected ArgumentException for {0} but got: {1}", url, e);