|
| 1 | +"""Unit tests for TR-TXN module. |
| 2 | +
|
| 3 | +This module had no unit tests, which is why all four of its failure paths measured |
| 4 | +margin 0 in `measurement/`: every one of them could have been deleted without a |
| 5 | +single test failing. TR-TXN-001 is the only place the Level 2 tool-transcript |
| 6 | +requirement is enforced anywhere in the suite. |
| 7 | +
|
| 8 | +Each test below targets one `Finding(..., Status.FAIL, ...)` site, so deleting that |
| 9 | +site takes exactly this test with it. |
| 10 | +""" |
| 11 | + |
| 12 | +from trace_tests.modules.tr_txn import check |
| 13 | + |
| 14 | +VALID_HASH_256 = "sha256:" + "a" * 64 |
| 15 | +VALID_HASH_384 = "sha384:" + "b" * 96 |
| 16 | + |
| 17 | + |
| 18 | +def _txn(**over): |
| 19 | + txn = {"hash": VALID_HASH_256} |
| 20 | + txn.update(over) |
| 21 | + return {"tool_transcript": txn} |
| 22 | + |
| 23 | + |
| 24 | +def test_valid_transcript_passes(): |
| 25 | + findings = check(_txn()) |
| 26 | + assert all(f.passed() or f.code == "TR-TXN-002" for f in findings), findings |
| 27 | + |
| 28 | + |
| 29 | +def test_sha384_hash_is_accepted(): |
| 30 | + failed = [f for f in check(_txn(hash=VALID_HASH_384)) if f.failed()] |
| 31 | + assert not failed, failed |
| 32 | + |
| 33 | + |
| 34 | +# --- TR-TXN-001, the Level 2 obligation ------------------------------------ |
| 35 | + |
| 36 | + |
| 37 | +def test_missing_transcript_fails(): |
| 38 | + """The Level 2 requirement itself. Nothing else in the suite enforces it.""" |
| 39 | + failed = [f for f in check({}) if f.failed()] |
| 40 | + assert any(f.code == "TR-TXN-001" for f in failed) |
| 41 | + assert any("required at Level 2" in f.message for f in failed) |
| 42 | + |
| 43 | + |
| 44 | +def test_non_object_transcript_fails(): |
| 45 | + failed = [f for f in check({"tool_transcript": "sha256:deadbeef"}) if f.failed()] |
| 46 | + assert any(f.code == "TR-TXN-001" for f in failed) |
| 47 | + assert any("must be an object" in f.message for f in failed) |
| 48 | + |
| 49 | + |
| 50 | +def test_malformed_hash_fails(): |
| 51 | + failed = [f for f in check(_txn(hash="deadbeef")) if f.failed()] |
| 52 | + assert any(f.code == "TR-TXN-001" for f in failed) |
| 53 | + |
| 54 | + |
| 55 | +def test_missing_hash_fails(): |
| 56 | + failed = [f for f in check({"tool_transcript": {}}) if f.failed()] |
| 57 | + assert any(f.code == "TR-TXN-001" for f in failed) |
| 58 | + |
| 59 | + |
| 60 | +def test_truncated_digest_fails(): |
| 61 | + """63 hex characters, not 64. A prefix comparison would let this through.""" |
| 62 | + failed = [f for f in check(_txn(hash="sha256:" + "a" * 63)) if f.failed()] |
| 63 | + assert any(f.code == "TR-TXN-001" for f in failed) |
| 64 | + |
| 65 | + |
| 66 | +def test_uppercase_digest_fails(): |
| 67 | + """The pattern is lowercase hex; a case-insensitive match would accept this.""" |
| 68 | + failed = [f for f in check(_txn(hash="sha256:" + "A" * 64)) if f.failed()] |
| 69 | + assert any(f.code == "TR-TXN-001" for f in failed) |
| 70 | + |
| 71 | + |
| 72 | +# --- TR-TXN-002, optional but constrained when present --------------------- |
| 73 | + |
| 74 | + |
| 75 | +def test_absent_call_count_is_skipped_not_failed(): |
| 76 | + """Optional means optional: absence must not be reported as a failure.""" |
| 77 | + codes = {f.code: f for f in check(_txn())} |
| 78 | + assert "TR-TXN-002" in codes |
| 79 | + assert not codes["TR-TXN-002"].failed() |
| 80 | + |
| 81 | + |
| 82 | +def test_negative_call_count_fails(): |
| 83 | + failed = [f for f in check(_txn(call_count=-1)) if f.failed()] |
| 84 | + assert any(f.code == "TR-TXN-002" for f in failed) |
| 85 | + |
| 86 | + |
| 87 | +def test_non_integer_call_count_fails(): |
| 88 | + failed = [f for f in check(_txn(call_count="3")) if f.failed()] |
| 89 | + assert any(f.code == "TR-TXN-002" for f in failed) |
| 90 | + |
| 91 | + |
| 92 | +def test_zero_call_count_is_valid(): |
| 93 | + """A transcript with no calls is a real outcome, not an error.""" |
| 94 | + failed = [f for f in check(_txn(call_count=0)) if f.failed()] |
| 95 | + assert not failed, failed |
0 commit comments