| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 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_current_products_are_both_production_profiles() -> None:
- assert set(package_target.HARDWARE_PROFILES) == {PROFILE, "mrdimm-chamber", "6400-v2"}
- for name, profile in package_target.HARDWARE_PROFILES.items():
- assert profile["production"] is True, name
- assert profile["stimulus"] is False, name
- 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"] in ("prod2u_6400_final_top", "prod2u_6400_rs232_only_top")
- assert "src/actuator/fan_board_output_adapter.v" in closure["sources"]
- assert host_contract["wire"]["uart"]["baud"] == 921600
- assert host_contract["wire"]["responses"]["SET_ACK"]["type"] == 0x81
- assert host_contract["wire"]["responses"]["GET_ACK"]["type"] == 0x82
- assert host_contract["wire"]["responses"]["GET_DATA"]["type"] == 0x42
- assert host_contract["wire"]["responses"]["NACK"]["type"] == 0xC0
- assert host_contract["wire"]["uart"]["length_width_bits"] == 8
- 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
- )
|