test: model modbus refusals and request accounting in grpc fixture

This commit is contained in:
Joseph Doherty
2026-11-16 06:05:59 -05:00
parent daea02b271
commit 09f4f18d0e
4 changed files with 256 additions and 27 deletions
@@ -56,13 +56,20 @@ class ModbusFixtureTests(unittest.TestCase):
self.assertEqual(self.state.snapshot()["effect_total"], 2)
def test_malformed_and_out_of_range_requests_never_mutate(self):
invalid = [b"", b"\x06\x00", request(3, 0, 0), request(3, 0, 126),
request(6, 999, 1), bytes([16]) + struct.pack(">HHB", 30, 2, 2) + b"\x00\x01"]
invalid = [(b"\x06\x00", b"\x86\x03"),
(request(3, 0, 0), b"\x83\x03"),
(request(3, 0, 126), b"\x83\x03"),
(request(6, 999, 1), b"\x86\x02"),
(bytes([16]) + struct.pack(">HHB", 30, 2, 2) + b"\x00\x01", b"\x90\x03")]
before = self.state.snapshot()
for payload in invalid:
with self.subTest(payload=payload), self.assertRaises(ValueError):
handle_modbus_pdu(self.state, 1, payload)
self.assertEqual(self.state.snapshot(), before)
for payload, expected in invalid:
with self.subTest(payload=payload):
self.assertEqual(handle_modbus_pdu(self.state, 1, payload), expected)
with self.assertRaises(ValueError):
handle_modbus_pdu(self.state, 1, b"")
after = self.state.snapshot()
for key in ("counter", "writable", "effects", "effect_total", "drop_next_write_response"):
self.assertEqual(after[key], before[key])
def test_control_rejects_unknown_actions_and_unbounded_input(self):
for command in ({"action": "set", "address": 20, "value": -1},