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 ed74831de984f6..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,390 +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)) - { - MountHelper.Unmount(mountedDirName); - 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)) - { - MountHelper.Unmount(mountedDirName); - 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)) - { - MountHelper.Unmount(mountedDirName); - 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)) - { - MountHelper.Unmount(mountedDirName); - 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(); - } - finally - { - if (!Eval(!Directory.Exists(mountedDirName), "Err_001yph! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) - { - MountHelper.Unmount(mountedDirName); - 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 (!Eval(!Directory.Exists(mountedDirName), "Err_625ckx! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) - { - MountHelper.Unmount(mountedDirName); - 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 (!Eval(!Directory.Exists(mountedDirName), "Err_462xtc! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) - { - MountHelper.Unmount(mountedDirName); - 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) @@ -415,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 d6d02b7ab7b1ff..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,355 +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 - { - MountHelper.Unmount(mountedDirName); - 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 - { - MountHelper.Unmount(mountedDirName); - 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 - { - MountHelper.Unmount(mountedDirName); - 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 - { - MountHelper.Unmount(mountedDirName); - 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) @@ -373,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); - } } }