[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / Test / System.Security.Permissions / UrlIdentityPermissionTest.cs
blob9a79fbe2a6f6003be86bacdcc1c7b857fbc94f82
1 //
2 // UrlIdentityPermissionTest.cs - NUnit Test Cases for UrlIdentityPermission
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit.Framework;
30 using System;
31 using System.Security;
32 using System.Security.Permissions;
34 using System.Diagnostics;
36 namespace MonoTests.System.Security.Permissions {
38 [TestFixture]
39 public class UrlIdentityPermissionTest {
41 static string[] GoodUrls = {
42 "http://www.mono-project.com:80/",
43 "http://www.mono-project.com/",
44 "http://www.mono-project.com",
45 "www.mono-project.com",
46 "www.novell.com",
47 "*.mono-project.com",
48 "*www.mono-project.com",
49 "*-project.com",
50 "*.com",
51 "*",
54 [Test]
55 public void PermissionState_None ()
57 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
58 // that cause a NullReferenceException before 2.0
59 Assert.AreEqual (String.Empty, uip.Url, "Url");
60 SecurityElement se = uip.ToXml ();
61 // only class and version are present
62 Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
63 Assert.IsNull (se.Children, "Xml-Children");
64 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
65 Assert.IsFalse (Object.ReferenceEquals (uip, copy), "ReferenceEquals");
69 [Test]
70 [Category ("NotWorking")]
71 public void PermissionStateUnrestricted ()
73 // In 2.0 Unrestricted are permitted for identity permissions
74 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.Unrestricted);
75 Assert.AreEqual (String.Empty, uip.Url, "Url");
76 SecurityElement se = uip.ToXml ();
77 // only class and version are present
78 Assert.AreEqual (3, se.Attributes.Count, "Xml-Attributes");
79 Assert.IsNull (se.Children, "Xml-Children");
80 // and they aren't equals to None
81 Assert.IsFalse (uip.Equals (new UrlIdentityPermission (PermissionState.None)));
83 [Test]
84 [ExpectedException (typeof (ArgumentException))]
85 public void PermissionState_Bad ()
87 UrlIdentityPermission uip = new UrlIdentityPermission ((PermissionState)Int32.MinValue);
90 [Test]
91 [ExpectedException (typeof (ArgumentNullException))]
92 public void UrlIdentityPermission_NullUrl ()
94 UrlIdentityPermission uip = new UrlIdentityPermission (null);
97 [Test]
98 public void Url ()
100 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
101 foreach (string s in GoodUrls) {
102 uip.Url = s;
103 Assert.AreEqual (s, uip.Url, s);
107 [Test]
108 public void Url_Null ()
110 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
111 uip.Url = null;
112 Assert.AreEqual (String.Empty, uip.Url, "Url");
115 [Test]
116 public void Copy ()
118 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
119 foreach (string s in GoodUrls) {
120 uip.Url = s;
121 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
122 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
123 // so we only compare the start of the url
124 Assert.AreEqual (uip.Url, copy.Url, "Url");
128 [Test]
129 public void Copy_None ()
131 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
132 UrlIdentityPermission copy = (UrlIdentityPermission)uip.Copy ();
135 [Test]
136 public void Intersect_Null ()
138 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
139 // No intersection with null
140 foreach (string s in GoodUrls) {
141 uip.Url = s;
142 Assert.IsNull (uip.Intersect (null), s);
145 [Category ("NotWorking")]
146 [Test]
147 public void Intersect_None ()
149 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
150 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
151 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
152 Assert.IsNull (result, "None N None");
153 foreach (string s in GoodUrls) {
154 uip1.Url = s;
155 // 1. Intersect None with Url
156 result = (UrlIdentityPermission)uip1.Intersect (uip2);
157 Assert.IsNull (result, "None N " + s);
158 // 2. Intersect Url with None
159 result = (UrlIdentityPermission)uip2.Intersect (uip1);
160 Assert.IsNull (result, s + "N None");
164 [Test]
165 public void Intersect_Self ()
167 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
168 foreach (string s in GoodUrls) {
169 uip.Url = s;
170 UrlIdentityPermission result = (UrlIdentityPermission)uip.Intersect (uip);
171 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
172 // so we only compare the start of the url
173 Assert.IsTrue (result.Url.StartsWith (uip.Url), s);
177 [Test]
178 public void Intersect_Different ()
180 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
181 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
182 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Intersect (uip2);
183 Assert.IsNull (result, "Mono N Novell");
186 [Test]
187 public void IsSubset_Null ()
189 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
190 Assert.IsTrue (uip.IsSubsetOf (null), "Empty");
191 foreach (string s in GoodUrls) {
192 uip.Url = s;
193 Assert.IsFalse (uip.IsSubsetOf (null), s);
197 [Category ("NotWorking")]
198 [Test]
199 public void IsSubset_None ()
201 // IsSubset with none
202 // a. source (this) is none -> target is never a subset
203 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
204 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
205 foreach (string s in GoodUrls) {
206 uip1.Url = s;
207 Assert.IsFalse (uip1.IsSubsetOf (uip2), "target " + s);
209 uip1 = new UrlIdentityPermission (PermissionState.None);
210 // b. destination (target) is none -> target is always a subset
211 foreach (string s in GoodUrls) {
212 uip2.Url = s;
213 Assert.IsFalse (uip2.IsSubsetOf (uip1), "source " + s);
217 [Test]
218 public void IsSubset_Self ()
220 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
221 Assert.IsTrue (uip.IsSubsetOf (uip), "None");
222 foreach (string s in GoodUrls) {
223 uip.Url = s;
224 Assert.IsTrue (uip.IsSubsetOf (uip), s);
228 [Test]
229 public void IsSubset_Different ()
231 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
232 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
233 Assert.IsFalse (uip1.IsSubsetOf (uip2), "Mono subset Novell");
234 Assert.IsFalse (uip2.IsSubsetOf (uip1), "Novell subset Mono");
237 [Test]
238 public void Union_Null ()
240 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
241 // Union with null is a simple copy
242 foreach (string s in GoodUrls) {
243 uip.Url = s;
244 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (null);
245 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
246 // so we only compare the start of the url
247 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
251 [Test]
252 public void Union_None ()
254 // Union with none is same
255 UrlIdentityPermission uip1 = new UrlIdentityPermission (PermissionState.None);
256 UrlIdentityPermission uip2 = new UrlIdentityPermission (PermissionState.None);
257 // a. source (this) is none
258 foreach (string s in GoodUrls) {
259 uip1.Url = s;
260 UrlIdentityPermission union = (UrlIdentityPermission)uip1.Union (uip2);
261 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
262 // so we only compare the start of the url
263 Assert.IsTrue (union.Url.StartsWith (uip1.Url), s);
265 uip1 = new UrlIdentityPermission (PermissionState.None);
266 // b. destination (target) is none
267 foreach (string s in GoodUrls) {
268 uip2.Url = s;
269 UrlIdentityPermission union = (UrlIdentityPermission)uip2.Union (uip1);
270 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
271 // so we only compare the start of the url
272 Assert.IsTrue (union.Url.StartsWith (uip2.Url), s);
276 [Test]
277 public void Union_Self ()
279 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
280 UrlIdentityPermission union = (UrlIdentityPermission)uip.Union (uip);
281 Assert.IsNull (union, "None U None");
282 foreach (string s in GoodUrls) {
283 uip.Url = s;
284 union = (UrlIdentityPermission)uip.Union (uip);
285 // Fx 1.0/1.1 adds a '/' at the end, while 2.0 keeps the original format
286 // so we only compare the start of the url
287 Assert.IsTrue (union.Url.StartsWith (uip.Url), s);
290 [Category ("NotWorking")]
291 [Test]
292 public void Union_Different ()
294 UrlIdentityPermission uip1 = new UrlIdentityPermission (GoodUrls [0]);
295 UrlIdentityPermission uip2 = new UrlIdentityPermission (GoodUrls [1]);
296 UrlIdentityPermission result = (UrlIdentityPermission)uip1.Union (uip2);
297 Assert.IsNotNull (result, "Mono U Novell");
298 // new XML format is used to contain more than one site
299 SecurityElement se = result.ToXml ();
300 Assert.AreEqual (2, se.Children.Count, "Childs");
301 Assert.AreEqual (GoodUrls [0], (se.Children [0] as SecurityElement).Attribute ("Url"), "Url#1");
302 Assert.AreEqual (GoodUrls [1], (se.Children [1] as SecurityElement).Attribute ("Url"), "Url#2");
303 // strangely it is still versioned as 'version="1"'.
304 Assert.AreEqual ("1", se.Attribute ("version"), "Version");
307 [Test]
308 [ExpectedException (typeof (ArgumentNullException))]
309 public void FromXml_Null ()
311 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
312 uip.FromXml (null);
315 [Test]
316 [ExpectedException (typeof (ArgumentException))]
317 public void FromXml_WrongTag ()
319 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
320 SecurityElement se = uip.ToXml ();
321 se.Tag = "IMono";
322 uip.FromXml (se);
325 [Test]
326 [ExpectedException (typeof (ArgumentException))]
327 public void FromXml_WrongTagCase ()
329 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
330 SecurityElement se = uip.ToXml ();
331 se.Tag = "IPERMISSION"; // instead of IPermission
332 uip.FromXml (se);
335 [Test]
336 public void FromXml_WrongClass ()
338 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
339 SecurityElement se = uip.ToXml ();
341 SecurityElement w = new SecurityElement (se.Tag);
342 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
343 w.AddAttribute ("version", se.Attribute ("version"));
344 uip.FromXml (w);
345 // doesn't care of the class name at that stage
346 // anyway the class has already be created so...
349 [Test]
350 public void FromXml_NoClass ()
352 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
353 SecurityElement se = uip.ToXml ();
355 SecurityElement w = new SecurityElement (se.Tag);
356 w.AddAttribute ("version", se.Attribute ("version"));
357 uip.FromXml (w);
358 // doesn't even care of the class attribute presence
361 [Test]
362 [ExpectedException (typeof (ArgumentException))]
363 public void FromXml_WrongVersion ()
365 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
366 SecurityElement se = uip.ToXml ();
367 se.Attributes.Remove ("version");
368 se.Attributes.Add ("version", "2");
369 uip.FromXml (se);
372 [Test]
373 public void FromXml_NoVersion ()
375 UrlIdentityPermission uip = new UrlIdentityPermission (PermissionState.None);
376 SecurityElement se = uip.ToXml ();
378 SecurityElement w = new SecurityElement (se.Tag);
379 w.AddAttribute ("class", se.Attribute ("class"));
380 uip.FromXml (w);