Skip to content

Commit 67d973a

Browse files
authored
Complete nearly all of getting test_crypto.py to type-check (#1399)
Remove some tests that were pointless.
1 parent 1fdced6 commit 67d973a

File tree

3 files changed

+163
-223
lines changed

3 files changed

+163
-223
lines changed

tests/test_crypto.py

Lines changed: 162 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
X509StoreContext,
4343
X509StoreContextError,
4444
X509StoreFlags,
45+
_EllipticCurve,
4546
_Key,
47+
_PrivateKey,
4648
dump_certificate,
4749
dump_certificate_request,
4850
dump_privatekey,
@@ -62,8 +64,6 @@
6264

6365
from .util import (
6466
NON_ASCII,
65-
EqualityTestsMixin,
66-
is_consistent_type,
6767
)
6868

6969

@@ -896,18 +896,6 @@ def test_str(self) -> None:
896896
== "CA:FALSE"
897897
)
898898

899-
def test_type(self) -> None:
900-
"""
901-
`X509Extension` can be used to create instances of that type.
902-
"""
903-
assert is_consistent_type(
904-
X509Extension,
905-
"X509Extension",
906-
b"basicConstraints",
907-
True,
908-
b"CA:true",
909-
)
910-
911899
def test_construction(self) -> None:
912900
"""
913901
`X509Extension` accepts an extension type name, a critical flag,
@@ -1107,11 +1095,31 @@ def test_convert_roundtrip_cryptography_private_key(
11071095
"""
11081096
key = serialization.load_pem_private_key(key_string, None)
11091097
assert isinstance(key, key_type)
1098+
assert isinstance(
1099+
key,
1100+
(
1101+
dsa.DSAPrivateKey,
1102+
ec.EllipticCurvePrivateKey,
1103+
ed25519.Ed25519PrivateKey,
1104+
ed448.Ed448PrivateKey,
1105+
rsa.RSAPrivateKey,
1106+
),
1107+
)
11101108
pkey = PKey.from_cryptography_key(key)
11111109

