Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_handle_control_stream_duplicate(self):
"""
We must only receive a single control stream.
"""
quic_server = FakeQuicConnection(
configuration=QuicConfiguration(is_client=False)
)
h3_server = H3Connection(quic_server)
# receive a first control stream
h3_server.handle_event(
StreamDataReceived(
stream_id=2, data=encode_uint_var(StreamType.CONTROL), end_stream=False
)
)
# receive a second control stream
h3_server.handle_event(
StreamDataReceived(
stream_id=6, data=encode_uint_var(StreamType.CONTROL), end_stream=False
)
)
self.assertEqual(
quic_server.closed,
(
ErrorCode.HTTP_STREAM_CREATION_ERROR,
"Only one control stream is allowed",
),
def test_handle_max_stream_data_frame(self):
with client_and_server() as (client, server):
# client creates bidirectional stream 0
stream = client._create_stream(stream_id=0)
self.assertEqual(stream.max_stream_data_remote, 1048576)
# client receives MAX_STREAM_DATA raising limit
client._handle_max_stream_data_frame(
client_receive_context(client),
QuicFrameType.MAX_STREAM_DATA,
Buffer(data=b"\x00" + encode_uint_var(1048577)),
)
self.assertEqual(stream.max_stream_data_remote, 1048577)
# client receives MAX_STREAM_DATA lowering limit
client._handle_max_stream_data_frame(
client_receive_context(client),
QuicFrameType.MAX_STREAM_DATA,
Buffer(data=b"\x00" + encode_uint_var(1048575)),
)
self.assertEqual(stream.max_stream_data_remote, 1048577)
def test_handle_stream_frame_over_largest_offset(self):
with client_and_server() as (client, server):
# client receives offset + length > 2^62 - 1
frame_type = QuicFrameType.STREAM_BASE | 6
stream_id = 1
with self.assertRaises(QuicConnectionError) as cm:
client._handle_stream_frame(
client_receive_context(client),
frame_type,
Buffer(
data=encode_uint_var(stream_id)
+ encode_uint_var(UINT_VAR_MAX)
+ encode_uint_var(1)
),
)
self.assertEqual(
cm.exception.error_code, QuicErrorCode.FRAME_ENCODING_ERROR
)
self.assertEqual(cm.exception.frame_type, frame_type)
self.assertEqual(
cm.exception.reason_phrase, "offset + length cannot exceed 2^62 - 1"
)
self.assertEqual(len(client._streams_blocked_uni), 0)
self.assertEqual(roundtrip(client, server), (0, 0))
# create one too many -> STREAMS_BLOCKED
stream_id = 128 * 4 + 2
client.send_stream_data(stream_id, b"")
self.assertTrue(client._streams[stream_id].is_blocked)
self.assertEqual(len(client._streams_blocked_bidi), 0)
self.assertEqual(len(client._streams_blocked_uni), 1)
self.assertEqual(roundtrip(client, server), (1, 1))
# peer raises max streams
client._handle_max_streams_uni_frame(
client_receive_context(client),
QuicFrameType.MAX_STREAMS_UNI,
Buffer(data=encode_uint_var(129)),
)
self.assertFalse(client._streams[stream_id].is_blocked)
def test_handle_qpack_encoder_stream_error(self):
"""
Receiving garbage on the QPACK encoder stream triggers an exception.
"""
quic_client = FakeQuicConnection(
configuration=QuicConfiguration(is_client=True)
)
h3_client = H3Connection(quic_client)
h3_client.handle_event(
StreamDataReceived(
stream_id=7,
data=encode_uint_var(StreamType.QPACK_ENCODER) + b"\x00",
end_stream=False,
)
)
self.assertEqual(
quic_client.closed, (ErrorCode.HTTP_QPACK_ENCODER_STREAM_ERROR, "")
)
def test_handle_stream_frame_over_largest_offset(self):
with client_and_server() as (client, server):
# client receives offset + length > 2^62 - 1
frame_type = QuicFrameType.STREAM_BASE | 6
stream_id = 1
with self.assertRaises(QuicConnectionError) as cm:
client._handle_stream_frame(
client_receive_context(client),
frame_type,
Buffer(
data=encode_uint_var(stream_id)
+ encode_uint_var(UINT_VAR_MAX)
+ encode_uint_var(1)
),
)
self.assertEqual(
cm.exception.error_code, QuicErrorCode.FRAME_ENCODING_ERROR
)
self.assertEqual(cm.exception.frame_type, frame_type)
self.assertEqual(
cm.exception.reason_phrase, "offset + length cannot exceed 2^62 - 1"
)
with client_and_server() as (client, server):
self.assertEqual(client._remote_max_streams_uni, 128)
# client receives MAX_STREAMS_UNI raising limit
client._handle_max_streams_uni_frame(
client_receive_context(client),
QuicFrameType.MAX_STREAMS_UNI,
Buffer(data=encode_uint_var(129)),
)
self.assertEqual(client._remote_max_streams_uni, 129)
# client receives MAX_STREAMS_UNI raising limit
client._handle_max_streams_uni_frame(
client_receive_context(client),
QuicFrameType.MAX_STREAMS_UNI,
Buffer(data=encode_uint_var(127)),
)
self.assertEqual(client._remote_max_streams_uni, 129)
def _create_uni_stream(self, stream_type: int) -> int:
"""
Create an unidirectional stream of the given type.
"""
stream_id = self._quic.get_next_available_stream_id(is_unidirectional=True)
self._quic.send_stream_data(stream_id, encode_uint_var(stream_type))
return stream_id