"""Regression for comparator tools whose successful exit only means they wrote a report.""" import json from pathlib import Path import tempfile import unittest from smoke_matrix import cache_counters, require_comparison, snapshot_pre_state class SemanticGateTests(unittest.TestCase): def test_checkpoint_includes_signature_and_mmap_sidecars_without_shared_writes(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) state = root / "state" (state / "db").mkdir(parents=True) (state / "db/000001.sst").write_bytes(b"old-db") for suffix in (".crypto-sig-cache", ".pp-cache-index", ".child-cert-cache-index"): (state / ("db" + suffix)).write_bytes(b"old-sidecar") proof = snapshot_pre_state(state, root / "checkpoint") self.assertEqual(len(proof["files_sha256"]), 4) (state / "db.crypto-sig-cache").write_bytes(b"post-step-success") self.assertEqual((root / "checkpoint/db.crypto-sig-cache").read_bytes(), b"old-sidecar") def test_signature_gate_checks_loaded_entries_and_per_point_hits(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) stage = {"crypto_signature_cache_observe": {"entries_loaded": 0, "entries_total": 1, "per_point": {"crl": {"calls": 1, "would_hit": 0, "verify_executed": 1, "verify_skipped": 0, "verify_nanos_on_new_key": 100}}}, "roa_validation_cache": {"hit_roas": 0, "lookup_nanos": 10}, "analysis_counts": {"roa_validation_cache_context_gate_nanos": 100, "publication_point_cache_reuse_nonzero_ms_total": 2, "publication_point_cache_reuse_nonzero_ms_hits": 1, "child_certificate_cache_hit": 5}} path = root / "stage-timing.json" path.write_text(json.dumps(stage)) cold = cache_counters(root) stage["analysis_counts"]["roa_validation_cache_context_gate_nanos"] = 200 stage["analysis_counts"]["publication_point_cache_reuse_nonzero_ms_total"] = 3 stage["analysis_counts"]["publication_point_cache_reuse_nonzero_ms_hits"] = 2 path.write_text(json.dumps(stage)) self.assertEqual(cold, cache_counters(root)) stage["analysis_counts"]["child_certificate_cache_hit"] = 6 path.write_text(json.dumps(stage)) self.assertNotEqual(cold, cache_counters(root)) stage["analysis_counts"]["child_certificate_cache_hit"] = 5 stage["crypto_signature_cache_observe"]["entries_loaded"] = 1 path.write_text(json.dumps(stage)) self.assertNotEqual(cold, cache_counters(root)) self.assertNotIn("lookup_nanos", cold["roa"]) def test_zero_exit_difference_report_is_rejected(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "comparison.json" for kind, field in (("cir", "allMatch"), ("ccr", "stateDigestMatch")): for value in (False, None, "true", 1): path.write_text(json.dumps({field: value})) with self.assertRaisesRegex(ValueError, "semantic comparison failed"): require_comparison(path, kind) path.write_text(json.dumps({field: True})) require_comparison(path, kind) if __name__ == "__main__": unittest.main()