Skip to content

Commit

Permalink
Account for getKeyTableEntry sometimes returning an invalid response (
Browse files Browse the repository at this point in the history
#625)

* Account for `getKeyTableEntry` sometimes returning an invalid response

* Remove `breakpoint()`
  • Loading branch information
puddly authored May 23, 2024
1 parent eae32ea commit 09cf7ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bellows/ezsp/v8/types/struct.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Protocol version 8 specific structs."""

from __future__ import annotations

import bellows.types.basic as basic
from bellows.types.struct import ( # noqa: F401
EmberAesMmoHashContext,
Expand Down Expand Up @@ -98,6 +100,15 @@ class EmberKeyStruct(EzspStruct):
# The IEEE address of the partner device also in possession of the key.
partnerEUI64: named.EUI64

@classmethod
def deserialize(cls, data: bytes) -> tuple[EmberKeyStruct, bytes]:
if len(data) == 24:
# XXX: `key` can seemingly be replaced with the uint32_t `psa_id` field in
# an invalid response. Pad it with zeroes so it deserializes.
data = data[:7] + b"\x00" * 12 + data[7:]

return super().deserialize(data)


class EmberGpSinkListEntry(EzspStruct):
# A sink list entry
Expand Down
15 changes: 15 additions & 0 deletions tests/test_ezsp_v8.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from bellows.ash import DataFrame
import bellows.ezsp.v8

from .async_mock import AsyncMock, MagicMock, patch
Expand Down Expand Up @@ -47,6 +48,20 @@ def test_command_frames(ezsp_f):
assert ezsp_f.COMMANDS_BY_ID[frame_id][0] == name


def test_get_key_table_entry_fallback_parsing(ezsp_f):
"""Test parsing of a getKeyTableEntry response with an invalid length."""
data_frame = DataFrame.from_bytes(
bytes.fromhex(
"039ba1a9252a1659c6974b25aa55d1209c6e76ddedce958bfdc6f29ffc5e0d2845"
)
)
ezsp_f(data_frame.ezsp_frame)

assert len(ezsp_f._handle_callback.mock_calls) == 1
mock_call = ezsp_f._handle_callback.mock_calls[0]
assert mock_call.args[0] == "getKeyTableEntry"


command_frames = {
"addEndpoint": 0x0002,
"addOrUpdateKeyTableEntry": 0x0066,
Expand Down

0 comments on commit 09cf7ce

Please sign in to comment.