| 1 |
import asyncio |
| 2 |
import binascii |
| 3 |
import contextlib |
| 4 |
import random |
| 5 |
import socket |
| 6 |
from typing import AsyncGenerator, Optional |
| 7 |
from unittest import TestCase, skipIf |
| 8 |
from unittest.mock import patch |
| 9 |
|
| 10 |
from aioquic.asyncio.client import connect |
| 11 |
from aioquic.asyncio.protocol import QuicConnectionProtocol |
| 12 |
from aioquic.asyncio.server import serve |
| 13 |
from aioquic.quic.configuration import QuicConfiguration |
| 14 |
from aioquic.quic.logger import QuicLogger |
| 15 |
from cryptography.hazmat.primitives import serialization |
| 16 |
|
| 17 |
from .utils import ( |
| 18 |
SERVER_CACERTFILE, |
| 19 |
SERVER_CERTFILE, |
| 20 |
SERVER_COMBINEDFILE, |
| 21 |
SERVER_KEYFILE, |
| 22 |
SKIP_TESTS, |
| 23 |
asynctest, |
| 24 |
generate_ec_certificate, |
| 25 |
generate_ed448_certificate, |
| 26 |
generate_ed25519_certificate, |
| 27 |
generate_rsa_certificate, |
| 28 |
) |
| 29 |
|
| 30 |
real_sendto = socket.socket.sendto |
| 31 |
|
| 32 |
|
| 33 |
def sendto_with_loss(self, data, addr=None): |
| 34 |
""" |
| 35 |
Simulate 25% packet loss. |
| 36 |
""" |
| 37 |
if random.random() > 0.25: |
| 38 |
real_sendto(self, data, addr) |
| 39 |
|
| 40 |
|
| 41 |
class SessionTicketStore: |
| 42 |
def __init__(self): |
| 43 |
self.tickets = {} |
| 44 |
|
| 45 |
def add(self, ticket): |
| 46 |
self.tickets[ticket.ticket] = ticket |
| 47 |
|
| 48 |
def pop(self, label): |
| 49 |
return self.tickets.pop(label, None) |
| 50 |
|
| 51 |
|
| 52 |
def handle_stream(reader, writer): |
| 53 |
async def serve(): |
| 54 |
data = await reader.read() |
| 55 |
writer.write(bytes(reversed(data))) |
| 56 |
writer.write_eof() |
| 57 |
|
| 58 |
asyncio.ensure_future(serve()) |
| 59 |
|
| 60 |
|
| 61 |
class HighLevelTest(TestCase): |
| 62 |
def setUp(self): |
| 63 |
self.bogus_port = 1024 |
| 64 |
self.server_host = "localhost" |
| 65 |
|
| 66 |
async def run_client( |
| 67 |
self, |
| 68 |
*, |
| 69 |
port: int, |
| 70 |
host=None, |
| 71 |
cadata=None, |
| 72 |
cafile=SERVER_CACERTFILE, |
| 73 |
configuration=None, |
| 74 |
request=b"ping", |
| 75 |
**kwargs, |
| 76 |
) -> bytes: |
| 77 |
if host is None: |
| 78 |
host = self.server_host |
| 79 |
if configuration is None: |
| 80 |
configuration = QuicConfiguration(is_client=True) |
| 81 |
configuration.load_verify_locations(cadata=cadata, cafile=cafile) |
| 82 |
async with connect(host, port, configuration=configuration, **kwargs) as client: |
| 83 |
|
| 84 |
await client.wait_connected() |
| 85 |
|
| 86 |
reader, writer = await client.create_stream() |
| 87 |
self.assertEqual(writer.can_write_eof(), True) |
| 88 |
self.assertEqual(writer.get_extra_info("stream_id"), 0) |
| 89 |
|
| 90 |
writer.write(request) |
| 91 |
writer.write_eof() |
| 92 |
|
| 93 |
response = await reader.read() |
| 94 |
|
| 95 |
|
| 96 |
writer.close() |
| 97 |
|
| 98 |
|
| 99 |
await client.wait_closed() |
| 100 |
|
| 101 |
return response |
| 102 |
|
| 103 |
@contextlib.asynccontextmanager |
| 104 |
async def run_server( |
| 105 |
self, configuration: Optional[QuicConfiguration] = None, host="::", **kwargs |
| 106 |
) -> AsyncGenerator[int, None]: |
| 107 |
if configuration is None: |
| 108 |
configuration = QuicConfiguration(is_client=False) |
| 109 |
configuration.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE) |
| 110 |
server = await serve( |
| 111 |
host=host, |
| 112 |
port=0, |
| 113 |
configuration=configuration, |
| 114 |
stream_handler=handle_stream, |
| 115 |
**kwargs, |
| 116 |
) |
| 117 |
try: |
| 118 |
yield server._transport.get_extra_info("sockname")[1] |
| 119 |
finally: |
| 120 |
server.close() |
| 121 |
|
| 122 |
@asynctest |
| 123 |
async def test_connect_and_serve(self) -> None: |
| 124 |
async with self.run_server() as server_port: |
| 125 |
response = await self.run_client(port=server_port) |
| 126 |
self.assertEqual(response, b"gnip") |
| 127 |
|
| 128 |
@asynctest |
| 129 |
async def test_connect_and_serve_ipv4(self) -> None: |
| 130 |
certificate, private_key = generate_rsa_certificate( |
| 131 |
alternative_names=["localhost", "127.0.0.1"], common_name="localhost" |
| 132 |
) |
| 133 |
async with self.run_server( |
| 134 |
configuration=QuicConfiguration( |
| 135 |
certificate=certificate, |
| 136 |
private_key=private_key, |
| 137 |
is_client=False, |
| 138 |
), |
| 139 |
host="0.0.0.0", |
| 140 |
) as server_port: |
| 141 |
response = await self.run_client( |
| 142 |
cadata=certificate.public_bytes(serialization.Encoding.PEM), |
| 143 |
cafile=None, |
| 144 |
host="127.0.0.1", |
| 145 |
port=server_port, |
| 146 |
) |
| 147 |
self.assertEqual(response, b"gnip") |
| 148 |
|
| 149 |
@skipIf("ipv6" in SKIP_TESTS, "Skipping IPv6 tests") |
| 150 |
@asynctest |
| 151 |
async def test_connect_and_serve_ipv6(self) -> None: |
| 152 |
certificate, private_key = generate_rsa_certificate( |
| 153 |
alternative_names=["localhost", "::1"], common_name="localhost" |
| 154 |
) |
| 155 |
async with self.run_server( |
| 156 |
configuration=QuicConfiguration( |
| 157 |
certificate=certificate, |
| 158 |
private_key=private_key, |
| 159 |
is_client=False, |
| 160 |
), |
| 161 |
host="::", |
| 162 |
) as server_port: |
| 163 |
response = await self.run_client( |
| 164 |
cadata=certificate.public_bytes(serialization.Encoding.PEM), |
| 165 |
cafile=None, |
| 166 |
host="::1", |
| 167 |
port=server_port, |
| 168 |
) |
| 169 |
self.assertEqual(response, b"gnip") |
| 170 |
|
| 171 |
async def _test_connect_and_serve_with_certificate( |
| 172 |
self, certificate, private_key |
| 173 |
) -> None: |
| 174 |
async with self.run_server( |
| 175 |
configuration=QuicConfiguration( |
| 176 |
certificate=certificate, |
| 177 |
private_key=private_key, |
| 178 |
is_client=False, |
| 179 |
) |
| 180 |
) as server_port: |
| 181 |
response = await self.run_client( |
| 182 |
cadata=certificate.public_bytes(serialization.Encoding.PEM), |
| 183 |
cafile=None, |
| 184 |
port=server_port, |
| 185 |
) |
| 186 |
self.assertEqual(response, b"gnip") |
| 187 |
|
| 188 |
@asynctest |
| 189 |
async def test_connect_and_serve_with_ec_certificate(self): |
| 190 |
await self._test_connect_and_serve_with_certificate( |
| 191 |
*generate_ec_certificate( |
| 192 |
alternative_names=["localhost"], common_name="localhost" |
| 193 |
) |
| 194 |
) |
| 195 |
|
| 196 |
@asynctest |
| 197 |
async def test_connect_and_serve_with_ed25519_certificate(self): |
| 198 |
await self._test_connect_and_serve_with_certificate( |
| 199 |
*generate_ed25519_certificate( |
| 200 |
alternative_names=["localhost"], common_name="localhost" |
| 201 |
) |
| 202 |
) |
| 203 |
|
| 204 |
@skipIf("ed448" in SKIP_TESTS, "Skipping ed448 tests") |
| 205 |
@asynctest |
| 206 |
async def test_connect_and_serve_with_ed448_certificate(self): |
| 207 |
await self._test_connect_and_serve_with_certificate( |
| 208 |
*generate_ed448_certificate( |
| 209 |
alternative_names=["localhost"], common_name="localhost" |
| 210 |
) |
| 211 |
) |
| 212 |
|
| 213 |
@asynctest |
| 214 |
async def test_connect_and_serve_with_rsa_certificate(self): |
| 215 |
await self._test_connect_and_serve_with_certificate( |
| 216 |
*generate_rsa_certificate( |
| 217 |
alternative_names=["localhost"], common_name="localhost" |
| 218 |
) |
| 219 |
) |
| 220 |
|
| 221 |
@asynctest |
| 222 |
async def test_connect_and_serve_large(self): |
| 223 |
""" |
| 224 |
Transfer enough data to require raising MAX_DATA and MAX_STREAM_DATA. |
| 225 |
""" |
| 226 |
data = b"Z" * 2097152 |
| 227 |
async with self.run_server() as server_port: |
| 228 |
response = await self.run_client(port=server_port, request=data) |
| 229 |
self.assertEqual(response, data) |
| 230 |
|
| 231 |
@asynctest |
| 232 |
async def test_connect_and_serve_without_client_configuration(self): |
| 233 |
async with self.run_server() as server_port: |
| 234 |
with self.assertRaises(ConnectionError): |
| 235 |
async with connect(self.server_host, server_port) as client: |
| 236 |
await client.ping() |
| 237 |
|
| 238 |
@asynctest |
| 239 |
async def test_connect_and_serve_writelines(self): |
| 240 |
async with self.run_server() as server_port: |
| 241 |
configuration = QuicConfiguration(is_client=True) |
| 242 |
configuration.load_verify_locations(cafile=SERVER_CACERTFILE) |
| 243 |
async with connect( |
| 244 |
self.server_host, server_port, configuration=configuration |
| 245 |
) as client: |
| 246 |
reader, writer = await client.create_stream() |
| 247 |
assert writer.can_write_eof() is True |
| 248 |
|
| 249 |
writer.writelines([b"01234567", b"89012345"]) |
| 250 |
writer.write_eof() |
| 251 |
|
| 252 |
response = await reader.read() |
| 253 |
self.assertEqual(response, b"5432109876543210") |
| 254 |
|
| 255 |
@skipIf("loss" in SKIP_TESTS, "Skipping loss tests") |
| 256 |
@patch("socket.socket.sendto", new_callable=lambda: sendto_with_loss) |
| 257 |
@asynctest |
| 258 |
async def test_connect_and_serve_with_packet_loss(self, mock_sendto) -> None: |
| 259 |
""" |
| 260 |
This test ensures handshake success and stream data is successfully sent |
| 261 |
and received in the presence of packet loss (randomized 25% in each direction). |
| 262 |
""" |
| 263 |
data = b"Z" * 65536 |
| 264 |
|
| 265 |
server_configuration = QuicConfiguration( |
| 266 |
is_client=False, quic_logger=QuicLogger() |
| 267 |
) |
| 268 |
server_configuration.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE) |
| 269 |
async with self.run_server(configuration=server_configuration) as server_port: |
| 270 |
response = await self.run_client( |
| 271 |
configuration=QuicConfiguration( |
| 272 |
is_client=True, quic_logger=QuicLogger() |
| 273 |
), |
| 274 |
port=server_port, |
| 275 |
request=data, |
| 276 |
) |
| 277 |
self.assertEqual(response, data) |
| 278 |
|
| 279 |
@asynctest |
| 280 |
async def test_connect_and_serve_with_session_ticket(self): |
| 281 |
client_ticket = None |
| 282 |
store = SessionTicketStore() |
| 283 |
|
| 284 |
def save_ticket(t): |
| 285 |
nonlocal client_ticket |
| 286 |
client_ticket = t |
| 287 |
|
| 288 |
async with self.run_server( |
| 289 |
session_ticket_fetcher=store.pop, session_ticket_handler=store.add |
| 290 |
) as server_port: |
| 291 |
|
| 292 |
response = await self.run_client( |
| 293 |
port=server_port, session_ticket_handler=save_ticket |
| 294 |
) |
| 295 |
self.assertEqual(response, b"gnip") |
| 296 |
|
| 297 |
self.assertIsNotNone(client_ticket) |
| 298 |
|
| 299 |
|
| 300 |
response = await self.run_client( |
| 301 |
configuration=QuicConfiguration( |
| 302 |
is_client=True, session_ticket=client_ticket |
| 303 |
), |
| 304 |
port=server_port, |
| 305 |
) |
| 306 |
self.assertEqual(response, b"gnip") |
| 307 |
|
| 308 |
@asynctest |
| 309 |
async def test_connect_and_serve_with_retry(self): |
| 310 |
async with self.run_server(retry=True) as server_port: |
| 311 |
response = await self.run_client(port=server_port) |
| 312 |
self.assertEqual(response, b"gnip") |
| 313 |
|
| 314 |
@asynctest |
| 315 |
async def test_connect_and_serve_with_retry_bad_original_destination_connection_id( |
| 316 |
self, |
| 317 |
): |
| 318 |
""" |
| 319 |
If the server's transport parameters do not have the correct |
| 320 |
original_destination_connection_id the connection must fail. |
| 321 |
""" |
| 322 |
|
| 323 |
def create_protocol(*args, **kwargs): |
| 324 |
protocol = QuicConnectionProtocol(*args, **kwargs) |
| 325 |
protocol._quic._original_destination_connection_id = None |
| 326 |
return protocol |
| 327 |
|
| 328 |
async with self.run_server( |
| 329 |
create_protocol=create_protocol, retry=True |
| 330 |
) as server_port: |
| 331 |
with self.assertRaises(ConnectionError): |
| 332 |
await self.run_client(port=server_port) |
| 333 |
|
| 334 |
@asynctest |
| 335 |
async def test_connect_and_serve_with_retry_bad_retry_source_connection_id(self): |
| 336 |
""" |
| 337 |
If the server's transport parameters do not have the correct |
| 338 |
retry_source_connection_id the connection must fail. |
| 339 |
""" |
| 340 |
|
| 341 |
def create_protocol(*args, **kwargs): |
| 342 |
protocol = QuicConnectionProtocol(*args, **kwargs) |
| 343 |
protocol._quic._retry_source_connection_id = None |
| 344 |
return protocol |
| 345 |
|
| 346 |
async with self.run_server( |
| 347 |
create_protocol=create_protocol, retry=True |
| 348 |
) as server_port: |
| 349 |
with self.assertRaises(ConnectionError): |
| 350 |
await self.run_client(port=server_port) |
| 351 |
|
| 352 |
@patch("aioquic.quic.retry.QuicRetryTokenHandler.validate_token") |
| 353 |
@asynctest |
| 354 |
async def test_connect_and_serve_with_retry_bad_token(self, mock_validate) -> None: |
| 355 |
mock_validate.side_effect = ValueError("Decryption failed.") |
| 356 |
|
| 357 |
async with self.run_server(retry=True) as server_port: |
| 358 |
with self.assertRaises(ConnectionError): |
| 359 |
await self.run_client( |
| 360 |
configuration=QuicConfiguration(is_client=True, idle_timeout=4.0), |
| 361 |
port=server_port, |
| 362 |
) |
| 363 |
|
| 364 |
@asynctest |
| 365 |
async def test_connect_and_serve_with_version_negotiation(self) -> None: |
| 366 |
async with self.run_server() as server_port: |
| 367 |
|
| 368 |
configuration = QuicConfiguration(is_client=True, quic_logger=QuicLogger()) |
| 369 |
configuration.supported_versions.insert(0, 0x1A2A3A4A) |
| 370 |
|
| 371 |
response = await self.run_client( |
| 372 |
configuration=configuration, port=server_port |
| 373 |
) |
| 374 |
self.assertEqual(response, b"gnip") |
| 375 |
|
| 376 |
@asynctest |
| 377 |
async def test_connect_timeout(self) -> None: |
| 378 |
with self.assertRaises(ConnectionError): |
| 379 |
await self.run_client( |
| 380 |
port=self.bogus_port, |
| 381 |
configuration=QuicConfiguration(is_client=True, idle_timeout=5), |
| 382 |
) |
| 383 |
|
| 384 |
@asynctest |
| 385 |
async def test_connect_timeout_no_wait_connected(self) -> None: |
| 386 |
with self.assertRaises(ConnectionError): |
| 387 |
configuration = QuicConfiguration(is_client=True, idle_timeout=5) |
| 388 |
configuration.load_verify_locations(cafile=SERVER_CACERTFILE) |
| 389 |
async with connect( |
| 390 |
self.server_host, |
| 391 |
self.bogus_port, |
| 392 |
configuration=configuration, |
| 393 |
wait_connected=False, |
| 394 |
) as client: |
| 395 |
await client.ping() |
| 396 |
|
| 397 |
@asynctest |
| 398 |
async def test_connect_local_port(self) -> None: |
| 399 |
async with self.run_server() as server_port: |
| 400 |
response = await self.run_client(local_port=3456, port=server_port) |
| 401 |
self.assertEqual(response, b"gnip") |
| 402 |
|
| 403 |
@asynctest |
| 404 |
async def test_connect_local_port_bind(self) -> None: |
| 405 |
with self.assertRaises(OverflowError): |
| 406 |
await self.run_client(local_port=-1, port=self.bogus_port) |
| 407 |
|
| 408 |
@asynctest |
| 409 |
async def test_change_connection_id(self) -> None: |
| 410 |
async with self.run_server() as server_port: |
| 411 |
configuration = QuicConfiguration(is_client=True) |
| 412 |
configuration.load_verify_locations(cafile=SERVER_CACERTFILE) |
| 413 |
async with connect( |
| 414 |
self.server_host, server_port, configuration=configuration |
| 415 |
) as client: |
| 416 |
await client.ping() |
| 417 |
client.change_connection_id() |
| 418 |
await client.ping() |
| 419 |
|
| 420 |
@asynctest |
| 421 |
async def test_key_update(self) -> None: |
| 422 |
async with self.run_server() as server_port: |
| 423 |
configuration = QuicConfiguration(is_client=True) |
| 424 |
configuration.load_verify_locations(cafile=SERVER_CACERTFILE) |
| 425 |
async with connect( |
| 426 |
self.server_host, server_port, configuration=configuration |
| 427 |
) as client: |
| 428 |
await client.ping() |
| 429 |
client.request_key_update() |
| 430 |
await client.ping() |
| 431 |
|
| 432 |
@asynctest |
| 433 |
async def test_ping(self) -> None: |
| 434 |
async with self.run_server() as server_port: |
| 435 |
configuration = QuicConfiguration(is_client=True) |
| 436 |
configuration.load_verify_locations(cafile=SERVER_CACERTFILE) |
| 437 |
async with connect( |
| 438 |
self.server_host, server_port, configuration=configuration |
| 439 |
) as client: |
| 440 |
await client.ping() |
| 441 |
await client.ping() |
| 442 |
|
| 443 |
@asynctest |
| 444 |
async def test_ping_parallel(self) -> None: |
| 445 |
async with self.run_server() as server_port: |
| 446 |
configuration = QuicConfiguration(is_client=True) |
| 447 |
configuration.load_verify_locations(cafile=SERVER_CACERTFILE) |
| 448 |
async with connect( |
| 449 |
self.server_host, server_port, configuration=configuration |
| 450 |
) as client: |
| 451 |
coros = [client.ping() for x in range(16)] |
| 452 |
await asyncio.gather(*coros) |
| 453 |
|
| 454 |
@asynctest |
| 455 |
async def test_server_receives_garbage(self) -> None: |
| 456 |
configuration = QuicConfiguration(is_client=False) |
| 457 |
configuration.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE) |
| 458 |
server = await serve( |
| 459 |
host=self.server_host, |
| 460 |
port=0, |
| 461 |
configuration=configuration, |
| 462 |
) |
| 463 |
server.datagram_received(binascii.unhexlify("c00000000080"), ("1.2.3.4", 1234)) |
| 464 |
server.close() |
| 465 |
|
| 466 |
@asynctest |
| 467 |
async def test_combined_key(self) -> None: |
| 468 |
config1 = QuicConfiguration() |
| 469 |
config2 = QuicConfiguration() |
| 470 |
config1.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE) |
| 471 |
config2.load_cert_chain(SERVER_COMBINEDFILE) |
| 472 |
|
| 473 |
self.assertEqual(config1.certificate, config2.certificate) |