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;
191 [ExpectedException (typeof (IsolatedStorageException))]
192 public void GetUserStoreForApplication_WithoutApplicationIdentity ()
194 // note: a manifest is required
195 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
199 [ExpectedException (typeof (IsolatedStorageException))]
200 public void GetUserStoreForApplication ()
202 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
203 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
205 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
206 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
207 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf (Assembly.GetExecutingAssembly ().CodeBase) > 0), "Url");
209 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
214 [ExpectedException (typeof (IsolatedStorageException))]
215 public void GetUserStoreForApplication_AssemblyIdentity ()
217 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
218 object o = isf.AssemblyIdentity;
222 [ExpectedException (typeof (IsolatedStorageException))]
223 public void GetUserStoreForApplication_DomainIdentity ()
225 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
226 object o = isf.DomainIdentity;
232 // This is supposed to be working only in SL.
234 [ExpectedException (typeof (NotSupportedException))]
235 public void GetUserStoreForSite ()
237 IsolatedStorageFile.GetUserStoreForSite ();
242 public void GetStore_Domain_Zone ()
244 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
245 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
246 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
248 Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
249 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
250 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
251 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
252 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
254 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
258 [ExpectedException (typeof (IsolatedStorageException))]
259 public void GetStore_Domain_NonPresentEvidences ()
261 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
262 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Publisher));
266 public void GetStore_Assembly_NonPresentDomainEvidences ()
268 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
269 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
270 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
271 Assert.AreEqual (scope, isf.Scope, "Scope");
273 Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
274 // note: mono transforms the CodeBase into uppercase
275 // for net 1.1 which uses file:// and not file:///
276 string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
277 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
278 // DomainIdentity throws a InvalidOperationException
280 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
284 [ExpectedException (typeof (ArgumentNullException))]
285 public void GetStore_Domain_DomainNullObject ()
287 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
288 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.MyComputer));
292 [ExpectedException (typeof (ArgumentNullException))]
293 public void GetStore_Domain_AssemblyNullObject ()
295 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
296 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
300 public void GetStore_Assembly_DomainNullObject ()
302 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
303 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.Internet));
304 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
305 Assert.AreEqual (scope, isf.Scope, "Scope");
306 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
307 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
308 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
312 [ExpectedException (typeof (ArgumentNullException))]
313 public void GetStore_Assembly_AssemblyNullObject ()
315 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
316 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
320 public void GetStore_Domain_ZoneObjectZoneObject ()
322 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
323 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.Internet), new Zone (SecurityZone.Internet));
324 Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
325 Assert.AreEqual (scope, isf.Scope, "Scope");
326 Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
327 Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
328 Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
329 Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Domain");
330 Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
334 [ExpectedException (typeof (ArgumentNullException))]
335 public void GetStore_Application_NullObject ()
337 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
338 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null);
342 [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));
417 public void RegressionBNC354539 ()
419 string filename = "test-bnc-354539";
420 byte[] expected = new byte[] { 0x01, 0x42, 0x00 };
421 byte[] actual = new byte [expected.Length];
423 using (IsolatedStorageFile file = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null)) {
424 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Create, FileAccess.Write, FileShare.None, file)) {
425 stream.Write (expected, 0, expected.Length);
429 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly ()) {
430 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read, file)) {
431 stream.Read (actual, 0, actual.Length);
434 file.DeleteFile (filename);
437 Assert.AreEqual (expected, actual);
441 [ExpectedException (typeof (ArgumentNullException))]
442 public void CreateDirectory_Null ()
444 IsolatedStorageFile.GetUserStoreForAssembly ().CreateDirectory (null);
448 public void CreateDirectory_FileWithSameNameExists ()
450 string path = "bug374377";
451 using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ()) {
452 using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream (path, FileMode.OpenOrCreate, isf)) {
455 isf.CreateDirectory (path);
457 #if NET_4_0 || NET_2_1
458 catch (IsolatedStorageException ex) {
459 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
460 Assert.IsNull (ex.InnerException, "InnerException");
463 catch (IOException ex) {
464 Assert.AreEqual (typeof (IOException), ex.GetType (), "Type");
465 // don't leak path information
466 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
467 Assert.IsNull (ex.InnerException, "InnerException");
474 public void CreateDirectory_DirectoryWithSameNameExists ()
476 string dir = "new-dir";
477 string file = Path.Combine (dir, "new-file");
478 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
480 isf.CreateDirectory (dir);
481 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (file, FileMode.OpenOrCreate, isf)) {
484 string pattern = Path.Combine (dir, "*");
485 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file exists");
487 // create again directory
488 isf.CreateDirectory (dir);
489 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file still exists");
492 isf.DeleteFile (file);
493 isf.DeleteDirectory (dir);
498 #if NET_4_0 || NET_2_1
499 [ExpectedException (typeof (ArgumentException))]
501 [ExpectedException (typeof (SecurityException))]
503 public void GetFilesInSubdirs ()
505 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
506 string pattern = Path.Combine ("..", "*");
507 isf.GetFileNames (pattern);
513 [ExpectedException (typeof (ArgumentException))]
514 public void GetDirsInSubDirs ()
516 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
517 isf.CreateDirectory ("subdir");
518 string [] dir_names = isf.GetDirectoryNames ("subdir/../*");
522 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=376188
523 public void CreateSubDirectory ()
525 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
526 isf.CreateDirectory ("subdir");
527 isf.CreateDirectory ("subdir/subdir2");
528 Assert.AreEqual (1, isf.GetDirectoryNames ("*").Length, "subdir");
529 Assert.AreEqual (1, isf.GetDirectoryNames ("subdir/*").Length, "subdir/subdir2");
530 isf.DeleteDirectory ("subdir/subdir2");
531 isf.DeleteDirectory ("subdir");
535 [ExpectedException (typeof (IsolatedStorageException))]
536 public void DeleteDirectory_NonEmpty ()
538 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
539 isf.CreateDirectory ("subdir");
540 isf.CreateDirectory ("subdir/subdir2");
541 isf.DeleteDirectory ("subdir");
545 public void DeleteFile ()
547 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
550 isf.DeleteFile (null);
551 Assert.Fail ("#Exc0");
552 } catch (ArgumentNullException) {
556 // We are getting an internal IndexOutOfRangeException in 2.0
557 // Not sure we want to mimic that one.
559 isf.DeleteFile (String.Empty);
560 Assert.Fail ("#Exc1");
561 } catch (IsolatedStorageException) {
566 isf.DeleteFile ("idontexist");
567 Assert.Fail ("#Exc2");
568 } catch (IsolatedStorageException) {
573 isf.DeleteFile ("../../file");
574 Assert.Fail ("#Exc3");
575 } catch (IsolatedStorageException) {
580 isf.DeleteFile ("subdir/file");
581 Assert.Fail ("#Exc4");
582 } catch (IsolatedStorageException) {
585 isf.CreateDirectory ("subdir");
587 isf.DeleteFile ("subdir");
588 Assert.Fail ("#Exc5");
589 } catch (IsolatedStorageException) {
594 public void GetStore_NullTypes ()
596 IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Roaming | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain;
597 IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null);
599 Assert.AreEqual (typeof (Url), isf.AssemblyIdentity.GetType (), "AssemblyIdentity");
600 Assert.AreEqual (typeof (Url), isf.DomainIdentity.GetType (), "DomainIdentity");
605 public void RemoveFromOtherInstance ()
607 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
608 IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForAssembly ();
613 Assert.Fail ("#Exc1");
614 } catch (IsolatedStorageException) {
620 public void Remove ()
622 // Test that we can call Remove several times
623 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
624 IsolatedStorageFile.Remove (IsolatedStorageScope.User);
626 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
629 // The second call to Remove should cause an InvalidOperationException, due to
630 // marking itself as closed.
633 Assert.Fail ("#Exc1");
634 } catch (InvalidOperationException) {
637 // Open, Close and try to Remove
638 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
642 Assert.Fail ("#Exc2");
643 } catch (InvalidOperationException) {
648 public void UsedSize ()
650 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
651 IsolatedStorageFileStream isfs = isf.CreateFile ("file");
652 StreamWriter writer = new StreamWriter (isfs);
653 writer.WriteLine ("hello mono");
656 Assert.AreEqual (true, isf.UsedSize > 0, "#A0");
660 Console.WriteLine (isf.UsedSize);
661 Assert.Fail ("#Exc1");
662 } catch (InvalidOperationException) {
667 Console.WriteLine (isf.UsedSize);
668 Assert.Fail ("#Exc2");
669 } catch (ObjectDisposedException) {
674 public void IncreateQuotaTo ()
676 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
679 isf.IncreaseQuotaTo (-2);
680 Assert.Fail ("#Exc1");
681 } catch (ArgumentException) {
684 // I wonder how this behaves on some systems
686 isf.IncreaseQuotaTo (100);
687 Assert.Fail ("#Exc2");
688 } catch (ArgumentException) {
691 // Since 'Quota' seems to be returning Int64.MaxValue, we cannot truly test against a value
696 public void DirectoryExists ()
698 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
699 isf.CreateDirectory ("subdir");
700 isf.CreateDirectory ("subdir/subdir2");
701 isf.CreateDirectory ("subdir3");
703 Assert.AreEqual (true, isf.DirectoryExists ("subdir/"), "#A0");
704 Assert.AreEqual (true, isf.DirectoryExists ("subdir/subdir2/"), "#A1");
705 Assert.AreEqual (true, isf.DirectoryExists ("subdir3"), "#A2");
706 Assert.AreEqual (true, isf.DirectoryExists (String.Empty), "#A3"); // Weird
707 Assert.AreEqual (false, isf.DirectoryExists ("subdir99"), "#A4");
708 Assert.AreEqual (false, isf.DirectoryExists ("../../subdir"), "#A5");
709 Assert.AreEqual (false, isf.DirectoryExists ("*"), "#A5");
710 Assert.AreEqual (false, isf.DirectoryExists ("subdir*"), "#A6");
712 isf.DeleteDirectory ("subdir3");
713 Assert.AreEqual (false, isf.DirectoryExists ("subdir3"), "#B0");
715 isf.DeleteDirectory ("subdir/subdir2");
716 isf.DeleteDirectory ("subdir");
719 isf.DirectoryExists (null);
720 Assert.Fail ("#Exc1");
721 } catch (ArgumentNullException) {
726 isf.DirectoryExists ("subdir");
727 Assert.Fail ("#Exc2");
728 } catch (InvalidOperationException) {
733 isf.DirectoryExists ("subdir");
734 Assert.Fail ("#Exc3");
735 } catch (ObjectDisposedException) {
738 // We want to be sure that if not closing but disposing
739 // should fire ObjectDisposedException instead of InvalidOperationException
740 isf = IsolatedStorageFile.GetUserStoreForAssembly ();
744 isf.DirectoryExists ("subdir");
745 Assert.Fail ("#Exc4");
746 } catch (ObjectDisposedException) {
751 public void FileExists ()
753 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
754 IsolatedStorageFileStream file_a = new IsolatedStorageFileStream ("file-a", FileMode.Create, isf);
755 IsolatedStorageFileStream file_b = new IsolatedStorageFileStream ("file-b", FileMode.Create, isf);
759 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
760 Assert.AreEqual (true, isf.FileExists ("file-b"), "#A1");
761 Assert.AreEqual (false, isf.FileExists (String.Empty), "#A2");
762 Assert.AreEqual (false, isf.FileExists ("file-"), "#A3");
763 Assert.AreEqual (false, isf.FileExists ("file-*"), "#A4");
764 Assert.AreEqual (false, isf.FileExists ("../../file-a"), "#A5");
766 isf.CreateDirectory ("subdir");
767 Assert.AreEqual (false, isf.FileExists ("subdir"), "#B0");
770 isf.FileExists (null);
771 Assert.Fail ("#Exc1");
772 } catch (ArgumentNullException) {
777 isf.FileExists ("file-a");
778 Assert.Fail ("#Exc2");
779 } catch (InvalidOperationException) {
784 isf.FileExists ("file-a");
785 Assert.Fail ("#Exc3");
786 } catch (ObjectDisposedException) {
791 public void CreateFile ()
793 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
794 // Make sure we are actually creating it, by first removing it in case it already exists
795 if (isf.FileExists ("file-a"))
796 isf.DeleteFile ("file-a");
798 IsolatedStorageFileStream isf_stream = isf.CreateFile ("file-a");
800 Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
802 // Re-open the file that is already created, so we make sure we are passing
803 // the proper FileOpen
804 isf_stream = isf.CreateFile ("file-a");
808 isf.CreateFile (null);
809 Assert.Fail ("#Exc1");
810 } catch (ArgumentNullException) {
814 isf.CreateFile ("random-dir/fileb");
815 Assert.Fail ("#Exc2");
816 } catch (DirectoryNotFoundException) {
821 isf.CreateFile ("file-b");
822 Assert.Fail ("#Exc3");
823 } catch (InvalidOperationException) {
828 isf.CreateFile ("file-a");
829 Assert.Fail ("#Exc4");
830 } catch (ObjectDisposedException) {
835 public void GetCreationTime ()
837 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
839 // This is not causing an exception
840 isf.GetCreationTime ("doesntexist");
841 isf.GetCreationTime ("dir/doesntexist");
844 isf.GetCreationTime (String.Empty);
845 Assert.Fail ("#Exc1");
846 } catch (ArgumentException) {
850 isf.GetCreationTime (" ");
851 Assert.Fail ("#Exc2");
852 } catch (ArgumentException) {
857 isf.GetCreationTime ("doesntexist");
858 Assert.Fail ("#Exc3");
859 } catch (InvalidOperationException) {
864 isf.GetCreationTime ("doesntexist");
865 Assert.Fail ("#Exc4");
866 } catch (ObjectDisposedException) {
871 public void MoveDirectory ()
873 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
874 // Mare sure to remove them if they exist already
875 if (isf.DirectoryExists ("subdir"))
876 isf.DeleteDirectory ("subdir");
877 if (isf.DirectoryExists ("subdir-new"))
878 isf.DeleteDirectory ("subdir-new");
880 isf.CreateDirectory ("subdir");
881 Assert.AreEqual (true, isf.DirectoryExists ("subdir"), "#A0");
883 isf.MoveDirectory ("subdir", "subdir-new");
884 Assert.AreEqual (false, isf.DirectoryExists ("subdir"), "#A1");
885 Assert.AreEqual (true, isf.DirectoryExists ("subdir-new"), "#A2");
888 isf.MoveDirectory (String.Empty, "subdir-new-new");
889 Assert.Fail ("#Exc1");
890 } catch (ArgumentException) {
894 isf.MoveDirectory (" ", "subdir-new-new");
895 Assert.Fail ("#Exc2");
896 } catch (ArgumentException) {
900 isf.MoveDirectory ("doesntexist", "subdir-new-new");
901 Assert.Fail ("#Exc3");
902 } catch (DirectoryNotFoundException) {
906 isf.MoveDirectory ("doesnexist/doesntexist", "subdir-new-new");
907 Assert.Fail ("#Exc4");
908 } catch (DirectoryNotFoundException) {
912 isf.MoveDirectory ("subdir-new", "doesntexist/doesntexist");
913 Assert.Fail ("#Exc5");
914 } catch (DirectoryNotFoundException) {
917 // Out of storage dir
919 isf.MoveDirectory ("subdir-new", "../../subdir-new");
920 Assert.Fail ("#Exc6");
921 } catch (IsolatedStorageException) {
930 public void CopyFile ()
932 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
933 if (isf.FileExists ("file"))
934 isf.DeleteFile ("file");
935 if (isf.FileExists ("file-new"))
936 isf.DeleteFile ("file-new");
938 isf.CreateFile ("file").Close ();
939 isf.CopyFile ("file", "file-new");
940 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
941 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A1");
943 // At this point 'file-exists' already exists.
944 isf.CopyFile ("file", "file-new", true);
945 Assert.AreEqual (true, isf.FileExists ("file"), "#B0");
946 Assert.AreEqual (true, isf.FileExists ("file-new"), "#B1");
948 isf.CreateDirectory ("subdir");
949 isf.CreateFile ("subdir/subfile").Close ();
950 isf.CopyFile ("subdir/subfile", "subdir/subfile-new");
951 Assert.AreEqual (true, isf.FileExists ("subdir/subfile"), "#C0");
952 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#C1");
955 isf.CopyFile ("file", "file-new");
956 Assert.Fail ("#Exc0");
957 } catch (IsolatedStorageException) {
960 // Using the same file name is failing for even when passing override=true.
962 isf.CopyFile ("file-new", "file-new", true);
963 Assert.Fail ("#Exc1");
964 } catch (IsolatedStorageException) {
968 isf.CopyFile ("file-new", "file-new", false);
969 Assert.Fail ("#Exc2");
970 } catch (IsolatedStorageException) {
973 // Remove 'file-new' for cleaness purposes.
974 isf.DeleteFile ("file-new");
977 isf.CopyFile ("doesntexist", "file-new", false);
978 Assert.Fail ("#Exc3");
979 } catch (FileNotFoundException) {
983 isf.CopyFile ("doesnexist/doesntexist", "file-new", false);
984 Assert.Fail ("#Exc4");
985 } catch (DirectoryNotFoundException) {
988 // I'd have expected a DirectoryNotFoundException here.
990 isf.CopyFile ("file", "doesntexist/doesntexist");
991 Assert.Fail ("#Exc5");
992 } catch (IsolatedStorageException) {
995 // Out of storage dir
997 isf.CopyFile ("file", "../../file");
998 Assert.Fail ("#Exc6");
999 } catch (IsolatedStorageException) {
1003 isf.CopyFile ("../file", "file-new");
1004 Assert.Fail ("#Exc7");
1005 } catch (IsolatedStorageException) {
1008 // We are creating a subdirectory and files within it, so remove it just in case.
1016 public void MoveFile ()
1018 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1019 // Mare sure to remove them if they exist already
1020 if (isf.FileExists ("file"))
1021 isf.DeleteFile ("file");
1022 if (isf.FileExists ("file-new"))
1023 isf.DeleteFile ("file-new");
1024 if (isf.FileExists ("subdir/subfile"))
1025 isf.DeleteFile ("subdir/subfile");
1026 if (isf.FileExists ("subdir/subfile-new"))
1027 isf.DeleteFile ("subdir/subfile-new");
1029 isf.CreateFile ("file").Close ();
1030 Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
1033 isf.MoveFile ("file", "file");
1034 Assert.AreEqual (true, isf.FileExists ("file"), "#A0-1");
1036 isf.MoveFile ("file", "file-new");
1037 Assert.AreEqual (false, isf.FileExists ("file"), "#A1");
1038 Assert.AreEqual (true, isf.FileExists ("file-new"), "#A2");
1040 isf.CreateDirectory ("subdir");
1041 isf.CreateFile ("subdir/subfile").Close ();
1042 isf.MoveFile ("subdir/subfile", "subdir/subfile-new");
1043 Assert.AreEqual (false, isf.FileExists ("subdir/subfile"), "#B0");
1044 Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#B1");
1047 isf.MoveFile (String.Empty, "file-new-new");
1048 Assert.Fail ("#Exc1");
1049 } catch (ArgumentException) {
1053 isf.MoveFile (" ", "file-new-new");
1054 Assert.Fail ("#Exc2");
1055 } catch (ArgumentException e) {
1056 Console.WriteLine (e);
1060 isf.MoveFile ("doesntexist", "file-new-new");
1061 Assert.Fail ("#Exc3");
1062 } catch (FileNotFoundException) {
1065 // CopyFile is throwing a DirectoryNotFoundException here.
1067 isf.MoveFile ("doesnexist/doesntexist", "file-new-new");
1068 Assert.Fail ("#Exc4");
1069 } catch (FileNotFoundException) {
1072 // I'd have expected a DirectoryNotFoundException here.
1074 isf.MoveFile ("file-new", "doesntexist/doesntexist");
1075 Assert.Fail ("#Exc5");
1076 } catch (IsolatedStorageException) {
1079 // Out of storage dir
1081 isf.MoveFile ("file-new", "../../file-new");
1082 Assert.Fail ("#Exc6");
1083 } catch (IsolatedStorageException) {
1092 public void MultiLevel ()
1095 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1097 isf.CreateDirectory ("dir1");
1098 string [] dirs = isf.GetDirectoryNames ("*");
1099 Assert.AreEqual (dirs.Length, 1, "1a");
1100 Assert.AreEqual (dirs [0], "dir1", "1b");
1102 isf.CreateDirectory ("dir1/test");
1103 dirs = isf.GetDirectoryNames ("dir1/*");
1104 Assert.AreEqual (dirs.Length, 1, "2a");
1105 Assert.AreEqual (dirs [0], "test", "2b");
1107 isf.CreateDirectory ("dir1/test/test2a");
1108 isf.CreateDirectory ("dir1/test/test2b");
1109 dirs = isf.GetDirectoryNames ("dir1/test/*");
1110 Assert.AreEqual (dirs.Length, 2, "3a");
1111 Assert.AreEqual (dirs [0], "test2a", "3b");
1112 Assert.AreEqual (dirs [1], "test2b", "3c");
1115 isf.DeleteDirectory ("dir1/test/test2a");
1116 isf.DeleteDirectory ("dir1/test/test2b");
1117 isf.DeleteDirectory ("dir1/test");
1118 isf.DeleteDirectory ("dir1");