11121110
assert isinstance(pkey, PKey)
11131111
parsed_key = pkey.to_cryptography_key()
11141112
assert isinstance(parsed_key, key_type)
1113+
assert isinstance(
1114+
parsed_key,
1115+
(
1116+
dsa.DSAPrivateKey,
1117+
ec.EllipticCurvePrivateKey,
1118+
ed25519.Ed25519PrivateKey,
1119+
ed448.Ed448PrivateKey,
1120+
rsa.RSAPrivateKey,
1121+
),
1122+
)
11151123
assert parsed_key.public_key().public_bytes(
11161124
serialization.Encoding.PEM,
11171125
serialization.PublicFormat.SubjectPublicKeyInfo,
@@ -1141,11 +1149,31 @@ def test_convert_roundtrip_cryptography_public_key(
11411149
"""
11421150
key = serialization.load_pem_public_key(key_string, None)
11431151
assert isinstance(key, key_type)
1152+
assert isinstance(
1153+
key,
1154+
(
1155+
dsa.DSAPublicKey,
1156+
ec.EllipticCurvePublicKey,
1157+
ed25519.Ed25519PublicKey,
1158+
ed448.Ed448PublicKey,
1159+
rsa.RSAPublicKey,
1160+
),
1161+
)
11441162
pkey = PKey.from_cryptography_key(key)
11451163

11461164
assert isinstance(pkey, PKey)
11471165
parsed_key = pkey.to_cryptography_key()
11481166
assert isinstance(parsed_key, key_type)
1167+
assert isinstance(
1168+
parsed_key,
1169+
(
1170+
dsa.DSAPublicKey,
1171+
ec.EllipticCurvePublicKey,
1172+
ed25519.Ed25519PublicKey,
1173+
ed448.Ed448PublicKey,
1174+
rsa.RSAPublicKey,
1175+
),
1176+
)
11491177
assert parsed_key.public_bytes(
11501178
serialization.Encoding.PEM,
11511179
serialization.PublicFormat.SubjectPublicKeyInfo,
@@ -1161,6 +1189,7 @@ def test_convert_from_cryptography_public_key(self) -> None:
11611189
PKey.from_cryptography_key creates a proper public PKey.
11621190
"""
11631191
key = serialization.load_pem_public_key(cleartextPublicKeyPEM)
1192+
assert isinstance(key, rsa.RSAPublicKey)
11641193
pkey = PKey.from_cryptography_key(key)
11651194

11661195
assert isinstance(pkey, PKey)
@@ -1174,7 +1203,7 @@ def test_convert_from_cryptography_unsupported_type(self) -> None:
11741203
"""
11751204
key = serialization.load_pem_private_key(x25519_private_key_pem, None)
11761205
with pytest.raises(TypeError):
1177-
PKey.from_cryptography_key(key)
1206+
PKey.from_cryptography_key(key) # type: ignore[arg-type]
11781207

11791208
def test_convert_public_pkey_to_cryptography_key(self) -> None:
11801209
"""
@@ -1186,12 +1215,6 @@ def test_convert_public_pkey_to_cryptography_key(self) -> None:
11861215
assert isinstance(key, rsa.RSAPublicKey)
11871216
assert pkey.bits() == key.key_size
11881217

1189-
def test_type(self) -> None:
1190-
"""
1191-
`PKey` can be used to create instances of that type.
1192-
"""
1193-
assert is_consistent_type(PKey, "PKey")
1194-
11951218
def test_construction(self) -> None:
11961219
"""
11971220
`PKey` takes no arguments and returns a new `PKey` instance.
@@ -1659,12 +1682,6 @@ def signable(self):
16591682
"""
16601683
return X509Req()
16611684

1662-
def test_type(self) -> None:
1663-
"""
1664-
`X509Req` can be used to create instances of that type.
1665-
"""
1666-
assert is_consistent_type(X509Req, "X509Req")
1667-
16681685
def test_construction(self) -> None:
16691686
"""
16701687
`X509Req` takes no arguments and returns an `X509Req` instance.
@@ -1821,7 +1838,7 @@ def test_convert_from_cryptography(self) -> None:
18211838

18221839
def test_convert_from_cryptography_unsupported_type(self) -> None:
18231840
with pytest.raises(TypeError):
1824-
X509Req.from_cryptography(object())
1841+
X509Req.from_cryptography(object()) # type: ignore[arg-type]
18251842

18261843
def test_convert_to_cryptography_key(self) -> None:
18271844
req = load_certificate_request(
@@ -1849,12 +1866,6 @@ def signable(self):
18491866
certificate.gmtime_adj_notAfter(24 * 60 * 60)
18501867
return certificate
18511868

1852-
def test_type(self) -> None:
1853-
"""
1854-
`X509` can be used to create instances of that type.
1855-
"""
1856-
assert is_consistent_type(X509, "X509")
1857-
18581869
def test_construction(self) -> None:
18591870
"""
18601871
`X509` takes no arguments and returns an instance of `X509`.
@@ -2081,7 +2092,9 @@ def test_digest(self) -> None:
20812092
)
20822093
)
20832094

