# M9.8e negative checks for the package guards (ADR-0086). Copies a staged package # folder, applies one defect at a time and requires each guard to refuse it. # Usage: package-selftest.ps1 -Staged build\packages\ [-DynamicRuntimeImage FILE] param( [Parameter(Mandatory = $true)][string]$Staged, # An image linked against the dynamic MSVC runtime (e.g. build\client-network\evo-client.exe). [string]$DynamicRuntimeImage = '' ) $ErrorActionPreference = 'Stop' . (Join-Path $PSScriptRoot 'package-common.ps1') $utf8 = New-Object Text.UTF8Encoding $false $failures = 0 function Expect-Refused([string]$Label, [scriptblock]$Mutate, [scriptblock]$Check) { $copy = Join-Path ([IO.Path]::GetTempPath()) ("evo-package-selftest-" + [Guid]::NewGuid().ToString('N')) Copy-Item -LiteralPath $Staged -Destination $copy -Recurse try { & $Mutate $copy $refused = $false try { & $Check $copy } catch { $refused = $true; Write-Output "refused ($Label): $($_.Exception.Message)" } if (!$refused) { Write-Output "NOT REFUSED: $Label"; $script:failures++ } } finally { Remove-Item -LiteralPath $copy -Recurse -Force } } $content = { param($p) Assert-PackageContent $p } $secrets = { param($p) Assert-NoSecrets $p } # Baseline: the untouched copy passes every guard. Assert-PackageContent $Staged Assert-NoSecrets $Staged foreach ($binary in $script:PackageImages) { Assert-AllowedImports (Join-Path $Staged $binary) } Expect-Refused 'extra file' { param($p) [IO.File]::WriteAllText((Join-Path $p 'notes.txt'), 'x', $utf8) } $content Expect-Refused 'missing file' { param($p) Remove-Item -LiteralPath (Join-Path $p 'LICENSES\SDL3.txt') } $content Expect-Refused 'missing msquic.dll' { param($p) Remove-Item -LiteralPath (Join-Path $p 'msquic.dll') } $content Expect-Refused 'checksum mismatch' { param($p) [IO.File]::AppendAllText((Join-Path $p 'README.txt'), ' ', $utf8) } $content Expect-Refused 'credential file type' { param($p) [IO.File]::WriteAllText((Join-Path $p 'dev-ca.pem'), 'x', $utf8) } $secrets Expect-Refused 'grant bundle file type' { param($p) [IO.File]::WriteAllBytes((Join-Path $p 'client-grants.bin'), [byte[]]::new(160)) } $secrets Expect-Refused 'private key text' { param($p) [IO.File]::AppendAllText((Join-Path $p 'README.txt'), "-----BEGIN EC PRIVATE KEY-----`nMHcCAQEEIBbF0p3l9QpKl2+abcdefghijklmnopqrstuvwxyzABCDEFG`n", $utf8) } $secrets Expect-Refused 'certificate text' { param($p) [IO.File]::AppendAllText((Join-Path $p 'BUILD-INFO.txt'), "-----BEGIN CERTIFICATE-----`nMIIBkTCB+wIJAKHBfpE7d1cRMA0GCSqGSIb3DQEBCwUAMBExDzANBgNVBAMM`n", $utf8) } $secrets Expect-Refused 'user profile path' { param($p) [IO.File]::AppendAllText((Join-Path $p 'BUILD-INFO.txt'), "C:\Users\someone\build`n", $utf8) } $secrets if ($DynamicRuntimeImage) { $refused = $false try { Assert-AllowedImports $DynamicRuntimeImage } catch { $refused = $true; Write-Output "refused (dynamic runtime): $($_.Exception.Message)" } if (!$refused) { Write-Output 'NOT REFUSED: dynamic runtime image'; $failures++ } } if ($failures -ne 0) { throw "$failures package guard(s) did not refuse a defect" } Write-Output 'package guard self-test passed'