Skip to content

Commit d3621f5

Browse files
authored
Clear out the remaining easy mypy issues in test_crypto.py (#1397)
1 parent b698943 commit d3621f5

File tree

1 file changed

+66
-48
lines changed

1 file changed

+66
-48
lines changed

tests/test_crypto.py

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,7 +2457,9 @@ def test_load_locations_parameters(
24572457
monkeypatch,
24582458
) -> None:
24592459
class LibMock:
2460-
def load_locations(self, store, cafile, capath):
2460+
def load_locations(
2461+
self, store: object, cafile: object, capath: object
2462+
) -> int:
24612463
self.cafile = cafile
24622464
self.capath = capath
24632465
return 1
@@ -2489,12 +2491,14 @@ def test_load_locations_raises_error_on_failure(
24892491
store.load_locations(cafile=str(invalid_ca_file))
24902492

24912493

2492-
def _runopenssl(pem, *args):
2494+
def _runopenssl(pem: bytes, *args: bytes) -> bytes:
24932495
"""
24942496
Run the command line openssl tool with the given arguments and write
24952497
the given PEM to its stdin. Not safe for quotes.
24962498
"""
24972499
proc = Popen([b"openssl", *list(args)], stdin=PIPE, stdout=PIPE)
2500+
assert proc.stdin is not None
2501+
assert proc.stdout is not None
24982502
proc.stdin.write(pem)
24992503
proc.stdin.close()
25002504
output = proc.stdout.read()
@@ -2562,7 +2566,9 @@ def test_load_privatekey_invalid_passphrase_type(self) -> None:
25622566
"""
25632567
with pytest.raises(TypeError):
25642568
load_privatekey(
2565-
FILETYPE_PEM, encryptedPrivateKeyPEMPassphrase, object()
2569+
FILETYPE_PEM,
2570+
encryptedPrivateKeyPEMPassphrase,
2571+
object(), # type: ignore[arg-type]
25662572
)
25672573

25682574
def test_load_privatekey_wrongPassphrase(self) -> None:
@@ -2576,14 +2582,14 @@ def test_load_privatekey_wrongPassphrase(self) -> None:
25762582

25772583
def test_load_privatekey_passphraseWrongType(self) -> None:
25782584
"""
2579-
`load_privatekey` raises `ValueError` when it is passeda passphrase
2585+
`load_privatekey` raises `ValueError` when it is passed a passphrase
25802586
with a private key encoded in a format, that doesn't support
25812587
encryption.
25822588
"""
25832589
key = load_privatekey(FILETYPE_PEM, root_key_pem)
25842590
blob = dump_privatekey(FILETYPE_ASN1, key)
25852591
with pytest.raises(ValueError):
2586-
load_privatekey(FILETYPE_ASN1, blob, "secret")
2592+
load_privatekey(FILETYPE_ASN1, blob, b"secret")
25872593

25882594
def test_load_privatekey_passphrase(self) -> None:
25892595
"""
@@ -2603,7 +2609,7 @@ def test_load_privatekey_passphrase_exception(self) -> None:
26032609
raised by `load_privatekey`.
26042610
"""
26052611

2606-
def cb(ignored):
2612+
def cb(ignored: object) -> bytes:
26072613
raise ArithmeticError
26082614

26092615
with pytest.raises(ArithmeticError):
@@ -2615,10 +2621,11 @@ def test_load_privatekey_wrongPassphraseCallback(self) -> None:
26152621
is passed an encrypted PEM and a passphrase callback which returns an
26162622
incorrect passphrase.
26172623
"""
2618-
called = []
2624+
called = False
26192625

2620-
def cb(*a):
2621-
called.append(None)
2626+
def cb(*a: object) -> bytes:
2627+
nonlocal called
2628+
called = True
26222629
return b"quack"
26232630

26242631
with pytest.raises(Error) as err:
@@ -2634,7 +2641,7 @@ def test_load_privatekey_passphraseCallback(self) -> None:
26342641
"""
26352642
called = []
26362643

2637-
def cb(writing):
2644+
def cb(writing: bool) -> bytes:
26382645
called.append(writing)
26392646
return encryptedPrivateKeyPEMPassphrase
26402647

@@ -2649,7 +2656,9 @@ def test_load_privatekey_passphrase_wrong_return_type(self) -> None:
26492656
"""
26502657
with pytest.raises(ValueError):
26512658
load_privatekey(
2652-
FILETYPE_PEM, encryptedPrivateKeyPEM, lambda *args: 3
2659+
FILETYPE_PEM,
2660+
encryptedPrivateKeyPEM,
2661+
lambda *args: 3, # type: ignore[arg-type]
26532662
)
26542663

26552664
def test_dump_privatekey_wrong_args(self) -> None:
@@ -2674,7 +2683,7 @@ def test_dump_privatekey_not_rsa_key(self) -> None:
26742683

26752684
def test_dump_privatekey_invalid_pkey(self) -> None:
26762685
with pytest.raises(TypeError):
2677-
dump_privatekey(FILETYPE_TEXT, object())
2686+
dump_privatekey(FILETYPE_TEXT, object()) # type: ignore[arg-type]
26782687

26792688
def test_dump_privatekey_unknown_cipher(self) -> None:
26802689
"""
@@ -2684,7 +2693,7 @@ def test_dump_privatekey_unknown_cipher(self) -> None:
26842693
key = PKey()
26852694
key.generate_key(TYPE_RSA, 2048)
26862695
with pytest.raises(ValueError):
2687-
dump_privatekey(FILETYPE_PEM, key, BAD_CIPHER, "passphrase")
2696+
dump_privatekey(FILETYPE_PEM, key, BAD_CIPHER, b"passphrase")
26882697

26892698
def test_dump_privatekey_invalid_passphrase_type(self) -> None:
26902699
"""
@@ -2694,7 +2703,7 @@ def test_dump_privatekey_invalid_passphrase_type(self) -> None:
26942703
key = PKey()
26952704
key.generate_key(TYPE_RSA, 2048)
26962705
with pytest.raises(TypeError):
2697-
dump_privatekey(FILETYPE_PEM, key, GOOD_CIPHER, object())
2706+
dump_privatekey(FILETYPE_PEM, key, GOOD_CIPHER, object()) # type: ignore[arg-type]
26982707

26992708
def test_dump_privatekey_invalid_filetype(self) -> None:
27002709
"""
@@ -2712,8 +2721,8 @@ def test_load_privatekey_passphrase_callback_length(self) -> None:
27122721
provided by the callback is too long, not silently truncate it.
27132722
"""
27142723

2715-
def cb(ignored):
2716-
return "a" * 1025
2724+
def cb(ignored: object) -> bytes:
2725+
return b"a" * 1025
27172726

27182727
with pytest.raises(ValueError):
27192728
load_privatekey(FILETYPE_PEM, encryptedPrivateKeyPEM, cb)
@@ -2739,7 +2748,7 @@ def test_dump_privatekey_passphrase_wrong_type(self) -> None:
27392748
"""
27402749
key = load_privatekey(FILETYPE_PEM, root_key_pem)
27412750
with pytest.raises(ValueError):
2742-
dump_privatekey(FILETYPE_ASN1, key, GOOD_CIPHER, "secret")
2751+
dump_privatekey(FILETYPE_ASN1, key, GOOD_CIPHER, b"secret")
27432752

27442753
def test_dump_certificate(self) -> None:
27452754
"""
@@ -2765,7 +2774,7 @@ def test_dump_certificate_bad_type(self) -> None:
27652774
"""
27662775
cert = load_certificate(FILETYPE_PEM, root_cert_pem)
27672776
with pytest.raises(ValueError):
2768-
dump_certificate(object(), cert)
2777+
dump_certificate(object(), cert) # type: ignore[arg-type]
27692778

27702779
def test_dump_privatekey_pem(self) -> None:
27712780
"""
@@ -2856,7 +2865,7 @@ def test_dump_privatekey_passphrase_callback(self) -> None:
28562865
passphrase = b"foo"
28572866
called = []
28582867

2859-
def cb(writing):
2868+
def cb(writing: bool) -> bytes:
28602869
called.append(writing)
28612870
return passphrase
28622871

@@ -2875,7 +2884,7 @@ def test_dump_privatekey_passphrase_exception(self) -> None:
28752884
by the passphrase callback.
28762885
"""
28772886

2878-
def cb(ignored):
2887+
def cb(ignored: object) -> bytes:
28792888
raise ArithmeticError
28802889

28812890
key = load_privatekey(FILETYPE_PEM, root_key_pem)
@@ -2888,8 +2897,8 @@ def test_dump_privatekey_passphraseCallbackLength(self) -> None:
28882897
provided by the callback is too long, not silently truncate it.
28892898
"""
28902899

2891-
def cb(ignored):
2892-
return "a" * 1025
2900+
def cb(ignored: object) -> bytes:
2901+
return b"a" * 1025
28932902

28942903
key = load_privatekey(FILETYPE_PEM, root_key_pem)
28952904
with pytest.raises(ValueError):
@@ -2945,9 +2954,9 @@ def test_bad_file_type(self) -> None:
29452954
`FILETYPE_PEM` nor `FILETYPE_ASN1` then `ValueError` is raised.
29462955
"""
29472956
with pytest.raises(ValueError):
2948-
load_certificate_request(object(), b"")
2957+
load_certificate_request(object(), b"") # type: ignore[arg-type]
29492958
with pytest.raises(ValueError):
2950-
load_certificate(object(), b"")
2959+
load_certificate(object(), b"") # type: ignore[arg-type]
29512960

29522961
def test_bad_certificate(self) -> None:
29532962
"""
@@ -2978,7 +2987,9 @@ class TestCRL:
29782987
)
29792988

29802989
@staticmethod
2981-
def _make_test_crl_cryptography(issuer_cert, issuer_key, certs=()):
2990+
def _make_test_crl_cryptography(
2991+
issuer_cert: X509, issuer_key: PKey, certs: list[X509] = []
2992+
) -> x509.CertificateRevocationList:
29822993
"""
29832994
Create a CRL using cryptography's API.
29842995
@@ -2988,9 +2999,7 @@ def _make_test_crl_cryptography(issuer_cert, issuer_key, certs=()):
29882999
from cryptography.x509.extensions import CRLReason, ReasonFlags
29893000

29903001
builder = x509.CertificateRevocationListBuilder()
2991-
builder = builder.issuer_name(
2992-
X509.to_cryptography(issuer_cert).subject
2993-
)
3002+
builder = builder.issuer_name(issuer_cert.to_cryptography().subject)
29943003
for cert in certs:
29953004
revoked = (
29963005
x509.RevokedCertificateBuilder()
@@ -3007,7 +3016,7 @@ def _make_test_crl_cryptography(issuer_cert, issuer_key, certs=()):
30073016
builder = builder.next_update(datetime(5000, 6, 1, 0, 0, 0))
30083017

30093018
crl = builder.sign(
3010-
private_key=PKey.to_cryptography_key(issuer_key),
3019+
private_key=issuer_key.to_cryptography_key(),
30113020
algorithm=hashes.SHA512(),
30123021
)
30133022
return crl
@@ -3078,7 +3087,7 @@ def test_valid(self) -> None:
30783087
store.add_cert(self.root_cert)
30793088
store.add_cert(self.intermediate_cert)
30803089
store_ctx = X509StoreContext(store, self.intermediate_server_cert)
3081-
assert store_ctx.verify_certificate() is None
3090+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
30823091

30833092
def test_reuse(self) -> None:
30843093
"""
@@ -3089,8 +3098,8 @@ def test_reuse(self) -> None:
30893098
store.add_cert(self.root_cert)
30903099
store.add_cert(self.intermediate_cert)
30913100
store_ctx = X509StoreContext(store, self.intermediate_server_cert)
3092-
assert store_ctx.verify_certificate() is None
3093-
assert store_ctx.verify_certificate() is None
3101+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
3102+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
30943103

30953104
@pytest.mark.parametrize(
30963105
"root_cert, chain, verified_cert",
@@ -3116,12 +3125,12 @@ def test_reuse(self) -> None:
31163125
],
31173126
)
31183127
def test_verify_success_with_chain(
3119-
self, root_cert, chain, verified_cert
3128+
self, root_cert: X509, chain: list[X509], verified_cert: X509
31203129
) -> None:
31213130
store = X509Store()
31223131
store.add_cert(root_cert)
31233132
store_ctx = X509StoreContext(store, verified_cert, chain=chain)
3124-
assert store_ctx.verify_certificate() is None
3133+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
31253134

