Skip to content

Commit 4ed08d4

Browse files
committed
Complete nearly all of getting test_crypto.py to type-check
Remove some tests that were pointless.
1 parent 1fdced6 commit 4ed08d4

File tree

3 files changed

+155
-223
lines changed

3 files changed

+155
-223
lines changed

tests/test_crypto.py

Lines changed: 154 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,27 @@ 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+
dsa.DSAPrivateKey
1101+
| ec.EllipticCurvePrivateKey
1102+
| ed25519.Ed25519PrivateKey
1103+
| ed448.Ed448PrivateKey
1104+
| rsa.RSAPrivateKey,
1105+
)
11101106
pkey = PKey.from_cryptography_key(key)
11111107

11121108
assert isinstance(pkey, PKey)
11131109
parsed_key = pkey.to_cryptography_key()
11141110
assert isinstance(parsed_key, key_type)
1111+
assert isinstance(
1112+
parsed_key,
1113+
dsa.DSAPrivateKey
1114+
| ec.EllipticCurvePrivateKey
1115+
| ed25519.Ed25519PrivateKey
1116+
| ed448.Ed448PrivateKey
1117+
| rsa.RSAPrivateKey,
1118+
)
11151119
assert parsed_key.public_key().public_bytes(
11161120
serialization.Encoding.PEM,
11171121
serialization.PublicFormat.SubjectPublicKeyInfo,
@@ -1141,11 +1145,27 @@ def test_convert_roundtrip_cryptography_public_key(
11411145
"""
11421146
key = serialization.load_pem_public_key(key_string, None)
11431147
assert isinstance(key, key_type)
1148+
assert isinstance(
1149+
key,
1150+
dsa.DSAPublicKey
1151+
| ec.EllipticCurvePublicKey
1152+
| ed25519.Ed25519PublicKey
1153+
| ed448.Ed448PublicKey
1154+
| rsa.RSAPublicKey,
1155+
)
11441156
pkey = PKey.from_cryptography_key(key)
11451157

11461158
assert isinstance(pkey, PKey)
11471159
parsed_key = pkey.to_cryptography_key()
11481160
assert isinstance(parsed_key, key_type)
1161+
assert isinstance(
1162+
parsed_key,
1163+
dsa.DSAPublicKey
1164+
| ec.EllipticCurvePublicKey
1165+
| ed25519.Ed25519PublicKey
1166+
| ed448.Ed448PublicKey
1167+
| rsa.RSAPublicKey,
1168+
)
11491169
assert parsed_key.public_bytes(
11501170
serialization.Encoding.PEM,
11511171
serialization.PublicFormat.SubjectPublicKeyInfo,
@@ -1161,6 +1181,7 @@ def test_convert_from_cryptography_public_key(self) -> None:
11611181
PKey.from_cryptography_key creates a proper public PKey.
11621182
"""
11631183
key = serialization.load_pem_public_key(cleartextPublicKeyPEM)
1184+
assert isinstance(key, rsa.RSAPublicKey)
11641185
pkey = PKey.from_cryptography_key(key)
11651186

11661187
assert isinstance(pkey, PKey)
@@ -1174,7 +1195,7 @@ def test_convert_from_cryptography_unsupported_type(self) -> None:
11741195
"""
11751196
key = serialization.load_pem_private_key(x25519_private_key_pem, None)
11761197
with pytest.raises(TypeError):
1177-
PKey.from_cryptography_key(key)
1198+
PKey.from_cryptography_key(key) # type: ignore[arg-type]
11781199

11791200
def test_convert_public_pkey_to_cryptography_key(self) -> None:
11801201
"""
@@ -1186,12 +1207,6 @@ def test_convert_public_pkey_to_cryptography_key(self) -> None:
11861207
assert isinstance(key, rsa.RSAPublicKey)
11871208
assert pkey.bits() == key.key_size
11881209

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-
11951210
def test_construction(self) -> None:
11961211
"""
11971212
`PKey` takes no arguments and returns a new `PKey` instance.
@@ -1659,12 +1674,6 @@ def signable(self):
16591674
"""
16601675
return X509Req()
16611676

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-
16681677
def test_construction(self) -> None:
16691678
"""
16701679
`X509Req` takes no arguments and returns an `X509Req` instance.
@@ -1821,7 +1830,7 @@ def test_convert_from_cryptography(self) -> None:
18211830

18221831
def test_convert_from_cryptography_unsupported_type(self) -> None:
18231832
with pytest.raises(TypeError):
1824-
X509Req.from_cryptography(object())
1833+
X509Req.from_cryptography(object()) # type: ignore[arg-type]
18251834

18261835
def test_convert_to_cryptography_key(self) -> None:
18271836
req = load_certificate_request(
@@ -1849,12 +1858,6 @@ def signable(self):
18491858
certificate.gmtime_adj_notAfter(24 * 60 * 60)
18501859
return certificate
18511860

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-
18581861
def test_construction(self) -> None:
18591862
"""
18601863
`X509` takes no arguments and returns an instance of `X509`.
@@ -2081,7 +2084,9 @@ def test_digest(self) -> None:
20812084
)
20822085
)
20832086

