42
42
X509StoreContext ,
43
43
X509StoreContextError ,
44
44
X509StoreFlags ,
45
+ _EllipticCurve ,
45
46
_Key ,
47
+ _PrivateKey ,
46
48
dump_certificate ,
47
49
dump_certificate_request ,
48
50
dump_privatekey ,
62
64
63
65
from .util import (
64
66
NON_ASCII ,
65
- EqualityTestsMixin ,
66
- is_consistent_type ,
67
67
)
68
68
69
69
@@ -896,18 +896,6 @@ def test_str(self) -> None:
896
896
== "CA:FALSE"
897
897
)
898
898
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
-
911
899
def test_construction (self ) -> None :
912
900
"""
913
901
`X509Extension` accepts an extension type name, a critical flag,
@@ -1107,11 +1095,27 @@ def test_convert_roundtrip_cryptography_private_key(
1107
1095
"""
1108
1096
key = serialization .load_pem_private_key (key_string , None )
1109
1097
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
+ )
1110
1106
pkey = PKey .from_cryptography_key (key )
1111
1107
1112
1108
assert isinstance (pkey , PKey )
1113
1109
parsed_key = pkey .to_cryptography_key ()
1114
1110
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
+ )
1115
1119
assert parsed_key .public_key ().public_bytes (
1116
1120
serialization .Encoding .PEM ,
1117
1121
serialization .PublicFormat .SubjectPublicKeyInfo ,
@@ -1141,11 +1145,27 @@ def test_convert_roundtrip_cryptography_public_key(
1141
1145
"""
1142
1146
key = serialization .load_pem_public_key (key_string , None )
1143
1147
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
+ )
1144
1156
pkey = PKey .from_cryptography_key (key )
1145
1157
1146
1158
assert isinstance (pkey , PKey )
1147
1159
parsed_key = pkey .to_cryptography_key ()
1148
1160
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
+ )
1149
1169
assert parsed_key .public_bytes (
1150
1170
serialization .Encoding .PEM ,
1151
1171
serialization .PublicFormat .SubjectPublicKeyInfo ,
@@ -1161,6 +1181,7 @@ def test_convert_from_cryptography_public_key(self) -> None:
1161
1181
PKey.from_cryptography_key creates a proper public PKey.
1162
1182
"""
1163
1183
key = serialization .load_pem_public_key (cleartextPublicKeyPEM )
1184
+ assert isinstance (key , rsa .RSAPublicKey )
1164
1185
pkey = PKey .from_cryptography_key (key )
1165
1186
1166
1187
assert isinstance (pkey , PKey )
@@ -1174,7 +1195,7 @@ def test_convert_from_cryptography_unsupported_type(self) -> None:
1174
1195
"""
1175
1196
key = serialization .load_pem_private_key (x25519_private_key_pem , None )
1176
1197
with pytest .raises (TypeError ):
1177
- PKey .from_cryptography_key (key )
1198
+ PKey .from_cryptography_key (key ) # type: ignore[arg-type]
1178
1199
1179
1200
def test_convert_public_pkey_to_cryptography_key (self ) -> None :
1180
1201
"""
@@ -1186,12 +1207,6 @@ def test_convert_public_pkey_to_cryptography_key(self) -> None:
1186
1207
assert isinstance (key , rsa .RSAPublicKey )
1187
1208
assert pkey .bits () == key .key_size
1188
1209
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
-
1195
1210
def test_construction (self ) -> None :
1196
1211
"""
1197
1212
`PKey` takes no arguments and returns a new `PKey` instance.
@@ -1659,12 +1674,6 @@ def signable(self):
1659
1674
"""
1660
1675
return X509Req ()
1661
1676
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
-
1668
1677
def test_construction (self ) -> None :
1669
1678
"""
1670
1679
`X509Req` takes no arguments and returns an `X509Req` instance.
@@ -1821,7 +1830,7 @@ def test_convert_from_cryptography(self) -> None:
1821
1830
1822
1831
def test_convert_from_cryptography_unsupported_type (self ) -> None :
1823
1832
with pytest .raises (TypeError ):
1824
- X509Req .from_cryptography (object ())
1833
+ X509Req .from_cryptography (object ()) # type: ignore[arg-type]
1825
1834
1826
1835
def test_convert_to_cryptography_key (self ) -> None :
1827
1836
req = load_certificate_request (
@@ -1849,12 +1858,6 @@ def signable(self):
1849
1858
certificate .gmtime_adj_notAfter (24 * 60 * 60 )
1850
1859
return certificate
1851
1860
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
-
1858
1861
def test_construction (self ) -> None :
1859
1862
"""
1860
1863
`X509` takes no arguments and returns an instance of `X509`.
@@ -2081,7 +2084,9 @@ def test_digest(self) -> None:
2081
2084
)
2082
2085
)
2083
2086
2084
- def _extcert (self , key : _Key , extensions : list [x509 .Extension ]) -> X509 :
2087
+ def _extcert (
2088
+ self , key : _PrivateKey , extensions : list [x509 .ExtensionType ]
2089
+ ) -> X509 :
2085
2090
subject = x509 .Name (
2086
2091
[x509 .NameAttribute (x509 .NameOID .COMMON_NAME , "Unit Tests" )]
2087
2092
)
@@ -2108,6 +2113,7 @@ def test_extension_count(self) -> None:
2108
2113
pkey = load_privatekey (
2109
2114
FILETYPE_PEM , client_key_pem
2110
2115
).to_cryptography_key ()
2116
+ assert isinstance (pkey , rsa .RSAPrivateKey )
2111
2117
ca = x509 .BasicConstraints (ca = False , path_length = None )
2112
2118
key = x509 .KeyUsage (
2113
2119
digital_signature = True ,
@@ -2142,6 +2148,7 @@ def test_get_extension(self) -> None:
2142
2148
pkey = load_privatekey (
2143
2149
FILETYPE_PEM , client_key_pem
2144
2150
).to_cryptography_key ()
2151
+ assert isinstance (pkey , rsa .RSAPrivateKey )
2145
2152
ca = x509 .BasicConstraints (ca = False , path_length = None )
2146
2153
key = x509 .KeyUsage (
2147
2154
digital_signature = True ,
@@ -2370,7 +2377,7 @@ def test_convert_from_cryptography(self) -> None:
2370
2377
2371
2378
def test_convert_from_cryptography_unsupported_type (self ) -> None :
2372
2379
with pytest .raises (TypeError ):
2373
- X509 .from_cryptography (object ())
2380
+ X509 .from_cryptography (object ()) # type: ignore[arg-type]
2374
2381
2375
2382
def test_convert_to_cryptography_key (self ) -> None :
2376
2383
cert = load_certificate (FILETYPE_PEM , intermediate_cert_pem )
@@ -2385,12 +2392,6 @@ class TestX509Store:
2385
2392
Test for `OpenSSL.crypto.X509Store`.
2386
2393
"""
2387
2394
2388
- def test_type (self ) -> None :
2389
- """
2390
- `X509Store` is a type object.
2391
- """
2392
- assert is_consistent_type (X509Store , "X509Store" )
2393
-
2394
2395
def test_add_cert (self ) -> None :
2395
2396
"""
2396
2397
`X509Store.add_cert` adds a `X509` instance to the certificate store.
@@ -3015,8 +3016,10 @@ def _make_test_crl_cryptography(
3015
3016
# considered to have expired.
3016
3017
builder = builder .next_update (datetime (5000 , 6 , 1 , 0 , 0 , 0 ))
3017
3018
3019
+ key = issuer_key .to_cryptography_key ()
3020
+ assert isinstance (key , rsa .RSAPrivateKey )
3018
3021
crl = builder .sign (
3019
- private_key = issuer_key . to_cryptography_key () ,
3022
+ key ,
3020
3023
algorithm = hashes .SHA512 (),
3021
3024
)
3022
3025
return crl
@@ -3523,36 +3526,121 @@ def test_to_EC_KEY(self) -> None:
3523
3526
curve ._to_EC_KEY ()
3524
3527
3525
3528
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 :
3538
3530
"""
3539
3531
Tests `_EllipticCurve`'s implementation of ``==`` and ``!=``.
3540
3532
"""
3541
3533
3542
- curve_factory = EllipticCurveFactory ()
3543
-
3544
- def anInstance (self ):
3534
+ def anInstance (self ) -> _EllipticCurve :
3545
3535
"""
3546
3536
Get the curve object for an arbitrary curve supported by the system.
3547
3537
"""
3548
- return get_elliptic_curve ( self . curve_factory . curve_name )
3538
+ return next ( iter ( get_elliptic_curves ()) )
3549
3539
3550
- def anotherInstance (self ):
3540
+ def anotherInstance (self ) -> _EllipticCurve :
3551
3541
"""
3552
3542
Get the curve object for an arbitrary curve supported by the system -
3553
3543
but not the one returned by C{anInstance}.
3554
3544
"""
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]
3556
3644
3557
3645
3558
3646
class TestEllipticCurveHash :
@@ -3561,14 +3649,12 @@ class TestEllipticCurveHash:
3561
3649
an item in a `dict` or `set`).
3562
3650
"""
3563
3651
3564
- curve_factory = EllipticCurveFactory ()
3565
-
3566
3652
def test_contains (self ) -> None :
3567
3653
"""
3568
3654
The ``in`` operator reports that a `set` containing a curve does
3569
3655
contain that curve.
3570
3656
"""
3571
- curve = get_elliptic_curve ( self . curve_factory . curve_name )
3657
+ curve = next ( iter ( get_elliptic_curves ()) )
3572
3658
curves = set ([curve ])
3573
3659
assert curve in curves
3574
3660
@@ -3577,8 +3663,8 @@ def test_does_not_contain(self) -> None:
3577
3663
The ``in`` operator reports that a `set` not containing a curve
3578
3664
does not contain that curve.
3579
3665
"""
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 ]] )
3584
3670
assert curve not in curves
0 commit comments