| 1 | #!/usr/bin/env bash |
| 2 | # Fast, network-free checks of the verified AppImage tooling cache and override. |
| 3 | set -euo pipefail |
| 4 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | # shellcheck source=verified-download.sh |
| 6 | . "$HERE/verified-download.sh" |
| 7 | |
| 8 | sha256() { |
| 9 | if command -v sha256sum >/dev/null; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi |
| 10 | } |
| 11 | fail() { echo "FAIL: $*" >&2; exit 1; } |
| 12 | |
| 13 | TMP="$(mktemp -d)" |
| 14 | trap 'rm -rf "$TMP"' EXIT |
| 15 | source_asset="$TMP/source.AppImage" |
| 16 | cache="$TMP/cached.AppImage" |
| 17 | printf '#!/bin/sh\nexit 0\n' >"$source_asset" |
| 18 | expected="$(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. |
| 22 | cp "$source_asset" "$cache" |
| 23 | /bin/chmod 644 "$cache" |
| 24 | download_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" |
| 29 | chmod() { return 1; } |
| 30 | if err="$(download_verified "file://$TMP/unavailable" "$cache" "$expected" true 2>&1)"; then fail "unfixable cache was accepted"; fi |
| 31 | case "$err" in *"verified asset is not executable and cannot be chmod'd: $cache"*) ;; *) fail "wrong cache error: $err" ;; esac |
| 32 | unset -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. |
| 36 | chmod() { fail "chmod called for an already executable cache"; } |
| 37 | download_verified "file://$TMP/unavailable" "$cache" "$expected" true || fail "executable cache was not reused" |
| 38 | unset -f chmod |
| 39 | |
| 40 | # The type2 runtime is read as data; a valid 0644 preseed must remain usable as-is. |
| 41 | runtime="$TMP/runtime" |
| 42 | cp "$source_asset" "$runtime" |
| 43 | /bin/chmod 644 "$runtime" |
| 44 | chmod() { fail "chmod called for a readable runtime cache"; } |
| 45 | download_verified "file://$TMP/unavailable" "$runtime" "$expected" || fail "readable runtime was not reused" |
| 46 | unset -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. |
| 50 | fresh_runtime="$TMP/fresh-runtime" |
| 51 | download_verified "file://$source_asset" "$fresh_runtime" "$expected" || fail "fresh runtime download failed" |
| 52 | cmp -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" |
| 55 | mode="$(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. |
| 59 | printf 'corrupt' >"$cache" |
| 60 | download_verified "file://$source_asset" "$cache" "$expected" true || fail "corrupt cache was not replaced" |
| 61 | cmp -s "$source_asset" "$cache" || fail "replacement differs from verified asset" |
| 62 | [[ -x "$cache" ]] || fail "replacement is not executable" |
| 63 | |
| 64 | wrong_digest="$(printf '%064d' 0)" |
| 65 | bad="$TMP/bad.AppImage" |
| 66 | if 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 | |
| 69 | verify_appimagetool_override "$cache" "$expected" 1.9.1 x86_64 || fail "valid override rejected" |
| 70 | /bin/chmod 644 "$cache" |
| 71 | if 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" |
| 73 | if verify_appimagetool_override "$cache" "$wrong_digest" 1.9.1 x86_64 >/dev/null 2>&1; then fail "wrong-digest override accepted"; fi |
| 74 | |
| 75 | echo "verified AppImage tooling ok" |