async def test_handleNodeListReport(gateway: ZIPGateway): async def ipOfNode(_): return ipaddress.IPv6Address("::1") listener = MagicMock() gateway.ipOfNode = ipOfNode gateway.addListener(listener) gateway._nodes = { 1: { "ip": ipaddress.IPv6Address("::1") }, 2: { "ip": ipaddress.IPv6Address("::1") }, } # pylint: disable=line-too-long zipPkt = b"#\x02@\x10\x00\x00\x00R\x02\x02\x00\x01!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" future = gateway.onUnsolicitedMessage(zipPkt, ("::1", )) await asyncio.gather(future) assert gateway._nodes == { 1: { "ip": ipaddress.IPv6Address("::1") }, 6: { "ip": ipaddress.IPv6Address("::1") }, } listener.nodeListUpdated.assert_called_once()
async def test_connect(gateway: ZIPGateway): gateway._nodes = {1: {}, 2: {}} gateway.ipOfNode = ipOfNode async def runScript(): await asyncio.sleep(0) gateway.commandReceived(ZipGateway.GatewayModeReport(mode=2)) await asyncio.gather(gateway.connect(), runScript())
def test_onUnsolicitedMessage(gateway: ZIPGateway): ip = ipaddress.IPv6Address("::ffff:c0a8:ee") gateway._nodes = {7: {"ip": ip}} pkt = b"#\x02\x80\xc0\xf9\x00\x00\x05\x84\x02\x00\x00%\x03\x00" assert gateway.onUnsolicitedMessage(pkt, (ip, 4123)) is True # Extract the flags from the call call = gateway.listener.messageReceived.mock_calls[1] args = call[1] flags = args[4] gateway.listener.messageReceived.assert_called_once_with( gateway, 7, 0, SwitchBinary.Report(value=0), flags) gateway._unsolicitedConnection.sendTo.assert_called_once_with( b"#\x02@\x00\xf9\x00\x00", (ip, 4123))
async def test_setupUnsolicitedConnection(gateway: ZIPGateway): gateway._nodes = {1: {}} await asyncio.gather( gateway.setupUnsolicitedConnection(), runDelayed( gateway.commandReceived, ZipND.ZipNodeAdvertisement( local=False, validity=0, nodeId=1, ipv6=ipaddress.IPv6Address("2001:db8::1"), homeId=0x12345678, ), ), ) assert gateway._nodes == {1: {"ip": IPv6("2001:db8::1")}}
def test_onUnsolicitedMessage_unknownNode(gateway: ZIPGateway): ip = ipaddress.IPv6Address("::ffff:c0a8:ee") gateway._nodes = {7: {"ip": ipaddress.IPv6Address("::ffff:c0a8:ef")}} pkt = b"#\x02\x00\xc0\xf9\x00\x00\x05\x84\x02\x00\x00%\x03\x00" assert gateway.onUnsolicitedMessage(pkt, (ip, )) is False gateway.listener.messageReceived.assert_not_called()
async def test_getNodeList_cached(gateway: ZIPGateway): gateway._nodes = {1: {}, 2: {}} assert await gateway.getNodeList() == {1, 2}