119 lines
4.7 KiB
Python
119 lines
4.7 KiB
Python
"""Real receipt risk checks; no remote collector or engine execution."""
|
|
|
|
import copy
|
|
import unittest
|
|
|
|
from audit import HERE, INCOMING, compare, load, matrix, references, validate_snapshot
|
|
from verify import validate_reference_records
|
|
|
|
|
|
class ReceiptContracts(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.snapshot = load(INCOMING / "ssh-snapshot.stdout.json")
|
|
cls.refs = references()
|
|
|
|
def test_original_snapshots_accepted(self):
|
|
validate_snapshot(self.snapshot)
|
|
|
|
def test_swapped_installations_rejected(self):
|
|
changed = copy.deepcopy(self.snapshot)
|
|
changed["installations"].reverse()
|
|
with self.assertRaises(AssertionError):
|
|
validate_snapshot(changed)
|
|
|
|
def test_head_misbound_to_working_bytes_rejected(self):
|
|
changed = copy.deepcopy(self.snapshot)
|
|
changed["installations"][1]["core"]["files"][0]["matches_head_blob"] = True
|
|
with self.assertRaises(AssertionError):
|
|
validate_snapshot(changed)
|
|
|
|
def test_parent_core_commit_not_aux_identity(self):
|
|
changed = copy.deepcopy(self.snapshot)
|
|
aux = changed["installations"][0]["aux"]
|
|
aux["head"] = changed["installations"][0]["core"]["head"]
|
|
with self.assertRaises(AssertionError):
|
|
validate_snapshot(changed)
|
|
|
|
def test_pyproject_version_not_whole_identity(self):
|
|
changed = copy.deepcopy(self.snapshot)
|
|
item = changed["installations"][0]["aux"]["files"][-1]
|
|
item["sha256"] = "0" * 64
|
|
rows = matrix(changed, self.refs)
|
|
row = next(
|
|
r
|
|
for r in rows
|
|
if r["installation_id"] == "install-1"
|
|
and r["kind"] == "aux"
|
|
and r["original_sample"]["relative_path"] == item["relative_path"]
|
|
)
|
|
self.assertEqual(row["comparison"], "different")
|
|
self.assertFalse(row["protocol_178_reusable"])
|
|
|
|
def test_missing_reference_not_silently_matched(self):
|
|
refs = [
|
|
r
|
|
for r in self.refs
|
|
if not (r["kind"] == "core" and r["path"] == "comfy_execution/graph_utils.py")
|
|
]
|
|
rows = matrix(self.snapshot, refs)
|
|
row = next(
|
|
r
|
|
for r in rows
|
|
if r["installation_id"] == "install-1"
|
|
and r["original_sample"]["relative_path"] == "comfy_execution/graph_utils.py"
|
|
)
|
|
self.assertEqual(row["comparison"], "no_exact_reference")
|
|
self.assertTrue(all(c["reference"] is None for c in row["candidates"]))
|
|
|
|
def test_other_member_reference_rejected(self):
|
|
file = self.snapshot["installations"][0]["aux"]["files"][1]
|
|
ref = next(r for r in self.refs if r["kind"] == "aux" and r["path"] == "utils.py")
|
|
with self.assertRaises(AssertionError):
|
|
compare(file, "aux", ref)
|
|
|
|
def test_original_times_not_replaced(self):
|
|
rows = load(HERE / "installation-matrix.json")
|
|
for row in rows:
|
|
i = int(row["installation_id"][-1]) - 1
|
|
original = next(
|
|
f
|
|
for f in self.snapshot["installations"][i][row["kind"]]["files"]
|
|
if f["relative_path"] == row["original_sample"]["relative_path"]
|
|
)
|
|
self.assertEqual(row["original_sample"], original)
|
|
for candidate in row["candidates"]:
|
|
if candidate["reference"]:
|
|
ref = candidate["reference"]
|
|
own = next(r for r in self.refs if r["proof_pointer"] == ref["proof_pointer"])
|
|
self.assertEqual(ref["original_acquired_at"], own["original_acquired_at"])
|
|
|
|
def test_missing_snapshot_state_preserved(self):
|
|
changed = copy.deepcopy(self.snapshot)
|
|
changed["installations"][0]["core"]["files"][0] = dict(
|
|
relative_path="execution.py",
|
|
state="missing",
|
|
recorded_at_utc=self.snapshot["installations"][0]["core"]["files"][0][
|
|
"recorded_at_utc"
|
|
],
|
|
)
|
|
validate_snapshot(changed)
|
|
self.assertEqual(matrix(changed, self.refs)[0]["comparison"], "snapshot_unavailable")
|
|
|
|
def test_borrowed_member_time_rejected(self):
|
|
refs = copy.deepcopy(self.refs)
|
|
refs[0]["original_acquired_at"] = refs[-1]["original_acquired_at"]
|
|
self.assertNotEqual(refs, self.refs)
|
|
with self.assertRaises(AssertionError):
|
|
validate_reference_records(refs)
|
|
|
|
def test_wrong_canonical_commit_rejected(self):
|
|
refs = copy.deepcopy(self.refs)
|
|
refs[0]["commit"] = "8010b48d2c1be986613f8e8c761527d205bd756f"
|
|
with self.assertRaises(AssertionError):
|
|
validate_reference_records(refs)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|