@@ -318,6 +318,16 @@ async def test():
318318
319319 self .loop .run_until_complete (test ())
320320
321+ def test_create_server_8 (self ):
322+ if self .implementation == 'asyncio' and not self .PY37 :
323+ raise unittest .SkipTest ()
324+
325+ with self .assertRaisesRegex (
326+ ValueError , 'ssl_handshake_timeout is only meaningful' ):
327+ self .loop .run_until_complete (
328+ self .loop .create_server (
329+ lambda : None , host = '::' , port = 0 , ssl_handshake_timeout = 10 ))
330+
321331 def test_create_connection_open_con_addr (self ):
322332 async def client (addr ):
323333 reader , writer = await asyncio .open_connection (
@@ -501,6 +511,16 @@ async def client(addr):
501511 backlog = 1 ) as srv :
502512 self .loop .run_until_complete (client (srv .addr ))
503513
514+ def test_create_connection_6 (self ):
515+ if self .implementation == 'asyncio' and not self .PY37 :
516+ raise unittest .SkipTest ()
517+
518+ with self .assertRaisesRegex (
519+ ValueError , 'ssl_handshake_timeout is only meaningful' ):
520+ self .loop .run_until_complete (
521+ self .loop .create_connection (
522+ lambda : None , host = '::' , port = 0 , ssl_handshake_timeout = 10 ))
523+
504524 def test_transport_shutdown (self ):
505525 CNT = 0 # number of clients that were successful
506526 TOTAL_CNT = 100 # total number of clients that test will create
@@ -855,6 +875,17 @@ async def runner():
855875 srv .close ()
856876 self .loop .run_until_complete (srv .wait_closed ())
857877
878+ def test_connect_accepted_socket_ssl_args (self ):
879+ if self .implementation == 'asyncio' and not self .PY37 :
880+ raise unittest .SkipTest ()
881+
882+ with self .assertRaisesRegex (
883+ ValueError , 'ssl_handshake_timeout is only meaningful' ):
884+ with socket .socket () as s :
885+ self .loop .run_until_complete (
886+ self .loop .connect_accepted_socket (
887+ (lambda : None ), s , ssl_handshake_timeout = 10.0 ))
888+
858889 def test_connect_accepted_socket (self , server_ssl = None , client_ssl = None ):
859890 loop = self .loop
860891
@@ -898,9 +929,15 @@ def client():
898929 conn , _ = lsock .accept ()
899930 proto = MyProto (loop = loop )
900931 proto .loop = loop
932+
933+ extras = {}
934+ if server_ssl and (self .implementation != 'asyncio' or self .PY37 ):
935+ extras = dict (ssl_handshake_timeout = 10.0 )
936+
901937 f = loop .create_task (
902938 loop .connect_accepted_socket (
903- (lambda : proto ), conn , ssl = server_ssl ))
939+ (lambda : proto ), conn , ssl = server_ssl ,
940+ ** extras ))
904941 loop .run_forever ()
905942 conn .close ()
906943 lsock .close ()
@@ -1017,12 +1054,17 @@ def prog(sock):
10171054 await fut
10181055
10191056 async def start_server ():
1057+ extras = {}
1058+ if self .implementation != 'asyncio' or self .PY37 :
1059+ extras = dict (ssl_handshake_timeout = 10.0 )
1060+
10201061 srv = await asyncio .start_server (
10211062 handle_client ,
10221063 '127.0.0.1' , 0 ,
10231064 family = socket .AF_INET ,
10241065 ssl = sslctx ,
1025- loop = self .loop )
1066+ loop = self .loop ,
1067+ ** extras )
10261068
10271069 try :
10281070 srv_socks = srv .sockets
@@ -1080,11 +1122,16 @@ def server(sock):
10801122 sock .close ()
10811123
10821124 async def client (addr ):
1125+ extras = {}
1126+ if self .implementation != 'asyncio' or self .PY37 :
1127+ extras = dict (ssl_handshake_timeout = 10.0 )
1128+
10831129 reader , writer = await asyncio .open_connection (
10841130 * addr ,
10851131 ssl = client_sslctx ,
10861132 server_hostname = '' ,
1087- loop = self .loop )
1133+ loop = self .loop ,
1134+ ** extras )
10881135
10891136 writer .write (A_DATA )
10901137 self .assertEqual (await reader .readexactly (2 ), b'OK' )
@@ -1140,6 +1187,77 @@ def run(coro):
11401187 with self ._silence_eof_received_warning ():
11411188 run (client_sock )
11421189
1190+ def test_create_connection_ssl_slow_handshake (self ):
1191+ if self .implementation == 'asyncio' :
1192+ raise unittest .SkipTest ()
1193+
1194+ client_sslctx = self ._create_client_ssl_context ()
1195+
1196+ # silence error logger
1197+ self .loop .set_exception_handler (lambda * args : None )
1198+
1199+ def server (sock ):
1200+ try :
1201+ sock .recv_all (1024 * 1024 )
1202+ except ConnectionAbortedError :
1203+ pass
1204+ finally :
1205+ sock .close ()
1206+
1207+ async def client (addr ):
1208+ reader , writer = await asyncio .open_connection (
1209+ * addr ,
1210+ ssl = client_sslctx ,
1211+ server_hostname = '' ,
1212+ loop = self .loop ,
1213+ ssl_handshake_timeout = 1.0 )
1214+
1215+ with self .tcp_server (server ,
1216+ max_clients = 1 ,
1217+ backlog = 1 ) as srv :
1218+
1219+ with self .assertRaisesRegex (
1220+ ConnectionAbortedError ,
1221+ r'SSL handshake.*is taking longer' ):
1222+
1223+ self .loop .run_until_complete (client (srv .addr ))
1224+
1225+ def test_create_connection_ssl_failed_certificate (self ):
1226+ if self .implementation == 'asyncio' :
1227+ raise unittest .SkipTest ()
1228+
1229+ # silence error logger
1230+ self .loop .set_exception_handler (lambda * args : None )
1231+
1232+ sslctx = self ._create_server_ssl_context (self .ONLYCERT , self .ONLYKEY )
1233+ client_sslctx = self ._create_client_ssl_context (disable_verify = False )
1234+
1235+ def server (sock ):
1236+ try :
1237+ sock .starttls (
1238+ sslctx ,
1239+ server_side = True )
1240+ sock .connect ()
1241+ except ssl .SSLError :
1242+ pass
1243+ finally :
1244+ sock .close ()
1245+
1246+ async def client (addr ):
1247+ reader , writer = await asyncio .open_connection (
1248+ * addr ,
1249+ ssl = client_sslctx ,
1250+ server_hostname = '' ,
1251+ loop = self .loop ,
1252+ ssl_handshake_timeout = 1.0 )
1253+
1254+ with self .tcp_server (server ,
1255+ max_clients = 1 ,
1256+ backlog = 1 ) as srv :
1257+
1258+ with self .assertRaises (ssl .SSLCertVerificationError ):
1259+ self .loop .run_until_complete (client (srv .addr ))
1260+
11431261 def test_ssl_connect_accepted_socket (self ):
11441262 server_context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
11451263 server_context .load_cert_chain (self .ONLYCERT , self .ONLYKEY )
0 commit comments