From a6e85cebba19d228087f58155e26607f17bb570d Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Fri, 3 Mar 2017 00:09:10 -0800 Subject: [PATCH 1/2] Do not reject Windows' reserved device names on non-Windows platforms. (#3221) --- .../engine/Utils.cs | 3 +- .../FileSystem.Tests.ps1 | 56 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 2d4a90dba6f..e701738d446 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1110,6 +1110,7 @@ internal static void NativeEnumerateDirectory(string directory, out List internal static bool IsReservedDeviceName(string destinationPath) { +#if !UNIX string[] reservedDeviceNames = { "CON", "PRN", "AUX", "CLOCK$", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" }; @@ -1133,7 +1134,7 @@ internal static bool IsReservedDeviceName(string destinationPath) return true; } } - +#endif return false; } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 8c87ca3adbc..e28bb834c16 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -1,3 +1,4 @@ +Import-Module $PSScriptRoot\..\..\Common\Test.Helpers.psm1 Describe "Basic FileSystem Provider Tests" -Tags "CI" { BeforeAll { $testDir = "TestDir" @@ -20,6 +21,10 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { $newTestFile = "NewTestFile.txt" $testContent = "Some Content" $testContent2 = "More Content" + $tempFile = $testFile + ".tmp" + $reservedNames = "CON", "PRN", "AUX", "CLOCK$", "NUL", + "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", + "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" } BeforeEach { @@ -103,6 +108,55 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { $contentBefore.Count | Should Be 1 $contentAfter.Count | Should Be 0 } + + It "Copy-Item on Windows rejects Windows reserved device names" -Skip:(-not $IsWindows) { + foreach ($deviceName in $reservedNames) + { + { Copy-Item -Path $testFile -Destination $deviceName -ErrorAction Stop } | ShouldBeErrorId "CopyError,Microsoft.PowerShell.Commands.CopyItemCommand" + } + } + + It "Move-Item on Windows rejects Windows reserved device names" -Skip:(-not $IsWindows) { + foreach ($deviceName in $reservedNames) + { + { Move-Item -Path $testFile -Destination $deviceName -ErrorAction Stop } | ShouldBeErrorId "MoveError,Microsoft.PowerShell.Commands.MoveItemCommand" + } + } + + It "Rename-Item on Windows rejects Windows reserved device names" -Skip:(-not $IsWindows) { + foreach ($deviceName in $reservedNames) + { + { Rename-Item -Path $testFile -NewName $deviceName -ErrorAction Stop } | ShouldBeErrorId "RenameError,Microsoft.PowerShell.Commands.RenameItemCommand" + } + } + + It "Copy-Item on Unix succeeds with Windows reserved device names" -Skip:($IsWindows) { + foreach ($deviceName in $reservedNames) + { + Copy-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue + Test-Path $deviceName | Should Be $true + } + } + + It "Move-Item on Unix succeeds with Windows reserved device names" -Skip:($IsWindows) { + foreach ($deviceName in $reservedNames) + { + Copy-Item -Path $testFile -Destination $tempFile -Force + Move-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue + Test-Path $deviceName | Should Be $true + Rename-Item -Path $tempFile -NewName $testFile + } + } + + It "Rename-Item on Unix succeeds with Windows reserved device names" -Skip:($IsWindows) { + foreach ($deviceName in $reservedNames) + { + Copy-Item -Path $testFile -Destination $tempFile -Force + Rename-Item -Path $testFile -NewName $deviceName -Force -ErrorAction SilentlyContinue + Test-Path $deviceName | Should Be $true + Rename-Item -Path $tempFile -NewName $testFile + } + } } Context "Validate basic host navigation functionality" { @@ -667,4 +721,4 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur catch { $_.FullyQualifiedErrorId | Should Be "Argument,Microsoft.PowerShell.Commands.PopLocationCommand" } } } -} \ No newline at end of file +} From 0d67f26e323ff069672d456fad64e97e996bc995 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 7 Mar 2017 10:02:16 -0800 Subject: [PATCH 2/2] Use 'New-Item' to replace copy and rename --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index e28bb834c16..9c88d72bfb6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -21,7 +21,6 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { $newTestFile = "NewTestFile.txt" $testContent = "Some Content" $testContent2 = "More Content" - $tempFile = $testFile + ".tmp" $reservedNames = "CON", "PRN", "AUX", "CLOCK$", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" @@ -141,20 +140,18 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { It "Move-Item on Unix succeeds with Windows reserved device names" -Skip:($IsWindows) { foreach ($deviceName in $reservedNames) { - Copy-Item -Path $testFile -Destination $tempFile -Force Move-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue Test-Path $deviceName | Should Be $true - Rename-Item -Path $tempFile -NewName $testFile + New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue } } It "Rename-Item on Unix succeeds with Windows reserved device names" -Skip:($IsWindows) { foreach ($deviceName in $reservedNames) { - Copy-Item -Path $testFile -Destination $tempFile -Force Rename-Item -Path $testFile -NewName $deviceName -Force -ErrorAction SilentlyContinue Test-Path $deviceName | Should Be $true - Rename-Item -Path $tempFile -NewName $testFile + New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue } } }