| 1 | """ |
| 2 | test_tlog_unit |
| 3 | ============== |
| 4 | |
| 5 | Focused transparency-log checks around RFC 6962 behavior. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import hashlib |
| 10 | import json |
| 11 | import shutil |
| 12 | import sys |
| 13 | import uuid |
| 14 | from pathlib import Path |
| 15 | |
| 16 | ROOT = Path(__file__).resolve().parent.parent |
| 17 | sys.path.insert(0, str(ROOT)) |
| 18 | |
| 19 | from oversight_core.tlog import TransparencyLog |
| 20 | |
| 21 | |
| 22 | def test_empty_tree_root_matches_rfc6962(tmp_path): |
| 23 | tlog = TransparencyLog(tmp_path) |
| 24 | assert tlog.size() == 0 |
| 25 | assert tlog.root() == hashlib.sha256(b"").digest() |
| 26 | |
| 27 | |
| 28 | def test_reopen_rejects_corrupt_leaf_record(tmp_path): |
| 29 | (tmp_path / "leaves.jsonl").write_text("{not-json}\n", encoding="utf-8") |
| 30 | try: |
| 31 | TransparencyLog(tmp_path) |
| 32 | except ValueError: |
| 33 | pass |
| 34 | else: |
| 35 | raise AssertionError("corrupt tlog leaf should fail closed on load") |
| 36 | |
| 37 | |
| 38 | def test_range_records_validate_disk_leaf_hashes(tmp_path): |
| 39 | tlog = TransparencyLog(tmp_path) |
| 40 | tlog.append({"event": "register", "file_id": "f1"}) |
| 41 | records = tlog.range_records(0, 1) |
| 42 | assert records[0]["index"] == 0 |
| 43 | assert "leaf_data_hex" in records[0] |
| 44 | |
| 45 | rec = json.loads((tmp_path / "leaves.jsonl").read_text(encoding="utf-8")) |
| 46 | rec["leaf_data"] = "tampered" |
| 47 | rec.pop("leaf_data_hex", None) |
| 48 | (tmp_path / "leaves.jsonl").write_text(json.dumps(rec) + "\n", encoding="utf-8") |
| 49 | try: |
| 50 | tlog.range_records(0, 1) |
| 51 | except ValueError as exc: |
| 52 | assert "leaf hash mismatch" in str(exc) |
| 53 | else: |
| 54 | raise AssertionError("tampered leaf should fail closed during range read") |