Update lastest two task unit tests
[mono.git] / mcs / class / corlib / Test / System.IO.IsolatedStorage / IsolatedStorageFileCas.cs
1 //
2 // IsolatedStorageFileCas.cs - CAS unit tests for 
3 //      System.IO.IsolatedStorage.IsolatedStorageFile
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 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
32 using System;
33 using System.IO;
34 using System.IO.IsolatedStorage;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38
39 namespace MonoCasTests.System.IO.IsolatedStorageTest {
40
41         [TestFixture]
42         [Category ("CAS")]
43         public class IsolatedStorageFileCas {
44
45                 [SetUp]
46                 public void SetUp ()
47                 {
48                         if (!SecurityManager.SecurityEnabled)
49                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
50                 }
51
52                 // use the caller stack to execute some read operations
53                 private void Read (IsolatedStorageFile isf)
54                 {
55                         Assert.IsNotNull (isf.GetDirectoryNames ("*"), "GetDirectoryNames");
56                         Assert.IsNotNull (isf.GetFileNames ("*"), "GetFileNames");
57                         try {
58                                 Assert.IsTrue (isf.CurrentSize >= 0, "CurrentSize");
59                                 Assert.IsTrue (isf.MaximumSize >= isf.CurrentSize, "MaximumSize");
60                         }
61                         catch (InvalidOperationException) {
62                                 // roaming
63                         }
64                 }
65
66                 // use the caller stack to execute some write operations
67                 private void Write (IsolatedStorageFile isf)
68                 {
69                         isf.CreateDirectory ("testdir");
70
71                         string filename = Path.Combine ("testdir", "file");
72                         using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (filename, FileMode.Create, isf)) {
73                         }
74                         isf.DeleteFile (filename);
75
76                         isf.DeleteDirectory ("testdir");
77                         try {
78                                 isf.Remove ();
79                         }
80                         catch (IsolatedStorageException) {
81                                 // fx 1.x doesn't like removing when things "could" still be in use
82                         }
83                 }
84
85 #if NET_2_0
86                 [Test]
87                 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByMachine)]
88                 [ExpectedException (typeof (SecurityException))]
89                 [Ignore ("no manifest")]
90                 public void GetMachineStoreForApplication_Fail ()
91                 {
92                         IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForApplication ();
93                 }
94
95                 [Test]
96                 [Ignore ("no manifest")]
97                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByMachine)]
98                 public void GetMachineStoreForApplication_Pass ()
99                 {
100                         IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForApplication ();
101                 }
102
103                 [Test]
104                 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByMachine)]
105                 [ExpectedException (typeof (SecurityException))]
106                 public void GetMachineStoreForAssembly_Fail ()
107                 {
108                         IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForAssembly ();
109                 }
110
111                 [Test]
112                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByMachine)]
113                 public void GetMachineStoreForAssembly_Pass ()
114                 {
115                         IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForAssembly ();
116                         Read (isf);
117                         Write (isf);
118                         isf.Dispose ();
119                         isf.Close ();
120                 }
121
122                 [Test]
123                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser)]
124                 public void GetMachineStoreForAssembly_Administer ()
125                 {
126                         IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForAssembly ();
127                         Read (isf);
128                         Write (isf);
129                         isf.Dispose ();
130                         isf.Close ();
131                 }
132
133                 [Test]
134                 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.DomainIsolationByMachine)]
135                 [ExpectedException (typeof (SecurityException))]
136                 public void GetMachineStoreForDomain_Fail ()
137                 {
138                         IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForDomain ();
139                 }
140
141                 [Test]
142                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.DomainIsolationByMachine)]
143                 public void GetMachineStoreForDomain_Pass ()
144                 {
145                         IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForDomain ();
146                         Read (isf);
147                         Write (isf);
148                         isf.Dispose ();
149                         isf.Close ();
150                 }
151
152                 [Test]
153                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser)]
154                 public void GetMachineStoreForDomain_Administer ()
155                 {
156                         IsolatedStorageFile isf = IsolatedStorageFile.GetMachineStoreForDomain ();
157                         Read (isf);
158                         Write (isf);
159                         isf.Dispose ();
160                         isf.Close ();
161                 }
162
163                 [Test]
164                 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser)]
165                 [ExpectedException (typeof (SecurityException))]
166                 [Ignore ("no manifest")]
167                 public void GetUserStoreForApplication_Fail ()
168                 {
169                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
170                 }
171
172                 [Test]
173                 [Ignore ("no manifest")]
174                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser)]
175                 public void GetUserStoreForApplication_Pass ()
176                 {
177                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
178                 }
179 #endif
180                 [Test]
181                 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
182                 [ExpectedException (typeof (SecurityException))]
183                 public void GetUserStoreForAssembly_Fail ()
184                 {
185                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
186                 }
187
188                 [Test]
189                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
190                 public void GetUserStoreForAssembly_Pass ()
191                 {
192                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
193                         Read (isf);
194                         Write (isf);
195                         isf.Dispose ();
196                         isf.Close ();
197                 }
198
199                 [Test]
200                 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByRoamingUser)]
201                 [ExpectedException (typeof (SecurityException))]
202                 public void GetUserStoreForAssembly_Roaming_Fail ()
203                 {
204                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
205                 }
206
207                 [Test]
208                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser)]
209                 public void GetUserStoreForAssembly_Roaming_Pass ()
210                 {
211                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
212                         Read (isf);
213                         Write (isf);
214                         isf.Dispose ();
215                         isf.Close ();
216                 }
217
218                 [Test]
219                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByRoamingUser)]
220                 public void GetUserStoreForAssembly_Administer ()
221                 {
222                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
223                         Read (isf);
224                         Write (isf);
225                         isf.Dispose ();
226                         isf.Close ();
227                 }
228
229                 [Test]
230                 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser)]
231                 [ExpectedException (typeof (SecurityException))]
232                 public void GetUserStoreForDomain_Fail ()
233                 {
234                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
235                 }
236
237                 [Test]
238                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser)]
239                 public void GetUserStoreForDomain_Pass ()
240                 {
241                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
242                         Read (isf);
243                         Write (isf);
244                         isf.Dispose ();
245                         isf.Close ();
246                 }
247
248                 [Test]
249                 [IsolatedStorageFilePermission (SecurityAction.Deny, UsageAllowed = IsolatedStorageContainment.DomainIsolationByRoamingUser)]
250                 [ExpectedException (typeof (SecurityException))]
251                 public void GetUserStoreForDomain_Roaming_Fail ()
252                 {
253                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
254                 }
255
256                 [Test]
257                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.DomainIsolationByRoamingUser)]
258                 public void GetUserStoreForDomain_Roaming_Pass ()
259                 {
260                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
261                         Read (isf);
262                         Write (isf);
263                         isf.Dispose ();
264                         isf.Close ();
265                 }
266
267                 [Test]
268                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser)]
269                 public void GetUserStoreForDomain_Administer ()
270                 {
271                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
272                         Read (isf);
273                         Write (isf);
274                         isf.Dispose ();
275                         isf.Close ();
276                 }
277
278
279                 private ulong MaximumSize (SecurityZone zone)
280                 {
281                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
282
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;
287                 }
288
289                 [Test]
290                 public void MaximumSize ()
291                 {
292                         Assert.AreEqual (Int64.MaxValue, MaximumSize (SecurityZone.MyComputer), "MyComputer");
293                         Assert.AreEqual (Int64.MaxValue, MaximumSize (SecurityZone.Intranet), "Intranet");
294 #if NET_2_0
295                         Assert.AreEqual (512000, MaximumSize (SecurityZone.Internet), "Internet");
296                         Assert.AreEqual (512000, MaximumSize (SecurityZone.Trusted), "Trusted");
297 #else
298                         Assert.AreEqual (10240, MaximumSize (SecurityZone.Internet), "Internet");
299                         Assert.AreEqual (10240, MaximumSize (SecurityZone.Trusted), "Trusted");
300 #endif
301                 }
302
303                 [Test]
304                 [ExpectedException (typeof (PolicyException))]
305                 public void MaximumSize_Untrusted ()
306                 {
307                         Assert.AreEqual (Int64.MaxValue, MaximumSize (SecurityZone.Untrusted), "Untrusted");
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (PolicyException))]
312                 public void MaximumSize_NoZone ()
313                 {
314                         Assert.AreEqual (Int64.MaxValue, MaximumSize (SecurityZone.NoZone), "NoZone");
315                 }
316         }
317 }