Files
ucvl-zonghengjia-open-source/windows/build/Test-ExactInstallerContents.ps1
T

74 lines
3.6 KiB
PowerShell
Raw Permalink Normal View History

#requires -Version 7.0
param(
[Parameter(Mandatory)][string]$Installer,
[Parameter(Mandatory)][ValidatePattern('^\d+\.\d+\.\d+$')][string]$ExpectedVersion,
[string]$SevenZip = 'C:\Program Files\7-Zip\7z.exe'
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$installerPath = [IO.Path]::GetFullPath($Installer)
if (-not (Test-Path -LiteralPath $installerPath -PathType Leaf)) {
throw "Installer does not exist: $installerPath"
}
if (-not (Test-Path -LiteralPath $SevenZip -PathType Leaf)) {
throw "7-Zip does not exist: $SevenZip"
}
$work = Join-Path ([IO.Path]::GetTempPath()) ('ucvl-exact-installer-' + [Guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $work | Out-Null
try {
$packageInfo = [Diagnostics.FileVersionInfo]::GetVersionInfo($installerPath)
if ($packageInfo.ProductVersion -cne $ExpectedVersion -or $packageInfo.FileVersion -cne $ExpectedVersion) {
throw "Installer metadata version mismatch: expected $ExpectedVersion, product=$($packageInfo.ProductVersion), file=$($packageInfo.FileVersion)"
}
& $SevenZip x '-bd' '-y' ("-o$work") $installerPath '$PLUGINSDIR\payload\VPNPlus.exe' '$PLUGINSDIR\payload\install-manifest.json' | Out-Null
if ($LASTEXITCODE -ne 0) { throw "7-Zip extraction failed with exit code $LASTEXITCODE" }
$payloadRoot = Join-Path $work '$PLUGINSDIR\payload'
$payloadExe = Join-Path $payloadRoot 'VPNPlus.exe'
$manifestPath = Join-Path $payloadRoot 'install-manifest.json'
if (-not (Test-Path -LiteralPath $payloadExe -PathType Leaf) -or
-not (Test-Path -LiteralPath $manifestPath -PathType Leaf)) {
throw 'Production installer does not contain the expected payload executable and manifest.'
}
$payloadInfo = [Diagnostics.FileVersionInfo]::GetVersionInfo($payloadExe)
$expectedFileVersion = "$ExpectedVersion.0"
if ($payloadInfo.ProductVersion -cne $ExpectedVersion -or $payloadInfo.FileVersion -cne $expectedFileVersion) {
throw "Embedded client version mismatch: expected product=$ExpectedVersion file=$expectedFileVersion, product=$($payloadInfo.ProductVersion), file=$($payloadInfo.FileVersion)"
}
$manifest = @(Get-Content -LiteralPath $manifestPath -Raw | ConvertFrom-Json)
$exeRows = @($manifest | Where-Object Path -ceq 'VPNPlus.exe')
if ($exeRows.Count -ne 1) { throw 'Embedded manifest must contain exactly one VPNPlus.exe row.' }
$payloadHash = (Get-FileHash -LiteralPath $payloadExe -Algorithm SHA256).Hash
if ($exeRows[0].Sha256 -cne $payloadHash) {
throw "Embedded manifest hash mismatch: manifest=$($exeRows[0].Sha256), actual=$payloadHash"
}
[pscustomobject]@{
Passed = $true
ExpectedVersion = $ExpectedVersion
Installer = $installerPath
InstallerSha256 = (Get-FileHash -LiteralPath $installerPath -Algorithm SHA256).Hash
InstallerProductVersion = $packageInfo.ProductVersion
PayloadProductVersion = $payloadInfo.ProductVersion
PayloadFileVersion = $payloadInfo.FileVersion
PayloadSha256 = $payloadHash
ManifestSha256 = (Get-FileHash -LiteralPath $manifestPath -Algorithm SHA256).Hash
} | ConvertTo-Json -Depth 4
}
finally {
if (Test-Path -LiteralPath $work) {
$resolved = [IO.Path]::GetFullPath($work)
$tempRoot = [IO.Path]::GetFullPath([IO.Path]::GetTempPath())
if ($resolved.StartsWith($tempRoot, [StringComparison]::OrdinalIgnoreCase) -and
[IO.Path]::GetFileName($resolved).StartsWith('ucvl-exact-installer-', [StringComparison]::Ordinal)) {
Remove-Item -LiteralPath $resolved -Recurse -Force
}
}
}