Merge pull request #1909 from esdrubal/reflection
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / ZoneTest.cs
1 //
2 // ZoneTest.cs - NUnit Test Cases for Zone
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 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.IO;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Policy;
36
37 namespace MonoTests.System.Security.Policy {
38
39         [TestFixture]
40         public class ZoneTest  {
41
42                 [Test]
43                 public void MyComputer () 
44                 {
45                         Zone z = new Zone (SecurityZone.MyComputer);
46                         Assert.AreEqual (SecurityZone.MyComputer, z.SecurityZone, "MyComputer.SecurityZone");
47                         Assert.IsTrue ((z.ToString ().IndexOf ("<Zone>MyComputer</Zone>") >= 0), "MyComputer.ToString");
48                         Zone zc = (Zone) z.Copy ();
49                         Assert.IsTrue (z.Equals (zc), "MyComputer.Copy.Equals");
50                         IPermission p = z.CreateIdentityPermission (null);
51                         Assert.IsNotNull (p, "MyComputer.CreateIdentityPermission");
52
53                         Assert.IsTrue (z.Equals (new Zone (SecurityZone.MyComputer)), "MyComputer.MyComputer.Equals");
54                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Intranet)), "MyComputer.Intranet.Equals");
55                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Trusted)), "MyComputer.Trusted.Equals");
56                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Internet)), "MyComputer.Internet.Equals");
57                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Untrusted)), "MyComputer.Untrusted.Equals");
58                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.NoZone)), "MyComputer.NoZone.Equals");
59                         Assert.IsTrue (!z.Equals (null), "MyComputer.Null.Equals");
60                 }
61
62                 [Test]
63                 public void Intranet () 
64                 {
65                         Zone z = new Zone (SecurityZone.Intranet);
66                         Assert.AreEqual (SecurityZone.Intranet, z.SecurityZone, "Intranet.SecurityZone");
67                         Assert.IsTrue ((z.ToString ().IndexOf ("<Zone>Intranet</Zone>") >= 0), "Intranet.ToString");
68                         Zone zc = (Zone) z.Copy ();
69                         Assert.IsTrue (z.Equals (zc), "Intranet.Copy.Equals");
70                         IPermission p = z.CreateIdentityPermission (null);
71                         Assert.IsNotNull (p, "Intranet.CreateIdentityPermission");
72
73                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.MyComputer)), "Intranet.MyComputer.Equals");
74                         Assert.IsTrue (z.Equals (new Zone (SecurityZone.Intranet)), "Intranet.Intranet.Equals");
75                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Trusted)), "Intranet.Trusted.Equals");
76                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Internet)), "Intranet.Internet.Equals");
77                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Untrusted)), "Intranet.Untrusted.Equals");
78                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.NoZone)), "Intranet.NoZone.Equals");
79                         Assert.IsTrue (!z.Equals (null), "Intranet.Null.Equals");
80                 }
81
82                 [Test]
83                 public void Trusted () 
84                 {
85                         Zone z = new Zone (SecurityZone.Trusted);
86                         Assert.AreEqual (SecurityZone.Trusted, z.SecurityZone, "Trusted.SecurityZone");
87                         Assert.IsTrue ((z.ToString ().IndexOf ("<Zone>Trusted</Zone>") >= 0), "Trusted.ToString");
88                         Zone zc = (Zone) z.Copy ();
89                         Assert.IsTrue (z.Equals (zc), "Trusted.Copy.Equals");
90                         IPermission p = z.CreateIdentityPermission (null);
91                         Assert.IsNotNull (p, "Trusted.CreateIdentityPermission");
92
93                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.MyComputer)), "Trusted.MyComputer.Equals");
94                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Intranet)), "Trusted.Intranet.Equals");
95                         Assert.IsTrue (z.Equals (new Zone (SecurityZone.Trusted)), "Trusted.Trusted.Equals");
96                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Internet)), "Trusted.Internet.Equals");
97                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Untrusted)), "Trusted.Untrusted.Equals");
98                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.NoZone)), "Trusted.NoZone.Equals");
99                         Assert.IsTrue (!z.Equals (null), "Trusted.Null.Equals");
100                 }
101
102                 [Test]
103                 public void Internet () 
104                 {
105                         Zone z = new Zone (SecurityZone.Internet);
106                         Assert.AreEqual (SecurityZone.Internet, z.SecurityZone, "Internet.SecurityZone");
107                         Assert.IsTrue ((z.ToString ().IndexOf ("<Zone>Internet</Zone>") >= 0), "Internet.ToString");
108                         Zone zc = (Zone) z.Copy ();
109                         Assert.IsTrue (z.Equals (zc), "Internet.Copy.Equals");
110                         IPermission p = z.CreateIdentityPermission (null);
111                         Assert.IsNotNull (p, "Internet.CreateIdentityPermission");
112
113                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.MyComputer)), "Internet.MyComputer.Equals");
114                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Intranet)), "Internet.Intranet.Equals");
115                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Trusted)), "Internet.Trusted.Equals");
116                         Assert.IsTrue (z.Equals (new Zone (SecurityZone.Internet)), "Internet.Internet.Equals");
117                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Untrusted)), "Internet.Untrusted.Equals");
118                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.NoZone)), "Internet.NoZone.Equals");
119                         Assert.IsTrue (!z.Equals (null), "Internet.Null.Equals");
120                 }
121
122                 [Test]
123                 public void Untrusted () 
124                 {
125                         Zone z = new Zone (SecurityZone.Untrusted);
126                         Assert.AreEqual (SecurityZone.Untrusted, z.SecurityZone, "Untrusted.SecurityZone");
127                         Assert.IsTrue ((z.ToString ().IndexOf ("<Zone>Untrusted</Zone>") >= 0), "Untrusted.ToString");
128                         Zone zc = (Zone) z.Copy ();
129                         Assert.IsTrue (z.Equals (zc), "Untrusted.Copy.Equals");
130                         IPermission p = z.CreateIdentityPermission (null);
131                         Assert.IsNotNull (p, "Untrusted.CreateIdentityPermission");
132
133                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.MyComputer)), "Untrusted.MyComputer.Equals");
134                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Intranet)), "Untrusted.Intranet.Equals");
135                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Trusted)), "Untrusted.Trusted.Equals");
136                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Internet)), "Untrusted.Internet.Equals");
137                         Assert.IsTrue (z.Equals (new Zone (SecurityZone.Untrusted)), "Untrusted.Untrusted.Equals");
138                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.NoZone)), "Untrusted.NoZone.Equals");
139                         Assert.IsTrue (!z.Equals (null), "Untrusted.Null.Equals");
140                 }
141
142                 [Test]
143                 public void NoZone () 
144                 {
145                         Zone z = new Zone (SecurityZone.NoZone);
146                         Assert.AreEqual (SecurityZone.NoZone, z.SecurityZone, "NoZone.SecurityZone");
147                         Assert.IsTrue ((z.ToString ().IndexOf ("<Zone>NoZone</Zone>") >= 0), "NoZone.ToString");
148                         Zone zc = (Zone) z.Copy ();
149                         Assert.IsTrue (z.Equals (zc), "NoZone.Copy.Equals");
150                         IPermission p = z.CreateIdentityPermission (null);
151                         Assert.IsNotNull (p, "NoZone.CreateIdentityPermission");
152                         // NoZone isn't added to the XML / string of permissions
153                         Assert.IsTrue (p.ToString ().IndexOf ("NoZone") < 0, "ToString!=NoZone");
154
155                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.MyComputer)), "NoZone.MyComputer.Equals");
156                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Intranet)), "NoZone.Intranet.Equals");
157                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Trusted)), "NoZone.Trusted.Equals");
158                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Internet)), "NoZone.Internet.Equals");
159                         Assert.IsTrue (!z.Equals (new Zone (SecurityZone.Untrusted)), "NoZone.Untrusted.Equals");
160                         Assert.IsTrue (z.Equals (new Zone (SecurityZone.NoZone)), "NoZone.NoZone.Equals");
161                         Assert.IsTrue (!z.Equals (null), "NoZone.Null.Equals");
162                 }
163
164                 [Test]
165                 [ExpectedException (typeof (ArgumentNullException))]
166                 public void CreateFromUrl_Null ()
167                 {
168                         Zone.CreateFromUrl (null);
169                 }
170
171                 string[] noZoneUrls = {
172                         String.Empty,                   // not accepted for a Site
173                 };
174
175                 [Test]
176                 public void CreateFromUrl_NoZone ()
177                 {
178                         foreach (string url in noZoneUrls) {
179                                 Zone z = Zone.CreateFromUrl (url);
180                                 Assert.AreEqual (SecurityZone.NoZone, z.SecurityZone, url);
181                         }
182                 }
183
184                 // files are always rooted (Path.IsPathRooted) and exists (File.Exists)
185                 string[] myComputerUrls = {
186                         Path.GetTempFileName (),
187 #if !MONODROID
188                         // Assembly.Location doesn't work on Android
189                         Assembly.GetExecutingAssembly ().Location,
190 #endif
191                 };
192
193                 [Test]
194                 public void CreateFromUrl_MyComputer ()
195                 {
196                         foreach (string u in myComputerUrls) {
197                                 string url = u;
198                                 Zone z = Zone.CreateFromUrl (url);
199                                 Assert.AreEqual (SecurityZone.MyComputer, z.SecurityZone, url);
200
201                                 url = "file://" + u;
202                                 z = Zone.CreateFromUrl (url);
203                                 Assert.AreEqual (SecurityZone.MyComputer, z.SecurityZone, url);
204
205                                 url = "FILE://" + u;
206                                 z = Zone.CreateFromUrl (url);
207                                 Assert.AreEqual (SecurityZone.MyComputer, z.SecurityZone, url);
208                         }
209                 }
210
211                 string[] intranetUrls = {
212                         "file://mono/index.html",       // file:// isn't supported as a site
213                         "FILE://MONO/INDEX.HTML",
214                 };
215
216                 [Test]
217                 public void CreateFromUrl_Intranet ()
218                 {
219                         foreach (string url in intranetUrls) {
220                                 Zone z = Zone.CreateFromUrl (url);
221                                 Assert.AreEqual (SecurityZone.Intranet, z.SecurityZone, url);
222                         }
223                 }
224
225                 string[] internetUrls = {
226                         "http://www.go-mono.com",
227                         "http://64.14.94.188/",
228                         "HTTP://WWW.GO-MONO.COM",
229                         "http://*.go-mono.com",
230                         "http://www.go-mono.com:8080/index.html",
231                         "mono://unknown/protocol",
232                         Path.DirectorySeparatorChar + "mono" + Path.DirectorySeparatorChar + "index.html",
233                 };
234
235                 [Test]
236                 public void CreateFromUrl_Internet ()
237                 {
238                         foreach (string url in internetUrls) {
239                                 Zone z = Zone.CreateFromUrl (url);
240                                 Assert.AreEqual (SecurityZone.Internet, z.SecurityZone, url);
241                         }
242                 }
243
244                 [Test]
245                 public void ToString_ ()
246                 {
247                         Zone z = Zone.CreateFromUrl (String.Empty);
248                         string ts = z.ToString ();
249                         Assert.IsTrue (ts.StartsWith ("<System.Security.Policy.Zone"), "Class");
250                         Assert.IsTrue ((ts.IndexOf (" version=\"1\"") >= 0), "Version");
251                         Assert.IsTrue ((ts.IndexOf ("<Zone>NoZone</Zone>") >= 0), "Zone");
252                         Assert.IsTrue ((ts.IndexOf ("</System.Security.Policy.Zone>") >= 0), "End");
253                 }
254         }
255 }