Files
NODEDC_MISSION_CORE/plugins/usb-can/tests/test_bridge.py
T
DCCONSTRUCTIONS 8b210258ca feat(usb-can): add isolated adapter runtime and confirmed CAN transactions
Package the adapter SDK boundary and runtime lifecycle. Confirm admitted CAN transactions, bound retained data and test failure paths with synthetic hardware.
2026-10-29 09:09:59 +03:00

151 lines
6.3 KiB
Python

"""Channel arbitration tests with an inert SDK only."""
import pytest
from test_runtime import Engine, FakeSDK
def setup():
clock = [10.0]
sdk = FakeSDK()
e = Engine(sdk, lambda: clock[0], lambda seconds: clock.__setitem__(0, clock[0] + seconds))
token = e.dispatch("bridge.open", {"channel": 0, "bitrate": 500000, "receive_id": 254})["token"]
return clock, sdk, e, token
def test_owner_excludes_manual_tx_and_expiry_stops_channel():
clock, sdk, e, token = setup()
for action in ("configure", "start", "stop", "clear", "transmit", "transmit.stop"):
with pytest.raises(ValueError):
e.dispatch(action, {"channel": 0})
clock[0] = 11
e.tick()
assert not e.running[0]
assert not e.bridges.claims
assert not any(c[0] == "tx" for c in sdk.calls)
with pytest.raises(ValueError):
e.dispatch("bridge.poll", {"token": token})
def test_confirmed_setter_and_no_replay_after_deadline():
clock, sdk, e, token = setup()
request = {"token": token, "frames": [{"id": 0x84A, "data": "fe000500000000"}],
"deadline": 10.2}
e.dispatch("bridge.send", request)
assert sdk.calls[-1][2]["send_type"] == 2
clock[0] = 10.3
with pytest.raises(ValueError):
e.dispatch("bridge.send", request)
assert len([c for c in sdk.calls if c[0] == "tx"]) == 1
def test_same_channel_distinct_response_addresses_and_last_close():
clock, sdk, e, one = setup()
two = e.dispatch("bridge.open", {"channel": 0, "bitrate": 500000, "receive_id": 253})["token"]
frame = {"id": 0x8FE, "channel": 0, "extended": True, "data": "4a0100"}
e.bridges.receive(frame)
assert e.dispatch("bridge.poll", {"token": one})["frames"] == [frame]
assert e.dispatch("bridge.poll", {"token": two})["frames"] == []
e.dispatch("bridge.close", {"token": one})
assert e.running[0]
e.dispatch("bridge.close", {"token": two})
assert not e.running[0]
def test_occupied_bitrate_conflict_and_receive_overflow_end_session():
clock, sdk, e, token = setup()
with pytest.raises(ValueError):
e.dispatch("bridge.open", {"channel": 0, "bitrate": 250000, "receive_id": 253})
for _ in range(513):
e.bridges.receive({"id": 0x8FE, "channel": 0, "extended": True})
with pytest.raises(ValueError):
e.dispatch("bridge.poll", {"token": token})
assert not e.running[0]
def test_status_does_not_enumerate_usb_during_active_bridge():
clock, sdk, e, token = setup()
def blocking_enumeration():
raise AssertionError("vendor USB enumeration must not block a live CAN lease")
sdk.online = blocking_enumeration
assert e.dispatch("status", {})["channels"][0]["owner"] == "vesc"
assert e.dispatch("bridge.poll", {"token": token}) == {"frames": []}
# Status is neither a lease renewal nor a deferred command replay.
clock[0] = 11
e.tick()
assert not e.bridges.claims and not e.running[0]
assert not any(c[0] == "tx" for c in sdk.calls)
sdk.online = lambda: False
with pytest.raises(OSError, match="USB"):
e.dispatch("status", {})
def test_sysfs_detach_retires_active_bridge_even_without_sdk_online(tmp_path):
from test_runtime import Attachment, FakeLink, Service
attachments = [Attachment("1-2:6", "1-2", "/dev/bus/usb/001/006", "480")]
service = Service(tmp_path, lambda: list(attachments), FakeLink)
service.scan()
device = next(iter(service.devices.values()))
link, session = device.link, device.session
request = {"adapter": device.id, "session": session, "action": "open",
"parameters": {"channel": 0, "bitrate": 500000, "receive_id": 254}}
service.bridge(request)
link.engine.sdk.online = lambda: (_ for _ in ()).throw(AssertionError("blocking USB check"))
service.scan()
assert device.link is link and device.activity is None
attachments.clear()
service.scan()
assert not link.alive and device.session != session and not service.devices
@pytest.mark.parametrize("code", range(256))
def test_only_exact_read_commands_have_arbitration_retry(code):
clock, sdk, e, token = setup()
e.dispatch("bridge.send", {"token": token,
"frames": [{"id": 0x84A, "data": bytes((254, 0, code)).hex()}],
"deadline": 10.2})
assert sdk.calls[-1][2]["send_type"] == (2 if code in (0, 4, 14, 17, 31, 62) else 1)
@pytest.mark.parametrize("frame", [
{"id": 0x84A, "data": "fe000400"}, # extra argument
{"id": 0x84A, "data": "fe003e00"}, # scan with unexpected argument
{"id": 0x8FF, "data": "fe003e"}, # broadcast scan
{"id": 0x84A, "data": "fe00224a3e"}, # forwarded scan
{"id": 0x84A, "data": "fd0004"}, # different reply address
{"id": 0x84A, "data": "fe0104"}, # reply, not request
{"id": 0x84A, "data": "fe00224a04"}, # forwarded command
{"id": 0x8FF, "data": "fe0004"}, # broadcast
{"id": 0x8FE, "data": "fe0004"}, # self
{"id": 0x74A, "data": "fe0004"}, # fragmented packet completion
])
def test_no_retry_for_ambiguous_or_forwarded_payload(frame):
clock, sdk, e, token = setup()
e.dispatch("bridge.send", {"token": token, "frames": [frame], "deadline": 10.2})
assert sdk.calls[-1][2]["send_type"] == 1
def test_read_timeout_closes_claim_without_application_replay():
clock, sdk, e, token = setup()
request = {"token": token, "frames": [{"id": 0x84A, "data": "fe0004"}], "deadline": 10.2}
e.dispatch("bridge.send", request)
e.dispatch("bridge.close", {"token": token})
assert not e.running[0]
with pytest.raises(ValueError):
e.dispatch("bridge.send", request)
assert len([c for c in sdk.calls if c[0] == "tx"]) == 1
def test_bridge_inventory_includes_exact_os_usb_generation(tmp_path):
from test_runtime import Attachment, FakeLink, Service
attached = [Attachment("1-2:6", "1-2", "/dev/bus/usb/001/006", "480")]
service = Service(tmp_path, lambda: list(attached), FakeLink)
service.scan()
first = service.bridge_inventory()["items"][0]
assert first["attachment_id"] == attached[0].id
attached[0] = Attachment("1-2:7", "1-2", "/dev/bus/usb/001/007", "480")
service.scan()
second = service.bridge_inventory()["items"][0]
assert first["id"] == second["id"]
assert first["attachment_id"] != second["attachment_id"]
assert first["session_id"] != second["session_id"]