Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bellows/ash.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ class DataFrame(AshFrame):

@staticmethod
def _randomize(data: bytes) -> bytes:
assert len(data) <= len(PSEUDO_RANDOM_DATA_SEQUENCE)
if len(data) > len(PSEUDO_RANDOM_DATA_SEQUENCE):
raise ParsingError(
f"Frame payload is too long: {len(data)} >"
f" {len(PSEUDO_RANDOM_DATA_SEQUENCE)} bytes"
)
return bytes([a ^ b for a, b in zip(data, PSEUDO_RANDOM_DATA_SEQUENCE)])

@classmethod
Expand Down
12 changes: 12 additions & 0 deletions tests/test_ash.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ def test_frame_parsing_errors():
assert ash.RstFrame.from_bytes(b"\xC0\xAB\xCD")


def test_data_frame_payload_too_long():
seq_len = len(ash.PSEUDO_RANDOM_DATA_SEQUENCE)

# A payload of exactly the sequence length still parses.
ash.DataFrame.from_bytes(ash.AshFrame.append_crc(bytes([0x00]) + bytes(seq_len)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: this line only checks that no exception is raised. Asserting on the result would also pin that the derandomized payload isn't truncated at the boundary, e.g. assert len(ash.DataFrame.from_bytes(...).ezsp_frame) == seq_len.


# One byte longer must raise ParsingError, not an uncaught AssertionError.
too_long = ash.AshFrame.append_crc(bytes([0x00]) + bytes(seq_len + 1))
with pytest.raises(ash.ParsingError, match=r"too long"):
ash.DataFrame.from_bytes(too_long)


def test_rst_frame():
assert ash.RstFrame() == ash.RstFrame()
assert ash.RstFrame().to_bytes() == b"\xC0\x38\xBC"
Expand Down
Loading