: nply/foundationpkgs

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
#!/bin/sh
# Offline manifest/staging validation; never installs packages or needs root.
set -eu
root=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
trap 'exit 1' HUP INT TERM
mkdir -p "$work/repo/scripts" "$work/repo/.config" "$work/bootstrap/packages" "$work/bin"
cp "$root/scripts/prepare-ci-host.sh" "$work/repo/scripts/"
export APT_LOG=$work/apt.log
cat >"$work/bin/apt-get" <<'APT'
#!/bin/sh
printf '%s\n' "$@" >"$APT_LOG"
exit 19
APT
chmod +x "$work/bin/apt-get"
export PATH=$work/bin:$PATH
printf 'synthetic deb\n' >"$work/bootstrap/packages/fixture.deb"
if command -v sha256sum >/dev/null 2>&1; then
	sum=$(sha256sum "$work/bootstrap/packages/fixture.deb")
else
	sum=$(shasum -a 256 "$work/bootstrap/packages/fixture.deb")
fi
sum=${sum%% *}
manifest=$work/repo/.config/ci-packages.list
cli=$work/repo/scripts/prepare-ci-host.sh
printf '# example\n\napt build-essential\ndeb packages/fixture.deb %s\n' "$sum" >"$manifest"
sh "$cli" --plan "$work/bootstrap" >"$work/result"
grep -q '^apt build-essential$' "$work/result"
grep -q "^deb packages/fixture.deb $sum$" "$work/result"
[ ! -e "$APT_LOG" ]
fail() {
	if sh "$cli" --plan "$work/bootstrap" >"$work/result" 2>&1; then
		echo 'Invalid manifest unexpectedly accepted' >&2; exit 1
	fi
	[ ! -e "$APT_LOG" ]
}
printf 'changed\n' >>"$work/bootstrap/packages/fixture.deb"
fail
printf 'deb packages/missing.deb %s\n' "$sum" >"$manifest"
fail
printf 'deb packages/../escape.deb %s\n' "$sum" >"$manifest"
fail
printf 'deb /tmp/escape.deb %s\n' "$sum" >"$manifest"
fail
printf 'deb packages/fixture.deb invalid\n' >"$manifest"
fail
printf 'apt --allow-unauthenticated\n' >"$manifest"
fail
printf 'apt git extra\n' >"$manifest"
fail
printf 'unknown git\n' >"$manifest"
fail
printf '# only a comment\n' >"$manifest"
fail
# Check a final record without a newline, and duplicate staging filenames.
printf 'synthetic deb\n' >"$work/bootstrap/packages/fixture.deb"
printf 'deb packages/fixture.deb %s' "$sum" >"$manifest"
sh "$cli" --plan "$work/bootstrap" >"$work/result"
printf '\ndeb packages/fixture.deb %s\n' "$sum" >>"$manifest"
fail
if sh "$cli" --unexpected "$work/bootstrap" >"$work/result" 2>&1; then
	echo 'Unknown setup action unexpectedly accepted' >&2; exit 1
fi
[ ! -e "$APT_LOG" ]
echo 'PASS: host manifest, pinned byte verification, invalid inputs, duplicate detection, plan never invokes APT'