1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
"""Test decoding fields in _meshcop._udp.local. services."""
import pytest
from python_otbr_api.mdns import (
Availability,
ConnectionMode,
StateBitmap,
ThreadInterfaceStatus,
)
@pytest.mark.parametrize(
"encoded, decoded",
[
(
b"\x00\x00\x01\xb1",
StateBitmap(
connection_mode=ConnectionMode.PSKC,
thread_interface_status=ThreadInterfaceStatus.ACTIVE,
availability=Availability.HIGH,
is_active=True,
is_primary=True,
),
),
(
b"\x00\x00\x00!",
StateBitmap(
connection_mode=ConnectionMode.PSKC,
thread_interface_status=ThreadInterfaceStatus.NOT_INITIALIZED,
availability=Availability.HIGH,
is_active=False,
is_primary=False,
),
),
],
)
def test_decode_state_bitmap(encoded, decoded) -> None:
"""Test the TLV parser."""
assert StateBitmap.from_bytes(encoded) == decoded
@pytest.mark.parametrize(
"encoded, error",
[
# Input not bytes
("blah", TypeError),
# Wrong length
(b"\x00\x01\xb1", ValueError),
# Padding not zeroed
(b"\xff\x00\x01\xb1", ValueError),
# Invalid ConnectionMode
(b"\x00\x00\x01\xb5", ValueError),
# Invalid ThreadInterfaceStatus
(b"\x00\x00\x01\xb9", ValueError),
# Invalid Availability
(b"\x00\x00\x01\xf1", ValueError),
],
)
def test_decode_state_bitmap_error(encoded, error) -> None:
"""Test the TLV parser."""
with pytest.raises(error):
StateBitmap.from_bytes(encoded)
|