|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Exercise the AKernel installer with a real candidate/release archive. |
| 3 | +
|
| 4 | +Usage: python3 builder/scripts/test-install-distill-fs.py /path/to/release.tar.gz |
| 5 | +Requires curl, jq, binutils, and a Linux/amd64 host. No network is needed. |
| 6 | +""" |
| 7 | + |
| 8 | +import hashlib |
| 9 | +import io |
| 10 | +import json |
| 11 | +import os |
| 12 | +from pathlib import Path |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +import tarfile |
| 16 | +import tempfile |
| 17 | + |
| 18 | + |
| 19 | +archive = Path(sys.argv[1]).resolve() |
| 20 | +installer = Path(__file__).with_name("install-distill-fs.sh") |
| 21 | +with tarfile.open(archive) as bundle: |
| 22 | + files = {name: bundle.extractfile(name).read() for name in ( |
| 23 | + "distill_fs", "manifest.json", "LICENSE", "NOTICE", "Cargo.lock" |
| 24 | + )} |
| 25 | +manifest = json.loads(files["manifest.json"]) |
| 26 | +release = manifest["release_tag"] |
| 27 | +digest = hashlib.sha256(archive.read_bytes()).hexdigest() |
| 28 | + |
| 29 | +with tempfile.TemporaryDirectory(prefix="distill-fs-installer-") as work: |
| 30 | + work = Path(work) |
| 31 | + |
| 32 | + def check(name, asset=archive, checksum=digest, tag=release, arch="amd64", error=None): |
| 33 | + destination = work / name |
| 34 | + result = subprocess.run( |
| 35 | + ["sh", str(installer), tag, asset.as_uri(), checksum, str(destination)], |
| 36 | + env={**os.environ, "TARGETARCH": arch}, |
| 37 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, |
| 38 | + ) |
| 39 | + binary = destination / "bin/distill_fs" |
| 40 | + if error is None: |
| 41 | + assert result.returncode == 0, result.stdout |
| 42 | + assert binary.read_bytes() == files["distill_fs"] |
| 43 | + for filename in ("manifest.json", "LICENSE", "NOTICE", "Cargo.lock"): |
| 44 | + assert (destination / "share/distill-fs" / filename).read_bytes() == files[filename] |
| 45 | + else: |
| 46 | + assert result.returncode != 0, name |
| 47 | + assert error in result.stdout, result.stdout |
| 48 | + assert not binary.exists(), "a rejected artifact must not be installed" |
| 49 | + print(f"PASS {name}") |
| 50 | + |
| 51 | + def altered_archive(name, binary, binary_hash): |
| 52 | + data = {**files, "distill_fs": binary} |
| 53 | + metadata = {**manifest, "binary_sha256": binary_hash} |
| 54 | + data["manifest.json"] = json.dumps(metadata).encode() |
| 55 | + path = work / f"{name}.tar.gz" |
| 56 | + with tarfile.open(path, "w:gz") as tar: |
| 57 | + for filename, content in data.items(): |
| 58 | + entry = tarfile.TarInfo(filename) |
| 59 | + entry.size = len(content) |
| 60 | + tar.addfile(entry, io.BytesIO(content)) |
| 61 | + return path, hashlib.sha256(path.read_bytes()).hexdigest() |
| 62 | + |
| 63 | + check("valid") |
| 64 | + check("missing-pin", checksum="", error="publish and pin") |
| 65 | + check("bad-pin", checksum="not-a-digest", error="invalid distill-fs SHA-256") |
| 66 | + check("corrupt-archive", checksum="0" * 64, error="FAILED") |
| 67 | + check("wrong-version", tag="v999.0.0", error="") |
| 68 | + check("unsupported-arch", arch="arm64", error="linux/amd64 only") |
| 69 | + bad, sha = altered_archive("bad-binary-hash", files["distill_fs"], "0" * 64) |
| 70 | + check("bad-binary-hash", asset=bad, checksum=sha, error="FAILED") |
| 71 | + # A correctly checksummed bundle must still reject a dynamic executable. |
| 72 | + dynamic = Path("/bin/true").read_bytes() |
| 73 | + headers = subprocess.check_output(["readelf", "-l", "/bin/true"], text=True) |
| 74 | + assert "INTERP" in headers, "this negative fixture needs a dynamic /bin/true" |
| 75 | + bad, sha = altered_archive("dynamic-binary", dynamic, hashlib.sha256(dynamic).hexdigest()) |
| 76 | + check("dynamic-binary", asset=bad, checksum=sha, error="must be a static executable") |
0 commit comments