12
12
import select
13
13
import sys
14
14
import time
15
+ import typing
15
16
import uuid
16
17
from errno import (
17
18
EAFNOSUPPORT ,
156
157
"""
157
158
158
159
159
- def socket_any_family ():
160
+ def socket_any_family () -> socket :
160
161
try :
161
162
return socket (AF_INET )
162
163
except OSError as e :
@@ -165,7 +166,7 @@ def socket_any_family():
165
166
raise
166
167
167
168
168
- def loopback_address (socket ) :
169
+ def loopback_address (socket : socket ) -> str :
169
170
if socket .family == AF_INET :
170
171
return "127.0.0.1"
171
172
else :
@@ -194,7 +195,7 @@ def verify_cb(conn, cert, errnum, depth, ok):
194
195
return ok
195
196
196
197
197
- def socket_pair ():
198
+ def socket_pair () -> tuple [ socket , socket ] :
198
199
"""
199
200
Establish and return a pair of network sockets connected to each other.
200
201
"""
@@ -225,7 +226,7 @@ def socket_pair():
225
226
return (server , client )
226
227
227
228
228
- def handshake (client , server ) :
229
+ def handshake (client : Connection , server : Connection ) -> None :
229
230
conns = [client , server ]
230
231
while conns :
231
232
for conn in conns :
@@ -322,13 +323,17 @@ def _create_certificate_chain():
322
323
]
323
324
324
325
325
- def loopback_client_factory (socket , version = SSLv23_METHOD ):
326
+ def loopback_client_factory (
327
+ socket : socket , version : int = SSLv23_METHOD
328
+ ) -> Connection :
326
329
client = Connection (Context (version ), socket )
327
330
client .set_connect_state ()
328
331
return client
329
332
330
333
331
- def loopback_server_factory (socket , version = SSLv23_METHOD ):
334
+ def loopback_server_factory (
335
+ socket : socket | None , version : int = SSLv23_METHOD
336
+ ) -> Connection :
332
337
ctx = Context (version )
333
338
ctx .use_privatekey (load_privatekey (FILETYPE_PEM , server_key_pem ))
334
339
ctx .use_certificate (load_certificate (FILETYPE_PEM , server_cert_pem ))
@@ -337,7 +342,10 @@ def loopback_server_factory(socket, version=SSLv23_METHOD):
337
342
return server
338
343
339
344
340
- def loopback (server_factory = None , client_factory = None ):
345
+ def loopback (
346
+ server_factory : typing .Callable [[socket ], Connection ] | None = None ,
347
+ client_factory : typing .Callable [[socket ], Connection ] | None = None ,
348
+ ) -> tuple [Connection , Connection ]:
341
349
"""
342
350
Create a connected socket pair and force two connected SSL sockets
343
351
to talk to each other via memory BIOs.
@@ -348,17 +356,19 @@ def loopback(server_factory=None, client_factory=None):
348
356
client_factory = loopback_client_factory
349
357
350
358
(server , client ) = socket_pair ()
351
- server = server_factory (server )
352
- client = client_factory (client )
359
+ tls_server = server_factory (server )
360
+ tls_client = client_factory (client )
353
361
354
- handshake (client , server )
362
+ handshake (tls_client , tls_server )
355
363
356
- server .setblocking (True )
357
- client .setblocking (True )
358
- return server , client
364
+ tls_server .setblocking (True )
365
+ tls_client .setblocking (True )
366
+ return tls_server , tls_client
359
367
360
368
361
- def interact_in_memory (client_conn , server_conn ):
369
+ def interact_in_memory (
370
+ client_conn : Connection , server_conn : Connection
371
+ ) -> None :
362
372
"""
363
373
Try to read application bytes from each of the two `Connection` objects.
364
374
Copy bytes back and forth between their send/receive buffers for as long
@@ -404,7 +414,9 @@ def interact_in_memory(client_conn, server_conn):
404
414
write .bio_write (dirty )
405
415
406
416
407
- def handshake_in_memory (client_conn , server_conn ):
417
+ def handshake_in_memory (
418
+ client_conn : Connection , server_conn : Connection
419
+ ) -> None :
408
420
"""
409
421
Perform the TLS handshake between two `Connection` instances connected to
410
422
each other via memory BIOs.
@@ -620,7 +632,7 @@ def test_method(self) -> None:
620
632
Context (meth )
621
633
622
634
with pytest .raises (TypeError ):
623
- Context ("" )
635
+ Context ("" ) # type: ignore[arg-type]
624
636
with pytest .raises (ValueError ):
625
637
Context (13 )
626
638
@@ -690,11 +702,11 @@ def test_use_certificate_file_wrong_args(self) -> None:
690
702
"""
691
703
ctx = Context (SSLv23_METHOD )
692
704
with pytest .raises (TypeError ):
693
- ctx .use_certificate_file (object (), FILETYPE_PEM )
705
+ ctx .use_certificate_file (object (), FILETYPE_PEM ) # type: ignore[arg-type]
694
706
with pytest .raises (TypeError ):
695
- ctx .use_certificate_file (b"somefile" , object ())
707
+ ctx .use_certificate_file (b"somefile" , object ()) # type: ignore[arg-type]
696
708
with pytest .raises (TypeError ):
697
- ctx .use_certificate_file (object (), FILETYPE_PEM )
709
+ ctx .use_certificate_file (object (), FILETYPE_PEM ) # type: ignore[arg-type]
698
710
699
711
def test_use_certificate_file_missing (self , tmpfile ) -> None :
700
712
"""
@@ -1070,7 +1082,7 @@ def _load_verify_locations_test(self, *args):
1070
1082
# connection will fail.
1071
1083
clientContext .set_verify (
1072
1084
VERIFY_PEER ,
1073
- lambda conn , cert , errno , depth , preverify_ok : preverify_ok ,
1085
+ lambda conn , cert , errno , depth , preverify_ok : bool ( preverify_ok ) ,
1074
1086
)
1075
1087
1076
1088
clientSSL = Connection (clientContext , client )
@@ -1094,6 +1106,7 @@ def _load_verify_locations_test(self, *args):
1094
1106
handshake (clientSSL , serverSSL )
1095
1107
1096
1108
cert = clientSSL .get_peer_certificate ()
1109
+ assert cert is not None
1097
1110
assert cert .get_subject ().CN == "Testing Root CA"
1098
1111
1099
1112
cryptography_cert = clientSSL .get_peer_certificate (
@@ -1228,6 +1241,7 @@ def test_fallback_default_verify_paths(self, monkeypatch) -> None:
1228
1241
)
1229
1242
context .set_default_verify_paths ()
1230
1243
store = context .get_cert_store ()
1244
+ assert store is not None
1231
1245
sk_obj = _lib .X509_STORE_get0_objects (store ._store )
1232
1246
assert sk_obj != _ffi .NULL
1233
1247
num = _lib .sk_X509_OBJECT_num (sk_obj )
@@ -1323,7 +1337,9 @@ def test_add_extra_chain_cert_invalid_cert(self) -> None:
1323
1337
with pytest .raises (TypeError ):
1324
1338
context .add_extra_chain_cert (object ())
1325
1339
1326
- def _handshake_test (self , serverContext , clientContext ):
1340
+ def _handshake_test (
1341
+ self , serverContext : Context , clientContext : Context
1342
+ ) -> None :
1327
1343
"""
1328
1344
Verify that a client and server created with the given contexts can
1329
1345
successfully handshake and communicate.
@@ -2691,12 +2707,14 @@ def test_get_verified_chain(self) -> None:
2691
2707
interact_in_memory (client , server )
2692
2708
2693
2709
chain = client .get_verified_chain ()
2710
+ assert chain is not None
2694
2711
assert len (chain ) == 3
2695
2712
assert "Server Certificate" == chain [0 ].get_subject ().CN
2696
2713
assert "Intermediate Certificate" == chain [1 ].get_subject ().CN
2697
2714
assert "Authority Certificate" == chain [2 ].get_subject ().CN
2698
2715
2699
2716
cryptography_chain = client .get_verified_chain (as_cryptography = True )
2717
+ assert cryptography_chain is not None
2700
2718
assert len (cryptography_chain ) == 3
2701
2719
assert (
2702
2720
cryptography_chain [0 ].subject .rfc4514_string ()
@@ -4509,7 +4527,7 @@ def pump_membio(label, source, sink):
4509
4527
sink .bio_write (chunk )
4510
4528
return True
4511
4529
4512
- def pump ():
4530
+ def pump () -> None :
4513
4531
# Raises if there was no data to pump, to avoid infinite loops if
4514
4532
# we aren't making progress.
4515
4533
assert pump_membio ("s -> c" , s , c ) or pump_membio ("c -> s" , c , s )
0 commit comments