31263135
def test_valid_untrusted_chain_reuse(self) -> None:
31273136
"""
@@ -3136,8 +3145,8 @@ def test_valid_untrusted_chain_reuse(self) -> None:
31363145
store_ctx = X509StoreContext(
31373146
store, self.intermediate_server_cert, chain=chain
31383147
)
3139-
assert store_ctx.verify_certificate() is None
3140-
assert store_ctx.verify_certificate() is None
3148+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
3149+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
31413150

31423151
def test_chain_reference(self) -> None:
31433152
"""
@@ -3153,7 +3162,7 @@ def test_chain_reference(self) -> None:
31533162
)
31543163

31553164
del chain
3156-
assert store_ctx.verify_certificate() is None
3165+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
31573166

31583167
@pytest.mark.parametrize(
31593168
"root_cert, chain, verified_cert",
@@ -3185,7 +3194,7 @@ def test_chain_reference(self) -> None:
31853194
],
31863195
)
31873196
def test_verify_fail_with_chain(
3188-
self, root_cert, chain, verified_cert
3197+
self, root_cert: X509, chain: list[X509], verified_cert: X509
31893198
) -> None:
31903199
store = X509Store()
31913200
if root_cert:
@@ -3211,7 +3220,9 @@ def test_verify_fail_with_chain(
32113220
),
32123221
],
32133222
)
3214-
def test_untrusted_chain_wrong_args(self, chain, expected_error) -> None:
3223+
def test_untrusted_chain_wrong_args(
3224+
self, chain: list[X509], expected_error: type[Exception]
3225+
) -> None:
32153226
"""
32163227
Creating ``X509StoreContext`` with wrong chain raises an exception.
32173228
"""
@@ -3245,7 +3256,7 @@ def test_trusted_self_signed(self) -> None:
32453256
store = X509Store()
32463257
store.add_cert(self.root_cert)
32473258
store_ctx = X509StoreContext(store, self.root_cert)
3248-
assert store_ctx.verify_certificate() is None
3259+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
32493260

