2 // IsolatedStorageFileTest.cs
3 // - Unit Tests for abstract IsolatedStorageFile class
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell Inc. (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
33 using System.IO.IsolatedStorage;
34 using System.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Policy;
39 using NUnit.Framework;
41 namespace MonoTests.System.IO.IsolatedStorageTest {
44 public class IsolatedStorageFileTest {
46 private const string dirname = "mono-unit-test";
48 private void CheckEnumerated (int n, IsolatedStorageScope scope, IsolatedStorageFile isf)
50 string prefix = n.ToString () + " - " + scope.ToString () + " - ";
51 Assert.IsNotNull (isf, prefix + "IsolatedStorageFile");
52 Assert.IsTrue (((scope & isf.Scope) != 0), prefix + "Scope");
54 if ((isf.Scope & IsolatedStorageScope.Assembly) != 0)
55 Assert.IsNotNull (isf.AssemblyIdentity, prefix + "AssemblyIdentity");
56 if ((isf.Scope & IsolatedStorageScope.Domain) != 0)
57 Assert.IsNotNull (isf.DomainIdentity, prefix + "DomainIdentity");
59 if ((isf.Scope & IsolatedStorageScope.Application) != 0)
60 Assert.IsNotNull (isf.ApplicationIdentity, prefix + "ApplicationIdentity");
64 private void GetEnumerator (IsolatedStorageScope scope)
66 IEnumerator e = IsolatedStorageFile.GetEnumerator (scope);
70 IsolatedStorageFile isf = (IsolatedStorageFile)e.Current;
71 CheckEnumerated (++n, scope, isf);
76 public void GetEnumerator_User ()
78 GetEnumerator (IsolatedStorageScope.User);
82 [ExpectedException (typeof (ArgumentException))]
83 public void GetEnumerator_User_Details ()
85 // giving more details is bad
86 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain);
90 public void GetEnumerator_UserRoaming ()
92 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Roaming);
96 [ExpectedException (typeof (ArgumentException))]
97 public void GetEnumerator_UserRoaming_Details ()
99 // giving more details is bad
100 GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain | IsolatedStorageScope.Roaming);
104 public void GetEnumerator_Machine ()
106 GetEnumerator (IsolatedStorageScope.Machine);
110 [ExpectedException (typeof (ArgumentException))]
111 public void GetEnumerator_Machine_Details ()
113 GetEnumerator (IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly);
117 [ExpectedException (typeof (ArgumentException))]
118 public void GetEnumerator_Application ()
120 // we can't enum application
121 GetEnumerator (IsolatedStorageScope.Application);
125 public void GetUserStoreForAssembly ()
127 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
128 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
129 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
131 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
132 // note: mono transforms the CodeBase into uppercase
133 // for net 1.1 which uses file:// and not file:///
134 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
135 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
136 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
138 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
142 [ExpectedException (typeof (InvalidOperationException))]
143 public void GetUserStoreForAssembly_DomainIdentity ()
145 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
146 object o = isf.DomainIdentity;
151 [ExpectedException (typeof (InvalidOperationException))]
152 public void GetUserStoreForAssembly_ApplicationIdentity ()
154 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
155 object o = isf.ApplicationIdentity;
160 public void GetUserStoreForDomain ()
162 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
163 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
164 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
166 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
167 // note: mono transforms the CodeBase into uppercase
168 // for net 1.1 which uses file:// and not file:///
169 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
170 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
171 Assert.IsTrue ((isf.DomainIdentity is Url), "DomainIdentity");
172 // note: with MS Assembly.GetEntryAssembly () only works in the default (first) AppDomain
173 // so we're using the first parameter to GetCommandLineArgs
174 string exe = Environment.GetCommandLineArgs ()[0].Replace ("\\", "/").ToUpper ();
175 Assert.IsTrue ((isf.DomainIdentity.ToString ().ToUpper ().IndexOf (exe) > 0), exe + "\n" + isf.DomainIdentity.ToString ().ToUpper ()); //"Url - Domain");
177 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
182 [ExpectedException (typeof (InvalidOperationException))]
183 public void GetUserStoreForDomain_ApplicationIdentity ()
185 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
186 object o = isf.ApplicationIdentity;
190 [ExpectedException (typeof (IsolatedStorageException))]
191 public void GetUserStoreForApplication_WithoutApplicationIdentity ()
193 // note: a manifest is required
194 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
198 [ExpectedException (typeof (IsolatedStorageException))]
199 public void GetUserStoreForApplication ()
201 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
202 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
204 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
205 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
206 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf (Assembly.GetExecutingAssembly ().CodeBase) > 0), "Url");
208 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
213 [ExpectedException (typeof (IsolatedStorageException))]
214 public void GetUserStoreForApplication_AssemblyIdentity ()
216 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
217 object o = isf.AssemblyIdentity;
221 [ExpectedException (typeof (IsolatedStorageException))]
222 public void GetUserStoreForApplication_DomainIdentity ()
224 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
225 object o = isf.DomainIdentity;
231 // This is supposed to be working only in SL.
233 [ExpectedException (typeof (NotSupportedException))]
234 public void GetUserStoreForSite ()
236 IsolatedStorageFile.GetUserStoreForSite ();
241 public void GetStore_Domain_Zone ()
243 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
244 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
245 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
247 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
248 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
249 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
250 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
251 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
253 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
257 [ExpectedException (typeof (IsolatedStorageException))]
258 public void GetStore_Domain_NonPresentEvidences ()
260 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
261 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Publisher));
265 public void GetStore_Assembly_NonPresentDomainEvidences ()
267 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
268 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
269 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
270 Assert.AreEqual (scope, isf.Scope, "Scope");
272 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
273 // note: mono transforms the CodeBase into uppercase
274 // for net 1.1 which uses file:// and not file:///
275 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
276 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
277 // DomainIdentity throws a InvalidOperationException
279 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");
333 [ExpectedException (typeof (ArgumentNullException))]
334 public void GetStore_Application_NullObject ()
336 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
337 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null);
341 [ExpectedException (typeof (IsolatedStorageException))]
342 public void GetStore_Application_NullType ()
344 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
345 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (Type)null);
346 // again it's the lack of a manifest
351 public void GetStore_DomainScope_Evidences ()
353 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
355 Evidence de = new Evidence ();
356 de.AddHost (new Zone (SecurityZone.Internet));
357 Evidence ae = new Evidence ();
358 ae.AddHost (new Zone (SecurityZone.Intranet));
359 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), ae, typeof (Zone));
361 // Maximum size for Internet isn't (by default) Int64.MaxValue
362 Assert.AreEqual (scope, isf.Scope, "Scope");
364 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
365 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Intranet") > 0), "Zone - Assembly");
366 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
367 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), isf.DomainIdentity.ToString ()); //"Zone - Domain");
369 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
373 [ExpectedException (typeof (ArgumentNullException))]
374 public void GetStore_DomainScope_Evidence_NullAssemblyEvidence ()
376 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
378 Evidence de = new Evidence ();
379 de.AddHost (new Zone (SecurityZone.Internet));
380 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
384 [ExpectedException (typeof (ArgumentNullException))]
385 public void GetStore_DomainScope_Evidence_NullDomainEvidence ()
387 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
389 Evidence ae = new Evidence ();
390 ae.AddHost (new Zone (SecurityZone.Internet));
391 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
395 [ExpectedException (typeof (ArgumentNullException))]
396 public void GetStore_AssemblyScope_Evidence_NullAssemblyEvidence ()
398 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
400 Evidence de = new Evidence ();
401 de.AddHost (new Zone (SecurityZone.Internet));
402 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
406 public void GetStore_AssemblyScope_Evidence_NullDomainEvidence ()
408 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
410 Evidence ae = new Evidence ();
411 ae.AddHost (new Zone (SecurityZone.Internet));
412 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
416 public void RegressionBNC354539 ()
418 string filename = "test-bnc-354539";
419 byte[] expected = new byte[] { 0x01, 0x42, 0x00 };
420 byte[] actual = new byte [expected.Length];
422 using (IsolatedStorageFile file = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null)) {
423 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Create, FileAccess.Write, FileShare.None, file)) {
424 stream.Write (expected, 0, expected.Length);
428 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly ()) {
429 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read, file)) {
430 stream.Read (actual, 0, actual.Length);
433 file.DeleteFile (filename);
436 Assert.AreEqual (expected, actual);
440 [ExpectedException (typeof (ArgumentNullException))]
441 public void CreateDirectory_Null ()
443 IsolatedStorageFile.GetUserStoreForAssembly ().CreateDirectory (null);
447 public void CreateDirectory_FileWithSameNameExists ()
449 string path = "bug374377";
450 using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ()) {
451 using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream (path, FileMode.OpenOrCreate, isf)) {
454 isf.CreateDirectory (path);
456 #if NET_4_0 || NET_2_1
457 catch (IsolatedStorageException ex) {
458 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
459 Assert.IsNull (ex.InnerException, "InnerException");
462 catch (IOException ex) {
463 Assert.AreEqual (typeof (IOException), ex.GetType (), "Type");
464 // don't leak path information
465 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
466 Assert.IsNull (ex.InnerException, "InnerException");
473 public void CreateDirectory_DirectoryWithSameNameExists ()
475 string dir = "new-dir";
476 string file = Path.Combine (dir, "new-file");
477 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
479 isf.CreateDirectory (dir);
480 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (file, FileMode.OpenOrCreate, isf)) {
483 string pattern = Path.Combine (dir, "*");
484 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file exists");
486 // create again directory
487 isf.CreateDirectory (dir);
488 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file still exists");
491 isf.DeleteFile (file);
492 isf.DeleteDirectory (dir);
497 #if NET_4_0 || NET_2_1
498 [ExpectedException (typeof (ArgumentException))]
500 [ExpectedException (typeof (SecurityException))]
502 public void GetFilesInSubdirs ()
504 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
505 string pattern = Path.Combine ("..", "*");
506 isf.GetFileNames (pattern);
512 [ExpectedException (typeof (ArgumentException))]
513 public void GetDirsInSubDirs ()
515 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
516 isf.CreateDirectory ("subdir");
517 string [] dir_names = isf.GetDirectoryNames ("subdir/../*");
521 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=376188
522 public void CreateSubDirectory ()
524 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
525 isf.CreateDirectory ("subdir");
526 isf.CreateDirectory ("subdir/subdir2");
527 Assert.AreEqual (1, isf.GetDirectoryNames ("*").Length, "subdir");
528 Assert.AreEqual (1, isf.GetDirectoryNames ("subdir/*").Length, "subdir/subdir2");
529 isf.DeleteDirectory ("subdir/subdir2");
530 isf.DeleteDirectory ("subdir");
534 [ExpectedException (typeof (IsolatedStorageException))]
535 public void DeleteDirectory_NonEmpty ()
537 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
538 isf.CreateDirectory ("subdir");
539 isf.CreateDirectory ("subdir/subdir2");
540 isf.DeleteDirectory ("subdir");
544 public void DeleteFile ()
546 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
549 isf.DeleteFile (null);
550 Assert.Fail ("#Exc0");
551 } catch (ArgumentNullException) {
555 // We are getting an internal IndexOutOfRangeException in 2.0
556 // Not sure we want to mimic that one.
558 isf.DeleteFile (String.Empty);
559 Assert.Fail ("#Exc1");
560 } catch (IsolatedStorageException) {
565 isf.DeleteFile ("idontexist");
566 Assert.Fail ("#Exc2");
567 } catch (IsolatedStorageException) {
572 isf.DeleteFile ("../../file");
573 Assert.Fail ("#Exc3");
574 } catch (IsolatedStorageException) {
579 isf.DeleteFile ("subdir/file");
580 Assert.Fail ("#Exc4");
581 } catch (IsolatedStorageException) {
584 isf.CreateDirectory ("subdir");
586 isf.DeleteFile ("subdir");
587 Assert.Fail ("#Exc5");
588 } catch (IsolatedStorageException) {
593 public void GetStore_NullTypes ()
595 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Roaming | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain;
596 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null);
598 Assert.AreEqual (typeof (Url), isf.AssemblyIdentity.GetType (), "AssemblyIdentity");
599 Assert.AreEqual (typeof (Url), isf.DomainIdentity.GetType (), "DomainIdentity");
604 public void RemoveFromOtherInstance ()
606 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
607 IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForAssembly ();
612 Assert.Fail ("#Exc1");
613 } catch (IsolatedStorageException) {
619 public void Remove ()
621 // Test that we can call Remove several times
622 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
623 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
625 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
628 // The second call to Remove should cause an InvalidOperationException, due to
629 // marking itself as closed.
632 Assert.Fail ("#Exc1");
633 } catch (InvalidOperationException) {
636 // Open, Close and try to Remove
637 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
641 Assert.Fail ("#Exc2");
642 } catch (InvalidOperationException) {
647 public void UsedSize ()
649 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
650 IsolatedStorageFileStream isfs = isf.CreateFile ("file");
651 StreamWriter writer = new StreamWriter (isfs);
652 writer.WriteLine ("hello mono");
655 Assert.AreEqual (true, isf.UsedSize > 0, "#A0");
659 Console.WriteLine (isf.UsedSize);
660 Assert.Fail ("#Exc1");
661 } catch (InvalidOperationException) {
666 Console.WriteLine (isf.UsedSize);
667 Assert.Fail ("#Exc2");
668 } catch (ObjectDisposedException) {
673 public void IncreateQuotaTo ()
675 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
678 isf.IncreaseQuotaTo (-2);
679 Assert.Fail ("#Exc1");
680 } catch (ArgumentException) {
683 // I wonder how this behaves on some systems
685 isf.IncreaseQuotaTo (100);
686 Assert.Fail ("#Exc2");
687 } catch (ArgumentException) {
690 // Since 'Quota' seems to be returning Int64.MaxValue, we cannot truly test against a value
695 public void DirectoryExists ()
697 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
698 isf.CreateDirectory ("subdir");
699 isf.CreateDirectory ("subdir/subdir2");
700 isf.CreateDirectory ("subdir3");
702 Assert.AreEqual (true, isf.DirectoryExists ("subdir/"), "#A0");
703 Assert.AreEqual (true, isf.DirectoryExists ("subdir/subdir2/"), "#A1");
704 Assert.AreEqual (true, isf.DirectoryExists ("subdir3"), "#A2");
705 Assert.AreEqual (true, isf.DirectoryExists (String.Empty), "#A3"); // Weird
706 Assert.AreEqual (false, isf.DirectoryExists ("subdir99"), "#A4");
707 Assert.AreEqual (false, isf.DirectoryExists ("../../subdir"), "#A5");
708 Assert.AreEqual (false, isf.DirectoryExists ("*"), "#A5");
709 Assert.AreEqual (false, isf.DirectoryExists ("subdir*"), "#A6");
711 isf.DeleteDirectory ("subdir3");
712 Assert.AreEqual (false, isf.DirectoryExists ("subdir3"), "#B0");
714 isf.DeleteDirectory ("subdir/subdir2");
715 isf.DeleteDirectory ("subdir");
718 isf.DirectoryExists (null);
719 Assert.Fail ("#Exc1");
720 } catch (ArgumentNullException) {
725 isf.DirectoryExists ("subdir");
726 Assert.Fail ("#Exc2");
727 } catch (InvalidOperationException) {
732 isf.DirectoryExists ("subdir");
733 Assert.Fail ("#Exc3");
734 } catch (ObjectDisposedException) {
737 // We want to be sure that if not closing but disposing
738 // should fire ObjectDisposedException instead of InvalidOperationException
739 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
743 isf.DirectoryExists ("subdir");
744 Assert.Fail ("#Exc4");
745 } catch (ObjectDisposedException) {
750 public void FileExists ()
752 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
753 IsolatedStorageFileStream file_a = new IsolatedStorageFileStream ("file-a", FileMode.Create, isf);
754 IsolatedStorageFileStream file_b = new IsolatedStorageFileStream ("file-b", FileMode.Create, isf);
758 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
759 Assert.AreEqual (true, isf.FileExists ("file-b"), "#A1");
760 Assert.AreEqual (false, isf.FileExists (String.Empty), "#A2");
761 Assert.AreEqual (false, isf.FileExists ("file-"), "#A3");
762 Assert.AreEqual (false, isf.FileExists ("file-*"), "#A4");
763 Assert.AreEqual (false, isf.FileExists ("../../file-a"), "#A5");
765 isf.CreateDirectory ("subdir");
766 Assert.AreEqual (false, isf.FileExists ("subdir"), "#B0");
769 isf.FileExists (null);
770 Assert.Fail ("#Exc1");
771 } catch (ArgumentNullException) {
776 isf.FileExists ("file-a");
777 Assert.Fail ("#Exc2");
778 } catch (InvalidOperationException) {
783 isf.FileExists ("file-a");
784 Assert.Fail ("#Exc3");
785 } catch (ObjectDisposedException) {
790 public void CreateFile ()
792 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
793 // Make sure we are actually creating it, by first removing it in case it already exists
794 if (isf.FileExists ("file-a"))
795 isf.DeleteFile ("file-a");
797 IsolatedStorageFileStream isf_stream = isf.CreateFile ("file-a");
799 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
801 // Re-open the file that is already created, so we make sure we are passing
802 // the proper FileOpen
803 isf_stream = isf.CreateFile ("file-a");
807 isf.CreateFile (null);
808 Assert.Fail ("#Exc1");
809 } catch (ArgumentNullException) {
813 isf.CreateFile ("random-dir/fileb");
814 Assert.Fail ("#Exc2");
815 } catch (DirectoryNotFoundException) {
820 isf.CreateFile ("file-b");
821 Assert.Fail ("#Exc3");
822 } catch (InvalidOperationException) {
827 isf.CreateFile ("file-a");
828 Assert.Fail ("#Exc4");
829 } catch (ObjectDisposedException) {
834 public void GetCreationTime ()
836 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
838 // This is not causing an exception
839 isf.GetCreationTime ("doesntexist");
840 isf.GetCreationTime ("dir/doesntexist");
843 isf.GetCreationTime (String.Empty);
844 Assert.Fail ("#Exc1");
845 } catch (ArgumentException) {
849 isf.GetCreationTime (" ");
850 Assert.Fail ("#Exc2");
851 } catch (ArgumentException) {
856 isf.GetCreationTime ("doesntexist");
857 Assert.Fail ("#Exc3");
858 } catch (InvalidOperationException) {
863 isf.GetCreationTime ("doesntexist");
864 Assert.Fail ("#Exc4");
865 } catch (ObjectDisposedException) {
870 public void MoveDirectory ()
872 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
873 // Mare sure to remove them if they exist already
874 if (isf.DirectoryExists ("subdir"))
875 isf.DeleteDirectory ("subdir");
876 if (isf.DirectoryExists ("subdir-new"))
877 isf.DeleteDirectory ("subdir-new");
879 isf.CreateDirectory ("subdir");
880 Assert.AreEqual (true, isf.DirectoryExists ("subdir"), "#A0");
882 isf.MoveDirectory ("subdir", "subdir-new");
883 Assert.AreEqual (false, isf.DirectoryExists ("subdir"), "#A1");
884 Assert.AreEqual (true, isf.DirectoryExists ("subdir-new"), "#A2");
887 isf.MoveDirectory (String.Empty, "subdir-new-new");
888 Assert.Fail ("#Exc1");
889 } catch (ArgumentException) {
893 isf.MoveDirectory (" ", "subdir-new-new");
894 Assert.Fail ("#Exc2");
895 } catch (ArgumentException) {
899 isf.MoveDirectory ("doesntexist", "subdir-new-new");
900 Assert.Fail ("#Exc3");
901 } catch (DirectoryNotFoundException) {
905 isf.MoveDirectory ("doesnexist/doesntexist", "subdir-new-new");
906 Assert.Fail ("#Exc4");
907 } catch (DirectoryNotFoundException) {
911 isf.MoveDirectory ("subdir-new", "doesntexist/doesntexist");
912 Assert.Fail ("#Exc5");
913 } catch (DirectoryNotFoundException) {
916 // Out of storage dir
918 isf.MoveDirectory ("subdir-new", "../../subdir-new");
919 Assert.Fail ("#Exc6");
920 } catch (IsolatedStorageException) {
929 public void CopyFile ()
931 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
932 if (isf.FileExists ("file"))
933 isf.DeleteFile ("file");
934 if (isf.FileExists ("file-new"))
935 isf.DeleteFile ("file-new");
937 isf.CreateFile ("file").Close ();
938 isf.CopyFile ("file", "file-new");
939 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
940 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A1");
942 // At this point 'file-exists' already exists.
943 isf.CopyFile ("file", "file-new", true);
944 Assert.AreEqual (true, isf.FileExists ("file"), "#B0");
945 Assert.AreEqual (true, isf.FileExists ("file-new"), "#B1");
947 isf.CreateDirectory ("subdir");
948 isf.CreateFile ("subdir/subfile").Close ();
949 isf.CopyFile ("subdir/subfile", "subdir/subfile-new");
950 Assert.AreEqual (true, isf.FileExists ("subdir/subfile"), "#C0");
951 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#C1");
954 isf.CopyFile ("file", "file-new");
955 Assert.Fail ("#Exc0");
956 } catch (IsolatedStorageException) {
959 // Using the same file name is failing for even when passing override=true.
961 isf.CopyFile ("file-new", "file-new", true);
962 Assert.Fail ("#Exc1");
963 } catch (IsolatedStorageException) {
967 isf.CopyFile ("file-new", "file-new", false);
968 Assert.Fail ("#Exc2");
969 } catch (IsolatedStorageException) {
972 // Remove 'file-new' for cleaness purposes.
973 isf.DeleteFile ("file-new");
976 isf.CopyFile ("doesntexist", "file-new", false);
977 Assert.Fail ("#Exc3");
978 } catch (FileNotFoundException) {
982 isf.CopyFile ("doesnexist/doesntexist", "file-new", false);
983 Assert.Fail ("#Exc4");
984 } catch (DirectoryNotFoundException) {
987 // I'd have expected a DirectoryNotFoundException here.
989 isf.CopyFile ("file", "doesntexist/doesntexist");
990 Assert.Fail ("#Exc5");
991 } catch (IsolatedStorageException) {
994 // Out of storage dir
996 isf.CopyFile ("file", "../../file");
997 Assert.Fail ("#Exc6");
998 } catch (IsolatedStorageException) {
1002 isf.CopyFile ("../file", "file-new");
1003 Assert.Fail ("#Exc7");
1004 } catch (IsolatedStorageException) {
1007 // We are creating a subdirectory and files within it, so remove it just in case.
1015 public void MoveFile ()
1017 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1018 // Mare sure to remove them if they exist already
1019 if (isf.FileExists ("file"))
1020 isf.DeleteFile ("file");
1021 if (isf.FileExists ("file-new"))
1022 isf.DeleteFile ("file-new");
1023 if (isf.FileExists ("subdir/subfile"))
1024 isf.DeleteFile ("subdir/subfile");
1025 if (isf.FileExists ("subdir/subfile-new"))
1026 isf.DeleteFile ("subdir/subfile-new");
1028 isf.CreateFile ("file").Close ();
1029 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
1032 isf.MoveFile ("file", "file");
1033 Assert.AreEqual (true, isf.FileExists ("file"), "#A0-1");
1035 isf.MoveFile ("file", "file-new");
1036 Assert.AreEqual (false, isf.FileExists ("file"), "#A1");
1037 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A2");
1039 isf.CreateDirectory ("subdir");
1040 isf.CreateFile ("subdir/subfile").Close ();
1041 isf.MoveFile ("subdir/subfile", "subdir/subfile-new");
1042 Assert.AreEqual (false, isf.FileExists ("subdir/subfile"), "#B0");
1043 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#B1");
1046 isf.MoveFile (String.Empty, "file-new-new");
1047 Assert.Fail ("#Exc1");
1048 } catch (ArgumentException) {
1052 isf.MoveFile (" ", "file-new-new");
1053 Assert.Fail ("#Exc2");
1054 } catch (ArgumentException e) {
1055 Console.WriteLine (e);
1059 isf.MoveFile ("doesntexist", "file-new-new");
1060 Assert.Fail ("#Exc3");
1061 } catch (FileNotFoundException) {
1064 // CopyFile is throwing a DirectoryNotFoundException here.
1066 isf.MoveFile ("doesnexist/doesntexist", "file-new-new");
1067 Assert.Fail ("#Exc4");
1068 } catch (FileNotFoundException) {
1071 // I'd have expected a DirectoryNotFoundException here.
1073 isf.MoveFile ("file-new", "doesntexist/doesntexist");
1074 Assert.Fail ("#Exc5");
1075 } catch (IsolatedStorageException) {
1078 // Out of storage dir
1080 isf.MoveFile ("file-new", "../../file-new");
1081 Assert.Fail ("#Exc6");
1082 } catch (IsolatedStorageException) {