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_padding_frame(self):
client = create_standalone_client(self)
# no more padding
buf = Buffer(data=b"")
client._handle_padding_frame(
client_receive_context(client), QuicFrameType.PADDING, buf
)
self.assertEqual(buf.tell(), 0)
# padding until end
buf = Buffer(data=bytes(10))
client._handle_padding_frame(
client_receive_context(client), QuicFrameType.PADDING, buf
)
self.assertEqual(buf.tell(), 10)
# padding then something else
buf = Buffer(data=bytes(10) + b"\x01")
client._handle_padding_frame(
client_receive_context(client), QuicFrameType.PADDING, buf
def test_data_slice(self):
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01")
self.assertEqual(buf.data_slice(0, 8), b"\x08\x07\x06\x05\x04\x03\x02\x01")
self.assertEqual(buf.data_slice(1, 3), b"\x07\x06")
with self.assertRaises(BufferReadError):
buf.data_slice(-1, 3)
with self.assertRaises(BufferReadError):
buf.data_slice(0, 9)
with self.assertRaises(BufferReadError):
buf.data_slice(1, 0)
def test_pull_retry(self):
buf = Buffer(data=load("retry.bin"))
header = pull_quic_header(buf, host_cid_length=8)
self.assertTrue(header.is_long_header)
self.assertEqual(header.version, QuicProtocolVersion.DRAFT_24)
self.assertEqual(header.packet_type, PACKET_TYPE_RETRY)
self.assertEqual(header.destination_cid, binascii.unhexlify("fee746dfde699d61"))
self.assertEqual(header.source_cid, binascii.unhexlify("59aa0942fd2f11e9"))
self.assertEqual(
header.original_destination_cid, binascii.unhexlify("d61e7448e0d63dff")
)
self.assertEqual(
header.token,
binascii.unhexlify(
"5282f57f85a1a5c50de5aac2ff7dba43ff34524737099ec41c4b8e8c76734f935e8efd51177dbbe764"
),
)
self.assertEqual(header.rest_length, 0)
def test_handle_datagram_frame_not_allowed(self):
client = create_standalone_client(self, max_datagram_frame_size=None)
with self.assertRaises(QuicConnectionError) as cm:
client._handle_datagram_frame(
client_receive_context(client),
QuicFrameType.DATAGRAM,
Buffer(data=b"hello"),
)
self.assertEqual(cm.exception.error_code, QuicErrorCode.PROTOCOL_VIOLATION)
self.assertEqual(cm.exception.frame_type, QuicFrameType.DATAGRAM)
self.assertEqual(cm.exception.reason_phrase, "Unexpected DATAGRAM frame")
def test_pull_client_hello_with_alpn(self):
buf = Buffer(data=load("tls_client_hello_with_alpn.bin"))
hello = pull_client_hello(buf)
self.assertTrue(buf.eof())
self.assertEqual(
hello.random,
binascii.unhexlify(
"ed575c6fbd599c4dfaabd003dca6e860ccdb0e1782c1af02e57bf27cb6479b76"
),
)
self.assertEqual(hello.session_id, b"")
self.assertEqual(
hello.cipher_suites,
[
tls.CipherSuite.AES_128_GCM_SHA256,
tls.CipherSuite.AES_256_GCM_SHA384,
tls.CipherSuite.CHACHA20_POLY1305_SHA256,
def test_pull_initial_server(self):
buf = Buffer(data=load("initial_server.bin"))
header = pull_quic_header(buf, host_cid_length=8)
self.assertTrue(header.is_long_header)
self.assertEqual(header.version, QuicProtocolVersion.DRAFT_24)
self.assertEqual(header.packet_type, PACKET_TYPE_INITIAL)
self.assertEqual(header.destination_cid, b"")
self.assertEqual(header.source_cid, binascii.unhexlify("195c68344e28d479"))
self.assertEqual(header.original_destination_cid, b"")
self.assertEqual(header.token, b"")
self.assertEqual(header.rest_length, 184)
self.assertEqual(buf.tell(), 18)
def test_handle_datagram_frame(self):
client = create_standalone_client(self, max_datagram_frame_size=6)
client._handle_datagram_frame(
client_receive_context(client),
QuicFrameType.DATAGRAM,
Buffer(data=b"hello"),
)
self.assertEqual(
client.next_event(), events.DatagramFrameReceived(data=b"hello")
)
def test_pull_long_header_too_short(self):
buf = Buffer(data=b"\xc0\x00")
with self.assertRaises(BufferReadError):
pull_quic_header(buf, host_cid_length=8)
def parse_settings(data: bytes) -> Dict[int, int]:
buf = Buffer(data=data)
settings = []
while not buf.eof():
setting = buf.pull_uint_var()
value = buf.pull_uint_var()
settings.append((setting, value))
return dict(settings)
def parse_max_push_id(data: bytes) -> int:
buf = Buffer(data=data)
max_push_id = buf.pull_uint_var()
assert buf.eof()
return max_push_id