Просмотр исходного кода

fix(mse): align R9 firmware unit fixtures with shared TD identity

Fake-bit packaging tests now patch canonical TD fields to the fixture
bitstream; ZIP selftest reads TD binding before closing the archive.

Co-authored-by: Cursor <cursoragent@cursor.com>
root 3 месяцев назад
Родитель
Сommit
d126c3df0c

+ 54 - 26
tests/unit/test_build_firmware_package.py

@@ -3,6 +3,7 @@
 
 from __future__ import annotations
 
+import hashlib
 import json
 import shutil
 import subprocess
@@ -11,27 +12,52 @@ import tempfile
 import unittest
 import zipfile
 from pathlib import Path
+from unittest import mock
 
 TOOLS = Path(__file__).resolve().parents[2] / "tools"
 if str(TOOLS) not in sys.path:
     sys.path.insert(0, str(TOOLS))
 
 import build_firmware_package as builder
-
+import package_provenance as pkg_prov
 
 ROOT = Path(__file__).resolve().parents[2]
+FAKE_BITS = b"FAKEBITS"
+FAKE_BIT_SHA = hashlib.sha256(FAKE_BITS).hexdigest()
+FAKE_TD_RUN_ID = "td-run-123"
 
 
 def _current_project_sha() -> str:
     return builder._sha256_file(ROOT / "projects" / "prod2u-door-temp" / "project.json")
 
 
+def _patch_canonical_td_for_fake(*, bits: bytes = FAKE_BITS, run_id: str = FAKE_TD_RUN_ID):
+    """Unit fixtures use fake bits; align shared identity to those bytes."""
+    sha = hashlib.sha256(bits).hexdigest()
+    fields = {
+        "artifact_source_commit": "fe1a0a2a19622b6f290905ec14795bb6cce6899d",
+        "td_bound_commit": "fe1a0a2a19622b6f290905ec14795bb6cce6899d",
+        "bitstream_sha256": sha,
+        "td_artifact_run_id": run_id,
+        "td_verification_run_id": "88888888888888888888888888888888",
+        "td_receipt_path": "receipts/mse-r4-td-a1-ff44128.json",
+        "td_receipt_sha256": "c" * 64,
+        "td_binding_status": "pass",
+        "production_binding": "37/37/0/0",
+        "lut": 1113,
+        "mslice": 570,
+        "dsp": 0,
+        "status": "pass",
+    }
+    return mock.patch.object(pkg_prov, "load_canonical_td_a1_fields", return_value=fields)
+
+
 def _write_report(path: Path, *, bitstream_sha256: str | None = None, project_sha: str | None = None) -> None:
     project_sha = project_sha or _current_project_sha()
     if bitstream_sha256 is None:
