2 // IsolatedStorageFileTest.cs
3 // - Unit Tests for abstract IsolatedStorageFile class
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.
31 using System.Collections;
32 using System.IO.IsolatedStorage;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Security.Policy;
38 using NUnit.Framework;
40 namespace MonoTests.System.IO.IsolatedStorageTest {
43 public class IsolatedStorageFileTest {
45 private const string dirname = "mono-unit-test";
47 private void CheckEnumerated (int n, IsolatedStorageScope scope, IsolatedStorageFile isf)
49 string prefix = n.ToString () + " - " + scope.ToString () + " - ";
50 Assert.IsNotNull (isf, prefix + "IsolatedStorageFile");
51 Assert.IsTrue (((scope & isf.Scope) != 0), prefix + "Scope");
53 if ((isf.Scope & IsolatedStorageScope.Assembly) != 0)
54 Assert.IsNotNull (isf.AssemblyIdentity, prefix + "AssemblyIdentity");
55 if ((isf.Scope & IsolatedStorageScope.Domain) != 0)
56 Assert.IsNotNull (isf.DomainIdentity, prefix + "DomainIdentity");
58 if ((isf.Scope & IsolatedStorageScope.Application) != 0)
59 Assert.IsNotNull (isf.ApplicationIdentity, prefix + "ApplicationIdentity");
63 private void GetEnumerator (IsolatedStorageScope scope)
65 IEnumerator e = IsolatedStorageFile.GetEnumerator (scope);
69 IsolatedStorageFile isf = (IsolatedStorageFile)e.Current;
70 CheckEnumerated (++n, scope, isf);
75 public void GetEnumerator_User ()
77 GetEnumerator (IsolatedStorageScope.User);
81 [ExpectedException (typeof (ArgumentException))]
82 public void GetEnumerator_User_Details ()
84 // giving more details is bad
85 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain);
89 public void GetEnumerator_UserRoaming ()
91 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Roaming);
95 [ExpectedException (typeof (ArgumentException))]
96 public void GetEnumerator_UserRoaming_Details ()
98 // giving more details is bad
99 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain | IsolatedStorageScope.Roaming);
103 public void GetEnumerator_Machine ()
105 GetEnumerator (IsolatedStorageScope.Machine);
109 [ExpectedException (typeof (ArgumentException))]
110 public void GetEnumerator_Machine_Details ()
112 GetEnumerator (IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly);
116 [ExpectedException (typeof (ArgumentException))]
117 public void GetEnumerator_Application ()
119 // we can't enum application
120 GetEnumerator (IsolatedStorageScope.Application);
124 public void GetUserStoreForAssembly ()
126 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
127 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
128 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
129 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
130 // note: mono transforms the CodeBase into uppercase
131 // for net 1.1 which uses file:// and not file:///
132 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
133 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
134 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
135 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
139 [ExpectedException (typeof (InvalidOperationException))]
140 public void GetUserStoreForAssembly_DomainIdentity ()
142 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
143 object o = isf.DomainIdentity;
148 [ExpectedException (typeof (InvalidOperationException))]
149 public void GetUserStoreForAssembly_ApplicationIdentity ()
151 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
152 object o = isf.ApplicationIdentity;
157 public void GetUserStoreForDomain ()
159 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
160 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
161 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
162 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
163 // note: mono transforms the CodeBase into uppercase
164 // for net 1.1 which uses file:// and not file:///
165 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
166 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
167 Assert.IsTrue ((isf.DomainIdentity is Url), "DomainIdentity");
168 // note: with MS Assembly.GetEntryAssembly () only works in the default (first) AppDomain
169 // so we're using the first parameter to GetCommandLineArgs
170 string exe = Environment.GetCommandLineArgs ()[0].Replace ("\\", "/").ToUpper ();
171 Assert.IsTrue ((isf.DomainIdentity.ToString ().ToUpper ().IndexOf (exe) > 0), exe + "\n" + isf.DomainIdentity.ToString ().ToUpper ()); //"Url - Domain");
172 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
177 [ExpectedException (typeof (InvalidOperationException))]
178 public void GetUserStoreForDomain_ApplicationIdentity ()
180 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
181 object o = isf.ApplicationIdentity;
185 [ExpectedException (typeof (IsolatedStorageException))]
186 public void GetUserStoreForApplication_WithoutApplicationIdentity ()
188 // note: a manifest is required
189 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
193 [ExpectedException (typeof (IsolatedStorageException))]
194 public void GetUserStoreForApplication ()
196 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
197 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
198 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
199 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
200 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf (Assembly.GetExecutingAssembly ().CodeBase) > 0), "Url");
201 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
205 [ExpectedException (typeof (IsolatedStorageException))]
206 public void GetUserStoreForApplication_AssemblyIdentity ()
208 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
209 object o = isf.AssemblyIdentity;
213 [ExpectedException (typeof (IsolatedStorageException))]
214 public void GetUserStoreForApplication_DomainIdentity ()
216 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
217 object o = isf.DomainIdentity;
222 public void GetStore_Domain_Zone ()
224 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
225 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
226 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
227 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
228 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
229 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
230 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
231 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
232 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
236 [ExpectedException (typeof (IsolatedStorageException))]
237 public void GetStore_Domain_NonPresentEvidences ()
239 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
240 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Publisher));
244 public void GetStore_Assembly_NonPresentDomainEvidences ()
246 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
247 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
248 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
249 Assert.AreEqual (scope, isf.Scope, "Scope");
250 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
251 // note: mono transforms the CodeBase into uppercase
252 // for net 1.1 which uses file:// and not file:///
253 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
254 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
255 // DomainIdentity throws a InvalidOperationException
256 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
260 [ExpectedException (typeof (ArgumentNullException))]
261 public void GetStore_Domain_DomainNullObject ()
263 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
264 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.MyComputer));
268 [ExpectedException (typeof (ArgumentNullException))]
269 public void GetStore_Domain_AssemblyNullObject ()
271 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
272 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
276 public void GetStore_Assembly_DomainNullObject ()
278 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
279 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.Internet));
280 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
281 Assert.AreEqual (scope, isf.Scope, "Scope");
282 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
283 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
284 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
288 [ExpectedException (typeof (ArgumentNullException))]
289 public void GetStore_Assembly_AssemblyNullObject ()
291 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
292 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
296 public void GetStore_Domain_ZoneObjectZoneObject ()
298 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
299 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.Internet), new Zone (SecurityZone.Internet));
300 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
301 Assert.AreEqual (scope, isf.Scope, "Scope");
302 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
303 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
304 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
305 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Domain");
306 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
310 [ExpectedException (typeof (ArgumentNullException))]
311 public void GetStore_Application_NullObject ()
313 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
314 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null);
318 [ExpectedException (typeof (IsolatedStorageException))]
319 public void GetStore_Application_NullType ()
321 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
322 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (Type)null);
323 // again it's the lack of a manifest
328 public void GetStore_DomainScope_Evidences ()
330 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
332 Evidence de = new Evidence ();
333 de.AddHost (new Zone (SecurityZone.Internet));
334 Evidence ae = new Evidence ();
335 ae.AddHost (new Zone (SecurityZone.Intranet));
336 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), ae, typeof (Zone));
338 // Maximum size for Internet isn't (by default) Int64.MaxValue
339 Assert.AreEqual (scope, isf.Scope, "Scope");
340 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
341 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Intranet") > 0), "Zone - Assembly");
342 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
343 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), isf.DomainIdentity.ToString ()); //"Zone - Domain");
344 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
348 [ExpectedException (typeof (ArgumentNullException))]
349 public void GetStore_DomainScope_Evidence_NullAssemblyEvidence ()
351 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
353 Evidence de = new Evidence ();
354 de.AddHost (new Zone (SecurityZone.Internet));
355 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
359 [ExpectedException (typeof (ArgumentNullException))]
360 public void GetStore_DomainScope_Evidence_NullDomainEvidence ()
362 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
364 Evidence ae = new Evidence ();
365 ae.AddHost (new Zone (SecurityZone.Internet));
366 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
370 [ExpectedException (typeof (ArgumentNullException))]
371 public void GetStore_AssemblyScope_Evidence_NullAssemblyEvidence ()
373 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
375 Evidence de = new Evidence ();
376 de.AddHost (new Zone (SecurityZone.Internet));
377 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
381 public void GetStore_AssemblyScope_Evidence_NullDomainEvidence ()
383 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
385 Evidence ae = new Evidence ();
386 ae.AddHost (new Zone (SecurityZone.Internet));
387 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));