@@ -49,35 +49,6 @@ def mock_handshake(callback):
49
49
ssl_proto .connection_made (transport )
50
50
return transport
51
51
52
- def test_cancel_handshake (self ):
53
- # Python issue #23197: cancelling a handshake must not raise an
54
- # exception or log an error, even if the handshake failed
55
- waiter = asyncio .Future (loop = self .loop )
56
- ssl_proto = self .ssl_protocol (waiter = waiter )
57
- handshake_fut = asyncio .Future (loop = self .loop )
58
-
59
- def do_handshake (callback ):
60
- exc = Exception ()
61
- callback (exc )
62
- handshake_fut .set_result (None )
63
- return []
64
-
65
- waiter .cancel ()
66
- self .connection_made (ssl_proto , do_handshake = do_handshake )
67
-
68
- with test_utils .disable_logger ():
69
- self .loop .run_until_complete (handshake_fut )
70
-
71
- def test_handshake_timeout (self ):
72
- # bpo-29970: Check that a connection is aborted if handshake is not
73
- # completed in timeout period, instead of remaining open indefinitely
74
- ssl_proto = self .ssl_protocol ()
75
- transport = self .connection_made (ssl_proto )
76
-
77
- with test_utils .disable_logger ():
78
- self .loop .run_until_complete (tasks .sleep (0.2 , loop = self .loop ))
79
- self .assertTrue (transport .abort .called )
80
-
81
52
def test_handshake_timeout_zero (self ):
82
53
sslcontext = test_utils .dummy_ssl_context ()
83
54
app_proto = mock .Mock ()
@@ -477,6 +448,116 @@ async def main():
477
448
478
449
self .loop .run_until_complete (main ())
479
450
451
+ def test_handshake_timeout (self ):
452
+ # bpo-29970: Check that a connection is aborted if handshake is not
453
+ # completed in timeout period, instead of remaining open indefinitely
454
+ client_sslctx = test_utils .simple_client_sslcontext ()
455
+
456
+ # silence error logger
457
+ messages = []
458
+ self .loop .set_exception_handler (lambda loop , ctx : messages .append (ctx ))
459
+
460
+ server_side_aborted = False
461
+
462
+ def server (sock ):
463
+ nonlocal server_side_aborted
464
+ try :
465
+ sock .recv_all (1024 * 1024 )
466
+ except ConnectionAbortedError :
467
+ server_side_aborted = True
468
+ finally :
469
+ sock .close ()
470
+
471
+ async def client (addr ):
472
+ await asyncio .wait_for (
473
+ self .loop .create_connection (
474
+ asyncio .Protocol ,
475
+ * addr ,
476
+ ssl = client_sslctx ,
477
+ server_hostname = '' ,
478
+ ssl_handshake_timeout = 10.0 ),
479
+ 0.5 ,
480
+ loop = self .loop )
481
+
482
+ with self .tcp_server (server ,
483
+ max_clients = 1 ,
484
+ backlog = 1 ) as srv :
485
+
486
+ with self .assertRaises (asyncio .TimeoutError ):
487
+ self .loop .run_until_complete (client (srv .addr ))
488
+
489
+ self .assertTrue (server_side_aborted )
490
+
491
+ # Python issue #23197: cancelling a handshake must not raise an
492
+ # exception or log an error, even if the handshake failed
493
+ self .assertEqual (messages , [])
494
+
495
+ def test_create_connection_ssl_slow_handshake (self ):
496
+ client_sslctx = test_utils .simple_client_sslcontext ()
497
+
498
+ # silence error logger
499
+ self .loop .set_exception_handler (lambda * args : None )
500
+
501
+ def server (sock ):
502
+ try :
503
+ sock .recv_all (1024 * 1024 )
504
+ except ConnectionAbortedError :
505
+ pass
506
+ finally :
507
+ sock .close ()
508
+
509
+ async def client (addr ):
510
+ reader , writer = await asyncio .open_connection (
511
+ * addr ,
512
+ ssl = client_sslctx ,
513
+ server_hostname = '' ,
514
+ loop = self .loop ,
515
+ ssl_handshake_timeout = 1.0 )
516
+
517
+ with self .tcp_server (server ,
518
+ max_clients = 1 ,
519
+ backlog = 1 ) as srv :
520
+
521
+ with self .assertRaisesRegex (
522
+ ConnectionAbortedError ,
523
+ r'SSL handshake.*is taking longer' ):
524
+
525
+ self .loop .run_until_complete (client (srv .addr ))
526
+
527
+ def test_create_connection_ssl_failed_certificate (self ):
528
+ # silence error logger
529
+ self .loop .set_exception_handler (lambda * args : None )
530
+
531
+ sslctx = test_utils .simple_server_sslcontext ()
532
+ client_sslctx = test_utils .simple_client_sslcontext (
533
+ disable_verify = False )
534
+
535
+ def server (sock ):
536
+ try :
537
+ sock .start_tls (
538
+ sslctx ,
539
+ server_side = True )
540
+ sock .connect ()
541
+ except ssl .SSLError :
542
+ pass
543
+ finally :
544
+ sock .close ()
545
+
546
+ async def client (addr ):
547
+ reader , writer = await asyncio .open_connection (
548
+ * addr ,
549
+ ssl = client_sslctx ,
550
+ server_hostname = '' ,
551
+ loop = self .loop ,
552
+ ssl_handshake_timeout = 1.0 )
553
+
554
+ with self .tcp_server (server ,
555
+ max_clients = 1 ,
556
+ backlog = 1 ) as srv :
557
+
558
+ with self .assertRaises (ssl .SSLCertVerificationError ):
559
+ self .loop .run_until_complete (client (srv .addr ))
560
+
480
561
481
562
@unittest .skipIf (ssl is None , 'No ssl module' )
482
563
class SelectorStartTLSTests (BaseStartTLS , unittest .TestCase ):
0 commit comments