2004-12-03 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / ZoneIdentityPermissionTest.cs
1 //
2 // ZoneIdentityPermissionTest.cs - NUnit Test Cases for ZoneIdentityPermission
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 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.
27 //
28
29 using NUnit.Framework;
30 using System;
31 using System.Security;
32 using System.Security.Permissions;
33
34 namespace MonoTests.System.Security.Permissions {
35
36         [TestFixture]
37         public class ZoneIdentityPermissionTest {
38
39                 [Test]
40                 public void PermissionStateNone ()
41                 {
42                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
43                         Assert.AreEqual (SecurityZone.NoZone, zip.SecurityZone);
44                 }
45
46                 [Test]
47                 [ExpectedException (typeof (ArgumentException))]
48                 public void PermissionStateUnrestricted ()
49                 {
50                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.Unrestricted);
51                 }
52
53                 [Test]
54                 [ExpectedException (typeof (ArgumentException))]
55                 public void PermissionStateInvalid ()
56                 {
57                         ZoneIdentityPermission zip = new ZoneIdentityPermission ((PermissionState)2);
58                 }
59
60                 private bool Same (ZoneIdentityPermission zip1, ZoneIdentityPermission zip2)
61                 {
62 #if NET_2_0
63                         return zip1.Equals (zip2);
64 #else
65                         return (zip1.SecurityZone == zip2.SecurityZone);
66 #endif
67                 }
68
69                 private ZoneIdentityPermission BasicTestZone (SecurityZone zone, bool special)
70                 {
71                         ZoneIdentityPermission zip = new ZoneIdentityPermission (zone);
72                         Assert.AreEqual (zone, zip.SecurityZone, "SecurityZone");
73                         
74                         ZoneIdentityPermission copy = (ZoneIdentityPermission) zip.Copy ();
75                         Assert.IsTrue (Same (zip, copy), "Equals-Copy");
76                         Assert.IsTrue (zip.IsSubsetOf (copy), "IsSubset-1");
77                         Assert.IsTrue (copy.IsSubsetOf (zip), "IsSubset-2");
78                         if (special) {
79                                 Assert.IsFalse (zip.IsSubsetOf (null), "IsSubset-Null");
80                         }
81                         
82                         IPermission intersect = zip.Intersect (copy);
83                         if (special) {
84                                 Assert.IsTrue (intersect.IsSubsetOf (zip), "IsSubset-3");
85                                 Assert.IsFalse (Object.ReferenceEquals (zip, intersect), "!ReferenceEquals1");
86                                 Assert.IsTrue (intersect.IsSubsetOf (copy), "IsSubset-4");
87                                 Assert.IsFalse (Object.ReferenceEquals (copy, intersect), "!ReferenceEquals2");
88                         }
89
90                         Assert.IsNull (zip.Intersect (null), "Intersect with null");
91
92                         intersect = zip.Intersect (new ZoneIdentityPermission (PermissionState.None));
93                         Assert.IsNull (intersect, "Intersect with PS.None");
94
95                         // note: can't be tested with PermissionState.Unrestricted
96
97                         // XML roundtrip
98                         SecurityElement se = zip.ToXml ();
99                         copy.FromXml (se);
100                         Assert.IsTrue (Same (zip, copy), "Equals-Xml");
101
102                         return zip;
103                 }
104
105                 [Test]
106                 public void SecurityZone_Internet ()
107                 {
108                         BasicTestZone (SecurityZone.Internet, true);
109                 }
110
111                 [Test]
112                 public void SecurityZone_Intranet ()
113                 {
114                         BasicTestZone (SecurityZone.Intranet, true);
115                 }
116
117                 [Test]
118                 public void SecurityZone_MyComputer ()
119                 {
120                         BasicTestZone (SecurityZone.MyComputer, true);
121                 }
122
123                 [Test]
124                 public void SecurityZone_NoZone ()
125                 {
126                         ZoneIdentityPermission zip = BasicTestZone (SecurityZone.NoZone, false);
127                         Assert.IsNull (zip.ToXml ().Attribute ("Zone"), "Zone Attribute");
128                         Assert.IsTrue (zip.IsSubsetOf (null), "IsSubset-Null");
129                         IPermission intersect = zip.Intersect (zip);
130                         Assert.IsNull (intersect, "Intersect with No Zone");
131                         // NoZone is special as it is a subset of all zones
132                         ZoneIdentityPermission ss = new ZoneIdentityPermission (SecurityZone.Internet);
133                         Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-Internet");
134                         ss.SecurityZone = SecurityZone.Intranet;
135                         Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-Intranet");
136                         ss.SecurityZone = SecurityZone.MyComputer;
137                         Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-MyComputer");
138                         ss.SecurityZone = SecurityZone.NoZone;
139                         Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-NoZone");
140                         ss.SecurityZone = SecurityZone.Trusted;
141                         Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-Trusted");
142                         ss.SecurityZone = SecurityZone.Untrusted;
143                         Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-Untrusted");
144                 }
145
146                 [Test]
147                 public void SecurityZone_Trusted ()
148                 {
149                         BasicTestZone (SecurityZone.Trusted, true);
150                 }
151
152                 [Test]
153                 public void SecurityZone_Untrusted ()
154                 {
155                         BasicTestZone (SecurityZone.Untrusted, true);
156                 }
157
158                 [Test]
159                 [ExpectedException (typeof (ArgumentException))]
160                 public void SecurityZone_Invalid ()
161                 {
162                         ZoneIdentityPermission zip = new ZoneIdentityPermission ((SecurityZone)128);
163                 }
164
165                 [Test]
166                 [ExpectedException (typeof (ArgumentException))]
167                 public void Intersect_DifferentPermissions ()
168                 {
169                         ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
170                         SecurityPermission b = new SecurityPermission (PermissionState.None);
171                         a.Intersect (b);
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (ArgumentException))]
176                 public void IsSubsetOf_DifferentPermissions ()
177                 {
178                         ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
179                         SecurityPermission b = new SecurityPermission (PermissionState.None);
180                         a.IsSubsetOf (b);
181                 }
182
183                 [Test]
184                 public void Union () 
185                 {
186                         ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
187
188                         ZoneIdentityPermission z = (ZoneIdentityPermission) a.Union (null);
189                         Assert.IsTrue (Same (a, z), "Trusted+null");
190                         Assert.IsFalse (Object.ReferenceEquals (a, z), "!ReferenceEquals1");
191
192                         z = (ZoneIdentityPermission) a.Union (new ZoneIdentityPermission (PermissionState.None));
193                         Assert.IsTrue (Same (a, z), "Trusted+PS.None");
194                         Assert.IsFalse (Object.ReferenceEquals (a, z), "!ReferenceEquals2");
195
196                         // note: can't be tested with PermissionState.Unrestricted
197
198                         ZoneIdentityPermission n = new ZoneIdentityPermission (SecurityZone.NoZone);
199                         z = (ZoneIdentityPermission) a.Union (n);
200                         Assert.IsTrue (Same (a, z), "Trusted+NoZone");
201                         Assert.IsFalse (Object.ReferenceEquals (a, z), "!ReferenceEquals3");
202
203                         z = (ZoneIdentityPermission) n.Union (a);
204                         Assert.IsTrue (Same (a, z), "NoZone+Trusted");
205                         Assert.IsFalse (Object.ReferenceEquals (a, z), "!ReferenceEquals4");
206                 }
207 #if NET_2_0
208                 [Test]
209                 [ExpectedException (typeof (ArgumentException))]
210                 public void Union_DifferentIdentities ()
211                 {
212                         ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
213                         ZoneIdentityPermission b = new ZoneIdentityPermission (SecurityZone.Untrusted);
214                         a.Union (b);
215                 }
216 #else
217                 [Test]
218                 public void Union_DifferentIdentities ()
219                 {
220                         ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
221                         ZoneIdentityPermission b = new ZoneIdentityPermission (SecurityZone.Untrusted);
222                         Assert.IsNull (a.Union (b));
223                 }
224 #endif
225                 [Test]
226                 [ExpectedException (typeof (ArgumentException))]
227                 public void Union_DifferentPermissions ()
228                 {
229                         ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
230                         SecurityPermission b = new SecurityPermission (PermissionState.None);
231                         a.Union (b);
232                 }
233
234                 [Test]
235                 [ExpectedException (typeof (ArgumentNullException))]
236                 public void FromXml_Null ()
237                 {
238                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
239                         zip.FromXml (null);
240                 }
241
242                 [Test]
243                 [ExpectedException (typeof (ArgumentException))]
244                 public void FromXml_WrongTag ()
245                 {
246                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
247                         SecurityElement se = zip.ToXml ();
248                         se.Tag = "IMono"; // instead of IPermission
249                         zip.FromXml (se);
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (ArgumentException))]
254                 public void FromXml_WrongTagCase ()
255                 {
256                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
257                         SecurityElement se = zip.ToXml ();
258                         se.Tag = "IPERMISSION"; // instead of IPermission
259                         zip.FromXml (se);
260                 }
261
262                 [Test]
263                 public void FromXml_WrongClass ()
264                 {
265                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
266                         SecurityElement se = zip.ToXml ();
267
268                         SecurityElement w = new SecurityElement (se.Tag);
269                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
270                         w.AddAttribute ("version", se.Attribute ("version"));
271                         zip.FromXml (w);
272                         // doesn't care of the class name at that stage
273                         // anyway the class has already be created so...
274                 }
275
276                 [Test]
277                 public void FromXml_NoClass ()
278                 {
279                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
280                         SecurityElement se = zip.ToXml ();
281
282                         SecurityElement w = new SecurityElement (se.Tag);
283                         w.AddAttribute ("version", se.Attribute ("version"));
284                         zip.FromXml (w);
285                         // doesn't even care of the class attribute presence
286                 }
287
288                 [Test]
289                 [ExpectedException (typeof (ArgumentException))]
290                 public void FromXml_WrongVersion ()
291                 {
292                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
293                         SecurityElement se = zip.ToXml ();
294
295                         SecurityElement w = new SecurityElement (se.Tag);
296                         w.AddAttribute ("class", se.Attribute ("class"));
297                         w.AddAttribute ("version", "2");
298                         zip.FromXml (w);
299                 }
300
301                 [Test]
302                 public void FromXml_NoVersion ()
303                 {
304                         ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
305                         SecurityElement se = zip.ToXml ();
306
307                         SecurityElement w = new SecurityElement (se.Tag);
308                         w.AddAttribute ("class", se.Attribute ("class"));
309                         zip.FromXml (w);
310                 }
311         }
312 }