|
6 | 6 |
|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
| 9 | +import hashlib |
9 | 10 | import json |
10 | 11 | import subprocess |
11 | 12 | import sys |
12 | 13 | from pathlib import Path |
13 | 14 | from typing import Any |
14 | 15 |
|
| 16 | +import pytest |
| 17 | + |
15 | 18 | from scripts.verify_round_receipt import ( |
16 | 19 | EXIT_INVALID, |
17 | 20 | EXIT_USAGE, |
18 | 21 | EXIT_VALID, |
19 | 22 | RECEIPT_CONTRACT_V, |
20 | 23 | compute_bundle_identity, |
| 24 | + main, |
21 | 25 | ) |
22 | 26 |
|
23 | 27 | REPO_ROOT = Path(__file__).resolve().parents[1] |
@@ -163,11 +167,20 @@ def test_contract_version_mismatch(tmp_path: Path) -> None: |
163 | 167 |
|
164 | 168 |
|
165 | 169 | def test_missing_required_lane(tmp_path: Path) -> None: |
166 | | - receipt, lock = _valid_pair(tmp_path) |
| 170 | + # Two-lane tier so the roster stays non-empty after deleting one lane |
| 171 | + # (R29: an empty receipt roster fails the non-empty rule instead). |
| 172 | + lock_payload = make_lock() |
| 173 | + lock_payload["tiers"]["pr-smoke"]["lanes"] = ["softhsm2", "kryoptic"] |
| 174 | + lock = _write(tmp_path / "lock.json", lock_payload) |
167 | 175 | payload = make_receipt() |
| 176 | + payload["lanes"]["kryoptic"] = { |
| 177 | + "verdict": "pass", |
| 178 | + "admitted": True, |
| 179 | + "evidence_digest": _hex64("a"), |
| 180 | + } |
168 | 181 | del payload["lanes"]["softhsm2"] |
169 | 182 | _recompute_bundle(payload) |
170 | | - _write(receipt, payload) |
| 183 | + receipt = _write(tmp_path / "receipt.json", payload) |
171 | 184 | result = _run(receipt, lock) |
172 | 185 | assert result.returncode == EXIT_INVALID, result.stderr |
173 | 186 | assert "lanes.softhsm2" in result.stderr |
@@ -269,3 +282,201 @@ def test_empty_expect_fw(tmp_path: Path) -> None: |
269 | 282 | receipt, lock = _valid_pair(tmp_path) |
270 | 283 | result = _run(receipt, lock, expect_fw="") |
271 | 284 | assert result.returncode == EXIT_USAGE, result.stderr |
| 285 | + |
| 286 | + |
| 287 | +# --- Fix round 1 (review R1): decode/parser INFRA, total bundle check, |
| 288 | +# --- non-empty lanes (R29), and hardening coverage. |
| 289 | + |
| 290 | + |
| 291 | +def test_receipt_with_invalid_utf8_bytes_is_infra(tmp_path: Path) -> None: |
| 292 | + _, lock = _valid_pair(tmp_path) |
| 293 | + receipt = tmp_path / "receipt.json" |
| 294 | + receipt.write_bytes(b'{"receipt_contract_v": \xff}') |
| 295 | + result = _run(receipt, lock) |
| 296 | + assert result.returncode == EXIT_USAGE, result.stderr |
| 297 | + assert "error:" in result.stderr |
| 298 | + assert "Traceback" not in result.stderr |
| 299 | + |
| 300 | + |
| 301 | +def test_lock_with_invalid_utf8_bytes_is_infra(tmp_path: Path) -> None: |
| 302 | + receipt, _ = _valid_pair(tmp_path) |
| 303 | + lock = tmp_path / "lock.json" |
| 304 | + lock.write_bytes(b'{"receipt_contract_v": \xff}') |
| 305 | + result = _run(receipt, lock) |
| 306 | + assert result.returncode == EXIT_USAGE, result.stderr |
| 307 | + assert "error:" in result.stderr |
| 308 | + assert "Traceback" not in result.stderr |
| 309 | + |
| 310 | + |
| 311 | +def test_lock_with_huge_integer_is_infra(tmp_path: Path) -> None: |
| 312 | + receipt, _ = _valid_pair(tmp_path) |
| 313 | + raw = json.dumps(make_lock()) |
| 314 | + needle = '"receipt_contract_v": 1' |
| 315 | + assert needle in raw |
| 316 | + lock = tmp_path / "lock.json" |
| 317 | + lock.write_text(raw.replace(needle, f'"receipt_contract_v": {"9" * 5000}'), encoding="utf-8") |
| 318 | + result = _run(receipt, lock) |
| 319 | + assert result.returncode == EXIT_USAGE, result.stderr |
| 320 | + assert "error:" in result.stderr |
| 321 | + assert "Traceback" not in result.stderr |
| 322 | + |
| 323 | + |
| 324 | +def test_deeply_nested_receipt_never_tracebacks(tmp_path: Path) -> None: |
| 325 | + # Reviewer probe shape (10k nested arrays). Where the parser raises |
| 326 | + # RecursionError (this venv's 3.13, like the reviewer's interpreter) |
| 327 | + # the exit is 2 via the INFRA handler; where it parses (3.14's |
| 328 | + # iterative scanner handles >=200k levels) the non-object top level |
| 329 | + # fails validation with exit 1. Either way: handled, an error: line, |
| 330 | + # zero tracebacks. |
| 331 | + _, lock = _valid_pair(tmp_path) |
| 332 | + receipt = tmp_path / "receipt.json" |
| 333 | + receipt.write_text("[" * 10000 + "]" * 10000, encoding="utf-8") |
| 334 | + result = _run(receipt, lock) |
| 335 | + assert result.returncode in (EXIT_INVALID, EXIT_USAGE), result.stderr |
| 336 | + assert "error:" in result.stderr |
| 337 | + assert "Traceback" not in result.stderr |
| 338 | + |
| 339 | + |
| 340 | +def test_recursion_error_maps_to_infra_exit( |
| 341 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] |
| 342 | +) -> None: |
| 343 | + # Deterministic pin of the RecursionError -> exit 2 path on every |
| 344 | + # interpreter (the real parser only raises it on some versions). |
| 345 | + def _boom(_text: str) -> Any: |
| 346 | + raise RecursionError("maximum recursion depth exceeded") |
| 347 | + |
| 348 | + monkeypatch.setattr(json, "loads", _boom) |
| 349 | + receipt, lock = _valid_pair(tmp_path) |
| 350 | + assert main([str(receipt), "--lock", str(lock), "--expect-fw", FW_SHA]) == EXIT_USAGE |
| 351 | + assert "error:" in capsys.readouterr().err |
| 352 | + |
| 353 | + |
| 354 | +def test_surrogate_digest_reports_rule6_without_crash(tmp_path: Path) -> None: |
| 355 | + receipt, lock = _valid_pair(tmp_path) |
| 356 | + payload = make_receipt() |
| 357 | + payload["lanes"]["softhsm2"]["evidence_digest"] = chr(0xD800) |
| 358 | + _write(receipt, payload) |
| 359 | + result = _run(receipt, lock) |
| 360 | + assert result.returncode == EXIT_INVALID, result.stderr |
| 361 | + assert "Traceback" not in result.stderr |
| 362 | + assert "lanes.softhsm2.evidence_digest" in result.stderr |
| 363 | + assert result.stderr.count("error:") == 1, result.stderr |
| 364 | + |
| 365 | + |
| 366 | +def test_surrogate_digest_preserves_other_diagnostics(tmp_path: Path) -> None: |
| 367 | + receipt, lock = _valid_pair(tmp_path) |
| 368 | + payload = make_receipt() |
| 369 | + payload["lanes"]["softhsm2"]["evidence_digest"] = chr(0xD800) |
| 370 | + payload["policy_hash"] = "bad" |
| 371 | + payload["attempt"] = 0 |
| 372 | + _write(receipt, payload) |
| 373 | + result = _run(receipt, lock) |
| 374 | + assert result.returncode == EXIT_INVALID, result.stderr |
| 375 | + assert "Traceback" not in result.stderr |
| 376 | + assert "lanes.softhsm2.evidence_digest" in result.stderr |
| 377 | + assert "policy_hash" in result.stderr |
| 378 | + assert "attempt" in result.stderr |
| 379 | + assert result.stderr.count("error:") == 3, result.stderr |
| 380 | + |
| 381 | + |
| 382 | +def test_surrogate_lane_name_reports_unexpected_lane(tmp_path: Path) -> None: |
| 383 | + receipt, lock = _valid_pair(tmp_path) |
| 384 | + payload = make_receipt() |
| 385 | + payload["lanes"][f"{chr(0xD800)}evil"] = { |
| 386 | + "verdict": "pass", |
| 387 | + "admitted": True, |
| 388 | + "evidence_digest": _hex64("a"), |
| 389 | + } |
| 390 | + # Bundle intentionally NOT recomputed: the check must skip (no-cascade), |
| 391 | + # not crash on the unencodable name nor emit a spurious mismatch. |
| 392 | + _write(receipt, payload) |
| 393 | + result = _run(receipt, lock) |
| 394 | + assert result.returncode == EXIT_INVALID, result.stderr |
| 395 | + assert "Traceback" not in result.stderr |
| 396 | + assert "unexpected lane" in result.stderr |
| 397 | + assert result.stderr.count("error:") == 1, result.stderr |
| 398 | + |
| 399 | + |
| 400 | +def test_empty_tier_lanes_rejects_lock(tmp_path: Path) -> None: |
| 401 | + receipt, _ = _valid_pair(tmp_path) |
| 402 | + bad_lock = make_lock() |
| 403 | + bad_lock["tiers"]["pr-smoke"]["lanes"] = [] |
| 404 | + lock = _write(tmp_path / "lock.json", bad_lock) |
| 405 | + result = _run(receipt, lock) |
| 406 | + assert result.returncode == EXIT_USAGE, result.stderr |
| 407 | + assert "tiers.pr-smoke.lanes" in result.stderr |
| 408 | + |
| 409 | + |
| 410 | +def test_empty_receipt_lanes_rejected(tmp_path: Path) -> None: |
| 411 | + receipt, lock = _valid_pair(tmp_path) |
| 412 | + payload = make_receipt() |
| 413 | + payload["lanes"] = {} |
| 414 | + payload["bundle_identity"] = hashlib.sha256(b"").hexdigest() |
| 415 | + _write(receipt, payload) |
| 416 | + result = _run(receipt, lock) |
| 417 | + assert result.returncode == EXIT_INVALID, result.stderr |
| 418 | + assert "lanes: must be a non-empty object" in result.stderr |
| 419 | + |
| 420 | + |
| 421 | +def test_empty_tier_and_empty_receipt_rejected_at_lock(tmp_path: Path) -> None: |
| 422 | + # The green-on-empty hole (empty tier + empty roster + empty-sha bundle |
| 423 | + # previously exited 0): the lock gate must fire first with exit 2. |
| 424 | + bad_lock = make_lock() |
| 425 | + bad_lock["tiers"]["pr-smoke"]["lanes"] = [] |
| 426 | + lock = _write(tmp_path / "lock.json", bad_lock) |
| 427 | + payload = make_receipt() |
| 428 | + payload["lanes"] = {} |
| 429 | + payload["bundle_identity"] = hashlib.sha256(b"").hexdigest() |
| 430 | + receipt = _write(tmp_path / "receipt.json", payload) |
| 431 | + result = _run(receipt, lock) |
| 432 | + assert result.returncode == EXIT_USAGE, result.stderr |
| 433 | + assert "tiers.pr-smoke.lanes" in result.stderr |
| 434 | + |
| 435 | + |
| 436 | +# Independent bundle golden: canonical bytes for lanes a -> "1"*64, |
| 437 | +# z -> "2"*64 are `a\0<1x64>\0z\0<2x64>\0` (134 bytes); hashed via |
| 438 | +# `printf ... | sha256sum` in the shell (fix-round-1 report). Matches the |
| 439 | +# reviewer's independently computed value. |
| 440 | +BUNDLE_GOLDEN_AZ = "4b8e992c3a69482dc6cf017bd0a2766fb0d3b142d29aacf22bc745992a80bd8e" |
| 441 | + |
| 442 | + |
| 443 | +def test_bundle_golden_matches_shell_sha256() -> None: |
| 444 | + assert compute_bundle_identity({"a": _hex64("1"), "z": _hex64("2")}) == BUNDLE_GOLDEN_AZ |
| 445 | + |
| 446 | + |
| 447 | +def test_bundle_binding_is_order_independent() -> None: |
| 448 | + assert compute_bundle_identity({"z": _hex64("2"), "a": _hex64("1")}) == BUNDLE_GOLDEN_AZ |
| 449 | + |
| 450 | + |
| 451 | +def test_multi_fault_receipt_reports_every_violation(tmp_path: Path) -> None: |
| 452 | + # Fail-loud batch: six distinct faults, all six lines, no cascade. |
| 453 | + receipt, lock = _valid_pair(tmp_path) |
| 454 | + payload = make_receipt() |
| 455 | + payload["framework_sha"] = "0" * 40 |
| 456 | + payload["policy_hash"] = "bad" |
| 457 | + payload["attempt"] = 0 |
| 458 | + payload["run_id"] = "" |
| 459 | + payload["wheel_hashes"] = {} |
| 460 | + payload["lanes"]["softhsm2"]["admitted"] = False |
| 461 | + _write(receipt, payload) |
| 462 | + result = _run(receipt, lock) |
| 463 | + assert result.returncode == EXIT_INVALID, result.stderr |
| 464 | + for field in ( |
| 465 | + "framework_sha", |
| 466 | + "policy_hash", |
| 467 | + "attempt", |
| 468 | + "run_id", |
| 469 | + "wheel_hashes", |
| 470 | + "lanes.softhsm2.admitted", |
| 471 | + ): |
| 472 | + assert field in result.stderr, field |
| 473 | + assert result.stderr.count("error:") == 6, result.stderr |
| 474 | + |
| 475 | + |
| 476 | +def test_unknown_top_level_keys_ignored(tmp_path: Path) -> None: |
| 477 | + receipt, lock = _valid_pair(tmp_path) |
| 478 | + payload = make_receipt() |
| 479 | + payload["future_field"] = {"nested": [1, 2, 3]} |
| 480 | + _write(receipt, payload) |
| 481 | + result = _run(receipt, lock) |
| 482 | + assert result.returncode == EXIT_VALID, result.stderr |
0 commit comments