test_negative_tool_fixtures.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from __future__ import annotations
  2. import shutil
  3. import subprocess
  4. import tempfile
  5. import unittest
  6. from pathlib import Path
  7. from tools.prod2u_p4_harness import resolve_tool, tool_env
  8. ROOT = Path(__file__).resolve().parents[2]
  9. FIXTURES = ROOT / "tests" / "fixtures"
  10. PLANNER_RTL = ROOT / "src" / "modules" / "door_percent_planner.v"
  11. PLANNER_TB = ROOT / "tests" / "sim" / "door_percent_planner_tb.v"
  12. def _run_mutated_planner_tb() -> tuple[
  13. subprocess.CompletedProcess[str],
  14. subprocess.CompletedProcess[str],
  15. str,
  16. ]:
  17. iverilog = resolve_tool("iverilog")
  18. vvp = resolve_tool("vvp")
  19. missing = [name for name, executable in (("iverilog", iverilog), ("vvp", vvp)) if not executable]
  20. if missing:
  21. raise unittest.SkipTest(
  22. "production RTL mutation gate unavailable: " + ", ".join(missing)
  23. )
  24. original_source = PLANNER_RTL.read_text(encoding="utf-8")
  25. comparison = "acc <= 8'd50;"
  26. mutation = "acc <= 8'd0;"
  27. if original_source.count(comparison) != 1:
  28. raise AssertionError("door_percent_planner.v mutation anchor is not unique")
  29. with tempfile.TemporaryDirectory(prefix="prod2u-planner-mutation-") as directory:
  30. workdir = Path(directory)
  31. mutated_rtl = workdir / PLANNER_RTL.name
  32. shutil.copy2(PLANNER_RTL, mutated_rtl)
  33. mutated_source = mutated_rtl.read_text(encoding="utf-8").replace(
  34. comparison,
  35. mutation,
  36. 1,
  37. )
  38. if mutated_source == original_source:
  39. raise AssertionError("planner mutation did not change the RTL copy")
  40. mutated_rtl.write_text(mutated_source, encoding="utf-8", newline="")
  41. output = workdir / "door_percent_planner_mutated.vvp"
  42. compile_result = subprocess.run(
  43. [
  44. iverilog,
  45. "-g2012",
  46. "-s",
  47. "door_percent_planner_tb",
  48. "-o",
  49. str(output),
  50. str(PLANNER_TB),
  51. str(mutated_rtl),
  52. ],
  53. cwd=workdir,
  54. env=tool_env(),
  55. text=True,
  56. stdout=subprocess.PIPE,
  57. stderr=subprocess.PIPE,
  58. timeout=60,
  59. check=False,
  60. )
  61. if compile_result.returncode != 0:
  62. return compile_result, compile_result, original_source
  63. run_result = subprocess.run(
  64. [vvp, str(output)],
  65. cwd=workdir,
  66. env=tool_env(),
  67. text=True,
  68. stdout=subprocess.PIPE,
  69. stderr=subprocess.PIPE,
  70. timeout=60,
  71. check=False,
  72. )
  73. return compile_result, run_result, original_source
  74. class NegativeToolFixtureTests(unittest.TestCase):
  75. def _run_fixture(self, tool: str, args: list[str], *, cwd: Path) -> subprocess.CompletedProcess[str]:
  76. executable = resolve_tool(tool)
  77. if not executable:
  78. self.skipTest(f"{tool} is unavailable in the configured toolchain")
  79. return subprocess.run(
  80. [executable, *args],
  81. cwd=cwd,
  82. env=tool_env(),
  83. text=True,
  84. stdout=subprocess.PIPE,
  85. stderr=subprocess.PIPE,
  86. timeout=60,
  87. check=False,
  88. )
  89. def test_icarus_rejects_negative_fixture(self):
  90. with tempfile.TemporaryDirectory() as directory:
  91. output = Path(directory) / "bad.vvp"
  92. result = self._run_fixture(
  93. "iverilog",
  94. ["-g2012", "-o", str(output), str(FIXTURES / "bad_icarus_tb.v")],
  95. cwd=FIXTURES,
  96. )
  97. self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
  98. def test_verilator_rejects_negative_fixture(self):
  99. result = self._run_fixture(
  100. "verilator",
  101. ["--lint-only", str(FIXTURES / "bad_verilator_lint.v")],
  102. cwd=FIXTURES,
  103. )
  104. self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
  105. def test_formal_runner_rejects_negative_fixture(self):
  106. sby = resolve_tool("sby")
  107. if not sby:
  108. self.skipTest("sby is unavailable in the configured toolchain")
  109. with tempfile.TemporaryDirectory() as directory:
  110. root = Path(directory)
  111. shutil.copy2(FIXTURES / "bad_formal_fail.v", root / "bad_formal_fail.v")
  112. shutil.copy2(FIXTURES / "bad_formal_fail.sby", root / "bad_formal_fail.sby")
  113. result = subprocess.run(
  114. [sby, "-f", "bad_formal_fail.sby"],
  115. cwd=root,
  116. env=tool_env(),
  117. text=True,
  118. stdout=subprocess.PIPE,
  119. stderr=subprocess.PIPE,
  120. timeout=60,
  121. check=False,
  122. )
  123. self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
  124. def test_real_production_planner_mutation_is_rejected_by_icarus(self):
  125. compile_result, run_result, original_source = _run_mutated_planner_tb()
  126. self.assertEqual(
  127. compile_result.returncode,
  128. 0,
  129. compile_result.stdout + compile_result.stderr,
  130. )
  131. self.assertNotEqual(
  132. run_result.returncode,
  133. 0,
  134. run_result.stdout + run_result.stderr,
  135. )
  136. self.assertEqual(
  137. PLANNER_RTL.read_text(encoding="utf-8"),
  138. original_source,
  139. )
  140. if __name__ == "__main__":
  141. unittest.main()