From 3032477390070aabd038b28b33d5f75c0930163f Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 19 Mar 2018 09:44:40 -0700 Subject: [PATCH 1/3] [feature] Change filehash tests to use raw bytes --- .../Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 index b05d6ac3a2d..296e5056047 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 @@ -3,8 +3,10 @@ Describe "Get-FileHash" -Tags "CI" { BeforeAll { - $testDocument = Join-Path -Path $PSScriptRoot -ChildPath assets testablescript.ps1 + $testDocument = Join-Path -Path $TestDrive -ChildPath "hashable.txt" Write-Host $testDocument + # Encode bytes directly to avoid platform issues + [System.IO.File]::WriteAllBytes($testDocument, @(0xef, 0xbb, 0xbf, 0x47, 0x65, 0x74, 0x2d, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x0a)) } Context "Default result tests" { From 3d2bedd26fd5ba6b40a2fd87266e08322f56a62f Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 19 Mar 2018 11:44:44 -0700 Subject: [PATCH 2/3] Remove testablescript.ps1 from Get-FileHash tests --- .../Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 | 3 +-- .../Microsoft.PowerShell.Utility/assets/testablescript.ps1 | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 test/powershell/Modules/Microsoft.PowerShell.Utility/assets/testablescript.ps1 diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 index 296e5056047..ef66a63671b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 @@ -4,8 +4,7 @@ Describe "Get-FileHash" -Tags "CI" { BeforeAll { $testDocument = Join-Path -Path $TestDrive -ChildPath "hashable.txt" - Write-Host $testDocument - # Encode bytes directly to avoid platform issues + # Encode bytes of "Get-Module`n" directly to avoid platform issues [System.IO.File]::WriteAllBytes($testDocument, @(0xef, 0xbb, 0xbf, 0x47, 0x65, 0x74, 0x2d, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x0a)) } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/assets/testablescript.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/assets/testablescript.ps1 deleted file mode 100644 index a506e0e8ad9..00000000000 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/assets/testablescript.ps1 +++ /dev/null @@ -1 +0,0 @@ -Get-Module From dd1b49040cb43fe6e67d04f55582566b50bf12a5 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 19 Mar 2018 16:37:16 -0700 Subject: [PATCH 3/3] Use BOM-less UTF8 input for Get-FileHash --- .../Get-FileHash.Tests.ps1 | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 index ef66a63671b..45b063002ea 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 @@ -4,15 +4,22 @@ Describe "Get-FileHash" -Tags "CI" { BeforeAll { $testDocument = Join-Path -Path $TestDrive -ChildPath "hashable.txt" - # Encode bytes of "Get-Module`n" directly to avoid platform issues - [System.IO.File]::WriteAllBytes($testDocument, @(0xef, 0xbb, 0xbf, 0x47, 0x65, 0x74, 0x2d, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x0a)) + $utf8NoBOM = [System.Text.UTF8Encoding]::new($false) + [System.IO.File]::WriteAllText($testDocument, "Get-Module`n", $utf8NoBOM) + } + + Context "Validate platform encoding" { + # If this test fails, then the problem lies outside the Get-FileHash implementation + It "Should have the bytes in the file as expected" { + [System.IO.File]::ReadAllBytes($testDocument) | Should -BeExactly @(0x47, 0x65, 0x74, 0x2d, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x0a) + } } Context "Default result tests" { It "Should default to correct algorithm, hash and path" { $result = Get-FileHash $testDocument $result.Algorithm | Should Be "SHA256" - $result.Hash | Should Be "8129a08e5d748ffb9361375677785f96545a1a37619a27608efd76a870787a7a" + $result.Hash | Should Be "41620f6c9f3531722efe90aed9abbc1d1b31788aa9141982030d3dde199f770c" $result.Path | Should Be $testDocument } } @@ -21,11 +28,11 @@ Describe "Get-FileHash" -Tags "CI" { BeforeAll { # Keep "sHA1" below! It is for testing that the cmdlet accept a hash algorithm name in any case! $testcases = - @{ algorithm = "sHA1"; hash = "f262f3d36c279883e81218510c06dc205ef24c9b" }, - @{ algorithm = "SHA256"; hash = "8129a08e5d748ffb9361375677785f96545a1a37619a27608efd76a870787a7a" }, - @{ algorithm = "SHA384"; hash = "77cdffd27d3dcd5810c3d32b4eca656f3ce61cb0081c5ca9bf21be856c0007f9fef2f588bae512a6ecf8dc56618aedc3" }, - @{ algorithm = "SHA512"; hash = "82e3bf7da14b6872b82d67af6580d25123b3612ba2dfcd0746036f609c7752e74af41e97130fbe943ec7b8c61549578176bff522d93dfb2f4b681de9f841c231" }, - @{ algorithm = "MD5"; hash = "2d70c2c2cf8ae23a1a86e64ffce2bbca" } + @{ algorithm = "sHA1"; hash = "0c483659b1f2d5a8f116211de8f58bf45893cffb" }, + @{ algorithm = "SHA256"; hash = "41620f6c9f3531722efe90aed9abbc1d1b31788aa9141982030d3dde199f770c" }, + @{ algorithm = "SHA384"; hash = "ec4c4d4f0b2a79f216118c5a5059b8ce061097ba9161be5890c098aaeb5db169c13dae0a6f855c9a589cd11df47d0c87" }, + @{ algorithm = "SHA512"; hash = "6aba8ba8b619100a6829beb940d9d77e4a8197fdcac2d0fe5ad6c2758dacc5a59774195fd8a7a92780b7582a966b81ca0c1576c0044c5af7be20f5ccf425bd76" }, + @{ algorithm = "MD5"; hash = "f9d78bd059ab162bea21eb02badde001" } } It "Should be able to get the correct hash from algorithm" -TestCases $testCases { param($algorithm, $hash)