From f4ea391492e6b7f6f929fc165bd6c3cfed1849a9 Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Mon, 24 Jul 2017 10:56:29 -0700 Subject: [PATCH 1/6] Increase test coverage for Rename-Item and Move-Item --- .../FileSystem.Tests.ps1 | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 94516097e65..fef7cb5bf76 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -65,6 +65,24 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { $existsAfter | Should Be $false } + It "Verify Rename-Item for file" { + Rename-Item -Path $testFile -NewName $newTestFile + $testFile | Should Not Exist + $newTestFile | Should Exist + } + + It "Verify Rename-Item for directory" { + Rename-Item -Path $testDir -NewName $newTestDir + $testDir | Should Not Exist + $newTestDir | Should Exist + } + + It "Verify Rename-Item will not rename to an existing name" { + { Rename-Item -Path $testFile -NewName $testDir -ErrorAction Stop } | ShouldBeErrorId "RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand" + $Error[0].Exception | Should BeOfType System.IO.IOException + $testFile | Should Exist + } + It "Verify Copy-Item" { $newFile = Copy-Item -Path $testFile -Destination $newTestFile -PassThru $fileExists = Test-Path $newTestFile @@ -72,7 +90,29 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { $newFile.Name | Should Be $newTestFile } - It "Verify Move-Item" { + It "Verify Move-Item for file" { + Move-Item -Path $testFile -Destination $testDir + $testFile | Should Not Exist + "$testDir/$testFile" | Should Exist + } + + It "Verify Move-Item for directory" { + $destDir = "DestinationDirectory" + New-Item -Path $destDir -ItemType Directory >$null + Move-Item -Path $testFile -Destination $testDir + Move-Item -Path $testDir -Destination $destDir + $testDir | Should Not Exist + "$destDir/$testDir" | Should Exist + "$destDir/$testDir/$testFile" | Should Exist + } + + It "Verity Move-Item will not move to an existing file" { + { Move-Item -Path $testDir -Destination $testFile -ErrorAction Stop } | ShouldBeErrorId "MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand" + $Error[0].Exception | Should BeOfType System.IO.IOException + $testDir | Should Exist + } + + It "Verify Move-Item as substitue for Rename-Item" { $newFile = Move-Item -Path $testFile -Destination $newTestFile -PassThru $fileExists = Test-Path $newTestFile $fileExists | Should Be $true From e8d304faaeb2706b9bcd813830dc344289685542 Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Tue, 25 Jul 2017 13:13:05 -0700 Subject: [PATCH 2/6] Changes per code review. --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index fef7cb5bf76..a7456de87e4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -66,13 +66,13 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { } It "Verify Rename-Item for file" { - Rename-Item -Path $testFile -NewName $newTestFile + Rename-Item -Path $testFile -NewName $newTestFile -ErrorAction Stop $testFile | Should Not Exist $newTestFile | Should Exist } It "Verify Rename-Item for directory" { - Rename-Item -Path $testDir -NewName $newTestDir + Rename-Item -Path $testDir -NewName $newTestDir -ErrorAction Stop $testDir | Should Not Exist $newTestDir | Should Exist } @@ -91,14 +91,14 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { } It "Verify Move-Item for file" { - Move-Item -Path $testFile -Destination $testDir + Move-Item -Path $testFile -Destination $testDir -ErrorAction Stop $testFile | Should Not Exist "$testDir/$testFile" | Should Exist } It "Verify Move-Item for directory" { $destDir = "DestinationDirectory" - New-Item -Path $destDir -ItemType Directory >$null + New-Item -Path $destDir -ItemType Directory -ErrorAction Stop >$null Move-Item -Path $testFile -Destination $testDir Move-Item -Path $testDir -Destination $destDir $testDir | Should Not Exist From 26595b8697c7f87bca75ae9250ed9653ad0a6ce1 Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Mon, 31 Jul 2017 18:26:45 -0700 Subject: [PATCH 3/6] Fix typos --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index a7456de87e4..3584dafedd7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -106,13 +106,13 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { "$destDir/$testDir/$testFile" | Should Exist } - It "Verity Move-Item will not move to an existing file" { + It "Verify Move-Item will not move to an existing file" { { Move-Item -Path $testDir -Destination $testFile -ErrorAction Stop } | ShouldBeErrorId "MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand" $Error[0].Exception | Should BeOfType System.IO.IOException $testDir | Should Exist } - It "Verify Move-Item as substitue for Rename-Item" { + It "Verify Move-Item as substitute for Rename-Item" { $newFile = Move-Item -Path $testFile -Destination $newTestFile -PassThru $fileExists = Test-Path $newTestFile $fileExists | Should Be $true From 657a4944e00f6b2553f27c23b68ed87dc991880c Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Mon, 31 Jul 2017 18:29:53 -0700 Subject: [PATCH 4/6] Add access-denied tests --- .../FileSystem.Tests.ps1 | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 3584dafedd7..0ec93711b79 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -195,6 +195,49 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { } } + Context "Validate behavior when access is denied" { + BeforeAll { + $powershell = "powershell.exe" + #$protectedPath = "C:\Windows\Temp" + $protectedPath = Join-Path ([environment]::GetFolderPath("windows")) "appcompat" "Programs" + $protectedPath2 = Join-Path $protectedPath "Install" + $newItemPath = Join-Path $protectedPath "foo" + $errFile = "error.txt" + $doneFile = "done.txt" + } + + BeforeEach { + + } + + AfterEach { + Remove-Item -Force $errFile -ErrorAction SilentlyContinue + Remove-Item -Force $doneFile -ErrorAction SilentlyContinue + } + + It "Access-denied test for '" -TestCases @( + @{cmdline = "Get-Item $protectedPath2"; expectedError = "ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetItemCommand"} + @{cmdline = "Get-ChildItem $protectedPath"; expectedError = "DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand"} + @{cmdline = "New-Item -Type File -Path $newItemPath"; expectedError = "NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand"} + @{cmdline = "Rename-Item -Path $protectedPath -NewName bar"; expectedError = "RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand"}, + @{cmdline = "Move-Item -Path $protectedPath -Destination bar"; expectedError = "MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand"}, + @{cmdline = "Remove-Item -Path $protectedPath"; expectedError = "RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand"} + ) { + param ($cmdline, $expectedError) + + runas.exe /trustlevel:0x20000 "$powershell -nop -c try { $cmdline -ErrorAction Stop } catch { `$_.FullyQualifiedErrorId | Out-File $errFile }; New-Item -Type File -Path $doneFile" + $startTime = Get-Date + while (((Get-Date) - $startTime).TotalSeconds -lt 5 -and -not (Test-Path $doneFile)) + { + Start-Sleep -Milliseconds 100 + } + + $errFile | Should Exist + $err = Get-Content $errFile + $err | Should Be $expectedError + } + } + Context "Validate basic host navigation functionality" { BeforeAll { #build semi-complex directory structure to test navigation within From ec3b580a14f2b8430220bb1b274d289bcfbb7d79 Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Mon, 31 Jul 2017 18:59:18 -0700 Subject: [PATCH 5/6] Add -skip on Unix for access-denied tests. --- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 0ec93711b79..db09fefa69c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -215,7 +215,7 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { Remove-Item -Force $doneFile -ErrorAction SilentlyContinue } - It "Access-denied test for '" -TestCases @( + It "Access-denied test for '" -Skip:(-not $IsWindows) -TestCases @( @{cmdline = "Get-Item $protectedPath2"; expectedError = "ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetItemCommand"} @{cmdline = "Get-ChildItem $protectedPath"; expectedError = "DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand"} @{cmdline = "New-Item -Type File -Path $newItemPath"; expectedError = "NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand"} From c7240df6ba53e0f9f627015c3384e5f6c9beaa67 Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Mon, 31 Jul 2017 21:21:56 -0700 Subject: [PATCH 6/6] Cleanup to fix CI failure. --- .../FileSystem.Tests.ps1 | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index db09fefa69c..e63ae2b5c1c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -197,19 +197,17 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { Context "Validate behavior when access is denied" { BeforeAll { - $powershell = "powershell.exe" - #$protectedPath = "C:\Windows\Temp" - $protectedPath = Join-Path ([environment]::GetFolderPath("windows")) "appcompat" "Programs" - $protectedPath2 = Join-Path $protectedPath "Install" - $newItemPath = Join-Path $protectedPath "foo" + if ($IsWindows) + { + $powershell = "powershell.exe" + $protectedPath = Join-Path ([environment]::GetFolderPath("windows")) "appcompat" "Programs" + $protectedPath2 = Join-Path $protectedPath "Install" + $newItemPath = Join-Path $protectedPath "foo" + } $errFile = "error.txt" $doneFile = "done.txt" } - BeforeEach { - - } - AfterEach { Remove-Item -Force $errFile -ErrorAction SilentlyContinue Remove-Item -Force $doneFile -ErrorAction SilentlyContinue