Files
NODEDC_MISSION_CORE/tests/test_b2_web_protocol.py

75 lines
2.7 KiB
Python

"""Bounded configuration HTTP framing, independent from monitoring packets."""
import pytest
from k1link.device_plugins.arnavi_b2.usb_protocol import Configuration, ProtocolError, Setting
from k1link.device_plugins.arnavi_b2.web_protocol import (
decode_request,
decode_settings_reply,
settings_reply,
time_reply,
)
def request(command, payload=b""):
body = int("123456789012345").to_bytes(8, "little") + bytes.fromhex("0001e7020000")
body += bytes([command]) + payload
return b"[\x01" + len(body).to_bytes(2, "big") + body + b"]"
def test_upload_preserves_unknown_records_and_read_request_has_no_settings():
config = Configuration((Setting(62, b"\x00\x96"), Setting(222, b"opaque\x00\xff")))
imei, cmd, payload = decode_request(request(5, config.encode()))
assert imei == "123456789012345" and cmd == 5
assert payload == config.encode()
assert decode_request(request(2))[1:] == (2, b"")
assert decode_request(request(6, b"\x02"))[1:] == (6, b"\x02")
reply = settings_reply(config)
assert decode_settings_reply(reply).encode() == config.encode()
assert reply[3:5] == b"\x00\x02"
@pytest.mark.parametrize(
"raw",
[
request(1, b"extra"),
request(2, b"extra"),
request(6, b"\x01"),
request(5, b"no-crc"),
request(2).replace(bytes.fromhex("e702"), bytes.fromhex("e703")),
request(2)[:-1],
request(2) + b"extra",
b"\xff\x23" + b"x" * 18,
],
)
def test_unqualified_requests_fail_closed(raw):
with pytest.raises(ProtocolError):
decode_request(raw)
def test_reply_wrong_size_status_command_and_crc_fail_closed():
raw = settings_reply(Configuration((Setting(62, b"\x00\x96"),)))
for position in (0, 1, 2, 3, 4, 7, 8, 10, len(raw) - 1):
changed = bytearray(raw)
changed[position] ^= 1
with pytest.raises(ProtocolError):
decode_settings_reply(bytes(changed))
def test_engineering_empty_check_admission_never_accepts_images_or_unknown_opcodes():
assert decode_request(request(1), observe_empty_check=True)[1:] == (1, b"")
for raw in (request(1, b"image"), request(3), request(4), request(1) + b"extra"):
with pytest.raises(ProtocolError):
decode_request(raw, observe_empty_check=True)
def test_empty_time_request_and_big_endian_utc_reply():
assert decode_request(request(7))[1:] == (7, b"")
assert time_reply(1700000000) == bytes.fromhex("5b000600076553f1005d")
for raw in (request(7, b"extra"), request(7).replace(b"\xe7\x02", b"\xe7\x03")):
with pytest.raises(ProtocolError):
decode_request(raw)
for value in (True, 0, -1, 2**32, 1700000000.0, "1700000000"):
with pytest.raises(ProtocolError):
time_reply(value)