-        bitstream_sha256 = "2ce6b1913b339453da963d4528ceb09ff93a26e61c9c14626d316bbd91f7243f"
+        bitstream_sha256 = FAKE_BIT_SHA
     report = {
-        "run_id": "td-run-123",
+        "run_id": FAKE_TD_RUN_ID,
         "verification_run_id": "88888888888888888888888888888888",
         "commit_full": "fe1a0a2a19622b6f290905ec14795bb6cce6899d",
         "project_config_sha256": project_sha,
@@ -69,7 +95,7 @@ class BuildFirmwarePackageTests(unittest.TestCase):
         td_root = tmp_path / "td_root"
         bitstream = td_root / "td" / "prod2u_p4_normal_v3" / "bitgen" / "prod2u_p4_normal_v3.bit"
         bitstream.parent.mkdir(parents=True, exist_ok=True)
-        bitstream.write_bytes(b"FAKEBITS")
+        bitstream.write_bytes(FAKE_BITS)
         report = td_root / "report.json"
         _write_report(report, bitstream_sha256=bitstream_sha256, project_sha=project_sha)
         # Add a fake stage log to verify allowlist
@@ -88,17 +114,18 @@ class BuildFirmwarePackageTests(unittest.TestCase):
             td_root, bitstream, report = self._make_td_root(tmp_path)
             output_dir = tmp_path / "dist"
             artifact, matrix, final_dev = self._commits()
-            package, sidecar = builder.build_firmware_package(
-                output_dir=output_dir,
-                package_name="test-firmware-package",
-                td_root=td_root,
-                bitstream_path=bitstream,
-                td_report_path=report,
-                artifact_commit=artifact,
-                matrix_commit=matrix,
-                final_dev_commit=final_dev,
-                source_closure_unchanged=True,
-            )
+            with _patch_canonical_td_for_fake():
+                package, sidecar = builder.build_firmware_package(
+                    output_dir=output_dir,
+                    package_name="test-firmware-package",
+                    td_root=td_root,
+                    bitstream_path=bitstream,
+                    td_report_path=report,
+                    artifact_commit=artifact,
+                    matrix_commit=matrix,
+                    final_dev_commit=final_dev,
+                    source_closure_unchanged=True,
+                )
             self.assertTrue(package.is_file())
             with zipfile.ZipFile(package, "r") as zipf:
                 names = set(zipf.namelist())
@@ -171,17 +198,18 @@ class BuildFirmwarePackageTests(unittest.TestCase):
             (ROOT / "td" / "td_20260630_134648.log").write_text("stale", encoding="utf-8")
             try:
                 artifact, matrix, final_dev = self._commits()
-                package, _ = builder.build_firmware_package(
-                    output_dir=tmp_path / "dist",
-                    package_name="no-stale",
-                    td_root=td_root,
-                    bitstream_path=bitstream,
-                    td_report_path=report,
-                    artifact_commit=artifact,
-                    matrix_commit=matrix,
-                    final_dev_commit=final_dev,
-                    source_closure_unchanged=True,
-                )
+                with _patch_canonical_td_for_fake():
+                    package, _ = builder.build_firmware_package(
+                        output_dir=tmp_path / "dist",
+                        package_name="no-stale",
+                        td_root=td_root,
+                        bitstream_path=bitstream,
+                        td_report_path=report,
+                        artifact_commit=artifact,
+                        matrix_commit=matrix,
+                        final_dev_commit=final_dev,
+                        source_closure_unchanged=True,
+                    )
                 with zipfile.ZipFile(package, "r") as zipf:
                     self.assertNotIn("td/td_20260630_134648.log", set(zipf.namelist()))
             finally:

+ 32 - 12
tests/unit/test_release_set_provenance.py

@@ -9,6 +9,7 @@ import tempfile
 import unittest
 import zipfile
 from pathlib import Path
+from unittest import mock
 
 TOOLS = Path(__file__).resolve().parents[2] / "tools"
 if str(TOOLS) not in sys.path:
@@ -325,8 +326,9 @@ class FirmwarePackageProvenanceIntegrationTests(unittest.TestCase):
             )
             # Use real identical commits so source-closure-unchanged can pass.
             commit = "f23d6d4c57959071cd3886ea5503fbe903aee547"
+            run_id = "td-run-prov"
             report = {
-                "run_id": "td-run-prov",
+                "run_id": run_id,
                 "commit_full": commit,
                 "project_config_sha256": project_sha,
                 "td_source_manifest_sha256": "8e0da456e865f3c3afa613d2660c1ed0ebf0e422ef51ec7acef223c2017a14bd",
@@ -346,17 +348,35 @@ class FirmwarePackageProvenanceIntegrationTests(unittest.TestCase):
             }
             report_path = td_root / "report.json"
             report_path.write_text(json.dumps(report), encoding="utf-8")
