test_codex_harness.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import hashlib
  2. import json
  3. import tempfile
  4. import unittest
  5. from pathlib import Path
  6. from tools import codex_harness
  7. from tools import prod2u_p4_harness
  8. from tools import prod2u_p4_normal_v3_build
  9. class CodexHarnessTests(unittest.TestCase):
  10. def test_run_id_is_lowercase_32_hex(self):
  11. value = codex_harness.new_run_id()
  12. self.assertRegex(value, r"^[0-9a-f]{32}$")
  13. def test_source_manifest_is_project_bound_and_excludes_legacy_protocol_sources(self):
  14. manifest = codex_harness.build_source_manifest(Path.cwd())
  15. paths = manifest["sources"]
  16. self.assertIn("src/top/prod2u_p4_normal_v3_top.v", paths)
  17. self.assertIn("src/common/frame_rx.v", paths)
  18. self.assertNotIn("src/rtl/binary921_crc16.v", paths)
  19. self.assertNotIn("src/rtl/binary921_frame_tx.v", paths)
  20. self.assertEqual(manifest["project_id"], "prod2u-door-temp")
  21. self.assertRegex(manifest["project_sha256"], r"^[0-9a-f]{64}$")
  22. def test_unverified_package_binds_commit_project_and_bitstream_hash(self):
  23. with tempfile.TemporaryDirectory() as directory:
  24. root = Path(directory)
  25. bitstream = root / "candidate.bit"
  26. bitstream.write_bytes(b"candidate-bitstream")
  27. project = root / "project.json"
  28. project.write_text('{"project_id":"prod2u-door-temp"}\n', encoding="utf-8")
  29. package = codex_harness.create_unverified_package(
  30. bitstream=bitstream,
  31. project=project,
  32. commit="0123abcd",
  33. run_id="a" * 32,
  34. output_root=root / "dist",
  35. )
  36. manifest = json.loads((package / "package_manifest.json").read_text(encoding="utf-8"))
  37. self.assertEqual(manifest["status"], "UNVERIFIED")
  38. self.assertEqual(manifest["commit"], "0123abcd")
  39. self.assertEqual(manifest["run_id"], "a" * 32)
  40. self.assertEqual(
  41. manifest["bitstream_sha256"],
  42. hashlib.sha256(b"candidate-bitstream").hexdigest(),
  43. )
  44. self.assertTrue((package / "fw.bit").is_file())
  45. self.assertEqual(package.name, "0123abcd-" + manifest["bitstream_sha256"][:16])
  46. package_info = json.loads((package / "package-info.json").read_text(encoding="utf-8"))
  47. self.assertEqual(package_info["schema"], "prod2u-p4-normal-v3-folder-package-info-v3")
  48. self.assertEqual(package_info["bitstream"], "fw.bit")
  49. self.assertEqual(package_info["bitstream_sha256"], manifest["bitstream_sha256"])
  50. self.assertTrue((package / "protocol.md").is_file())
  51. self.assertTrue((package / "requirements.zh-CN.md").is_file())
  52. self.assertTrue((package / "source_manifest.json").is_file())
  53. self.assertTrue((package / "verification_targets.json").is_file())
  54. for relative in (
  55. "evidence/td-primary-receipt.json",
  56. "evidence/td-confirmation-receipt.json",
  57. "evidence/python-unit-receipt.json",
  58. "evidence/icarus-receipt.json",
  59. "evidence/verilator-receipt.json",
  60. "evidence/cocotb-receipt.json",
  61. "evidence/formal-receipt.json",
  62. "evidence/validation-summary.json",
  63. ):
  64. self.assertTrue((package / relative).is_file(), relative)
  65. def test_unverified_package_matches_prod2u_selftest_contract(self):
  66. with tempfile.TemporaryDirectory() as directory:
  67. root = Path(directory)
  68. bitstream = root / "candidate.bit"
  69. bitstream.write_bytes(b"candidate-bitstream")
  70. package = codex_harness.create_unverified_package(
  71. bitstream=bitstream,
  72. project=Path("projects/prod2u-door-temp/project.json"),
  73. commit="0123abcd",
  74. run_id="b" * 32,
  75. output_root=root / "dist",
  76. )
  77. receipt = prod2u_p4_harness.package_selftest_payload(
  78. package,
  79. verification_run_id="b" * 32,
  80. )
  81. self.assertEqual(receipt["status"], "pass")
  82. self.assertEqual(receipt["failures"], [])
  83. def test_build_source_list_and_build_info_use_only_the_new_contract(self):
  84. sources = prod2u_p4_normal_v3_build.PRODUCTION_RTL_SOURCES
  85. self.assertIn("src/common/frame_rx.v", sources)
  86. self.assertIn("src/top/prod2u_p4_normal_v3_top.v", sources)
  87. self.assertNotIn("src/rtl/binary921_crc16.v", sources)
  88. self.assertNotIn("src/rtl/prod2u_p4_normal_v3_watchdog.v", sources)
  89. with tempfile.TemporaryDirectory() as directory:
  90. bitstream = Path(directory) / "candidate.bit"
  91. bitstream.write_bytes(b"candidate")
  92. info = prod2u_p4_normal_v3_build.create_build_info(
  93. bitstream=bitstream,
  94. commit="0123abcd",
  95. )
  96. self.assertEqual(info["serial"], {"baud": 115200, "format": "8N1", "flow_control": "none"})
  97. self.assertEqual(
  98. info["hardware_access"],
  99. {"com": False, "jtag": False, "programming": False, "drivers": False},
  100. )
  101. def test_runtime_profile_is_the_new_115200_contract(self):
  102. self.assertEqual(
  103. prod2u_p4_harness.RUNTIME_PROFILE,
  104. "prod2u-door-temp-115200-v1",
  105. )
  106. def test_qor_attribute_count_accepts_td_gate_qor_name(self):
  107. with tempfile.TemporaryDirectory() as directory:
  108. root = Path(directory)
  109. (root / "gate.qor").write_text(
  110. "| #async_reg | 6 |\n",
  111. encoding="ascii",
  112. )
  113. observed = prod2u_p4_harness.parse_qor_attribute_count(
  114. root / "prod2u_p4_normal_v3_gate.qor",
  115. "async_reg",
  116. )
  117. self.assertEqual(observed, 6)
  118. def test_verification_inventory_uses_only_the_current_manifest(self):
  119. manifest = prod2u_p4_harness.load_target_manifest()
  120. self.assertEqual(
  121. manifest["schema"],
  122. "prod2u-simple-verification-target-manifest-v1",
  123. )
  124. self.assertEqual(
  125. [target["name"] for target in prod2u_p4_harness.cocotb_tests()],
  126. ["prod2u_door_temp_uart_e2e"],
  127. )
  128. self.assertEqual(
  129. [target["name"] for target in prod2u_p4_harness.formal_targets()],
  130. ["simple_frame_contract", "simple_motor_temperature_contract"],
  131. )
  132. icarus_names = [target["name"] for target in prod2u_p4_harness.hdl_testbenches()]
  133. self.assertEqual(
  134. icarus_names,
  135. sorted(manifest["layers"]["icarus"]),
  136. )
  137. if __name__ == "__main__":
  138. unittest.main()