8
8
server_ctx = ssl .create_default_context (ssl .Purpose .CLIENT_AUTH )
9
9
server_ctx .load_cert_chain ("trio-test-1.pem" )
10
10
11
+
11
12
def _ssl_echo_serve_sync (sock ):
12
13
try :
13
14
wrapped = server_ctx .wrap_socket (sock , server_side = True )
@@ -20,16 +21,19 @@ def _ssl_echo_serve_sync(sock):
20
21
except BrokenPipeError :
21
22
pass
22
23
24
+
23
25
@contextmanager
24
26
def echo_server_connection ():
25
27
client_sock , server_sock = socket .socketpair ()
26
28
with client_sock , server_sock :
27
29
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
+ )
29
32
t .start ()
30
33
31
34
yield client_sock
32
35
36
+
33
37
class ManuallyWrappedSocket :
34
38
def __init__ (self , ctx , sock , ** kwargs ):
35
39
self .incoming = ssl .MemoryBIO ()
@@ -82,21 +86,23 @@ def unwrap(self):
82
86
def wrap_socket_via_wrap_socket (ctx , sock , ** kwargs ):
83
87
return ctx .wrap_socket (sock , do_handshake_on_connect = False , ** kwargs )
84
88
89
+
85
90
def wrap_socket_via_wrap_bio (ctx , sock , ** kwargs ):
86
91
return ManuallyWrappedSocket (ctx , sock , ** kwargs )
87
92
88
93
89
94
for 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 ,
92
97
]:
93
- print ("\n --- checking {} ---\n " . format ( wrap_socket . __name__ ) )
98
+ print (f "\n --- checking { wrap_socket . __name__ } ---\n " )
94
99
95
100
print ("checking with do_handshake + correct hostname..." )
96
101
with echo_server_connection () as client_sock :
97
102
client_ctx = ssl .create_default_context (cafile = "trio-test-CA.pem" )
98
103
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
+ )
100
106
wrapped .do_handshake ()
101
107
wrapped .sendall (b"x" )
102
108
assert wrapped .recv (1 ) == b"x"
@@ -107,7 +113,8 @@ def wrap_socket_via_wrap_bio(ctx, sock, **kwargs):
107
113
with echo_server_connection () as client_sock :
108
114
client_ctx = ssl .create_default_context (cafile = "trio-test-CA.pem" )
109
115
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
+ )
111
118
try :
112
119
wrapped .do_handshake ()
113
120
except Exception :
@@ -119,7 +126,8 @@ def wrap_socket_via_wrap_bio(ctx, sock, **kwargs):
119
126
with echo_server_connection () as client_sock :
120
127
client_ctx = ssl .create_default_context (cafile = "trio-test-CA.pem" )
121
128
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
+ )
123
131
# We forgot to call do_handshake
124
132
# But the hostname is wrong so something had better error out...
125
133
sent = b"x"
0 commit comments