8ddae2f43d
- device: read-only scale block (holding registers 1000..5095) + scale_set control action - seed: GRPC_DATA_SCALE=N adds SOURCE driver modscale/dev with N UInt16 tags in TagGroups small (first 160) / large (rest), one RawTagGroup export + CONSUMER mapping each (map-scale-small|large); fixture.json records the counts - rig.py: seed --scale N; deploy trigger gets a 60 s socket timeout (the AdminUI's StartDeployment ask waits up to 45 s; a 2048-tag snapshot read took ~25 s, so the general 15 s cap aborted the POST) - scale_gate.py: opens a >128 and a >1024-item mapping via Client CLI recursive subscribes on CONSUMER, requires all Good, churns the block, restarts the SOURCE pair, requires all Good with a post-restart value, checks the consumer ResetHistory counter and the container logs for 'history expired'; PASS/FAIL + evidence/S1026-results.json Co-Authored-By: Claude <noreply@anthropic.com>
101 lines
5.4 KiB
Python
101 lines
5.4 KiB
Python
"""Pure Modbus fixture tests; no sockets, Docker, or production process starts."""
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "device"))
|
|
from server import DeviceState, apply_control, handle_modbus_pdu
|
|
|
|
|
|
def request(function, address, value):
|
|
return bytes([function]) + struct.pack(">HH", address, value)
|
|
|
|
|
|
def words(response):
|
|
return struct.unpack(f">{response[1] // 2}H", response[2:])
|
|
|
|
|
|
class ModbusFixtureTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.state = DeviceState()
|
|
|
|
def test_counter_advances_only_by_explicit_bounded_control(self):
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (0,))
|
|
apply_control(self.state, {"action": "advance", "steps": 3})
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (3,))
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (3,))
|
|
with self.assertRaises(ValueError):
|
|
apply_control(self.state, {"action": "advance", "steps": 1001})
|
|
self.assertEqual(self.state.snapshot()["counter"], 3)
|
|
|
|
def test_scalar_and_array_registers_have_deterministic_readback(self):
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 10, 3))), (7, 11, 13))
|
|
self.assertEqual(handle_modbus_pdu(self.state, 1, request(6, 20, 42)), request(6, 20, 42))
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 20, 1))), (42,))
|
|
payload = bytes([16]) + struct.pack(">HHBHH", 30, 2, 4, 0x1234, 0x00FF)
|
|
self.assertEqual(handle_modbus_pdu(self.state, 1, payload), request(16, 30, 2))
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 30, 2))), (0x1234, 0x00FF))
|
|
|
|
def test_each_committed_write_has_one_effect_and_repeated_value_is_a_new_request(self):
|
|
write = request(6, 20, 42)
|
|
handle_modbus_pdu(self.state, 1, write)
|
|
handle_modbus_pdu(self.state, 1, write)
|
|
ledger = self.state.snapshot()["effects"]
|
|
self.assertEqual([item["sequence"] for item in ledger], [1, 2])
|
|
self.assertEqual([item["values"] for item in ledger], [[42], [42]])
|
|
self.assertEqual(self.state.snapshot()["effect_total"], 2)
|
|
|
|
def test_response_loss_is_armed_for_one_write_after_effect(self):
|
|
apply_control(self.state, {"action": "drop_next_write_response"})
|
|
self.assertIsNone(handle_modbus_pdu(self.state, 1, request(6, 20, 42)))
|
|
snapshot = self.state.snapshot()
|
|
self.assertEqual(snapshot["effect_total"], 1)
|
|
self.assertEqual(snapshot["effects"][0]["values"], [42])
|
|
self.assertEqual(handle_modbus_pdu(self.state, 1, request(6, 20, 43)), request(6, 20, 43))
|
|
self.assertEqual(self.state.snapshot()["effect_total"], 2)
|
|
|
|
def test_malformed_and_out_of_range_requests_never_mutate(self):
|
|
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, 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},
|
|
{"action": "set", "address": 999, "value": 3},
|
|
{"action": "advance", "steps": True},
|
|
{"action": "unknown"}):
|
|
with self.subTest(command=command), self.assertRaises(ValueError):
|
|
apply_control(self.state, command)
|
|
self.assertEqual(self.state.snapshot()["effect_total"], 0)
|
|
|
|
def test_scale_block_is_read_only_and_set_as_one_value(self):
|
|
# #1115: 4096 contiguous read-only registers from 1000; one control action sets them all.
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 1000, 125))), (1,) * 125)
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 5095 - 124, 125))), (1,) * 125)
|
|
self.assertEqual(handle_modbus_pdu(self.state, 1, request(3, 5096, 1)), b"\x83\x02")
|
|
self.assertEqual(handle_modbus_pdu(self.state, 1, request(3, 999, 2)), b"\x83\x02")
|
|
self.assertEqual(handle_modbus_pdu(self.state, 1, request(6, 1000, 5)), b"\x86\x02")
|
|
apply_control(self.state, {"action": "scale_set", "value": 4242})
|
|
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 2047, 3))), (4242,) * 3)
|
|
self.assertEqual(self.state.snapshot()["scale"], {"base": 1000, "count": 4096, "value": 4242, "sets": 1})
|
|
for invalid in (-1, 65536, "1", None):
|
|
with self.assertRaises(ValueError):
|
|
apply_control(self.state, {"action": "scale_set", "value": invalid})
|
|
self.assertEqual(self.state.snapshot()["effect_total"], 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|