@@ -318,6 +318,16 @@ async def test():
318
318
319
319
self .loop .run_until_complete (test ())
320
320
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
+
321
331
def test_create_connection_open_con_addr (self ):
322
332
async def client (addr ):
323
333
reader , writer = await asyncio .open_connection (
@@ -501,6 +511,16 @@ async def client(addr):
501
511
backlog = 1 ) as srv :
502
512
self .loop .run_until_complete (client (srv .addr ))
503
513
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
+
504
524
def test_transport_shutdown (self ):
505
525
CNT = 0 # number of clients that were successful
506
526
TOTAL_CNT = 100 # total number of clients that test will create
@@ -855,6 +875,17 @@ async def runner():
855
875
srv .close ()
856
876
self .loop .run_until_complete (srv .wait_closed ())
857
877
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
+
858
889
def test_connect_accepted_socket (self , server_ssl = None , client_ssl = None ):
859
890
loop = self .loop
860
891
@@ -898,9 +929,15 @@ def client():
898
929
conn , _ = lsock .accept ()
899
930
proto = MyProto (loop = loop )
900
931
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
+
901
937
f = loop .create_task (
902
938
loop .connect_accepted_socket (
903
- (lambda : proto ), conn , ssl = server_ssl ))
939
+ (lambda : proto ), conn , ssl = server_ssl ,
940
+ ** extras ))
904
941
loop .run_forever ()
905
942
conn .close ()
906
943
lsock .close ()
@@ -1017,12 +1054,17 @@ def prog(sock):
1017
1054
await fut
1018
1055
1019
1056
async def start_server ():
1057
+ extras = {}
1058
+ if self .implementation != 'asyncio' or self .PY37 :
1059
+ extras = dict (ssl_handshake_timeout = 10.0 )
1060
+
1020
1061
srv = await asyncio .start_server (
1021
1062
handle_client ,
1022
1063
'127.0.0.1' , 0 ,
1023
1064
family = socket .AF_INET ,
1024
1065
ssl = sslctx ,
1025
- loop = self .loop )
1066
+ loop = self .loop ,
1067
+ ** extras )
1026
1068
1027
1069
try :
1028
1070
srv_socks = srv .sockets
@@ -1080,11 +1122,16 @@ def server(sock):
1080
1122
sock .close ()
1081
1123
1082
1124
async def client (addr ):
1125
+ extras = {}
1126
+ if self .implementation != 'asyncio' or self .PY37 :
1127
+ extras = dict (ssl_handshake_timeout = 10.0 )
1128
+
1083
1129
reader , writer = await asyncio .open_connection (
1084
1130
* addr ,
1085
1131
ssl = client_sslctx ,
1086
1132
server_hostname = '' ,
1087
- loop = self .loop )
1133
+ loop = self .loop ,
1134
+ ** extras )
1088
1135
1089
1136
writer .write (A_DATA )
1090
1137
self .assertEqual (await reader .readexactly (2 ), b'OK' )
@@ -1140,6 +1187,77 @@ def run(coro):
1140
1187
with self ._silence_eof_received_warning ():
1141
1188
run (client_sock )
1142
1189
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
+
1143
1261
def test_ssl_connect_accepted_socket (self ):
1144
1262
server_context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
1145
1263
server_context .load_cert_chain (self .ONLYCERT , self .ONLYKEY )
0 commit comments