Skip to content

Commit 97be759

Browse files
jeffbidaxian-dbw
authored andcommitted
Do not reject Windows' reserved device names on non-Windows platforms. (PowerShell#3252)
1 parent e734d44 commit 97be759

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/System.Management.Automation/engine/Utils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ internal static void NativeEnumerateDirectory(string directory, out List<string>
11101110

11111111
internal static bool IsReservedDeviceName(string destinationPath)
11121112
{
1113+
#if !UNIX
11131114
string[] reservedDeviceNames = { "CON", "PRN", "AUX", "CLOCK$", "NUL",
11141115
"COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
11151116
"LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
@@ -1133,7 +1134,7 @@ internal static bool IsReservedDeviceName(string destinationPath)
11331134
return true;
11341135
}
11351136
}
1136-
1137+
#endif
11371138
return false;
11381139
}
11391140

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Import-Module $PSScriptRoot\..\..\Common\Test.Helpers.psm1
12
Describe "Basic FileSystem Provider Tests" -Tags "CI" {
23
BeforeAll {
34
$testDir = "TestDir"
@@ -20,6 +21,9 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
2021
$newTestFile = "NewTestFile.txt"
2122
$testContent = "Some Content"
2223
$testContent2 = "More Content"
24+
$reservedNames = "CON", "PRN", "AUX", "CLOCK$", "NUL",
25+
"COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
26+
"LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
2327
}
2428

2529
BeforeEach {
@@ -103,6 +107,53 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
103107
$contentBefore.Count | Should Be 1
104108
$contentAfter.Count | Should Be 0
105109
}
110+
111+
It "Copy-Item on Windows rejects Windows reserved device names" -Skip:(-not $IsWindows) {
112+
foreach ($deviceName in $reservedNames)
113+
{
114+
{ Copy-Item -Path $testFile -Destination $deviceName -ErrorAction Stop } | ShouldBeErrorId "CopyError,Microsoft.PowerShell.Commands.CopyItemCommand"
115+
}
116+
}
117+
118+
It "Move-Item on Windows rejects Windows reserved device names" -Skip:(-not $IsWindows) {
119+
foreach ($deviceName in $reservedNames)
120+
{
121+
{ Move-Item -Path $testFile -Destination $deviceName -ErrorAction Stop } | ShouldBeErrorId "MoveError,Microsoft.PowerShell.Commands.MoveItemCommand"
122+
}
123+
}
124+
125+
It "Rename-Item on Windows rejects Windows reserved device names" -Skip:(-not $IsWindows) {
126+
foreach ($deviceName in $reservedNames)
127+
{
128+
{ Rename-Item -Path $testFile -NewName $deviceName -ErrorAction Stop } | ShouldBeErrorId "RenameError,Microsoft.PowerShell.Commands.RenameItemCommand"
129+
}
130+
}
131+
132+
It "Copy-Item on Unix succeeds with Windows reserved device names" -Skip:($IsWindows) {
133+
foreach ($deviceName in $reservedNames)
134+
{
135+
Copy-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue
136+
Test-Path $deviceName | Should Be $true
137+
}
138+
}
139+
140+
It "Move-Item on Unix succeeds with Windows reserved device names" -Skip:($IsWindows) {
141+
foreach ($deviceName in $reservedNames)
142+
{
143+
Move-Item -Path $testFile -Destination $deviceName -Force -ErrorAction SilentlyContinue
144+
Test-Path $deviceName | Should Be $true
145+
New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue
146+
}
147+
}
148+
149+
It "Rename-Item on Unix succeeds with Windows reserved device names" -Skip:($IsWindows) {
150+
foreach ($deviceName in $reservedNames)
151+
{
152+
Rename-Item -Path $testFile -NewName $deviceName -Force -ErrorAction SilentlyContinue
153+
Test-Path $deviceName | Should Be $true
154+
New-Item -Path $testFile -ItemType File -Force -ErrorAction SilentlyContinue
155+
}
156+
}
106157
}
107158

108159
Context "Validate basic host navigation functionality" {
@@ -667,4 +718,4 @@ Describe "Extended FileSystem Path/Location Cmdlet Provider Tests" -Tags "Featur
667718
catch { $_.FullyQualifiedErrorId | Should Be "Argument,Microsoft.PowerShell.Commands.PopLocationCommand" }
668719
}
669720
}
670-
}
721+
}

0 commit comments

Comments
 (0)