[Tests] Fail with message in case symlink fails
[mono.git] / mcs / class / corlib / Test / System.IO / FileTest.cs
index 05c2241144d8fa6363f6babfe8bc32c8ca8acc6b..7e01ce544d7240a073f1ca6fbc32d2cf3cb54c71 100644 (file)
@@ -14,6 +14,8 @@ using System;
 using System.IO;
 using System.Globalization;
 using System.Threading;
+using System.Runtime.InteropServices;
+
 
 using NUnit.Framework;
 
@@ -530,6 +532,12 @@ namespace MonoTests.System.IO
                        }
                }
 
+               [Test]
+               public void Delete_NonExisting_NoException ()
+               {
+                       File.Delete (Path.Combine (Directory.GetDirectoryRoot (Directory.GetCurrentDirectory ()), "monononexistingfile.dat"));
+               }
+
                [Test]
                public void GetAttributes_Archive ()
                {
@@ -812,11 +820,7 @@ namespace MonoTests.System.IO
                                        Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
                                        Assert.IsNotNull (ex.Message, "#4");
-#if NET_2_0
                                        Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
-#else
-                                       Assert.IsTrue (ex.Message.IndexOf (destFile) != -1, "#5");
-#endif
                                }
                        } finally {
                                DeleteFile (sourceFile);
@@ -1292,7 +1296,6 @@ namespace MonoTests.System.IO
                        string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
                        DeleteFile (path);
 
-#if NET_2_0
                        DateTime time = File.GetCreationTime (path);
                        DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
                        Assert.AreEqual (expectedTime.Year, time.Year, "#1");
@@ -1301,18 +1304,6 @@ namespace MonoTests.System.IO
                        Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
                        Assert.AreEqual (expectedTime.Second, time.Second, "#5");
                        Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
-#else
-                       try {
-                               File.GetCreationTime (path);
-                               Assert.Fail ("#1");
-                       } catch (IOException ex) {
-                               // Could not find a part of the path "..."
-                               Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
-                               Assert.IsNull (ex.InnerException, "#3");
-                               Assert.IsNotNull (ex.Message, "#4");
-                               Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
-                       }
-#endif
                }
 
                [Test]
@@ -1380,7 +1371,6 @@ namespace MonoTests.System.IO
                        string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
                        DeleteFile (path);
 
-#if NET_2_0
                        DateTime time = File.GetCreationTimeUtc (path);
                        Assert.AreEqual (1601, time.Year, "#1");
                        Assert.AreEqual (1, time.Month, "#2");
@@ -1388,18 +1378,6 @@ namespace MonoTests.System.IO
                        Assert.AreEqual (0, time.Hour, "#4");
                        Assert.AreEqual (0, time.Second, "#5");
                        Assert.AreEqual (0, time.Millisecond, "#6");
-#else
-                       try {
-                               File.GetCreationTimeUtc (path);
-                               Assert.Fail ("#1");
-                       } catch (IOException ex) {
-                               // Could not find a part of the path "..."
-                               Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
-                               Assert.IsNull (ex.InnerException, "#3");
-                               Assert.IsNotNull (ex.Message, "#4");
-                               Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
-                       }
-#endif
                }
 
                [Test]
@@ -1467,7 +1445,6 @@ namespace MonoTests.System.IO
                        string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
                        DeleteFile (path);
 
-#if NET_2_0
                        DateTime time = File.GetLastAccessTime (path);
                        DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
                        Assert.AreEqual (expectedTime.Year, time.Year, "#1");
@@ -1476,18 +1453,6 @@ namespace MonoTests.System.IO
                        Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
                        Assert.AreEqual (expectedTime.Second, time.Second, "#5");
                        Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
-#else
-                       try {
-                               File.GetLastAccessTime (path);
-                               Assert.Fail ("#1");
-                       } catch (IOException ex) {
-                               // Could not find a part of the path "..."
-                               Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
-                               Assert.IsNull (ex.InnerException, "#3");
-                               Assert.IsNotNull (ex.Message, "#4");
-                               Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
-                       }
-#endif
                }
 
                [Test]
@@ -1555,7 +1520,6 @@ namespace MonoTests.System.IO
                        string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
                        DeleteFile (path);
 
-#if NET_2_0
                        DateTime time = File.GetLastAccessTimeUtc (path);
                        Assert.AreEqual (1601, time.Year, "#1");
                        Assert.AreEqual (1, time.Month, "#2");
@@ -1563,18 +1527,6 @@ namespace MonoTests.System.IO
                        Assert.AreEqual (0, time.Hour, "#4");
                        Assert.AreEqual (0, time.Second, "#5");
                        Assert.AreEqual (0, time.Millisecond, "#6");
-#else
-                       try {
-                               File.GetLastAccessTimeUtc (path);
-                               Assert.Fail ("#1");
-                       } catch (IOException ex) {
-                               // Could not find a part of the path "..."
-                               Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
-                               Assert.IsNull (ex.InnerException, "#3");
-                               Assert.IsNotNull (ex.Message, "#4");
-                               Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
-                       }
-#endif
                }
 
                [Test]
@@ -1642,7 +1594,6 @@ namespace MonoTests.System.IO
                        string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
                        DeleteFile (path);
 
