from __future__ import annotations import hashlib import json from pathlib import Path import zipfile import pytest from tools import package_target ROOT = Path(__file__).resolve().parents[2] PROFILE = "6400-front-rear" TEST_COMMIT = "a" * 40 TEST_SOURCE_COMMIT = "b" * 40 def _receipt(tmp_path: Path, commit: str = TEST_SOURCE_COMMIT) -> Path: profile = package_target.HARDWARE_PROFILES[PROFILE] inputs = package_target.td_input_manifest(PROFILE) bitstream = tmp_path / f"{profile['artifact_basename']}.bit" bitstream.write_bytes(b"TD-BIT-TEST") payload = { "schema": "prod2u-6400-td-receipt-v1", "status": "PASS", "profile": PROFILE, "project": str(profile["project"]), "source_closure": str(profile["closure"]), "commit": commit, "production": True, "stimulus": False, "simulation_only": False, "artifact_basename": str(profile["artifact_basename"]), "timing_pass_25mhz": True, "project_sha256": package_target._sha256(ROOT / str(profile["project"])), "source_closure_sha256": package_target._sha256(ROOT / str(profile["closure"])), "source_manifest_sha256": hashlib.sha256( json.dumps(inputs, sort_keys=True, separators=(",", ":")).encode() ).hexdigest(), "inputs": inputs, "known_unbound_ports": [], "deployment_status": package_target._expected_deployment_status(PROFILE), "warning_audit": { "status": "PASS", "expected_unbound_ports": [], "observed_unbound_ports": [], "unexpected_unbound_ports": [], "missing_expected_unbound_ports": [], }, "bitstream": str(bitstream), "bitstream_sha256": package_target._sha256(bitstream), } path = tmp_path / "td-receipt.json" path.write_text(json.dumps(payload), encoding="utf-8") return path def test_only_front_rear_profile_is_active() -> None: assert set(package_target.HARDWARE_PROFILES) == {PROFILE} profile = package_target.HARDWARE_PROFILES[PROFILE] assert profile["production"] is True assert profile["stimulus"] is False def test_firmware_requires_explicit_profile_and_td_receipt( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT) with pytest.raises(SystemExit, match="explicitly select --profile"): package_target.build_target_package("6400", "firmware", tmp_path) with pytest.raises(SystemExit, match="explicit --td-receipt is required"): package_target.build_target_package( "6400", "firmware", tmp_path, profile_name=PROFILE ) with pytest.raises(SystemExit, match="explicitly select --profile"): package_target.build_target_package( "6400", "firmware", tmp_path, profile_name="6400-identification" ) def test_td_receipt_is_commit_closure_and_bit_bound(tmp_path: Path) -> None: receipt_path = _receipt(tmp_path) bitstream, receipt = package_target._validated_td_bitstream( PROFILE, receipt_path, TEST_COMMIT ) assert bitstream.name == "prod2u_6400_front_back.bit" assert receipt["profile"] == PROFILE assert receipt["commit"] == TEST_SOURCE_COMMIT payload = json.loads(receipt_path.read_text(encoding="utf-8")) payload["source_closure_sha256"] = "0" * 64 receipt_path.write_text(json.dumps(payload), encoding="utf-8") with pytest.raises(SystemExit, match="TD receipt mismatch"): package_target._validated_td_bitstream(PROFILE, receipt_path, TEST_COMMIT) @pytest.mark.parametrize( "tamper", ["input_digest", "input_missing", "ports_missing", "ports_duplicate", "warning_observed"], ) def test_td_receipt_rejects_tampered_provenance(tmp_path: Path, tamper: str) -> None: receipt_path = _receipt(tmp_path) payload = json.loads(receipt_path.read_text(encoding="utf-8")) if tamper == "input_digest": payload["inputs"][next(iter(payload["inputs"]))] = "0" * 64 elif tamper == "input_missing": payload["inputs"].pop(next(iter(payload["inputs"]))) elif tamper == "ports_missing": payload.pop("known_unbound_ports") elif tamper == "ports_duplicate": payload["known_unbound_ports"] = ["host_ttl_rx", "host_ttl_rx"] else: payload["warning_audit"]["observed_unbound_ports"].append("unexpected") receipt_path.write_text(json.dumps(payload), encoding="utf-8") with pytest.raises(SystemExit, match="TD receipt mismatch|warning audit mismatch"): package_target._validated_td_bitstream(PROFILE, receipt_path, TEST_COMMIT) def test_source_package_contains_only_active_closure(tmp_path: Path) -> None: archive = package_target.build_target_package( "6400", "source", tmp_path, profile_name=PROFILE ) with zipfile.ZipFile(archive) as zipf: names = set(zipf.namelist()) manifest = json.loads(zipf.read("manifest.json")) closure = json.loads(zipf.read("source-closure-expanded.json")) host_contract = json.loads(zipf.read("generated/6400/host-contract.json")) assert manifest["profile"] == PROFILE assert manifest["top"] == "prod2u_6400_rs232_only_top" assert "src/actuator/fan_board_output_adapter.v" in closure["sources"] assert host_contract["wire"]["host_uart"]["baud"] == 921600 assert host_contract["wire"]["responses"]["ACK_NACK"]["type"] == 0x06 assert set(host_contract["canonical_profiles"]) == {PROFILE} assert not any("identification" in name or "sim-debug" in name for name in names) def test_firmware_package_is_single_product_and_fail_closed( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT) receipt = _receipt(tmp_path) archive = package_target.build_target_package( PROFILE, "firmware", tmp_path / "out", td_receipt=receipt ) with zipfile.ZipFile(archive) as zipf: names = set(zipf.namelist()) manifest = json.loads(zipf.read("manifest.json")) instructions = zipf.read("FLASH-INSTRUCTIONS.zh-CN.txt").decode("utf-8") assert manifest["profile"] == PROFILE assert manifest["production"] is True and manifest["stimulus"] is False assert manifest["source_commit"] == TEST_SOURCE_COMMIT assert "prod2u_6400_front_back.bit" in names assert "source-manifest.json" in names assert "921600 8N1" in instructions and "UNSUPPORTED NACK" in instructions assert manifest["hardware_validated"] is False assert manifest["deployable"] is False assert manifest["known_unbound_ports"] == [] def test_firmware_selftest_rejects_unsafe_programming_instructions( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT) monkeypatch.setattr( package_target, "PRODUCTION_PROGRAM_LINE", "RS-232-ONLY PRODUCTION TD PASS\n3. 先执行 Erase/Program", ) receipt = _receipt(tmp_path) with pytest.raises(SystemExit, match="Host UART/ACK contract missing"): package_target.build_target_package( PROFILE, "firmware", tmp_path / "out", td_receipt=receipt )