Files
breeze/tests/apt_repo_test.sh
waozi 3ee425cfe6 Keep the APT repository test working under a long TMPDIR
gpg-agent listens on sockets inside each GnuPG home, and Unix socket
paths end at 107 bytes. Under an agent sandbox's long TMPDIR the agent
could not start, and the test exited 2 with no output because key
generation discarded gpg's errors. The test now uses /tmp when TMPDIR is
long, and shows gpg's error when a test key cannot be created.
2026-11-06 22:22:17 -03:00

83 lines
3.6 KiB
Bash

#!/bin/sh
# scripts/build-apt-repo.sh signs a repository apt accepts, and refuses a key
# that differs from the published keyring or a deb that differs from its name.
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
# gpg-agent listens on sockets inside each GnuPG home, here and in
# build-apt-repo.sh, and Unix socket paths end at 107 bytes. A long TMPDIR
# (such as an agent sandbox's) would stop the agent, so use /tmp instead.
tmp=${TMPDIR:-/tmp}
[ "${#tmp}" -le 48 ] || { TMPDIR=/tmp && export TMPDIR; }
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT INT TERM
version=9.8.7
fail() {
printf 'apt repo test: %s\n' "$1" >&2
exit 1
}
make_deb() {
pkg=$work/pkg-$1
mkdir -p -m 755 "$pkg/DEBIAN" "$pkg/usr/bin"
printf '#!/bin/sh\n' > "$pkg/usr/bin/breeze"
chmod 755 "$pkg/DEBIAN" "$pkg/usr/bin/breeze"
printf 'Package: inbe\nVersion: %s\nArchitecture: %s\nMaintainer: Test <[email protected]>\nDescription: test\n' \
"$2" "$1" > "$pkg/DEBIAN/control"
dpkg-deb --root-owner-group --build "$pkg" "$work/debs/inbe_${version}_$1.deb" >/dev/null
}
new_key() {
GNUPGHOME=$work/gpg-$1 && export GNUPGHOME
mkdir -m 700 "$GNUPGHOME"
gpg --batch --quiet --passphrase '' --quick-gen-key "test $1 <[email protected]>" rsa2048 sign never 2>"$work/gpg-$1.log" ||
{ cat "$work/gpg-$1.log" >&2; fail "could not create the $1 test key"; }
gpg --export > "$work/$1.gpg"
gpg --armor --export-secret-keys
unset GNUPGHOME
}
mkdir -p "$work/debs"
make_deb amd64 "$version"
make_deb arm64 "$version"
key=$(new_key good)
other=$(new_key other)
APT_DEB_DIR=$work/debs APT_KEYRING=$work/good.gpg APT_SIGNING_KEY=$key \
sh "$root/scripts/build-apt-repo.sh" "$work/repo" "$version" >/dev/null
for path in dists/stable/InRelease dists/stable/Release.gpg breeze-archive-keyring.gpg inbe.sources \
dists/stable/main/binary-amd64/Packages dists/stable/main/binary-arm64/Packages \
pool/main/i/inbe/inbe_${version}_amd64.deb pool/main/i/inbe/inbe_${version}_arm64.deb; do
[ -s "$work/repo/$path" ] || fail "missing $path"
done
if APT_DEB_DIR=$work/debs APT_KEYRING=$work/good.gpg APT_SIGNING_KEY=$other \
sh "$root/scripts/build-apt-repo.sh" "$work/bad-key" "$version" 2>/dev/null; then
fail "accepted a key that differs from the keyring"
fi
make_deb arm64 9.8.6
if APT_DEB_DIR=$work/debs APT_KEYRING=$work/good.gpg APT_SIGNING_KEY=$key \
sh "$root/scripts/build-apt-repo.sh" "$work/bad-deb" "$version" 2>/dev/null; then
fail "accepted a deb whose version differs from its name"
fi
# apt itself verifies the signature and finds the package, without root.
apt_dir=$work/apt
mkdir -p "$apt_dir/etc/sources.list.d" "$apt_dir/etc/preferences.d" "$apt_dir/state/lists/partial" \
"$apt_dir/cache/archives/partial"
: > "$apt_dir/state/status"
sed -e "s#https://breeze.waozi.xyz/apt#file://$work/repo#" \
-e "s#/usr/share/keyrings/breeze-archive-keyring.gpg#$work/good.gpg#" \
"$work/repo/inbe.sources" > "$apt_dir/etc/sources.list.d/inbe.sources"
apt_opts="-o Dir::Etc=$apt_dir/etc -o Dir::Etc::sourcelist=/dev/null -o Dir::State=$apt_dir/state
-o Dir::State::status=$apt_dir/state/status -o Dir::Cache=$apt_dir/cache -o Debug::NoLocking=1
-o APT::Architecture=amd64"
# shellcheck disable=SC2086
apt-get $apt_opts update >"$work/apt-update.log" 2>&1 || { cat "$work/apt-update.log" >&2; fail "apt update failed"; }
grep -q -E '^(W|E):' "$work/apt-update.log" && { cat "$work/apt-update.log" >&2; fail "apt update warned"; }
# shellcheck disable=SC2086
candidate=$(apt-cache $apt_opts policy inbe | awk '/Candidate:/ { print $2 }')
[ "$candidate" = "$version" ] || fail "apt candidate is '$candidate', expected $version"
echo "apt repo test passed"