-#if NET_2_0
                        DateTime time = File.GetLastWriteTime (path);
                        DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
                        Assert.AreEqual (expectedTime.Year, time.Year, "#1");
@@ -1651,18 +1602,6 @@ namespace MonoTests.System.IO
                        Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
                        Assert.AreEqual (expectedTime.Second, time.Second, "#5");
                        Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
-#else
-                       try {
-                               File.GetLastWriteTime (path);
-                               Assert.Fail ("#1");
-                       } catch (IOException ex) {
-                               // Could not find a part of the path "..."
-                               Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
-                               Assert.IsNull (ex.InnerException, "#3");
-                               Assert.IsNotNull (ex.Message, "#4");
-                               Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
-                       }
-#endif
                }
 
                [Test]
@@ -1730,7 +1669,6 @@ namespace MonoTests.System.IO
                        string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
                        DeleteFile (path);
 
-#if NET_2_0
                        DateTime time = File.GetLastWriteTimeUtc (path);
                        Assert.AreEqual (1601, time.Year, "#1");
                        Assert.AreEqual (1, time.Month, "#2");
@@ -1738,18 +1676,6 @@ namespace MonoTests.System.IO
                        Assert.AreEqual (0, time.Hour, "#4");
                        Assert.AreEqual (0, time.Second, "#5");
                        Assert.AreEqual (0, time.Millisecond, "#6");
-#else
-                       try {
-                               File.GetLastWriteTimeUtc (path);
-                               Assert.Fail ("#1");
-                       } catch (IOException ex) {
-                               // Could not find a part of the path "..."
-                               Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
-                               Assert.IsNull (ex.InnerException, "#3");
-                               Assert.IsNotNull (ex.Message, "#4");
-                               Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
-                       }
-#endif
                }
 
                [Test]
@@ -2573,6 +2499,7 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+               [Category ("LargeFileSupport")]
                public void Position_Large ()
                {
                        // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
@@ -2595,6 +2522,7 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+               [Category ("LargeFileSupport")]
                public void Seek_Large ()
                {
                        // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
@@ -2619,13 +2547,13 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+               [Category ("LargeFileSupport")]
                public void Lock_Large ()
                {
                        // note: already worked without HAVE_LARGE_FILE_SUPPORT
                        LockUnlock ((long) Int32.MaxValue + 1);
                }
        
-#if NET_2_0
                [Test]
                public void ReadWriteAllText ()
                {
@@ -2679,7 +2607,6 @@ namespace MonoTests.System.IO
                                Assert.AreEqual ("replaceFile", txt, "#3");
                        }
                }
-#endif
 
                static bool RunningOnUnix {
                        get {
@@ -2700,7 +2627,6 @@ namespace MonoTests.System.IO
                                Directory.Delete (path, true);
                }
 
-#if NET_2_0
                void read_all (string s)
                {
                        string f = Path.GetTempFileName ();
@@ -2712,7 +2638,6 @@ namespace MonoTests.System.IO
                                DeleteFile (f);
                        }
                }
-#endif
 
                void MoveTest (FileAccess acc, FileShare share, bool works)
                {
@@ -2763,5 +2688,55 @@ namespace MonoTests.System.IO
                        MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
                        MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);
                }
+
+
+               [DllImport ("libc", SetLastError=true)]
+               public static extern int symlink (string oldpath, string newpath);
+
+               [Test]
+               public void SymLinkLoop ()
+               {
+                       if (!RunningOnUnix)
+                               Assert.Ignore ("Symlink are hard on windows");
+
+                       var name1 = Path.GetRandomFileName ();
+                       var name2 = Path.GetRandomFileName ();
+
+                       var path1 = Path.Combine (Path.GetTempPath (), name1);
+                       var path2 = Path.Combine (Path.GetTempPath (), name2);
+
+                       File.Delete (path1);
+                       File.Delete (path2);
+
+                       try {
+                               if (symlink (path1, path2) != 0)
+                                       Assert.Fail ("symlink #1 failed with errno={0}", Marshal.GetLastWin32Error ());
+                               if (symlink (path2, path1) != 0)
+                                       Assert.Fail ("symlink #2 failed with errno={0}", Marshal.GetLastWin32Error ());
+
+                               Assert.IsTrue (File.Exists (path1), "File.Exists must return true for path1 symlink loop");
+                               Assert.IsTrue (File.Exists (path2), "File.Exists must return true for path2 symlink loop");
+
+                               try {
+                                       using (var f = File.Open (path1, FileMode.Open, FileAccess.Read)) {
+                                               Assert.Fail ("File.Open must throw for symlink loops");
+                                       }
+                               } catch (IOException ex) {
+                                       Assert.AreEqual (0x80070781u, (uint)ex.HResult, "Ensure HRESULT is correct");
+                               }
+
+                               File.Delete (path1); //Delete must not throw and must work
+                               Assert.IsFalse (File.Exists (path1), "File.Delete must delete symlink loops");
+
+                       } finally {
+                               try {
+                                       File.Delete (path1);
+                                       File.Delete (path2);
+                               } catch (IOException) {
+                                       //Don't double fault any exception from the tests.
+                               }
+
+                       }
+               }
        }
 }