| 1 |
# Contains code from https://github.com/MagicStack/uvloop/tree/v0.16.0 |
| 2 |
# SPDX-License-Identifier: PSF-2.0 AND (MIT OR Apache-2.0) |
| 3 |
# SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io |
| 4 |
|
| 5 |
import asyncio |
| 6 |
import contextlib |
| 7 |
import gc |
| 8 |
import logging |
| 9 |
import select |
| 10 |
import socket |
| 11 |
import sys |
| 12 |
import tempfile |
| 13 |
import threading |
| 14 |
import time |
| 15 |
import unittest.mock |
| 16 |
import weakref |
| 17 |
import unittest |
| 18 |
|
| 19 |
try: |
| 20 |
import ssl |
| 21 |
except ImportError: |
| 22 |
ssl = None |
| 23 |
|
| 24 |
from test import support |
| 25 |
from test.test_asyncio import utils as test_utils |
| 26 |
|
| 27 |
|
| 28 |
MACOS = (sys.platform == 'darwin') |
| 29 |
BUF_MULTIPLIER = 1024 if not MACOS else 64 |
| 30 |
HANDSHAKE_TIMEOUT = support.LONG_TIMEOUT |
| 31 |
|
| 32 |
|
| 33 |
def tearDownModule(): |
| 34 |
asyncio.events._set_event_loop_policy(None) |
| 35 |
|
| 36 |
|
| 37 |
class MyBaseProto(asyncio.Protocol): |
| 38 |
connected = None |
| 39 |
done = None |
| 40 |
|
| 41 |
def __init__(self, loop=None): |
| 42 |
self.transport = None |
| 43 |
self.state = 'INITIAL' |
| 44 |
self.nbytes = 0 |
| 45 |
if loop is not None: |
| 46 |
self.connected = asyncio.Future(loop=loop) |
| 47 |
self.done = asyncio.Future(loop=loop) |
| 48 |
|
| 49 |
def connection_made(self, transport): |
| 50 |
self.transport = transport |
| 51 |
assert self.state == 'INITIAL', self.state |
| 52 |
self.state = 'CONNECTED' |
| 53 |
if self.connected: |
| 54 |
self.connected.set_result(None) |
| 55 |
|
| 56 |
def data_received(self, data): |
| 57 |
assert self.state == 'CONNECTED', self.state |
| 58 |
self.nbytes += len(data) |
| 59 |
|
| 60 |
def eof_received(self): |
| 61 |
assert self.state == 'CONNECTED', self.state |
| 62 |
self.state = 'EOF' |
| 63 |
|
| 64 |
def connection_lost(self, exc): |
| 65 |
assert self.state in ('CONNECTED', 'EOF'), self.state |
| 66 |
self.state = 'CLOSED' |
| 67 |
if self.done: |
| 68 |
self.done.set_result(None) |
| 69 |
|
| 70 |
|
| 71 |
class MessageOutFilter(logging.Filter): |
| 72 |
def __init__(self, msg): |
| 73 |
self.msg = msg |
| 74 |
|
| 75 |
def filter(self, record): |
| 76 |
if self.msg in record.msg: |
| 77 |
return False |
| 78 |
return True |
| 79 |
|
| 80 |
|
| 81 |
@unittest.skipIf(ssl is None, 'No ssl module') |
| 82 |
class TestSSL(test_utils.TestCase): |
| 83 |
|
| 84 |
PAYLOAD_SIZE = 1024 * 100 |
| 85 |
TIMEOUT = support.LONG_TIMEOUT |
| 86 |
|
| 87 |
def setUp(self): |
| 88 |
super().setUp() |
| 89 |
self.loop = asyncio.new_event_loop() |
| 90 |
self.set_event_loop(self.loop) |
| 91 |
self.addCleanup(self.loop.close) |
| 92 |
|
| 93 |
def tearDown(self): |
| 94 |
# just in case if we have transport close callbacks |
| 95 |
if not self.loop.is_closed(): |
| 96 |
test_utils.run_briefly(self.loop) |
| 97 |
|
| 98 |
self.doCleanups() |
| 99 |
support.gc_collect() |
| 100 |
super().tearDown() |
| 101 |
|
| 102 |
def tcp_server(self, server_prog, *, |
| 103 |
family=socket.AF_INET, |
| 104 |
addr=None, |
| 105 |
timeout=support.SHORT_TIMEOUT, |
| 106 |
backlog=1, |
| 107 |
max_clients=10): |
| 108 |
|
| 109 |
if addr is None: |
| 110 |
if family == getattr(socket, "AF_UNIX", None): |
| 111 |
with tempfile.NamedTemporaryFile() as tmp: |
| 112 |
addr = tmp.name |
| 113 |
else: |
| 114 |
addr = ('127.0.0.1', 0) |
| 115 |
|
| 116 |
sock = socket.socket(family, socket.SOCK_STREAM) |
| 117 |
|
| 118 |
if timeout is None: |
| 119 |
raise RuntimeError('timeout is required') |
| 120 |
if timeout <= 0: |
| 121 |
raise RuntimeError('only blocking sockets are supported') |
| 122 |
sock.settimeout(timeout) |
| 123 |
|
| 124 |
try: |
| 125 |
sock.bind(addr) |
| 126 |
sock.listen(backlog) |
| 127 |
except OSError as ex: |
| 128 |
sock.close() |
| 129 |
raise ex |
| 130 |
|
| 131 |
return TestThreadedServer( |
| 132 |
self, sock, server_prog, timeout, max_clients) |
| 133 |
|
| 134 |
def tcp_client(self, client_prog, |
| 135 |
family=socket.AF_INET, |
| 136 |
timeout=support.SHORT_TIMEOUT): |
| 137 |
|
| 138 |
sock = socket.socket(family, socket.SOCK_STREAM) |
| 139 |
|
| 140 |
if timeout is None: |
| 141 |
raise RuntimeError('timeout is required') |
| 142 |
if timeout <= 0: |
| 143 |
raise RuntimeError('only blocking sockets are supported') |
| 144 |
sock.settimeout(timeout) |
| 145 |
|
| 146 |
return TestThreadedClient( |
| 147 |
self, sock, client_prog, timeout) |
| 148 |
|
| 149 |
def unix_server(self, *args, **kwargs): |
| 150 |
return self.tcp_server(*args, family=socket.AF_UNIX, **kwargs) |
| 151 |
|
| 152 |
def unix_client(self, *args, **kwargs): |
| 153 |
return self.tcp_client(*args, family=socket.AF_UNIX, **kwargs) |
| 154 |
|
| 155 |
def _create_server_ssl_context(self, certfile, keyfile=None): |
| 156 |
sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) |
| 157 |
sslcontext.options |= ssl.OP_NO_SSLv2 |
| 158 |
sslcontext.load_cert_chain(certfile, keyfile) |
| 159 |
return sslcontext |
| 160 |
|
| 161 |
def _create_client_ssl_context(self, *, disable_verify=True): |
| 162 |
sslcontext = ssl.create_default_context() |
| 163 |
sslcontext.check_hostname = False |
| 164 |
if disable_verify: |
| 165 |
sslcontext.verify_mode = ssl.CERT_NONE |
| 166 |
return sslcontext |
| 167 |
|
| 168 |
@contextlib.contextmanager |
| 169 |
def _silence_eof_received_warning(self): |
| 170 |
# TODO This warning has to be fixed in asyncio. |
| 171 |
logger = logging.getLogger('asyncio') |
| 172 |
filter = MessageOutFilter('has no effect when using ssl') |
| 173 |
logger.addFilter(filter) |
| 174 |
try: |
| 175 |
yield |
| 176 |
finally: |
| 177 |
logger.removeFilter(filter) |
| 178 |
|
| 179 |
def _abort_socket_test(self, ex): |
| 180 |
try: |
| 181 |
self.loop.stop() |
| 182 |
finally: |
| 183 |
self.fail(ex) |
| 184 |
|
| 185 |
def new_loop(self): |
| 186 |
return asyncio.new_event_loop() |
| 187 |
|
| 188 |
def new_policy(self): |
| 189 |
return asyncio.DefaultEventLoopPolicy() |
| 190 |
|
| 191 |
async def wait_closed(self, obj): |
| 192 |
if not isinstance(obj, asyncio.StreamWriter): |
| 193 |
return |
| 194 |
try: |
| 195 |
await obj.wait_closed() |
| 196 |
except (BrokenPipeError, ConnectionError): |
| 197 |
pass |
| 198 |
|
| 199 |
@support.bigmemtest(size=25, memuse=90*2**20, dry_run=False) |
| 200 |
def test_create_server_ssl_1(self, size): |
| 201 |
CNT = 0 # number of clients that were successful |
| 202 |
TOTAL_CNT = size # total number of clients that test will create |
| 203 |
TIMEOUT = support.LONG_TIMEOUT # timeout for this test |
| 204 |
|
| 205 |
A_DATA = b'A' * 1024 * BUF_MULTIPLIER |
| 206 |
B_DATA = b'B' * 1024 * BUF_MULTIPLIER |
| 207 |
|
| 208 |
sslctx = self._create_server_ssl_context( |
| 209 |
test_utils.ONLYCERT, test_utils.ONLYKEY |
| 210 |
) |
| 211 |
client_sslctx = self._create_client_ssl_context() |
| 212 |
|
| 213 |
clients = [] |
| 214 |
|
| 215 |
async def handle_client(reader, writer): |
| 216 |
nonlocal CNT |
| 217 |
|
| 218 |
data = await reader.readexactly(len(A_DATA)) |
| 219 |
self.assertEqual(data, A_DATA) |
| 220 |
writer.write(b'OK') |
| 221 |
|
| 222 |
data = await reader.readexactly(len(B_DATA)) |
| 223 |
self.assertEqual(data, B_DATA) |
| 224 |
writer.writelines([b'SP', bytearray(b'A'), memoryview(b'M')]) |
| 225 |
|
| 226 |
await writer.drain() |
| 227 |
writer.close() |
| 228 |
|
| 229 |
CNT += 1 |
| 230 |
|
| 231 |
async def test_client(addr): |
| 232 |
fut = asyncio.Future() |
| 233 |
|
| 234 |
def prog(sock): |
| 235 |
try: |
| 236 |
sock.starttls(client_sslctx) |
| 237 |
sock.connect(addr) |
| 238 |
sock.send(A_DATA) |
| 239 |
|
| 240 |
data = sock.recv_all(2) |
| 241 |
self.assertEqual(data, b'OK') |
| 242 |
|
| 243 |
sock.send(B_DATA) |
| 244 |
data = sock.recv_all(4) |
| 245 |
self.assertEqual(data, b'SPAM') |
| 246 |
|
| 247 |
sock.close() |
| 248 |
|
| 249 |
except Exception as ex: |
| 250 |
self.loop.call_soon_threadsafe(fut.set_exception, ex) |
| 251 |
else: |
| 252 |
self.loop.call_soon_threadsafe(fut.set_result, None) |
| 253 |
|
| 254 |
client = self.tcp_client(prog) |
| 255 |
client.start() |
| 256 |
clients.append(client) |
| 257 |
|
| 258 |
await fut |
| 259 |
|
| 260 |
async def start_server(): |
| 261 |
srv = await asyncio.start_server( |
| 262 |
handle_client, |
| 263 |
'127.0.0.1', 0, |
| 264 |
family=socket.AF_INET, |
| 265 |
ssl=sslctx, |
| 266 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 267 |
|
| 268 |
try: |
| 269 |
srv_socks = srv.sockets |
| 270 |
self.assertTrue(srv_socks) |
| 271 |
|
| 272 |
addr = srv_socks[0].getsockname() |
| 273 |
|
| 274 |
tasks = [] |
| 275 |
for _ in range(TOTAL_CNT): |
| 276 |
tasks.append(test_client(addr)) |
| 277 |
|
| 278 |
await asyncio.wait_for(asyncio.gather(*tasks), TIMEOUT) |
| 279 |
|
| 280 |
finally: |
| 281 |
self.loop.call_soon(srv.close) |
| 282 |
await srv.wait_closed() |
| 283 |
|
| 284 |
with self._silence_eof_received_warning(): |
| 285 |
self.loop.run_until_complete(start_server()) |
| 286 |
|
| 287 |
self.assertEqual(CNT, TOTAL_CNT) |
| 288 |
|
| 289 |
for client in clients: |
| 290 |
client.stop() |
| 291 |
|
| 292 |
def test_create_connection_ssl_1(self): |
| 293 |
self.loop.set_exception_handler(None) |
| 294 |
|
| 295 |
CNT = 0 |
| 296 |
TOTAL_CNT = 25 |
| 297 |
|
| 298 |
A_DATA = b'A' * 1024 * BUF_MULTIPLIER |
| 299 |
B_DATA = b'B' * 1024 * BUF_MULTIPLIER |
| 300 |
|
| 301 |
sslctx = self._create_server_ssl_context( |
| 302 |
test_utils.ONLYCERT, |
| 303 |
test_utils.ONLYKEY |
| 304 |
) |
| 305 |
client_sslctx = self._create_client_ssl_context() |
| 306 |
|
| 307 |
def server(sock): |
| 308 |
sock.starttls( |
| 309 |
sslctx, |
| 310 |
server_side=True) |
| 311 |
|
| 312 |
data = sock.recv_all(len(A_DATA)) |
| 313 |
self.assertEqual(data, A_DATA) |
| 314 |
sock.send(b'OK') |
| 315 |
|
| 316 |
data = sock.recv_all(len(B_DATA)) |
| 317 |
self.assertEqual(data, B_DATA) |
| 318 |
sock.send(b'SPAM') |
| 319 |
|
| 320 |
sock.close() |
| 321 |
|
| 322 |
async def client(addr): |
| 323 |
reader, writer = await asyncio.open_connection( |
| 324 |
*addr, |
| 325 |
ssl=client_sslctx, |
| 326 |
server_hostname='', |
| 327 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 328 |
|
| 329 |
writer.write(A_DATA) |
| 330 |
self.assertEqual(await reader.readexactly(2), b'OK') |
| 331 |
|
| 332 |
writer.write(B_DATA) |
| 333 |
self.assertEqual(await reader.readexactly(4), b'SPAM') |
| 334 |
|
| 335 |
nonlocal CNT |
| 336 |
CNT += 1 |
| 337 |
|
| 338 |
writer.close() |
| 339 |
await self.wait_closed(writer) |
| 340 |
|
| 341 |
async def client_sock(addr): |
| 342 |
sock = socket.socket() |
| 343 |
sock.connect(addr) |
| 344 |
reader, writer = await asyncio.open_connection( |
| 345 |
sock=sock, |
| 346 |
ssl=client_sslctx, |
| 347 |
server_hostname='', |
| 348 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 349 |
|
| 350 |
writer.write(A_DATA) |
| 351 |
self.assertEqual(await reader.readexactly(2), b'OK') |
| 352 |
|
| 353 |
writer.write(B_DATA) |
| 354 |
self.assertEqual(await reader.readexactly(4), b'SPAM') |
| 355 |
|
| 356 |
nonlocal CNT |
| 357 |
CNT += 1 |
| 358 |
|
| 359 |
writer.close() |
| 360 |
await self.wait_closed(writer) |
| 361 |
sock.close() |
| 362 |
|
| 363 |
def run(coro): |
| 364 |
nonlocal CNT |
| 365 |
CNT = 0 |
| 366 |
|
| 367 |
async def _gather(*tasks): |
| 368 |
# trampoline |
| 369 |
return await asyncio.gather(*tasks) |
| 370 |
|
| 371 |
with self.tcp_server(server, |
| 372 |
max_clients=TOTAL_CNT, |
| 373 |
backlog=TOTAL_CNT) as srv: |
| 374 |
tasks = [] |
| 375 |
for _ in range(TOTAL_CNT): |
| 376 |
tasks.append(coro(srv.addr)) |
| 377 |
|
| 378 |
self.loop.run_until_complete(_gather(*tasks)) |
| 379 |
|
| 380 |
self.assertEqual(CNT, TOTAL_CNT) |
| 381 |
|
| 382 |
with self._silence_eof_received_warning(): |
| 383 |
run(client) |
| 384 |
|
| 385 |
with self._silence_eof_received_warning(): |
| 386 |
run(client_sock) |
| 387 |
|
| 388 |
def test_create_connection_ssl_slow_handshake(self): |
| 389 |
client_sslctx = self._create_client_ssl_context() |
| 390 |
|
| 391 |
# silence error logger |
| 392 |
self.loop.set_exception_handler(lambda *args: None) |
| 393 |
|
| 394 |
def server(sock): |
| 395 |
try: |
| 396 |
sock.recv_all(1024 * 1024) |
| 397 |
except ConnectionAbortedError: |
| 398 |
pass |
| 399 |
finally: |
| 400 |
sock.close() |
| 401 |
|
| 402 |
async def client(addr): |
| 403 |
reader, writer = await asyncio.open_connection( |
| 404 |
*addr, |
| 405 |
ssl=client_sslctx, |
| 406 |
server_hostname='', |
| 407 |
ssl_handshake_timeout=1.0) |
| 408 |
writer.close() |
| 409 |
await self.wait_closed(writer) |
| 410 |
|
| 411 |
with self.tcp_server(server, |
| 412 |
max_clients=1, |
| 413 |
backlog=1) as srv: |
| 414 |
|
| 415 |
with self.assertRaisesRegex( |
| 416 |
ConnectionAbortedError, |
| 417 |
r'SSL handshake.*is taking longer'): |
| 418 |
|
| 419 |
self.loop.run_until_complete(client(srv.addr)) |
| 420 |
|
| 421 |
def test_create_connection_ssl_failed_certificate(self): |
| 422 |
# silence error logger |
| 423 |
self.loop.set_exception_handler(lambda *args: None) |
| 424 |
|
| 425 |
sslctx = self._create_server_ssl_context( |
| 426 |
test_utils.ONLYCERT, |
| 427 |
test_utils.ONLYKEY |
| 428 |
) |
| 429 |
client_sslctx = self._create_client_ssl_context(disable_verify=False) |
| 430 |
|
| 431 |
def server(sock): |
| 432 |
try: |
| 433 |
sock.starttls( |
| 434 |
sslctx, |
| 435 |
server_side=True) |
| 436 |
sock.connect() |
| 437 |
except (ssl.SSLError, OSError): |
| 438 |
pass |
| 439 |
finally: |
| 440 |
sock.close() |
| 441 |
|
| 442 |
async def client(addr): |
| 443 |
reader, writer = await asyncio.open_connection( |
| 444 |
*addr, |
| 445 |
ssl=client_sslctx, |
| 446 |
server_hostname='', |
| 447 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 448 |
writer.close() |
| 449 |
await self.wait_closed(writer) |
| 450 |
|
| 451 |
with self.tcp_server(server, |
| 452 |
max_clients=1, |
| 453 |
backlog=1) as srv: |
| 454 |
|
| 455 |
with self.assertRaises(ssl.SSLCertVerificationError): |
| 456 |
self.loop.run_until_complete(client(srv.addr)) |
| 457 |
|
| 458 |
def test_ssl_handshake_timeout(self): |
| 459 |
# bpo-29970: Check that a connection is aborted if handshake is not |
| 460 |
# completed in timeout period, instead of remaining open indefinitely |
| 461 |
client_sslctx = test_utils.simple_client_sslcontext() |
| 462 |
|
| 463 |
# silence error logger |
| 464 |
messages = [] |
| 465 |
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) |
| 466 |
|
| 467 |
server_side_aborted = False |
| 468 |
|
| 469 |
def server(sock): |
| 470 |
nonlocal server_side_aborted |
| 471 |
try: |
| 472 |
sock.recv_all(1024 * 1024) |
| 473 |
except ConnectionAbortedError: |
| 474 |
server_side_aborted = True |
| 475 |
finally: |
| 476 |
sock.close() |
| 477 |
|
| 478 |
async def client(addr): |
| 479 |
await asyncio.wait_for( |
| 480 |
self.loop.create_connection( |
| 481 |
asyncio.Protocol, |
| 482 |
*addr, |
| 483 |
ssl=client_sslctx, |
| 484 |
server_hostname='', |
| 485 |
ssl_handshake_timeout=10.0), |
| 486 |
0.5) |
| 487 |
|
| 488 |
with self.tcp_server(server, |
| 489 |
max_clients=1, |
| 490 |
backlog=1) as srv: |
| 491 |
|
| 492 |
with self.assertRaises(asyncio.TimeoutError): |
| 493 |
self.loop.run_until_complete(client(srv.addr)) |
| 494 |
|
| 495 |
self.assertTrue(server_side_aborted) |
| 496 |
|
| 497 |
# Python issue #23197: cancelling a handshake must not raise an |
| 498 |
# exception or log an error, even if the handshake failed |
| 499 |
self.assertEqual(messages, []) |
| 500 |
|
| 501 |
def test_ssl_handshake_connection_lost(self): |
| 502 |
# #246: make sure that no connection_lost() is called before |
| 503 |
# connection_made() is called first |
| 504 |
|
| 505 |
client_sslctx = test_utils.simple_client_sslcontext() |
| 506 |
|
| 507 |
# silence error logger |
| 508 |
self.loop.set_exception_handler(lambda loop, ctx: None) |
| 509 |
|
| 510 |
connection_made_called = False |
| 511 |
connection_lost_called = False |
| 512 |
|
| 513 |
def server(sock): |
| 514 |
sock.recv(1024) |
| 515 |
# break the connection during handshake |
| 516 |
sock.close() |
| 517 |
|
| 518 |
class ClientProto(asyncio.Protocol): |
| 519 |
def connection_made(self, transport): |
| 520 |
nonlocal connection_made_called |
| 521 |
connection_made_called = True |
| 522 |
|
| 523 |
def connection_lost(self, exc): |
| 524 |
nonlocal connection_lost_called |
| 525 |
connection_lost_called = True |
| 526 |
|
| 527 |
async def client(addr): |
| 528 |
await self.loop.create_connection( |
| 529 |
ClientProto, |
| 530 |
*addr, |
| 531 |
ssl=client_sslctx, |
| 532 |
server_hostname=''), |
| 533 |
|
| 534 |
with self.tcp_server(server, |
| 535 |
max_clients=1, |
| 536 |
backlog=1) as srv: |
| 537 |
|
| 538 |
with self.assertRaises(ConnectionResetError): |
| 539 |
self.loop.run_until_complete(client(srv.addr)) |
| 540 |
|
| 541 |
if connection_lost_called: |
| 542 |
if connection_made_called: |
| 543 |
self.fail("unexpected call to connection_lost()") |
| 544 |
else: |
| 545 |
self.fail("unexpected call to connection_lost() without" |
| 546 |
"calling connection_made()") |
| 547 |
elif connection_made_called: |
| 548 |
self.fail("unexpected call to connection_made()") |
| 549 |
|
| 550 |
def test_ssl_connect_accepted_socket(self): |
| 551 |
proto = ssl.PROTOCOL_TLS_SERVER |
| 552 |
server_context = ssl.SSLContext(proto) |
| 553 |
server_context.load_cert_chain(test_utils.ONLYCERT, test_utils.ONLYKEY) |
| 554 |
if hasattr(server_context, 'check_hostname'): |
| 555 |
server_context.check_hostname = False |
| 556 |
server_context.verify_mode = ssl.CERT_NONE |
| 557 |
|
| 558 |
client_context = ssl.SSLContext(proto) |
| 559 |
if hasattr(server_context, 'check_hostname'): |
| 560 |
client_context.check_hostname = False |
| 561 |
client_context.verify_mode = ssl.CERT_NONE |
| 562 |
|
| 563 |
def test_connect_accepted_socket(self, server_ssl=None, client_ssl=None): |
| 564 |
loop = self.loop |
| 565 |
|
| 566 |
class MyProto(MyBaseProto): |
| 567 |
|
| 568 |
def connection_lost(self, exc): |
| 569 |
super().connection_lost(exc) |
| 570 |
loop.call_soon(loop.stop) |
| 571 |
|
| 572 |
def data_received(self, data): |
| 573 |
super().data_received(data) |
| 574 |
self.transport.write(expected_response) |
| 575 |
|
| 576 |
lsock = socket.socket(socket.AF_INET) |
| 577 |
lsock.bind(('127.0.0.1', 0)) |
| 578 |
lsock.listen(1) |
| 579 |
addr = lsock.getsockname() |
| 580 |
|
| 581 |
message = b'test data' |
| 582 |
response = None |
| 583 |
expected_response = b'roger' |
| 584 |
|
| 585 |
def client(): |
| 586 |
nonlocal response |
| 587 |
try: |
| 588 |
csock = socket.socket(socket.AF_INET) |
| 589 |
if client_ssl is not None: |
| 590 |
csock = client_ssl.wrap_socket(csock) |
| 591 |
csock.connect(addr) |
| 592 |
csock.sendall(message) |
| 593 |
response = csock.recv(99) |
| 594 |
csock.close() |
| 595 |
except Exception as exc: |
| 596 |
print( |
| 597 |
"Failure in client thread in test_connect_accepted_socket", |
| 598 |
exc) |
| 599 |
|
| 600 |
thread = threading.Thread(target=client, daemon=True) |
| 601 |
thread.start() |
| 602 |
|
| 603 |
conn, _ = lsock.accept() |
| 604 |
proto = MyProto(loop=loop) |
| 605 |
proto.loop = loop |
| 606 |
|
| 607 |
extras = {} |
| 608 |
if server_ssl: |
| 609 |
extras = dict(ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 610 |
|
| 611 |
f = loop.create_task( |
| 612 |
loop.connect_accepted_socket( |
| 613 |
(lambda: proto), conn, ssl=server_ssl, |
| 614 |
**extras)) |
| 615 |
loop.run_forever() |
| 616 |
conn.close() |
| 617 |
lsock.close() |
| 618 |
|
| 619 |
thread.join(1) |
| 620 |
self.assertFalse(thread.is_alive()) |
| 621 |
self.assertEqual(proto.state, 'CLOSED') |
| 622 |
self.assertEqual(proto.nbytes, len(message)) |
| 623 |
self.assertEqual(response, expected_response) |
| 624 |
tr, _ = f.result() |
| 625 |
|
| 626 |
if server_ssl: |
| 627 |
self.assertIn('SSL', tr.__class__.__name__) |
| 628 |
|
| 629 |
tr.close() |
| 630 |
# let it close |
| 631 |
self.loop.run_until_complete(asyncio.sleep(0.1)) |
| 632 |
|
| 633 |
def test_start_tls_client_corrupted_ssl(self): |
| 634 |
self.loop.set_exception_handler(lambda loop, ctx: None) |
| 635 |
|
| 636 |
sslctx = test_utils.simple_server_sslcontext() |
| 637 |
client_sslctx = test_utils.simple_client_sslcontext() |
| 638 |
|
| 639 |
def server(sock): |
| 640 |
orig_sock = sock.dup() |
| 641 |
try: |
| 642 |
sock.starttls( |
| 643 |
sslctx, |
| 644 |
server_side=True) |
| 645 |
sock.sendall(b'A\n') |
| 646 |
sock.recv_all(1) |
| 647 |
orig_sock.send(b'please corrupt the SSL connection') |
| 648 |
except ssl.SSLError: |
| 649 |
pass |
| 650 |
finally: |
| 651 |
sock.close() |
| 652 |
orig_sock.close() |
| 653 |
|
| 654 |
async def client(addr): |
| 655 |
reader, writer = await asyncio.open_connection( |
| 656 |
*addr, |
| 657 |
ssl=client_sslctx, |
| 658 |
server_hostname='', |
| 659 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 660 |
|
| 661 |
self.assertEqual(await reader.readline(), b'A\n') |
| 662 |
writer.write(b'B') |
| 663 |
with self.assertRaises(ssl.SSLError): |
| 664 |
await reader.readline() |
| 665 |
writer.close() |
| 666 |
try: |
| 667 |
await self.wait_closed(writer) |
| 668 |
except ssl.SSLError: |
| 669 |
pass |
| 670 |
return 'OK' |
| 671 |
|
| 672 |
with self.tcp_server(server, |
| 673 |
max_clients=1, |
| 674 |
backlog=1) as srv: |
| 675 |
|
| 676 |
res = self.loop.run_until_complete(client(srv.addr)) |
| 677 |
|
| 678 |
self.assertEqual(res, 'OK') |
| 679 |
|
| 680 |
def test_start_tls_client_reg_proto_1(self): |
| 681 |
HELLO_MSG = b'1' * self.PAYLOAD_SIZE |
| 682 |
|
| 683 |
server_context = test_utils.simple_server_sslcontext() |
| 684 |
client_context = test_utils.simple_client_sslcontext() |
| 685 |
|
| 686 |
def serve(sock): |
| 687 |
sock.settimeout(self.TIMEOUT) |
| 688 |
|
| 689 |
data = sock.recv_all(len(HELLO_MSG)) |
| 690 |
self.assertEqual(len(data), len(HELLO_MSG)) |
| 691 |
|
| 692 |
sock.starttls(server_context, server_side=True) |
| 693 |
|
| 694 |
sock.sendall(b'O') |
| 695 |
data = sock.recv_all(len(HELLO_MSG)) |
| 696 |
self.assertEqual(len(data), len(HELLO_MSG)) |
| 697 |
|
| 698 |
sock.unwrap() |
| 699 |
sock.close() |
| 700 |
|
| 701 |
class ClientProto(asyncio.Protocol): |
| 702 |
def __init__(self, on_data, on_eof): |
| 703 |
self.on_data = on_data |
| 704 |
self.on_eof = on_eof |
| 705 |
self.con_made_cnt = 0 |
| 706 |
|
| 707 |
def connection_made(proto, tr): |
| 708 |
proto.con_made_cnt += 1 |
| 709 |
# Ensure connection_made gets called only once. |
| 710 |
self.assertEqual(proto.con_made_cnt, 1) |
| 711 |
|
| 712 |
def data_received(self, data): |
| 713 |
self.on_data.set_result(data) |
| 714 |
|
| 715 |
def eof_received(self): |
| 716 |
self.on_eof.set_result(True) |
| 717 |
|
| 718 |
async def client(addr): |
| 719 |
await asyncio.sleep(0.5) |
| 720 |
|
| 721 |
on_data = self.loop.create_future() |
| 722 |
on_eof = self.loop.create_future() |
| 723 |
|
| 724 |
tr, proto = await self.loop.create_connection( |
| 725 |
lambda: ClientProto(on_data, on_eof), *addr) |
| 726 |
|
| 727 |
tr.write(HELLO_MSG) |
| 728 |
new_tr = await self.loop.start_tls(tr, proto, client_context) |
| 729 |
|
| 730 |
self.assertEqual(await on_data, b'O') |
| 731 |
new_tr.write(HELLO_MSG) |
| 732 |
await on_eof |
| 733 |
|
| 734 |
new_tr.close() |
| 735 |
|
| 736 |
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv: |
| 737 |
self.loop.run_until_complete( |
| 738 |
asyncio.wait_for(client(srv.addr), |
| 739 |
timeout=support.SHORT_TIMEOUT)) |
| 740 |
|
| 741 |
def test_create_connection_memory_leak(self): |
| 742 |
HELLO_MSG = b'1' * self.PAYLOAD_SIZE |
| 743 |
|
| 744 |
server_context = self._create_server_ssl_context( |
| 745 |
test_utils.ONLYCERT, test_utils.ONLYKEY) |
| 746 |
client_context = self._create_client_ssl_context() |
| 747 |
|
| 748 |
def serve(sock): |
| 749 |
sock.settimeout(self.TIMEOUT) |
| 750 |
|
| 751 |
sock.starttls(server_context, server_side=True) |
| 752 |
|
| 753 |
sock.sendall(b'O') |
| 754 |
data = sock.recv_all(len(HELLO_MSG)) |
| 755 |
self.assertEqual(len(data), len(HELLO_MSG)) |
| 756 |
|
| 757 |
sock.unwrap() |
| 758 |
sock.close() |
| 759 |
|
| 760 |
class ClientProto(asyncio.Protocol): |
| 761 |
def __init__(self, on_data, on_eof): |
| 762 |
self.on_data = on_data |
| 763 |
self.on_eof = on_eof |
| 764 |
self.con_made_cnt = 0 |
| 765 |
|
| 766 |
def connection_made(proto, tr): |
| 767 |
# XXX: We assume user stores the transport in protocol |
| 768 |
proto.tr = tr |
| 769 |
proto.con_made_cnt += 1 |
| 770 |
# Ensure connection_made gets called only once. |
| 771 |
self.assertEqual(proto.con_made_cnt, 1) |
| 772 |
|
| 773 |
def data_received(self, data): |
| 774 |
self.on_data.set_result(data) |
| 775 |
|
| 776 |
def eof_received(self): |
| 777 |
self.on_eof.set_result(True) |
| 778 |
|
| 779 |
async def client(addr): |
| 780 |
await asyncio.sleep(0.5) |
| 781 |
|
| 782 |
on_data = self.loop.create_future() |
| 783 |
on_eof = self.loop.create_future() |
| 784 |
|
| 785 |
tr, proto = await self.loop.create_connection( |
| 786 |
lambda: ClientProto(on_data, on_eof), *addr, |
| 787 |
ssl=client_context) |
| 788 |
|
| 789 |
self.assertEqual(await on_data, b'O') |
| 790 |
tr.write(HELLO_MSG) |
| 791 |
await on_eof |
| 792 |
|
| 793 |
tr.close() |
| 794 |
|
| 795 |
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv: |
| 796 |
self.loop.run_until_complete( |
| 797 |
asyncio.wait_for(client(srv.addr), |
| 798 |
timeout=support.SHORT_TIMEOUT)) |
| 799 |
|
| 800 |
# No garbage is left for SSL client from loop.create_connection, even |
| 801 |
# if user stores the SSLTransport in corresponding protocol instance |
| 802 |
client_context = weakref.ref(client_context) |
| 803 |
self.assertIsNone(client_context()) |
| 804 |
|
| 805 |
def test_start_tls_client_buf_proto_1(self): |
| 806 |
HELLO_MSG = b'1' * self.PAYLOAD_SIZE |
| 807 |
|
| 808 |
server_context = test_utils.simple_server_sslcontext() |
| 809 |
client_context = test_utils.simple_client_sslcontext() |
| 810 |
|
| 811 |
client_con_made_calls = 0 |
| 812 |
|
| 813 |
def serve(sock): |
| 814 |
sock.settimeout(self.TIMEOUT) |
| 815 |
|
| 816 |
data = sock.recv_all(len(HELLO_MSG)) |
| 817 |
self.assertEqual(len(data), len(HELLO_MSG)) |
| 818 |
|
| 819 |
sock.starttls(server_context, server_side=True) |
| 820 |
|
| 821 |
sock.sendall(b'O') |
| 822 |
data = sock.recv_all(len(HELLO_MSG)) |
| 823 |
self.assertEqual(len(data), len(HELLO_MSG)) |
| 824 |
|
| 825 |
sock.sendall(b'2') |
| 826 |
data = sock.recv_all(len(HELLO_MSG)) |
| 827 |
self.assertEqual(len(data), len(HELLO_MSG)) |
| 828 |
|
| 829 |
sock.unwrap() |
| 830 |
sock.close() |
| 831 |
|
| 832 |
class ClientProtoFirst(asyncio.BufferedProtocol): |
| 833 |
def __init__(self, on_data): |
| 834 |
self.on_data = on_data |
| 835 |
self.buf = bytearray(1) |
| 836 |
|
| 837 |
def connection_made(self, tr): |
| 838 |
nonlocal client_con_made_calls |
| 839 |
client_con_made_calls += 1 |
| 840 |
|
| 841 |
def get_buffer(self, sizehint): |
| 842 |
return self.buf |
| 843 |
|
| 844 |
def buffer_updated(self, nsize): |
| 845 |
assert nsize == 1 |
| 846 |
self.on_data.set_result(bytes(self.buf[:nsize])) |
| 847 |
|
| 848 |
def eof_received(self): |
| 849 |
pass |
| 850 |
|
| 851 |
class ClientProtoSecond(asyncio.Protocol): |
| 852 |
def __init__(self, on_data, on_eof): |
| 853 |
self.on_data = on_data |
| 854 |
self.on_eof = on_eof |
| 855 |
self.con_made_cnt = 0 |
| 856 |
|
| 857 |
def connection_made(self, tr): |
| 858 |
nonlocal client_con_made_calls |
| 859 |
client_con_made_calls += 1 |
| 860 |
|
| 861 |
def data_received(self, data): |
| 862 |
self.on_data.set_result(data) |
| 863 |
|
| 864 |
def eof_received(self): |
| 865 |
self.on_eof.set_result(True) |
| 866 |
|
| 867 |
async def client(addr): |
| 868 |
await asyncio.sleep(0.5) |
| 869 |
|
| 870 |
on_data1 = self.loop.create_future() |
| 871 |
on_data2 = self.loop.create_future() |
| 872 |
on_eof = self.loop.create_future() |
| 873 |
|
| 874 |
tr, proto = await self.loop.create_connection( |
| 875 |
lambda: ClientProtoFirst(on_data1), *addr) |
| 876 |
|
| 877 |
tr.write(HELLO_MSG) |
| 878 |
new_tr = await self.loop.start_tls(tr, proto, client_context) |
| 879 |
|
| 880 |
self.assertEqual(await on_data1, b'O') |
| 881 |
new_tr.write(HELLO_MSG) |
| 882 |
|
| 883 |
new_tr.set_protocol(ClientProtoSecond(on_data2, on_eof)) |
| 884 |
self.assertEqual(await on_data2, b'2') |
| 885 |
new_tr.write(HELLO_MSG) |
| 886 |
await on_eof |
| 887 |
|
| 888 |
new_tr.close() |
| 889 |
|
| 890 |
# connection_made() should be called only once -- when |
| 891 |
# we establish connection for the first time. Start TLS |
| 892 |
# doesn't call connection_made() on application protocols. |
| 893 |
self.assertEqual(client_con_made_calls, 1) |
| 894 |
|
| 895 |
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv: |
| 896 |
self.loop.run_until_complete( |
| 897 |
asyncio.wait_for(client(srv.addr), |
| 898 |
timeout=self.TIMEOUT)) |
| 899 |
|
| 900 |
def test_start_tls_slow_client_cancel(self): |
| 901 |
HELLO_MSG = b'1' * self.PAYLOAD_SIZE |
| 902 |
|
| 903 |
client_context = test_utils.simple_client_sslcontext() |
| 904 |
server_waits_on_handshake = self.loop.create_future() |
| 905 |
|
| 906 |
def serve(sock): |
| 907 |
sock.settimeout(self.TIMEOUT) |
| 908 |
|
| 909 |
data = sock.recv_all(len(HELLO_MSG)) |
| 910 |
self.assertEqual(len(data), len(HELLO_MSG)) |
| 911 |
|
| 912 |
try: |
| 913 |
self.loop.call_soon_threadsafe( |
| 914 |
server_waits_on_handshake.set_result, None) |
| 915 |
data = sock.recv_all(1024 * 1024) |
| 916 |
except ConnectionAbortedError: |
| 917 |
pass |
| 918 |
finally: |
| 919 |
sock.close() |
| 920 |
|
| 921 |
class ClientProto(asyncio.Protocol): |
| 922 |
def __init__(self, on_data, on_eof): |
| 923 |
self.on_data = on_data |
| 924 |
self.on_eof = on_eof |
| 925 |
self.con_made_cnt = 0 |
| 926 |
|
| 927 |
def connection_made(proto, tr): |
| 928 |
proto.con_made_cnt += 1 |
| 929 |
# Ensure connection_made gets called only once. |
| 930 |
self.assertEqual(proto.con_made_cnt, 1) |
| 931 |
|
| 932 |
def data_received(self, data): |
| 933 |
self.on_data.set_result(data) |
| 934 |
|
| 935 |
def eof_received(self): |
| 936 |
self.on_eof.set_result(True) |
| 937 |
|
| 938 |
async def client(addr): |
| 939 |
await asyncio.sleep(0.5) |
| 940 |
|
| 941 |
on_data = self.loop.create_future() |
| 942 |
on_eof = self.loop.create_future() |
| 943 |
|
| 944 |
tr, proto = await self.loop.create_connection( |
| 945 |
lambda: ClientProto(on_data, on_eof), *addr) |
| 946 |
|
| 947 |
tr.write(HELLO_MSG) |
| 948 |
|
| 949 |
await server_waits_on_handshake |
| 950 |
|
| 951 |
with self.assertRaises(asyncio.TimeoutError): |
| 952 |
await asyncio.wait_for( |
| 953 |
self.loop.start_tls(tr, proto, client_context), |
| 954 |
0.5) |
| 955 |
|
| 956 |
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv: |
| 957 |
self.loop.run_until_complete( |
| 958 |
asyncio.wait_for(client(srv.addr), |
| 959 |
timeout=support.SHORT_TIMEOUT)) |
| 960 |
|
| 961 |
def test_start_tls_server_1(self): |
| 962 |
HELLO_MSG = b'1' * self.PAYLOAD_SIZE |
| 963 |
|
| 964 |
server_context = test_utils.simple_server_sslcontext() |
| 965 |
client_context = test_utils.simple_client_sslcontext() |
| 966 |
|
| 967 |
def client(sock, addr): |
| 968 |
sock.settimeout(self.TIMEOUT) |
| 969 |
|
| 970 |
sock.connect(addr) |
| 971 |
data = sock.recv_all(len(HELLO_MSG)) |
| 972 |
self.assertEqual(len(data), len(HELLO_MSG)) |
| 973 |
|
| 974 |
sock.starttls(client_context) |
| 975 |
sock.sendall(HELLO_MSG) |
| 976 |
|
| 977 |
sock.unwrap() |
| 978 |
sock.close() |
| 979 |
|
| 980 |
class ServerProto(asyncio.Protocol): |
| 981 |
def __init__(self, on_con, on_eof, on_con_lost): |
| 982 |
self.on_con = on_con |
| 983 |
self.on_eof = on_eof |
| 984 |
self.on_con_lost = on_con_lost |
| 985 |
self.data = b'' |
| 986 |
|
| 987 |
def connection_made(self, tr): |
| 988 |
self.on_con.set_result(tr) |
| 989 |
|
| 990 |
def data_received(self, data): |
| 991 |
self.data += data |
| 992 |
|
| 993 |
def eof_received(self): |
| 994 |
self.on_eof.set_result(1) |
| 995 |
|
| 996 |
def connection_lost(self, exc): |
| 997 |
if exc is None: |
| 998 |
self.on_con_lost.set_result(None) |
| 999 |
else: |
| 1000 |
self.on_con_lost.set_exception(exc) |
| 1001 |
|
| 1002 |
async def main(proto, on_con, on_eof, on_con_lost): |
| 1003 |
tr = await on_con |
| 1004 |
tr.write(HELLO_MSG) |
| 1005 |
|
| 1006 |
self.assertEqual(proto.data, b'') |
| 1007 |
|
| 1008 |
new_tr = await self.loop.start_tls( |
| 1009 |
tr, proto, server_context, |
| 1010 |
server_side=True, |
| 1011 |
ssl_handshake_timeout=self.TIMEOUT) |
| 1012 |
|
| 1013 |
await on_eof |
| 1014 |
await on_con_lost |
| 1015 |
self.assertEqual(proto.data, HELLO_MSG) |
| 1016 |
new_tr.close() |
| 1017 |
|
| 1018 |
async def run_main(): |
| 1019 |
on_con = self.loop.create_future() |
| 1020 |
on_eof = self.loop.create_future() |
| 1021 |
on_con_lost = self.loop.create_future() |
| 1022 |
proto = ServerProto(on_con, on_eof, on_con_lost) |
| 1023 |
|
| 1024 |
server = await self.loop.create_server( |
| 1025 |
lambda: proto, '127.0.0.1', 0) |
| 1026 |
addr = server.sockets[0].getsockname() |
| 1027 |
|
| 1028 |
with self.tcp_client(lambda sock: client(sock, addr), |
| 1029 |
timeout=self.TIMEOUT): |
| 1030 |
await asyncio.wait_for( |
| 1031 |
main(proto, on_con, on_eof, on_con_lost), |
| 1032 |
timeout=self.TIMEOUT) |
| 1033 |
|
| 1034 |
server.close() |
| 1035 |
await server.wait_closed() |
| 1036 |
|
| 1037 |
self.loop.run_until_complete(run_main()) |
| 1038 |
|
| 1039 |
@support.bigmemtest(size=25, memuse=90*2**20, dry_run=False) |
| 1040 |
def test_create_server_ssl_over_ssl(self, size): |
| 1041 |
CNT = 0 # number of clients that were successful |
| 1042 |
TOTAL_CNT = size # total number of clients that test will create |
| 1043 |
TIMEOUT = support.LONG_TIMEOUT # timeout for this test |
| 1044 |
|
| 1045 |
A_DATA = b'A' * 1024 * BUF_MULTIPLIER |
| 1046 |
B_DATA = b'B' * 1024 * BUF_MULTIPLIER |
| 1047 |
|
| 1048 |
sslctx_1 = self._create_server_ssl_context( |
| 1049 |
test_utils.ONLYCERT, test_utils.ONLYKEY) |
| 1050 |
client_sslctx_1 = self._create_client_ssl_context() |
| 1051 |
sslctx_2 = self._create_server_ssl_context( |
| 1052 |
test_utils.ONLYCERT, test_utils.ONLYKEY) |
| 1053 |
client_sslctx_2 = self._create_client_ssl_context() |
| 1054 |
|
| 1055 |
clients = [] |
| 1056 |
|
| 1057 |
async def handle_client(reader, writer): |
| 1058 |
nonlocal CNT |
| 1059 |
|
| 1060 |
data = await reader.readexactly(len(A_DATA)) |
| 1061 |
self.assertEqual(data, A_DATA) |
| 1062 |
writer.write(b'OK') |
| 1063 |
|
| 1064 |
data = await reader.readexactly(len(B_DATA)) |
| 1065 |
self.assertEqual(data, B_DATA) |
| 1066 |
writer.writelines([b'SP', bytearray(b'A'), memoryview(b'M')]) |
| 1067 |
|
| 1068 |
await writer.drain() |
| 1069 |
writer.close() |
| 1070 |
|
| 1071 |
CNT += 1 |
| 1072 |
|
| 1073 |
class ServerProtocol(asyncio.StreamReaderProtocol): |
| 1074 |
def connection_made(self, transport): |
| 1075 |
super_ = super() |
| 1076 |
transport.pause_reading() |
| 1077 |
fut = self._loop.create_task(self._loop.start_tls( |
| 1078 |
transport, self, sslctx_2, server_side=True)) |
| 1079 |
|
| 1080 |
def cb(_): |
| 1081 |
try: |
| 1082 |
tr = fut.result() |
| 1083 |
except Exception as ex: |
| 1084 |
super_.connection_lost(ex) |
| 1085 |
else: |
| 1086 |
super_.connection_made(tr) |
| 1087 |
fut.add_done_callback(cb) |
| 1088 |
|
| 1089 |
def server_protocol_factory(): |
| 1090 |
reader = asyncio.StreamReader() |
| 1091 |
protocol = ServerProtocol(reader, handle_client) |
| 1092 |
return protocol |
| 1093 |
|
| 1094 |
async def test_client(addr): |
| 1095 |
fut = asyncio.Future() |
| 1096 |
|
| 1097 |
def prog(sock): |
| 1098 |
try: |
| 1099 |
sock.connect(addr) |
| 1100 |
sock.starttls(client_sslctx_1) |
| 1101 |
|
| 1102 |
# because wrap_socket() doesn't work correctly on |
| 1103 |
# SSLSocket, we have to do the 2nd level SSL manually |
| 1104 |
incoming = ssl.MemoryBIO() |
| 1105 |
outgoing = ssl.MemoryBIO() |
| 1106 |
sslobj = client_sslctx_2.wrap_bio(incoming, outgoing) |
| 1107 |
|
| 1108 |
def do(func, *args): |
| 1109 |
while True: |
| 1110 |
try: |
| 1111 |
rv = func(*args) |
| 1112 |
break |
| 1113 |
except ssl.SSLWantReadError: |
| 1114 |
if outgoing.pending: |
| 1115 |
sock.send(outgoing.read()) |
| 1116 |
incoming.write(sock.recv(65536)) |
| 1117 |
if outgoing.pending: |
| 1118 |
sock.send(outgoing.read()) |
| 1119 |
return rv |
| 1120 |
|
| 1121 |
do(sslobj.do_handshake) |
| 1122 |
|
| 1123 |
do(sslobj.write, A_DATA) |
| 1124 |
data = do(sslobj.read, 2) |
| 1125 |
self.assertEqual(data, b'OK') |
| 1126 |
|
| 1127 |
do(sslobj.write, B_DATA) |
| 1128 |
data = b'' |
| 1129 |
while True: |
| 1130 |
chunk = do(sslobj.read, 4) |
| 1131 |
if not chunk: |
| 1132 |
break |
| 1133 |
data += chunk |
| 1134 |
self.assertEqual(data, b'SPAM') |
| 1135 |
|
| 1136 |
do(sslobj.unwrap) |
| 1137 |
sock.close() |
| 1138 |
|
| 1139 |
except Exception as ex: |
| 1140 |
self.loop.call_soon_threadsafe(fut.set_exception, ex) |
| 1141 |
sock.close() |
| 1142 |
else: |
| 1143 |
self.loop.call_soon_threadsafe(fut.set_result, None) |
| 1144 |
|
| 1145 |
client = self.tcp_client(prog) |
| 1146 |
client.start() |
| 1147 |
clients.append(client) |
| 1148 |
|
| 1149 |
await fut |
| 1150 |
|
| 1151 |
async def start_server(): |
| 1152 |
srv = await self.loop.create_server( |
| 1153 |
server_protocol_factory, |
| 1154 |
'127.0.0.1', 0, |
| 1155 |
family=socket.AF_INET, |
| 1156 |
ssl=sslctx_1) |
| 1157 |
|
| 1158 |
try: |
| 1159 |
srv_socks = srv.sockets |
| 1160 |
self.assertTrue(srv_socks) |
| 1161 |
|
| 1162 |
addr = srv_socks[0].getsockname() |
| 1163 |
|
| 1164 |
tasks = [] |
| 1165 |
for _ in range(TOTAL_CNT): |
| 1166 |
tasks.append(test_client(addr)) |
| 1167 |
|
| 1168 |
await asyncio.wait_for(asyncio.gather(*tasks), TIMEOUT) |
| 1169 |
|
| 1170 |
finally: |
| 1171 |
self.loop.call_soon(srv.close) |
| 1172 |
await srv.wait_closed() |
| 1173 |
|
| 1174 |
with self._silence_eof_received_warning(): |
| 1175 |
self.loop.run_until_complete(start_server()) |
| 1176 |
|
| 1177 |
self.assertEqual(CNT, TOTAL_CNT) |
| 1178 |
|
| 1179 |
for client in clients: |
| 1180 |
client.stop() |
| 1181 |
|
| 1182 |
def test_shutdown_cleanly(self): |
| 1183 |
CNT = 0 |
| 1184 |
TOTAL_CNT = 25 |
| 1185 |
|
| 1186 |
A_DATA = b'A' * 1024 * BUF_MULTIPLIER |
| 1187 |
|
| 1188 |
sslctx = self._create_server_ssl_context( |
| 1189 |
test_utils.ONLYCERT, test_utils.ONLYKEY) |
| 1190 |
client_sslctx = self._create_client_ssl_context() |
| 1191 |
|
| 1192 |
def server(sock): |
| 1193 |
sock.starttls( |
| 1194 |
sslctx, |
| 1195 |
server_side=True) |
| 1196 |
|
| 1197 |
data = sock.recv_all(len(A_DATA)) |
| 1198 |
self.assertEqual(data, A_DATA) |
| 1199 |
sock.send(b'OK') |
| 1200 |
|
| 1201 |
sock.unwrap() |
| 1202 |
|
| 1203 |
sock.close() |
| 1204 |
|
| 1205 |
async def client(addr): |
| 1206 |
reader, writer = await asyncio.open_connection( |
| 1207 |
*addr, |
| 1208 |
ssl=client_sslctx, |
| 1209 |
server_hostname='', |
| 1210 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 1211 |
|
| 1212 |
writer.write(A_DATA) |
| 1213 |
self.assertEqual(await reader.readexactly(2), b'OK') |
| 1214 |
|
| 1215 |
self.assertEqual(await reader.read(), b'') |
| 1216 |
|
| 1217 |
nonlocal CNT |
| 1218 |
CNT += 1 |
| 1219 |
|
| 1220 |
writer.close() |
| 1221 |
await self.wait_closed(writer) |
| 1222 |
|
| 1223 |
def run(coro): |
| 1224 |
nonlocal CNT |
| 1225 |
CNT = 0 |
| 1226 |
|
| 1227 |
async def _gather(*tasks): |
| 1228 |
return await asyncio.gather(*tasks) |
| 1229 |
|
| 1230 |
with self.tcp_server(server, |
| 1231 |
max_clients=TOTAL_CNT, |
| 1232 |
backlog=TOTAL_CNT) as srv: |
| 1233 |
tasks = [] |
| 1234 |
for _ in range(TOTAL_CNT): |
| 1235 |
tasks.append(coro(srv.addr)) |
| 1236 |
|
| 1237 |
self.loop.run_until_complete( |
| 1238 |
_gather(*tasks)) |
| 1239 |
|
| 1240 |
self.assertEqual(CNT, TOTAL_CNT) |
| 1241 |
|
| 1242 |
with self._silence_eof_received_warning(): |
| 1243 |
run(client) |
| 1244 |
|
| 1245 |
def test_flush_before_shutdown(self): |
| 1246 |
CHUNK = 1024 * 128 |
| 1247 |
SIZE = 32 |
| 1248 |
|
| 1249 |
sslctx = self._create_server_ssl_context( |
| 1250 |
test_utils.ONLYCERT, test_utils.ONLYKEY) |
| 1251 |
client_sslctx = self._create_client_ssl_context() |
| 1252 |
|
| 1253 |
future = None |
| 1254 |
|
| 1255 |
def server(sock): |
| 1256 |
sock.starttls(sslctx, server_side=True) |
| 1257 |
self.assertEqual(sock.recv_all(4), b'ping') |
| 1258 |
sock.send(b'pong') |
| 1259 |
time.sleep(0.5) # hopefully stuck the TCP buffer |
| 1260 |
data = sock.recv_all(CHUNK * SIZE) |
| 1261 |
self.assertEqual(len(data), CHUNK * SIZE) |
| 1262 |
sock.close() |
| 1263 |
|
| 1264 |
def run(meth): |
| 1265 |
def wrapper(sock): |
| 1266 |
try: |
| 1267 |
meth(sock) |
| 1268 |
except Exception as ex: |
| 1269 |
self.loop.call_soon_threadsafe(future.set_exception, ex) |
| 1270 |
else: |
| 1271 |
self.loop.call_soon_threadsafe(future.set_result, None) |
| 1272 |
return wrapper |
| 1273 |
|
| 1274 |
async def client(addr): |
| 1275 |
nonlocal future |
| 1276 |
future = self.loop.create_future() |
| 1277 |
reader, writer = await asyncio.open_connection( |
| 1278 |
*addr, |
| 1279 |
ssl=client_sslctx, |
| 1280 |
server_hostname='', |
| 1281 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 1282 |
sslprotocol = writer.transport._ssl_protocol |
| 1283 |
writer.write(b'ping') |
| 1284 |
data = await reader.readexactly(4) |
| 1285 |
self.assertEqual(data, b'pong') |
| 1286 |
|
| 1287 |
sslprotocol.pause_writing() |
| 1288 |
for _ in range(SIZE): |
| 1289 |
writer.write(b'x' * CHUNK) |
| 1290 |
|
| 1291 |
writer.close() |
| 1292 |
sslprotocol.resume_writing() |
| 1293 |
|
| 1294 |
await self.wait_closed(writer) |
| 1295 |
try: |
| 1296 |
data = await reader.read() |
| 1297 |
self.assertEqual(data, b'') |
| 1298 |
except ConnectionResetError: |
| 1299 |
pass |
| 1300 |
await future |
| 1301 |
|
| 1302 |
with self.tcp_server(run(server)) as srv: |
| 1303 |
self.loop.run_until_complete(client(srv.addr)) |
| 1304 |
|
| 1305 |
def test_remote_shutdown_receives_trailing_data(self): |
| 1306 |
CHUNK = 1024 * 128 |
| 1307 |
SIZE = 32 |
| 1308 |
|
| 1309 |
sslctx = self._create_server_ssl_context( |
| 1310 |
test_utils.ONLYCERT, |
| 1311 |
test_utils.ONLYKEY |
| 1312 |
) |
| 1313 |
client_sslctx = self._create_client_ssl_context() |
| 1314 |
future = None |
| 1315 |
|
| 1316 |
def server(sock): |
| 1317 |
incoming = ssl.MemoryBIO() |
| 1318 |
outgoing = ssl.MemoryBIO() |
| 1319 |
sslobj = sslctx.wrap_bio(incoming, outgoing, server_side=True) |
| 1320 |
|
| 1321 |
while True: |
| 1322 |
try: |
| 1323 |
sslobj.do_handshake() |
| 1324 |
except ssl.SSLWantReadError: |
| 1325 |
if outgoing.pending: |
| 1326 |
sock.send(outgoing.read()) |
| 1327 |
incoming.write(sock.recv(16384)) |
| 1328 |
else: |
| 1329 |
if outgoing.pending: |
| 1330 |
sock.send(outgoing.read()) |
| 1331 |
break |
| 1332 |
|
| 1333 |
while True: |
| 1334 |
try: |
| 1335 |
data = sslobj.read(4) |
| 1336 |
except ssl.SSLWantReadError: |
| 1337 |
incoming.write(sock.recv(16384)) |
| 1338 |
else: |
| 1339 |
break |
| 1340 |
|
| 1341 |
self.assertEqual(data, b'ping') |
| 1342 |
sslobj.write(b'pong') |
| 1343 |
sock.send(outgoing.read()) |
| 1344 |
|
| 1345 |
time.sleep(0.2) # wait for the peer to fill its backlog |
| 1346 |
|
| 1347 |
# send close_notify but don't wait for response |
| 1348 |
with self.assertRaises(ssl.SSLWantReadError): |
| 1349 |
sslobj.unwrap() |
| 1350 |
sock.send(outgoing.read()) |
| 1351 |
|
| 1352 |
# should receive all data |
| 1353 |
data_len = 0 |
| 1354 |
while True: |
| 1355 |
try: |
| 1356 |
chunk = len(sslobj.read(16384)) |
| 1357 |
data_len += chunk |
| 1358 |
except ssl.SSLWantReadError: |
| 1359 |
incoming.write(sock.recv(16384)) |
| 1360 |
except ssl.SSLZeroReturnError: |
| 1361 |
break |
| 1362 |
|
| 1363 |
self.assertEqual(data_len, CHUNK * SIZE) |
| 1364 |
|
| 1365 |
# verify that close_notify is received |
| 1366 |
sslobj.unwrap() |
| 1367 |
|
| 1368 |
sock.close() |
| 1369 |
|
| 1370 |
def eof_server(sock): |
| 1371 |
sock.starttls(sslctx, server_side=True) |
| 1372 |
self.assertEqual(sock.recv_all(4), b'ping') |
| 1373 |
sock.send(b'pong') |
| 1374 |
|
| 1375 |
time.sleep(0.2) # wait for the peer to fill its backlog |
| 1376 |
|
| 1377 |
# send EOF |
| 1378 |
sock.shutdown(socket.SHUT_WR) |
| 1379 |
|
| 1380 |
# should receive all data |
| 1381 |
data = sock.recv_all(CHUNK * SIZE) |
| 1382 |
self.assertEqual(len(data), CHUNK * SIZE) |
| 1383 |
|
| 1384 |
sock.close() |
| 1385 |
|
| 1386 |
async def client(addr): |
| 1387 |
nonlocal future |
| 1388 |
future = self.loop.create_future() |
| 1389 |
|
| 1390 |
reader, writer = await asyncio.open_connection( |
| 1391 |
*addr, |
| 1392 |
ssl=client_sslctx, |
| 1393 |
server_hostname='', |
| 1394 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 1395 |
writer.write(b'ping') |
| 1396 |
data = await reader.readexactly(4) |
| 1397 |
self.assertEqual(data, b'pong') |
| 1398 |
|
| 1399 |
# fill write backlog in a hacky way - renegotiation won't help |
| 1400 |
for _ in range(SIZE): |
| 1401 |
writer.transport._test__append_write_backlog(b'x' * CHUNK) |
| 1402 |
|
| 1403 |
try: |
| 1404 |
data = await reader.read() |
| 1405 |
self.assertEqual(data, b'') |
| 1406 |
except (BrokenPipeError, ConnectionResetError): |
| 1407 |
pass |
| 1408 |
|
| 1409 |
await future |
| 1410 |
|
| 1411 |
writer.close() |
| 1412 |
await self.wait_closed(writer) |
| 1413 |
|
| 1414 |
def run(meth): |
| 1415 |
def wrapper(sock): |
| 1416 |
try: |
| 1417 |
meth(sock) |
| 1418 |
except Exception as ex: |
| 1419 |
self.loop.call_soon_threadsafe(future.set_exception, ex) |
| 1420 |
else: |
| 1421 |
self.loop.call_soon_threadsafe(future.set_result, None) |
| 1422 |
return wrapper |
| 1423 |
|
| 1424 |
with self.tcp_server(run(server)) as srv: |
| 1425 |
self.loop.run_until_complete(client(srv.addr)) |
| 1426 |
|
| 1427 |
with self.tcp_server(run(eof_server)) as srv: |
| 1428 |
self.loop.run_until_complete(client(srv.addr)) |
| 1429 |
|
| 1430 |
def test_remote_shutdown_receives_trailing_data_on_slow_socket(self): |
| 1431 |
# This test is the same as test_remote_shutdown_receives_trailing_data, |
| 1432 |
# except it simulates a socket that is not able to write data in time, |
| 1433 |
# thus triggering different code path in _SelectorSocketTransport. |
| 1434 |
# This triggers bug gh-115514, also tested using mocks in |
| 1435 |
# test.test_asyncio.test_selector_events.SelectorSocketTransportTests.test_write_buffer_after_close |
| 1436 |
# The slow path is triggered here by setting SO_SNDBUF, see code and comment below. |
| 1437 |
|
| 1438 |
CHUNK = 1024 * 128 |
| 1439 |
SIZE = 32 |
| 1440 |
|
| 1441 |
sslctx = self._create_server_ssl_context( |
| 1442 |
test_utils.ONLYCERT, |
| 1443 |
test_utils.ONLYKEY |
| 1444 |
) |
| 1445 |
client_sslctx = self._create_client_ssl_context() |
| 1446 |
future = None |
| 1447 |
|
| 1448 |
def server(sock): |
| 1449 |
incoming = ssl.MemoryBIO() |
| 1450 |
outgoing = ssl.MemoryBIO() |
| 1451 |
sslobj = sslctx.wrap_bio(incoming, outgoing, server_side=True) |
| 1452 |
|
| 1453 |
while True: |
| 1454 |
try: |
| 1455 |
sslobj.do_handshake() |
| 1456 |
except ssl.SSLWantReadError: |
| 1457 |
if outgoing.pending: |
| 1458 |
sock.send(outgoing.read()) |
| 1459 |
incoming.write(sock.recv(16384)) |
| 1460 |
else: |
| 1461 |
if outgoing.pending: |
| 1462 |
sock.send(outgoing.read()) |
| 1463 |
break |
| 1464 |
|
| 1465 |
while True: |
| 1466 |
try: |
| 1467 |
data = sslobj.read(4) |
| 1468 |
except ssl.SSLWantReadError: |
| 1469 |
incoming.write(sock.recv(16384)) |
| 1470 |
else: |
| 1471 |
break |
| 1472 |
|
| 1473 |
self.assertEqual(data, b'ping') |
| 1474 |
sslobj.write(b'pong') |
| 1475 |
sock.send(outgoing.read()) |
| 1476 |
|
| 1477 |
time.sleep(0.2) # wait for the peer to fill its backlog |
| 1478 |
|
| 1479 |
# send close_notify but don't wait for response |
| 1480 |
with self.assertRaises(ssl.SSLWantReadError): |
| 1481 |
sslobj.unwrap() |
| 1482 |
sock.send(outgoing.read()) |
| 1483 |
|
| 1484 |
# should receive all data |
| 1485 |
data_len = 0 |
| 1486 |
while True: |
| 1487 |
try: |
| 1488 |
chunk = len(sslobj.read(16384)) |
| 1489 |
data_len += chunk |
| 1490 |
except ssl.SSLWantReadError: |
| 1491 |
incoming.write(sock.recv(16384)) |
| 1492 |
except ssl.SSLZeroReturnError: |
| 1493 |
break |
| 1494 |
|
| 1495 |
self.assertEqual(data_len, CHUNK * SIZE*2) |
| 1496 |
|
| 1497 |
# verify that close_notify is received |
| 1498 |
sslobj.unwrap() |
| 1499 |
|
| 1500 |
sock.close() |
| 1501 |
|
| 1502 |
def eof_server(sock): |
| 1503 |
sock.starttls(sslctx, server_side=True) |
| 1504 |
self.assertEqual(sock.recv_all(4), b'ping') |
| 1505 |
sock.send(b'pong') |
| 1506 |
|
| 1507 |
time.sleep(0.2) # wait for the peer to fill its backlog |
| 1508 |
|
| 1509 |
# send EOF |
| 1510 |
sock.shutdown(socket.SHUT_WR) |
| 1511 |
|
| 1512 |
# should receive all data |
| 1513 |
data = sock.recv_all(CHUNK * SIZE) |
| 1514 |
self.assertEqual(len(data), CHUNK * SIZE) |
| 1515 |
|
| 1516 |
sock.close() |
| 1517 |
|
| 1518 |
async def client(addr): |
| 1519 |
nonlocal future |
| 1520 |
future = self.loop.create_future() |
| 1521 |
|
| 1522 |
reader, writer = await asyncio.open_connection( |
| 1523 |
*addr, |
| 1524 |
ssl=client_sslctx, |
| 1525 |
server_hostname='', |
| 1526 |
ssl_handshake_timeout=HANDSHAKE_TIMEOUT) |
| 1527 |
writer.write(b'ping') |
| 1528 |
data = await reader.readexactly(4) |
| 1529 |
self.assertEqual(data, b'pong') |
| 1530 |
|
| 1531 |
# fill write backlog in a hacky way - renegotiation won't help |
| 1532 |
for _ in range(SIZE*2): |
| 1533 |
writer.transport._test__append_write_backlog(b'x' * CHUNK) |
| 1534 |
|
| 1535 |
try: |
| 1536 |
data = await reader.read() |
| 1537 |
self.assertEqual(data, b'') |
| 1538 |
except (BrokenPipeError, ConnectionResetError): |
| 1539 |
pass |
| 1540 |
|
| 1541 |
# Make sure _SelectorSocketTransport enters the delayed write |
| 1542 |
# path in its `write` method by wrapping socket in a fake class |
| 1543 |
# that acts as if there is not enough space in socket buffer. |
| 1544 |
# This triggers bug gh-115514, also tested using mocks in |
| 1545 |
# test.test_asyncio.test_selector_events.SelectorSocketTransportTests.test_write_buffer_after_close |
| 1546 |
socket_transport = writer.transport._ssl_protocol._transport |
| 1547 |
|
| 1548 |
class SocketWrapper: |
| 1549 |
def __init__(self, sock) -> None: |
| 1550 |
self.sock = sock |
| 1551 |
|
| 1552 |
def __getattr__(self, name): |
| 1553 |
return getattr(self.sock, name) |
| 1554 |
|
| 1555 |
def send(self, data): |
| 1556 |
# Fake that our write buffer is full, send only half |
| 1557 |
to_send = len(data)//2 |
| 1558 |
return self.sock.send(data[:to_send]) |
| 1559 |
|
| 1560 |
def _fake_full_write_buffer(data): |
| 1561 |
if socket_transport._read_ready_cb is None and not isinstance(socket_transport._sock, SocketWrapper): |
| 1562 |
socket_transport._sock = SocketWrapper(socket_transport._sock) |
| 1563 |
return unittest.mock.DEFAULT |
| 1564 |
|
| 1565 |
with unittest.mock.patch.object( |
| 1566 |
socket_transport, "write", |
| 1567 |
wraps=socket_transport.write, |
| 1568 |
side_effect=_fake_full_write_buffer |
| 1569 |
): |
| 1570 |
await future |
| 1571 |
|
| 1572 |
writer.close() |
| 1573 |
await self.wait_closed(writer) |
| 1574 |
|
| 1575 |
def run(meth): |
| 1576 |
def wrapper(sock): |
| 1577 |
try: |
| 1578 |
meth(sock) |
| 1579 |
except Exception as ex: |
| 1580 |
self.loop.call_soon_threadsafe(future.set_exception, ex) |
| 1581 |
else: |
| 1582 |
self.loop.call_soon_threadsafe(future.set_result, None) |
| 1583 |
return wrapper |
| 1584 |
|
| 1585 |
with self.tcp_server(run(server)) as srv: |
| 1586 |
self.loop.run_until_complete(client(srv.addr)) |
| 1587 |
|
| 1588 |
with self.tcp_server(run(eof_server)) as srv: |
| 1589 |
self.loop.run_until_complete(client(srv.addr)) |
| 1590 |
|
| 1591 |
def test_connect_timeout_warning(self): |
| 1592 |
s = socket.socket(socket.AF_INET) |
| 1593 |
s.bind(('127.0.0.1', 0)) |
| 1594 |
addr = s.getsockname() |
| 1595 |
|
| 1596 |
async def test(): |
| 1597 |
try: |
| 1598 |
await asyncio.wait_for( |
| 1599 |
self.loop.create_connection(asyncio.Protocol, |
| 1600 |
*addr, ssl=True), |
| 1601 |
0.1) |
| 1602 |
except (ConnectionRefusedError, asyncio.TimeoutError): |
| 1603 |
pass |
| 1604 |
else: |
| 1605 |
self.fail('TimeoutError is not raised') |
| 1606 |
|
| 1607 |
with s: |
| 1608 |
try: |
| 1609 |
with self.assertWarns(ResourceWarning) as cm: |
| 1610 |
self.loop.run_until_complete(test()) |
| 1611 |
gc.collect() |
| 1612 |
gc.collect() |
| 1613 |
gc.collect() |
| 1614 |
except AssertionError as e: |
| 1615 |
self.assertEqual(str(e), 'ResourceWarning not triggered') |
| 1616 |
else: |
| 1617 |
self.fail('Unexpected ResourceWarning: {}'.format(cm.warning)) |
| 1618 |
|
| 1619 |
def test_handshake_timeout_handler_leak(self): |
| 1620 |
s = socket.socket(socket.AF_INET) |
| 1621 |
s.bind(('127.0.0.1', 0)) |
| 1622 |
s.listen(1) |
| 1623 |
addr = s.getsockname() |
| 1624 |
|
| 1625 |
async def test(ctx): |
| 1626 |
try: |
| 1627 |
await asyncio.wait_for( |
| 1628 |
self.loop.create_connection(asyncio.Protocol, *addr, |
| 1629 |
ssl=ctx), |
| 1630 |
0.1) |
| 1631 |
except (ConnectionRefusedError, asyncio.TimeoutError): |
| 1632 |
pass |
| 1633 |
else: |
| 1634 |
self.fail('TimeoutError is not raised') |
| 1635 |
|
| 1636 |
with s: |
| 1637 |
ctx = ssl.create_default_context() |
| 1638 |
self.loop.run_until_complete(test(ctx)) |
| 1639 |
ctx = weakref.ref(ctx) |
| 1640 |
|
| 1641 |
# SSLProtocol should be DECREF to 0 |
| 1642 |
self.assertIsNone(ctx()) |
| 1643 |
|
| 1644 |
def test_shutdown_timeout_handler_leak(self): |
| 1645 |
loop = self.loop |
| 1646 |
|
| 1647 |
def server(sock): |
| 1648 |
sslctx = self._create_server_ssl_context( |
| 1649 |
test_utils.ONLYCERT, |
| 1650 |
test_utils.ONLYKEY |
| 1651 |
) |
| 1652 |
sock = sslctx.wrap_socket(sock, server_side=True) |
| 1653 |
sock.recv(32) |
| 1654 |
sock.close() |
| 1655 |
|
| 1656 |
class Protocol(asyncio.Protocol): |
| 1657 |
def __init__(self): |
| 1658 |
self.fut = asyncio.Future(loop=loop) |
| 1659 |
|
| 1660 |
def connection_lost(self, exc): |
| 1661 |
self.fut.set_result(None) |
| 1662 |
|
| 1663 |
async def client(addr, ctx): |
| 1664 |
tr, pr = await loop.create_connection(Protocol, *addr, ssl=ctx) |
| 1665 |
tr.close() |
| 1666 |
await pr.fut |
| 1667 |
|
| 1668 |
with self.tcp_server(server) as srv: |
| 1669 |
ctx = self._create_client_ssl_context() |
| 1670 |
loop.run_until_complete(client(srv.addr, ctx)) |
| 1671 |
ctx = weakref.ref(ctx) |
| 1672 |
|
| 1673 |
# asyncio has no shutdown timeout, but it ends up with a circular |
| 1674 |
# reference loop - not ideal (introduces gc glitches), but at least |
| 1675 |
# not leaking |
| 1676 |
gc.collect() |
| 1677 |
gc.collect() |
| 1678 |
gc.collect() |
| 1679 |
|
| 1680 |
# SSLProtocol should be DECREF to 0 |
| 1681 |
self.assertIsNone(ctx()) |
| 1682 |
|
| 1683 |
def test_shutdown_timeout_handler_not_set(self): |
| 1684 |
loop = self.loop |
| 1685 |
eof = asyncio.Event() |
| 1686 |
extra = None |
| 1687 |
|
| 1688 |
def server(sock): |
| 1689 |
sslctx = self._create_server_ssl_context( |
| 1690 |
test_utils.ONLYCERT, |
| 1691 |
test_utils.ONLYKEY |
| 1692 |
) |
| 1693 |
sock = sslctx.wrap_socket(sock, server_side=True) |
| 1694 |
sock.send(b'hello') |
| 1695 |
assert sock.recv(1024) == b'world' |
| 1696 |
sock.send(b'extra bytes') |
| 1697 |
# sending EOF here |
| 1698 |
sock.shutdown(socket.SHUT_WR) |
| 1699 |
loop.call_soon_threadsafe(eof.set) |
| 1700 |
# make sure we have enough time to reproduce the issue |
| 1701 |
assert sock.recv(1024) == b'' |
| 1702 |
sock.close() |
| 1703 |
|
| 1704 |
class Protocol(asyncio.Protocol): |
| 1705 |
def __init__(self): |
| 1706 |
self.fut = asyncio.Future(loop=loop) |
| 1707 |
self.transport = None |
| 1708 |
|
| 1709 |
def connection_made(self, transport): |
| 1710 |
self.transport = transport |
| 1711 |
|
| 1712 |
def data_received(self, data): |
| 1713 |
if data == b'hello': |
| 1714 |
self.transport.write(b'world') |
| 1715 |
# pause reading would make incoming data stay in the sslobj |
| 1716 |
self.transport.pause_reading() |
| 1717 |
else: |
| 1718 |
nonlocal extra |
| 1719 |
extra = data |
| 1720 |
|
| 1721 |
def connection_lost(self, exc): |
| 1722 |
if exc is None: |
| 1723 |
self.fut.set_result(None) |
| 1724 |
else: |
| 1725 |
self.fut.set_exception(exc) |
| 1726 |
|
| 1727 |
async def client(addr): |
| 1728 |
ctx = self._create_client_ssl_context() |
| 1729 |
tr, pr = await loop.create_connection(Protocol, *addr, ssl=ctx) |
| 1730 |
await eof.wait() |
| 1731 |
tr.resume_reading() |
| 1732 |
await pr.fut |
| 1733 |
tr.close() |
| 1734 |
assert extra == b'extra bytes' |
| 1735 |
|
| 1736 |
with self.tcp_server(server) as srv: |
| 1737 |
loop.run_until_complete(client(srv.addr)) |
| 1738 |
|
| 1739 |
|
| 1740 |
############################################################################### |
| 1741 |
# Socket Testing Utilities |
| 1742 |
############################################################################### |
| 1743 |
|
| 1744 |
|
| 1745 |
class TestSocketWrapper: |
| 1746 |
|
| 1747 |
def __init__(self, sock): |
| 1748 |
self.__sock = sock |
| 1749 |
|
| 1750 |
def recv_all(self, n): |
| 1751 |
buf = b'' |
| 1752 |
while len(buf) < n: |
| 1753 |
data = self.recv(n - len(buf)) |
| 1754 |
if data == b'': |
| 1755 |
raise ConnectionAbortedError |
| 1756 |
buf += data |
| 1757 |
return buf |
| 1758 |
|
| 1759 |
def starttls(self, ssl_context, *, |
| 1760 |
server_side=False, |
| 1761 |
server_hostname=None, |
| 1762 |
do_handshake_on_connect=True): |
| 1763 |
|
| 1764 |
assert isinstance(ssl_context, ssl.SSLContext) |
| 1765 |
|
| 1766 |
ssl_sock = ssl_context.wrap_socket( |
| 1767 |
self.__sock, server_side=server_side, |
| 1768 |
server_hostname=server_hostname, |
| 1769 |
do_handshake_on_connect=do_handshake_on_connect) |
| 1770 |
|
| 1771 |
if server_side: |
| 1772 |
ssl_sock.do_handshake() |
| 1773 |
|
| 1774 |
self.__sock.close() |
| 1775 |
self.__sock = ssl_sock |
| 1776 |
|
| 1777 |
def __getattr__(self, name): |
| 1778 |
return getattr(self.__sock, name) |
| 1779 |
|
| 1780 |
def __repr__(self): |
| 1781 |
return '<{} {!r}>'.format(type(self).__name__, self.__sock) |
| 1782 |
|
| 1783 |
|
| 1784 |
class SocketThread(threading.Thread): |
| 1785 |
|
| 1786 |
def stop(self): |
| 1787 |
self._active = False |
| 1788 |
self.join() |
| 1789 |
|
| 1790 |
def __enter__(self): |
| 1791 |
self.start() |
| 1792 |
return self |
| 1793 |
|
| 1794 |
def __exit__(self, *exc): |
| 1795 |
self.stop() |
| 1796 |
|
| 1797 |
|
| 1798 |
class TestThreadedClient(SocketThread): |
| 1799 |
|
| 1800 |
def __init__(self, test, sock, prog, timeout): |
| 1801 |
threading.Thread.__init__(self, None, None, 'test-client') |
| 1802 |
self.daemon = True |
| 1803 |
|
| 1804 |
self._timeout = timeout |
| 1805 |
self._sock = sock |
| 1806 |
self._active = True |
| 1807 |
self._prog = prog |
| 1808 |
self._test = test |
| 1809 |
|
| 1810 |
def run(self): |
| 1811 |
try: |
| 1812 |
self._prog(TestSocketWrapper(self._sock)) |
| 1813 |
except (KeyboardInterrupt, SystemExit): |
| 1814 |
raise |
| 1815 |
except BaseException as ex: |
| 1816 |
self._test._abort_socket_test(ex) |
| 1817 |
|
| 1818 |
|
| 1819 |
class TestThreadedServer(SocketThread): |
| 1820 |
|
| 1821 |
def __init__(self, test, sock, prog, timeout, max_clients): |
| 1822 |
threading.Thread.__init__(self, None, None, 'test-server') |
| 1823 |
self.daemon = True |
| 1824 |
|
| 1825 |
self._clients = 0 |
| 1826 |
self._finished_clients = 0 |
| 1827 |
self._max_clients = max_clients |
| 1828 |
self._timeout = timeout |
| 1829 |
self._sock = sock |
| 1830 |
self._active = True |
| 1831 |
|
| 1832 |
self._prog = prog |
| 1833 |
|
| 1834 |
self._s1, self._s2 = socket.socketpair() |
| 1835 |
self._s1.setblocking(False) |
| 1836 |
|
| 1837 |
self._test = test |
| 1838 |
|
| 1839 |
def stop(self): |
| 1840 |
try: |
| 1841 |
if self._s2 and self._s2.fileno() != -1: |
| 1842 |
try: |
| 1843 |
self._s2.send(b'stop') |
| 1844 |
except OSError: |
| 1845 |
pass |
| 1846 |
finally: |
| 1847 |
super().stop() |
| 1848 |
self._sock.close() |
| 1849 |
self._s1.close() |
| 1850 |
self._s2.close() |
| 1851 |
|
| 1852 |
def run(self): |
| 1853 |
self._sock.setblocking(False) |
| 1854 |
self._run() |
| 1855 |
|
| 1856 |
def _run(self): |
| 1857 |
while self._active: |
| 1858 |
if self._clients >= self._max_clients: |
| 1859 |
return |
| 1860 |
|
| 1861 |
r, w, x = select.select( |
| 1862 |
[self._sock, self._s1], [], [], self._timeout) |
| 1863 |
|
| 1864 |
if self._s1 in r: |
| 1865 |
return |
| 1866 |
|
| 1867 |
if self._sock in r: |
| 1868 |
try: |
| 1869 |
conn, addr = self._sock.accept() |
| 1870 |
except BlockingIOError: |
| 1871 |
continue |
| 1872 |
except socket.timeout: |
| 1873 |
if not self._active: |
| 1874 |
return |
| 1875 |
else: |
| 1876 |
raise |
| 1877 |
else: |
| 1878 |
self._clients += 1 |
| 1879 |
conn.settimeout(self._timeout) |
| 1880 |
try: |
| 1881 |
with conn: |
| 1882 |
self._handle_client(conn) |
| 1883 |
except (KeyboardInterrupt, SystemExit): |
| 1884 |
raise |
| 1885 |
except BaseException as ex: |
| 1886 |
self._active = False |
| 1887 |
try: |
| 1888 |
raise |
| 1889 |
finally: |
| 1890 |
self._test._abort_socket_test(ex) |
| 1891 |
|
| 1892 |
def _handle_client(self, sock): |
| 1893 |
self._prog(TestSocketWrapper(sock)) |
| 1894 |
|
| 1895 |
@property |
| 1896 |
def addr(self): |
| 1897 |
return self._sock.getsockname() |