2084-
def _extcert(self, key: _Key, extensions: list[x509.Extension]) -> X509:
2087+
def _extcert(
2088+
self, key: _PrivateKey, extensions: list[x509.ExtensionType]
2089+
) -> X509:
20852090
subject = x509.Name(
20862091
[x509.NameAttribute(x509.NameOID.COMMON_NAME, "Unit Tests")]
20872092
)
@@ -2108,6 +2113,7 @@ def test_extension_count(self) -> None:
21082113
pkey = load_privatekey(
21092114
FILETYPE_PEM, client_key_pem
21102115
).to_cryptography_key()
2116+
assert isinstance(pkey, rsa.RSAPrivateKey)
21112117
ca = x509.BasicConstraints(ca=False, path_length=None)
21122118
key = x509.KeyUsage(
21132119
digital_signature=True,
@@ -2142,6 +2148,7 @@ def test_get_extension(self) -> None:
21422148
pkey = load_privatekey(
21432149
FILETYPE_PEM, client_key_pem
21442150
).to_cryptography_key()
2151+
assert isinstance(pkey, rsa.RSAPrivateKey)
21452152
ca = x509.BasicConstraints(ca=False, path_length=None)
21462153
key = x509.KeyUsage(
21472154
digital_signature=True,
@@ -2370,7 +2377,7 @@ def test_convert_from_cryptography(self) -> None:
23702377

23712378
def test_convert_from_cryptography_unsupported_type(self) -> None:
23722379
with pytest.raises(TypeError):
2373-
X509.from_cryptography(object())
2380+
X509.from_cryptography(object()) # type: ignore[arg-type]
23742381

23752382
def test_convert_to_cryptography_key(self) -> None:
23762383
cert = load_certificate(FILETYPE_PEM, intermediate_cert_pem)
@@ -2385,12 +2392,6 @@ class TestX509Store:
23852392
Test for `OpenSSL.crypto.X509Store`.
23862393
"""
23872394

2388-
def test_type(self) -> None:
2389-
"""
2390-
`X509Store` is a type object.
2391-
"""
2392-
assert is_consistent_type(X509Store, "X509Store")
2393-
23942395
def test_add_cert(self) -> None:
23952396
"""
23962397
`X509Store.add_cert` adds a `X509` instance to the certificate store.
@@ -3015,8 +3016,10 @@ def _make_test_crl_cryptography(
30153016
# considered to have expired.
30163017
builder = builder.next_update(datetime(5000, 6, 1, 0, 0, 0))
30173018

3019+
key = issuer_key.to_cryptography_key()
3020+
assert isinstance(key, rsa.RSAPrivateKey)
30183021
crl = builder.sign(
3019-
private_key=issuer_key.to_cryptography_key(),
3022+
key,
30203023
algorithm=hashes.SHA512(),
30213024
)
30223025
return crl
@@ -3523,36 +3526,121 @@ def test_to_EC_KEY(self) -> None:
35233526
curve._to_EC_KEY()
35243527

35253528

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):
3529+
class TestEllipticCurveEquality:
35383530
"""
35393531
Tests `_EllipticCurve`'s implementation of ``==`` and ``!=``.
35403532
"""
35413533

3542-
curve_factory = EllipticCurveFactory()
3543-
3544-
def anInstance(self):
3534+
def anInstance(self) -> _EllipticCurve:
35453535
"""
35463536
Get the curve object for an arbitrary curve supported by the system.
35473537
"""
3548-
return get_elliptic_curve(self.curve_factory.curve_name)
3538+
return next(iter(get_elliptic_curves()))
35493539

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

35573645

35583646
class TestEllipticCurveHash:
@@ -3561,14 +3649,12 @@ class TestEllipticCurveHash:
35613649
an item in a `dict` or `set`).
35623650
"""
35633651

3564-
curve_factory = EllipticCurveFactory()
3565-
35663652
def test_contains(self) -> None:
35673653
"""
35683654
The ``in`` operator reports that a `set` containing a curve does
35693655
contain that curve.
35703656
"""
3571-
curve = get_elliptic_curve(self.curve_factory.curve_name)
3657+
curve = next(iter(get_elliptic_curves()))
35723658
curves = set([curve])
35733659
assert curve in curves
35743660

@@ -3577,8 +3663,8 @@ def test_does_not_contain(self) -> None:
35773663
The ``in`` operator reports that a `set` not containing a curve
35783664
does not contain that curve.
35793665
"""
3580-
curve = get_elliptic_curve(self.curve_factory.curve_name)
3581-
curves = set(
3582-
[get_elliptic_curve(self.curve_factory.another_curve_name)]
3583-
)
3666+
all_curves = list(get_elliptic_curves())
3667+
3668+
curve = all_curves[0]
3669+
curves = set([all_curves[1]])
35843670
assert curve not in curves

0 commit comments

Comments
 (0)