88server_ctx = ssl .create_default_context (ssl .Purpose .CLIENT_AUTH )
99server_ctx .load_cert_chain ("trio-test-1.pem" )
1010
11+
1112def _ssl_echo_serve_sync (sock ):
1213 try :
1314 wrapped = server_ctx .wrap_socket (sock , server_side = True )
@@ -20,16 +21,19 @@ def _ssl_echo_serve_sync(sock):
2021 except BrokenPipeError :
2122 pass
2223
24+
2325@contextmanager
2426def echo_server_connection ():
2527 client_sock , server_sock = socket .socketpair ()
2628 with client_sock , server_sock :
2729 t = threading .Thread (
28- target = _ssl_echo_serve_sync , args = (server_sock ,), daemon = True )
30+ target = _ssl_echo_serve_sync , args = (server_sock ,), daemon = True
31+ )
2932 t .start ()
3033
3134 yield client_sock
3235
36+
3337class ManuallyWrappedSocket :
3438 def __init__ (self , ctx , sock , ** kwargs ):
3539 self .incoming = ssl .MemoryBIO ()
@@ -82,21 +86,23 @@ def unwrap(self):
8286def wrap_socket_via_wrap_socket (ctx , sock , ** kwargs ):
8387 return ctx .wrap_socket (sock , do_handshake_on_connect = False , ** kwargs )
8488
89+
8590def wrap_socket_via_wrap_bio (ctx , sock , ** kwargs ):
8691 return ManuallyWrappedSocket (ctx , sock , ** kwargs )
8792
8893
8994for wrap_socket in [
90- wrap_socket_via_wrap_socket ,
91- wrap_socket_via_wrap_bio ,
95+ wrap_socket_via_wrap_socket ,
96+ wrap_socket_via_wrap_bio ,
9297]:
93- print ("\n --- checking {} ---\n " . format ( wrap_socket . __name__ ) )
98+ print (f "\n --- checking { wrap_socket . __name__ } ---\n " )
9499
95100 print ("checking with do_handshake + correct hostname..." )
96101 with echo_server_connection () as client_sock :
97102 client_ctx = ssl .create_default_context (cafile = "trio-test-CA.pem" )
98103 wrapped = wrap_socket (
99- client_ctx , client_sock , server_hostname = "trio-test-1.example.org" )
104+ client_ctx , client_sock , server_hostname = "trio-test-1.example.org"
105+ )
100106 wrapped .do_handshake ()
101107 wrapped .sendall (b"x" )
102108 assert wrapped .recv (1 ) == b"x"
@@ -107,7 +113,8 @@ def wrap_socket_via_wrap_bio(ctx, sock, **kwargs):
107113 with echo_server_connection () as client_sock :
108114 client_ctx = ssl .create_default_context (cafile = "trio-test-CA.pem" )
109115 wrapped = wrap_socket (
110- client_ctx , client_sock , server_hostname = "trio-test-2.example.org" )
116+ client_ctx , client_sock , server_hostname = "trio-test-2.example.org"
117+ )
111118 try :
112119 wrapped .do_handshake ()
113120 except Exception :
@@ -119,7 +126,8 @@ def wrap_socket_via_wrap_bio(ctx, sock, **kwargs):
119126 with echo_server_connection () as client_sock :
120127 client_ctx = ssl .create_default_context (cafile = "trio-test-CA.pem" )
121128 wrapped = wrap_socket (
122- client_ctx , client_sock , server_hostname = "trio-test-2.example.org" )
129+ client_ctx , client_sock , server_hostname = "trio-test-2.example.org"
130+ )
123131 # We forgot to call do_handshake
124132 # But the hostname is wrong so something had better error out...
125133 sent = b"x"
0 commit comments