From 16daa026e935a0978410dd53268a48865993a2cf Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 31 Jul 2017 09:28:38 +0300 Subject: [PATCH 1/4] Refactor tests to use ShouldBeErrorId --- .../Unblock-File.Tests.ps1 | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 index 201e32bb92e..04099af23c9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 @@ -7,7 +7,7 @@ function Test-UnblockFile { return $true } } - + return $false } @@ -37,23 +37,11 @@ Describe "Unblock-File" -Tags "CI" { } It "With '-Path': no file exist" { - try { - Unblock-File -Path nofileexist.ttt -ErrorAction Stop - throw "No Exception!" - } - catch { - $_.FullyQualifiedErrorId | Should Be "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand" - } + { Unblock-File -Path nofileexist.ttt -ErrorAction Stop } | ShouldBeErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand" } It "With '-LiteralPath': no file exist" { - try { - Unblock-File -LiteralPath nofileexist.ttt -ErrorAction Stop - throw "No Exception!" - } - catch { - $_.FullyQualifiedErrorId | Should Be "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand" - } + { Unblock-File -LiteralPath nofileexist.ttt -ErrorAction Stop } | ShouldBeErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand" } It "With '-Path': file exist" { From 0215e2f1f9168ee76fe8e8f916ea3855f5f5b5f4 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 31 Jul 2017 09:30:47 +0300 Subject: [PATCH 2/4] Add throw if we can not remove a file stream --- .../namespaces/FileSystemProvider.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 7205120bdd1..2abdce1ff3b 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -8770,7 +8770,11 @@ internal static void DeleteFileStream(string path, string streamName) } string resultPath = path + adjustedStreamName; - NativeMethods.DeleteFile(resultPath); + if (!NativeMethods.DeleteFile(resultPath)) + { + int error = Marshal.GetLastWin32Error(); + throw new Win32Exception(error); + } } internal static void SetZoneOfOrigin(string path, SecurityZone securityZone) From 01bf4443f8e48ae687a6b93e7c0fbcf0ef50c86a Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 31 Jul 2017 09:44:07 +0300 Subject: [PATCH 3/4] Add new test --- .../Unblock-File.Tests.ps1 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 index 04099af23c9..3b4b3631854 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 @@ -53,4 +53,19 @@ Describe "Unblock-File" -Tags "CI" { Unblock-File -LiteralPath $testfilepath Test-UnblockFile | Should Be $true } + + It "Throw if a file is read only" { + $TestFile = Join-Path $TestDrive "testfileunlock.ps1" + Set-Content -Path $TestFile -value 'test' + $ZoneIdentifier = { + [ZoneTransfer] + ZoneId=3 + } + Set-Content -Path $TestFile -Value $ZoneIdentifier -Stream 'Zone.Identifier' + Set-ItemProperty -Path $TestFile -Name IsReadOnly -Value $True + + $TestFileCreated = Get-ChildItem $TestFile + $TestFileCreated.IsReadOnly | Should Be $true + { Unblock-File -LiteralPath $TestFile -ErrorAction Stop } | ShouldBeErrorId "System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.UnblockFileCommand" + } } From 7461f799b62ab0cb75b1a8ae91b1c75b0fa376f8 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 11 Aug 2017 16:35:28 +0300 Subject: [PATCH 4/4] Write non-terminating error --- .../commands/utility/UnblockFile.cs | 11 ++++++++++- .../Unblock-File.Tests.ps1 | 6 ++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs index d726eaa870e..0f3a4cdc09e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Management.Automation; using System.Management.Automation.Internal; @@ -113,7 +114,15 @@ protected override void ProcessRecord() { if (ShouldProcess(path)) { - AlternateDataStreamUtilities.DeleteFileStream(path, "Zone.Identifier"); + try + { + AlternateDataStreamUtilities.DeleteFileStream(path, "Zone.Identifier"); + } + catch (Win32Exception accessException) + { + WriteError(new ErrorRecord(accessException, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path)); + } + } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 index 3b4b3631854..4965571b9ad 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 @@ -54,7 +54,7 @@ Describe "Unblock-File" -Tags "CI" { Test-UnblockFile | Should Be $true } - It "Throw if a file is read only" { + It "Write an error if a file is read only" { $TestFile = Join-Path $TestDrive "testfileunlock.ps1" Set-Content -Path $TestFile -value 'test' $ZoneIdentifier = { @@ -66,6 +66,8 @@ Describe "Unblock-File" -Tags "CI" { $TestFileCreated = Get-ChildItem $TestFile $TestFileCreated.IsReadOnly | Should Be $true - { Unblock-File -LiteralPath $TestFile -ErrorAction Stop } | ShouldBeErrorId "System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.UnblockFileCommand" + + { Unblock-File -LiteralPath $TestFile -ErrorAction SilentlyContinue } | Should Not Throw + $error[0].FullyQualifiedErrorId | Should Be "RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.UnblockFileCommand" } }