-            package, sidecar = builder.build_firmware_package(
-                output_dir=tmp_path / "dist",
-                package_name="prov-firmware",
-                td_root=td_root,
-                bitstream_path=bitstream,
-                td_report_path=report_path,
-                artifact_commit=commit,
-                matrix_commit=commit,
-                final_dev_commit=commit,
-                source_closure_unchanged=True,
-            )
+            fake_td = {
+                "artifact_source_commit": commit,
+                "td_bound_commit": commit,
+                "bitstream_sha256": bit_sha,
+                "td_artifact_run_id": run_id,
+                "td_verification_run_id": None,
+                "td_receipt_path": "receipts/mse-r4-td-a1-ff44128.json",
+                "td_receipt_sha256": "c" * 64,
+                "td_binding_status": "pass",
+                "production_binding": "37/37/0/0",
+                "lut": 0,
+                "mslice": 0,
+                "dsp": 0,
+                "status": "pass",
+            }
+            with mock.patch.object(
+                pkg_prov, "load_canonical_td_a1_fields", return_value=fake_td
+            ):
+                package, sidecar = builder.build_firmware_package(
+                    output_dir=tmp_path / "dist",
+                    package_name="prov-firmware",
+                    td_root=td_root,
+                    bitstream_path=bitstream,
+                    td_report_path=report_path,
+                    artifact_commit=commit,
+                    matrix_commit=commit,
+                    final_dev_commit=commit,
+                    source_closure_unchanged=True,
+                )
             self.assertTrue(sidecar.is_file())
             with zipfile.ZipFile(package, "r") as zipf:
                 names = set(zipf.namelist())

+ 27 - 21
tools/compact_package_selftest.py

@@ -617,6 +617,33 @@ def run_compact_zip_selftest(
         if stage == PACKAGE_STAGE_FINAL:
             failures.extend(check_final_canonical_evidence(archive, members))
 
+        # Round-9: never ship final (or bitstream-claiming) packages stuck on hook_ready.
+        # Must run while the ZipFile is still open.
+        for name in members:
+            norm = _normalize_member(name)
+            if not (
+                norm.endswith("meta/td/td-source-closure-binding.json")
+                or norm.endswith("meta/td-source-closure-binding.json")
+            ):
+                continue
+            try:
+                binding_doc = json.loads(archive.read(name).decode("utf-8"))
+            except Exception as exc:  # noqa: BLE001
+                failures.append(f"td_binding_unreadable:{exc}")
+                break
+            st = str(binding_doc.get("status") or "").casefold()
+            if stage == PACKAGE_STAGE_FINAL and st in {
+                "hook_ready",
+                "hook_partial",
+                "deferred",
+            }:
+                failures.append(f"td_binding_not_pass:{st or 'empty'}")
+            if st == "hook_ready":
+                identity = _read_zip_json(archive, "meta/release-set-identity.json")
+                if isinstance(identity, dict) and identity.get("bitstream_sha256"):
+                    failures.append("td_binding_hook_ready_while_bitstream_claimed")
+            break
+
     required_meta_suffixes = (
         "meta/git/HEAD.txt",
         "meta/git/branch.txt",
@@ -650,27 +677,6 @@ def run_compact_zip_selftest(
     # Intermediate may omit TD binding; final requires it.
     if binding_status == "missing" and stage == PACKAGE_STAGE_FINAL:
         failures.append("missing_td_source_closure_binding")
-    # Round-9: never ship final (or bitstream-claiming) packages stuck on hook_ready.
-    for name in members:
-        norm = _normalize_member(name)
-        if not (
-            norm.endswith("meta/td/td-source-closure-binding.json")
-            or norm.endswith("meta/td-source-closure-binding.json")
-        ):
-            continue
-        try:
-            binding_doc = json.loads(archive.read(name).decode("utf-8"))
-        except Exception as exc:  # noqa: BLE001
-            failures.append(f"td_binding_unreadable:{exc}")
-            break
-        st = str(binding_doc.get("status") or "").casefold()
-        if stage == PACKAGE_STAGE_FINAL and st in {"hook_ready", "hook_partial", "deferred"}:
-            failures.append(f"td_binding_not_pass:{st or 'empty'}")
-        if st == "hook_ready":
-            identity = _read_zip_json(archive, "meta/release-set-identity.json")
-            if isinstance(identity, dict) and identity.get("bitstream_sha256"):
-                failures.append("td_binding_hook_ready_while_bitstream_claimed")
-        break
 
     # Structure selftest ≠ software replay.
     if not skip_software: