|
| 1 | +"""Actual bounded-memory copies and immutable-read/overwrite rejection controls.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import hashlib |
| 5 | +import json |
| 6 | +import tempfile |
| 7 | +import time |
| 8 | +import tracemalloc |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from tests.bundle_integrity import rejected |
| 12 | +from verification.bundle import Bundle |
| 13 | + |
| 14 | + |
| 15 | +def main(): |
| 16 | + parser = argparse.ArgumentParser() |
| 17 | + parser.add_argument("--output", type=Path, required=True) |
| 18 | + args = parser.parse_args() |
| 19 | + args.output.mkdir(parents=True, exist_ok=False) |
| 20 | + cases = [] |
| 21 | + with tempfile.TemporaryDirectory(prefix="caissa-stream-copy-") as temporary: |
| 22 | + root = Path(temporary) / "evidence" |
| 23 | + root.mkdir() |
| 24 | + payload = b"original-evidence\n" * 524288 |
| 25 | + source = root / "raw.bin" |
| 26 | + source.write_bytes(payload) |
| 27 | + sha = hashlib.sha256(payload).hexdigest() |
| 28 | + (root / "MANIFEST.json").write_text(json.dumps({"files": {"raw.bin": sha}})) |
| 29 | + destination = Path(temporary) / "copy.bin" |
| 30 | + with Bundle(root, 20) as bundle: |
| 31 | + bundle.verify_manifest() |
| 32 | + tracemalloc.start() |
| 33 | + copied = bundle.copy("raw.bin", destination, 16 * 1024**2) |
| 34 | + _, peak = tracemalloc.get_traced_memory() |
| 35 | + tracemalloc.stop() |
| 36 | + assert copied == {"bytes": len(payload), "sha256": sha} |
| 37 | + assert peak < 4 * 1024**2, peak |
| 38 | + assert destination.read_bytes() == payload |
| 39 | + cases.append( |
| 40 | + { |
| 41 | + "case": "actual-stream-copy", |
| 42 | + "bytes": len(payload), |
| 43 | + "peak_python_bytes": peak, |
| 44 | + } |
| 45 | + ) |
| 46 | + cases.append( |
| 47 | + { |
| 48 | + "case": "no-overwrite", |
| 49 | + "reason": rejected( |
| 50 | + lambda: bundle.copy("raw.bin", destination), None |
| 51 | + ), |
| 52 | + } |
| 53 | + ) |
| 54 | + assert destination.read_bytes() == payload |
| 55 | + cases.append( |
| 56 | + { |
| 57 | + "case": "preserve-original-root", |
| 58 | + "reason": rejected( |
| 59 | + lambda: bundle.copy("raw.bin", root / "extra.bin"), |
| 60 | + "preserve the original", |
| 61 | + ), |
| 62 | + } |
| 63 | + ) |
| 64 | + cases.append( |
| 65 | + { |
| 66 | + "case": "copy-byte-ceiling", |
| 67 | + "reason": rejected( |
| 68 | + lambda: bundle.copy( |
| 69 | + "raw.bin", Path(temporary) / "too-large", 1024 |
| 70 | + ), |
| 71 | + "byte ceiling", |
| 72 | + ), |
| 73 | + } |
| 74 | + ) |
| 75 | + cases.append( |
| 76 | + { |
| 77 | + "case": "typed-byte-ceiling", |
| 78 | + "reason": rejected( |
| 79 | + lambda: bundle.copy( |
| 80 | + "raw.bin", Path(temporary) / "bool-bound", True |
| 81 | + ), |
| 82 | + "byte ceiling", |
| 83 | + ), |
| 84 | + } |
| 85 | + ) |
| 86 | + source.write_bytes(b"changed evidence\n") |
| 87 | + cases.append( |
| 88 | + { |
| 89 | + "case": "changed-after-manifest", |
| 90 | + "reason": rejected( |
| 91 | + lambda: bundle.copy("raw.bin", Path(temporary) / "changed"), |
| 92 | + "changed after manifest", |
| 93 | + ), |
| 94 | + } |
| 95 | + ) |
| 96 | + source.unlink() |
| 97 | + source.symlink_to(destination) |
| 98 | + cases.append( |
| 99 | + { |
| 100 | + "case": "linked-source", |
| 101 | + "reason": rejected( |
| 102 | + lambda: bundle.copy("raw.bin", Path(temporary) / "linked"), None |
| 103 | + ), |
| 104 | + } |
| 105 | + ) |
| 106 | + source.unlink() |
| 107 | + source.write_bytes(payload) |
| 108 | + with Bundle(root, 20) as bundle: |
| 109 | + cases.append( |
| 110 | + { |
| 111 | + "case": "unverified-source", |
| 112 | + "reason": rejected( |
| 113 | + lambda: bundle.copy("raw.bin", Path(temporary) / "unverified"), |
| 114 | + "verified manifest", |
| 115 | + ), |
| 116 | + } |
| 117 | + ) |
| 118 | + bundle.verify_manifest() |
| 119 | + bundle.wall_deadline = time.time() - 1 |
| 120 | + cases.append( |
| 121 | + { |
| 122 | + "case": "original-deadline", |
| 123 | + "reason": rejected( |
| 124 | + lambda: bundle.copy("raw.bin", Path(temporary) / "expired"), |
| 125 | + "deadline exhausted", |
| 126 | + ), |
| 127 | + } |
| 128 | + ) |
| 129 | + value = { |
| 130 | + "status": "passed", |
| 131 | + "cases": cases, |
| 132 | + "full_acceptance": False, |
| 133 | + "scope": "Synthetic immutable streaming-copy controls; no model/native execution", |
| 134 | + } |
| 135 | + (args.output / "CONTROLS.json").write_text(json.dumps(value, indent=2) + "\n") |
| 136 | + print(json.dumps(value)) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + main() |
0 commit comments