Merge pull request #1972 from esdrubal/proc_pidpath
[mono.git] / mcs / class / corlib / Test / System.IO.IsolatedStorage / IsolatedStorageFileTest.cs
index 57fa50345357c3f66e9c6e19dd39c5a2bcb760b2..66eb27ed32407833e14b69f9fc6cb357b4f3d7ab 100644 (file)
@@ -6,6 +6,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // Copyright (C) 2005 Novell Inc. (http://www.novell.com)
+// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -55,10 +56,8 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                                Assert.IsNotNull (isf.AssemblyIdentity, prefix + "AssemblyIdentity");
                        if ((isf.Scope & IsolatedStorageScope.Domain) != 0)
                                Assert.IsNotNull (isf.DomainIdentity, prefix + "DomainIdentity");
-#if NET_2_0
                        if ((isf.Scope & IsolatedStorageScope.Application) != 0)
                                Assert.IsNotNull (isf.ApplicationIdentity, prefix + "ApplicationIdentity");
-#endif
                }
 
                private void GetEnumerator (IsolatedStorageScope scope)
@@ -99,7 +98,6 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        // giving more details is bad
                        GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain | IsolatedStorageScope.Roaming);
                }
-#if NET_2_0
                [Test]
                public void GetEnumerator_Machine ()
                {
@@ -120,19 +118,20 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        // we can't enum application
                        GetEnumerator (IsolatedStorageScope.Application);
                }
-#endif
                [Test]
                public void GetUserStoreForAssembly ()
                {
                        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
                        Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
                        Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
+#if !NET_2_1
                        Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
                        // note: mono transforms the CodeBase into uppercase
                        // for net 1.1 which uses file:// and not file:///
                        string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
                        Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
                        Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
+#endif
                        Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
                }
 
@@ -144,7 +143,6 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        object o = isf.DomainIdentity;
                }
 
-#if NET_2_0
                [Test]
                [ExpectedException (typeof (InvalidOperationException))]
                public void GetUserStoreForAssembly_ApplicationIdentity ()
@@ -152,7 +150,6 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
                        object o = isf.ApplicationIdentity;
                }
-#endif
 
                [Test]
                public void GetUserStoreForDomain ()
@@ -160,6 +157,7 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
                        Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
                        Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
+#if !NET_2_1
                        Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
                        // note: mono transforms the CodeBase into uppercase
                        // for net 1.1 which uses file:// and not file:///
@@ -170,10 +168,10 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        // so we're using the first parameter to GetCommandLineArgs
                        string exe = Environment.GetCommandLineArgs ()[0].Replace ("\\", "/").ToUpper ();
                        Assert.IsTrue ((isf.DomainIdentity.ToString ().ToUpper ().IndexOf (exe) > 0), exe + "\n" + isf.DomainIdentity.ToString ().ToUpper ()); //"Url - Domain");
+#endif
                        Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
                }
 
-#if NET_2_0
                [Test]
                [ExpectedException (typeof (InvalidOperationException))]
                public void GetUserStoreForDomain_ApplicationIdentity ()
@@ -183,7 +181,9 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                }
 
                [Test]
+#if !MOBILE
                [ExpectedException (typeof (IsolatedStorageException))]
+#endif
                public void GetUserStoreForApplication_WithoutApplicationIdentity ()
                {
                        // note: a manifest is required
@@ -191,17 +191,22 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                }
 
                [Test]
+#if !MOBILE
                [ExpectedException (typeof (IsolatedStorageException))]
+#endif
                public void GetUserStoreForApplication ()
                {
                        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
                        Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
+#if !NET_2_1
                        Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
                        Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
                        Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf (Assembly.GetExecutingAssembly ().CodeBase) > 0), "Url");
+#endif
                        Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
                }
-
+               
+#if !NET_2_1
                [Test]
                [ExpectedException (typeof (IsolatedStorageException))]
                public void GetUserStoreForApplication_AssemblyIdentity ()
@@ -219,22 +224,34 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                }
 #endif
 
+               // This is supposed to be working only in SL.
+               [Test]
+               [ExpectedException (typeof (NotSupportedException))]
+               public void GetUserStoreForSite ()
+               {
+                       IsolatedStorageFile.GetUserStoreForSite ();
+               }
+
                [Test]
                public void GetStore_Domain_Zone ()
                {
                        IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
                        IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
                        Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
+#if !NET_2_1
                        Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
                        Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
                        Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
                        Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
                        Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
+#endif
                        Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
                }
 
                [Test]
