From 97be759cc7bd33abbe220dd8766e147bcdd700bb Mon Sep 17 00:00:00 2001 From: jeffbi Date: Tue, 7 Mar 2017 10:29:20 -0800 Subject: [PATCH] Do not reject Windows' reserved device names on non-Windows platforms. (#3252) --- .../engine/Utils.cs | 3 +- .../FileSystem.Tests.ps1 | 53 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 2d4a90dba6..e701738d44 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1110,6 +1110,7 @@ namespace System.Management.Automation 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 @@ namespace System.Management.Automation 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 8c87ca3adb..9c88d72bfb 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,9 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { $newTestFile = "NewTestFile.txt" $testContent = "Some Content" $testContent2 = "More Content" + $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 +107,53 @@ 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) + { + Move-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue + Test-Path $deviceName | Should Be $true + 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) + { + Rename-Item -Path $testFile -NewName $deviceName -Force -ErrorAction SilentlyContinue + Test-Path $deviceName | Should Be $true + New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue + } + } } Context "Validate basic host navigation functionality" { @@ -667,4 +718,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 +}