2026-11-19 21:03:27 -08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
## verify-image-arch.test.sh - scripts/verify-image-arch.py against a stub `docker` that serves
|
|
|
|
|
## fabricated ELF headers through `docker cp -L ctr:path -` (a tar stream), like the real one.
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
HERE=$(cd "$(dirname "$0")/.." && pwd)
|
|
|
|
|
T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
|
|
|
|
|
fail=0
|
|
|
|
|
chk() { if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: got '$2' want '$3'"; fail=1; fi; }
|
|
|
|
|
|
|
|
|
|
cat >"$T/mktar.py" <<'PY'
|
|
|
|
|
import io, os, struct, sys, tarfile
|
|
|
|
|
path = sys.argv[1]
|
|
|
|
|
spec = dict(l.rstrip('\n').split('=', 1) for l in open(os.environ['FIX']) if '=' in l).get(path)
|
|
|
|
|
if spec is None or spec == 'missing':
|
|
|
|
|
sys.stderr.write('Error response from daemon: Could not find the file %s in container\n' % path); sys.exit(1)
|
|
|
|
|
if spec == 'notelf': head = b'#!/bin/sh\necho hi\n' + b'\0' * 64
|
|
|
|
|
elif spec == 'elf32': head = b'\x7fELF\x01\x01\x01' + b'\0' * 11 + struct.pack('<H', 0x3E) + b'\0' * 60
|
|
|
|
|
elif spec == 'be': head = b'\x7fELF\x02\x02\x01' + b'\0' * 11 + struct.pack('>H', 0xB7) + b'\0' * 60
|
|
|
|
|
elif spec == 'dir':
|
|
|
|
|
t = tarfile.open(fileobj=sys.stdout.buffer, mode='w|'); ti = tarfile.TarInfo('d'); ti.type = tarfile.DIRTYPE; t.addfile(ti); t.close(); sys.exit(0)
|
|
|
|
|
else: head = b'\x7fELF\x02\x01\x01' + b'\0' * 11 + struct.pack('<H', int(spec, 16)) + b'\0' * 60
|
|
|
|
|
body = head + b'\0' * 200000 # bigger than the pipe buffer: reader must stop early
|
|
|
|
|
t = tarfile.open(fileobj=sys.stdout.buffer, mode='w|')
|
|
|
|
|
ti = tarfile.TarInfo(os.path.basename(path)); ti.size = len(body); t.addfile(ti, io.BytesIO(body)); t.close()
|
|
|
|
|
PY
|
|
|
|
|
cat >"$T/dk" <<'STUB'
|
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
echo "$*" >>"$LOG"
|
|
|
|
|
case "$1" in
|
|
|
|
|
pull) exit "${PULL_RC:-0}" ;;
|
|
|
|
|
create) [ "${CREATE_RC:-0}" = 0 ] || { echo "no such image" >&2; exit 1; }; echo cid1 ;;
|
2026-11-19 21:10:08 -08:00
|
|
|
rm|image) ;;
|
2026-11-19 21:03:27 -08:00
|
|
|
cp) python3 -I "$MKTAR" "${3#*:}" ;;
|
|
|
|
|
esac
|
|
|
|
|
STUB
|
|
|
|
|
chmod +x "$T/dk"
|
|
|
|
|
export DOCKER="$T/dk" FIX=$T/fix LOG=$T/log MKTAR=$T/mktar.py
|
|
|
|
|
V="python3 -I $HERE/verify-image-arch.py"
|
|
|
|
|
run() { : >"$LOG"; $V "$@" >"$T/out" 2>"$T/err"; echo $?; }
|
|
|
|
|
|
|
|
|
|
printf '%s\n' /usr/bin/php=b7 /usr/sbin/httpd=b7 >"$T/fix"
|
|
|
|
|
chk "arm64 image with aarch64 binaries -> 0" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/sbin/httpd --bin /usr/bin/php)" 0
|
|
|
|
|
chk " pulled with the platform flag, container removed" "$(grep -c '^pull --quiet --platform linux/arm64 r/x@sha256:1$\|^rm -f cid1$' "$LOG")" 2
|
|
|
|
|
chk " prints AArch64" "$(grep -c 'e_machine=0xB7 (AArch64)' "$T/out")" 2
|
|
|
|
|
printf '%s\n' /usr/bin/php=3e >"$T/fix"
|
|
|
|
|
chk "arm64 image carrying an x86-64 binary -> 1" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php)" 1
|
|
|
|
|
chk " says x86-64 was found" "$(grep -c 'FAIL.*e_machine=0x3E (x86-64), want 0xB7 (AArch64)' "$T/out")" 1
|
|
|
|
|
chk "amd64 image with x86-64 binary -> 0" "$(run --image r/x@sha256:1 --platform linux/amd64 --bin /usr/bin/php)" 0
|
|
|
|
|
printf '%s\n' /usr/bin/php=b7 >"$T/fix"
|
|
|
|
|
chk "amd64 image carrying an aarch64 binary -> 1" "$(run --image r/x@sha256:1 --platform linux/amd64 --bin /usr/bin/php)" 1
|
|
|
|
|
printf '%s\n' /usr/bin/php=b7 /usr/sbin/httpd=3e >"$T/fix"
|
|
|
|
|
chk "one good + one wrong binary -> 1 (all are checked)" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php --bin /usr/sbin/httpd)" 1
|
|
|
|
|
chk " the good one is still reported OK" "$(grep -c '^OK ' "$T/out")" 1
|
|
|
|
|
printf '%s\n' /bin/a=notelf /bin/b=elf32 /bin/c=be /bin/d=dir /bin/e=missing >"$T/fix"
|
|
|
|
|
for b in a b c d e; do chk "bad binary /bin/$b -> 1" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /bin/$b)" 1; done
|
|
|
|
|
chk "binary absent from the fixture -> 1" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/nothere)" 1
|
|
|
|
|
printf '%s\n' /usr/bin/php=b7 >"$T/fix"
|
2026-11-19 21:10:08 -08:00
|
|
|
chk "docker pull fails (3 attempts) -> 4, not an arch verdict" "$(VERIFY_PULL_SLEEP=0 PULL_RC=1 run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php)" 4
|
|
|
|
|
chk " pull was tried 3 times" "$(grep -c '^pull' "$LOG")" 3
|
2026-11-19 21:03:27 -08:00
|
|
|
chk "docker create fails -> 1" "$(CREATE_RC=1 run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php)" 1
|
2026-11-19 21:10:08 -08:00
|
|
|
chk "pulled image is removed afterwards" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php >/dev/null; grep -c '^image rm r/x@sha256:1$' "$LOG")" 1
|
2026-11-19 21:03:27 -08:00
|
|
|
chk "--no-pull skips the pull" "$(run --image r/x@sha256:1 --platform linux/arm64 --no-pull --bin /usr/bin/php; grep -c '^pull' "$LOG" || true)" "0
|
|
|
|
|
0"
|
|
|
|
|
chk "usage: no --bin -> 2" "$(run --image r/x --platform linux/arm64)" 2
|
|
|
|
|
chk "usage: bad platform -> 2" "$(run --image r/x --platform linux/s390x --bin /usr/bin/php)" 2
|
|
|
|
|
|
|
|
|
|
[ "$fail" -eq 0 ] && echo "ALL PASS" || { echo FAILURES; exit 1; }
|