Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
Get-FileHash.Tests.ps196 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-FileHash" -Tags "CI" {4 5    BeforeAll {6        $testDocument = Join-Path -Path $TestDrive -ChildPath "hashable.txt"7        $utf8NoBOM = [System.Text.UTF8Encoding]::new($false)8        [System.IO.File]::WriteAllText($testDocument, "Get-Module`n", $utf8NoBOM)9    }10 11    Context "Validate platform encoding" {12        # If this test fails, then the problem lies outside the Get-FileHash implementation13        It "Should have the bytes in the file as expected" {14            [System.IO.File]::ReadAllBytes($testDocument) | Should -BeExactly @(0x47, 0x65, 0x74, 0x2d, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x0a)15        }16    }17 18    Context "Default result tests" {19        It "Should default to correct algorithm, hash and path" {20            $result = Get-FileHash $testDocument21            $result.Algorithm | Should -Be "SHA256"22            $result.Hash | Should -Be "41620f6c9f3531722efe90aed9abbc1d1b31788aa9141982030d3dde199f770c"23            $result.Path | Should -Be $testDocument24        }25 26        It "Should write non-terminating error if argument is a folder" {27            $result = $PSHOME, "${pshome}\pwsh.dll" | Get-FileHash -ErrorVariable errorVariable -ErrorAction SilentlyContinue28            $result.Count | Should -Be 129            $errorVariable.FullyQualifiedErrorId | Should -BeExactly "UnauthorizedAccessError,Microsoft.PowerShell.Commands.GetFileHashCommand"30        }31 32        It "Should write non-terminating error if a file is locked" -Skip:(-not $IsWindows) {33            $pagefilePath = (Get-CimInstance -ClassName Win32_PageFileusage).Name34            $result = $pagefilePath, "${pshome}\pwsh.dll" | Get-FileHash -ErrorVariable errorVariable -ErrorAction SilentlyContinue35            $result.Count | Should -Be 136            $errorVariable.FullyQualifiedErrorId | Should -BeExactly "FileReadError,Microsoft.PowerShell.Commands.GetFileHashCommand"37        }38    }39 40    Context "Algorithm tests" {41        BeforeAll {42            # Keep "sHA1" below! It is for testing that the cmdlet accept a hash algorithm name in any case!43            $testcases =44                @{ algorithm = "sHA1";   hash = "0c483659b1f2d5a8f116211de8f58bf45893cffb" },45                @{ algorithm = "SHA256"; hash = "41620f6c9f3531722efe90aed9abbc1d1b31788aa9141982030d3dde199f770c" },46                @{ algorithm = "SHA384"; hash = "ec4c4d4f0b2a79f216118c5a5059b8ce061097ba9161be5890c098aaeb5db169c13dae0a6f855c9a589cd11df47d0c87" },47                @{ algorithm = "SHA512"; hash = "6aba8ba8b619100a6829beb940d9d77e4a8197fdcac2d0fe5ad6c2758dacc5a59774195fd8a7a92780b7582a966b81ca0c1576c0044c5af7be20f5ccf425bd76" },48                @{ algorithm = "MD5";    hash = "f9d78bd059ab162bea21eb02badde001" }49        }50        It "Should be able to get the correct hash from <algorithm> algorithm" -TestCases $testCases {51            param($algorithm, $hash)52            $algorithmResult = Get-FileHash $testDocument -Algorithm $algorithm53            $algorithmResult.Hash | Should -Be $hash54        }55 56        It "Should be throw for wrong algorithm name" {57            { Get-FileHash Get-FileHash $testDocument -Algorithm wrongAlgorithm } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetFileHashCommand"58        }59    }60 61    Context "Paths tests" {62        It "With '-Path': no file exist" {63            { Get-FileHash -Path nofileexist.ttt -ErrorAction Stop } | Should -Throw -ErrorId "FileNotFound,Microsoft.PowerShell.Commands.GetFileHashCommand"64        }65 66        It "With '-LiteralPath': no file exist" {67            { Get-FileHash -LiteralPath nofileexist.ttt -ErrorAction Stop } | Should -Throw -ErrorId "FileNotFound,Microsoft.PowerShell.Commands.GetFileHashCommand"68        }69 70        It "With '-Path': file exist" {71            $result = Get-FileHash -Path $testDocument72            $result.Path | Should -Be $testDocument73        }74 75        It "With '-LiteralPath': file exist" {76            $result = Get-FileHash -LiteralPath $testDocument77            $result.Path | Should -Be $testDocument78        }79    }80 81    Context "File should be closed before Get-FileHash writes pipeline output" {82        It "Should be able to edit the file without 'file is in use' exceptions" {83            # This test runs against a copy of the document84            # because it involves renaming it,85            # and that might break tests added later on.86            $testDocumentCopy = "${testDocument}-copy"87            Copy-Item -Path $testdocument -Destination $testDocumentCopy88 89            $newPath = Get-FileHash -Path $testDocumentCopy | Rename-Item -NewName {$_.Hash} -PassThru90            $newPath.FullName | Should -Exist91 92            Remove-Item -Path $testDocumentCopy -Force -ErrorAction SilentlyContinue93        }94    }95}96