|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import pytest |
| 4 | +import serial_asyncio |
| 5 | + |
| 6 | +from zigpy_cc import uart |
| 7 | + |
| 8 | + |
| 9 | +def eq(a, b): |
| 10 | + assert str(a) == str(b) |
| 11 | + |
| 12 | +@pytest.fixture(scope="function") |
| 13 | +def gw(): |
| 14 | + gw = uart.Gateway(mock.MagicMock()) |
| 15 | + gw._transport = mock.MagicMock() |
| 16 | + return gw |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.asyncio |
| 20 | +async def test_connect(monkeypatch): |
| 21 | + api = mock.MagicMock() |
| 22 | + portmock = mock.MagicMock() |
| 23 | + transport = mock.MagicMock() |
| 24 | + |
| 25 | + async def mock_conn(loop, protocol_factory, **kwargs): |
| 26 | + protocol = protocol_factory() |
| 27 | + loop.call_soon(protocol.connection_made, transport) |
| 28 | + return None, protocol |
| 29 | + |
| 30 | + monkeypatch.setattr(serial_asyncio, "create_serial_connection", mock_conn) |
| 31 | + |
| 32 | + await uart.connect(portmock, 57600, api) |
| 33 | + |
| 34 | + |
| 35 | +def test_write(gw): |
| 36 | + data = b"\x00" |
| 37 | + gw.write(data) |
| 38 | + assert gw._transport.write.call_count == 1 |
| 39 | + assert gw._transport.write.called_once_with(data) |
| 40 | + |
| 41 | + |
| 42 | +def test_close(gw): |
| 43 | + gw.close() |
| 44 | + assert gw._transport.close.call_count == 1 |
| 45 | + |
| 46 | + |
| 47 | +def test_data_received_chunk_frame(gw): |
| 48 | + data = b"\xfe\x0ea\x02\x02\x00\x02\x06\x03\x90\x154\x01\x02\x00\x00\x00\x00\xda" |
| 49 | + gw.data_received(data[:-4]) |
| 50 | + assert gw._api.data_received.call_count == 0 |
| 51 | + gw.data_received(data[-4:]) |
| 52 | + assert gw._api.data_received.call_count == 1 |
| 53 | + eq(gw._api.data_received.call_args[0][0], uart.UnpiFrame(3, 1, 2, bytearray(data[4:-1]), 14, 218)) |
| 54 | + |
| 55 | + |
| 56 | +def test_data_received_full_frame(gw): |
| 57 | + data = b"\xfe\x0ea\x02\x02\x00\x02\x06\x03\x90\x154\x01\x02\x01\x00\x00\x00\xdb" |
| 58 | + gw.data_received(data) |
| 59 | + assert gw._api.data_received.call_count == 1 |
| 60 | + eq(gw._api.data_received.call_args[0][0], uart.UnpiFrame(3, 1, 2, bytearray(data[4:-1]), 14, 219)) |
| 61 | + |
| 62 | + |
| 63 | +def test_data_received_incomplete_frame(gw): |
| 64 | + data = b"~\x00\x00" |
| 65 | + gw.data_received(data) |
| 66 | + assert gw._api.data_received.call_count == 0 |
| 67 | + |
| 68 | + |
| 69 | +def test_data_received_runt_frame(gw): |
| 70 | + data = b"\x02\x44\xC0" |
| 71 | + gw.data_received(data) |
| 72 | + assert gw._api.data_received.call_count == 0 |
| 73 | + |
| 74 | + |
| 75 | +def test_data_received_extra(gw): |
| 76 | + data = b"\xfe\x0ea\x02\x02\x00\x02\x06\x03\x90\x154\x01\x02\x01\x00\x00\x00\xdb\xfe\x00" |
| 77 | + gw.data_received(data) |
| 78 | + assert gw._api.data_received.call_count == 1 |
| 79 | + assert gw._parser.buffer == b"\xfe\x00" |
| 80 | + |
| 81 | + |
| 82 | +def test_data_received_wrong_checksum(gw): |
| 83 | + data = b"\xfe\x0ea\x02\x02\x00\x02\x06\x03\x90\x154\x01\x02\x01\x00\x00\x00\xdc" |
| 84 | + gw.data_received(data) |
| 85 | + assert gw._api.data_received.call_count == 0 |
| 86 | + |
| 87 | +@pytest.mark.skip("TODO") |
| 88 | +def test_unescape(gw): |
| 89 | + data = b"\x00\xDB\xDC\x00\xDB\xDD\x00\x00\x00" |
| 90 | + data_unescaped = b"\x00\xC0\x00\xDB\x00\x00\x00" |
| 91 | + r = gw._unescape(data) |
| 92 | + assert r == data_unescaped |
| 93 | + |
| 94 | +@pytest.mark.skip("TODO") |
| 95 | +def test_unescape_error(gw): |
| 96 | + data = b"\x00\xDB\xDC\x00\xDB\xDD\x00\x00\x00\xDB" |
| 97 | + r = gw._unescape(data) |
| 98 | + assert r is None |
| 99 | + |
| 100 | +@pytest.mark.skip("TODO") |
| 101 | +def test_escape(gw): |
| 102 | + data = b"\x00\xC0\x00\xDB\x00\x00\x00" |
| 103 | + data_escaped = b"\x00\xDB\xDC\x00\xDB\xDD\x00\x00\x00" |
| 104 | + r = gw._escape(data) |
| 105 | + assert r == data_escaped |
| 106 | + |
| 107 | + |
| 108 | +def test_checksum(): |
| 109 | + data = b"\x07\x01\x00\x08\x00\xaa\x00\x02" |
| 110 | + checksum = 166 |
| 111 | + r = uart.UnpiFrame.calculate_checksum(data) |
| 112 | + assert r == checksum |
0 commit comments