65 lines
3.4 KiB
PowerShell
65 lines
3.4 KiB
PowerShell
#Requires -Version 5.0
|
|||
|
|
# Offline regression tests. BITS and URL resolution are mocked.
|
||
|
|
$ErrorActionPreference = 'Stop'
|
||
|
|
$DownloadToolsPath = Join-Path (Split-Path $PSScriptRoot -Parent) 'Tools.ps1'
|
||
|
|
$DownloadTokens = $null
|
||
|
|
$DownloadParseErrors = $null
|
||
|
|
$DownloadAst = [Management.Automation.Language.Parser]::ParseFile($DownloadToolsPath, [ref]$DownloadTokens, [ref]$DownloadParseErrors)
|
||
|
|
If ($DownloadParseErrors.Count) { Throw ($DownloadParseErrors | Out-String) }
|
||
|
|
$DownloadDefinition = $DownloadAst.Find({
|
||
|
|
param($Node)
|
||
|
|
$Node -is [Management.Automation.Language.FunctionDefinitionAst] -and $Node.Name -eq 'Download-File'
|
||
|
|
}, $true)
|
||
|
|
. ([scriptblock]::Create($DownloadDefinition.Extent.Text))
|
||
|
|
|
||
|
|
Function Get-AbsoluteURI { param($URL) Return $URL }
|
||
|
|
Function Start-BitsTransfer {
|
||
|
|
[CmdletBinding()]
|
||
|
|
param($Source, $Destination)
|
||
|
|
If ($script:DownloadTestMode -eq 'success') {
|
||
|
|
[IO.File]::WriteAllText($Destination, 'Synthetic download.')
|
||
|
|
Return
|
||
|
|
}
|
||
|
|
[IO.File]::WriteAllBytes($Destination, [byte[]]@())
|
||
|
|
If ($script:DownloadTestMode -eq 'terminating-error') { Throw 'Synthetic BITS connection failure.' }
|
||
|
|
Microsoft.PowerShell.Utility\Write-Error 'Synthetic BITS TLS failure.'
|
||
|
|
}
|
||
|
|
|
||
|
|
$DownloadTestRoot = Join-Path ([IO.Path]::GetTempPath()) ('download-file-tests-' + [guid]::NewGuid().ToString('N'))
|
||
|
|
$null = New-Item -Path $DownloadTestRoot -ItemType Directory
|
||
|
|
Try {
|
||
|
|
$DownloadTestFile = Join-Path $DownloadTestRoot 'collector $test.exe'
|
||
|
|
$script:DownloadTestMode = 'success'
|
||
|
|
$DownloadResult = Download-File -URL 'https://example.invalid/collector.exe' -File $DownloadTestFile
|
||
|
|
If ($DownloadResult -cne $DownloadTestFile -or [IO.File]::ReadAllText($DownloadResult) -cne 'Synthetic download.') {
|
||
|
|
Throw 'Successful download did not return the completed file.'
|
||
|
|
}
|
||
|
|
Write-Host 'PASS: successful download returns its destination.'
|
||
|
|
|
||
|
|
Foreach ($script:DownloadTestMode in @('nonterminating-error','terminating-error')) {
|
||
|
|
# Match an RMM that uses the normal Continue preference and does not pass -ErrorAction.
|
||
|
|
$ErrorActionPreference = 'Continue'
|
||
|
|
$DownloadCaught = $null
|
||
|
|
$DownloadReturnedPaths = @()
|
||
|
|
Try {
|
||
|
|
Download-File -URL 'https://example.invalid/collector.exe' -File $DownloadTestFile |
|
||
|
|
ForEach-Object { $DownloadReturnedPaths += $_ }
|
||
|
|
}
|
||
|
|
Catch { $DownloadCaught = $_ }
|
||
|
|
Finally { $ErrorActionPreference = 'Stop' }
|
||
|
|
If ($null -eq $DownloadCaught -or $DownloadCaught.Exception.Message -notlike '*Synthetic BITS*failure*') {
|
||
|
|
Throw 'Failed transfer must throw and preserve the BITS error.'
|
||
|
|
}
|
||
|
|
If ($DownloadReturnedPaths.Count -ne 0) { Throw 'Failed transfer returned a success path.' }
|
||
|
|
Write-Host "PASS: $script:DownloadTestMode stops the caller and preserves the transfer error."
|
||
|
|
}
|
||
|
|
Write-Host "All Download-File tests passed on PowerShell $($PSVersionTable.PSVersion), $([IntPtr]::Size * 8)-bit."
|
||
|
|
}
|
||
|
|
Finally {
|
||
|
|
$DownloadResolvedRoot = (Resolve-Path -LiteralPath $DownloadTestRoot).Path
|
||
|
|
$DownloadTempRoot = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()).TrimEnd('\') + '\'
|
||
|
|
If (!$DownloadResolvedRoot.StartsWith($DownloadTempRoot, [StringComparison]::OrdinalIgnoreCase) -or
|
||
|
|
(Split-Path $DownloadResolvedRoot -Leaf) -notlike 'download-file-tests-*') { Throw 'Unexpected cleanup directory.' }
|
||
|
|
Remove-Item -LiteralPath $DownloadResolvedRoot -Recurse -Force
|
||
|
|
}
|