2018-02-13 09:23:53 -08:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
# Licensed under the MIT License.
|
2016-07-25 14:01:41 -07:00
|
|
|
Describe "Get-FileHash" -Tags "CI" {
|
2016-03-04 14:38:01 -08:00
|
|
|
|
2017-02-04 06:11:57 +04:00
|
|
|
BeforeAll {
|
|
|
|
|
$testDocument = Join-Path -Path $PSScriptRoot -ChildPath assets testablescript.ps1
|
|
|
|
|
Write-Host $testDocument
|
|
|
|
|
}
|
2016-03-04 14:38:01 -08:00
|
|
|
|
2017-02-04 06:11:57 +04:00
|
|
|
Context "Default result tests" {
|
|
|
|
|
It "Should default to correct algorithm, hash and path" {
|
|
|
|
|
$result = Get-FileHash $testDocument
|
|
|
|
|
$result.Algorithm | Should Be "SHA256"
|
2017-09-25 15:14:39 -07:00
|
|
|
$result.Hash | Should Be "8129a08e5d748ffb9361375677785f96545a1a37619a27608efd76a870787a7a"
|
2017-02-04 06:11:57 +04:00
|
|
|
$result.Path | Should Be $testDocument
|
|
|
|
|
}
|
2016-03-04 14:38:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context "Algorithm tests" {
|
2017-02-04 06:11:57 +04:00
|
|
|
BeforeAll {
|
|
|
|
|
# Keep "sHA1" below! It is for testing that the cmdlet accept a hash algorithm name in any case!
|
|
|
|
|
$testcases =
|
2017-09-25 15:14:39 -07:00
|
|
|
@{ algorithm = "sHA1"; hash = "f262f3d36c279883e81218510c06dc205ef24c9b" },
|
|
|
|
|
@{ algorithm = "SHA256"; hash = "8129a08e5d748ffb9361375677785f96545a1a37619a27608efd76a870787a7a" },
|
|
|
|
|
@{ algorithm = "SHA384"; hash = "77cdffd27d3dcd5810c3d32b4eca656f3ce61cb0081c5ca9bf21be856c0007f9fef2f588bae512a6ecf8dc56618aedc3" },
|
|
|
|
|
@{ algorithm = "SHA512"; hash = "82e3bf7da14b6872b82d67af6580d25123b3612ba2dfcd0746036f609c7752e74af41e97130fbe943ec7b8c61549578176bff522d93dfb2f4b681de9f841c231" },
|
|
|
|
|
@{ algorithm = "MD5"; hash = "2d70c2c2cf8ae23a1a86e64ffce2bbca" }
|
2017-02-04 06:11:57 +04:00
|
|
|
}
|
|
|
|
|
It "Should be able to get the correct hash from <algorithm> algorithm" -TestCases $testCases {
|
|
|
|
|
param($algorithm, $hash)
|
|
|
|
|
$algorithmResult = Get-FileHash $testDocument -Algorithm $algorithm
|
|
|
|
|
$algorithmResult.Hash | Should Be $hash
|
|
|
|
|
}
|
2016-03-04 14:38:01 -08:00
|
|
|
|
2017-02-04 06:11:57 +04:00
|
|
|
It "Should be throw for wrong algorithm name" {
|
|
|
|
|
try {
|
|
|
|
|
Get-FileHash Get-FileHash $testDocument -Algorithm wrongAlgorithm
|
|
|
|
|
throw "No Exception!"
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetFileHashCommand"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-04 14:38:01 -08:00
|
|
|
|
2017-02-04 06:11:57 +04:00
|
|
|
Context "Paths tests" {
|
|
|
|
|
It "With '-Path': no file exist" {
|
|
|
|
|
try {
|
|
|
|
|
Get-FileHash -Path nofileexist.ttt -ErrorAction Stop
|
|
|
|
|
throw "No Exception!"
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
$_.FullyQualifiedErrorId | Should Be "FileNotFound,Microsoft.PowerShell.Commands.GetFileHashCommand"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "With '-LiteralPath': no file exist" {
|
|
|
|
|
try {
|
|
|
|
|
Get-FileHash -LiteralPath nofileexist.ttt -ErrorAction Stop
|
|
|
|
|
throw "No Exception!"
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
$_.FullyQualifiedErrorId | Should Be "FileNotFound,Microsoft.PowerShell.Commands.GetFileHashCommand"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "With '-Path': file exist" {
|
|
|
|
|
$result = Get-FileHash -Path $testDocument
|
|
|
|
|
$result.Path | Should Be $testDocument
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "With '-LiteralPath': file exist" {
|
|
|
|
|
$result = Get-FileHash -LiteralPath $testDocument
|
|
|
|
|
$result.Path | Should Be $testDocument
|
|
|
|
|
}
|
2016-03-04 14:38:01 -08:00
|
|
|
}
|
|
|
|
|
}
|