|
@@ -0,0 +1,328 @@
|
|
|
|
|
+"""Round-15: detached replay sidecar evidence completeness must fail-close."""
|
|
|
|
|
+
|
|
|
|
|
+from __future__ import annotations
|
|
|
|
|
+
|
|
|
|
|
+import copy
|
|
|
|
|
+import hashlib
|
|
|
|
|
+import json
|
|
|
|
|
+import zipfile
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+from unittest import mock
|
|
|
|
|
+
|
|
|
|
|
+from tests.unit.test_mse_r11_release_validation_bind import (
|
|
|
|
|
+ _matching_sidecars,
|
|
|
|
|
+ _validate,
|
|
|
|
|
+ _write_identity_zip,
|
|
|
|
|
+)
|
|
|
|
|
+from tools import replay_semantic as rs
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _setup(tmp_path: Path):
|
|
|
|
|
+ review = tmp_path / "review.zip"
|
|
|
|
|
+ firmware = tmp_path / "firmware.zip"
|
|
|
|
|
+ _write_identity_zip(review)
|
|
|
|
|
+ _write_identity_zip(firmware, extra="fw")
|
|
|
|
|
+ return review, firmware, _matching_sidecars(tmp_path, review, firmware)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _mutate_and_validate(tmp_path: Path, mutate):
|
|
|
|
|
+ review, firmware, sidecars = _setup(tmp_path)
|
|
|
|
|
+ doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
|
|
|
|
|
+ mutate(doc)
|
|
|
|
|
+ sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
|
|
|
|
|
+ return _validate(tmp_path, review, firmware, sidecars)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _assert_fail(result: dict, *codes: str) -> None:
|
|
|
|
|
+ assert result["status"] != "pass"
|
|
|
|
|
+ joined = " ".join(result.get("failures") or [])
|
|
|
|
|
+ for code in codes:
|
|
|
|
|
+ assert code in joined, f"missing {code} in {result.get('failures')}"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_happy_complete_sidecar_still_pass(tmp_path: Path):
|
|
|
|
|
+ review, firmware, sidecars = _setup(tmp_path)
|
|
|
|
|
+ result = _validate(tmp_path, review, firmware, sidecars)
|
|
|
|
|
+ assert result["status"] == "pass", result.get("failures")
|
|
|
|
|
+ assert result.get("failures") == []
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_empty_unit_modules_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ doc["detached_software"]["unit"]["modules"] = []
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(_mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_modules_empty")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_partial_unit_modules_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ doc["detached_software"]["unit"]["modules"] = [
|
|
|
|
|
+ {"module": "tests.unit.test_protocol", "status": "pass", "returncode": 0}
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_modules_incomplete"
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_wrong_unit_module_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ mods = doc["detached_software"]["unit"]["modules"]
|
|
|
|
|
+ mods[0]["module"] = "tests.unit.test_not_a_required_module"
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(_mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_module_unknown")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_duplicate_unit_module_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ mods = doc["detached_software"]["unit"]["modules"]
|
|
|
|
|
+ mods[1] = copy.deepcopy(mods[0])
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_module_duplicate"
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_unit_pass_nonzero_rc_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ doc["detached_software"]["unit"]["modules"][0]["returncode"] = 13
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_module_not_pass"
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_minimal_nested_pass_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ doc["package_content"] = {
|
|
|
|
|
+ "status": "pass",
|
|
|
|
|
+ "package_content_replay": "pass",
|
|
|
|
|
+ "checked_files": 1,
|
|
|
|
|
+ "volumes": doc["package_content"]["volumes"],
|
|
|
|
|
+ }
|
|
|
|
|
+ doc["detached_software"] = {
|
|
|
|
|
+ "status": "pass",
|
|
|
|
|
+ "git_mode": "detached",
|
|
|
|
|
+ "layout": {"status": "pass"},
|
|
|
|
|
+ "provenance": {"status": "pass"},
|
|
|
|
|
+ "unit": {"status": "pass", "modules": []},
|
|
|
|
|
+ "board": {"status": "pass"},
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate),
|
|
|
|
|
+ "replay_sidecar_unit_modules_empty",
|
|
|
|
|
+ "replay_sidecar_layout_checks_missing",
|
|
|
|
|
+ "replay_volume_manifest_file_count_mismatch",
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_board_status_only_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ doc["detached_software"]["board"] = {"status": "pass"}
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate), "replay_sidecar_board_mode_not_canonical"
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_board_mode_bogus_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ doc["detached_software"]["board"]["mode"] = "bogus"
|
|
|
|
|
+ doc["detached_software"]["board"]["returncode"] = 99
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate),
|
|
|
|
|
+ "replay_sidecar_board_mode_not_canonical",
|
|
|
|
|
+ "replay_sidecar_board_returncode_nonzero",
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_layout_checks_false_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ for key in doc["detached_software"]["layout"]["checks"]:
|
|
|
|
|
+ doc["detached_software"]["layout"]["checks"][key] = False
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate), "replay_sidecar_layout_required_check_false"
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_provenance_details_false_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ doc["detached_software"]["provenance"]["head_txt"] = False
|
|
|
|
|
+ doc["detached_software"]["provenance"]["tree_manifest"] = False
|
|
|
|
|
+ doc["detached_software"]["provenance"]["sanitized_snapshot_status"] = False
|
|
|
|
|
+ doc["detached_software"]["provenance"]["raw_git_required"] = True
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate),
|
|
|
|
|
+ "replay_sidecar_provenance_details_false",
|
|
|
|
|
+ "replay_sidecar_provenance_raw_git_required",
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_checked_files_one_fail(tmp_path: Path):
|
|
|
|
|
+ def mutate(doc):
|
|
|
|
|
+ doc["package_content"]["checked_files"] = 1
|
|
|
|
|
+
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ _mutate_and_validate(tmp_path, mutate), "replay_volume_manifest_file_count_mismatch"
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _write_source_review_zip(path: Path) -> None:
|
|
|
|
|
+ identity = {
|
|
|
|
|
+ "schema": "prod2u-release-set-identity-v1",
|
|
|
|
|
+ "delivery_commit": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
|
+ "release_set_id": "bb" * 32,
|
|
|
|
|
+ }
|
|
|
|
|
+ members = {
|
|
|
|
|
+ "meta/release-set-identity.json": json.dumps(identity).encode("utf-8"),
|
|
|
|
|
+ "payload.txt": b"review-source",
|
|
|
|
|
+ "CPLD/source/hello.py": b"x = 1\n",
|
|
|
|
|
+ }
|
|
|
|
|
+ files = [
|
|
|
|
|
+ {
|
|
|
|
|
+ "archive_path": name,
|
|
|
|
|
+ "size": len(blob),
|
|
|
|
|
+ "sha256": hashlib.sha256(blob).hexdigest(),
|
|
|
|
|
+ }
|
|
|
|
|
+ for name, blob in members.items()
|
|
|
|
|
+ ]
|
|
|
|
|
+ manifest = {"schema": "prod2u-volume-manifest-v1", "file_count": len(files), "files": files}
|
|
|
|
|
+ with zipfile.ZipFile(path, "w") as zf:
|
|
|
|
|
+ for name, blob in members.items():
|
|
|
|
|
+ zf.writestr(name, blob)
|
|
|
|
|
+ zf.writestr("CPLD/HANDOFF-METADATA/VOLUME-MANIFEST.json", json.dumps(manifest))
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _matching_source_software() -> dict:
|
|
|
|
|
+ return {
|
|
|
|
|
+ "status": "pass",
|
|
|
|
|
+ "git_mode": "detached",
|
|
|
|
|
+ "layout": {
|
|
|
|
|
+ "status": "pass",
|
|
|
|
|
+ "checks": {
|
|
|
|
|
+ "source_dir": True,
|
|
|
|
|
+ "meta_dir": True,
|
|
|
|
|
+ "evidence_dir": True,
|
|
|
|
|
+ "meta_git_dir": True,
|
|
|
|
|
+ "no_raw_git_under_source": True,
|
|
|
|
|
+ "no_raw_git_under_cpld": True,
|
|
|
|
|
+ "project_contract_present": True,
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ "provenance": {
|
|
|
|
|
+ "status": "pass",
|
|
|
|
|
+ "head_txt": True,
|
|
|
|
|
+ "tree_manifest": True,
|
|
|
|
|
+ "sanitized_snapshot_status": True,
|
|
|
|
|
+ "raw_git_required": False,
|
|
|
|
|
+ },
|
|
|
|
|
+ "unit": {
|
|
|
|
|
+ "status": "pass",
|
|
|
|
|
+ "modules": [
|
|
|
|
|
+ {"module": "tests.unit.test_protocol", "status": "pass", "returncode": 0},
|
|
|
|
|
+ {"module": "tests.unit.test_project_config", "status": "pass", "returncode": 0},
|
|
|
|
|
+ {
|
|
|
|
|
+ "module": "tests.unit.test_product_contract_coverage",
|
|
|
|
|
+ "status": "pass",
|
|
|
|
|
+ "returncode": 0,
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ "board": {
|
|
|
|
|
+ "status": "pass",
|
|
|
|
|
+ "mode": "validate_detached_derived",
|
|
|
|
|
+ "returncode": 0,
|
|
|
|
|
+ "reference_regeneration_replay": "not_packaged_by_policy",
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_dummy_zip_skips_independent_rerun(tmp_path: Path):
|
|
|
|
|
+ review, firmware, sidecars = _setup(tmp_path)
|
|
|
|
|
+ assert rs.review_zip_has_source_tree(review) is False
|
|
|
|
|
+ result = _validate(tmp_path, review, firmware, sidecars)
|
|
|
|
|
+ assert result["status"] == "pass", result.get("failures")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_independent_rerun_matches_sidecar(tmp_path: Path):
|
|
|
|
|
+ review = tmp_path / "review.zip"
|
|
|
|
|
+ firmware = tmp_path / "firmware.zip"
|
|
|
|
|
+ _write_source_review_zip(review)
|
|
|
|
|
+ _write_identity_zip(firmware, extra="fw")
|
|
|
|
|
+ sidecars = _matching_sidecars(tmp_path, review, firmware)
|
|
|
|
|
+ doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
|
|
|
|
|
+ doc["package_content"]["checked_files"] = 3
|
|
|
|
|
+ actual = _matching_source_software()
|
|
|
|
|
+ claimed = rs.semantic_object(
|
|
|
|
|
+ content=doc["package_content"], software=doc["detached_software"]
|
|
|
|
|
+ )
|
|
|
|
|
+ actual_sem = rs.semantic_object(content=doc["package_content"], software=actual)
|
|
|
|
|
+ actual_sem["checked_files"] = 3
|
|
|
|
|
+ doc["replay_semantic_sha256"] = rs.semantic_sha256(actual_sem)
|
|
|
|
|
+ sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
|
|
|
|
|
+ with mock.patch.object(rs, "_run_actual_software_replay", return_value=actual):
|
|
|
|
|
+ result = _validate(tmp_path, review, firmware, sidecars)
|
|
|
|
|
+ assert result["status"] == "pass", result.get("failures")
|
|
|
|
|
+ assert claimed["unit_modules"] == actual_sem["unit_modules"]
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_independent_rerun_unit_mismatch_fail(tmp_path: Path):
|
|
|
|
|
+ review = tmp_path / "review.zip"
|
|
|
|
|
+ firmware = tmp_path / "firmware.zip"
|
|
|
|
|
+ _write_source_review_zip(review)
|
|
|
|
|
+ _write_identity_zip(firmware, extra="fw")
|
|
|
|
|
+ sidecars = _matching_sidecars(tmp_path, review, firmware)
|
|
|
|
|
+ doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
|
|
|
|
|
+ doc["package_content"]["checked_files"] = 3
|
|
|
|
|
+ sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
|
|
|
|
|
+ actual = _matching_source_software()
|
|
|
|
|
+ actual["unit"]["modules"][0]["module"] = "tests.unit.test_not_replayed"
|
|
|
|
|
+ with mock.patch.object(rs, "_run_actual_software_replay", return_value=actual):
|
|
|
|
|
+ result = _validate(tmp_path, review, firmware, sidecars)
|
|
|
|
|
+ _assert_fail(result, "replay_semantic_unit_mismatch")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_mkdtemp_then_extract_does_not_file_exists(tmp_path: Path):
|
|
|
|
|
+ """Regression: mkdtemp() + mkdir(exist_ok=False) on the same path."""
|
|
|
|
|
+ review = tmp_path / "review.zip"
|
|
|
|
|
+ _write_source_review_zip(review)
|
|
|
|
|
+ result = rs._run_actual_software_replay(review)
|
|
|
|
|
+ assert isinstance(result, dict)
|
|
|
|
|
+ assert "status" in result
|
|
|
|
|
+ assert "layout" in result
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_independent_rerun_error_fail(tmp_path: Path):
|
|
|
|
|
+ review = tmp_path / "review.zip"
|
|
|
|
|
+ firmware = tmp_path / "firmware.zip"
|
|
|
|
|
+ _write_source_review_zip(review)
|
|
|
|
|
+ _write_identity_zip(firmware, extra="fw")
|
|
|
|
|
+ sidecars = _matching_sidecars(tmp_path, review, firmware)
|
|
|
|
|
+ doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
|
|
|
|
|
+ doc["package_content"]["checked_files"] = 3
|
|
|
|
|
+ sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
|
|
|
|
|
+ with mock.patch.object(rs, "_run_actual_software_replay", side_effect=RuntimeError("extract")):
|
|
|
|
|
+ result = _validate(tmp_path, review, firmware, sidecars)
|
|
|
|
|
+ _assert_fail(result, "replay_actual_rerun_error")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_r15_semantic_checked_files_mismatch_fail(tmp_path: Path):
|
|
|
|
|
+ review = tmp_path / "review.zip"
|
|
|
|
|
+ firmware = tmp_path / "firmware.zip"
|
|
|
|
|
+ _write_source_review_zip(review)
|
|
|
|
|
+ _write_identity_zip(firmware, extra="fw")
|
|
|
|
|
+ sidecars = _matching_sidecars(tmp_path, review, firmware)
|
|
|
|
|
+ doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
|
|
|
|
|
+ doc["package_content"]["checked_files"] = 1
|
|
|
|
|
+ sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
|
|
|
|
|
+ with mock.patch.object(rs, "_run_actual_software_replay", return_value=_matching_source_software()):
|
|
|
|
|
+ result = _validate(tmp_path, review, firmware, sidecars)
|
|
|
|
|
+ _assert_fail(
|
|
|
|
|
+ result,
|
|
|
|
|
+ "replay_volume_manifest_file_count_mismatch",
|
|
|
|
|
+ "replay_semantic_checked_files_mismatch",
|
|
|
|
|
+ )
|