+#if !MOBILE
                [ExpectedException (typeof (IsolatedStorageException))]
+#endif
                public void GetStore_Domain_NonPresentEvidences ()
                {
                        IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
@@ -248,15 +265,18 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
                        Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
                        Assert.AreEqual (scope, isf.Scope, "Scope");
+#if !NET_2_1
                        Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
                        // note: mono transforms the CodeBase into uppercase
                        // for net 1.1 which uses file:// and not file:///
                        string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
                        Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
                        // DomainIdentity throws a InvalidOperationException
+#endif
                        Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
                }
 
+#if !MOBILE
                [Test]
                [ExpectedException (typeof (ArgumentNullException))]
                public void GetStore_Domain_DomainNullObject ()
@@ -306,7 +326,8 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Domain");
                        Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
                }
-#if NET_2_0
+#endif
+
                [Test]
                [ExpectedException (typeof (ArgumentNullException))]
                public void GetStore_Application_NullObject ()
@@ -316,15 +337,17 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                }
 
                [Test]
+#if !MOBILE
                [ExpectedException (typeof (IsolatedStorageException))]
+#endif
                public void GetStore_Application_NullType ()
                {
                        IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
                        IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (Type)null);
                        // again it's the lack of a manifest
                }
-#endif
 
+#if !MOBILE
                [Test]
                public void GetStore_DomainScope_Evidences ()
                {
@@ -338,10 +361,12 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
 
                        // Maximum size for Internet isn't (by default) Int64.MaxValue
                        Assert.AreEqual (scope, isf.Scope, "Scope");
+#if !NET_2_1
                        Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
                        Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Intranet") > 0), "Zone - Assembly");
                        Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
                        Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), isf.DomainIdentity.ToString ()); //"Zone - Domain");
+#endif
                        Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
                }
 
@@ -387,6 +412,7 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        ae.AddHost (new Zone (SecurityZone.Internet));
                        IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
                }
+#endif
 
                [Test]
                public void RegressionBNC354539 ()
@@ -429,19 +455,10 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                                try {
                                        isf.CreateDirectory (path);
                                }
-#if NET_4_0
                                catch (IsolatedStorageException ex) {
                                        Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
                                        Assert.IsNull (ex.InnerException, "InnerException");
                                }
-#else
-                               catch (IOException ex) {
-                                       Assert.AreEqual (typeof (IOException), ex.GetType (), "Type");
-                                       // don't leak path information
-                                       Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
-                                       Assert.IsNull (ex.InnerException, "InnerException");
-                               }
-#endif
                        }
                }
 
@@ -470,11 +487,7 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                }
 
                [Test]
-#if NET_4_0
                [ExpectedException (typeof (ArgumentException))]
-#else
-               [ExpectedException (typeof (SecurityException))]
-#endif
                public void GetFilesInSubdirs ()
                {
                        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
@@ -483,7 +496,6 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                }
 
         
-#if NET_4_0
                [Test]
                [ExpectedException (typeof (ArgumentException))]
                public void GetDirsInSubDirs ()
@@ -492,7 +504,6 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        isf.CreateDirectory ("subdir");
                        string [] dir_names = isf.GetDirectoryNames ("subdir/../*");
                }
-#endif
 
                [Test] // https://bugzilla.novell.com/show_bug.cgi?id=376188
                public void CreateSubDirectory ()
@@ -516,16 +527,76 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        isf.DeleteDirectory ("subdir");
                }
 
