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,31 @@ 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
+ (
1101
+ dsa .DSAPrivateKey ,
1102
+ ec .EllipticCurvePrivateKey ,
1103
+ ed25519 .Ed25519PrivateKey ,
1104
+ ed448 .Ed448PrivateKey ,
1105
+ rsa .RSAPrivateKey ,
1106
+ ),
1107
+ )
1110
1108
pkey = PKey .from_cryptography_key (key )
1111
1109
1112
1110
assert isinstance (pkey , PKey )
1113
1111
parsed_key = pkey .to_cryptography_key ()
1114
1112
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
+ )
1115
1123
assert parsed_key .public_key ().public_bytes (
1116
1124
serialization .Encoding .PEM ,
1117
1125
serialization .PublicFormat .SubjectPublicKeyInfo ,
@@ -1141,11 +1149,31 @@ def test_convert_roundtrip_cryptography_public_key(
1141
1149
"""
1142
1150
key = serialization .load_pem_public_key (key_string , None )
1143
1151
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
+ )
1144
1162
pkey = PKey .from_cryptography_key (key )
1145
1163
1146
1164
assert isinstance (pkey , PKey )
1147
1165
parsed_key = pkey .to_cryptography_key ()
1148
1166
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
+ )
1149
1177
assert parsed_key .public_bytes (
1150
1178
serialization .Encoding .PEM ,
1151
1179
serialization .PublicFormat .SubjectPublicKeyInfo ,
@@ -1161,6 +1189,7 @@ def test_convert_from_cryptography_public_key(self) -> None:
1161
1189
PKey.from_cryptography_key creates a proper public PKey.
1162
1190
"""
1163
1191
key = serialization .load_pem_public_key (cleartextPublicKeyPEM )
1192
+ assert isinstance (key , rsa .RSAPublicKey )
1164
1193
pkey = PKey .from_cryptography_key (key )
1165
1194
1166
1195
assert isinstance (pkey , PKey )
@@ -1174,7 +1203,7 @@ def test_convert_from_cryptography_unsupported_type(self) -> None:
1174
1203
"""
1175
1204
key = serialization .load_pem_private_key (x25519_private_key_pem , None )
1176
1205
with pytest .raises (TypeError ):
1177
- PKey .from_cryptography_key (key )
1206
+ PKey .from_cryptography_key (key ) # type: ignore[arg-type]
1178
1207
1179
1208
def test_convert_public_pkey_to_cryptography_key (self ) -> None :
1180
1209
"""
@@ -1186,12 +1215,6 @@ def test_convert_public_pkey_to_cryptography_key(self) -> None:
1186
1215
assert isinstance (key , rsa .RSAPublicKey )
1187
1216
assert pkey .bits () == key .key_size
1188
1217
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
1218
def test_construction (self ) -> None :
1196
1219
"""
1197
1220
`PKey` takes no arguments and returns a new `PKey` instance.
@@ -1659,12 +1682,6 @@ def signable(self):
1659
1682
"""
1660
1683
return X509Req ()
1661
1684
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
1685
def test_construction (self ) -> None :
1669
1686
"""
1670
1687
`X509Req` takes no arguments and returns an `X509Req` instance.
@@ -1821,7 +1838,7 @@ def test_convert_from_cryptography(self) -> None:
1821
1838
1822
1839
def test_convert_from_cryptography_unsupported_type (self ) -> None :
1823
1840
with pytest .raises (TypeError ):
1824
- X509Req .from_cryptography (object ())
1841
+ X509Req .from_cryptography (object ()) # type: ignore[arg-type]
1825
1842
1826
1843
def test_convert_to_cryptography_key (self ) -> None :
1827
1844
req = load_certificate_request (
@@ -1849,12 +1866,6 @@ def signable(self):
1849
1866
certificate .gmtime_adj_notAfter (24 * 60 * 60 )
1850
1867
return certificate
1851
1868
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
1869
def test_construction (self ) -> None :
1859
1870
"""
1860
1871
`X509` takes no arguments and returns an instance of `X509`.
@@ -2081,7 +2092,9 @@ def test_digest(self) -> None:
2081
2092
)
2082
2093
)
2083
2094
2084
- def _extcert (self , key : _Key , extensions : list [x509 .Extension ]) -> X509 :
2095
+ def _extcert (
2096
+ self , key : _PrivateKey , extensions : list [x509 .ExtensionType ]
2097
+ ) -> X509 :
2085
2098
subject = x509 .Name (
2086
2099
[x509 .NameAttribute (x509 .NameOID .COMMON_NAME , "Unit Tests" )]
2087
2100
)
@@ -2108,6 +2121,7 @@ def test_extension_count(self) -> None:
2108
2121
pkey = load_privatekey (
2109
2122
FILETYPE_PEM , client_key_pem
2110
2123
).to_cryptography_key ()
2124
+ assert isinstance (pkey , rsa .RSAPrivateKey )
2111
2125
ca = x509 .BasicConstraints (ca = False , path_length = None )
2112
2126
key = x509 .KeyUsage (
2113
2127
digital_signature = True ,
@@ -2142,6 +2156,7 @@ def test_get_extension(self) -> None:
2142
2156
pkey = load_privatekey (
2143
2157
FILETYPE_PEM , client_key_pem
2144
2158
).to_cryptography_key ()
2159
+ assert isinstance (pkey , rsa .RSAPrivateKey )
2145
2160
ca = x509 .BasicConstraints (ca = False , path_length = None )
2146
2161
key = x509 .KeyUsage (
2147
2162
digital_signature = True ,
@@ -2370,7 +2385,7 @@ def test_convert_from_cryptography(self) -> None:
2370
2385
2371
2386
def test_convert_from_cryptography_unsupported_type (self ) -> None :
2372
2387
with pytest .raises (TypeError ):
2373
- X509 .from_cryptography (object ())
2388
+ X509 .from_cryptography (object ()) # type: ignore[arg-type]
2374
2389
2375
2390
def test_convert_to_cryptography_key (self ) -> None :
2376
2391
cert = load_certificate (FILETYPE_PEM , intermediate_cert_pem )
@@ -2385,12 +2400,6 @@ class TestX509Store:
2385
2400
Test for `OpenSSL.crypto.X509Store`.
2386
2401
"""
2387
2402
2388
- def test_type (self ) -> None :
2389
- """
2390
- `X509Store` is a type object.
2391
- """
2392
- assert is_consistent_type (X509Store , "X509Store" )
2393
-
2394
2403
def test_add_cert (self ) -> None :
2395
2404
"""
2396
2405
`X509Store.add_cert` adds a `X509` instance to the certificate store.
@@ -3015,8 +3024,10 @@ def _make_test_crl_cryptography(
3015
3024
# considered to have expired.
3016
3025
builder = builder .next_update (datetime (5000 , 6 , 1 , 0 , 0 , 0 ))
3017
3026
3027
+ key = issuer_key .to_cryptography_key ()
3028
+ assert isinstance (key , rsa .RSAPrivateKey )
3018
3029
crl = builder .sign (
3019
- private_key = issuer_key . to_cryptography_key () ,
3030
+ key ,
3020
3031
algorithm = hashes .SHA512 (),
3021
3032
)
3022
3033
return crl
@@ -3523,36 +3534,121 @@ def test_to_EC_KEY(self) -> None:
3523
3534
curve ._to_EC_KEY ()
3524
3535
3525
3536
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 :
3538
3538
"""
3539
3539
Tests `_EllipticCurve`'s implementation of ``==`` and ``!=``.
3540
3540
"""
3541
3541
3542
- curve_factory = EllipticCurveFactory ()
3543
-
3544
- def anInstance (self ):
3542
+ def anInstance (self ) -> _EllipticCurve :
3545
3543
"""
3546
3544
Get the curve object for an arbitrary curve supported by the system.
3547
3545
"""
3548
- return get_elliptic_curve ( self . curve_factory . curve_name )
3546
+ return next ( iter ( get_elliptic_curves ()) )
3549
3547
3550
- def anotherInstance (self ):
3548
+ def anotherInstance (self ) -> _EllipticCurve :
3551
3549
"""
3552
3550
Get the curve object for an arbitrary curve supported by the system -
3553
3551
but not the one returned by C{anInstance}.
3554
3552
"""
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]
3556
3652
3557
3653
3558
3654
class TestEllipticCurveHash :
@@ -3561,14 +3657,12 @@ class TestEllipticCurveHash:
3561
3657
an item in a `dict` or `set`).
3562
3658
"""
3563
3659
3564
- curve_factory = EllipticCurveFactory ()
3565
-
3566
3660
def test_contains (self ) -> None :
3567
3661
"""
3568
3662
The ``in`` operator reports that a `set` containing a curve does
3569
3663
contain that curve.
3570
3664
"""
3571
- curve = get_elliptic_curve ( self . curve_factory . curve_name )
3665
+ curve = next ( iter ( get_elliptic_curves ()) )
3572
3666
curves = set ([curve ])
3573
3667
assert curve in curves
3574
3668
@@ -3577,8 +3671,8 @@ def test_does_not_contain(self) -> None:
3577
3671
The ``in`` operator reports that a `set` not containing a curve
3578
3672
does not contain that curve.
3579
3673
"""
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 ]] )
3584
3678
assert curve not in curves
0 commit comments