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)
9 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
34 using System.IO.IsolatedStorage;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Security.Policy;
40 using NUnit.Framework;
42 namespace MonoTests.System.IO.IsolatedStorageTest {
45 public class IsolatedStorageFileTest {
47 private const string dirname = "mono-unit-test";
49 private void CheckEnumerated (int n, IsolatedStorageScope scope, IsolatedStorageFile isf)
51 string prefix = n.ToString () + " - " + scope.ToString () + " - ";
52 Assert.IsNotNull (isf, prefix + "IsolatedStorageFile");
53 Assert.IsTrue (((scope & isf.Scope) != 0), prefix + "Scope");
55 if ((isf.Scope & IsolatedStorageScope.Assembly) != 0)
56 Assert.IsNotNull (isf.AssemblyIdentity, prefix + "AssemblyIdentity");
57 if ((isf.Scope & IsolatedStorageScope.Domain) != 0)
58 Assert.IsNotNull (isf.DomainIdentity, prefix + "DomainIdentity");
59 if ((isf.Scope & IsolatedStorageScope.Application) != 0)
60 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);
102 public void GetEnumerator_Machine ()
104 GetEnumerator (IsolatedStorageScope.Machine);
108 [ExpectedException (typeof (ArgumentException))]
109 public void GetEnumerator_Machine_Details ()
111 GetEnumerator (IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly);
115 [ExpectedException (typeof (ArgumentException))]
116 public void GetEnumerator_Application ()
118 // we can't enum application
119 GetEnumerator (IsolatedStorageScope.Application);
122 public void GetUserStoreForAssembly ()
124 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
125 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
126 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
128 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
129 // note: mono transforms the CodeBase into uppercase
130 // for net 1.1 which uses file:// and not file:///
131 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
132 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
133 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;
147 [ExpectedException (typeof (InvalidOperationException))]
148 public void GetUserStoreForAssembly_ApplicationIdentity ()
150 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
151 object o = isf.ApplicationIdentity;
155 public void GetUserStoreForDomain ()
157 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
158 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
159 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
161 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
162 // note: mono transforms the CodeBase into uppercase
163 // for net 1.1 which uses file:// and not file:///
164 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
165 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
166 Assert.IsTrue ((isf.DomainIdentity is Url), "DomainIdentity");
167 // note: with MS Assembly.GetEntryAssembly () only works in the default (first) AppDomain
168 // so we're using the first parameter to GetCommandLineArgs
169 string exe = Environment.GetCommandLineArgs ()[0].Replace ("\\", "/").ToUpper ();
170 Assert.IsTrue ((isf.DomainIdentity.ToString ().ToUpper ().IndexOf (exe) > 0), exe + "\n" + isf.DomainIdentity.ToString ().ToUpper ()); //"Url - Domain");
172 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
176 [ExpectedException (typeof (InvalidOperationException))]
177 public void GetUserStoreForDomain_ApplicationIdentity ()
179 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
180 object o = isf.ApplicationIdentity;
185 [ExpectedException (typeof (IsolatedStorageException))]
187 public void GetUserStoreForApplication_WithoutApplicationIdentity ()
189 // note: a manifest is required
190 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
195 [ExpectedException (typeof (IsolatedStorageException))]
197 public void GetUserStoreForApplication ()
199 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
200 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
202 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
203 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
204 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf (Assembly.GetExecutingAssembly ().CodeBase) > 0), "Url");
206 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
211 [ExpectedException (typeof (IsolatedStorageException))]
212 public void GetUserStoreForApplication_AssemblyIdentity ()
214 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
215 object o = isf.AssemblyIdentity;
219 [ExpectedException (typeof (IsolatedStorageException))]
220 public void GetUserStoreForApplication_DomainIdentity ()
222 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
223 object o = isf.DomainIdentity;
228 // This is supposed to be working only in SL.
230 [ExpectedException (typeof (NotSupportedException))]
231 public void GetUserStoreForSite ()
233 IsolatedStorageFile.GetUserStoreForSite ();
238 public void GetStore_Domain_Zone ()
240 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
241 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
242 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
244 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
245 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
246 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
247 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
248 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
250 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
255 [ExpectedException (typeof (IsolatedStorageException))]
257 public void GetStore_Domain_NonPresentEvidences ()
259 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
260 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Publisher));
264 public void GetStore_Assembly_NonPresentDomainEvidences ()
266 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
267 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
268 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
269 Assert.AreEqual (scope, isf.Scope, "Scope");
271 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
272 // note: mono transforms the CodeBase into uppercase
273 // for net 1.1 which uses file:// and not file:///
274 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
275 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
276 // DomainIdentity throws a InvalidOperationException
278 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
283 [ExpectedException (typeof (ArgumentNullException))]
284 public void GetStore_Domain_DomainNullObject ()
286 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
287 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.MyComputer));
291 [ExpectedException (typeof (ArgumentNullException))]
292 public void GetStore_Domain_AssemblyNullObject ()
294 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
295 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
299 public void GetStore_Assembly_DomainNullObject ()
301 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
302 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.Internet));
303 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
304 Assert.AreEqual (scope, isf.Scope, "Scope");
305 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
306 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
307 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
311 [ExpectedException (typeof (ArgumentNullException))]
312 public void GetStore_Assembly_AssemblyNullObject ()
314 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
315 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
319 public void GetStore_Domain_ZoneObjectZoneObject ()
321 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
322 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.Internet), new Zone (SecurityZone.Internet));
323 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
324 Assert.AreEqual (scope, isf.Scope, "Scope");
325 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
326 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
327 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
328 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Domain");
329 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
334 [ExpectedException (typeof (ArgumentNullException))]
335 public void GetStore_Application_NullObject ()
337 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
338 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null);
343 [ExpectedException (typeof (IsolatedStorageException))]
345 public void GetStore_Application_NullType ()
347 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
348 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (Type)null);
349 // again it's the lack of a manifest
354 public void GetStore_DomainScope_Evidences ()
356 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
358 Evidence de = new Evidence ();
359 de.AddHost (new Zone (SecurityZone.Internet));
360 Evidence ae = new Evidence ();
361 ae.AddHost (new Zone (SecurityZone.Intranet));
362 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), ae, typeof (Zone));
364 // Maximum size for Internet isn't (by default) Int64.MaxValue
365 Assert.AreEqual (scope, isf.Scope, "Scope");
367 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
368 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Intranet") > 0), "Zone - Assembly");
369 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
370 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), isf.DomainIdentity.ToString ()); //"Zone - Domain");
372 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
376 [ExpectedException (typeof (ArgumentNullException))]
377 public void GetStore_DomainScope_Evidence_NullAssemblyEvidence ()
379 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
381 Evidence de = new Evidence ();
382 de.AddHost (new Zone (SecurityZone.Internet));
383 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
387 [ExpectedException (typeof (ArgumentNullException))]
388 public void GetStore_DomainScope_Evidence_NullDomainEvidence ()
390 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
392 Evidence ae = new Evidence ();
393 ae.AddHost (new Zone (SecurityZone.Internet));
394 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
398 [ExpectedException (typeof (ArgumentNullException))]
399 public void GetStore_AssemblyScope_Evidence_NullAssemblyEvidence ()
401 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
403 Evidence de = new Evidence ();
404 de.AddHost (new Zone (SecurityZone.Internet));
405 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
409 public void GetStore_AssemblyScope_Evidence_NullDomainEvidence ()
411 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
413 Evidence ae = new Evidence ();
414 ae.AddHost (new Zone (SecurityZone.Internet));
415 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
420 public void RegressionBNC354539 ()
422 string filename = "test-bnc-354539";
423 byte[] expected = new byte[] { 0x01, 0x42, 0x00 };
424 byte[] actual = new byte [expected.Length];
426 using (IsolatedStorageFile file = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null)) {
427 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Create, FileAccess.Write, FileShare.None, file)) {
428 stream.Write (expected, 0, expected.Length);
432 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly ()) {
433 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read, file)) {
434 stream.Read (actual, 0, actual.Length);
437 file.DeleteFile (filename);
440 Assert.AreEqual (expected, actual);
444 [ExpectedException (typeof (ArgumentNullException))]
445 public void CreateDirectory_Null ()
447 IsolatedStorageFile.GetUserStoreForAssembly ().CreateDirectory (null);
451 public void CreateDirectory_FileWithSameNameExists ()
453 string path = "bug374377";
454 using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ()) {
455 using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream (path, FileMode.OpenOrCreate, isf)) {
458 isf.CreateDirectory (path);
461 catch (IsolatedStorageException ex) {
462 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
463 Assert.IsNull (ex.InnerException, "InnerException");
466 catch (IOException ex) {
467 Assert.AreEqual (typeof (IOException), ex.GetType (), "Type");
468 // don't leak path information
469 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
470 Assert.IsNull (ex.InnerException, "InnerException");
477 public void CreateDirectory_DirectoryWithSameNameExists ()
479 string dir = "new-dir";
480 string file = Path.Combine (dir, "new-file");
481 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
483 isf.CreateDirectory (dir);
484 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (file, FileMode.OpenOrCreate, isf)) {
487 string pattern = Path.Combine (dir, "*");
488 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file exists");
490 // create again directory
491 isf.CreateDirectory (dir);
492 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file still exists");
495 isf.DeleteFile (file);
496 isf.DeleteDirectory (dir);
502 [ExpectedException (typeof (ArgumentException))]
504 [ExpectedException (typeof (SecurityException))]
506 public void GetFilesInSubdirs ()
508 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
509 string pattern = Path.Combine ("..", "*");
510 isf.GetFileNames (pattern);
516 [ExpectedException (typeof (ArgumentException))]
517 public void GetDirsInSubDirs ()
519 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
520 isf.CreateDirectory ("subdir");
521 string [] dir_names = isf.GetDirectoryNames ("subdir/../*");
525 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=376188
526 public void CreateSubDirectory ()
528 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
529 isf.CreateDirectory ("subdir");
530 isf.CreateDirectory ("subdir/subdir2");
531 Assert.AreEqual (1, isf.GetDirectoryNames ("*").Length, "subdir");
532 Assert.AreEqual (1, isf.GetDirectoryNames ("subdir/*").Length, "subdir/subdir2");
533 isf.DeleteDirectory ("subdir/subdir2");
534 isf.DeleteDirectory ("subdir");
538 [ExpectedException (typeof (IsolatedStorageException))]
539 public void DeleteDirectory_NonEmpty ()
541 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
542 isf.CreateDirectory ("subdir");
543 isf.CreateDirectory ("subdir/subdir2");
544 isf.DeleteDirectory ("subdir");
548 public void DeleteFile ()
550 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
553 isf.DeleteFile (null);
554 Assert.Fail ("#Exc0");
555 } catch (ArgumentNullException) {
559 // We are getting an internal IndexOutOfRangeException in 2.0
560 // Not sure we want to mimic that one.
562 isf.DeleteFile (String.Empty);
563 Assert.Fail ("#Exc1");
564 } catch (IsolatedStorageException) {
569 isf.DeleteFile ("idontexist");
570 Assert.Fail ("#Exc2");
571 } catch (IsolatedStorageException) {
576 isf.DeleteFile ("../../file");
577 Assert.Fail ("#Exc3");
578 } catch (IsolatedStorageException) {
583 isf.DeleteFile ("subdir/file");
584 Assert.Fail ("#Exc4");
585 } catch (IsolatedStorageException) {
588 isf.CreateDirectory ("subdir");
590 isf.DeleteFile ("subdir");
591 Assert.Fail ("#Exc5");
592 } catch (IsolatedStorageException) {
597 public void GetStore_NullTypes ()
599 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Roaming | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain;
600 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null);
602 Assert.AreEqual (typeof (Url), isf.AssemblyIdentity.GetType (), "AssemblyIdentity");
603 Assert.AreEqual (typeof (Url), isf.DomainIdentity.GetType (), "DomainIdentity");
608 public void RemoveFromOtherInstance ()
610 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
611 IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForAssembly ();
616 Assert.Fail ("#Exc1");
617 } catch (IsolatedStorageException) {
623 public void Remove ()
625 // Test that we can call Remove several times
626 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
627 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
629 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
632 // The second call to Remove should cause an InvalidOperationException, due to
633 // marking itself as closed.
636 Assert.Fail ("#Exc1");
637 } catch (InvalidOperationException) {
640 // Open, Close and try to Remove
641 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
645 Assert.Fail ("#Exc2");
646 } catch (InvalidOperationException) {
651 public void UsedSize ()
653 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
654 IsolatedStorageFileStream isfs = isf.CreateFile ("file");
655 StreamWriter writer = new StreamWriter (isfs);
656 writer.WriteLine ("hello mono");
659 Assert.AreEqual (true, isf.UsedSize > 0, "#A0");
663 Console.WriteLine (isf.UsedSize);
664 Assert.Fail ("#Exc1");
665 } catch (InvalidOperationException) {
670 Console.WriteLine (isf.UsedSize);
671 Assert.Fail ("#Exc2");
672 } catch (ObjectDisposedException) {
677 public void IncreateQuotaTo ()
679 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
682 isf.IncreaseQuotaTo (-2);
683 Assert.Fail ("#Exc1");
684 } catch (ArgumentException) {
687 // I wonder how this behaves on some systems
689 isf.IncreaseQuotaTo (100);
690 Assert.Fail ("#Exc2");
691 } catch (ArgumentException) {
694 // Since 'Quota' seems to be returning Int64.MaxValue, we cannot truly test against a value
699 public void DirectoryExists ()
701 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
702 isf.CreateDirectory ("subdir");
703 isf.CreateDirectory ("subdir/subdir2");
704 isf.CreateDirectory ("subdir3");
706 Assert.AreEqual (true, isf.DirectoryExists ("subdir/"), "#A0");
707 Assert.AreEqual (true, isf.DirectoryExists ("subdir/subdir2/"), "#A1");
708 Assert.AreEqual (true, isf.DirectoryExists ("subdir3"), "#A2");
709 Assert.AreEqual (true, isf.DirectoryExists (String.Empty), "#A3"); // Weird
710 Assert.AreEqual (false, isf.DirectoryExists ("subdir99"), "#A4");
711 Assert.AreEqual (false, isf.DirectoryExists ("../../subdir"), "#A5");
712 Assert.AreEqual (false, isf.DirectoryExists ("*"), "#A5");
713 Assert.AreEqual (false, isf.DirectoryExists ("subdir*"), "#A6");
715 isf.DeleteDirectory ("subdir3");
716 Assert.AreEqual (false, isf.DirectoryExists ("subdir3"), "#B0");
718 isf.DeleteDirectory ("subdir/subdir2");
719 isf.DeleteDirectory ("subdir");
722 isf.DirectoryExists (null);
723 Assert.Fail ("#Exc1");
724 } catch (ArgumentNullException) {
729 isf.DirectoryExists ("subdir");
730 Assert.Fail ("#Exc2");
731 } catch (InvalidOperationException) {
736 isf.DirectoryExists ("subdir");
737 Assert.Fail ("#Exc3");
738 } catch (ObjectDisposedException) {
741 // We want to be sure that if not closing but disposing
742 // should fire ObjectDisposedException instead of InvalidOperationException
743 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
747 isf.DirectoryExists ("subdir");
748 Assert.Fail ("#Exc4");
749 } catch (ObjectDisposedException) {
754 public void FileExists ()
756 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
757 IsolatedStorageFileStream file_a = new IsolatedStorageFileStream ("file-a", FileMode.Create, isf);
758 IsolatedStorageFileStream file_b = new IsolatedStorageFileStream ("file-b", FileMode.Create, isf);
762 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
763 Assert.AreEqual (true, isf.FileExists ("file-b"), "#A1");
764 Assert.AreEqual (false, isf.FileExists (String.Empty), "#A2");
765 Assert.AreEqual (false, isf.FileExists ("file-"), "#A3");
766 Assert.AreEqual (false, isf.FileExists ("file-*"), "#A4");
767 Assert.AreEqual (false, isf.FileExists ("../../file-a"), "#A5");
769 isf.CreateDirectory ("subdir");
770 Assert.AreEqual (false, isf.FileExists ("subdir"), "#B0");
773 isf.FileExists (null);
774 Assert.Fail ("#Exc1");
775 } catch (ArgumentNullException) {
780 isf.FileExists ("file-a");
781 Assert.Fail ("#Exc2");
782 } catch (InvalidOperationException) {
787 isf.FileExists ("file-a");
788 Assert.Fail ("#Exc3");
789 } catch (ObjectDisposedException) {
794 public void CreateFile ()
796 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
797 // Make sure we are actually creating it, by first removing it in case it already exists
798 if (isf.FileExists ("file-a"))
799 isf.DeleteFile ("file-a");
801 IsolatedStorageFileStream isf_stream = isf.CreateFile ("file-a");
803 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
805 // Re-open the file that is already created, so we make sure we are passing
806 // the proper FileOpen
807 isf_stream = isf.CreateFile ("file-a");
811 isf.CreateFile (null);
812 Assert.Fail ("#Exc1");
813 } catch (ArgumentNullException) {
817 isf.CreateFile ("random-dir/fileb");
818 Assert.Fail ("#Exc2");
819 } catch (DirectoryNotFoundException) {
824 isf.CreateFile ("file-b");
825 Assert.Fail ("#Exc3");
826 } catch (InvalidOperationException) {
831 isf.CreateFile ("file-a");
832 Assert.Fail ("#Exc4");
833 } catch (ObjectDisposedException) {
838 public void GetCreationTime ()
840 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
842 // This is not causing an exception
843 isf.GetCreationTime ("doesntexist");
844 isf.GetCreationTime ("dir/doesntexist");
847 isf.GetCreationTime (String.Empty);
848 Assert.Fail ("#Exc1");
849 } catch (ArgumentException) {
853 isf.GetCreationTime (" ");
854 Assert.Fail ("#Exc2");
855 } catch (ArgumentException) {
860 isf.GetCreationTime ("doesntexist");
861 Assert.Fail ("#Exc3");
862 } catch (InvalidOperationException) {
867 isf.GetCreationTime ("doesntexist");
868 Assert.Fail ("#Exc4");
869 } catch (ObjectDisposedException) {
874 public void MoveDirectory ()
876 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
877 // Mare sure to remove them if they exist already
878 if (isf.DirectoryExists ("subdir"))
879 isf.DeleteDirectory ("subdir");
880 if (isf.DirectoryExists ("subdir-new"))
881 isf.DeleteDirectory ("subdir-new");
883 isf.CreateDirectory ("subdir");
884 Assert.AreEqual (true, isf.DirectoryExists ("subdir"), "#A0");
886 isf.MoveDirectory ("subdir", "subdir-new");
887 Assert.AreEqual (false, isf.DirectoryExists ("subdir"), "#A1");
888 Assert.AreEqual (true, isf.DirectoryExists ("subdir-new"), "#A2");
891 isf.MoveDirectory (String.Empty, "subdir-new-new");
892 Assert.Fail ("#Exc1");
893 } catch (ArgumentException) {
897 isf.MoveDirectory (" ", "subdir-new-new");
898 Assert.Fail ("#Exc2");
899 } catch (ArgumentException) {
903 isf.MoveDirectory ("doesntexist", "subdir-new-new");
904 Assert.Fail ("#Exc3");
905 } catch (DirectoryNotFoundException) {
909 isf.MoveDirectory ("doesnexist/doesntexist", "subdir-new-new");
910 Assert.Fail ("#Exc4");
911 } catch (DirectoryNotFoundException) {
915 isf.MoveDirectory ("subdir-new", "doesntexist/doesntexist");
916 Assert.Fail ("#Exc5");
917 } catch (DirectoryNotFoundException) {
920 // Out of storage dir
922 isf.MoveDirectory ("subdir-new", "../../subdir-new");
923 Assert.Fail ("#Exc6");
924 } catch (IsolatedStorageException) {
933 public void CopyFile ()
935 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
936 if (isf.FileExists ("file"))
937 isf.DeleteFile ("file");
938 if (isf.FileExists ("file-new"))
939 isf.DeleteFile ("file-new");
941 isf.CreateFile ("file").Close ();
942 isf.CopyFile ("file", "file-new");
943 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
944 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A1");
946 // At this point 'file-exists' already exists.
947 isf.CopyFile ("file", "file-new", true);
948 Assert.AreEqual (true, isf.FileExists ("file"), "#B0");
949 Assert.AreEqual (true, isf.FileExists ("file-new"), "#B1");
951 isf.CreateDirectory ("subdir");
952 isf.CreateFile ("subdir/subfile").Close ();
953 isf.CopyFile ("subdir/subfile", "subdir/subfile-new");
954 Assert.AreEqual (true, isf.FileExists ("subdir/subfile"), "#C0");
955 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#C1");
958 isf.CopyFile ("file", "file-new");
959 Assert.Fail ("#Exc0");
960 } catch (IsolatedStorageException) {
963 // Using the same file name is failing for even when passing override=true.
965 isf.CopyFile ("file-new", "file-new", true);
966 Assert.Fail ("#Exc1");
967 } catch (IsolatedStorageException) {
971 isf.CopyFile ("file-new", "file-new", false);
972 Assert.Fail ("#Exc2");
973 } catch (IsolatedStorageException) {
976 // Remove 'file-new' for cleaness purposes.
977 isf.DeleteFile ("file-new");
980 isf.CopyFile ("doesntexist", "file-new", false);
981 Assert.Fail ("#Exc3");
982 } catch (FileNotFoundException) {
986 isf.CopyFile ("doesnexist/doesntexist", "file-new", false);
987 Assert.Fail ("#Exc4");
988 } catch (DirectoryNotFoundException) {
991 // I'd have expected a DirectoryNotFoundException here.
993 isf.CopyFile ("file", "doesntexist/doesntexist");
994 Assert.Fail ("#Exc5");
995 } catch (IsolatedStorageException) {
998 // Out of storage dir
1000 isf.CopyFile ("file", "../../file");
1001 Assert.Fail ("#Exc6");
1002 } catch (IsolatedStorageException) {
1006 isf.CopyFile ("../file", "file-new");
1007 Assert.Fail ("#Exc7");
1008 } catch (IsolatedStorageException) {
1011 // We are creating a subdirectory and files within it, so remove it just in case.
1019 public void MoveFile ()
1021 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1022 // Mare sure to remove them if they exist already
1023 if (isf.FileExists ("file"))
1024 isf.DeleteFile ("file");
1025 if (isf.FileExists ("file-new"))
1026 isf.DeleteFile ("file-new");
1027 if (isf.FileExists ("subdir/subfile"))
1028 isf.DeleteFile ("subdir/subfile");
1029 if (isf.FileExists ("subdir/subfile-new"))
1030 isf.DeleteFile ("subdir/subfile-new");
1032 isf.CreateFile ("file").Close ();
1033 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
1036 isf.MoveFile ("file", "file");
1037 Assert.AreEqual (true, isf.FileExists ("file"), "#A0-1");
1039 isf.MoveFile ("file", "file-new");
1040 Assert.AreEqual (false, isf.FileExists ("file"), "#A1");
1041 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A2");
1043 isf.CreateDirectory ("subdir");
1044 isf.CreateFile ("subdir/subfile").Close ();
1045 isf.MoveFile ("subdir/subfile", "subdir/subfile-new");
1046 Assert.AreEqual (false, isf.FileExists ("subdir/subfile"), "#B0");
1047 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#B1");
1050 isf.MoveFile (String.Empty, "file-new-new");
1051 Assert.Fail ("#Exc1");
1052 } catch (ArgumentException) {
1056 isf.MoveFile (" ", "file-new-new");
1057 Assert.Fail ("#Exc2");
1058 } catch (ArgumentException e) {
1059 Console.WriteLine (e);
1063 isf.MoveFile ("doesntexist", "file-new-new");
1064 Assert.Fail ("#Exc3");
1065 } catch (FileNotFoundException) {
1068 // CopyFile is throwing a DirectoryNotFoundException here.
1070 isf.MoveFile ("doesnexist/doesntexist", "file-new-new");
1071 Assert.Fail ("#Exc4");
1072 } catch (FileNotFoundException) {
1075 // I'd have expected a DirectoryNotFoundException here.
1077 isf.MoveFile ("file-new", "doesntexist/doesntexist");
1078 Assert.Fail ("#Exc5");
1079 } catch (IsolatedStorageException) {
1082 // Out of storage dir
1084 isf.MoveFile ("file-new", "../../file-new");
1085 Assert.Fail ("#Exc6");
1086 } catch (IsolatedStorageException) {
1095 public void MultiLevel ()
1098 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1100 isf.CreateDirectory ("dir1");
1101 string [] dirs = isf.GetDirectoryNames ("*");
1102 Assert.AreEqual (dirs.Length, 1, "1a");
1103 Assert.AreEqual (dirs [0], "dir1", "1b");
1105 isf.CreateDirectory ("dir1/test");
1106 dirs = isf.GetDirectoryNames ("dir1/*");
1107 Assert.AreEqual (dirs.Length, 1, "2a");
1108 Assert.AreEqual (dirs [0], "test", "2b");
1110 isf.CreateDirectory ("dir1/test/test2a");
1111 isf.CreateDirectory ("dir1/test/test2b");
1112 dirs = isf.GetDirectoryNames ("dir1/test/*");
1113 Assert.AreEqual (dirs.Length, 2, "3a");
1114 Assert.AreEqual (dirs [0], "test2a", "3b");
1115 Assert.AreEqual (dirs [1], "test2b", "3c");
1118 isf.DeleteDirectory ("dir1/test/test2a");
1119 isf.DeleteDirectory ("dir1/test/test2b");
1120 isf.DeleteDirectory ("dir1/test");
1121 isf.DeleteDirectory ("dir1");
1126 public void RootedDirectory ()
1128 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1130 isf.CreateDirectory ("test/nested/directory/structure/without/root");
1131 isf.CreateDirectory ("/test/nested/directory/structure/with/root");
1134 isf.DeleteDirectory ("test/nested/directory/structure/without/root");
1135 isf.DeleteDirectory ("test/nested/directory/structure/without");
1137 isf.DeleteDirectory ("/test/nested/directory/structure/with/root");
1138 isf.DeleteDirectory ("/test/nested/directory/structure/with");
1139 isf.DeleteDirectory ("/test/nested/directory/structure");
1140 isf.DeleteDirectory ("/test/nested/directory");
1141 isf.DeleteDirectory ("/test/nested");
1142 isf.DeleteDirectory ("/test");