+               [Test]
+               public void DeleteFile ()
+               {
+                       IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
+
+                       try {
+                               isf.DeleteFile (null);
+                               Assert.Fail ("#Exc0");
+                       } catch (ArgumentNullException) {
+                       }
+
+                       // We are getting an internal IndexOutOfRangeException in 2.0
+                       // Not sure we want to mimic that one.
+                       try {
+                               isf.DeleteFile (String.Empty);
+                               Assert.Fail ("#Exc1");
+                       } catch (IsolatedStorageException) {
+                       }
+
+                       try {
+                               isf.DeleteFile ("idontexist");
+                               Assert.Fail ("#Exc2");
+                       } catch (IsolatedStorageException) {
+                       }
+
+                       try {
+                               isf.DeleteFile ("../../file");
+                               Assert.Fail ("#Exc3");
+                       } catch (IsolatedStorageException) {
+                       }
+               
+                       try {
+                               isf.DeleteFile ("subdir/file");
+                               Assert.Fail ("#Exc4");
+                       } catch (IsolatedStorageException) {
+                       }
+
+                       isf.CreateDirectory ("subdir");
+                       try {
+                               isf.DeleteFile ("subdir");
+                               Assert.Fail ("#Exc5");
+                       } catch (IsolatedStorageException) {
+                       }
+               }
+
                [Test]
                public void GetStore_NullTypes ()
                {
                        IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Roaming | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain;
                        IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null);
+#if !NET_2_1
                        Assert.AreEqual (typeof (Url), isf.AssemblyIdentity.GetType (), "AssemblyIdentity");
                        Assert.AreEqual (typeof (Url), isf.DomainIdentity.GetType (), "DomainIdentity");
+#endif
+               }
+
+               [Test]
+               public void RemoveFromOtherInstance ()
+               {
+                       IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
+                       IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForAssembly ();
+
+                       isf.Remove ();
+                       try {
+                               isf2.Remove ();
+                               Assert.Fail ("#Exc1");
+                       } catch (IsolatedStorageException) {
+                       }
                }
 
-#if NET_4_0
                [Test]
                public void Remove ()
                {
@@ -963,7 +1034,6 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                                isf.MoveFile ("  ", "file-new-new");
                                Assert.Fail ("#Exc2");
                        } catch (ArgumentException e) {
-                               Console.WriteLine (e);
                        }
 
                        try {
@@ -997,6 +1067,56 @@ namespace MonoTests.System.IO.IsolatedStorageTest {
                        isf.Close ();
                        isf.Dispose ();
                }
-#endif
+
+               [Test]
+               public void MultiLevel ()
+               {
+                       // see bug #4101
+                       IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
+                       try {
+                               isf.CreateDirectory ("dir1");
+                               string [] dirs = isf.GetDirectoryNames ("*");
+                               Assert.AreEqual (dirs.Length, 1, "1a");
+                               Assert.AreEqual (dirs [0], "dir1", "1b");
+       
+                               isf.CreateDirectory ("dir1/test");
+                               dirs = isf.GetDirectoryNames ("dir1/*");
+                               Assert.AreEqual (dirs.Length, 1, "2a");
+                               Assert.AreEqual (dirs [0], "test", "2b");
+       
+                               isf.CreateDirectory ("dir1/test/test2a");
+                               isf.CreateDirectory ("dir1/test/test2b");
+                               dirs = isf.GetDirectoryNames ("dir1/test/*");
+                               Assert.AreEqual (dirs.Length, 2, "3a");
+                               Assert.AreEqual (dirs [0], "test2a", "3b");
+                               Assert.AreEqual (dirs [1], "test2b", "3c");
+                       }
+                       finally {
+                               isf.DeleteDirectory ("dir1/test/test2a");
+                               isf.DeleteDirectory ("dir1/test/test2b");
+                               isf.DeleteDirectory ("dir1/test");
+                               isf.DeleteDirectory ("dir1");
+                       }
+               }
+               [Test]
+               public void RootedDirectory ()
+               {
+                       IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
+                       try {
+                               isf.CreateDirectory ("test/nested/directory/structure/without/root");
+                               isf.CreateDirectory ("/test/nested/directory/structure/with/root");
+                       }
+                       finally {
+                               isf.DeleteDirectory ("test/nested/directory/structure/without/root");
+                               isf.DeleteDirectory ("test/nested/directory/structure/without");
+
+                               isf.DeleteDirectory ("/test/nested/directory/structure/with/root");
+                               isf.DeleteDirectory ("/test/nested/directory/structure/with");
+                               isf.DeleteDirectory ("/test/nested/directory/structure");
+                               isf.DeleteDirectory ("/test/nested/directory");
+                               isf.DeleteDirectory ("/test/nested");
+                               isf.DeleteDirectory ("/test");
+                       }
+               }
        }
 }