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;
227 // This is supposed to be working only in SL.
229 [ExpectedException (typeof (NotSupportedException))]
230 public void GetUserStoreForSite ()
232 IsolatedStorageFile.GetUserStoreForSite ();
236 public void GetStore_Domain_Zone ()
238 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
239 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
240 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
242 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
243 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
244 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
245 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
246 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
248 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
253 [ExpectedException (typeof (IsolatedStorageException))]
255 public void GetStore_Domain_NonPresentEvidences ()
257 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
258 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Publisher));
262 public void GetStore_Assembly_NonPresentDomainEvidences ()
264 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
265 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
266 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
267 Assert.AreEqual (scope, isf.Scope, "Scope");
269 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
270 // note: mono transforms the CodeBase into uppercase
271 // for net 1.1 which uses file:// and not file:///
272 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
273 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
274 // DomainIdentity throws a InvalidOperationException
276 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
281 [ExpectedException (typeof (ArgumentNullException))]
282 public void GetStore_Domain_DomainNullObject ()
284 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
285 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.MyComputer));
289 [ExpectedException (typeof (ArgumentNullException))]
290 public void GetStore_Domain_AssemblyNullObject ()
292 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
293 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
297 public void GetStore_Assembly_DomainNullObject ()
299 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
300 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.Internet));
301 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
302 Assert.AreEqual (scope, isf.Scope, "Scope");
303 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
304 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
305 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
309 [ExpectedException (typeof (ArgumentNullException))]
310 public void GetStore_Assembly_AssemblyNullObject ()
312 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
313 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
317 public void GetStore_Domain_ZoneObjectZoneObject ()
319 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
320 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.Internet), new Zone (SecurityZone.Internet));
321 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
322 Assert.AreEqual (scope, isf.Scope, "Scope");
323 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
324 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
325 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
326 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Domain");
327 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
332 [ExpectedException (typeof (ArgumentNullException))]
333 public void GetStore_Application_NullObject ()
335 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
336 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null);
341 [ExpectedException (typeof (IsolatedStorageException))]
343 public void GetStore_Application_NullType ()
345 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
346 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (Type)null);
347 // again it's the lack of a manifest
352 public void GetStore_DomainScope_Evidences ()
354 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
356 Evidence de = new Evidence ();
357 de.AddHost (new Zone (SecurityZone.Internet));
358 Evidence ae = new Evidence ();
359 ae.AddHost (new Zone (SecurityZone.Intranet));
360 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), ae, typeof (Zone));
362 // Maximum size for Internet isn't (by default) Int64.MaxValue
363 Assert.AreEqual (scope, isf.Scope, "Scope");
365 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
366 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Intranet") > 0), "Zone - Assembly");
367 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
368 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), isf.DomainIdentity.ToString ()); //"Zone - Domain");
370 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
374 [ExpectedException (typeof (ArgumentNullException))]
375 public void GetStore_DomainScope_Evidence_NullAssemblyEvidence ()
377 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
379 Evidence de = new Evidence ();
380 de.AddHost (new Zone (SecurityZone.Internet));
381 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
385 [ExpectedException (typeof (ArgumentNullException))]
386 public void GetStore_DomainScope_Evidence_NullDomainEvidence ()
388 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
390 Evidence ae = new Evidence ();
391 ae.AddHost (new Zone (SecurityZone.Internet));
392 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
396 [ExpectedException (typeof (ArgumentNullException))]
397 public void GetStore_AssemblyScope_Evidence_NullAssemblyEvidence ()
399 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
401 Evidence de = new Evidence ();
402 de.AddHost (new Zone (SecurityZone.Internet));
403 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
407 public void GetStore_AssemblyScope_Evidence_NullDomainEvidence ()
409 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
411 Evidence ae = new Evidence ();
412 ae.AddHost (new Zone (SecurityZone.Internet));
413 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
418 public void RegressionBNC354539 ()
420 string filename = "test-bnc-354539";
421 byte[] expected = new byte[] { 0x01, 0x42, 0x00 };
422 byte[] actual = new byte [expected.Length];
424 using (IsolatedStorageFile file = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null)) {
425 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Create, FileAccess.Write, FileShare.None, file)) {
426 stream.Write (expected, 0, expected.Length);
430 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly ()) {
431 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read, file)) {
432 stream.Read (actual, 0, actual.Length);
435 file.DeleteFile (filename);
438 Assert.AreEqual (expected, actual);
442 [ExpectedException (typeof (ArgumentNullException))]
443 public void CreateDirectory_Null ()
445 IsolatedStorageFile.GetUserStoreForAssembly ().CreateDirectory (null);
449 public void CreateDirectory_FileWithSameNameExists ()
451 string path = "bug374377";
452 using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ()) {
453 using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream (path, FileMode.OpenOrCreate, isf)) {
456 isf.CreateDirectory (path);
458 catch (IsolatedStorageException ex) {
459 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
460 Assert.IsNull (ex.InnerException, "InnerException");
466 public void CreateDirectory_DirectoryWithSameNameExists ()
468 string dir = "new-dir";
469 string file = Path.Combine (dir, "new-file");
470 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
472 isf.CreateDirectory (dir);
473 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (file, FileMode.OpenOrCreate, isf)) {
476 string pattern = Path.Combine (dir, "*");
477 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file exists");
479 // create again directory
480 isf.CreateDirectory (dir);
481 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file still exists");
484 isf.DeleteFile (file);
485 isf.DeleteDirectory (dir);
490 [ExpectedException (typeof (ArgumentException))]
491 public void GetFilesInSubdirs ()
493 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
494 string pattern = Path.Combine ("..", "*");
495 isf.GetFileNames (pattern);
500 [ExpectedException (typeof (ArgumentException))]
501 public void GetDirsInSubDirs ()
503 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
504 isf.CreateDirectory ("subdir");
505 string [] dir_names = isf.GetDirectoryNames ("subdir/../*");
508 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=376188
509 public void CreateSubDirectory ()
511 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
512 isf.CreateDirectory ("subdir");
513 isf.CreateDirectory ("subdir/subdir2");
514 Assert.AreEqual (1, isf.GetDirectoryNames ("*").Length, "subdir");
515 Assert.AreEqual (1, isf.GetDirectoryNames ("subdir/*").Length, "subdir/subdir2");
516 isf.DeleteDirectory ("subdir/subdir2");
517 isf.DeleteDirectory ("subdir");
521 [ExpectedException (typeof (IsolatedStorageException))]
522 public void DeleteDirectory_NonEmpty ()
524 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
525 isf.CreateDirectory ("subdir");
526 isf.CreateDirectory ("subdir/subdir2");
527 isf.DeleteDirectory ("subdir");
531 public void DeleteFile ()
533 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
536 isf.DeleteFile (null);
537 Assert.Fail ("#Exc0");
538 } catch (ArgumentNullException) {
541 // We are getting an internal IndexOutOfRangeException in 2.0
542 // Not sure we want to mimic that one.
544 isf.DeleteFile (String.Empty);
545 Assert.Fail ("#Exc1");
546 } catch (IsolatedStorageException) {
550 isf.DeleteFile ("idontexist");
551 Assert.Fail ("#Exc2");
552 } catch (IsolatedStorageException) {
556 isf.DeleteFile ("../../file");
557 Assert.Fail ("#Exc3");
558 } catch (IsolatedStorageException) {
562 isf.DeleteFile ("subdir/file");
563 Assert.Fail ("#Exc4");
564 } catch (IsolatedStorageException) {
567 isf.CreateDirectory ("subdir");
569 isf.DeleteFile ("subdir");
570 Assert.Fail ("#Exc5");
571 } catch (IsolatedStorageException) {
576 public void GetStore_NullTypes ()
578 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Roaming | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain;
579 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null);
581 Assert.AreEqual (typeof (Url), isf.AssemblyIdentity.GetType (), "AssemblyIdentity");
582 Assert.AreEqual (typeof (Url), isf.DomainIdentity.GetType (), "DomainIdentity");
587 public void RemoveFromOtherInstance ()
589 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
590 IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForAssembly ();
595 Assert.Fail ("#Exc1");
596 } catch (IsolatedStorageException) {
601 public void Remove ()
603 // Test that we can call Remove several times
604 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
605 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
607 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
610 // The second call to Remove should cause an InvalidOperationException, due to
611 // marking itself as closed.
614 Assert.Fail ("#Exc1");
615 } catch (InvalidOperationException) {
618 // Open, Close and try to Remove
619 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
623 Assert.Fail ("#Exc2");
624 } catch (InvalidOperationException) {
629 public void UsedSize ()
631 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
632 IsolatedStorageFileStream isfs = isf.CreateFile ("file");
633 StreamWriter writer = new StreamWriter (isfs);
634 writer.WriteLine ("hello mono");
637 Assert.AreEqual (true, isf.UsedSize > 0, "#A0");
641 Console.WriteLine (isf.UsedSize);
642 Assert.Fail ("#Exc1");
643 } catch (InvalidOperationException) {
648 Console.WriteLine (isf.UsedSize);
649 Assert.Fail ("#Exc2");
650 } catch (ObjectDisposedException) {
655 public void IncreateQuotaTo ()
657 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
660 isf.IncreaseQuotaTo (-2);
661 Assert.Fail ("#Exc1");
662 } catch (ArgumentException) {
665 // I wonder how this behaves on some systems
667 isf.IncreaseQuotaTo (100);
668 Assert.Fail ("#Exc2");
669 } catch (ArgumentException) {
672 // Since 'Quota' seems to be returning Int64.MaxValue, we cannot truly test against a value
677 public void DirectoryExists ()
679 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
680 isf.CreateDirectory ("subdir");
681 isf.CreateDirectory ("subdir/subdir2");
682 isf.CreateDirectory ("subdir3");
684 Assert.AreEqual (true, isf.DirectoryExists ("subdir/"), "#A0");
685 Assert.AreEqual (true, isf.DirectoryExists ("subdir/subdir2/"), "#A1");
686 Assert.AreEqual (true, isf.DirectoryExists ("subdir3"), "#A2");
687 Assert.AreEqual (true, isf.DirectoryExists (String.Empty), "#A3"); // Weird
688 Assert.AreEqual (false, isf.DirectoryExists ("subdir99"), "#A4");
689 Assert.AreEqual (false, isf.DirectoryExists ("../../subdir"), "#A5");
690 Assert.AreEqual (false, isf.DirectoryExists ("*"), "#A5");
691 Assert.AreEqual (false, isf.DirectoryExists ("subdir*"), "#A6");
693 isf.DeleteDirectory ("subdir3");
694 Assert.AreEqual (false, isf.DirectoryExists ("subdir3"), "#B0");
696 isf.DeleteDirectory ("subdir/subdir2");
697 isf.DeleteDirectory ("subdir");
700 isf.DirectoryExists (null);
701 Assert.Fail ("#Exc1");
702 } catch (ArgumentNullException) {
707 isf.DirectoryExists ("subdir");
708 Assert.Fail ("#Exc2");
709 } catch (InvalidOperationException) {
714 isf.DirectoryExists ("subdir");
715 Assert.Fail ("#Exc3");
716 } catch (ObjectDisposedException) {
719 // We want to be sure that if not closing but disposing
720 // should fire ObjectDisposedException instead of InvalidOperationException
721 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
725 isf.DirectoryExists ("subdir");
726 Assert.Fail ("#Exc4");
727 } catch (ObjectDisposedException) {
732 public void FileExists ()
734 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
735 IsolatedStorageFileStream file_a = new IsolatedStorageFileStream ("file-a", FileMode.Create, isf);
736 IsolatedStorageFileStream file_b = new IsolatedStorageFileStream ("file-b", FileMode.Create, isf);
740 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
741 Assert.AreEqual (true, isf.FileExists ("file-b"), "#A1");
742 Assert.AreEqual (false, isf.FileExists (String.Empty), "#A2");
743 Assert.AreEqual (false, isf.FileExists ("file-"), "#A3");
744 Assert.AreEqual (false, isf.FileExists ("file-*"), "#A4");
745 Assert.AreEqual (false, isf.FileExists ("../../file-a"), "#A5");
747 isf.CreateDirectory ("subdir");
748 Assert.AreEqual (false, isf.FileExists ("subdir"), "#B0");
751 isf.FileExists (null);
752 Assert.Fail ("#Exc1");
753 } catch (ArgumentNullException) {
758 isf.FileExists ("file-a");
759 Assert.Fail ("#Exc2");
760 } catch (InvalidOperationException) {
765 isf.FileExists ("file-a");
766 Assert.Fail ("#Exc3");
767 } catch (ObjectDisposedException) {
772 public void CreateFile ()
774 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
775 // Make sure we are actually creating it, by first removing it in case it already exists
776 if (isf.FileExists ("file-a"))
777 isf.DeleteFile ("file-a");
779 IsolatedStorageFileStream isf_stream = isf.CreateFile ("file-a");
781 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
783 // Re-open the file that is already created, so we make sure we are passing
784 // the proper FileOpen
785 isf_stream = isf.CreateFile ("file-a");
789 isf.CreateFile (null);
790 Assert.Fail ("#Exc1");
791 } catch (ArgumentNullException) {
795 isf.CreateFile ("random-dir/fileb");
796 Assert.Fail ("#Exc2");
797 } catch (DirectoryNotFoundException) {
802 isf.CreateFile ("file-b");
803 Assert.Fail ("#Exc3");
804 } catch (InvalidOperationException) {
809 isf.CreateFile ("file-a");
810 Assert.Fail ("#Exc4");
811 } catch (ObjectDisposedException) {
816 public void GetCreationTime ()
818 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
820 // This is not causing an exception
821 isf.GetCreationTime ("doesntexist");
822 isf.GetCreationTime ("dir/doesntexist");
825 isf.GetCreationTime (String.Empty);
826 Assert.Fail ("#Exc1");
827 } catch (ArgumentException) {
831 isf.GetCreationTime (" ");
832 Assert.Fail ("#Exc2");
833 } catch (ArgumentException) {
838 isf.GetCreationTime ("doesntexist");
839 Assert.Fail ("#Exc3");
840 } catch (InvalidOperationException) {
845 isf.GetCreationTime ("doesntexist");
846 Assert.Fail ("#Exc4");
847 } catch (ObjectDisposedException) {
852 public void MoveDirectory ()
854 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
855 // Mare sure to remove them if they exist already
856 if (isf.DirectoryExists ("subdir"))
857 isf.DeleteDirectory ("subdir");
858 if (isf.DirectoryExists ("subdir-new"))
859 isf.DeleteDirectory ("subdir-new");
861 isf.CreateDirectory ("subdir");
862 Assert.AreEqual (true, isf.DirectoryExists ("subdir"), "#A0");
864 isf.MoveDirectory ("subdir", "subdir-new");
865 Assert.AreEqual (false, isf.DirectoryExists ("subdir"), "#A1");
866 Assert.AreEqual (true, isf.DirectoryExists ("subdir-new"), "#A2");
869 isf.MoveDirectory (String.Empty, "subdir-new-new");
870 Assert.Fail ("#Exc1");
871 } catch (ArgumentException) {
875 isf.MoveDirectory (" ", "subdir-new-new");
876 Assert.Fail ("#Exc2");
877 } catch (ArgumentException) {
881 isf.MoveDirectory ("doesntexist", "subdir-new-new");
882 Assert.Fail ("#Exc3");
883 } catch (DirectoryNotFoundException) {
887 isf.MoveDirectory ("doesnexist/doesntexist", "subdir-new-new");
888 Assert.Fail ("#Exc4");
889 } catch (DirectoryNotFoundException) {
893 isf.MoveDirectory ("subdir-new", "doesntexist/doesntexist");
894 Assert.Fail ("#Exc5");
895 } catch (DirectoryNotFoundException) {
898 // Out of storage dir
900 isf.MoveDirectory ("subdir-new", "../../subdir-new");
901 Assert.Fail ("#Exc6");
902 } catch (IsolatedStorageException) {
911 public void CopyFile ()
913 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
914 if (isf.FileExists ("file"))
915 isf.DeleteFile ("file");
916 if (isf.FileExists ("file-new"))
917 isf.DeleteFile ("file-new");
919 isf.CreateFile ("file").Close ();
920 isf.CopyFile ("file", "file-new");
921 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
922 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A1");
924 // At this point 'file-exists' already exists.
925 isf.CopyFile ("file", "file-new", true);
926 Assert.AreEqual (true, isf.FileExists ("file"), "#B0");
927 Assert.AreEqual (true, isf.FileExists ("file-new"), "#B1");
929 isf.CreateDirectory ("subdir");
930 isf.CreateFile ("subdir/subfile").Close ();
931 isf.CopyFile ("subdir/subfile", "subdir/subfile-new");
932 Assert.AreEqual (true, isf.FileExists ("subdir/subfile"), "#C0");
933 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#C1");
936 isf.CopyFile ("file", "file-new");
937 Assert.Fail ("#Exc0");
938 } catch (IsolatedStorageException) {
941 // Using the same file name is failing for even when passing override=true.
943 isf.CopyFile ("file-new", "file-new", true);
944 Assert.Fail ("#Exc1");
945 } catch (IsolatedStorageException) {
949 isf.CopyFile ("file-new", "file-new", false);
950 Assert.Fail ("#Exc2");
951 } catch (IsolatedStorageException) {
954 // Remove 'file-new' for cleaness purposes.
955 isf.DeleteFile ("file-new");
958 isf.CopyFile ("doesntexist", "file-new", false);
959 Assert.Fail ("#Exc3");
960 } catch (FileNotFoundException) {
964 isf.CopyFile ("doesnexist/doesntexist", "file-new", false);
965 Assert.Fail ("#Exc4");
966 } catch (DirectoryNotFoundException) {
969 // I'd have expected a DirectoryNotFoundException here.
971 isf.CopyFile ("file", "doesntexist/doesntexist");
972 Assert.Fail ("#Exc5");
973 } catch (IsolatedStorageException) {
976 // Out of storage dir
978 isf.CopyFile ("file", "../../file");
979 Assert.Fail ("#Exc6");
980 } catch (IsolatedStorageException) {
984 isf.CopyFile ("../file", "file-new");
985 Assert.Fail ("#Exc7");
986 } catch (IsolatedStorageException) {
989 // We are creating a subdirectory and files within it, so remove it just in case.
997 public void MoveFile ()
999 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1000 // Mare sure to remove them if they exist already
1001 if (isf.FileExists ("file"))
1002 isf.DeleteFile ("file");
1003 if (isf.FileExists ("file-new"))
1004 isf.DeleteFile ("file-new");
1005 if (isf.FileExists ("subdir/subfile"))
1006 isf.DeleteFile ("subdir/subfile");
1007 if (isf.FileExists ("subdir/subfile-new"))
1008 isf.DeleteFile ("subdir/subfile-new");
1010 isf.CreateFile ("file").Close ();
1011 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
1014 isf.MoveFile ("file", "file");
1015 Assert.AreEqual (true, isf.FileExists ("file"), "#A0-1");
1017 isf.MoveFile ("file", "file-new");
1018 Assert.AreEqual (false, isf.FileExists ("file"), "#A1");
1019 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A2");
1021 isf.CreateDirectory ("subdir");
1022 isf.CreateFile ("subdir/subfile").Close ();
1023 isf.MoveFile ("subdir/subfile", "subdir/subfile-new");
1024 Assert.AreEqual (false, isf.FileExists ("subdir/subfile"), "#B0");
1025 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#B1");
1028 isf.MoveFile (String.Empty, "file-new-new");
1029 Assert.Fail ("#Exc1");
1030 } catch (ArgumentException) {
1034 isf.MoveFile (" ", "file-new-new");
1035 Assert.Fail ("#Exc2");
1036 } catch (ArgumentException e) {
1040 isf.MoveFile ("doesntexist", "file-new-new");
1041 Assert.Fail ("#Exc3");
1042 } catch (FileNotFoundException) {
1045 // CopyFile is throwing a DirectoryNotFoundException here.
1047 isf.MoveFile ("doesnexist/doesntexist", "file-new-new");
1048 Assert.Fail ("#Exc4");
1049 } catch (FileNotFoundException) {
1052 // I'd have expected a DirectoryNotFoundException here.
1054 isf.MoveFile ("file-new", "doesntexist/doesntexist");
1055 Assert.Fail ("#Exc5");
1056 } catch (IsolatedStorageException) {
1059 // Out of storage dir
1061 isf.MoveFile ("file-new", "../../file-new");
1062 Assert.Fail ("#Exc6");
1063 } catch (IsolatedStorageException) {
1072 public void MultiLevel ()
1075 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1077 isf.CreateDirectory ("dir1");
1078 string [] dirs = isf.GetDirectoryNames ("*");
1079 Assert.AreEqual (dirs.Length, 1, "1a");
1080 Assert.AreEqual (dirs [0], "dir1", "1b");
1082 isf.CreateDirectory ("dir1/test");
1083 dirs = isf.GetDirectoryNames ("dir1/*");
1084 Assert.AreEqual (dirs.Length, 1, "2a");
1085 Assert.AreEqual (dirs [0], "test", "2b");
1087 isf.CreateDirectory ("dir1/test/test2a");
1088 isf.CreateDirectory ("dir1/test/test2b");
1089 dirs = isf.GetDirectoryNames ("dir1/test/*");
1090 Assert.AreEqual (dirs.Length, 2, "3a");
1091 Assert.AreEqual (dirs [0], "test2a", "3b");
1092 Assert.AreEqual (dirs [1], "test2b", "3c");
1095 isf.DeleteDirectory ("dir1/test/test2a");
1096 isf.DeleteDirectory ("dir1/test/test2b");
1097 isf.DeleteDirectory ("dir1/test");
1098 isf.DeleteDirectory ("dir1");
1102 public void RootedDirectory ()
1104 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1106 isf.CreateDirectory ("test/nested/directory/structure/without/root");
1107 isf.CreateDirectory ("/test/nested/directory/structure/with/root");
1110 isf.DeleteDirectory ("test/nested/directory/structure/without/root");
1111 isf.DeleteDirectory ("test/nested/directory/structure/without");
1113 isf.DeleteDirectory ("/test/nested/directory/structure/with/root");
1114 isf.DeleteDirectory ("/test/nested/directory/structure/with");
1115 isf.DeleteDirectory ("/test/nested/directory/structure");
1116 isf.DeleteDirectory ("/test/nested/directory");
1117 isf.DeleteDirectory ("/test/nested");
1118 isf.DeleteDirectory ("/test");