2084-
def _extcert(self, key: _Key, extensions: list[x509.Extension]) -> X509:
2095+
def _extcert(
2096+
self, key: _PrivateKey, extensions: list[x509.ExtensionType]
2097+
) -> X509:
20852098
subject = x509.Name(
20862099
[x509.NameAttribute(x509.NameOID.COMMON_NAME, "Unit Tests")]
20872100
)
@@ -2108,6 +2121,7 @@ def test_extension_count(self) -> None:
21082121
pkey = load_privatekey(
21092122
FILETYPE_PEM, client_key_pem
21102123
).to_cryptography_key()
2124+
assert isinstance(pkey, rsa.RSAPrivateKey)
21112125
ca = x509.BasicConstraints(ca=False, path_length=None)
21122126
key = x509.KeyUsage(
21132127
digital_signature=True,
@@ -2142,6 +2156,7 @@ def test_get_extension(self) -> None:
21422156
pkey = load_privatekey(
21432157
FILETYPE_PEM, client_key_pem
21442158
).to_cryptography_key()
2159+
assert isinstance(pkey, rsa.RSAPrivateKey)
21452160
ca = x509.BasicConstraints(ca=False, path_length=None)
21462161
key = x509.KeyUsage(
21472162
digital_signature=True,
@@ -2370,7 +2385,7 @@ def test_convert_from_cryptography(self) -> None:
23702385

23712386
def test_convert_from_cryptography_unsupported_type(self) -> None:
23722387
with pytest.raises(TypeError):
2373-
X509.from_cryptography(object())
2388+
X509.from_cryptography(object()) # type: ignore[arg-type]
23742389

23752390
def test_convert_to_cryptography_key(self) -> None:
23762391
cert = load_certificate(FILETYPE_PEM, intermediate_cert_pem)
@@ -2385,12 +2400,6 @@ class TestX509Store:
23852400
Test for `OpenSSL.crypto.X509Store`.
23862401
"""
23872402

2388-
def test_type(self) -> None:
2389-
"""
2390-
`X509Store` is a type object.
2391-
"""
2392-
assert is_consistent_type(X509Store, "X509Store")
2393-
23942403
def test_add_cert(self) -> None:
23952404
"""
23962405
`X509Store.add_cert` adds a `X509` instance to the certificate store.
@@ -3015,8 +3024,10 @@ def _make_test_crl_cryptography(
30153024
# considered to have expired.
30163025
builder = builder.next_update(datetime(5000, 6, 1, 0, 0, 0))
30173026

3027+
key = issuer_key.to_cryptography_key()
3028+
assert isinstance(key, rsa.RSAPrivateKey)
30183029
crl = builder.sign(
3019-
private_key=issuer_key.to_cryptography_key(),
3030+
key,
30203031
algorithm=hashes.SHA512(),
30213032
)
30223033
return crl
@@ -3523,36 +3534,121 @@ def test_to_EC_KEY(self) -> None:
35233534
curve._to_EC_KEY()
35243535

35253536

3526-
class EllipticCurveFactory:
3527-
"""
3528-
A helper to get the names of two curves.
3529-
"""
3530-
3531-
def __init__(self):
3532-
curves = iter(get_elliptic_curves())
3533-
self.curve_name = next(curves).name
3534-
self.another_curve_name = next(curves).name
3535-
3536-
3537-
class TestEllipticCurveEquality(EqualityTestsMixin):
3537+
class TestEllipticCurveEquality:
35383538
"""
35393539
Tests `_EllipticCurve`'s implementation of ``==`` and ``!=``.
35403540
"""
35413541

3542-
curve_factory = EllipticCurveFactory()
3543-
3544-
def anInstance(self):
3542+
def anInstance(self) -> _EllipticCurve:
35453543
"""
35463544
Get the curve object for an arbitrary curve supported by the system.
35473545
"""
3548-
return get_elliptic_curve(self.curve_factory.curve_name)
3546+
return next(iter(get_elliptic_curves()))
35493547

3550-
def anotherInstance(self):
3548+
def anotherInstance(self) -> _EllipticCurve:
35513549
"""
35523550
Get the curve object for an arbitrary curve supported by the system -
35533551
but not the one returned by C{anInstance}.
35543552
"""
3555-
return get_elliptic_curve(self.curve_factory.another_curve_name)
3553+
return list(get_elliptic_curves())[1]
3554+
3555+
def test_identicalEq(self) -> None:
3556+
"""
3557+
An object compares equal to itself using the C{==} operator.
3558+
"""
3559+
o = self.anInstance()
3560+
assert o == o
3561+
3562+
def test_identicalNe(self) -> None:
3563+
"""
3564+
An object doesn't compare not equal to itself using the C{!=} operator.
3565+
"""
3566+
o = self.anInstance()
3567+
assert not (o != o)
3568+
3569+
def test_sameEq(self) -> None:
3570+
"""
3571+
Two objects that are equal to each other compare equal to each other
3572+
using the C{==} operator.
3573+
"""
3574+
a = self.anInstance()
3575+
b = self.anInstance()
3576+
assert a == b
3577+
3578+
def test_sameNe(self) -> None:
3579+
"""
3580+
Two objects that are equal to each other do not compare not equal to
3581+
each other using the C{!=} operator.
3582+
"""
3583+
a = self.anInstance()
3584+
b = self.anInstance()
3585+
assert not (a != b)
3586+
3587+
def test_differentEq(self) -> None:
3588+
"""
3589+
Two objects that are not equal to each other do not compare equal to
3590+
each other using the C{==} operator.
3591+
"""
3592+
a = self.anInstance()
3593+
b = self.anotherInstance()
3594+
assert not (a == b)
3595+
3596+
def test_differentNe(self) -> None:
3597+
"""
3598+
Two objects that are not equal to each other compare not equal to each
3599+
other using the C{!=} operator.
3600+
"""
3601+
a = self.anInstance()
3602+
b = self.anotherInstance()
3603+
assert a != b
3604+
3605+
def test_anotherTypeEq(self) -> None:
3606+
"""
3607+
The object does not compare equal to an object of an unrelated type
3608+
(which does not implement the comparison) using the C{==} operator.
3609+
"""
3610+
a = self.anInstance()
3611+
b = object()
3612+
assert not (a == b)
3613+
3614+
def test_anotherTypeNe(self) -> None:
3615+
"""
3616+
The object compares not equal to an object of an unrelated type (which
3617+
does not implement the comparison) using the C{!=} operator.
3618+
"""
3619+
a = self.anInstance()
3620+
b = object()
3621+
assert a != b
3622+
3623+
def test_delegatedEq(self) -> None:
3624+
"""
3625+
The result of comparison using C{==} is delegated to the right-hand
3626+
operand if it is of an unrelated type.
3627+
"""
3628+
3629+
class Delegate:
3630+
def __eq__(self, other):
3631+
# Do something crazy and obvious.
3632+
return [self]
3633+
3634+
a = self.anInstance()
3635+
b = Delegate()
3636+
assert (a == b) == [b] # type: ignore[comparison-overlap]
3637+
3638+
def test_delegateNe(self) -> None:
3639+
"""
3640+
The result of comparison using C{!=} is delegated to the right-hand
3641+
operand if it is of an unrelated type.
3642+
"""
3643+
3644+
class Delegate:
3645+
def __ne__(self, other):
3646+
# Do something crazy and obvious.
3647+
return [self]
3648+
3649+
a = self.anInstance()
3650+
b = Delegate()
3651+
assert (a != b) == [b] # type: ignore[comparison-overlap]
35563652

35573653

35583654
class TestEllipticCurveHash:
@@ -3561,14 +3657,12 @@ class TestEllipticCurveHash:
35613657
an item in a `dict` or `set`).
35623658
"""
35633659

3564-
curve_factory = EllipticCurveFactory()
3565-
35663660
def test_contains(self) -> None:
35673661
"""
35683662
The ``in`` operator reports that a `set` containing a curve does
35693663
contain that curve.
35703664
"""
3571-
curve = get_elliptic_curve(self.curve_factory.curve_name)
3665+
curve = next(iter(get_elliptic_curves()))
35723666
curves = set([curve])
35733667
assert curve in curves
35743668

@@ -3577,8 +3671,8 @@ def test_does_not_contain(self) -> None:
35773671
The ``in`` operator reports that a `set` not containing a curve
35783672
does not contain that curve.
35793673
"""
3580-
curve = get_elliptic_curve(self.curve_factory.curve_name)
3581-
curves = set(
3582-
[get_elliptic_curve(self.curve_factory.another_curve_name)]
3583-
)
3674+
all_curves = list(get_elliptic_curves())
3675+
3676+
curve = all_curves[0]
3677+
curves = set([all_curves[1]])
35843678
assert curve not in curves

0 commit comments

Comments
 (0)