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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
"""Test management handling."""
import asyncio
from unittest.mock import AsyncMock, call, patch
import pytest
from xknx import XKNX
from xknx.exceptions import (
CommunicationError,
ConfirmationError,
ManagementConnectionError,
ManagementConnectionTimeout,
)
from xknx.management.management import MANAGAMENT_ACK_TIMEOUT
from xknx.telegram import (
GroupAddress,
IndividualAddress,
Telegram,
TelegramDirection,
apci,
tpci,
)
from ..conftest import EventLoopClockAdvancer
async def test_connect() -> None:
"""Test establishing connections."""
xknx = XKNX()
xknx.cemi_handler = AsyncMock()
ia_1 = IndividualAddress("4.0.1")
ia_2 = IndividualAddress("4.0.2")
def tg_connect(ia: IndividualAddress) -> Telegram:
return Telegram(
source_address=xknx.current_address,
destination_address=ia,
direction=TelegramDirection.OUTGOING,
tpci=tpci.TConnect(),
)
def tg_disconnect(ia: IndividualAddress) -> Telegram:
return Telegram(
source_address=xknx.current_address,
destination_address=ia,
direction=TelegramDirection.OUTGOING,
tpci=tpci.TDisconnect(),
)
await xknx.management.connect(ia_1)
conn_2 = await xknx.management.connect(ia_2)
with pytest.raises(ManagementConnectionError):
# no 2 connections to the same IA
await xknx.management.connect(ia_1)
assert xknx.cemi_handler.send_telegram.call_args_list == [
call(tg_connect(ia_1)),
call(tg_connect(ia_2)),
]
xknx.cemi_handler.send_telegram.reset_mock()
await xknx.management.disconnect(ia_1)
await conn_2.disconnect()
assert xknx.cemi_handler.send_telegram.call_args_list == [
call(tg_disconnect(ia_1)),
call(tg_disconnect(ia_2)),
]
# connect again doesn't raise
await xknx.management.connect(ia_1)
async def test_ack_timeout(time_travel: EventLoopClockAdvancer) -> None:
"""Test ACK timeout handling."""
xknx = XKNX()
xknx.cemi_handler = AsyncMock()
_ia = IndividualAddress("4.0.1")
conn = await xknx.management.connect(_ia)
xknx.cemi_handler.send_telegram.reset_mock()
device_desc_read = Telegram(
destination_address=_ia,
tpci=tpci.TDataConnected(0),
payload=apci.DeviceDescriptorRead(descriptor=0),
)
task = asyncio.create_task(
conn.request(
payload=apci.DeviceDescriptorRead(descriptor=0),
expected=apci.DeviceDescriptorResponse,
)
)
await asyncio.sleep(0)
assert xknx.cemi_handler.send_telegram.call_args_list == [
call(device_desc_read),
]
await time_travel(MANAGAMENT_ACK_TIMEOUT)
# telegram repeated
assert xknx.cemi_handler.send_telegram.call_args_list == [
call(device_desc_read),
call(device_desc_read),
]
await time_travel(MANAGAMENT_ACK_TIMEOUT)
with pytest.raises(ManagementConnectionTimeout):
# still no ACK -> timeout
await task
await conn.disconnect()
async def test_failed_connect_disconnect() -> None:
"""Test failing connections."""
xknx = XKNX()
xknx.cemi_handler = AsyncMock()
ia_1 = IndividualAddress("4.0.1")
xknx.cemi_handler.send_telegram.side_effect = ConfirmationError("")
with pytest.raises(ManagementConnectionError):
await xknx.management.connect(ia_1)
xknx.cemi_handler.send_telegram.side_effect = CommunicationError("")
with pytest.raises(ManagementConnectionError):
await xknx.management.connect(ia_1)
xknx.cemi_handler.send_telegram.side_effect = None
conn_1 = await xknx.management.connect(ia_1)
xknx.cemi_handler.send_telegram.side_effect = ConfirmationError("")
with pytest.raises(ManagementConnectionError):
await xknx.management.disconnect(ia_1)
xknx.cemi_handler.send_telegram.side_effect = None
conn_1 = await xknx.management.connect(ia_1)
xknx.cemi_handler.send_telegram.side_effect = CommunicationError("")
with pytest.raises(ManagementConnectionError):
await conn_1.disconnect()
async def test_reject_incoming_connection() -> None:
"""Test rejecting incoming transport connections."""
# Note: incoming L_DATA.ind indication connection requests are rejected
# L_DATA.req frames received from a tunnelling client are not yet supported
xknx = XKNX()
individual_address = IndividualAddress("4.0.10")
connect = Telegram(
source_address=individual_address,
destination_address=xknx.current_address,
direction=TelegramDirection.INCOMING,
tpci=tpci.TConnect(),
)
disconnect = Telegram(
source_address=xknx.current_address,
destination_address=individual_address,
tpci=tpci.TDisconnect(),
)
with patch("xknx.cemi.CEMIHandler.send_telegram") as send_telegram:
xknx.cemi_handler.telegram_received(connect)
await asyncio.sleep(0)
assert send_telegram.call_args_list == [call(disconnect)]
async def test_incoming_unexpected_numbered_telegram() -> None:
"""Test incoming unexpected numbered telegram is acked."""
xknx = XKNX()
individual_address = IndividualAddress("4.0.10")
device_desc_read = Telegram(
source_address=individual_address,
destination_address=xknx.current_address,
direction=TelegramDirection.INCOMING,
tpci=tpci.TDataConnected(0),
payload=apci.DeviceDescriptorRead(descriptor=0),
)
ack = Telegram(
source_address=xknx.current_address,
destination_address=individual_address,
direction=TelegramDirection.OUTGOING,
tpci=tpci.TAck(0),
)
with patch("xknx.cemi.CEMIHandler.send_telegram") as send_telegram:
xknx.cemi_handler.telegram_received(device_desc_read)
await asyncio.sleep(0)
assert send_telegram.call_args_list == [call(ack)]
async def test_incoming_wrong_address() -> None:
"""Test incoming telegrams addressed to different devices."""
xknx = XKNX()
individual_address = IndividualAddress("4.0.10")
other_address = IndividualAddress("4.0.11")
assert xknx.current_address != other_address
connect = Telegram(
source_address=individual_address,
destination_address=other_address,
direction=TelegramDirection.INCOMING,
tpci=tpci.TConnect(),
)
ack = Telegram(
source_address=individual_address,
destination_address=other_address,
direction=TelegramDirection.INCOMING,
tpci=tpci.TAck(0),
)
disconnect = Telegram(
source_address=individual_address,
destination_address=other_address,
direction=TelegramDirection.INCOMING,
tpci=tpci.TDisconnect(),
)
with patch("xknx.cemi.CEMIHandler.send_telegram") as send_telegram:
xknx.cemi_handler.telegram_received(connect)
xknx.cemi_handler.telegram_received(ack)
xknx.cemi_handler.telegram_received(disconnect)
await asyncio.sleep(0)
send_telegram.assert_not_called()
async def test_broadcast_message() -> None:
"""Test broadcast message sending."""
xknx = XKNX()
test_telegram = Telegram(
source_address=IndividualAddress("0.0.0"),
destination_address=GroupAddress("0/0/0"),
direction=TelegramDirection.OUTGOING,
tpci=tpci.TDataBroadcast(),
payload=apci.IndividualAddressRead(),
)
with patch("xknx.cemi.CEMIHandler.send_telegram") as send_telegram:
await xknx.management.send_broadcast(apci.IndividualAddressRead())
assert send_telegram.call_args_list == [call(test_telegram)]
@pytest.mark.parametrize("rate_limit", [0, 1])
async def test_p2p_rate_limit(
time_travel: EventLoopClockAdvancer, rate_limit: int
) -> None:
"""Test rate limit for P2P management connections."""
xknx = XKNX()
xknx.cemi_handler = AsyncMock()
ia = IndividualAddress("4.0.1")
def send_responses(index: int) -> None:
ack = Telegram(
source_address=ia,
destination_address=IndividualAddress(0),
direction=TelegramDirection.INCOMING,
tpci=tpci.TAck(index),
)
device_desc_resp = Telegram(
source_address=ia,
destination_address=IndividualAddress(0),
direction=TelegramDirection.INCOMING,
tpci=tpci.TDataConnected(index),
payload=apci.DeviceDescriptorResponse(),
)
xknx.management.process(ack)
xknx.management.process(device_desc_resp)
conn = await xknx.management.connect(ia, rate_limit)
# create task and request data
task = asyncio.create_task(
conn.request(
payload=apci.DeviceDescriptorRead(descriptor=0),
expected=apci.DeviceDescriptorResponse,
)
)
await asyncio.sleep(0)
send_responses(0)
await task
xknx.cemi_handler.reset_mock()
# create second task
task = asyncio.create_task(
conn.request(
payload=apci.DeviceDescriptorRead(descriptor=0),
expected=apci.DeviceDescriptorResponse,
)
)
await asyncio.sleep(0)
if rate_limit:
await time_travel(0.5 / rate_limit)
# the request is still queued
assert not xknx.cemi_handler.send_telegram.call_args_list
await time_travel(0.5 / rate_limit)
# the requests should be sent now, the behaviour should match no rate limit
assert xknx.cemi_handler.send_telegram.call_args_list == [
call(
Telegram(
destination_address=ia,
tpci=tpci.TDataConnected(1),
payload=apci.DeviceDescriptorRead(descriptor=0),
)
),
]
send_responses(1)
await task
|