"""Fault injection into a tiny content-addressed fixture, not a production backup product.""" from hashlib import sha256 from itertools import combinations from pathlib import Path from tempfile import TemporaryDirectory import argparse import json import platform import sys from model import CASES PAYLOADS = {name: (name.encode("ascii") * 64) for name in "ABCD"} def run_case(case, mode): if mode not in ("missing", "corrupt"): raise ValueError("mode must be missing or corrupt") with TemporaryDirectory(prefix="hesela-audit-") as temp: root = Path(temp) digests = {key: sha256(value).hexdigest() for key, value in PAYLOADS.items()} for key, value in PAYLOADS.items(): (root / digests[key]).write_bytes(value) # Separate object identity from content identity, even for identical recipes. manifest = root / "recipes.json" manifest.write_text(json.dumps(case["recipes"])) recipes = json.loads(manifest.read_text()) def verified_read(key): content = (root / digests[key]).read_bytes() if sha256(content).hexdigest() != digests[key]: raise ValueError("digest mismatch") return content intact = [b"".join(verified_read(key) for key in recipe) for recipe in recipes] for key in case["damaged"]: path = root / digests[key] if mode == "missing": path.unlink() else: path.write_bytes(b"!" * len(PAYLOADS[key])) audit_rows = [] for selected in combinations(sorted(digests), case["sample_size"]): detected = [] for key in selected: try: verified_read(key) except (FileNotFoundError, ValueError): detected.append(key) audit_rows.append({"selected": list(selected), "detected": detected}) restores = [] for index, recipe in enumerate(recipes): try: payload = b"".join(verified_read(key) for key in recipe) except (FileNotFoundError, ValueError): restores.append({"recipe": index, "complete": False}) else: if payload != intact[index]: raise RuntimeError("unexpected accepted wrong restore") restores.append({"recipe": index, "complete": True}) return { "case": case["id"], "mode": mode, "chunk_bytes": 64, "physical_chunk_files_before": len(digests), "intact_restore_count": len(intact), "audit_rows": audit_rows, "restores": restores, } def results(): return { "schema_version": 1, "kind": "synthetic_native_filesystem_fault_injection", "not_tested": ["restic", "PDP", "PoR", "device reliability", "durability after crash"], "python": sys.version, "platform": platform.platform(), "generator_sha256": sha256(Path(__file__).read_bytes()).hexdigest(), "model_sha256": sha256(Path(__file__).with_name("model.py").read_bytes()).hexdigest(), "cases": [run_case(case, mode) for case in CASES for mode in ("missing", "corrupt")], } if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() args.output.write_text(json.dumps(results(), indent=2, sort_keys=True) + "\n")