|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Offline contract tests use real hashes and no engine or network access. |
| 3 | +set -euo pipefail |
| 4 | +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 5 | +fixture=$(mktemp -d) |
| 6 | +trap 'rm -rf "$fixture"' EXIT |
| 7 | +mkdir "$fixture/dist" |
| 8 | +cp "$repo_root/release-targets.txt" "$fixture/targets" |
| 9 | +if command -v sha256sum >/dev/null 2>&1; then |
| 10 | + hash=(sha256sum) |
| 11 | +else |
| 12 | + hash=(shasum -a 256) |
| 13 | +fi |
| 14 | +while read -r os arch asset; do |
| 15 | + [[ -z "$os" || "$os" == \#* ]] && continue |
| 16 | + printf 'fixture for %s/%s\n' "$os" "$arch" > "$fixture/dist/$asset" |
| 17 | +done < "$fixture/targets" |
| 18 | +(cd "$fixture/dist" && "${hash[@]}" threat-detect-*) > "$fixture/valid" |
| 19 | +cp "$fixture/valid" "$fixture/dist/checksums.txt" |
| 20 | + |
| 21 | +validate() { |
| 22 | + bash "$repo_root/scripts/validate-release-checksums.sh" "$fixture/dist" "$fixture/targets" > "$fixture/log" 2>&1 |
| 23 | +} |
| 24 | +reject() { |
| 25 | + if validate; then |
| 26 | + printf 'FAIL: accepted %s\n' "$1" >&2 |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | + if ! grep -q "$2" "$fixture/log"; then |
| 30 | + cat "$fixture/log" >&2 |
| 31 | + printf 'FAIL: wrong diagnostic for %s\n' "$1" >&2 |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | +} |
| 35 | +validate |
| 36 | +# The default matrix path works independently of the caller's working directory. |
| 37 | +(cd "$fixture" && bash "$repo_root/scripts/validate-release-checksums.sh" dist) > "$fixture/log" |
| 38 | +sort -r "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 39 | +validate |
| 40 | + |
| 41 | +: > "$fixture/dist/checksums.txt" |
| 42 | +reject 'empty manifest' 'Missing checksum asset' |
| 43 | +sed '1d' "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 44 | +reject 'missing platform' 'Missing checksum asset' |
| 45 | +cat "$fixture/valid" "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 46 | +reject 'duplicate platform' 'Duplicate checksum asset' |
| 47 | +sed '1s/threat-detect-/unexpected-/' "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 48 | +reject 'unexpected asset' 'Unexpected checksum asset' |
| 49 | +sed '1s/^./g/' "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 50 | +reject 'nonhex digest' 'Invalid checksum line' |
| 51 | +sed '1s/^.//' "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 52 | +reject 'short digest' 'Invalid checksum line' |
| 53 | +tr 'abcdef' 'ABCDEF' < "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 54 | +reject 'uppercase digest' 'Invalid checksum line' |
| 55 | +sed '1s/ / /' "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 56 | +reject 'wrong separator' 'Invalid checksum line' |
| 57 | +sed '1s/$/ extra/' "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 58 | +reject 'extra field' 'Invalid checksum line' |
| 59 | +sed '1s| | ../|' "$fixture/valid" > "$fixture/dist/checksums.txt" |
| 60 | +reject 'path traversal' 'Unexpected checksum asset' |
| 61 | +printf '%s' "$(cat "$fixture/valid")" > "$fixture/dist/checksums.txt" |
| 62 | +reject 'missing final newline' 'must end with a newline' |
| 63 | + |
| 64 | +cp "$fixture/valid" "$fixture/dist/checksums.txt" |
| 65 | +: > "$fixture/targets" |
| 66 | +reject 'empty matrix' 'Release target matrix is empty' |
| 67 | +cat "$repo_root/release-targets.txt" "$repo_root/release-targets.txt" > "$fixture/targets" |
| 68 | +reject 'duplicate matrix' 'Duplicate release' |
| 69 | +printf 'linux amd64 ../binary\n' > "$fixture/targets" |
| 70 | +reject 'malformed matrix' 'Invalid release target' |
| 71 | +cp "$repo_root/release-targets.txt" "$fixture/targets" |
| 72 | +printf 'tampered\n' > "$fixture/dist/threat-detect-linux-amd64" |
| 73 | +reject 'digest mismatch' 'FAILED' |
| 74 | +printf 'fixture for linux/amd64\n' > "$fixture/dist/threat-detect-linux-amd64" |
| 75 | +mv "$fixture/dist/threat-detect-linux-arm64" "$fixture/absent" |
| 76 | +reject 'missing binary' 'threat-detect-linux-arm64' |
| 77 | +mv "$fixture/dist/checksums.txt" "$fixture/absent-checksums" |
| 78 | +reject 'missing manifest' 'checksums.txt' |
| 79 | +printf 'PASS: release checksum contract\n' |
0 commit comments