@@ -2338,7 +2338,7 @@ def test_bio_write(self) -> None:
2338
2338
connection .bio_write (b"xy" )
2339
2339
connection .bio_write (bytearray (b"za" ))
2340
2340
with pytest .warns (DeprecationWarning ):
2341
- connection .bio_write ("deprecated" )
2341
+ connection .bio_write ("deprecated" ) # type: ignore[arg-type]
2342
2342
2343
2343
def test_get_context (self ) -> None :
2344
2344
"""
@@ -2357,11 +2357,11 @@ def test_set_context_wrong_args(self) -> None:
2357
2357
ctx = Context (SSLv23_METHOD )
2358
2358
connection = Connection (ctx , None )
2359
2359
with pytest .raises (TypeError ):
2360
- connection .set_context (object ())
2360
+ connection .set_context (object ()) # type: ignore[arg-type]
2361
2361
with pytest .raises (TypeError ):
2362
- connection .set_context ("hello" )
2362
+ connection .set_context ("hello" ) # type: ignore[arg-type]
2363
2363
with pytest .raises (TypeError ):
2364
- connection .set_context (1 )
2364
+ connection .set_context (1 ) # type: ignore[arg-type]
2365
2365
assert ctx is connection .get_context ()
2366
2366
2367
2367
def test_set_context (self ) -> None :
@@ -2387,12 +2387,12 @@ def test_set_tlsext_host_name_wrong_args(self) -> None:
2387
2387
"""
2388
2388
conn = Connection (Context (SSLv23_METHOD ), None )
2389
2389
with pytest .raises (TypeError ):
2390
- conn .set_tlsext_host_name (object ())
2390
+ conn .set_tlsext_host_name (object ()) # type: ignore[arg-type]
2391
2391
with pytest .raises (TypeError ):
2392
2392
conn .set_tlsext_host_name (b"with\0 null" )
2393
2393
2394
2394
with pytest .raises (TypeError ):
2395
- conn .set_tlsext_host_name (b"example.com" .decode ("ascii" ))
2395
+ conn .set_tlsext_host_name (b"example.com" .decode ("ascii" )) # type: ignore[arg-type]
2396
2396
2397
2397
def test_pending (self ) -> None :
2398
2398
"""
@@ -2498,7 +2498,7 @@ def test_shutdown_wrong_args(self) -> None:
2498
2498
"""
2499
2499
connection = Connection (Context (SSLv23_METHOD ), None )
2500
2500
with pytest .raises (TypeError ):
2501
- connection .set_shutdown (None )
2501
+ connection .set_shutdown (None ) # type: ignore[arg-type]
2502
2502
2503
2503
def test_shutdown (self ) -> None :
2504
2504
"""
@@ -2568,14 +2568,14 @@ def test_state_string(self) -> None:
2568
2568
the `Connection`.
2569
2569
"""
2570
2570
server , client = socket_pair ()
2571
- server = loopback_server_factory (server )
2572
- client = loopback_client_factory (client )
2571
+ tls_server = loopback_server_factory (server )
2572
+ tls_client = loopback_client_factory (client )
2573
2573
2574
- assert server .get_state_string () in [
2574
+ assert tls_server .get_state_string () in [
2575
2575
b"before/accept initialization" ,
2576
2576
b"before SSL initialization" ,
2577
2577
]
2578
- assert client .get_state_string () in [
2578
+ assert tls_client .get_state_string () in [
2579
2579
b"before/connect initialization" ,
2580
2580
b"before SSL initialization" ,
2581
2581
]
@@ -2656,12 +2656,14 @@ def test_get_peer_cert_chain(self) -> None:
2656
2656
interact_in_memory (client , server )
2657
2657
2658
2658
chain = client .get_peer_cert_chain ()
2659
+ assert chain is not None
2659
2660
assert len (chain ) == 3
2660
2661
assert "Server Certificate" == chain [0 ].get_subject ().CN
2661
2662
assert "Intermediate Certificate" == chain [1 ].get_subject ().CN
2662
2663
assert "Authority Certificate" == chain [2 ].get_subject ().CN
2663
2664
2664
2665
cryptography_chain = client .get_peer_cert_chain (as_cryptography = True )
2666
+ assert cryptography_chain is not None
2665
2667
assert len (cryptography_chain ) == 3
2666
2668
assert (
2667
2669
cryptography_chain [0 ].subject .rfc4514_string ()
@@ -2710,7 +2712,9 @@ def test_get_verified_chain(self) -> None:
2710
2712
clientContext = Context (SSLv23_METHOD )
2711
2713
# cacert is self-signed so the client must trust it for verification
2712
2714
# to succeed.
2713
- clientContext .get_cert_store ().add_cert (cacert )
2715
+ cert_store = clientContext .get_cert_store ()
2716
+ assert cert_store is not None
2717
+ cert_store .add_cert (cacert )
2714
2718
clientContext .set_verify (VERIFY_PEER , verify_cb )
2715
2719
client = Connection (clientContext , None )
2716
2720
client .set_connect_state ()
@@ -2774,10 +2778,10 @@ def test_set_verify_overrides_context(self) -> None:
2774
2778
assert conn .get_verify_mode () == VERIFY_NONE
2775
2779
2776
2780
with pytest .raises (TypeError ):
2777
- conn .set_verify (None )
2781
+ conn .set_verify (None ) # type: ignore[arg-type]
2778
2782
2779
2783
with pytest .raises (TypeError ):
2780
- conn .set_verify (VERIFY_PEER , "not a callable" )
2784
+ conn .set_verify (VERIFY_PEER , "not a callable" ) # type: ignore[arg-type]
2781
2785
2782
2786
def test_set_verify_callback_reference (self ) -> None :
2783
2787
"""
@@ -2801,7 +2805,9 @@ def callback(conn, cert, errnum, depth, ok): # pragma: no cover
2801
2805
collect ()
2802
2806
assert tracker ()
2803
2807
2804
- conn .set_verify (VERIFY_PEER , lambda conn , cert , errnum , depth , ok : ok )
2808
+ conn .set_verify (
2809
+ VERIFY_PEER , lambda conn , cert , errnum , depth , ok : bool (ok )
2810
+ )
2805
2811
collect ()
2806
2812
collect ()
2807
2813
callback = tracker ()
@@ -2846,11 +2852,11 @@ def test_set_session_wrong_args(self) -> None:
2846
2852
ctx = Context (SSLv23_METHOD )
2847
2853
connection = Connection (ctx , None )
2848
2854
with pytest .raises (TypeError ):
2849
- connection .set_session (123 )
2855
+ connection .set_session (123 ) # type: ignore[arg-type]
2850
2856
with pytest .raises (TypeError ):
2851
- connection .set_session ("hello" )
2857
+ connection .set_session ("hello" ) # type: ignore[arg-type]
2852
2858
with pytest .raises (TypeError ):
2853
- connection .set_session (object ())
2859
+ connection .set_session (object ()) # type: ignore[arg-type]
2854
2860
2855
2861
def test_client_set_session (self ) -> None :
2856
2862
"""
@@ -2872,6 +2878,7 @@ def makeServer(socket):
2872
2878
2873
2879
originalServer , originalClient = loopback (server_factory = makeServer )
2874
2880
originalSession = originalClient .get_session ()
2881
+ assert originalSession is not None
2875
2882
2876
2883
def makeClient (socket ):
2877
2884
client = loopback_client_factory (socket )
@@ -2920,6 +2927,7 @@ def makeOriginalClient(socket):
2920
2927
server_factory = makeServer , client_factory = makeOriginalClient
2921
2928
)
2922
2929
originalSession = originalClient .get_session ()
2930
+ assert originalSession is not None
2923
2931
2924
2932
def makeClient (socket ):
2925
2933
# Intentionally use a different, incompatible method here.
@@ -2993,8 +3001,9 @@ def test_get_finished(self) -> None:
2993
3001
"""
2994
3002
server , _ = loopback ()
2995
3003
2996
- assert server .get_finished () is not None
2997
- assert len (server .get_finished ()) > 0
3004
+ finished = server .get_finished ()
3005
+ assert finished is not None
3006
+ assert len (finished ) > 0
2998
3007
2999
3008
def test_get_peer_finished (self ) -> None :
3000
3009
"""
@@ -3004,8 +3013,9 @@ def test_get_peer_finished(self) -> None:
3004
3013
"""
3005
3014
server , _ = loopback ()
3006
3015
3007
- assert server .get_peer_finished () is not None
3008
- assert len (server .get_peer_finished ()) > 0
3016
+ finished = server .get_peer_finished ()
3017
+ assert finished is not None
3018
+ assert len (finished ) > 0
3009
3019
3010
3020
def test_tls_finished_message_symmetry (self ) -> None :
3011
3021
"""
@@ -3198,9 +3208,9 @@ def test_wrong_args(self) -> None:
3198
3208
"""
3199
3209
connection = Connection (Context (SSLv23_METHOD ), None )
3200
3210
with pytest .raises (TypeError ):
3201
- connection .send (object ())
3211
+ connection .send (object ()) # type: ignore[arg-type]
3202
3212
with pytest .raises (TypeError ):
3203
- connection .send ([1 , 2 , 3 ])
3213
+ connection .send ([1 , 2 , 3 ]) # type: ignore[arg-type]
3204
3214
3205
3215
def test_short_bytes (self ) -> None :
3206
3216
"""
@@ -3219,7 +3229,7 @@ def test_text(self) -> None:
3219
3229
"""
3220
3230
server , client = loopback ()
3221
3231
with pytest .warns (DeprecationWarning ) as w :
3222
- count = server .send (b"xy" .decode ("ascii" ))
3232
+ count = server .send (b"xy" .decode ("ascii" )) # type: ignore[arg-type]
3223
3233
assert (
3224
3234
f"{ WARNING_TYPE_EXPECTED } for buf is no longer accepted, "
3225
3235
f"use bytes"
@@ -3407,9 +3417,9 @@ def test_wrong_args(self) -> None:
3407
3417
"""
3408
3418
connection = Connection (Context (SSLv23_METHOD ), None )
3409
3419
with pytest .raises (TypeError ):
3410
- connection .sendall (object ())
3420
+ connection .sendall (object ()) # type: ignore[arg-type]
3411
3421
with pytest .raises (TypeError ):
3412
- connection .sendall ([1 , 2 , 3 ])
3422
+ connection .sendall ([1 , 2 , 3 ]) # type: ignore[arg-type]
3413
3423
3414
3424
def test_short (self ) -> None :
3415
3425
"""
@@ -3427,7 +3437,7 @@ def test_text(self) -> None:
3427
3437
"""
3428
3438
server , client = loopback ()
3429
3439
with pytest .warns (DeprecationWarning ) as w :
3430
- server .sendall (b"x" .decode ("ascii" ))
3440
+ server .sendall (b"x" .decode ("ascii" )) # type: ignore[arg-type]
3431
3441
assert (
3432
3442
f"{ WARNING_TYPE_EXPECTED } for buf is no longer accepted, "
3433
3443
f"use bytes"
@@ -3656,7 +3666,7 @@ class TestMemoryBIO:
3656
3666
Tests for `OpenSSL.SSL.Connection` using a memory BIO.
3657
3667
"""
3658
3668
3659
- def _server (self , sock ) :
3669
+ def _server (self , sock : socket | None ) -> Connection :
3660
3670
"""
3661
3671
Create a new server-side SSL `Connection` object wrapped around `sock`.
3662
3672
"""
@@ -3669,6 +3679,7 @@ def _server(self, sock):
3669
3679
verify_cb ,
3670
3680
)
3671
3681
server_store = server_ctx .get_cert_store ()
3682
+ assert server_store is not None
3672
3683
server_ctx .use_privatekey (
3673
3684
load_privatekey (FILETYPE_PEM , server_key_pem )
3674
3685
)
@@ -3683,7 +3694,7 @@ def _server(self, sock):
3683
3694
server_conn .set_accept_state ()
3684
3695
return server_conn
3685
3696
3686
- def _client (self , sock ) :
3697
+ def _client (self , sock : socket | None ) -> Connection :
3687
3698
"""
3688
3699
Create a new client-side SSL `Connection` object wrapped around `sock`.
3689
3700
"""
@@ -3696,6 +3707,7 @@ def _client(self, sock):
3696
3707
verify_cb ,
3697
3708
)
3698
3709
client_store = client_ctx .get_cert_store ()
3710
+ assert client_store is not None
3699
3711
client_ctx .use_privatekey (
3700
3712
load_privatekey (FILETYPE_PEM , client_key_pem )
3701
3713
)
@@ -4154,14 +4166,22 @@ def inner(): # pragma: nocover
4154
4166
assert "Error text" in str (e .value )
4155
4167
4156
4168
4169
+ T = typing .TypeVar ("T" )
4170
+
4171
+
4157
4172
class TestOCSP :
4158
4173
"""
4159
4174
Tests for PyOpenSSL's OCSP stapling support.
4160
4175
"""
4161
4176
4162
4177
sample_ocsp_data = b"this is totally ocsp data"
4163
4178
4164
- def _client_connection (self , callback , data , request_ocsp = True ):
4179
+ def _client_connection (
4180
+ self ,
4181
+ callback : typing .Callable [[Connection , bytes , T | None ], bool ],
4182
+ data : T | None ,
4183
+ request_ocsp = True ,
4184
+ ) -> Connection :
4165
4185
"""
4166
4186
Builds a client connection suitable for using OCSP.
4167
4187
@@ -4181,7 +4201,11 @@ def _client_connection(self, callback, data, request_ocsp=True):
4181
4201
client .set_connect_state ()
4182
4202
return client
4183
4203
4184
- def _server_connection (self , callback , data ):
4204
+ def _server_connection (
4205
+ self ,
4206
+ callback : typing .Callable [[Connection , T | None ], bytes ],
4207
+ data : T | None ,
4208
+ ) -> Connection :
4185
4209
"""
4186
4210
Builds a server connection suitable for using OCSP.
4187
4211
@@ -4473,7 +4497,7 @@ class TestDTLS:
4473
4497
# Arbitrary number larger than any conceivable handshake volley.
4474
4498
LARGE_BUFFER = 65536
4475
4499
4476
- def _test_handshake_and_data (self , srtp_profile ) :
4500
+ def _test_handshake_and_data (self , srtp_profile : bytes | None ) -> None :
4477
4501
s_ctx = Context (DTLS_METHOD )
4478
4502
4479
4503
def generate_cookie (ssl ):
@@ -4506,7 +4530,9 @@ def verify_cookie(ssl, cookie):
4506
4530
4507
4531
latest_client_hello = None
4508
4532
4509
- def pump_membio (label , source , sink ):
4533
+ def pump_membio (
4534
+ label : str , source : Connection , sink : Connection
4535
+ ) -> bool :
4510
4536
try :
4511
4537
chunk = source .bio_read (self .LARGE_BUFFER )
4512
4538
except WantReadError :
@@ -4597,7 +4623,7 @@ def test_it_works_at_all(self) -> None:
4597
4623
def test_it_works_with_srtp (self ) -> None :
4598
4624
self ._test_handshake_and_data (srtp_profile = b"SRTP_AES128_CM_SHA1_80" )
4599
4625
4600
- def test_timeout (self , monkeypatch ) -> None :
4626
+ def test_timeout (self , monkeypatch : pytest . MonkeyPatch ) -> None :
4601
4627
c_ctx = Context (DTLS_METHOD )
4602
4628
c = Connection (c_ctx )
4603
4629
0 commit comments