Skip to content

Commit 9baefba

Browse files
authored
Bring us under 100 test_ssl type-check issues (#1404)
1 parent 317c7fa commit 9baefba

File tree

1 file changed

+61
-35
lines changed

1 file changed

+61
-35
lines changed

Diff for: tests/test_ssl.py

+61-35
Original file line numberDiff line numberDiff line change
@@ -2338,7 +2338,7 @@ def test_bio_write(self) -> None:
23382338
connection.bio_write(b"xy")
23392339
connection.bio_write(bytearray(b"za"))
23402340
with pytest.warns(DeprecationWarning):
2341-
connection.bio_write("deprecated")
2341+
connection.bio_write("deprecated") # type: ignore[arg-type]
23422342

23432343
def test_get_context(self) -> None:
23442344
"""
@@ -2357,11 +2357,11 @@ def test_set_context_wrong_args(self) -> None:
23572357
ctx = Context(SSLv23_METHOD)
23582358
connection = Connection(ctx, None)
23592359
with pytest.raises(TypeError):
2360-
connection.set_context(object())
2360+
connection.set_context(object()) # type: ignore[arg-type]
23612361
with pytest.raises(TypeError):
2362-
connection.set_context("hello")
2362+
connection.set_context("hello") # type: ignore[arg-type]
23632363
with pytest.raises(TypeError):
2364-
connection.set_context(1)
2364+
connection.set_context(1) # type: ignore[arg-type]
23652365
assert ctx is connection.get_context()
23662366

23672367
def test_set_context(self) -> None:
@@ -2387,12 +2387,12 @@ def test_set_tlsext_host_name_wrong_args(self) -> None:
23872387
"""
23882388
conn = Connection(Context(SSLv23_METHOD), None)
23892389
with pytest.raises(TypeError):
2390-
conn.set_tlsext_host_name(object())
2390+
conn.set_tlsext_host_name(object()) # type: ignore[arg-type]
23912391
with pytest.raises(TypeError):
23922392
conn.set_tlsext_host_name(b"with\0null")
23932393

23942394
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]
23962396

23972397
def test_pending(self) -> None:
23982398
"""
@@ -2498,7 +2498,7 @@ def test_shutdown_wrong_args(self) -> None:
24982498
"""
24992499
connection = Connection(Context(SSLv23_METHOD), None)
25002500
with pytest.raises(TypeError):
2501-
connection.set_shutdown(None)
2501+
connection.set_shutdown(None) # type: ignore[arg-type]
25022502

25032503
def test_shutdown(self) -> None:
25042504
"""
@@ -2568,14 +2568,14 @@ def test_state_string(self) -> None:
25682568
the `Connection`.
25692569
"""
25702570
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)
25732573

