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");
60 if ((isf.Scope & IsolatedStorageScope.Application) != 0)
61 Assert.IsNotNull (isf.ApplicationIdentity, prefix + "ApplicationIdentity");
65 private void GetEnumerator (IsolatedStorageScope scope)
67 IEnumerator e = IsolatedStorageFile.GetEnumerator (scope);
71 IsolatedStorageFile isf = (IsolatedStorageFile)e.Current;
72 CheckEnumerated (++n, scope, isf);
77 public void GetEnumerator_User ()
79 GetEnumerator (IsolatedStorageScope.User);
83 [ExpectedException (typeof (ArgumentException))]
84 public void GetEnumerator_User_Details ()
86 // giving more details is bad
87 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain);
91 public void GetEnumerator_UserRoaming ()
93 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Roaming);
97 [ExpectedException (typeof (ArgumentException))]
98 public void GetEnumerator_UserRoaming_Details ()
100 // giving more details is bad
101 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain | IsolatedStorageScope.Roaming);
105 public void GetEnumerator_Machine ()
107 GetEnumerator (IsolatedStorageScope.Machine);
111 [ExpectedException (typeof (ArgumentException))]
112 public void GetEnumerator_Machine_Details ()
114 GetEnumerator (IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly);
118 [ExpectedException (typeof (ArgumentException))]
119 public void GetEnumerator_Application ()
121 // we can't enum application
122 GetEnumerator (IsolatedStorageScope.Application);
126 public void GetUserStoreForAssembly ()
128 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
129 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
130 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
132 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
133 // note: mono transforms the CodeBase into uppercase
134 // for net 1.1 which uses file:// and not file:///
135 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
136 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
137 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
139 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
143 [ExpectedException (typeof (InvalidOperationException))]
144 public void GetUserStoreForAssembly_DomainIdentity ()
146 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
147 object o = isf.DomainIdentity;
152 [ExpectedException (typeof (InvalidOperationException))]
153 public void GetUserStoreForAssembly_ApplicationIdentity ()
155 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
156 object o = isf.ApplicationIdentity;
161 public void GetUserStoreForDomain ()
163 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
164 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
165 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
167 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
168 // note: mono transforms the CodeBase into uppercase
169 // for net 1.1 which uses file:// and not file:///
170 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
171 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
172 Assert.IsTrue ((isf.DomainIdentity is Url), "DomainIdentity");
173 // note: with MS Assembly.GetEntryAssembly () only works in the default (first) AppDomain
174 // so we're using the first parameter to GetCommandLineArgs
175 string exe = Environment.GetCommandLineArgs ()[0].Replace ("\\", "/").ToUpper ();
176 Assert.IsTrue ((isf.DomainIdentity.ToString ().ToUpper ().IndexOf (exe) > 0), exe + "\n" + isf.DomainIdentity.ToString ().ToUpper ()); //"Url - Domain");
178 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
183 [ExpectedException (typeof (InvalidOperationException))]
184 public void GetUserStoreForDomain_ApplicationIdentity ()
186 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
187 object o = isf.ApplicationIdentity;
192 [ExpectedException (typeof (IsolatedStorageException))]
194 public void GetUserStoreForApplication_WithoutApplicationIdentity ()
196 // note: a manifest is required
197 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
202 [ExpectedException (typeof (IsolatedStorageException))]
204 public void GetUserStoreForApplication ()
206 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
207 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
209 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
210 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
211 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf (Assembly.GetExecutingAssembly ().CodeBase) > 0), "Url");
213 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
218 [ExpectedException (typeof (IsolatedStorageException))]
219 public void GetUserStoreForApplication_AssemblyIdentity ()
221 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
222 object o = isf.AssemblyIdentity;
226 [ExpectedException (typeof (IsolatedStorageException))]
227 public void GetUserStoreForApplication_DomainIdentity ()
229 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
230 object o = isf.DomainIdentity;
236 // This is supposed to be working only in SL.
238 [ExpectedException (typeof (NotSupportedException))]
239 public void GetUserStoreForSite ()
241 IsolatedStorageFile.GetUserStoreForSite ();
246 public void GetStore_Domain_Zone ()
248 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
249 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
250 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
252 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
253 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
254 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
255 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
256 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
258 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
263 [ExpectedException (typeof (IsolatedStorageException))]
265 public void GetStore_Domain_NonPresentEvidences ()
267 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
268 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Publisher));
272 public void GetStore_Assembly_NonPresentDomainEvidences ()
274 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
275 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
276 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
277 Assert.AreEqual (scope, isf.Scope, "Scope");
279 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
280 // note: mono transforms the CodeBase into uppercase
281 // for net 1.1 which uses file:// and not file:///
282 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
283 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
284 // DomainIdentity throws a InvalidOperationException
286 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
291 [ExpectedException (typeof (ArgumentNullException))]
292 public void GetStore_Domain_DomainNullObject ()
294 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
295 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.MyComputer));
299 [ExpectedException (typeof (ArgumentNullException))]
300 public void GetStore_Domain_AssemblyNullObject ()
302 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
303 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
307 public void GetStore_Assembly_DomainNullObject ()
309 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
310 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.Internet));
311 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
312 Assert.AreEqual (scope, isf.Scope, "Scope");
313 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
314 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
315 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
319 [ExpectedException (typeof (ArgumentNullException))]
320 public void GetStore_Assembly_AssemblyNullObject ()
322 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
323 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
327 public void GetStore_Domain_ZoneObjectZoneObject ()
329 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
330 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.Internet), new Zone (SecurityZone.Internet));
331 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
332 Assert.AreEqual (scope, isf.Scope, "Scope");
333 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
334 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
335 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
336 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Domain");
337 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
342 [ExpectedException (typeof (ArgumentNullException))]
343 public void GetStore_Application_NullObject ()
345 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
346 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null);
351 [ExpectedException (typeof (IsolatedStorageException))]
353 public void GetStore_Application_NullType ()
355 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
356 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (Type)null);
357 // again it's the lack of a manifest
362 public void GetStore_DomainScope_Evidences ()
364 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
366 Evidence de = new Evidence ();
367 de.AddHost (new Zone (SecurityZone.Internet));
368 Evidence ae = new Evidence ();
369 ae.AddHost (new Zone (SecurityZone.Intranet));
370 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), ae, typeof (Zone));
372 // Maximum size for Internet isn't (by default) Int64.MaxValue
373 Assert.AreEqual (scope, isf.Scope, "Scope");
375 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
376 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Intranet") > 0), "Zone - Assembly");
377 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
378 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), isf.DomainIdentity.ToString ()); //"Zone - Domain");
380 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
384 [ExpectedException (typeof (ArgumentNullException))]
385 public void GetStore_DomainScope_Evidence_NullAssemblyEvidence ()
387 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
389 Evidence de = new Evidence ();
390 de.AddHost (new Zone (SecurityZone.Internet));
391 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
395 [ExpectedException (typeof (ArgumentNullException))]
396 public void GetStore_DomainScope_Evidence_NullDomainEvidence ()
398 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
400 Evidence ae = new Evidence ();
401 ae.AddHost (new Zone (SecurityZone.Internet));
402 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
406 [ExpectedException (typeof (ArgumentNullException))]
407 public void GetStore_AssemblyScope_Evidence_NullAssemblyEvidence ()
409 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
411 Evidence de = new Evidence ();
412 de.AddHost (new Zone (SecurityZone.Internet));
413 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
417 public void GetStore_AssemblyScope_Evidence_NullDomainEvidence ()
419 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
421 Evidence ae = new Evidence ();
422 ae.AddHost (new Zone (SecurityZone.Internet));
423 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
428 public void RegressionBNC354539 ()
430 string filename = "test-bnc-354539";
431 byte[] expected = new byte[] { 0x01, 0x42, 0x00 };
432 byte[] actual = new byte [expected.Length];
434 using (IsolatedStorageFile file = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null)) {
435 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Create, FileAccess.Write, FileShare.None, file)) {
436 stream.Write (expected, 0, expected.Length);
440 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly ()) {
441 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read, file)) {
442 stream.Read (actual, 0, actual.Length);
445 file.DeleteFile (filename);
448 Assert.AreEqual (expected, actual);
452 [ExpectedException (typeof (ArgumentNullException))]
453 public void CreateDirectory_Null ()
455 IsolatedStorageFile.GetUserStoreForAssembly ().CreateDirectory (null);
459 public void CreateDirectory_FileWithSameNameExists ()
461 string path = "bug374377";
462 using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ()) {
463 using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream (path, FileMode.OpenOrCreate, isf)) {
466 isf.CreateDirectory (path);
469 catch (IsolatedStorageException ex) {
470 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
471 Assert.IsNull (ex.InnerException, "InnerException");
474 catch (IOException ex) {
475 Assert.AreEqual (typeof (IOException), ex.GetType (), "Type");
476 // don't leak path information
477 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
478 Assert.IsNull (ex.InnerException, "InnerException");
485 public void CreateDirectory_DirectoryWithSameNameExists ()
487 string dir = "new-dir";
488 string file = Path.Combine (dir, "new-file");
489 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
491 isf.CreateDirectory (dir);
492 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (file, FileMode.OpenOrCreate, isf)) {
495 string pattern = Path.Combine (dir, "*");
496 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file exists");
498 // create again directory
499 isf.CreateDirectory (dir);
500 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file still exists");
503 isf.DeleteFile (file);
504 isf.DeleteDirectory (dir);
510 [ExpectedException (typeof (ArgumentException))]
512 [ExpectedException (typeof (SecurityException))]
514 public void GetFilesInSubdirs ()
516 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
517 string pattern = Path.Combine ("..", "*");
518 isf.GetFileNames (pattern);
524 [ExpectedException (typeof (ArgumentException))]
525 public void GetDirsInSubDirs ()
527 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
528 isf.CreateDirectory ("subdir");
529 string [] dir_names = isf.GetDirectoryNames ("subdir/../*");
533 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=376188
534 public void CreateSubDirectory ()
536 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
537 isf.CreateDirectory ("subdir");
538 isf.CreateDirectory ("subdir/subdir2");
539 Assert.AreEqual (1, isf.GetDirectoryNames ("*").Length, "subdir");
540 Assert.AreEqual (1, isf.GetDirectoryNames ("subdir/*").Length, "subdir/subdir2");
541 isf.DeleteDirectory ("subdir/subdir2");
542 isf.DeleteDirectory ("subdir");
546 [ExpectedException (typeof (IsolatedStorageException))]
547 public void DeleteDirectory_NonEmpty ()
549 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
550 isf.CreateDirectory ("subdir");
551 isf.CreateDirectory ("subdir/subdir2");
552 isf.DeleteDirectory ("subdir");
556 public void DeleteFile ()
558 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
561 isf.DeleteFile (null);
562 Assert.Fail ("#Exc0");
563 } catch (ArgumentNullException) {
567 // We are getting an internal IndexOutOfRangeException in 2.0
568 // Not sure we want to mimic that one.
570 isf.DeleteFile (String.Empty);
571 Assert.Fail ("#Exc1");
572 } catch (IsolatedStorageException) {
577 isf.DeleteFile ("idontexist");
578 Assert.Fail ("#Exc2");
579 } catch (IsolatedStorageException) {
584 isf.DeleteFile ("../../file");
585 Assert.Fail ("#Exc3");
586 } catch (IsolatedStorageException) {
591 isf.DeleteFile ("subdir/file");
592 Assert.Fail ("#Exc4");
593 } catch (IsolatedStorageException) {
596 isf.CreateDirectory ("subdir");
598 isf.DeleteFile ("subdir");
599 Assert.Fail ("#Exc5");
600 } catch (IsolatedStorageException) {
605 public void GetStore_NullTypes ()
607 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Roaming | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain;
608 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null);
610 Assert.AreEqual (typeof (Url), isf.AssemblyIdentity.GetType (), "AssemblyIdentity");
611 Assert.AreEqual (typeof (Url), isf.DomainIdentity.GetType (), "DomainIdentity");
616 public void RemoveFromOtherInstance ()
618 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
619 IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForAssembly ();
624 Assert.Fail ("#Exc1");
625 } catch (IsolatedStorageException) {
631 public void Remove ()
633 // Test that we can call Remove several times
634 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
635 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
637 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
640 // The second call to Remove should cause an InvalidOperationException, due to
641 // marking itself as closed.
644 Assert.Fail ("#Exc1");
645 } catch (InvalidOperationException) {
648 // Open, Close and try to Remove
649 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
653 Assert.Fail ("#Exc2");
654 } catch (InvalidOperationException) {
659 public void UsedSize ()
661 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
662 IsolatedStorageFileStream isfs = isf.CreateFile ("file");
663 StreamWriter writer = new StreamWriter (isfs);
664 writer.WriteLine ("hello mono");
667 Assert.AreEqual (true, isf.UsedSize > 0, "#A0");
671 Console.WriteLine (isf.UsedSize);
672 Assert.Fail ("#Exc1");
673 } catch (InvalidOperationException) {
678 Console.WriteLine (isf.UsedSize);
679 Assert.Fail ("#Exc2");
680 } catch (ObjectDisposedException) {
685 public void IncreateQuotaTo ()
687 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
690 isf.IncreaseQuotaTo (-2);
691 Assert.Fail ("#Exc1");
692 } catch (ArgumentException) {
695 // I wonder how this behaves on some systems
697 isf.IncreaseQuotaTo (100);
698 Assert.Fail ("#Exc2");
699 } catch (ArgumentException) {
702 // Since 'Quota' seems to be returning Int64.MaxValue, we cannot truly test against a value
707 public void DirectoryExists ()
709 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
710 isf.CreateDirectory ("subdir");
711 isf.CreateDirectory ("subdir/subdir2");
712 isf.CreateDirectory ("subdir3");
714 Assert.AreEqual (true, isf.DirectoryExists ("subdir/"), "#A0");
715 Assert.AreEqual (true, isf.DirectoryExists ("subdir/subdir2/"), "#A1");
716 Assert.AreEqual (true, isf.DirectoryExists ("subdir3"), "#A2");
717 Assert.AreEqual (true, isf.DirectoryExists (String.Empty), "#A3"); // Weird
718 Assert.AreEqual (false, isf.DirectoryExists ("subdir99"), "#A4");
719 Assert.AreEqual (false, isf.DirectoryExists ("../../subdir"), "#A5");
720 Assert.AreEqual (false, isf.DirectoryExists ("*"), "#A5");
721 Assert.AreEqual (false, isf.DirectoryExists ("subdir*"), "#A6");
723 isf.DeleteDirectory ("subdir3");
724 Assert.AreEqual (false, isf.DirectoryExists ("subdir3"), "#B0");
726 isf.DeleteDirectory ("subdir/subdir2");
727 isf.DeleteDirectory ("subdir");
730 isf.DirectoryExists (null);
731 Assert.Fail ("#Exc1");
732 } catch (ArgumentNullException) {
737 isf.DirectoryExists ("subdir");
738 Assert.Fail ("#Exc2");
739 } catch (InvalidOperationException) {
744 isf.DirectoryExists ("subdir");
745 Assert.Fail ("#Exc3");
746 } catch (ObjectDisposedException) {
749 // We want to be sure that if not closing but disposing
750 // should fire ObjectDisposedException instead of InvalidOperationException
751 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
755 isf.DirectoryExists ("subdir");
756 Assert.Fail ("#Exc4");
757 } catch (ObjectDisposedException) {
762 public void FileExists ()
764 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
765 IsolatedStorageFileStream file_a = new IsolatedStorageFileStream ("file-a", FileMode.Create, isf);
766 IsolatedStorageFileStream file_b = new IsolatedStorageFileStream ("file-b", FileMode.Create, isf);
770 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
771 Assert.AreEqual (true, isf.FileExists ("file-b"), "#A1");
772 Assert.AreEqual (false, isf.FileExists (String.Empty), "#A2");
773 Assert.AreEqual (false, isf.FileExists ("file-"), "#A3");
774 Assert.AreEqual (false, isf.FileExists ("file-*"), "#A4");
775 Assert.AreEqual (false, isf.FileExists ("../../file-a"), "#A5");
777 isf.CreateDirectory ("subdir");
778 Assert.AreEqual (false, isf.FileExists ("subdir"), "#B0");
781 isf.FileExists (null);
782 Assert.Fail ("#Exc1");
783 } catch (ArgumentNullException) {
788 isf.FileExists ("file-a");
789 Assert.Fail ("#Exc2");
790 } catch (InvalidOperationException) {
795 isf.FileExists ("file-a");
796 Assert.Fail ("#Exc3");
797 } catch (ObjectDisposedException) {
802 public void CreateFile ()
804 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
805 // Make sure we are actually creating it, by first removing it in case it already exists
806 if (isf.FileExists ("file-a"))
807 isf.DeleteFile ("file-a");
809 IsolatedStorageFileStream isf_stream = isf.CreateFile ("file-a");
811 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
813 // Re-open the file that is already created, so we make sure we are passing
814 // the proper FileOpen
815 isf_stream = isf.CreateFile ("file-a");
819 isf.CreateFile (null);
820 Assert.Fail ("#Exc1");
821 } catch (ArgumentNullException) {
825 isf.CreateFile ("random-dir/fileb");
826 Assert.Fail ("#Exc2");
827 } catch (DirectoryNotFoundException) {
832 isf.CreateFile ("file-b");
833 Assert.Fail ("#Exc3");
834 } catch (InvalidOperationException) {
839 isf.CreateFile ("file-a");
840 Assert.Fail ("#Exc4");
841 } catch (ObjectDisposedException) {
846 public void GetCreationTime ()
848 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
850 // This is not causing an exception
851 isf.GetCreationTime ("doesntexist");
852 isf.GetCreationTime ("dir/doesntexist");
855 isf.GetCreationTime (String.Empty);
856 Assert.Fail ("#Exc1");
857 } catch (ArgumentException) {
861 isf.GetCreationTime (" ");
862 Assert.Fail ("#Exc2");
863 } catch (ArgumentException) {
868 isf.GetCreationTime ("doesntexist");
869 Assert.Fail ("#Exc3");
870 } catch (InvalidOperationException) {
875 isf.GetCreationTime ("doesntexist");
876 Assert.Fail ("#Exc4");
877 } catch (ObjectDisposedException) {
882 public void MoveDirectory ()
884 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
885 // Mare sure to remove them if they exist already
886 if (isf.DirectoryExists ("subdir"))
887 isf.DeleteDirectory ("subdir");
888 if (isf.DirectoryExists ("subdir-new"))
889 isf.DeleteDirectory ("subdir-new");
891 isf.CreateDirectory ("subdir");
892 Assert.AreEqual (true, isf.DirectoryExists ("subdir"), "#A0");
894 isf.MoveDirectory ("subdir", "subdir-new");
895 Assert.AreEqual (false, isf.DirectoryExists ("subdir"), "#A1");
896 Assert.AreEqual (true, isf.DirectoryExists ("subdir-new"), "#A2");
899 isf.MoveDirectory (String.Empty, "subdir-new-new");
900 Assert.Fail ("#Exc1");
901 } catch (ArgumentException) {
905 isf.MoveDirectory (" ", "subdir-new-new");
906 Assert.Fail ("#Exc2");
907 } catch (ArgumentException) {
911 isf.MoveDirectory ("doesntexist", "subdir-new-new");
912 Assert.Fail ("#Exc3");
913 } catch (DirectoryNotFoundException) {
917 isf.MoveDirectory ("doesnexist/doesntexist", "subdir-new-new");
918 Assert.Fail ("#Exc4");
919 } catch (DirectoryNotFoundException) {
923 isf.MoveDirectory ("subdir-new", "doesntexist/doesntexist");
924 Assert.Fail ("#Exc5");
925 } catch (DirectoryNotFoundException) {
928 // Out of storage dir
930 isf.MoveDirectory ("subdir-new", "../../subdir-new");
931 Assert.Fail ("#Exc6");
932 } catch (IsolatedStorageException) {
941 public void CopyFile ()
943 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
944 if (isf.FileExists ("file"))
945 isf.DeleteFile ("file");
946 if (isf.FileExists ("file-new"))
947 isf.DeleteFile ("file-new");
949 isf.CreateFile ("file").Close ();
950 isf.CopyFile ("file", "file-new");
951 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
952 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A1");
954 // At this point 'file-exists' already exists.
955 isf.CopyFile ("file", "file-new", true);
956 Assert.AreEqual (true, isf.FileExists ("file"), "#B0");
957 Assert.AreEqual (true, isf.FileExists ("file-new"), "#B1");
959 isf.CreateDirectory ("subdir");
960 isf.CreateFile ("subdir/subfile").Close ();
961 isf.CopyFile ("subdir/subfile", "subdir/subfile-new");
962 Assert.AreEqual (true, isf.FileExists ("subdir/subfile"), "#C0");
963 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#C1");
966 isf.CopyFile ("file", "file-new");
967 Assert.Fail ("#Exc0");
968 } catch (IsolatedStorageException) {
971 // Using the same file name is failing for even when passing override=true.
973 isf.CopyFile ("file-new", "file-new", true);
974 Assert.Fail ("#Exc1");
975 } catch (IsolatedStorageException) {
979 isf.CopyFile ("file-new", "file-new", false);
980 Assert.Fail ("#Exc2");
981 } catch (IsolatedStorageException) {
984 // Remove 'file-new' for cleaness purposes.
985 isf.DeleteFile ("file-new");
988 isf.CopyFile ("doesntexist", "file-new", false);
989 Assert.Fail ("#Exc3");
990 } catch (FileNotFoundException) {
994 isf.CopyFile ("doesnexist/doesntexist", "file-new", false);
995 Assert.Fail ("#Exc4");
996 } catch (DirectoryNotFoundException) {
999 // I'd have expected a DirectoryNotFoundException here.
1001 isf.CopyFile ("file", "doesntexist/doesntexist");
1002 Assert.Fail ("#Exc5");
1003 } catch (IsolatedStorageException) {
1006 // Out of storage dir
1008 isf.CopyFile ("file", "../../file");
1009 Assert.Fail ("#Exc6");
1010 } catch (IsolatedStorageException) {
1014 isf.CopyFile ("../file", "file-new");
1015 Assert.Fail ("#Exc7");
1016 } catch (IsolatedStorageException) {
1019 // We are creating a subdirectory and files within it, so remove it just in case.
1027 public void MoveFile ()
1029 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1030 // Mare sure to remove them if they exist already
1031 if (isf.FileExists ("file"))
1032 isf.DeleteFile ("file");
1033 if (isf.FileExists ("file-new"))
1034 isf.DeleteFile ("file-new");
1035 if (isf.FileExists ("subdir/subfile"))
1036 isf.DeleteFile ("subdir/subfile");
1037 if (isf.FileExists ("subdir/subfile-new"))
1038 isf.DeleteFile ("subdir/subfile-new");
1040 isf.CreateFile ("file").Close ();
1041 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
1044 isf.MoveFile ("file", "file");
1045 Assert.AreEqual (true, isf.FileExists ("file"), "#A0-1");
1047 isf.MoveFile ("file", "file-new");
1048 Assert.AreEqual (false, isf.FileExists ("file"), "#A1");
1049 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A2");
1051 isf.CreateDirectory ("subdir");
1052 isf.CreateFile ("subdir/subfile").Close ();
1053 isf.MoveFile ("subdir/subfile", "subdir/subfile-new");
1054 Assert.AreEqual (false, isf.FileExists ("subdir/subfile"), "#B0");
1055 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#B1");
1058 isf.MoveFile (String.Empty, "file-new-new");
1059 Assert.Fail ("#Exc1");
1060 } catch (ArgumentException) {
1064 isf.MoveFile (" ", "file-new-new");
1065 Assert.Fail ("#Exc2");
1066 } catch (ArgumentException e) {
1067 Console.WriteLine (e);
1071 isf.MoveFile ("doesntexist", "file-new-new");
1072 Assert.Fail ("#Exc3");
1073 } catch (FileNotFoundException) {
1076 // CopyFile is throwing a DirectoryNotFoundException here.
1078 isf.MoveFile ("doesnexist/doesntexist", "file-new-new");
1079 Assert.Fail ("#Exc4");
1080 } catch (FileNotFoundException) {
1083 // I'd have expected a DirectoryNotFoundException here.
1085 isf.MoveFile ("file-new", "doesntexist/doesntexist");
1086 Assert.Fail ("#Exc5");
1087 } catch (IsolatedStorageException) {
1090 // Out of storage dir
1092 isf.MoveFile ("file-new", "../../file-new");
1093 Assert.Fail ("#Exc6");
1094 } catch (IsolatedStorageException) {
1103 public void MultiLevel ()
1106 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1108 isf.CreateDirectory ("dir1");
1109 string [] dirs = isf.GetDirectoryNames ("*");
1110 Assert.AreEqual (dirs.Length, 1, "1a");
1111 Assert.AreEqual (dirs [0], "dir1", "1b");
1113 isf.CreateDirectory ("dir1/test");
1114 dirs = isf.GetDirectoryNames ("dir1/*");
1115 Assert.AreEqual (dirs.Length, 1, "2a");
1116 Assert.AreEqual (dirs [0], "test", "2b");
1118 isf.CreateDirectory ("dir1/test/test2a");
1119 isf.CreateDirectory ("dir1/test/test2b");
1120 dirs = isf.GetDirectoryNames ("dir1/test/*");
1121 Assert.AreEqual (dirs.Length, 2, "3a");
1122 Assert.AreEqual (dirs [0], "test2a", "3b");
1123 Assert.AreEqual (dirs [1], "test2b", "3c");
1126 isf.DeleteDirectory ("dir1/test/test2a");
1127 isf.DeleteDirectory ("dir1/test/test2b");
1128 isf.DeleteDirectory ("dir1/test");
1129 isf.DeleteDirectory ("dir1");
1134 public void RootedDirectory ()
1136 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1138 isf.CreateDirectory ("test/nested/directory/structure/without/root");
1139 isf.CreateDirectory ("/test/nested/directory/structure/with/root");
1142 isf.DeleteDirectory ("test/nested/directory/structure/without/root");
1143 isf.DeleteDirectory ("test/nested/directory/structure/without");
1145 isf.DeleteDirectory ("/test/nested/directory/structure/with/root");
1146 isf.DeleteDirectory ("/test/nested/directory/structure/with");
1147 isf.DeleteDirectory ("/test/nested/directory/structure");
1148 isf.DeleteDirectory ("/test/nested/directory");
1149 isf.DeleteDirectory ("/test/nested");
1150 isf.DeleteDirectory ("/test");