Files
marqdo-extension-conformance/tests/verify_frozen_cli.py
T

58 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Verify that the frozen CLI binary is an absolute file and never changed.
Usage:
python3 tests/verify_frozen_cli.py --hash # print sha256
python3 tests/verify_frozen_cli.py --expect <sha256>
MARQDO_BIN=... python3 tests/verify_frozen_cli.py --hash
The suite never accepts a PATH lookup, `cargo run` or a relative binary: that
would let the "frozen binary" assumption silently drift.
"""
import hashlib
import os
import sys
from pathlib import Path
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def main(argv: list[str]) -> int:
raw = os.environ.get("MARQDO_BIN")
if not raw:
print("error: MARQDO_BIN must be set to the absolute frozen CLI path", file=sys.stderr)
return 2
cli = Path(raw)
if not cli.is_absolute():
print(f"error: MARQDO_BIN must be absolute, got {raw}", file=sys.stderr)
return 2
if not cli.is_file():
print(f"error: MARQDO_BIN is not a regular file: {cli}", file=sys.stderr)
return 2
actual = sha256(cli)
if "--hash" in argv:
print(actual)
return 0
if "--expect" in argv:
expected = argv[argv.index("--expect") + 1]
if expected != actual:
print(
f"error: frozen CLI changed: expected {expected}, actual {actual}",
file=sys.stderr,
)
return 1
print(f"frozen CLI unchanged: {actual}")
return 0
print(f"{cli} {actual}")
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv))