feat: prove and decode K1 realtime MQTT streams
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import pytest
|
||||
|
||||
from k1link.ble.wifi_provisioning import (
|
||||
FRAME_LENGTH,
|
||||
build_wifi_provisioning_frame,
|
||||
parse_wifi_status,
|
||||
)
|
||||
|
||||
|
||||
def test_build_wifi_provisioning_frame_layout() -> None:
|
||||
frame = build_wifi_provisioning_frame("LabNet", "correct horse")
|
||||
|
||||
assert len(frame) == FRAME_LENGTH
|
||||
assert frame[0] == 6
|
||||
assert frame[1:7] == b"LabNet"
|
||||
assert frame[7:33] == bytes(26)
|
||||
assert frame[33] == 13
|
||||
assert frame[34:47] == b"correct horse"
|
||||
assert frame[47:98] == bytes(51)
|
||||
assert frame[98] == 0
|
||||
|
||||
|
||||
def test_build_wifi_provisioning_frame_uses_utf8_byte_lengths() -> None:
|
||||
frame = build_wifi_provisioning_frame("Сеть", "пароль")
|
||||
|
||||
assert frame[0] == len("Сеть".encode())
|
||||
assert frame[33] == len("пароль".encode())
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("ssid", "password", "message"),
|
||||
[
|
||||
("", "password", "SSID must not be empty"),
|
||||
("network", "", "password must not be empty"),
|
||||
("x" * 33, "password", "at most 32 UTF-8 bytes"),
|
||||
("network", "x" * 65, "at most 64 UTF-8 bytes"),
|
||||
],
|
||||
)
|
||||
def test_build_wifi_provisioning_frame_rejects_invalid_lengths(
|
||||
ssid: str,
|
||||
password: str,
|
||||
message: str,
|
||||
) -> None:
|
||||
with pytest.raises(ValueError, match=message):
|
||||
build_wifi_provisioning_frame(ssid, password)
|
||||
|
||||
|
||||
def test_parse_wifi_status_ap_baseline() -> None:
|
||||
value = bytearray(54)
|
||||
value[0] = 7
|
||||
value[1:8] = b"WIFI_AP"
|
||||
value[33] = 4
|
||||
value[34:38] = bytes((192, 168, 56, 1))
|
||||
value[50] = 1
|
||||
value[52:54] = b"XX"
|
||||
|
||||
assert parse_wifi_status(bytes(value)) == {
|
||||
"value_length": 54,
|
||||
"mode": "WIFI_AP",
|
||||
"ipv4": "192.168.56.1",
|
||||
"status_code": 1,
|
||||
"reserved": 0,
|
||||
"trailer_hex": "5858",
|
||||
}
|
||||
|
||||
|
||||
def test_parse_wifi_status_rejects_short_frame() -> None:
|
||||
with pytest.raises(ValueError, match="at least 51 bytes"):
|
||||
parse_wifi_status(bytes(50))
|
||||
Reference in New Issue
Block a user