| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #!/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,
- )
- if __name__ == "__main__":
- unittest.main()
|