| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- from __future__ import annotations
- import shutil
- import subprocess
- import tempfile
- import unittest
- from pathlib import Path
- from tools.prod2u_p4_harness import resolve_tool, tool_env
- ROOT = Path(__file__).resolve().parents[2]
- FIXTURES = ROOT / "tests" / "fixtures"
- MOTOR_RTL = ROOT / "src" / "modules" / "door_motor_engine.v"
- MOTOR_TB = ROOT / "tests" / "sim" / "door_motor_engine_tb.v"
- def _run_mutated_motor_tb() -> tuple[
- subprocess.CompletedProcess[str],
- subprocess.CompletedProcess[str],
- str,
- ]:
- iverilog = resolve_tool("iverilog")
- vvp = resolve_tool("vvp")
- missing = [name for name, executable in (("iverilog", iverilog), ("vvp", vvp)) if not executable]
- if missing:
- raise unittest.SkipTest(
- "production RTL mutation gate unavailable: " + ", ".join(missing)
- )
- original_source = MOTOR_RTL.read_text(encoding="ascii")
- comparison = "if (rising_edges + 1 >= FIXED_STEPS) begin"
- mutation = "if (rising_edges + 1 >= (FIXED_STEPS - 1)) begin"
- if original_source.count(comparison) != 1:
- raise AssertionError("door_motor_engine.v mutation anchor is not unique")
- with tempfile.TemporaryDirectory(prefix="prod2u-motor-mutation-") as directory:
- workdir = Path(directory)
- mutated_rtl = workdir / MOTOR_RTL.name
- shutil.copy2(MOTOR_RTL, mutated_rtl)
- mutated_source = mutated_rtl.read_text(encoding="ascii").replace(
- comparison,
- mutation,
- 1,
- )
- if mutated_source == original_source:
- raise AssertionError("motor mutation did not change the RTL copy")
- mutated_rtl.write_text(mutated_source, encoding="ascii", newline="")
- output = workdir / "door_motor_engine_mutated.vvp"
- compile_result = subprocess.run(
- [
- iverilog,
- "-g2012",
- "-s",
- "door_motor_engine_tb",
- "-o",
- str(output),
- str(MOTOR_TB),
- str(mutated_rtl),
- ],
- cwd=workdir,
- env=tool_env(),
- text=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- timeout=60,
- check=False,
- )
- if compile_result.returncode != 0:
- return compile_result, compile_result, original_source
- run_result = subprocess.run(
- [vvp, str(output)],
- cwd=workdir,
- env=tool_env(),
- text=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- timeout=60,
- check=False,
- )
- return compile_result, run_result, original_source
- class NegativeToolFixtureTests(unittest.TestCase):
- def _run_fixture(self, tool: str, args: list[str], *, cwd: Path) -> subprocess.CompletedProcess[str]:
- executable = resolve_tool(tool)
- if not executable:
- self.skipTest(f"{tool} is unavailable in the configured toolchain")
- return subprocess.run(
- [executable, *args],
- cwd=cwd,
- env=tool_env(),
- text=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- timeout=60,
- check=False,
- )
- def test_icarus_rejects_negative_fixture(self):
- with tempfile.TemporaryDirectory() as directory:
- output = Path(directory) / "bad.vvp"
- result = self._run_fixture(
- "iverilog",
- ["-g2012", "-o", str(output), str(FIXTURES / "bad_icarus_tb.v")],
- cwd=FIXTURES,
- )
- self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
- def test_verilator_rejects_negative_fixture(self):
- result = self._run_fixture(
- "verilator",
- ["--lint-only", str(FIXTURES / "bad_verilator_lint.v")],
- cwd=FIXTURES,
- )
- self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
- def test_formal_runner_rejects_negative_fixture(self):
- sby = resolve_tool("sby")
- if not sby:
- self.skipTest("sby is unavailable in the configured toolchain")
- with tempfile.TemporaryDirectory() as directory:
- root = Path(directory)
- shutil.copy2(FIXTURES / "bad_formal_fail.v", root / "bad_formal_fail.v")
- shutil.copy2(FIXTURES / "bad_formal_fail.sby", root / "bad_formal_fail.sby")
- result = subprocess.run(
- [sby, "-f", "bad_formal_fail.sby"],
- cwd=root,
- env=tool_env(),
- text=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- timeout=60,
- check=False,
- )
- self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
- def test_real_production_motor_mutation_is_rejected_by_icarus(self):
- compile_result, run_result, original_source = _run_mutated_motor_tb()
- self.assertEqual(
- compile_result.returncode,
- 0,
- compile_result.stdout + compile_result.stderr,
- )
- self.assertNotEqual(
- run_result.returncode,
- 0,
- run_result.stdout + run_result.stderr,
- )
- self.assertEqual(
- (ROOT / "src" / "modules" / "door_motor_engine.v").read_text(encoding="ascii"),
- original_source,
- )
- if __name__ == "__main__":
- unittest.main()
|