#!/usr/bin/env python3 """Payload acceptance demonstration, not a TAR parser or a BagIt validator.""" import argparse import json from pathlib import Path import re from observe import PAYLOADS, ZERO, digest def records(rows): if not isinstance(rows, list): raise ValueError("Expected a list of entry records") result = {} duplicates = set() for row in rows: if not isinstance(row, dict): raise ValueError("Expected an entry object") name, size, checksum = (row.get(key) for key in ("name", "bytes", "sha256")) if not isinstance(name, str) or not name: raise ValueError("Expected a nonempty literal name") if type(size) is not int or size < 0: raise ValueError("Expected a nonnegative integer byte count") if not isinstance(checksum, str) or not re.fullmatch(r"[0-9a-f]{64}", checksum): raise ValueError("Expected a lowercase SHA-256 digest") if name in result: duplicates.add(name) result[name] = (size, checksum) return result, sorted(duplicates) def assess(expected, observed, completed): """Compare literal regular-file payload records; trust is supplied externally.""" if type(completed) is not bool: raise ValueError("Completion must be an explicit boolean") wanted, duplicate_expected = records(expected) found, duplicates = records(observed) if duplicate_expected: raise ValueError("The expected manifest must have unique names") missing = sorted(wanted.keys() - found.keys()) unexpected = sorted(found.keys() - wanted.keys()) changed = sorted(name for name in wanted.keys() & found.keys() if wanted[name] != found[name]) inventory_complete = not (missing or unexpected or duplicates) return {"completed": completed, "missing": missing, "unexpected": unexpected, "duplicates": duplicates, "mismatched": changed, "inventory_complete": inventory_complete, "accepted": completed and inventory_complete and not changed} def from_receipt(receipt): rows = [] for case in receipt["cases"]: # The target predates parsing and does not come from the surviving entries. payloads = dict(PAYLOADS) if case["id"] == "zero-payload-control": payloads["alpha.bin"] = ZERO expected = [{"name": name, "bytes": len(data), "sha256": digest(data)} for name, data in payloads.items()] for reader in case["readers"]: if reader["reader"] not in ("python-default", "python-ignore"): continue rows.append({"fixture": case["id"], "reader": reader["reader"], **assess(expected, reader["entries"], reader["api_ok"])}) return rows def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() source = Path(__file__).with_name("results.json").read_bytes() result = {"kind": "derived payload checks on retained observations; no new reader runs", "receipt_sha256": digest(source), "policy_sha256": digest(Path(__file__).read_bytes()), "scope": "Literal unique regular-file names, sizes and SHA-256; no metadata, path safety, parser or authenticity proof", "rows": from_receipt(json.loads(source))} with args.output.open("x", encoding="utf-8") as output: json.dump(result, output, indent=2) output.write("\n") if __name__ == "__main__": main()