microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0bce418ef9a17e5e311d7cc01dc4e8ac699aa51f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/tests/Get-VerifiedDownload.Tests.ps1

163lines · modecode

1#Requires -Modules Pester
2
3BeforeAll {
4 . $PSScriptRoot/../lib/Get-VerifiedDownload.ps1
5}
6
7Describe 'Get-FileHashValue' {
8 It 'Returns uppercase hash string for valid file' {
9 $tempFile = New-TemporaryFile
10 try {
11 'test content' | Set-Content -Path $tempFile.FullName -NoNewline
12 $result = Get-FileHashValue -Path $tempFile.FullName -Algorithm 'SHA256'
13 $result | Should -BeOfType [string]
14 $result | Should -Match '^[A-F0-9]{64}$'
15 } finally {
16 Remove-Item -Path $tempFile.FullName -Force -ErrorAction SilentlyContinue
17 }
18 }
19
20 It 'Supports SHA384 algorithm' {
21 $tempFile = New-TemporaryFile
22 try {
23 'test' | Set-Content -Path $tempFile.FullName -NoNewline
24 $result = Get-FileHashValue -Path $tempFile.FullName -Algorithm 'SHA384'
25 $result | Should -Match '^[A-F0-9]{96}$'
26 } finally {
27 Remove-Item -Path $tempFile.FullName -Force -ErrorAction SilentlyContinue
28 }
29 }
30
31 It 'Supports SHA512 algorithm' {
32 $tempFile = New-TemporaryFile
33 try {
34 'test' | Set-Content -Path $tempFile.FullName -NoNewline
35 $result = Get-FileHashValue -Path $tempFile.FullName -Algorithm 'SHA512'
36 $result | Should -Match '^[A-F0-9]{128}$'
37 } finally {
38 Remove-Item -Path $tempFile.FullName -Force -ErrorAction SilentlyContinue
39 }
40 }
41}
42
43Describe 'Test-HashMatch' {
44 It 'Returns true when hashes match (case-insensitive)' {
45 $result = Test-HashMatch -ComputedHash 'ABC123' -ExpectedHash 'abc123'
46 $result | Should -BeTrue
47 }
48
49 It 'Returns false when hashes do not match' {
50 $result = Test-HashMatch -ComputedHash 'ABC123' -ExpectedHash 'DEF456'
51 $result | Should -BeFalse
52 }
53
54 It 'Returns true when hashes match exactly' {
55 $result = Test-HashMatch -ComputedHash 'ABC123DEF456' -ExpectedHash 'ABC123DEF456'
56 $result | Should -BeTrue
57 }
58}
59
60Describe 'Get-DownloadTargetPath' {
61 BeforeAll {
62 $script:testDir = [System.IO.Path]::GetTempPath().TrimEnd([System.IO.Path]::DirectorySeparatorChar)
63 }
64
65 It 'Uses filename from URL when FileName not specified' {
66 $result = Get-DownloadTargetPath -Url 'https://example.com/file.zip' -DestinationDirectory $script:testDir
67 $expected = [System.IO.Path]::Combine($script:testDir, 'file.zip')
68 $result | Should -Be $expected
69 }
70
71 It 'Uses explicit FileName when specified' {
72 $result = Get-DownloadTargetPath -Url 'https://example.com/file.zip' -DestinationDirectory $script:testDir -FileName 'custom.zip'
73 $expected = [System.IO.Path]::Combine($script:testDir, 'custom.zip')
74 $result | Should -Be $expected
75 }
76
77 It 'Handles URL with query parameters' {
78 $result = Get-DownloadTargetPath -Url 'https://example.com/file.zip?token=abc' -DestinationDirectory $script:testDir
79 $expected = [System.IO.Path]::Combine($script:testDir, 'file.zip')
80 $result | Should -Be $expected
81 }
82}
83
84Describe 'Test-ExistingFileValid' {
85 It 'Returns true when file exists with matching hash' {
86 $tempFile = New-TemporaryFile
87 try {
88 'known content' | Set-Content -Path $tempFile.FullName -NoNewline
89 $expectedHash = (Get-FileHash -Path $tempFile.FullName -Algorithm SHA256).Hash
90 $result = Test-ExistingFileValid -Path $tempFile.FullName -ExpectedHash $expectedHash -Algorithm 'SHA256'
91 $result | Should -BeTrue
92 } finally {
93 Remove-Item -Path $tempFile.FullName -Force -ErrorAction SilentlyContinue
94 }
95 }
96
97 It 'Returns false when file exists with non-matching hash' {
98 $tempFile = New-TemporaryFile
99 try {
100 'some content' | Set-Content -Path $tempFile.FullName -NoNewline
101 $result = Test-ExistingFileValid -Path $tempFile.FullName -ExpectedHash 'INVALID_HASH' -Algorithm 'SHA256'
102 $result | Should -BeFalse
103 } finally {
104 Remove-Item -Path $tempFile.FullName -Force -ErrorAction SilentlyContinue
105 }
106 }
107
108 It 'Returns false when file does not exist' {
109 $nonexistentPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'nonexistent-dir-12345', 'file.txt')
110 $result = Test-ExistingFileValid -Path $nonexistentPath -ExpectedHash 'ABC123' -Algorithm 'SHA256'
111 $result | Should -BeFalse
112 }
113}
114
115Describe 'New-DownloadResult' {
116 It 'Creates hashtable with all properties' {
117 $result = New-DownloadResult -Path 'C:\file.zip' -WasDownloaded $true -HashVerified $true
118 $result | Should -BeOfType [hashtable]
119 $result.Path | Should -Be 'C:\file.zip'
120 $result.WasDownloaded | Should -BeTrue
121 $result.HashVerified | Should -BeTrue
122 }
123
124 It 'Handles false values correctly' {
125 $result = New-DownloadResult -Path 'C:\cached.zip' -WasDownloaded $false -HashVerified $true
126 $result.WasDownloaded | Should -BeFalse
127 $result.HashVerified | Should -BeTrue
128 }
129}
130
131Describe 'Get-ArchiveType' {
132 It 'Returns tar.gz for .tar.gz files' {
133 $result = Get-ArchiveType -Path 'archive.tar.gz'
134 $result | Should -Be 'tar.gz'
135 }
136
137 It 'Returns tar.gz for .tgz files' {
138 $result = Get-ArchiveType -Path 'archive.tgz'
139 $result | Should -Be 'tar.gz'
140 }
141
142 It 'Returns zip for .zip files' {
143 $result = Get-ArchiveType -Path 'archive.zip'
144 $result | Should -Be 'zip'
145 }
146
147 It 'Returns unknown for unrecognized extensions' {
148 $result = Get-ArchiveType -Path 'file.txt'
149 $result | Should -Be 'unknown'
150 }
151
152 It 'Handles paths with directories' {
153 $result = Get-ArchiveType -Path 'C:\downloads\archive.tar.gz'
154 $result | Should -Be 'tar.gz'
155 }
156}
157
158Describe 'Test-TarAvailable' {
159 It 'Returns boolean indicating tar availability' {
160 $result = Test-TarAvailable
161 $result | Should -BeOfType [bool]
162 }
163}
164