Merge pull request #5714 from alexischr/update_bockbuild
[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  {
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                         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");
86
87                         SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
88                         Assert.AreEqual (s.Name, sip.Site, "CreateIdentityPermission");
89
90                         Assert.IsTrue (s.Equals (s2), "Equals");
91                         Site s3 = new Site ("go-mono.com");
92                         Assert.IsTrue (!s.Equals (s3), "!Equals");
93                 }
94
95                 [Test]
96                 public void Site_AllGoMonoSite () 
97                 {
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");
104
105                         SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
106                         Assert.AreEqual (s.Name, sip.Site, "CreateIdentityPermission");
107
108                         Assert.IsTrue (s.Equals (s2), "Equals");
109                         Site s3 = new Site ("go-mono.com");
110                         Assert.IsTrue (!s.Equals (s3), "!Equals");
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentException))]
115                 public void Site_GoMonoAllTLD () 
116                 {
117                         Site s = new Site ("www.go-mono.*");
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (ArgumentException))]
122                 public void Site_TwoStars () 
123                 {
124                         Site s = new Site ("*.*.go-mono.com");
125                 }
126
127                 [Test]
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");
132                 }
133
134                 [Test]
135                 public void EqualsPartial () 
136                 {
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");
141                 }
142
143                 [Test]
144                 public void EqualsNull () 
145                 {
146                         Site s1 = new Site ("*.go-mono.com");
147                         Assert.IsTrue (!s1.Equals (null), "EqualsNull");
148                 }
149
150                 [Test]
151                 public void Site_LoneStar () 
152                 {
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");
159
160                         SiteIdentityPermission sip = (SiteIdentityPermission) s.CreateIdentityPermission (null);
161                         Assert.AreEqual (s.Name, sip.Site, "CreateIdentityPermission");
162
163                         Assert.IsTrue (s.Equals (s2), "Equals");
164                         Site s3 = new Site ("go-mono.com");
165                         Assert.IsTrue (!s.Equals (s3), "!Equals");
166                 }
167
168                 [Test]
169                 public void AllChars () 
170                 {
171                         for (int i=1; i < 256; i++) {
172                                 bool actual = false;
173                                 char c = Convert.ToChar (i);
174                                 try {
175                                         Site s = new Site (Convert.ToString (c));
176                                         actual = true;
177                                         // Console.WriteLine ("GOOD: {0} - {1}", i, c);
178                                 }
179                                 catch {
180                                         // Console.WriteLine ("FAIL: {0} - {1}", i, c);
181                                 }
182                                 bool result = ((i == 45)                // -
183                                         || (i == 33)                    // !
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);
191                         }
192                 }
193
194                 [Test]
195                 [ExpectedException (typeof (ArgumentNullException))]
196                 public void CreateFromUrl_Null ()
197                 {
198                         Site.CreateFromUrl (null);
199                 }
200
201                 [Test]
202                 [ExpectedException (typeof (FormatException))]
203                 public void CreateFromUrl_Empty ()
204                 {
205                         Site.CreateFromUrl (String.Empty);
206                 }
207
208                 string[] valid_urls = {
209                         "http://www.go-mono.com",
210                         "http://*.go-mono.com",
211                         "http://www.go-mono.com:8080/index.html",
212                 };
213
214                 [Test]
215                 public void CreateFromUrl_Valid () 
216                 {
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);
220                         }
221                 }
222
223                 string[] invalid_urls = {
224                         "file://mono/index.html",       // file:// isn't supported as a site (2.0)
225                 };
226
227                 [Test]
228                 public void CreateFromUrl_Invalid ()
229                 {
230                         string msg = null;
231                         foreach (string url in invalid_urls) {
232                                 try {
233                                         Site.CreateFromUrl (url);
234                                         msg = String.Format ("Expected ArgumentException for {0} but got none", url);
235                                 }
236                                 catch (ArgumentException) {
237                                 }
238                                 catch (Exception e) {
239                                         msg = String.Format ("Expected ArgumentException for {0} but got: {1}", url, e);
240                                 }
241                                 finally {
242                                         if (msg != null) {
243                                                 Assert.Fail (msg);
244                                                 msg = null;
245                                         }
246                                 }
247                         }
248                 }
249         }
250 }