test_6400_profile_package.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. from __future__ import annotations
  2. import hashlib
  3. import json
  4. from pathlib import Path
  5. import zipfile
  6. import pytest
  7. from tools import package_target
  8. ROOT = Path(__file__).resolve().parents[2]
  9. PROFILE = "6400-front-rear"
  10. TEST_COMMIT = "a" * 40
  11. TEST_SOURCE_COMMIT = "b" * 40
  12. def _receipt(tmp_path: Path, commit: str = TEST_SOURCE_COMMIT) -> Path:
  13. profile = package_target.HARDWARE_PROFILES[PROFILE]
  14. inputs = package_target.td_input_manifest(PROFILE)
  15. bitstream = tmp_path / f"{profile['artifact_basename']}.bit"
  16. bitstream.write_bytes(b"TD-BIT-TEST")
  17. payload = {
  18. "schema": "prod2u-6400-td-receipt-v1",
  19. "status": "PASS",
  20. "profile": PROFILE,
  21. "project": str(profile["project"]),
  22. "source_closure": str(profile["closure"]),
  23. "commit": commit,
  24. "production": True,
  25. "stimulus": False,
  26. "simulation_only": False,
  27. "artifact_basename": str(profile["artifact_basename"]),
  28. "timing_pass_25mhz": True,
  29. "project_sha256": package_target._sha256(ROOT / str(profile["project"])),
  30. "source_closure_sha256": package_target._sha256(ROOT / str(profile["closure"])),
  31. "source_manifest_sha256": hashlib.sha256(
  32. json.dumps(inputs, sort_keys=True, separators=(",", ":")).encode()
  33. ).hexdigest(),
  34. "inputs": inputs,
  35. "known_unbound_ports": [],
  36. "deployment_status": package_target._expected_deployment_status(PROFILE),
  37. "warning_audit": {
  38. "status": "PASS",
  39. "expected_unbound_ports": [],
  40. "observed_unbound_ports": [],
  41. "unexpected_unbound_ports": [],
  42. "missing_expected_unbound_ports": [],
  43. },
  44. "bitstream": str(bitstream),
  45. "bitstream_sha256": package_target._sha256(bitstream),
  46. }
  47. path = tmp_path / "td-receipt.json"
  48. path.write_text(json.dumps(payload), encoding="utf-8")
  49. return path
  50. def test_only_front_rear_profile_is_active() -> None:
  51. assert set(package_target.HARDWARE_PROFILES) == {PROFILE}
  52. profile = package_target.HARDWARE_PROFILES[PROFILE]
  53. assert profile["production"] is True
  54. assert profile["stimulus"] is False
  55. def test_firmware_requires_explicit_profile_and_td_receipt(
  56. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  57. ) -> None:
  58. monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT)
  59. with pytest.raises(SystemExit, match="explicitly select --profile"):
  60. package_target.build_target_package("6400", "firmware", tmp_path)
  61. with pytest.raises(SystemExit, match="explicit --td-receipt is required"):
  62. package_target.build_target_package(
  63. "6400", "firmware", tmp_path, profile_name=PROFILE
  64. )
  65. with pytest.raises(SystemExit, match="explicitly select --profile"):
  66. package_target.build_target_package(
  67. "6400", "firmware", tmp_path, profile_name="6400-identification"
  68. )
  69. def test_td_receipt_is_commit_closure_and_bit_bound(tmp_path: Path) -> None:
  70. receipt_path = _receipt(tmp_path)
  71. bitstream, receipt = package_target._validated_td_bitstream(
  72. PROFILE, receipt_path, TEST_COMMIT
  73. )
  74. assert bitstream.name == "prod2u_6400_front_back.bit"
  75. assert receipt["profile"] == PROFILE
  76. assert receipt["commit"] == TEST_SOURCE_COMMIT
  77. payload = json.loads(receipt_path.read_text(encoding="utf-8"))
  78. payload["source_closure_sha256"] = "0" * 64
  79. receipt_path.write_text(json.dumps(payload), encoding="utf-8")
  80. with pytest.raises(SystemExit, match="TD receipt mismatch"):
  81. package_target._validated_td_bitstream(PROFILE, receipt_path, TEST_COMMIT)
  82. @pytest.mark.parametrize(
  83. "tamper",
  84. ["input_digest", "input_missing", "ports_missing", "ports_duplicate", "warning_observed"],
  85. )
  86. def test_td_receipt_rejects_tampered_provenance(tmp_path: Path, tamper: str) -> None:
  87. receipt_path = _receipt(tmp_path)
  88. payload = json.loads(receipt_path.read_text(encoding="utf-8"))
  89. if tamper == "input_digest":
  90. payload["inputs"][next(iter(payload["inputs"]))] = "0" * 64
  91. elif tamper == "input_missing":
  92. payload["inputs"].pop(next(iter(payload["inputs"])))
  93. elif tamper == "ports_missing":
  94. payload.pop("known_unbound_ports")
  95. elif tamper == "ports_duplicate":
  96. payload["known_unbound_ports"] = ["host_ttl_rx", "host_ttl_rx"]
  97. else:
  98. payload["warning_audit"]["observed_unbound_ports"].append("unexpected")
  99. receipt_path.write_text(json.dumps(payload), encoding="utf-8")
  100. with pytest.raises(SystemExit, match="TD receipt mismatch|warning audit mismatch"):
  101. package_target._validated_td_bitstream(PROFILE, receipt_path, TEST_COMMIT)
  102. def test_source_package_contains_only_active_closure(tmp_path: Path) -> None:
  103. archive = package_target.build_target_package(
  104. "6400", "source", tmp_path, profile_name=PROFILE
  105. )
  106. with zipfile.ZipFile(archive) as zipf:
  107. names = set(zipf.namelist())
  108. manifest = json.loads(zipf.read("manifest.json"))
  109. closure = json.loads(zipf.read("source-closure-expanded.json"))
  110. host_contract = json.loads(zipf.read("generated/6400/host-contract.json"))
  111. assert manifest["profile"] == PROFILE
  112. assert manifest["top"] == "prod2u_6400_rs232_only_top"
  113. assert "src/actuator/fan_board_output_adapter.v" in closure["sources"]
  114. assert host_contract["wire"]["host_uart"]["baud"] == 921600
  115. assert host_contract["wire"]["responses"]["ACK_NACK"]["type"] == 0x06
  116. assert set(host_contract["canonical_profiles"]) == {PROFILE}
  117. assert not any("identification" in name or "sim-debug" in name for name in names)
  118. def test_firmware_package_is_single_product_and_fail_closed(
  119. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  120. ) -> None:
  121. monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT)
  122. receipt = _receipt(tmp_path)
  123. archive = package_target.build_target_package(
  124. PROFILE, "firmware", tmp_path / "out", td_receipt=receipt
  125. )
  126. with zipfile.ZipFile(archive) as zipf:
  127. names = set(zipf.namelist())
  128. manifest = json.loads(zipf.read("manifest.json"))
  129. instructions = zipf.read("FLASH-INSTRUCTIONS.zh-CN.txt").decode("utf-8")
  130. assert manifest["profile"] == PROFILE
  131. assert manifest["production"] is True and manifest["stimulus"] is False
  132. assert manifest["source_commit"] == TEST_SOURCE_COMMIT
  133. assert "prod2u_6400_front_back.bit" in names
  134. assert "source-manifest.json" in names
  135. assert "921600 8N1" in instructions and "UNSUPPORTED NACK" in instructions
  136. assert manifest["hardware_validated"] is False
  137. assert manifest["deployable"] is False
  138. assert manifest["known_unbound_ports"] == []
  139. def test_firmware_selftest_rejects_unsafe_programming_instructions(
  140. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  141. ) -> None:
  142. monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT)
  143. monkeypatch.setattr(
  144. package_target,
  145. "PRODUCTION_PROGRAM_LINE",
  146. "RS-232-ONLY PRODUCTION TD PASS\n3. 先执行 Erase/Program",
  147. )
  148. receipt = _receipt(tmp_path)
  149. with pytest.raises(SystemExit, match="Host UART/ACK contract missing"):
  150. package_target.build_target_package(
  151. PROFILE, "firmware", tmp_path / "out", td_receipt=receipt
  152. )