Skip to content

Commit

Permalink
Update event stream tests (#3254)
Browse files Browse the repository at this point in the history
This fixes a pair of event stream tests. Implementations of
`application/vnd.amazon.eventstream` are expected to check the CRC
before attempting to semantically parse the prelude. The tests meant
to enforce that were present, but the wrong error was being asserted.

This change fixes the ordering of checks, fixes the tests, and adds
two new test cases to make assertions about the error types that were
previously being applied to the old tests.
  • Loading branch information
JordonPhillips committed Sep 12, 2024
1 parent 2707445 commit db41663
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion botocore/eventstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ def _parse_prelude(self):
prelude_bytes = self._data[:_PRELUDE_LENGTH]
raw_prelude, _ = DecodeUtils.unpack_prelude(prelude_bytes)
prelude = MessagePrelude(*raw_prelude)
self._validate_prelude(prelude)
# The minus 4 removes the prelude crc from the bytes to be checked
_validate_checksum(prelude_bytes[: _PRELUDE_LENGTH - 4], prelude.crc)
self._validate_prelude(prelude)
return prelude

def _parse_headers(self):
Expand Down
45 changes: 42 additions & 3 deletions tests/unit/test_eventstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
b"\x00\x00\x00=\xff\x00\x01\x02\x07\xfd\x83\x96\x0ccontent-type\x07\x00"
b"\x10application/json{'foo':'bar'}\x8d\x9c\x08\xb1"
),
InvalidHeadersLength,
ChecksumMismatch,
)

CORRUPTED_HEADERS = (
Expand All @@ -231,7 +231,7 @@

CORRUPTED_LENGTH = (
b"\x01\x00\x00\x1d\x00\x00\x00\x00\xfdR\x8cZ{'foo':'bar'}\xc3e96",
InvalidPayloadLength,
ChecksumMismatch,
)

CORRUPTED_PAYLOAD = (
Expand All @@ -247,13 +247,40 @@
DuplicateHeader,
)

# In contrast to the CORRUPTED_HEADERS case, this message is otherwise
# well-formed - the checksums match.
INVALID_HEADERS_LENGTH = (
(
b"\x00\x00\x00\x3d" # total length
b"\xff\x00\x01\x02" # headers length
b"\x15\x83\xf5\xc2" # prelude crc
b"\x0ccontent-type\x07\x00\x10application/json" # headers
b"{'foo':'bar'}" # payload
b"\x2f\x37\x7f\x5d" # message crc
),
InvalidHeadersLength,
)

# In contrast to the CORRUPTED_PAYLOAD case, this message is otherwise
# well-formed - the checksums match.
INVALID_PAYLOAD_LENGTH = (
b"\x01\x00\x00\x11" # total length
+ b"\x00\x00\x00\x00" # headers length
+ b"\xf4\x08\x61\xc5" # prelude crc
+ b"0" * (16 * 1024**2 + 1) # payload
+ b"\x2a\xb4\xc5\xa5", # message crc
InvalidPayloadLength,
)

# Tuples of encoded messages and their expected exception
NEGATIVE_CASES = [
CORRUPTED_LENGTH,
CORRUPTED_PAYLOAD,
CORRUPTED_HEADERS,
CORRUPTED_HEADER_LENGTH,
DUPLICATE_HEADER,
INVALID_HEADERS_LENGTH,
INVALID_PAYLOAD_LENGTH,
]


Expand Down Expand Up @@ -311,7 +338,19 @@ def test_all_positive_cases():
assert_message_equal(expected, decoded)


@pytest.mark.parametrize("encoded, exception", NEGATIVE_CASES)
@pytest.mark.parametrize(
"encoded, exception",
NEGATIVE_CASES,
ids=[
"corrupted-length",
"corrupted-payload",
"corrupted-headers",
"corrupted-headers-length",
"duplicate-headers",
"invalid-headers-length",
"invalid-payload-length",
],
)
def test_negative_cases(encoded, exception):
"""Test that all negative cases raise the expected exception."""
with pytest.raises(exception):
Expand Down

0 comments on commit db41663

Please sign in to comment.