BlueGit

ict rusty-teams

Files

rusty-teams / packaging / linux / verified-download-test.sh

verified-download-test.sh

feat(packaging): Linux AppImage, deb, rpm and Flatpak
metis Samuel David Merlin Kemper · pushed by metis · 2 weeks ago
228f36c5 History
verified-download-test.sh 3.8 KiB Download Raw
1#!/usr/bin/env bash
2# Fast, network-free checks of the verified AppImage tooling cache and override.
3set -euo pipefail
4HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5# shellcheck source=verified-download.sh
6. "$HERE/verified-download.sh"
7
8sha256() {
9 if command -v sha256sum >/dev/null; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi
10}
11fail() { echo "FAIL: $*" >&2; exit 1; }
12
13TMP="$(mktemp -d)"
14trap 'rm -rf "$TMP"' EXIT
15source_asset="$TMP/source.AppImage"
16cache="$TMP/cached.AppImage"
17printf '#!/bin/sh\nexit 0\n' >"$source_asset"
18expected="$(sha256 "$source_asset")"
19# Setup uses the external binary because failure-path tests below shadow the chmod function.
20
21# Offline preseed: a valid 0644 cache must not try the unavailable URL and must become runnable.
22cp "$source_asset" "$cache"
23/bin/chmod 644 "$cache"
24download_verified "file://$TMP/unavailable" "$cache" "$expected" true || fail "offline cache was not reused"
25[[ -x "$cache" ]] || fail "valid preseed did not become executable"
26
27# If a valid but non-executable cache cannot be chmod'd, fail here rather than at invocation.
28/bin/chmod 644 "$cache"
29chmod() { return 1; }
30if err="$(download_verified "file://$TMP/unavailable" "$cache" "$expected" true 2>&1)"; then fail "unfixable cache was accepted"; fi
31case "$err" in *"verified asset is not executable and cannot be chmod'd: $cache"*) ;; *) fail "wrong cache error: $err" ;; esac
32unset -f chmod
33/bin/chmod 755 "$cache"
34
35# A verified, executable cache can be owned by another user; do not try to chmod it again.
36chmod() { fail "chmod called for an already executable cache"; }
37download_verified "file://$TMP/unavailable" "$cache" "$expected" true || fail "executable cache was not reused"
38unset -f chmod
39
40# The type2 runtime is read as data; a valid 0644 preseed must remain usable as-is.
41runtime="$TMP/runtime"
42cp "$source_asset" "$runtime"
43/bin/chmod 644 "$runtime"
44chmod() { fail "chmod called for a readable runtime cache"; }
45download_verified "file://$TMP/unavailable" "$runtime" "$expected" || fail "readable runtime was not reused"
46unset -f chmod
47[[ ! -x "$runtime" ]] || fail "runtime was needlessly made executable"
48
49# A fresh runtime download is verified, readable data rather than an executable program.
50fresh_runtime="$TMP/fresh-runtime"
51download_verified "file://$source_asset" "$fresh_runtime" "$expected" || fail "fresh runtime download failed"
52cmp -s "$source_asset" "$fresh_runtime" || fail "fresh runtime differs from verified asset"
53[[ -r "$fresh_runtime" ]] || fail "fresh runtime is not readable"
54[[ ! -x "$fresh_runtime" ]] || fail "fresh runtime was made executable"
55mode="$(stat -c %a "$fresh_runtime" 2>/dev/null || stat -f %Lp "$fresh_runtime")"
56[[ "$mode" = 644 ]] || fail "fresh runtime mode is $mode, expected 644"
57
58# A corrupt cache is replaced only by bytes with the pinned digest.
59printf 'corrupt' >"$cache"
60download_verified "file://$source_asset" "$cache" "$expected" true || fail "corrupt cache was not replaced"
61cmp -s "$source_asset" "$cache" || fail "replacement differs from verified asset"
62[[ -x "$cache" ]] || fail "replacement is not executable"
63
64wrong_digest="$(printf '%064d' 0)"
65bad="$TMP/bad.AppImage"
66if download_verified "file://$source_asset" "$bad" "$wrong_digest" >/dev/null 2>&1; then fail "wrong digest accepted"; fi
67[[ ! -e "$bad" ]] || fail "wrong-digest download left a cache entry"
68
69verify_appimagetool_override "$cache" "$expected" 1.9.1 x86_64 || fail "valid override rejected"
70/bin/chmod 644 "$cache"
71if verify_appimagetool_override "$cache" "$expected" 1.9.1 x86_64 >/dev/null 2>&1; then fail "non-executable override accepted"; fi
72/bin/chmod 755 "$cache"
73if verify_appimagetool_override "$cache" "$wrong_digest" 1.9.1 x86_64 >/dev/null 2>&1; then fail "wrong-digest override accepted"; fi
74
75echo "verified AppImage tooling ok"