2 // IsolatedStorageFilePermissionTest.cs - NUnit Test Cases for IsolatedStorageFilePermission
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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;
31 using System.Security;
32 using System.Security.Permissions;
34 namespace MonoTests.System.Security.Permissions {
37 public class IsolatedStorageFilePermissionTest {
40 public void PermissionStateNone ()
42 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
43 Assert.AreEqual (IsolatedStorageContainment.None, isfp.UsageAllowed, "UsageAllowed");
44 Assert.AreEqual (0, isfp.UserQuota, "UserQuota");
46 SecurityElement se = isfp.ToXml ();
47 // only class and version are present
48 Assert.AreEqual ("None", se.Attribute ("Allowed"), "Xml-Allowed");
49 Assert.IsNull (se.Children, "Xml-Children");
51 IsolatedStorageFilePermission copy = (IsolatedStorageFilePermission)isfp.Copy ();
52 Assert.IsFalse (Object.ReferenceEquals (isfp, copy), "ReferenceEquals");
53 Assert.AreEqual (isfp.UsageAllowed, copy.UsageAllowed, "UsageAllowed");
54 Assert.AreEqual (isfp.UserQuota, copy.UserQuota, "UserQuota");
58 public void PermissionStateUnrestricted ()
60 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.Unrestricted);
61 Assert.AreEqual (IsolatedStorageContainment.UnrestrictedIsolatedStorage, isfp.UsageAllowed, "UsageAllowed");
62 Assert.AreEqual (Int64.MaxValue, isfp.UserQuota, "UserQuota");
64 SecurityElement se = isfp.ToXml ();
65 // only class and version are present
66 Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted");
67 Assert.IsNull (se.Children, "Xml-Children");
69 IsolatedStorageFilePermission copy = (IsolatedStorageFilePermission)isfp.Copy ();
70 Assert.IsFalse (Object.ReferenceEquals (isfp, copy), "ReferenceEquals");
71 Assert.AreEqual (isfp.UsageAllowed, copy.UsageAllowed, "UsageAllowed");
72 Assert.AreEqual (isfp.UserQuota, copy.UserQuota, "UserQuota");
76 [ExpectedException (typeof (ArgumentException))]
77 public void PermissionStateInvalid ()
79 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission ((PermissionState)2);
83 public void Intersect ()
85 IsolatedStorageFilePermission empty = new IsolatedStorageFilePermission (PermissionState.None);
86 IsolatedStorageFilePermission intersect = (IsolatedStorageFilePermission)empty.Intersect (null);
87 Assert.IsNull (intersect, "empty N null");
89 intersect = (IsolatedStorageFilePermission)empty.Intersect (empty);
90 Assert.IsNull (intersect, "empty N empty");
92 IsolatedStorageFilePermission unrestricted = new IsolatedStorageFilePermission (PermissionState.Unrestricted);
93 intersect = (IsolatedStorageFilePermission)unrestricted.Intersect (null);
94 Assert.IsNull (intersect, "unrestricted N null");
96 intersect = (IsolatedStorageFilePermission)unrestricted.Intersect (empty);
97 Assert.IsNotNull (intersect, "unrestricted N empty");
99 intersect = (IsolatedStorageFilePermission)unrestricted.Intersect (unrestricted);
100 Assert.IsNotNull (intersect, "unrestricted N unrestricted");
104 [ExpectedException (typeof (ArgumentException))]
105 public void Intersect_DifferentPermissions ()
107 IsolatedStorageFilePermission a = new IsolatedStorageFilePermission (PermissionState.None);
108 SecurityPermission b = new SecurityPermission (PermissionState.None);
113 public void IsSubsetOf ()
115 IsolatedStorageFilePermission empty = new IsolatedStorageFilePermission (PermissionState.None);
116 Assert.IsTrue (empty.IsSubsetOf (null), "empty.IsSubsetOf (null)");
118 IsolatedStorageFilePermission unrestricted = new IsolatedStorageFilePermission (PermissionState.Unrestricted);
119 Assert.IsFalse (unrestricted.IsSubsetOf (null), "unrestricted.IsSubsetOf (null)");
120 Assert.IsFalse (unrestricted.IsSubsetOf (empty), "unrestricted.IsSubsetOf (empty)");
121 Assert.IsTrue (empty.IsSubsetOf (unrestricted), "empty.IsSubsetOf (unrestricted)");
125 [ExpectedException (typeof (ArgumentException))]
126 public void IsSubsetOf_DifferentPermissions ()
128 IsolatedStorageFilePermission a = new IsolatedStorageFilePermission (PermissionState.None);
129 SecurityPermission b = new SecurityPermission (PermissionState.None);
136 IsolatedStorageFilePermission empty = new IsolatedStorageFilePermission (PermissionState.None);
137 IsolatedStorageFilePermission union = (IsolatedStorageFilePermission)empty.Union (null);
138 Assert.IsNotNull (union, "empty U null");
139 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-1");
140 Assert.IsFalse (Object.ReferenceEquals (empty, union), "ReferenceEquals-1");
142 union = (IsolatedStorageFilePermission)empty.Union (empty);
143 Assert.IsNotNull (union, "empty U empty");
144 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-2");
145 Assert.IsFalse (Object.ReferenceEquals (empty, union), "ReferenceEquals-2");
147 IsolatedStorageFilePermission unrestricted = new IsolatedStorageFilePermission (PermissionState.Unrestricted);
148 union = (IsolatedStorageFilePermission)unrestricted.Union (null);
149 Assert.IsNotNull (union, "unrestricted U null");
150 Assert.IsTrue (union.IsUnrestricted (), "IsUnrestricted-3");
151 Assert.IsFalse (Object.ReferenceEquals (unrestricted, union), "ReferenceEquals-3");
153 union = (IsolatedStorageFilePermission)unrestricted.Union (empty);
154 Assert.IsNotNull (union, "unrestricted U empty");
155 Assert.IsTrue (union.IsUnrestricted (), "IsUnrestricted-4");
156 Assert.IsFalse (Object.ReferenceEquals (unrestricted, union), "ReferenceEquals-4");
158 union = (IsolatedStorageFilePermission)unrestricted.Union (unrestricted);
159 Assert.IsNotNull (union, "unrestricted U unrestricted");
160 Assert.IsTrue (union.IsUnrestricted (), "IsUnrestricted-5");
161 Assert.IsFalse (Object.ReferenceEquals (unrestricted, union), "ReferenceEquals-5");
165 [ExpectedException (typeof (ArgumentException))]
166 public void Union_DifferentPermissions ()
168 IsolatedStorageFilePermission a = new IsolatedStorageFilePermission (PermissionState.None);
169 SecurityPermission b = new SecurityPermission (PermissionState.None);
174 public void UsageAllowedQuota ()
176 IsolatedStorageFilePermission empty = new IsolatedStorageFilePermission (PermissionState.None);
177 IsolatedStorageFilePermission small = new IsolatedStorageFilePermission (PermissionState.None);
178 small.UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser;
180 IsolatedStorageFilePermission union = (IsolatedStorageFilePermission)empty.Union (small);
181 Assert.AreEqual (IsolatedStorageContainment.DomainIsolationByUser, union.UsageAllowed, "DomainIsolationByUser");
182 Assert.AreEqual (1, union.UserQuota, "1");
183 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-1");
184 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-1a");
185 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-1b");
186 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-1c");
187 IsolatedStorageFilePermission intersect = (IsolatedStorageFilePermission)union.Intersect (small);
188 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-1");
189 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-1");
191 small.UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser;
193 union = (IsolatedStorageFilePermission)union.Union (small);
194 Assert.AreEqual (IsolatedStorageContainment.AssemblyIsolationByUser, union.UsageAllowed, "AssemblyIsolationByUser");
195 Assert.AreEqual (2, union.UserQuota, "2");
196 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-2");
197 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-2a");
198 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-2b");
199 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-2c");
200 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
201 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-2");
202 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-2");
204 small.UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser;
206 union = (IsolatedStorageFilePermission)union.Union (small);
207 Assert.AreEqual (IsolatedStorageContainment.AssemblyIsolationByUser, union.UsageAllowed, "ApplicationIsolationByUser");
208 Assert.AreEqual (3, union.UserQuota, "3");
209 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-3");
210 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-3a");
211 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-3b");
212 Assert.IsFalse (union.IsSubsetOf (small), "IsSubset-3c");
213 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
214 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-3");
215 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-3");
217 small.UsageAllowed = IsolatedStorageContainment.DomainIsolationByMachine;
219 union = (IsolatedStorageFilePermission)union.Union (small);
220 Assert.AreEqual (IsolatedStorageContainment.DomainIsolationByMachine, union.UsageAllowed, "DomainIsolationByMachine");
221 Assert.AreEqual (4, union.UserQuota, "4");
222 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-4");
223 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-4a");
224 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-4b");
225 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-4c");
226 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
227 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-4");
228 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-4");
230 small.UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByMachine;
232 union = (IsolatedStorageFilePermission)union.Union (small);
233 Assert.AreEqual (IsolatedStorageContainment.AssemblyIsolationByMachine, union.UsageAllowed, "AssemblyIsolationByMachine");
234 Assert.AreEqual (5, union.UserQuota, "5");
235 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-5");
236 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-5a");
237 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-5b");
238 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-5c");
239 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
240 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-5");
241 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-5");
243 small.UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByMachine;
245 union = (IsolatedStorageFilePermission)union.Union (small);
246 Assert.AreEqual (IsolatedStorageContainment.ApplicationIsolationByMachine, union.UsageAllowed, "ApplicationIsolationByMachine");
247 Assert.AreEqual (6, union.UserQuota, "6");
248 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-6");
249 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-6a");
250 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-6b");
251 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-6c");
252 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
253 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-6");
254 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-6");
256 small.UsageAllowed = IsolatedStorageContainment.DomainIsolationByRoamingUser;
258 union = (IsolatedStorageFilePermission)union.Union (small);
259 Assert.AreEqual (IsolatedStorageContainment.DomainIsolationByRoamingUser, union.UsageAllowed, "DomainIsolationByRoamingUser");
260 Assert.AreEqual (7, union.UserQuota, "7a");
261 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-7a");
262 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-7a");
263 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-7b");
264 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-7c");
265 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
266 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-7");
267 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-7");
270 small.UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser;
272 union = (IsolatedStorageFilePermission)union.Union (small);
273 Assert.AreEqual (IsolatedStorageContainment.DomainIsolationByRoamingUser, union.UsageAllowed, "DomainIsolationByRoamingUser");
274 Assert.AreEqual (7, union.UserQuota, "7b");
275 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-7b");
277 small.UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByRoamingUser;
278 small.UserQuota = 7; // no change
279 union = (IsolatedStorageFilePermission)union.Union (small);
280 Assert.AreEqual (IsolatedStorageContainment.AssemblyIsolationByRoamingUser, union.UsageAllowed, "AssemblyIsolationByRoamingUser");
281 Assert.AreEqual (7, union.UserQuota, "7c");
282 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-7c");
284 small.UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByRoamingUser;
286 union = (IsolatedStorageFilePermission)union.Union (small);
287 Assert.AreEqual (IsolatedStorageContainment.ApplicationIsolationByRoamingUser, union.UsageAllowed, "ApplicationIsolationByRoamingUser");
288 Assert.AreEqual (8, union.UserQuota, "8");
289 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-8");
290 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-8a");
291 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-8b");
292 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-8c");
293 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
294 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-8");
295 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-8");
297 small.UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser;
299 union = (IsolatedStorageFilePermission)union.Union (small);
300 Assert.AreEqual (IsolatedStorageContainment.AdministerIsolatedStorageByUser, union.UsageAllowed, "AdministerIsolatedStorageByUser");
301 Assert.AreEqual (9, union.UserQuota, "9");
302 Assert.IsFalse (union.IsUnrestricted (), "IsUnrestricted-9");
303 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-9a");
304 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-9b");
305 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-9c");
306 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
307 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-9");
308 Assert.AreEqual (small.UserQuota, intersect.UserQuota, "Intersect-UserQuota-9");
310 small.UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
311 small.UserQuota = 10;
312 union = (IsolatedStorageFilePermission)union.Union (small);
313 Assert.AreEqual (IsolatedStorageContainment.UnrestrictedIsolatedStorage, union.UsageAllowed, "UnrestrictedIsolatedStorage");
314 Assert.AreEqual (Int64.MaxValue, union.UserQuota, "10");
315 Assert.IsTrue (union.IsUnrestricted (), "IsUnrestricted-10");
316 Assert.IsTrue (small.IsSubsetOf (union), "IsSubset-10a");
317 Assert.IsTrue (empty.IsSubsetOf (union), "IsSubset-10b");
318 Assert.IsTrue (union.IsSubsetOf (small), "IsSubset-10c");
319 intersect = (IsolatedStorageFilePermission)union.Intersect (small);
320 Assert.AreEqual (small.UsageAllowed, intersect.UsageAllowed, "Intersect-UsageAllowed-10");
321 Assert.IsFalse ((small.UserQuota == intersect.UserQuota), "Intersect-UserQuota-10");
325 [ExpectedException (typeof (ArgumentNullException))]
326 public void FromXml_Null ()
328 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
333 [ExpectedException (typeof (ArgumentException))]
334 public void FromXml_WrongTag ()
336 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
337 SecurityElement se = isfp.ToXml ();
343 [ExpectedException (typeof (ArgumentException))]
344 public void FromXml_WrongTagCase ()
346 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
347 SecurityElement se = isfp.ToXml ();
348 se.Tag = "IPERMISSION"; // instead of IPermission
353 public void FromXml_WrongClass ()
355 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
356 SecurityElement se = isfp.ToXml ();
358 SecurityElement w = new SecurityElement (se.Tag);
359 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
360 w.AddAttribute ("version", se.Attribute ("version"));
362 // doesn't care of the class name at that stage
363 // anyway the class has already be created so...
367 public void FromXml_NoClass ()
369 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
370 SecurityElement se = isfp.ToXml ();
372 SecurityElement w = new SecurityElement (se.Tag);
373 w.AddAttribute ("version", se.Attribute ("version"));
375 // doesn't even care of the class attribute presence
379 [ExpectedException (typeof (ArgumentException))]
380 public void FromXml_WrongVersion ()
382 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
383 SecurityElement se = isfp.ToXml ();
384 se.Attributes.Remove ("version");
385 se.Attributes.Add ("version", "2");
390 public void FromXml_NoVersion ()
392 IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
393 SecurityElement se = isfp.ToXml ();
395 SecurityElement w = new SecurityElement (se.Tag);
396 w.AddAttribute ("class", se.Attribute ("class"));