From 0e9886ff4af532115a495c0bf8a02e03009d4315 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 17:14:18 +0000 Subject: [PATCH 1/4] Initial plan From d9e71de63b5396c9a0b88360bef70d29560def07 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 17:36:27 +0000 Subject: [PATCH 2/4] Fix flaky mount volume tests: guard Unmount in finally blocks with Directory.Exists - ReparsePoints_MountVolume.cs (fix #125624): 4 finally blocks in Scenarios 1, 2, 3.1, 3.2 unconditionally called MountHelper.Unmount(mountedDirName). When the mount point was already removed the call threw (Win32 error 4390/3), got caught by the scenario catch, and set s_pass=false. Wrap with Directory.Exists guard. - Delete_MountVolume.cs (remaining issue in #125295): Scenarios 3.3, 3.4, 3.5 used if (!Eval(!Directory.Exists(mountedDirName), ...)) in finally blocks, which set s_pass=false during cleanup when Directory.Delete left the mount point around. Change to plain if (Directory.Exists(mountedDirName)) guard. For Scenario 3.3 (which had no assertion in the try block), move the Eval assertion into the try block after Task.Delay(300).Wait(). Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com> --- .../Directory/Delete_MountVolume.cs | 7 +++-- .../Directory/ReparsePoints_MountVolume.cs | 28 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs index ed74831de984f6..b364391d7737c0 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs @@ -251,10 +251,11 @@ public static void RunTest() Directory.Delete(mountedDirName, true); Task.Delay(300).Wait(); + Eval(!Directory.Exists(mountedDirName), "Err_001yph! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName)); } finally { - if (!Eval(!Directory.Exists(mountedDirName), "Err_001yph! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) + if (Directory.Exists(mountedDirName)) { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); @@ -307,7 +308,7 @@ public static void RunTest() } finally { - if (!Eval(!Directory.Exists(mountedDirName), "Err_625ckx! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) + if (Directory.Exists(mountedDirName)) { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); @@ -363,7 +364,7 @@ public static void RunTest() } finally { - if (!Eval(!Directory.Exists(mountedDirName), "Err_462xtc! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) + if (Directory.Exists(mountedDirName)) { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs index d6d02b7ab7b1ff..ce01812eaa4398 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs @@ -109,8 +109,11 @@ public static void runTest() } finally { - MountHelper.Unmount(mountedDirName); - DeleteDir(mountedDirName, true); + if (Directory.Exists(mountedDirName)) + { + MountHelper.Unmount(mountedDirName); + DeleteDir(mountedDirName, true); + } } } else @@ -186,8 +189,11 @@ public static void runTest() } finally { - MountHelper.Unmount(mountedDirName); - DeleteDir(mountedDirName, true); + if (Directory.Exists(mountedDirName)) + { + MountHelper.Unmount(mountedDirName); + DeleteDir(mountedDirName, true); + } } } else @@ -261,8 +267,11 @@ public static void runTest() } finally { - MountHelper.Unmount(mountedDirName); - DeleteDir(mountedDirName, true); + if (Directory.Exists(mountedDirName)) + { + MountHelper.Unmount(mountedDirName); + DeleteDir(mountedDirName, true); + } } } else @@ -336,8 +345,11 @@ public static void runTest() } finally { - MountHelper.Unmount(mountedDirName); - DeleteDir(mountedDirName, true); + if (Directory.Exists(mountedDirName)) + { + MountHelper.Unmount(mountedDirName); + DeleteDir(mountedDirName, true); + } } } else From c5e58527ed1758b4a0bc90a555ab6a6e4e0e5652 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 22:54:40 +0000 Subject: [PATCH 3/4] Wrap MountHelper.Unmount in try/catch in all finally cleanup blocks The Directory.Exists guard (from PR #125348) is insufficient when MountHelper.Mount fails after Directory.CreateDirectory succeeds: the directory exists but is not a mount point, so DeleteVolumeMountPoint throws Win32 error 4390. This exception escapes the finally block, gets caught by the scenario catch, and sets s_pass = false. Confirmed by the console log (refs/heads/main, 2026-03-15, after PR #125348): Scenario 1 of Delete_MountVolume still failing with error 4390. Wrap all MountHelper.Unmount calls in `try { } catch { }` so that cleanup exceptions never escape the finally block: - Delete_MountVolume.cs: scenarios 1, 2, 3.1, 3.2, 3.3, 3.4, 3.5 - ReparsePoints_MountVolume.cs: scenarios 1, 2, 3.1, 3.2 Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com> --- .../Directory/Delete_MountVolume.cs | 14 +++++++------- .../Directory/ReparsePoints_MountVolume.cs | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs index b364391d7737c0..4295e750d35934 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs @@ -81,7 +81,7 @@ public static void RunTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -129,7 +129,7 @@ public static void RunTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -174,7 +174,7 @@ public static void RunTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -219,7 +219,7 @@ public static void RunTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -257,7 +257,7 @@ public static void RunTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -310,7 +310,7 @@ public static void RunTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -366,7 +366,7 @@ public static void RunTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs index ce01812eaa4398..f503ea5b323db8 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs @@ -111,7 +111,7 @@ public static void runTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -191,7 +191,7 @@ public static void runTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -269,7 +269,7 @@ public static void runTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } @@ -347,7 +347,7 @@ public static void runTest() { if (Directory.Exists(mountedDirName)) { - MountHelper.Unmount(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } catch { } DeleteDir(mountedDirName, true); } } From c3c12b03ca1c14e20f2221dfa7821aadceec1a99 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 19 Mar 2026 20:59:44 +0000 Subject: [PATCH 4/4] Refactor mount volume tests into separate test methods with typed Win32 exception handling - Update MountHelper.Unmount to throw Win32Exception instead of Exception, enabling typed exception filtering by NativeErrorCode in callers - Replace all bare `catch { }` cleanup handlers with `catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3)`: - 4390 = ERROR_NOT_A_REPARSE_POINT (dir exists but was never mounted, or partially cleaned) - 3 = ERROR_PATH_NOT_FOUND (path on a drive that's no longer accessible) - Break ReparsePoints_MountVolume.cs single RunTest into 4 separate ConditionalFact methods (Scenario1-2 require another NTFS drive; Scenario31-32 require current drive to be NTFS) - Break Delete_MountVolume.cs single RunTest into 7 separate ConditionalFact methods (Scenario1 requires IsNtfs+OtherNtfsDrive; Scenario2 requires OtherNtfsDrive; Scenario31-35 require current drive to be NTFS) - Remove s_pass/Eval pattern; replace with Assert.True/False directly in test methods - Remove debug file infrastructure; Console output is sufficient for CI diagnostics - Update Delete.Windows.cs cleanup similarly Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com> --- .../tests/System/IO/ReparsePointUtilities.cs | 3 +- .../Directory/Delete.Windows.cs | 4 +- .../Directory/Delete_MountVolume.cs | 600 ++++++------------ .../Directory/ReparsePoints_MountVolume.cs | 575 ++++++----------- 4 files changed, 407 insertions(+), 775 deletions(-) diff --git a/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs b/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs index a049ef7491a384..67fb92c6501d86 100644 --- a/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs +++ b/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.IO; #if !NETFRAMEWORK @@ -251,7 +252,7 @@ public static void Unmount(string mountPoint) bool r = DeleteVolumeMountPoint(mountPoint); if (!r) - throw new Exception(string.Format("Win32 error: {0}", Marshal.GetLastWin32Error())); + throw new Win32Exception(Marshal.GetLastWin32Error()); } private static ProcessStartInfo CreateProcessStartInfo(string fileName, params string[] arguments) diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.Windows.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.Windows.cs index 10c9c3b15bc1b7..ab7397d54ede0d 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.Windows.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete.Windows.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.ComponentModel; using System.Text; using Xunit; using Microsoft.DotNet.XUnitExtensions; @@ -82,7 +83,8 @@ public void Delete_VolumeMountPoint() { if (Directory.Exists(mountPoint)) { - MountHelper.Unmount(mountPoint); + try { MountHelper.Unmount(mountPoint); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } Directory.Delete(mountPoint); } } diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs index 4295e750d35934..937c908bb4dc7d 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/Delete_MountVolume.cs @@ -9,10 +9,8 @@ This testcase attempts to delete some directories in a mounted volume - refer to the directory in a recursive manner in addition to the normal one **/ using System; +using System.ComponentModel; using System.IO; -using System.Text; -using System.Reflection; -using System.Threading; using System.Threading.Tasks; using Microsoft.DotNet.XUnitExtensions; using Xunit; @@ -21,391 +19,261 @@ namespace System.IO.Tests { public class Directory_Delete_MountVolume { - private delegate void ExceptionCode(); - private static bool s_pass = true; + private const string MountPrefixName = "LaksMount"; private static bool IsNtfs => FileSystemDebugInfo.IsCurrentDriveNTFS(); - [ConditionalFact(nameof(IsNtfs))] + private static bool IsNtfsWithOtherNtfsDrive => + FileSystemDebugInfo.IsCurrentDriveNTFS() && IOServices.GetNtfsDriveOtherThanCurrent() != null; + + private static bool HasOtherNtfsDrive => + IOServices.GetNtfsDriveOtherThanCurrent() != null; + + [ConditionalFact(nameof(IsNtfsWithOtherNtfsDrive))] [PlatformSpecific(TestPlatforms.Windows)] // testing volumes / mounts / drive letters - public static void RunTest() + public static void Scenario1_DifferentDriveMountedOnCurrentDrive() { + string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); try { - const string MountPrefixName = "LaksMount"; - - string mountedDirName; - string dirName; - string dirNameWithoutRoot; - string dirNameReferredFromMountedDrive; - - //Adding debug info since this test hangs sometime in RTS runs - string debugFileName = "Co7604Delete_MountVolume_Debug.txt"; - DeleteFile(debugFileName); - string scenarioDescription; + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(otherDriveInMachine.Substring(0, 2), mountedDirName); - scenarioDescription = "Scenario 1: Vanilla - Different drive is mounted on the current drive"; - try + string dirName = ManageFileSystem.GetNonExistingDir(otherDriveInMachine, ManageFileSystem.DirPrefixName); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { - File.AppendAllText(debugFileName, string.Format("{0}{1}", scenarioDescription, Environment.NewLine)); - - string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); - //out labs use UIP tools in one drive and don't expect this drive to be used by others. We avoid this problem by not testing if the other drive is not NTFS - if (FileSystemDebugInfo.IsCurrentDriveNTFS() && otherDriveInMachine != null) - { - Console.WriteLine(scenarioDescription); - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); - - try - { - Directory.CreateDirectory(mountedDirName); - - File.AppendAllText(debugFileName, string.Format("Mounting on {0}{1}{2}", otherDriveInMachine.Substring(0, 2), mountedDirName, Environment.NewLine)); - MountHelper.Mount(otherDriveInMachine.Substring(0, 2), mountedDirName); - - dirName = ManageFileSystem.GetNonExistingDir(otherDriveInMachine, ManageFileSystem.DirPrefixName); - File.AppendAllText(debugFileName, string.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) - { - Eval(Directory.Exists(dirName), "Err_3974g! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); - //Lets refer to these via mounted drive and check - dirNameWithoutRoot = dirName.Substring(3); - dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); - Directory.Delete(dirNameReferredFromMountedDrive, true); - Task.Delay(300).Wait(); - Eval(!Directory.Exists(dirName), "Err_20387g! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } - File.AppendAllText(debugFileName, string.Format("Completed scenario {0}", Environment.NewLine)); - } - else - File.AppendAllText(debugFileName, string.Format("Scenario 1 - Vanilla - NOT RUN: Different drive is mounted on the current drive {0}", Environment.NewLine)); + Assert.True(Directory.Exists(dirName), $"Err_3974g! Directory {dirName} doesn't exist"); + string dirNameWithoutRoot = dirName.Substring(3); + string dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); + Directory.Delete(dirNameReferredFromMountedDrive, true); + Task.Delay(300).Wait(); + Assert.False(Directory.Exists(dirName), $"Err_20387g! Directory {dirName} still exists"); } - catch (Exception ex) + } + finally + { + if (Directory.Exists(mountedDirName)) { - s_pass = false; - Console.WriteLine("Err_768lme! Exception caught in scenario: {0}", ex); + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); } + } + } - scenarioDescription = "Scenario 2: Current drive is mounted on a different drive"; - Console.WriteLine(scenarioDescription); - File.AppendAllText(debugFileName, string.Format("{0}{1}", scenarioDescription, Environment.NewLine)); - try - { - string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); - if (otherDriveInMachine != null) - { - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(otherDriveInMachine.Substring(0, 3), MountPrefixName)); - try - { - Directory.CreateDirectory(mountedDirName); - - File.AppendAllText(debugFileName, string.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - - dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); - File.AppendAllText(debugFileName, string.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) - { - Eval(Directory.Exists(dirName), "Err_239ufz! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); - //Lets refer to these via mounted drive and check - dirNameWithoutRoot = dirName.Substring(3); - dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); - Directory.Delete(dirNameReferredFromMountedDrive, true); - Task.Delay(300).Wait(); - Eval(!Directory.Exists(dirName), "Err_794aiu! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } - File.AppendAllText(debugFileName, string.Format("Completed scenario {0}", Environment.NewLine)); - } - } - catch (Exception ex) - { - s_pass = false; - Console.WriteLine("Err_231vwf! Exception caught in scenario: {0}", ex); - } + [ConditionalFact(nameof(HasOtherNtfsDrive))] + [PlatformSpecific(TestPlatforms.Windows)] // testing volumes / mounts / drive letters + public static void Scenario2_CurrentDriveMountedOnOtherDrive() + { + string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(otherDriveInMachine.Substring(0, 3), MountPrefixName)); + try + { + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - //scenario 3.1: Current drive is mounted on current drive - scenarioDescription = "Scenario 3.1 - Current drive is mounted on current drive"; - try + string dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { - if (FileSystemDebugInfo.IsCurrentDriveNTFS()) - { - File.AppendAllText(debugFileName, string.Format("{0}{1}", scenarioDescription, Environment.NewLine)); - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); - try - { - Directory.CreateDirectory(mountedDirName); - - File.AppendAllText(debugFileName, string.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - - dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); - File.AppendAllText(debugFileName, string.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) - { - Eval(Directory.Exists(dirName), "Err_324eez! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); - //Lets refer to these via mounted drive and check - dirNameWithoutRoot = dirName.Substring(3); - dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); - Directory.Delete(dirNameReferredFromMountedDrive, true); - Task.Delay(300).Wait(); - Eval(!Directory.Exists(dirName), "Err_195whv! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } - File.AppendAllText(debugFileName, string.Format("Completed scenario {0}", Environment.NewLine)); - } + Assert.True(Directory.Exists(dirName), $"Err_239ufz! Directory {dirName} doesn't exist"); + string dirNameWithoutRoot = dirName.Substring(3); + string dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); + Directory.Delete(dirNameReferredFromMountedDrive, true); + Task.Delay(300).Wait(); + Assert.False(Directory.Exists(dirName), $"Err_794aiu! Directory {dirName} still exists"); } - catch (Exception ex) + } + finally + { + if (Directory.Exists(mountedDirName)) { - s_pass = false; - Console.WriteLine("Err_493ojg! Exception caught in scenario: {0}", ex); + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); } + } + } - //scenario 3.2: Current drive is mounted on current directory - scenarioDescription = "Scenario 3.2 - Current drive is mounted on current directory"; - try - { - if (FileSystemDebugInfo.IsCurrentDriveNTFS()) - { - File.AppendAllText(debugFileName, string.Format("{0}{1}", scenarioDescription, Environment.NewLine)); - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); - try - { - Directory.CreateDirectory(mountedDirName); - - File.AppendAllText(debugFileName, string.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); + [ConditionalFact(nameof(IsNtfs))] + [PlatformSpecific(TestPlatforms.Windows)] // testing volumes / mounts / drive letters + public static void Scenario31_CurrentDriveMountedOnCurrentDrive() + { + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); + try + { + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); - File.AppendAllText(debugFileName, string.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) - { - Eval(Directory.Exists(dirName), "Err_951ipb! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); - //Lets refer to these via mounted drive and check - dirNameWithoutRoot = dirName.Substring(3); - dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); - Directory.Delete(dirNameReferredFromMountedDrive, true); - Task.Delay(300).Wait(); - Eval(!Directory.Exists(dirName), "Err_493yin! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } - File.AppendAllText(debugFileName, string.Format("Completed scenario {0}", Environment.NewLine)); - } - } - catch (Exception ex) + string dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { - s_pass = false; - Console.WriteLine("Err_432qcp! Exception caught in scenario: {0}", ex); + Assert.True(Directory.Exists(dirName), $"Err_324eez! Directory {dirName} doesn't exist"); + string dirNameWithoutRoot = dirName.Substring(3); + string dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); + Directory.Delete(dirNameReferredFromMountedDrive, true); + Task.Delay(300).Wait(); + Assert.False(Directory.Exists(dirName), $"Err_195whv! Directory {dirName} still exists"); } - - //@WATCH - potentially dangerous code - can delete the whole drive!! - //scenario 3.3: we call delete on the mounted volume - this should only delete the mounted drive? - //Current drive is mounted on current directory - scenarioDescription = "Scenario 3.3 - we call delete on the mounted volume"; - try + } + finally + { + if (Directory.Exists(mountedDirName)) { - if (FileSystemDebugInfo.IsCurrentDriveNTFS()) - { - File.AppendAllText(debugFileName, string.Format("{0}{1}", scenarioDescription, Environment.NewLine)); - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); - try - { - Directory.CreateDirectory(mountedDirName); + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); + } + } + } - File.AppendAllText(debugFileName, string.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); + [ConditionalFact(nameof(IsNtfs))] + [PlatformSpecific(TestPlatforms.Windows)] // testing volumes / mounts / drive letters + public static void Scenario32_CurrentDriveMountedOnCurrentDirectory() + { + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); + try + { + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - Directory.Delete(mountedDirName, true); - Task.Delay(300).Wait(); - Eval(!Directory.Exists(mountedDirName), "Err_001yph! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName)); - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } - File.AppendAllText(debugFileName, string.Format("Completed scenario {0}", Environment.NewLine)); - } - } - catch (Exception ex) + string dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { - s_pass = false; - Console.WriteLine("Err_386rpj! Exception caught in scenario: {0}", ex); + Assert.True(Directory.Exists(dirName), $"Err_951ipb! Directory {dirName} doesn't exist"); + string dirNameWithoutRoot = dirName.Substring(3); + string dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); + Directory.Delete(dirNameReferredFromMountedDrive, true); + Task.Delay(300).Wait(); + Assert.False(Directory.Exists(dirName), $"Err_493yin! Directory {dirName} still exists"); } - - //@WATCH - potentially dangerous code - can delete the whole drive!! - //scenario 3.4: we call delete on parent directory of the mounted volume, the parent directory will have some other directories and files - //Current drive is mounted on current directory - scenarioDescription = "Scenario 3.4 - we call delete on parent directory of the mounted volume, the parent directory will have some other directories and files"; - try + } + finally + { + if (Directory.Exists(mountedDirName)) { - if (FileSystemDebugInfo.IsCurrentDriveNTFS()) - { - File.AppendAllText(debugFileName, string.Format("{0}{1}", scenarioDescription, Environment.NewLine)); - mountedDirName = null; - try - { - dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); - File.AppendAllText(debugFileName, string.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 2, 20)) - { - Eval(Directory.Exists(dirName), "Err_469yvh! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); - string[] dirs = fileManager.GetDirectories(1); - mountedDirName = Path.GetFullPath(dirs[0]); - if (Eval(Directory.GetDirectories(mountedDirName).Length == 0, "Err_974tsg! the sub directory has directories: {0}", mountedDirName)) - { - foreach (string file in Directory.GetFiles(mountedDirName)) - File.Delete(file); - if (Eval(Directory.GetFiles(mountedDirName).Length == 0, "Err_13ref! the mounted directory has files: {0}", mountedDirName)) - { - File.AppendAllText(debugFileName, string.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - //now lets call delete on the parent directory - Directory.Delete(dirName, true); - Task.Delay(300).Wait(); - Eval(!Directory.Exists(dirName), "Err_006jsf! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); - Console.WriteLine("Completed Scenario 3.4"); - } - } - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } - File.AppendAllText(debugFileName, string.Format("Completed scenario {0}", Environment.NewLine)); - } + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); } - catch (Exception ex) + } + } + + // @WATCH - potentially dangerous code - can delete the whole drive!! + // Scenario 3.3: we call delete on the mounted volume - this should only delete the mounted drive? + [ConditionalFact(nameof(IsNtfs))] + [PlatformSpecific(TestPlatforms.Windows)] // testing volumes / mounts / drive letters + public static void Scenario33_DeleteMountedVolume() + { + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); + try + { + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); + Directory.Delete(mountedDirName, true); + Task.Delay(300).Wait(); + Assert.False(Directory.Exists(mountedDirName), $"Err_001yph! Directory {mountedDirName} still exists"); + } + finally + { + if (Directory.Exists(mountedDirName)) { - s_pass = false; - Console.WriteLine("Err_578tni! Exception caught in scenario: {0}", ex); + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); } + } + } - //@WATCH - potentially dangerous code - can delete the whole drive!! - //scenario 3.5: we call delete on parent directory of the mounted volume, the parent directory will have some other directories and files - //we call a different directory than the first - //Current drive is mounted on current directory - scenarioDescription = "Scenario 3.5 - we call delete on parent directory of the mounted volume, the parent directory will have some other directories and files"; - try + // @WATCH - potentially dangerous code - can delete the whole drive!! + // Scenario 3.4: we call delete on parent directory of the mounted volume + [ConditionalFact(nameof(IsNtfs))] + [PlatformSpecific(TestPlatforms.Windows)] // testing volumes / mounts / drive letters + public static void Scenario34_DeleteParentOfMountPoint() + { + string mountedDirName = null; + string dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); + try + { + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 2, 20)) { - if (FileSystemDebugInfo.IsCurrentDriveNTFS()) - { - File.AppendAllText(debugFileName, string.Format("{0}{1}", scenarioDescription, Environment.NewLine)); - mountedDirName = null; - try - { - dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); - File.AppendAllText(debugFileName, string.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 2, 30)) - { - Eval(Directory.Exists(dirName), "Err_715tdq! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); - string[] dirs = fileManager.GetDirectories(1); - mountedDirName = Path.GetFullPath(dirs[0]); - if (dirs.Length > 1) - mountedDirName = Path.GetFullPath(dirs[1]); - if (Eval(Directory.GetDirectories(mountedDirName).Length == 0, "Err_492qwl! the sub directory has directories: {0}", mountedDirName)) - { - foreach (string file in Directory.GetFiles(mountedDirName)) - File.Delete(file); - if (Eval(Directory.GetFiles(mountedDirName).Length == 0, "Err_904kij! the mounted directory has files: {0}", mountedDirName)) - { - File.AppendAllText(debugFileName, string.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - //now lets call delete on the parent directory - Directory.Delete(dirName, true); - Task.Delay(300).Wait(); - Eval(!Directory.Exists(dirName), "Err_900edl! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); - Console.WriteLine("Completed Scenario 3.5: {0}", mountedDirName); - } - } - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } - File.AppendAllText(debugFileName, string.Format("Completed scenario {0}", Environment.NewLine)); - } + Assert.True(Directory.Exists(dirName), $"Err_469yvh! Directory {dirName} doesn't exist"); + string[] dirs = fileManager.GetDirectories(1); + mountedDirName = Path.GetFullPath(dirs[0]); + Assert.True(Directory.GetDirectories(mountedDirName).Length == 0, $"Err_974tsg! the sub directory has directories: {mountedDirName}"); + foreach (string file in Directory.GetFiles(mountedDirName)) + File.Delete(file); + Assert.True(Directory.GetFiles(mountedDirName).Length == 0, $"Err_13ref! the mounted directory has files: {mountedDirName}"); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); + Directory.Delete(dirName, true); + Task.Delay(300).Wait(); + Assert.False(Directory.Exists(dirName), $"Err_006jsf! Directory {dirName} still exists"); + Console.WriteLine("Completed Scenario 3.4"); } - catch (Exception ex) + } + finally + { + if (Directory.Exists(mountedDirName)) { - s_pass = false; - Console.WriteLine("Err_471jli! Exception caught in scenario: {0}", ex); + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); } + DeleteDir(dirName, true); } - catch (Exception ex) + } + + // @WATCH - potentially dangerous code - can delete the whole drive!! + // Scenario 3.5: we call delete on parent directory of the mounted volume (alternate subdirectory) + [ConditionalFact(nameof(IsNtfs))] + [PlatformSpecific(TestPlatforms.Windows)] // testing volumes / mounts / drive letters + public static void Scenario35_DeleteParentOfMountPointAlternateDir() + { + string mountedDirName = null; + string dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); + try { - s_pass = false; - Console.WriteLine("Err_234rsgf! Uncaught exception in RunTest: {0}", ex); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 2, 30)) + { + Assert.True(Directory.Exists(dirName), $"Err_715tdq! Directory {dirName} doesn't exist"); + string[] dirs = fileManager.GetDirectories(1); + mountedDirName = Path.GetFullPath(dirs[0]); + if (dirs.Length > 1) + mountedDirName = Path.GetFullPath(dirs[1]); + Assert.True(Directory.GetDirectories(mountedDirName).Length == 0, $"Err_492qwl! the sub directory has directories: {mountedDirName}"); + foreach (string file in Directory.GetFiles(mountedDirName)) + File.Delete(file); + Assert.True(Directory.GetFiles(mountedDirName).Length == 0, $"Err_904kij! the mounted directory has files: {mountedDirName}"); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); + Directory.Delete(dirName, true); + Task.Delay(300).Wait(); + Assert.False(Directory.Exists(dirName), $"Err_900edl! Directory {dirName} still exists"); + Console.WriteLine("Completed Scenario 3.5: {0}", mountedDirName); + } } finally { - Assert.True(s_pass); + if (Directory.Exists(mountedDirName)) + { + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); + } + DeleteDir(dirName, true); } } - private static void DeleteFile(string debugFileName) - { - if (File.Exists(debugFileName)) - File.Delete(debugFileName); - } - - private static void DeleteDir(string debugFileName, bool sub) + private static void DeleteDir(string path, bool sub) { bool deleted = false; int maxAttempts = 5; while (!deleted && maxAttempts > 0) { - if (Directory.Exists(debugFileName)) + if (Directory.Exists(path)) { try { - Directory.Delete(debugFileName, sub); + Directory.Delete(path, sub); deleted = true; } catch (Exception) @@ -416,67 +284,11 @@ private static void DeleteDir(string debugFileName, bool sub) Task.Delay(300).Wait(); } } - } - } - - //Checks for error - private static bool Eval(bool expression, string msg, params object[] values) - { - return Eval(expression, string.Format(msg, values)); - } - - private static bool Eval(T actual, T expected, string errorMsg) - { - bool retValue = expected == null ? actual == null : expected.Equals(actual); - - if (!retValue) - Eval(retValue, errorMsg + - " Expected:" + (null == expected ? "" : expected.ToString()) + - " Actual:" + (null == actual ? "" : actual.ToString())); - - return retValue; - } - - private static bool Eval(bool expression, string msg) - { - if (!expression) - { - s_pass = false; - Console.WriteLine(msg); - } - return expression; - } - - //Checks for a particular type of exception - private static void CheckException(ExceptionCode test, string error) - { - CheckException(test, error, null); - } - - //Checks for a particular type of exception and an Exception msg in the English locale - private static void CheckException(ExceptionCode test, string error, string msgExpected) - { - bool exception = false; - try - { - test(); - error = string.Format("{0} Exception NOT thrown ", error); - } - catch (Exception e) - { - if (e.GetType() == typeof(E)) + else { - exception = true; - if (System.Globalization.CultureInfo.CurrentUICulture.Name == "en-US" && msgExpected != null && e.Message != msgExpected) - { - exception = false; - error = string.Format("{0} Message Different: <{1}>", error, e.Message); - } + deleted = true; } - else - error = string.Format("{0} Exception type: {1}", error, e.GetType().Name); } - Eval(exception, error); } } } diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs index f503ea5b323db8..d23fe378d94e0a 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/ReparsePoints_MountVolume.cs @@ -6,10 +6,9 @@ This testcase attempts to checks GetDirectories/GetFiles with the following Repa - Mount Volumes **/ using System; -using System.IO; -using System.Text; using System.Collections.Generic; -using System.Diagnostics; +using System.ComponentModel; +using System.IO; using Microsoft.DotNet.XUnitExtensions; using Xunit; @@ -17,367 +16,245 @@ namespace System.IO.Tests { public class Directory_ReparsePoints_MountVolume { - private delegate void ExceptionCode(); - private static bool s_pass = true; + private const string MountPrefixName = "LaksMount"; private static bool IsNtfs => FileSystemDebugInfo.IsCurrentDriveNTFS(); - [ConditionalFact(nameof(IsNtfs))] + private static bool IsNtfsWithOtherNtfsDrive => + FileSystemDebugInfo.IsCurrentDriveNTFS() && IOServices.GetNtfsDriveOtherThanCurrent() != null; + + private static bool HasOtherNtfsDrive => + IOServices.GetNtfsDriveOtherThanCurrent() != null; + + [ConditionalFact(nameof(IsNtfsWithOtherNtfsDrive))] [PlatformSpecific(TestPlatforms.Windows)] // testing mounting volumes and reparse points - public static void runTest() + public static void Scenario1_DifferentDriveMountedOnCurrentDrive() { + string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); try { - Stopwatch watch; - - const string MountPrefixName = "LaksMount"; + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(otherDriveInMachine.Substring(0, 2), mountedDirName); - string mountedDirName; - string dirNameWithoutRoot; - string dirNameReferredFromMountedDrive; - string dirName; - string[] expectedFiles; - string[] files; - string[] expectedDirs; - string[] dirs; - List list; - - watch = new Stopwatch(); - watch.Start(); - try + string dirName = ManageFileSystem.GetNonExistingDir(otherDriveInMachine, ManageFileSystem.DirPrefixName); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { - //Scenario 1: Vanilla - Different drive is mounted on the current drive - Console.WriteLine("Scenario 1 - Vanilla: Different drive is mounted on the current drive: {0}", watch.Elapsed); - - string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); - if (FileSystemDebugInfo.IsCurrentDriveNTFS() && otherDriveInMachine != null) + string dirNameWithoutRoot = dirName.Substring(3); + string dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); + + // Files + string[] expectedFiles = fileManager.GetAllFiles(); + List list = new List(); + foreach (string file in expectedFiles) + list.Add(Path.GetFileName(file)); + string[] files = Directory.GetFiles(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); + Assert.True(files.Length == list.Count, $"Err_3947g! wrong count: expected {list.Count} got {files.Length}"); + for (int i = 0; i < files.Length; i++) { - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); - try - { - Console.WriteLine("Creating directory " + mountedDirName); - Directory.CreateDirectory(mountedDirName); - MountHelper.Mount(otherDriveInMachine.Substring(0, 2), mountedDirName); - - dirName = ManageFileSystem.GetNonExistingDir(otherDriveInMachine, ManageFileSystem.DirPrefixName); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) - { - dirNameWithoutRoot = dirName.Substring(3); - dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); - - //Files - expectedFiles = fileManager.GetAllFiles(); - list = new List(); - //We will only test the filenames since they are unique - foreach (string file in expectedFiles) - list.Add(Path.GetFileName(file)); - files = Directory.GetFiles(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); - Eval(files.Length == list.Count, "Err_3947g! wrong count"); - for (int i = 0; i < expectedFiles.Length; i++) - { - if (Eval(list.Contains(Path.GetFileName(files[i])), "Err_582bmw! No file found: {0}", files[i])) - list.Remove(Path.GetFileName(files[i])); - } - if (!Eval(list.Count == 0, "Err_891vut! wrong count: {0}", list.Count)) - { - Console.WriteLine(); - foreach (string fileName in list) - Console.WriteLine(fileName); - } - - //Directories - expectedDirs = fileManager.GetAllDirectories(); - list = new List(); - foreach (string dir in expectedDirs) - list.Add(dir.Substring(dirName.Length)); - dirs = Directory.GetDirectories(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); - Eval(dirs.Length == list.Count, "Err_813weq! wrong count"); - for (int i = 0; i < dirs.Length; i++) - { - string exDir = dirs[i].Substring(dirNameReferredFromMountedDrive.Length); - if (Eval(list.Contains(exDir), "Err_287kkm! No file found: {0}", exDir)) - list.Remove(exDir); - } - if (!Eval(list.Count == 0, "Err_921mhs! wrong count: {0}", list.Count)) - { - Console.WriteLine(); - foreach (string value in list) - Console.WriteLine(value); - } - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } + Assert.True(list.Contains(Path.GetFileName(files[i])), $"Err_582bmw! No file found: {files[i]}"); + list.Remove(Path.GetFileName(files[i])); } - else + Assert.True(list.Count == 0, $"Err_891vut! wrong count: {list.Count}\n{string.Join("\n", list)}"); + + // Directories + string[] expectedDirs = fileManager.GetAllDirectories(); + list = new List(); + foreach (string dir in expectedDirs) + list.Add(dir.Substring(dirName.Length)); + string[] dirs = Directory.GetDirectories(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); + Assert.True(dirs.Length == list.Count, $"Err_813weq! wrong count: expected {list.Count} got {dirs.Length}"); + for (int i = 0; i < dirs.Length; i++) { - Console.WriteLine("Skipping since drive is not NTFS and there is no other drive on the machine"); + string exDir = dirs[i].Substring(dirNameReferredFromMountedDrive.Length); + Assert.True(list.Contains(exDir), $"Err_287kkm! No dir found: {exDir}"); + list.Remove(exDir); } + Assert.True(list.Count == 0, $"Err_921mhs! wrong count: {list.Count}\n{string.Join("\n", list)}"); } - catch (Exception ex) + } + finally + { + if (Directory.Exists(mountedDirName)) { - s_pass = false; - Console.WriteLine("Err_768lme! Exception caught in scenario: {0}", ex); + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); } + } + } + + [ConditionalFact(nameof(HasOtherNtfsDrive))] + [PlatformSpecific(TestPlatforms.Windows)] // testing mounting volumes and reparse points + public static void Scenario2_CurrentDriveMountedOnOtherDrive() + { + string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(otherDriveInMachine.Substring(0, 3), MountPrefixName)); + try + { + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - //Scenario 2: Current drive is mounted on a different drive - Console.WriteLine(Environment.NewLine + "Scenario 2 - Current drive is mounted on a different drive: {0}", watch.Elapsed); - try + string dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { - string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); - if (otherDriveInMachine != null) + string dirNameWithoutRoot = dirName.Substring(3); + string dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); + + // Files + string[] expectedFiles = fileManager.GetAllFiles(); + List list = new List(); + foreach (string file in expectedFiles) + list.Add(Path.GetFileName(file)); + string[] files = Directory.GetFiles(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); + Assert.True(files.Length == list.Count, $"Err_689myg! wrong count: expected {list.Count} got {files.Length}"); + for (int i = 0; i < files.Length; i++) { - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(otherDriveInMachine.Substring(0, 3), MountPrefixName)); - try - { - Directory.CreateDirectory(mountedDirName); - - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - - dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) - { - dirNameWithoutRoot = dirName.Substring(3); - dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); - //Files - expectedFiles = fileManager.GetAllFiles(); - list = new List(); - //We will only test the filenames since they are unique - foreach (string file in expectedFiles) - list.Add(Path.GetFileName(file)); - files = Directory.GetFiles(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); - Eval(files.Length == list.Count, "Err_689myg! wrong count"); - for (int i = 0; i < expectedFiles.Length; i++) - { - if (Eval(list.Contains(Path.GetFileName(files[i])), "Err_894vhm! No file found: {0}", files[i])) - list.Remove(Path.GetFileName(files[i])); - } - if (!Eval(list.Count == 0, "Err_952qkj! wrong count: {0}", list.Count)) - { - Console.WriteLine(); - foreach (string fileName in list) - Console.WriteLine(fileName); - } - - //Directories - expectedDirs = fileManager.GetAllDirectories(); - list = new List(); - foreach (string dir in expectedDirs) - list.Add(dir.Substring(dirName.Length)); - dirs = Directory.GetDirectories(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); - Eval(dirs.Length == list.Count, "Err_154vrz! wrong count"); - for (int i = 0; i < dirs.Length; i++) - { - string exDir = dirs[i].Substring(dirNameReferredFromMountedDrive.Length); - if (Eval(list.Contains(exDir), "Err_301sao! No file found: {0}", exDir)) - list.Remove(exDir); - } - if (!Eval(list.Count == 0, "Err_630gjj! wrong count: {0}", list.Count)) - { - Console.WriteLine(); - foreach (string value in list) - Console.WriteLine(value); - } - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } + Assert.True(list.Contains(Path.GetFileName(files[i])), $"Err_894vhm! No file found: {files[i]}"); + list.Remove(Path.GetFileName(files[i])); } - else + Assert.True(list.Count == 0, $"Err_952qkj! wrong count: {list.Count}\n{string.Join("\n", list)}"); + + // Directories + string[] expectedDirs = fileManager.GetAllDirectories(); + list = new List(); + foreach (string dir in expectedDirs) + list.Add(dir.Substring(dirName.Length)); + string[] dirs = Directory.GetDirectories(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); + Assert.True(dirs.Length == list.Count, $"Err_154vrz! wrong count: expected {list.Count} got {dirs.Length}"); + for (int i = 0; i < dirs.Length; i++) { - Console.WriteLine("Skipping since drive is not NTFS and there is no other drive on the machine"); + string exDir = dirs[i].Substring(dirNameReferredFromMountedDrive.Length); + Assert.True(list.Contains(exDir), $"Err_301sao! No dir found: {exDir}"); + list.Remove(exDir); } + Assert.True(list.Count == 0, $"Err_630gjj! wrong count: {list.Count}\n{string.Join("\n", list)}"); } - catch (Exception ex) + } + finally + { + if (Directory.Exists(mountedDirName)) { - s_pass = false; - Console.WriteLine("Err_231vwf! Exception caught in scenario: {0}", ex); + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); } + } + } - //scenario 3.1: Current drive is mounted on current drive - Console.WriteLine(Environment.NewLine + "Scenario 3.1 - Current drive is mounted on current drive: {0}", watch.Elapsed); - try + [ConditionalFact(nameof(IsNtfs))] + [PlatformSpecific(TestPlatforms.Windows)] // testing mounting volumes and reparse points + public static void Scenario31_CurrentDriveMountedOnCurrentDrive() + { + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); + try + { + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); + + string dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { - if (FileSystemDebugInfo.IsCurrentDriveNTFS()) + string dirNameWithoutRoot = dirName.Substring(3); + string dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); + + // Files + string[] expectedFiles = fileManager.GetAllFiles(); + List list = new List(); + foreach (string file in expectedFiles) + list.Add(Path.GetFileName(file)); + string[] files = Directory.GetFiles(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); + Assert.True(files.Length == list.Count, $"Err_213fuo! wrong count: expected {list.Count} got {files.Length}"); + for (int i = 0; i < files.Length; i++) { - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); - try - { - Directory.CreateDirectory(mountedDirName); - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - - dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) - { - dirNameWithoutRoot = dirName.Substring(3); - dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); - //Files - expectedFiles = fileManager.GetAllFiles(); - list = new List(); - //We will only test the filenames since they are unique - foreach (string file in expectedFiles) - list.Add(Path.GetFileName(file)); - files = Directory.GetFiles(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); - Eval(files.Length == list.Count, "Err_213fuo! wrong count"); - for (int i = 0; i < expectedFiles.Length; i++) - { - if (Eval(list.Contains(Path.GetFileName(files[i])), "Err_499oxz! No file found: {0}", files[i])) - list.Remove(Path.GetFileName(files[i])); - } - if (!Eval(list.Count == 0, "Err_301gtz! wrong count: {0}", list.Count)) - { - Console.WriteLine(); - foreach (string fileName in list) - Console.WriteLine(fileName); - } - - //Directories - expectedDirs = fileManager.GetAllDirectories(); - list = new List(); - foreach (string dir in expectedDirs) - list.Add(dir.Substring(dirName.Length)); - dirs = Directory.GetDirectories(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); - Eval(dirs.Length == list.Count, "Err_771dxv! wrong count"); - for (int i = 0; i < dirs.Length; i++) - { - string exDir = dirs[i].Substring(dirNameReferredFromMountedDrive.Length); - if (Eval(list.Contains(exDir), "Err_315jey! No file found: {0}", exDir)) - list.Remove(exDir); - } - if (!Eval(list.Count == 0, "Err_424opm! wrong count: {0}", list.Count)) - { - Console.WriteLine(); - foreach (string value in list) - Console.WriteLine(value); - } - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } + Assert.True(list.Contains(Path.GetFileName(files[i])), $"Err_499oxz! No file found: {files[i]}"); + list.Remove(Path.GetFileName(files[i])); } - else + Assert.True(list.Count == 0, $"Err_301gtz! wrong count: {list.Count}\n{string.Join("\n", list)}"); + + // Directories + string[] expectedDirs = fileManager.GetAllDirectories(); + list = new List(); + foreach (string dir in expectedDirs) + list.Add(dir.Substring(dirName.Length)); + string[] dirs = Directory.GetDirectories(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); + Assert.True(dirs.Length == list.Count, $"Err_771dxv! wrong count: expected {list.Count} got {dirs.Length}"); + for (int i = 0; i < dirs.Length; i++) { - Console.WriteLine("Drive is not NTFS. Skipping scenario"); + string exDir = dirs[i].Substring(dirNameReferredFromMountedDrive.Length); + Assert.True(list.Contains(exDir), $"Err_315jey! No dir found: {exDir}"); + list.Remove(exDir); } + Assert.True(list.Count == 0, $"Err_424opm! wrong count: {list.Count}\n{string.Join("\n", list)}"); } - catch (Exception ex) + } + finally + { + if (Directory.Exists(mountedDirName)) { - s_pass = false; - Console.WriteLine("Err_493ojg! Exception caught in scenario: {0}", ex); + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); } + } + } + + [ConditionalFact(nameof(IsNtfs))] + [PlatformSpecific(TestPlatforms.Windows)] // testing mounting volumes and reparse points + public static void Scenario32_CurrentDriveMountedOnCurrentDirectory() + { + string mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); + try + { + Directory.CreateDirectory(mountedDirName); + MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - //scenario 3.2: Current drive is mounted on current directory - Console.WriteLine(Environment.NewLine + "Scenario 3.2 - Current drive is mounted on current directory: {0}", watch.Elapsed); - try + string dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); + using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { - if (FileSystemDebugInfo.IsCurrentDriveNTFS()) + string dirNameWithoutRoot = dirName.Substring(3); + string dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); + + // Files + string[] expectedFiles = fileManager.GetAllFiles(); + List list = new List(); + foreach (string file in expectedFiles) + list.Add(Path.GetFileName(file)); + string[] files = Directory.GetFiles(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); + Assert.True(files.Length == list.Count, $"Err_253yit! wrong count: expected {list.Count} got {files.Length}"); + for (int i = 0; i < files.Length; i++) { - mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); - try - { - Directory.CreateDirectory(mountedDirName); - MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); - - dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); - using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) - { - dirNameWithoutRoot = dirName.Substring(3); - dirNameReferredFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); - //Files - expectedFiles = fileManager.GetAllFiles(); - list = new List(); - //We will only test the filenames since they are unique - foreach (string file in expectedFiles) - list.Add(Path.GetFileName(file)); - files = Directory.GetFiles(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); - Eval(files.Length == list.Count, "Err_253yit! wrong count"); - for (int i = 0; i < expectedFiles.Length; i++) - { - if (Eval(list.Contains(Path.GetFileName(files[i])), "Err_798mjs! No file found: {0}", files[i])) - list.Remove(Path.GetFileName(files[i])); - } - if (!Eval(list.Count == 0, "Err_141lgl! wrong count: {0}", list.Count)) - { - Console.WriteLine(); - foreach (string fileName in list) - Console.WriteLine(fileName); - } - - //Directories - expectedDirs = fileManager.GetAllDirectories(); - list = new List(); - foreach (string dir in expectedDirs) - list.Add(dir.Substring(dirName.Length)); - dirs = Directory.GetDirectories(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); - Eval(dirs.Length == list.Count, "Err_512oxq! wrong count"); - for (int i = 0; i < dirs.Length; i++) - { - string exDir = dirs[i].Substring(dirNameReferredFromMountedDrive.Length); - if (Eval(list.Contains(exDir), "Err_907zbr! No file found: {0}", exDir)) - list.Remove(exDir); - } - if (!Eval(list.Count == 0, "Err_574raf! wrong count: {0}", list.Count)) - { - Console.WriteLine(); - foreach (string value in list) - Console.WriteLine(value); - } - } - } - finally - { - if (Directory.Exists(mountedDirName)) - { - try { MountHelper.Unmount(mountedDirName); } catch { } - DeleteDir(mountedDirName, true); - } - } + Assert.True(list.Contains(Path.GetFileName(files[i])), $"Err_798mjs! No file found: {files[i]}"); + list.Remove(Path.GetFileName(files[i])); } - else + Assert.True(list.Count == 0, $"Err_141lgl! wrong count: {list.Count}\n{string.Join("\n", list)}"); + + // Directories + string[] expectedDirs = fileManager.GetAllDirectories(); + list = new List(); + foreach (string dir in expectedDirs) + list.Add(dir.Substring(dirName.Length)); + string[] dirs = Directory.GetDirectories(dirNameReferredFromMountedDrive, "*.*", SearchOption.AllDirectories); + Assert.True(dirs.Length == list.Count, $"Err_512oxq! wrong count: expected {list.Count} got {dirs.Length}"); + for (int i = 0; i < dirs.Length; i++) { - Console.WriteLine("Drive is not NTFS. Skipping scenario"); + string exDir = dirs[i].Substring(dirNameReferredFromMountedDrive.Length); + Assert.True(list.Contains(exDir), $"Err_907zbr! No dir found: {exDir}"); + list.Remove(exDir); } + Assert.True(list.Count == 0, $"Err_574raf! wrong count: {list.Count}\n{string.Join("\n", list)}"); } - catch (Exception ex) - { - s_pass = false; - Console.WriteLine("Err_432qcp! Exception caught in scenario: {0}", ex); - } - - Console.WriteLine("Completed {0}", watch.Elapsed); } - catch (Exception ex) + finally { - s_pass = false; - Console.WriteLine("Err_234rsgf! Uncaught exception in RunTest: {0}", ex); + if (Directory.Exists(mountedDirName)) + { + try { MountHelper.Unmount(mountedDirName); } + catch (Win32Exception ex) when (ex.NativeErrorCode is 4390 or 3) { } + DeleteDir(mountedDirName, true); + } } - - Assert.True(s_pass); - } - - private static void DeleteFile(string fileName) - { - if (File.Exists(fileName)) - File.Delete(fileName); } private static void DeleteDir(string fileName, bool sub) @@ -385,65 +262,5 @@ private static void DeleteDir(string fileName, bool sub) if (Directory.Exists(fileName)) Directory.Delete(fileName, sub); } - - //Checks for error - private static bool Eval(bool expression, string msg, params object[] values) - { - return Eval(expression, string.Format(msg, values)); - } - - private static bool Eval(T actual, T expected, string errorMsg) - { - bool retValue = expected == null ? actual == null : expected.Equals(actual); - - if (!retValue) - Eval(retValue, errorMsg + - " Expected:" + (null == expected ? "" : expected.ToString()) + - " Actual:" + (null == actual ? "" : actual.ToString())); - - return retValue; - } - - private static bool Eval(bool expression, string msg) - { - if (!expression) - { - s_pass = false; - Console.WriteLine(msg); - } - return expression; - } - - //Checks for a particular type of exception - private static void CheckException(ExceptionCode test, string error) - { - CheckException(test, error, null); - } - - //Checks for a particular type of exception and an Exception msg in the English locale - private static void CheckException(ExceptionCode test, string error, string msgExpected) - { - bool exception = false; - try - { - test(); - error = string.Format("{0} Exception NOT thrown ", error); - } - catch (Exception e) - { - if (e.GetType() == typeof(E)) - { - exception = true; - if (System.Globalization.CultureInfo.CurrentUICulture.Name == "en-US" && msgExpected != null && e.Message != msgExpected) - { - exception = false; - error = string.Format("{0} Message Different: <{1}>", error, e.Message); - } - } - else - error = string.Format("{0} Exception type: {1}", error, e.GetType().Name); - } - Eval(exception, error); - } } }