2574-
assert server.get_state_string() in [
2574+
assert tls_server.get_state_string() in [
25752575
b"before/accept initialization",
25762576
b"before SSL initialization",
25772577
]
2578-
assert client.get_state_string() in [
2578+
assert tls_client.get_state_string() in [
25792579
b"before/connect initialization",
25802580
b"before SSL initialization",
25812581
]
@@ -2656,12 +2656,14 @@ def test_get_peer_cert_chain(self) -> None:
26562656
interact_in_memory(client, server)
26572657

26582658
chain = client.get_peer_cert_chain()
2659+
assert chain is not None
26592660
assert len(chain) == 3
26602661
assert "Server Certificate" == chain[0].get_subject().CN
26612662
assert "Intermediate Certificate" == chain[1].get_subject().CN
26622663
assert "Authority Certificate" == chain[2].get_subject().CN
26632664

26642665
cryptography_chain = client.get_peer_cert_chain(as_cryptography=True)
2666+
assert cryptography_chain is not None
26652667
assert len(cryptography_chain) == 3
26662668
assert (
26672669
cryptography_chain[0].subject.rfc4514_string()
@@ -2710,7 +2712,9 @@ def test_get_verified_chain(self) -> None:
27102712
clientContext = Context(SSLv23_METHOD)
27112713
# cacert is self-signed so the client must trust it for verification
27122714
# 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)
27142718
clientContext.set_verify(VERIFY_PEER, verify_cb)
27152719
client = Connection(clientContext, None)
27162720
client.set_connect_state()
@@ -2774,10 +2778,10 @@ def test_set_verify_overrides_context(self) -> None:
27742778
assert conn.get_verify_mode() == VERIFY_NONE
27752779

27762780
with pytest.raises(TypeError):
2777-
conn.set_verify(None)
2781+
conn.set_verify(None) # type: ignore[arg-type]
27782782

27792783
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]
27812785

27822786
def test_set_verify_callback_reference(self) -> None:
27832787
"""
@@ -2801,7 +2805,9 @@ def callback(conn, cert, errnum, depth, ok): # pragma: no cover
28012805
collect()
28022806
assert tracker()
28032807

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+
)
28052811
collect()
28062812
collect()
28072813
callback = tracker()
@@ -2846,11 +2852,11 @@ def test_set_session_wrong_args(self) -> None:
28462852
ctx = Context(SSLv23_METHOD)
28472853
connection = Connection(ctx, None)
28482854
with pytest.raises(TypeError):
2849-
connection.set_session(123)
2855+
connection.set_session(123) # type: ignore[arg-type]
28502856
with pytest.raises(TypeError):
2851-
connection.set_session("hello")
2857+
connection.set_session("hello") # type: ignore[arg-type]
28522858
with pytest.raises(TypeError):
2853-
connection.set_session(object())
2859+
connection.set_session(object()) # type: ignore[arg-type]
28542860

28552861
def test_client_set_session(self) -> None:
28562862
"""
@@ -2872,6 +2878,7 @@ def makeServer(socket):
28722878

28732879
originalServer, originalClient = loopback(server_factory=makeServer)
28742880
originalSession = originalClient.get_session()
2881+
assert originalSession is not None
28752882

28762883
def makeClient(socket):
28772884
client = loopback_client_factory(socket)
@@ -2920,6 +2927,7 @@ def makeOriginalClient(socket):
29202927
server_factory=makeServer, client_factory=makeOriginalClient
29212928
)
29222929
originalSession = originalClient.get_session()
2930+
assert originalSession is not None
29232931

29242932
def makeClient(socket):
29252933
# Intentionally use a different, incompatible method here.
@@ -2993,8 +3001,9 @@ def test_get_finished(self) -> None:
29933001
"""
29943002
server, _ = loopback()
29953003

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
29983007

29993008
def test_get_peer_finished(self) -> None:
30003009
"""
@@ -3004,8 +3013,9 @@ def test_get_peer_finished(self) -> None:
30043013
"""
30053014
server, _ = loopback()
30063015

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
30093019

30103020
def test_tls_finished_message_symmetry(self) -> None:
30113021
"""
@@ -3198,9 +3208,9 @@ def test_wrong_args(self) -> None:
31983208
"""
31993209
connection = Connection(Context(SSLv23_METHOD), None)
32003210
with pytest.raises(TypeError):
3201-
connection.send(object())
3211+
connection.send(object()) # type: ignore[arg-type]
32023212
with pytest.raises(TypeError):
3203-
connection.send([1, 2, 3])
3213+
connection.send([1, 2, 3]) # type: ignore[arg-type]
32043214

32053215
def test_short_bytes(self) -> None:
32063216
"""
@@ -3219,7 +3229,7 @@ def test_text(self) -> None:
32193229
"""
32203230
server, client = loopback()
32213231
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]
32233233
assert (
32243234
f"{WARNING_TYPE_EXPECTED} for buf is no longer accepted, "
32253235
f"use bytes"
@@ -3407,9 +3417,9 @@ def test_wrong_args(self) -> None:
34073417
"""
34083418
connection = Connection(Context(SSLv23_METHOD), None)
34093419
with pytest.raises(TypeError):
3410-
connection.sendall(object())
3420+
connection.sendall(object()) # type: ignore[arg-type]
34113421
with pytest.raises(TypeError):
3412-
connection.sendall([1, 2, 3])
3422+
connection.sendall([1, 2, 3]) # type: ignore[arg-type]
34133423

34143424
def test_short(self) -> None:
34153425
"""
@@ -3427,7 +3437,7 @@ def test_text(self) -> None:
34273437
"""
34283438
server, client = loopback()
34293439
with pytest.warns(DeprecationWarning) as w:
3430-
server.sendall(b"x".decode("ascii"))
3440+
server.sendall(b"x".decode("ascii")) # type: ignore[arg-type]
34313441
assert (
34323442
f"{WARNING_TYPE_EXPECTED} for buf is no longer accepted, "
34333443
f"use bytes"
@@ -3656,7 +3666,7 @@ class TestMemoryBIO:
36563666
Tests for `OpenSSL.SSL.Connection` using a memory BIO.
36573667
"""
36583668

3659-
def _server(self, sock):
3669+
def _server(self, sock: socket | None) -> Connection:
36603670
"""
36613671
Create a new server-side SSL `Connection` object wrapped around `sock`.
36623672
"""
@@ -3669,6 +3679,7 @@ def _server(self, sock):
36693679
verify_cb,
36703680
)
36713681
server_store = server_ctx.get_cert_store()
3682+
assert server_store is not None
36723683
server_ctx.use_privatekey(
36733684
load_privatekey(FILETYPE_PEM, server_key_pem)
36743685
)
@@ -3683,7 +3694,7 @@ def _server(self, sock):
36833694
server_conn.set_accept_state()
36843695
return server_conn
36853696

3686-
def _client(self, sock):
3697+
def _client(self, sock: socket | None) -> Connection:
36873698
"""
36883699
Create a new client-side SSL `Connection` object wrapped around `sock`.
36893700
"""
@@ -3696,6 +3707,7 @@ def _client(self, sock):
36963707
verify_cb,
36973708
)
36983709
client_store = client_ctx.get_cert_store()
3710+
assert client_store is not None
36993711
client_ctx.use_privatekey(
37003712
load_privatekey(FILETYPE_PEM, client_key_pem)
37013713
)
@@ -4154,14 +4166,22 @@ def inner(): # pragma: nocover
41544166
assert "Error text" in str(e.value)
41554167

41564168

4169+
T = typing.TypeVar("T")
4170+
4171+
41574172
class TestOCSP:
41584173
"""
41594174
Tests for PyOpenSSL's OCSP stapling support.
41604175
"""
41614176

41624177
sample_ocsp_data = b"this is totally ocsp data"
41634178

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:
41654185
"""
41664186
Builds a client connection suitable for using OCSP.
41674187
@@ -4181,7 +4201,11 @@ def _client_connection(self, callback, data, request_ocsp=True):
41814201
client.set_connect_state()
41824202
return client
41834203

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:
41854209
"""
41864210
Builds a server connection suitable for using OCSP.
41874211
@@ -4473,7 +4497,7 @@ class TestDTLS:
44734497
# Arbitrary number larger than any conceivable handshake volley.
44744498
LARGE_BUFFER = 65536
44754499

4476-
def _test_handshake_and_data(self, srtp_profile):
4500+
def _test_handshake_and_data(self, srtp_profile: bytes | None) -> None:
44774501
s_ctx = Context(DTLS_METHOD)
44784502

44794503
def generate_cookie(ssl):
@@ -4506,7 +4530,9 @@ def verify_cookie(ssl, cookie):
45064530

45074531
latest_client_hello = None
45084532

4509-
def pump_membio(label, source, sink):
4533+
def pump_membio(
4534+
label: str, source: Connection, sink: Connection
4535+
) -> bool:
45104536
try:
45114537
chunk = source.bio_read(self.LARGE_BUFFER)
45124538
except WantReadError:
@@ -4597,7 +4623,7 @@ def test_it_works_at_all(self) -> None:
45974623
def test_it_works_with_srtp(self) -> None:
45984624
self._test_handshake_and_data(srtp_profile=b"SRTP_AES128_CM_SHA1_80")
45994625

4600-
def test_timeout(self, monkeypatch) -> None:
4626+
def test_timeout(self, monkeypatch: pytest.MonkeyPatch) -> None:
46014627
c_ctx = Context(DTLS_METHOD)
46024628
c = Connection(c_ctx)
46034629

0 commit comments

Comments
 (0)