#!/usr/bin/env python3 """Unit tests for tools/build_firmware_package.py.""" from __future__ import annotations import json import shutil import subprocess import sys import tempfile import unittest import zipfile from pathlib import Path 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 ROOT = Path(__file__).resolve().parents[2] def _current_project_sha() -> str: return builder._sha256_file(ROOT / "projects" / "prod2u-door-temp" / "project.json") 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" report = { "run_id": "td-run-123", "verification_run_id": "88888888888888888888888888888888", "commit_full": "fe1a0a2a19622b6f290905ec14795bb6cce6899d", "project_config_sha256": project_sha, "td_source_manifest_sha256": "8e0da456e865f3c3afa613d2660c1ed0ebf0e422ef51ec7acef223c2017a14bd", "bitstream_sha256": bitstream_sha256, "synthesis_input_manifest": { "schema": "prod2u-simple-synthesis-inputs-v2", "sha256": "aa9f59236f8ffb89df318eaa5ed5037523d7f46e8b838d45c1e3ab44b60bc423", "inputs": { "src/generated/prod2u_door_temp_params.vh": "ffcb0ffbaef92ef63f96ba7ed6a048cb85d3e9d8a1a5b553e458252446fc6950", "projects/prod2u-door-temp/project.json": project_sha, "src/generated/td-source-manifest.json": "8e0da456e865f3c3afa613d2660c1ed0ebf0e422ef51ec7acef223c2017a14bd", }, }, "timing": { "fmax_mhz": 90.49, "worst_setup_slack_ns": 28.949, "worst_hold_slack_ns": 0.046, }, "resources": { "utilization": { "lut": {"used": 1113}, "reg": {"used": 677}, "bram": {"used": 0}, "dsp": {"used": 0}, }, "packing": {"total_slices": 570}, }, } path.write_text(json.dumps(report), encoding="utf-8") class BuildFirmwarePackageTests(unittest.TestCase): def _make_td_root(self, tmp_path: Path, *, bitstream_sha256: str | None = None, project_sha: str | None = None) -> tuple[Path, Path, Path]: 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") report = td_root / "report.json" _write_report(report, bitstream_sha256=bitstream_sha256, project_sha=project_sha) # Add a fake stage log to verify allowlist (td_root / "td" / "prod2u_p4_normal_v3" / "syn").mkdir(parents=True, exist_ok=True) (td_root / "td" / "prod2u_p4_normal_v3" / "syn" / "gate.qor").write_text("fake qor", encoding="utf-8") return td_root, bitstream, report def _commits(self) -> tuple[str, str, str]: # Use the artifact commit for all three in tests so git comparison succeeds. artifact = "fe1a0a2a19622b6f290905ec14795bb6cce6899d" return (artifact, artifact, artifact) def test_build_with_fake_bitstream_and_report(self) -> None: with tempfile.TemporaryDirectory() as tmp: tmp_path = Path(tmp) 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, ) self.assertTrue(package.is_file()) with zipfile.ZipFile(package, "r") as zipf: names = set(zipf.namelist()) self.assertIn("fw.bit", names) self.assertIn("BITSTREAM-SHA256.txt", names) self.assertIn("PACKAGE-MANIFEST.json", names) self.assertIn("SHA256SUMS.txt", names) self.assertIn("td/original-final_td_report.json", names) self.assertIn("td/TD-PROVENANCE.json", names) self.assertIn("evidence/SOURCE-CLOSURE-COMPARISON.json", names) self.assertIn("td/syn/gate.qor", names) manifest = json.loads(zipf.read("PACKAGE-MANIFEST.json")) self.assertEqual(manifest["hardware_validation"], "not_run") self.assertEqual(manifest["verification_scope"], "offline") self.assertTrue(manifest["source_closure_unchanged"]) self.assertEqual(manifest["schema"], "prod2u-door-temp-final-package-manifest-v2") sums = zipf.read("SHA256SUMS.txt").decode("utf-8") self.assertIn("fw.bit", sums) def test_bitstream_report_mismatch_blocks(self) -> None: with tempfile.TemporaryDirectory() as tmp: tmp_path = Path(tmp) td_root, bitstream, report = self._make_td_root(tmp_path, bitstream_sha256="0" * 64) artifact, matrix, final_dev = self._commits() with self.assertRaises(builder.PackagingError): builder.build_firmware_package( output_dir=tmp_path / "dist", package_name="mismatch", 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, ) def test_report_outside_td_root_blocks(self) -> None: with tempfile.TemporaryDirectory() as tmp: tmp_path = Path(tmp) td_root, bitstream, _ = self._make_td_root(tmp_path) other_report = tmp_path / "other_report.json" _write_report(other_report) artifact, matrix, final_dev = self._commits() with self.assertRaises(builder.PackagingError): builder.build_firmware_package( output_dir=tmp_path / "dist", package_name="bad-root", td_root=td_root, bitstream_path=bitstream, td_report_path=other_report, artifact_commit=artifact, matrix_commit=matrix, final_dev_commit=final_dev, source_closure_unchanged=True, ) def test_stale_td_logs_not_in_package(self) -> None: with tempfile.TemporaryDirectory() as tmp: tmp_path = Path(tmp) td_root, bitstream, report = self._make_td_root(tmp_path) # Place an old log in repo td/ that should not be included (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 zipfile.ZipFile(package, "r") as zipf: self.assertNotIn("td/td_20260630_134648.log", set(zipf.namelist())) finally: (ROOT / "td" / "td_20260630_134648.log").unlink(missing_ok=True) def test_missing_bitstream_blocks(self) -> None: with tempfile.TemporaryDirectory() as tmp: tmp_path = Path(tmp) td_root = tmp_path / "td_root" td_root.mkdir(parents=True, exist_ok=True) report = td_root / "report.json" _write_report(report) artifact, matrix, final_dev = self._commits() with self.assertRaises(FileNotFoundError): builder.build_firmware_package( output_dir=tmp_path / "dist", package_name="missing", td_root=td_root, artifact_commit=artifact, matrix_commit=matrix, final_dev_commit=final_dev, source_closure_unchanged=True, ) def test_source_closure_unavailable_without_git_is_not_fake_diff(self) -> None: td_report = { "synthesis_input_manifest": { "inputs": { "projects/prod2u-door-temp/project.json": "a" * 64, "src/generated/td-source-manifest.json": "b" * 64, } } } comparison = builder.generate_source_closure_comparison( td_report=td_report, artifact_commit="0" * 40, matrix_commit="1" * 40, final_dev_commit="2" * 40, repo_root=ROOT, ) self.assertEqual(comparison["mode"], "comparison_unavailable_without_git") self.assertIsNone(comparison["source_closure_equal"]) self.assertEqual(comparison["production_changed_paths"], []) self.assertTrue(all(entry["status"] == "unavailable" for entry in comparison["inputs"])) def test_source_closure_snapshot_hash_mode(self) -> None: project_sha = _current_project_sha() td_report = { "synthesis_input_manifest": { "inputs": { "projects/prod2u-door-temp/project.json": project_sha, } } } snapshot = { "artifact": {"projects/prod2u-door-temp/project.json": project_sha}, "matrix": {"projects/prod2u-door-temp/project.json": project_sha}, "final_dev": {"projects/prod2u-door-temp/project.json": project_sha}, } comparison = builder.generate_source_closure_comparison( td_report=td_report, artifact_commit="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", matrix_commit="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", final_dev_commit="cccccccccccccccccccccccccccccccccccccccc", snapshot_hashes=snapshot, ) self.assertEqual(comparison["mode"], "snapshot_hash") self.assertTrue(comparison["source_closure_equal"]) self.assertEqual(comparison["production_changed_paths"], []) def test_source_closure_live_commit_compare(self) -> None: # live_git remains strict; compact/detached hosts skip instead of hard-failing. sys.path.insert(0, str(ROOT / "tools")) from git_worktree_mode import require_live_git # noqa: WPS433 require_live_git(ROOT, self) head = subprocess.check_output( ["git", "-C", str(ROOT), "rev-parse", "HEAD"], text=True, ).strip() td_report = { "synthesis_input_manifest": { "inputs": { "projects/prod2u-door-temp/project.json": _current_project_sha(), } } } comparison = builder.generate_source_closure_comparison( td_report=td_report, artifact_commit=head, matrix_commit=head, final_dev_commit=head, repo_root=ROOT, ) self.assertEqual(comparison["mode"], "commit_compare") self.assertTrue(comparison["source_closure_equal"]) self.assertEqual(comparison["inputs"][0]["status"], "same") if __name__ == "__main__": unittest.main()