-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Change Get-FileHash tests to use raw bytes
#6430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,23 @@ | |
| Describe "Get-FileHash" -Tags "CI" { | ||
|
|
||
| BeforeAll { | ||
| $testDocument = Join-Path -Path $PSScriptRoot -ChildPath assets testablescript.ps1 | ||
| Write-Host $testDocument | ||
| $testDocument = Join-Path -Path $TestDrive -ChildPath "hashable.txt" | ||
| $utf8NoBOM = [System.Text.UTF8Encoding]::new($false) | ||
| [System.IO.File]::WriteAllText($testDocument, "Get-Module`n", $utf8NoBOM) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the PR description:
I wonder, please clarify - how encoding of the static file is changed dynamically depending on a platform? Also I don't understand why we exclude BOM - it seems the cmdlet should works correctly with BOM and without BOM on all platforms. Why we use the WriteAllText overload - last parameter is "append"?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent questions! The default Git configuration on Windows machines means that LF is checked out as CRLF -- which added a CR byte to I would have been happy with leaving the BOM, or even just writing some arbitrary bytes to a file, but we decided that if .NET Core is trying to get away from using BOMs in UTF-8, we should encourage that everywhere. Also, it makes the tests easier to verify against other hashsum calculators in BOM-less UTF-8 environments, which are arguably more prevalent and more likely to have an independent implementation from ours. Finally, the WriteAllText we use is
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The hash should be based on the bytes of the file and the file encoding including BOM is opaque to the cmdlet. We have use -TestCases to have both BOM and no-BOM tests if needed. |
||
| } | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
|
|
@@ -20,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> algorithm" -TestCases $testCases { | ||
| param($algorithm, $hash) | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see that script being used anywhere else, so you should delete that script if it's no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted in latest commit