32503261
def test_untrusted_self_signed(self) -> None:
32513262
"""
@@ -3313,7 +3324,7 @@ def test_modification_pre_verify(self) -> None:
33133324
assert exc.value.certificate.get_subject().CN == "intermediate"
33143325

33153326
store_ctx.set_store(store_good)
3316-
assert store_ctx.verify_certificate() is None
3327+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
33173328

33183329
def test_verify_with_time(self) -> None:
33193330
"""
@@ -3325,6 +3336,7 @@ def test_verify_with_time(self) -> None:
33253336
store.add_cert(self.intermediate_cert)
33263337

33273338
expire_time = self.intermediate_server_cert.get_notAfter()
3339+
assert expire_time is not None
33283340
expire_datetime = datetime.strptime(
33293341
expire_time.decode("utf-8"), "%Y%m%d%H%M%SZ"
33303342
)
@@ -3393,23 +3405,29 @@ def _create_ca_file(
33933405
cafile.write_bytes(dump_certificate(FILETYPE_PEM, cacert))
33943406
return cafile
33953407

3396-
def test_verify_with_ca_file_location(self, root_ca_file) -> None:
3408+
def test_verify_with_ca_file_location(
3409+
self, root_ca_file: pathlib.Path
3410+
) -> None:
33973411
store = X509Store()
33983412
store.load_locations(str(root_ca_file))
33993413

34003414
store_ctx = X509StoreContext(store, self.intermediate_cert)
34013415
store_ctx.verify_certificate()
34023416

3403-
def test_verify_with_ca_path_location(self, root_ca_file) -> None:
3417+
def test_verify_with_ca_path_location(
3418+
self, root_ca_file: pathlib.Path
3419+
) -> None:
34043420
store = X509Store()
34053421
store.load_locations(None, str(root_ca_file.parent))
34063422

34073423
store_ctx = X509StoreContext(store, self.intermediate_cert)
34083424
store_ctx.verify_certificate()
34093425

34103426
def test_verify_with_cafile_and_capath(
3411-
self, root_ca_file, intermediate_ca_file
3412-
):
3427+
self,
3428+
root_ca_file: pathlib.Path,
3429+
intermediate_ca_file: pathlib.Path,
3430+
) -> None:
34133431
store = X509Store()
34143432
store.load_locations(
34153433
cafile=str(root_ca_file), capath=str(intermediate_ca_file.parent)
@@ -3419,8 +3437,8 @@ def test_verify_with_cafile_and_capath(
34193437
store_ctx.verify_certificate()
34203438

34213439
def test_verify_with_multiple_ca_files(
3422-
self, root_ca_file, intermediate_ca_file
3423-
):
3440+
self, root_ca_file: pathlib.Path, intermediate_ca_file: pathlib.Path
3441+
) -> None:
34243442
store = X509Store()
34253443
store.load_locations(str(root_ca_file))
34263444
store.load_locations(str(intermediate_ca_file))
@@ -3451,7 +3469,7 @@ def test_verify_with_partial_chain(self) -> None:
34513469
# Now set the partial verification flag for verification.
34523470
store.set_flags(X509StoreFlags.PARTIAL_CHAIN)
34533471
store_ctx = X509StoreContext(store, self.intermediate_server_cert)
3454-
assert store_ctx.verify_certificate() is None
3472+
assert store_ctx.verify_certificate() is None # type: ignore[func-returns-value]
34553473

34563474

34573475
class TestEllipticCurve:

0 commit comments

Comments
 (0)