test_6400_profile_package.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from __future__ import annotations
  2. import json
  3. from pathlib import Path
  4. import zipfile
  5. import pytest
  6. from tools import package_target
  7. ROOT = Path(__file__).resolve().parents[2]
  8. TEST_COMMIT = "a" * 40
  9. def _receipt(
  10. tmp_path: Path, profile_name: str, commit: str = TEST_COMMIT
  11. ) -> Path:
  12. profile = package_target.HARDWARE_PROFILES[profile_name]
  13. bitstream = tmp_path / f"{profile['artifact_basename']}.bit"
  14. bitstream.write_bytes(b"TD-BIT-TEST")
  15. payload = {
  16. "schema": "prod2u-6400-td-receipt-v1",
  17. "status": "PASS",
  18. "profile": profile_name,
  19. "project": str(profile["project"]),
  20. "source_closure": str(profile["closure"]),
  21. "commit": commit,
  22. "production": bool(profile["production"]),
  23. "stimulus": bool(profile["stimulus"]),
  24. "simulation_only": False,
  25. "artifact_basename": str(profile["artifact_basename"]),
  26. "timing_pass_25mhz": True,
  27. "project_sha256": package_target._sha256(ROOT / str(profile["project"])),
  28. "source_closure_sha256": package_target._sha256(
  29. ROOT / str(profile["closure"])
  30. ),
  31. "known_unbound_ports": ["host_ttl_rx", "host_ttl_tx"],
  32. "deployment_status": "UNSAFE_UNTIL_HOST_TTL_PINS_ARE_FROZEN_AND_HARDWARE_VALIDATED",
  33. "bitstream": str(bitstream),
  34. "bitstream_sha256": package_target._sha256(bitstream),
  35. }
  36. path = tmp_path / "td-receipt.json"
  37. path.write_text(json.dumps(payload), encoding="utf-8")
  38. return path
  39. def test_firmware_requires_explicit_profile_and_td_receipt(
  40. tmp_path: Path, monkeypatch: pytest.MonkeyPatch
  41. ) -> None:
  42. monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT)
  43. with pytest.raises(SystemExit, match="explicitly select --profile"):
  44. package_target.build_target_package("6400", "firmware", tmp_path)
  45. with pytest.raises(SystemExit, match="explicit --td-receipt is required"):
  46. package_target.build_target_package(
  47. "6400", "firmware", tmp_path, profile_name="6400-control"
  48. )
  49. with pytest.raises(SystemExit, match="Sim-Debug"):
  50. package_target.build_target_package(
  51. "6400", "firmware", tmp_path, profile_name="6400-sim-debug"
  52. )
  53. @pytest.mark.parametrize("profile_name", ["6400-control", "6400-identification"])
  54. def test_td_receipt_is_profile_commit_closure_and_bit_bound(
  55. tmp_path: Path, profile_name: str
  56. ) -> None:
  57. receipt_path = _receipt(tmp_path, profile_name)
  58. commit = TEST_COMMIT
  59. bitstream, receipt = package_target._validated_td_bitstream(
  60. profile_name, receipt_path, commit
  61. )
  62. assert bitstream.name == f"{package_target.HARDWARE_PROFILES[profile_name]['artifact_basename']}.bit"
  63. assert receipt["profile"] == profile_name
  64. payload = json.loads(receipt_path.read_text(encoding="utf-8"))
  65. payload["source_closure_sha256"] = "0" * 64
  66. receipt_path.write_text(json.dumps(payload), encoding="utf-8")
  67. with pytest.raises(SystemExit, match="TD receipt mismatch"):
  68. package_target._validated_td_bitstream(profile_name, receipt_path, commit)
  69. def test_control_source_package_contains_only_active_grouped_closure(tmp_path: Path) -> None:
  70. archive = package_target.build_target_package(
  71. "6400", "source", tmp_path, profile_name="6400-control"
  72. )
  73. with zipfile.ZipFile(archive) as zipf:
  74. names = set(zipf.namelist())
  75. manifest = json.loads(zipf.read("manifest.json"))
  76. closure = json.loads(zipf.read("source-closure-expanded.json"))
  77. host_contract = json.loads(zipf.read("generated/6400/host-contract.json"))
  78. assert manifest["profile"] == "6400-control"
  79. assert manifest["top"] == "prod2u_6400_v1_top"
  80. assert "src/actuator/fan_board_output_adapter.v" in closure["sources"]
  81. assert host_contract["wire"]["host_uart"]["baud"] == 921600
  82. assert host_contract["wire"]["responses"]["SET_TARGET_ACK"]["type"] == 0x81
  83. assert host_contract["wire"]["responses"]["GET_SNAPSHOT_ACK"]["type"] == 0x82
  84. assert host_contract["wire"]["responses"]["SNAPSHOT_DATA"]["type"] == 0x42
  85. assert host_contract["canonical_profiles"]["6400-control"]["stimulus"] is False
  86. assert not any(name.startswith("src/generated/6400/") for name in names)
  87. assert not any("prod2u_6400_front_back_top" in name for name in names)
  88. @pytest.mark.parametrize(
  89. ("profile_name", "expected_bit", "role", "production", "stimulus", "contract_text"),
  90. [
  91. (
  92. "6400-control",
  93. "prod2u_6400_front_back.bit",
  94. "production",
  95. True,
  96. False,
  97. "stimulus=false",
  98. ),
  99. (
  100. "6400-identification",
  101. "prod2u_6400_identification.bit",
  102. "identification",
  103. False,
  104. True,
  105. "不得作为 Production",
  106. ),
  107. ],
  108. )
  109. def test_firmware_package_is_fail_closed_and_profile_bound(
  110. tmp_path: Path,
  111. monkeypatch: pytest.MonkeyPatch,
  112. profile_name: str,
  113. expected_bit: str,
  114. role: str,
  115. production: bool,
  116. stimulus: bool,
  117. contract_text: str,
  118. ) -> None:
  119. monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT)
  120. receipt = _receipt(tmp_path, profile_name)
  121. archive = package_target.build_target_package(
  122. profile_name, "firmware", tmp_path / "out", td_receipt=receipt
  123. )
  124. with zipfile.ZipFile(archive) as zipf:
  125. names = set(zipf.namelist())
  126. manifest = json.loads(zipf.read("manifest.json"))
  127. instructions = zipf.read("FLASH-INSTRUCTIONS.zh-CN.txt").decode("utf-8")
  128. assert manifest["profile"] == profile_name
  129. assert manifest["firmware_role"] == role
  130. assert manifest["production"] is production
  131. assert manifest["stimulus"] is stimulus
  132. assert expected_bit in names
  133. assert "921600 8N1" in instructions
  134. assert "独立 ACK" in instructions
  135. assert contract_text in instructions
  136. assert instructions.startswith(package_target.DO_NOT_PROGRAM_LINE)
  137. assert manifest["release_blocked"] is True
  138. assert manifest["deployable"] is False
  139. assert manifest["known_unbound_ports"] == ["host_ttl_rx", "host_ttl_tx"]
  140. assert "CANDIDATE-NOTICE.zh-CN.txt" in names