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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
"""Tests for the CEMIFrame object."""
import pytest
from xknx.cemi import (
CEMIFlags,
CEMIFrame,
CEMILData,
CEMIMessageCode,
CEMIMPropReadRequest,
CEMIMPropReadResponse,
CEMIMPropWriteRequest,
CEMIMPropWriteResponse,
)
from xknx.cemi.const import CEMIErrorCode
from xknx.exceptions import ConversionError, CouldNotParseCEMI, UnsupportedCEMIMessage
from xknx.profile.const import ResourceKNXNETIPPropertyId, ResourceObjectType
from xknx.telegram import GroupAddress, IndividualAddress, Telegram
from xknx.telegram.apci import GroupValueRead
from xknx.telegram.tpci import TConnect, TDataBroadcast, TDataGroup
def get_data(
code: int,
adil: int,
flags: int,
src: int,
dst: int,
npdu_len: int,
tpci_apci: int,
payload: list[int],
) -> bytes:
"""Encode to cemi data raw bytes."""
return bytes(
[
code,
adil, # adil
(flags >> 8) & 255, # flags
flags & 255, # flags
(src >> 8) & 255, # src
src & 255, # src
(dst >> 8) & 255, # dst
dst & 255, # dst
npdu_len, # npdu_len
(tpci_apci >> 8) & 255, # tpci_apci
tpci_apci & 255, # tpci_apci
*payload, # payload
]
)
def test_valid_command() -> None:
"""Test for valid frame parsing."""
raw = get_data(0x29, 0, 0x0080, 1, 1, 1, 0, [])
frame = CEMIFrame.from_knx(raw)
assert frame.code == CEMIMessageCode.L_DATA_IND
assert isinstance(frame.data, CEMILData)
assert frame.data.flags == 0x0080
assert frame.data.hops == 0
assert frame.data.src_addr == IndividualAddress(1)
assert frame.data.dst_addr == GroupAddress(1)
assert frame.data.payload == GroupValueRead()
assert frame.data.tpci == TDataGroup()
assert frame.calculated_length() == 11
assert frame.to_knx() == raw
def test_valid_tpci_control() -> None:
"""Test for valid tpci control."""
raw = bytes((0x29, 0, 0, 0, 0, 0, 0, 0, 0, 0x80))
frame = CEMIFrame.from_knx(raw)
assert frame.code == CEMIMessageCode.L_DATA_IND
assert isinstance(frame.data, CEMILData)
assert frame.data.flags == 0
assert frame.data.hops == 0
assert frame.data.payload is None
assert frame.data.src_addr == IndividualAddress(0)
assert frame.data.dst_addr == IndividualAddress(0)
assert frame.data.tpci == TConnect()
assert frame.calculated_length() == 10
assert frame.to_knx() == raw
@pytest.mark.parametrize(
"raw,err_msg",
[
(
get_data(0x29, 0, 0, 0, 0, 1, 0xFFC0, []),
r".*Invalid length for control TPDU.*",
),
],
)
def test_invalid_tpci_apci(raw: bytes, err_msg: str) -> None:
"""Test for invalid APCIService."""
with pytest.raises(CouldNotParseCEMI, match=err_msg):
CEMIFrame.from_knx(raw)
@pytest.mark.parametrize(
"raw,err_msg",
[
(
get_data(0x29, 0, 0, 0, 0, 1, 0x08C0, []),
r".*TPCI not supported.*",
),
(
get_data(0x29, 0, 0, 0, 0, 1, 0x03C0, []),
r".*APDU not supported*",
),
],
)
def test_unsupported_tpci_apci(raw: bytes, err_msg: str) -> None:
"""Test for invalid APCIService."""
with pytest.raises(UnsupportedCEMIMessage, match=err_msg):
CEMIFrame.from_knx(raw)
def test_invalid_apdu_len() -> None:
"""Test for invalid apdu len."""
with pytest.raises(CouldNotParseCEMI, match=r".*APDU LEN should be .*"):
CEMIFrame.from_knx(get_data(0x29, 0, 0, 0, 0, 2, 0, []))
def test_invalid_payload() -> None:
"""Test for having wrong payload set."""
frame = CEMIFrame(
code=CEMIMessageCode.L_DATA_IND,
data=CEMILData(
flags=0,
src_addr=IndividualAddress(0),
dst_addr=IndividualAddress(0),
tpci=TDataGroup(),
payload=None,
),
)
with pytest.raises(TypeError):
frame.calculated_length()
with pytest.raises(ConversionError):
frame.to_knx()
def test_missing_data() -> None:
"""Test for having no data set."""
frame = CEMIFrame(
code=CEMIMessageCode.L_DATA_IND,
data=None,
)
with pytest.raises(UnsupportedCEMIMessage):
frame.calculated_length()
with pytest.raises(UnsupportedCEMIMessage):
frame.to_knx()
def test_from_knx_with_not_handleable_cemi() -> None:
"""Test for having unhandlebale cemi set."""
with pytest.raises(
UnsupportedCEMIMessage, match=r".*CEMIMessageCode not implemented:.*"
):
CEMIFrame.from_knx(get_data(0x30, 0, 0, 0, 0, 2, 0, []))
def test_from_knx_with_not_implemented_cemi() -> None:
"""Test for having not implemented CEMI set."""
with pytest.raises(
UnsupportedCEMIMessage, match=r".*Could not handle CEMIMessageCode:.*"
):
CEMIFrame.from_knx(
get_data(CEMIMessageCode.L_BUSMON_IND.value, 0, 0, 0, 0, 2, 0, [])
)
def test_invalid_invalid_len() -> None:
"""Test for invalid cemi len."""
with pytest.raises(CouldNotParseCEMI, match=r".*CEMI too small.*"):
CEMIFrame.from_knx(get_data(0x29, 0, 0, 0, 0, 2, 0, [])[:5])
def test_from_knx_group_address() -> None:
"""Test conversion for a cemi with a group address as destination."""
frame = CEMIFrame.from_knx(get_data(0x29, 0, 0x80, 0, 0, 1, 0, []))
assert isinstance(frame.data, CEMILData)
assert frame.data.dst_addr == GroupAddress(0)
def test_from_knx_individual_address() -> None:
"""Test conversion for a cemi with a individual address as destination."""
frame = CEMIFrame.from_knx(get_data(0x29, 0, 0x00, 0, 0, 1, 0, []))
assert isinstance(frame.data, CEMILData)
assert frame.data.dst_addr == IndividualAddress(0)
def test_telegram_group_address() -> None:
"""Test telegram conversion flags with a group address."""
_telegram = Telegram(destination_address=GroupAddress(1))
frame = CEMIFrame(
code=CEMIMessageCode.L_DATA_IND,
data=CEMILData.init_from_telegram(_telegram),
)
assert isinstance(frame.data, CEMILData)
assert frame.data.flags & 0x0080 == CEMIFlags.DESTINATION_GROUP_ADDRESS
assert frame.data.flags & 0x0C00 == CEMIFlags.PRIORITY_LOW
# test CEMIFrame.telegram property
assert frame.data.telegram() == _telegram
def test_telegram_broadcast() -> None:
"""Test telegram conversion flags with a group address."""
_telegram = Telegram(destination_address=GroupAddress(0))
frame = CEMIFrame(
code=CEMIMessageCode.L_DATA_IND,
data=CEMILData.init_from_telegram(_telegram),
)
assert isinstance(frame.data, CEMILData)
assert frame.data.flags & 0x0080 == CEMIFlags.DESTINATION_GROUP_ADDRESS
assert frame.data.flags & 0x0C00 == CEMIFlags.PRIORITY_SYSTEM
assert frame.data.tpci == TDataBroadcast()
# test CEMIFrame.telegram property
assert frame.data.telegram() == _telegram
def test_telegram_individual_address() -> None:
"""Test telegram conversion flags with a individual address."""
_telegram = Telegram(destination_address=IndividualAddress(0), tpci=TConnect())
frame = CEMIFrame(
code=CEMIMessageCode.L_DATA_IND,
data=CEMILData.init_from_telegram(_telegram),
)
assert isinstance(frame.data, CEMILData)
assert frame.data.flags & 0x0080 == CEMIFlags.DESTINATION_INDIVIDUAL_ADDRESS
assert frame.data.flags & 0x0C00 == CEMIFlags.PRIORITY_SYSTEM
assert frame.data.flags & 0x0200 == CEMIFlags.NO_ACK_REQUESTED
# test CEMIFrame.telegram property
assert frame.data.telegram() == _telegram
def test_telegram_unsupported_address() -> None:
"""Test telegram conversion flags with an unsupported address."""
with pytest.raises(TypeError):
CEMIFrame(
code=CEMIMessageCode.L_DATA_IND,
data=CEMILData.init_from_telegram(Telegram(destination_address=object())),
)
def get_prop(
code: int,
obj_id: int,
obj_inst: int,
prop_id: int,
num: int,
six: int,
payload: list[int],
) -> bytes:
"""Encode to cemi prop raw bytes."""
return bytes(
[
code,
(obj_id >> 8) & 255, # Interface Object Type
obj_id & 255, # Interface Object Type
obj_inst & 255, # Object instance
prop_id & 255, # Property ID
(num << 4) | (six >> 8), # Number of Elements (4bit) Start index (hsb 4bit)
six & 255, # Start index (lsb 8bit)
*payload, # payload
]
)
def test_valid_read_req() -> None:
"""Test for valid frame parsing."""
raw = get_prop(0xFC, 0x000B, 1, 52, 1, 1, [])
frame = CEMIFrame.from_knx(raw)
assert frame.code == CEMIMessageCode.M_PROP_READ_REQ
assert isinstance(frame.data, CEMIMPropReadRequest)
assert (
frame.data.property_info.object_type
== ResourceObjectType.OBJECT_KNXNETIP_PARAMETER
)
assert frame.data.property_info.object_instance == 1
assert (
frame.data.property_info.property_id
== ResourceKNXNETIPPropertyId.PID_KNX_INDIVIDUAL_ADDRESS
)
assert frame.data.property_info.number_of_elements == 1
assert frame.data.property_info.start_index == 1
assert frame.calculated_length() == 7
assert frame.to_knx() == raw
with pytest.raises(AttributeError):
frame.data.telegram()
def test_valid_read_con() -> None:
"""Test for valid frame parsing."""
raw = get_prop(0xFB, 0x000B, 1, 52, 1, 1, [0x12, 0x03])
frame = CEMIFrame.from_knx(raw)
assert frame.code == CEMIMessageCode.M_PROP_READ_CON
assert isinstance(frame.data, CEMIMPropReadResponse)
assert (
frame.data.property_info.object_type
== ResourceObjectType.OBJECT_KNXNETIP_PARAMETER
)
assert frame.data.property_info.object_instance == 1
assert (
frame.data.property_info.property_id
== ResourceKNXNETIPPropertyId.PID_KNX_INDIVIDUAL_ADDRESS
)
assert frame.data.property_info.number_of_elements == 1
assert frame.data.property_info.start_index == 1
assert frame.data.error_code is None
assert IndividualAddress.from_knx(frame.data.data) == IndividualAddress("1.2.3")
assert frame.calculated_length() == 9
assert frame.to_knx() == raw
def test_valid_error_read_con() -> None:
"""Test for valid frame parsing."""
raw = get_prop(0xFB, 0x000B, 1, 52, 0, 1, [0x07])
frame = CEMIFrame.from_knx(raw)
assert frame.code == CEMIMessageCode.M_PROP_READ_CON
assert isinstance(frame.data, CEMIMPropReadResponse)
assert (
frame.data.property_info.object_type
== ResourceObjectType.OBJECT_KNXNETIP_PARAMETER
)
assert frame.data.property_info.object_instance == 1
assert (
frame.data.property_info.property_id
== ResourceKNXNETIPPropertyId.PID_KNX_INDIVIDUAL_ADDRESS
)
assert frame.data.property_info.number_of_elements == 0
assert frame.data.property_info.start_index == 1
assert frame.data.error_code == CEMIErrorCode.CEMI_ERROR_VOID_DP
assert frame.calculated_length() == 8
assert frame.to_knx() == raw
def test_valid_write_req() -> None:
"""Test for valid frame parsing."""
raw = get_prop(0xF6, 0x000B, 1, 52, 1, 1, [0x12, 0x03])
frame = CEMIFrame.from_knx(raw)
assert frame.code == CEMIMessageCode.M_PROP_WRITE_REQ
assert isinstance(frame.data, CEMIMPropWriteRequest)
assert (
frame.data.property_info.object_type
== ResourceObjectType.OBJECT_KNXNETIP_PARAMETER
)
assert frame.data.property_info.object_instance == 1
assert (
frame.data.property_info.property_id
== ResourceKNXNETIPPropertyId.PID_KNX_INDIVIDUAL_ADDRESS
)
assert frame.data.property_info.number_of_elements == 1
assert frame.data.property_info.start_index == 1
assert IndividualAddress.from_knx(frame.data.data) == IndividualAddress("1.2.3")
assert frame.calculated_length() == 9
assert frame.to_knx() == raw
def test_valid_empty_write_con() -> None:
"""Test for valid frame parsing."""
raw = get_prop(0xF5, 0x000B, 1, 52, 1, 1, [])
frame = CEMIFrame.from_knx(raw)
assert frame.code == CEMIMessageCode.M_PROP_WRITE_CON
assert isinstance(frame.data, CEMIMPropWriteResponse)
assert (
frame.data.property_info.object_type
== ResourceObjectType.OBJECT_KNXNETIP_PARAMETER
)
assert frame.data.property_info.object_instance == 1
assert (
frame.data.property_info.property_id
== ResourceKNXNETIPPropertyId.PID_KNX_INDIVIDUAL_ADDRESS
)
assert frame.data.property_info.number_of_elements == 1
assert frame.data.property_info.start_index == 1
assert frame.data.error_code is None
assert frame.calculated_length() == 7
assert frame.to_knx() == raw
def test_valid_error_write_con() -> None:
"""Test for valid frame parsing."""
raw = get_prop(0xF5, 0x000B, 1, 52, 0, 1, [0x07])
frame = CEMIFrame.from_knx(raw)
assert frame.code == CEMIMessageCode.M_PROP_WRITE_CON
assert isinstance(frame.data, CEMIMPropWriteResponse)
assert (
frame.data.property_info.object_type
== ResourceObjectType.OBJECT_KNXNETIP_PARAMETER
)
assert frame.data.property_info.object_instance == 1
assert (
frame.data.property_info.property_id
== ResourceKNXNETIPPropertyId.PID_KNX_INDIVIDUAL_ADDRESS
)
assert frame.data.property_info.number_of_elements == 0
assert frame.data.property_info.start_index == 1
assert frame.data.error_code == CEMIErrorCode.CEMI_ERROR_VOID_DP
assert frame.calculated_length() == 8
assert frame.to_knx() == raw
@pytest.mark.parametrize(
"raw,err_msg",
[
(
get_prop(0xFC, 0x000B, 1, 52, 1, 1, [])[:5],
r".*Invalid CEMI length:*",
),
(
get_prop(0xFB, 0x000B, 1, 52, 1, 1, [])[:5],
r".*CEMI Property Read Response too small.*",
),
(
get_prop(0xFB, 0x000B, 1, 52, 0, 1, [0x07, 0x00]),
r".*Invalid CEMI error response length:.*",
),
(
get_prop(0xF6, 0x000B, 1, 52, 1, 1, [])[:5],
r".*CEMI Property Write Request too small.*",
),
(
get_prop(0xF5, 0x000B, 1, 52, 1, 1, [])[:5],
r".*CEMI Property Write Response too small.*",
),
(
get_prop(0xF5, 0x000B, 1, 52, 0, 1, [0x07, 0x00]),
r".*Invalid CEMI error response length:.*",
),
(
get_prop(0xF5, 0x000B, 1, 52, 1, 1, [0x07]),
r".*Invalid CEMI response length:.*",
),
],
)
def test_invalid_length(raw: bytes, err_msg: str) -> None:
"""Test for invalid frame parsing."""
with pytest.raises(CouldNotParseCEMI, match=err_msg):
CEMIFrame.from_knx(raw)
def test_invalid_resource_object() -> None:
"""Test for invalid frame parsing."""
with pytest.raises(
UnsupportedCEMIMessage, match=r".*CEMIMProp Object Type not supported:.*"
):
CEMIFrame.from_knx(get_prop(0xFC, 0x1234, 1, 52, 1, 1, []))
|