2 // IsolatedStorageFileCas.cs - CAS unit tests for
3 // System.IO.IsolatedStorage.IsolatedStorageFile
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
34 using System.IO.IsolatedStorage;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Policy;
39 namespace MonoCasTests.System.IO.IsolatedStorageTest {
43 public class IsolatedStorageFileCas {
48 if (!SecurityManager.SecurityEnabled)
49 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
52 // use the caller stack to execute some read operations
53 private void Read (IsolatedStorageFile isf)
55 Assert.IsNotNull (isf.GetDirectoryNames ("*"), "GetDirectoryNames");
56 Assert.IsNotNull (isf.GetFileNames ("*"), "GetFileNames");
58 Assert.IsTrue (isf.CurrentSize >= 0, "CurrentSize");
59 Assert.IsTrue (isf.MaximumSize >= isf.CurrentSize, "MaximumSize");
61 catch (InvalidOperationException) {
66 // use the caller stack to execute some write operations
67 private void Write (IsolatedStorageFile isf)
69 isf.CreateDirectory ("testdir");
71 string filename = Path.Combine ("testdir", "file");
72 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (filename, FileMode.Create, isf)) {
74 isf.DeleteFile (filename);
76 isf.DeleteDirectory ("testdir");
80 catch (IsolatedStorageException) {
81 // fx 1.x doesn't like removing when things "could" still be in use
87 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByMachine)]
88 [ExpectedException (typeof (SecurityException))]
89 [Ignore ("no manifest")]
90 public void GetMachineStoreForApplication_Fail ()
92 IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForApplication ();
96 [Ignore ("no manifest")]
97 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByMachine)]
98 public void GetMachineStoreForApplication_Pass ()
100 IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForApplication ();
104 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByMachine)]
105 [ExpectedException (typeof (SecurityException))]
106 public void GetMachineStoreForAssembly_Fail ()
108 IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForAssembly ();
112 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByMachine)]
113 public void GetMachineStoreForAssembly_Pass ()
115 IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForAssembly ();
123 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser)]
124 public void GetMachineStoreForAssembly_Administer ()
126 IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForAssembly ();
134 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.DomainIsolationByMachine)]
135 [ExpectedException (typeof (SecurityException))]
136 public void GetMachineStoreForDomain_Fail ()
138 IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForDomain ();
142 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.DomainIsolationByMachine)]
143 public void GetMachineStoreForDomain_Pass ()
145 IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForDomain ();
153 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser)]
154 public void GetMachineStoreForDomain_Administer ()
156 IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForDomain ();
164 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser)]
165 [ExpectedException (typeof (SecurityException))]
166 [Ignore ("no manifest")]
167 public void GetUserStoreForApplication_Fail ()
169 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
173 [Ignore ("no manifest")]
174 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser)]
175 public void GetUserStoreForApplication_Pass ()
177 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
181 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
182 [ExpectedException (typeof (SecurityException))]
183 public void GetUserStoreForAssembly_Fail ()
185 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
189 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
190 public void GetUserStoreForAssembly_Pass ()
192 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
200 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByRoamingUser)]
201 [ExpectedException (typeof (SecurityException))]
202 public void GetUserStoreForAssembly_Roaming_Fail ()
204 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
208 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser)]
209 public void GetUserStoreForAssembly_Roaming_Pass ()
211 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
219 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByRoamingUser)]
220 public void GetUserStoreForAssembly_Administer ()
222 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
230 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser)]
231 [ExpectedException (typeof (SecurityException))]
232 public void GetUserStoreForDomain_Fail ()
234 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
238 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser)]
239 public void GetUserStoreForDomain_Pass ()
241 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
249 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.DomainIsolationByRoamingUser)]
250 [ExpectedException (typeof (SecurityException))]
251 public void GetUserStoreForDomain_Roaming_Fail ()
253 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
257 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.DomainIsolationByRoamingUser)]
258 public void GetUserStoreForDomain_Roaming_Pass ()
260 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
268 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser)]
269 public void GetUserStoreForDomain_Administer ()
271 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
279 private ulong MaximumSize (SecurityZone zone)
281 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
283 Evidence ae = new Evidence ();
284 ae.AddHost (new Zone (zone));
285 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
286 return isf.MaximumSize;
290 public void MaximumSize ()
292 Assert.AreEqual (Int64.MaxValue, MaximumSize (SecurityZone.MyComputer), "MyComputer");
293 Assert.AreEqual (Int64.MaxValue, MaximumSize (SecurityZone.Intranet), "Intranet");
295 Assert.AreEqual (512000, MaximumSize (SecurityZone.Internet), "Internet");
296 Assert.AreEqual (512000, MaximumSize (SecurityZone.Trusted), "Trusted");
298 Assert.AreEqual (10240, MaximumSize (SecurityZone.Internet), "Internet");
299 Assert.AreEqual (10240, MaximumSize (SecurityZone.Trusted), "Trusted");
304 [ExpectedException (typeof (PolicyException))]
305 public void MaximumSize_Untrusted ()
307 Assert.AreEqual (Int64.MaxValue, MaximumSize (SecurityZone.Untrusted), "Untrusted");
311 [ExpectedException (typeof (PolicyException))]
312 public void MaximumSize_NoZone ()
314 Assert.AreEqual (Int64.MaxValue, MaximumSize (SecurityZone.NoZone), "NoZone");