| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- from __future__ import annotations
- import json
- from pathlib import Path
- import zipfile
- import pytest
- from tools import package_target
- ROOT = Path(__file__).resolve().parents[2]
- TEST_COMMIT = "a" * 40
- def _receipt(
- tmp_path: Path, profile_name: str, commit: str = TEST_COMMIT
- ) -> Path:
- profile = package_target.HARDWARE_PROFILES[profile_name]
- 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_name,
- "project": str(profile["project"]),
- "source_closure": str(profile["closure"]),
- "commit": commit,
- "production": bool(profile["production"]),
- "stimulus": bool(profile["stimulus"]),
- "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"])
- ),
- "known_unbound_ports": ["host_ttl_rx", "host_ttl_tx"],
- "deployment_status": "UNSAFE_UNTIL_HOST_TTL_PINS_ARE_FROZEN_AND_HARDWARE_VALIDATED",
- "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_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="6400-control"
- )
- with pytest.raises(SystemExit, match="Sim-Debug"):
- package_target.build_target_package(
- "6400", "firmware", tmp_path, profile_name="6400-sim-debug"
- )
- @pytest.mark.parametrize("profile_name", ["6400-control", "6400-identification"])
- def test_td_receipt_is_profile_commit_closure_and_bit_bound(
- tmp_path: Path, profile_name: str
- ) -> None:
- receipt_path = _receipt(tmp_path, profile_name)
- commit = TEST_COMMIT
- bitstream, receipt = package_target._validated_td_bitstream(
- profile_name, receipt_path, commit
- )
- assert bitstream.name == f"{package_target.HARDWARE_PROFILES[profile_name]['artifact_basename']}.bit"
- assert receipt["profile"] == profile_name
- 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_name, receipt_path, commit)
- def test_control_source_package_contains_only_active_grouped_closure(tmp_path: Path) -> None:
- archive = package_target.build_target_package(
- "6400", "source", tmp_path, profile_name="6400-control"
- )
- 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"] == "6400-control"
- assert manifest["top"] == "prod2u_6400_v1_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"]["SET_TARGET_ACK"]["type"] == 0x81
- assert host_contract["wire"]["responses"]["GET_SNAPSHOT_ACK"]["type"] == 0x82
- assert host_contract["wire"]["responses"]["SNAPSHOT_DATA"]["type"] == 0x42
- assert host_contract["canonical_profiles"]["6400-control"]["stimulus"] is False
- assert not any(name.startswith("src/generated/6400/") for name in names)
- assert not any("prod2u_6400_front_back_top" in name for name in names)
- @pytest.mark.parametrize(
- ("profile_name", "expected_bit", "role", "production", "stimulus", "contract_text"),
- [
- (
- "6400-control",
- "prod2u_6400_front_back.bit",
- "production",
- True,
- False,
- "stimulus=false",
- ),
- (
- "6400-identification",
- "prod2u_6400_identification.bit",
- "identification",
- False,
- True,
- "不得作为 Production",
- ),
- ],
- )
- def test_firmware_package_is_fail_closed_and_profile_bound(
- tmp_path: Path,
- monkeypatch: pytest.MonkeyPatch,
- profile_name: str,
- expected_bit: str,
- role: str,
- production: bool,
- stimulus: bool,
- contract_text: str,
- ) -> None:
- monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT)
- receipt = _receipt(tmp_path, profile_name)
- archive = package_target.build_target_package(
- profile_name, "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_name
- assert manifest["firmware_role"] == role
- assert manifest["production"] is production
- assert manifest["stimulus"] is stimulus
- assert expected_bit in names
- assert "921600 8N1" in instructions
- assert "独立 ACK" in instructions
- assert contract_text in instructions
- assert instructions.startswith(package_target.DO_NOT_PROGRAM_LINE)
- assert manifest["release_blocked"] is True
- assert manifest["deployable"] is False
- assert manifest["known_unbound_ports"] == ["host_ttl_rx", "host_ttl_tx"]
- assert "CANDIDATE-NOTICE.zh-CN.txt" in names
|