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");
83 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");
85 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");
87 Site s2
= (Site
) s
.Copy ();
88 Assert
.AreEqual (s
.Name
, s2
.Name
, "Copy.Name");
89 Assert
.AreEqual (s
.GetHashCode (), s2
.GetHashCode (), "Copy.GetHashCode");
91 SiteIdentityPermission sip
= (SiteIdentityPermission
) s
.CreateIdentityPermission (null);
92 Assert
.AreEqual (s
.Name
, sip
.Site
, "CreateIdentityPermission");
94 Assert
.IsTrue (s
.Equals (s2
), "Equals");
95 Site s3
= new Site ("go-mono.com");
96 Assert
.IsTrue (!s
.Equals (s3
), "!Equals");
100 public void Site_AllGoMonoSite ()
102 Site s
= new Site ("*.go-mono.com");
103 Assert
.AreEqual ("*.go-mono.com", s
.Name
, "Name");
105 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");
107 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");
109 Site s2
= (Site
) s
.Copy ();
110 Assert
.AreEqual (s
.Name
, s2
.Name
, "Copy.Name");
111 Assert
.AreEqual (s
.GetHashCode (), s2
.GetHashCode (), "Copy.GetHashCode");
113 SiteIdentityPermission sip
= (SiteIdentityPermission
) s
.CreateIdentityPermission (null);
114 Assert
.AreEqual (s
.Name
, sip
.Site
, "CreateIdentityPermission");
116 Assert
.IsTrue (s
.Equals (s2
), "Equals");
117 Site s3
= new Site ("go-mono.com");
118 Assert
.IsTrue (!s
.Equals (s3
), "!Equals");
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
.IsTrue (s1
.Equals (s2
), "CaseSensitive");
143 public void EqualsPartial ()
145 Site s1
= new Site ("www.go-mono.com");
146 Site s2
= new Site ("*.go-mono.com");
147 Assert
.IsTrue (!s1
.Equals (s2
), "Partial:1-2");
148 Assert
.IsTrue (!s2
.Equals (s1
), "Partial:2-1");
152 public void EqualsNull ()
154 Site s1
= new Site ("*.go-mono.com");
155 Assert
.IsTrue (!s1
.Equals (null), "EqualsNull");
159 public void Site_LoneStar ()
161 Site s
= new Site ("*");
162 Assert
.AreEqual ("*", s
.Name
, "Name");
164 Assert
.AreEqual ("<System.Security.Policy.Site version=\"1\">" + Environment
.NewLine
+ "<Name>*</Name>" + Environment
.NewLine
+ "</System.Security.Policy.Site>" + Environment
.NewLine
, s
.ToString (), "ToString");
166 Assert
.AreEqual ("<System.Security.Policy.Site version=\"1\">" + Environment
.NewLine
+ " <Name>*</Name>" + Environment
.NewLine
+ "</System.Security.Policy.Site>" + Environment
.NewLine
, s
.ToString (), "ToString");
168 Site s2
= (Site
) s
.Copy ();
169 Assert
.AreEqual (s
.Name
, s2
.Name
, "Copy.Name");
170 Assert
.AreEqual (s
.GetHashCode (), s2
.GetHashCode (), "Copy.GetHashCode");
172 SiteIdentityPermission sip
= (SiteIdentityPermission
) s
.CreateIdentityPermission (null);
173 Assert
.AreEqual (s
.Name
, sip
.Site
, "CreateIdentityPermission");
175 Assert
.IsTrue (s
.Equals (s2
), "Equals");
176 Site s3
= new Site ("go-mono.com");
177 Assert
.IsTrue (!s
.Equals (s3
), "!Equals");
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
.IsTrue ((actual
== result
), "#"+i
);
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
.IsTrue ((s
.Name
.ToUpper (CultureInfo
.InvariantCulture
).IndexOf ("MONO") != -1), s
.Name
);
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
);