@@ -1605,22 +1605,16 @@ def test_set_attribute_failure(self) -> None:
1605
1605
setattr (name , "O" , b"x" * 512 )
1606
1606
1607
1607
1608
- class _PKeyInteractionTestsMixin :
1608
+ class TestX509Req :
1609
1609
"""
1610
- Tests which involve another thing and a PKey .
1610
+ Tests for `OpenSSL.crypto.X509Req` .
1611
1611
"""
1612
1612
1613
- def signable (self ):
1614
- """
1615
- Return something with `set_pubkey` and `sign` methods.
1616
- """
1617
- raise NotImplementedError ()
1618
-
1619
1613
def test_sign_with_ungenerated (self ) -> None :
1620
1614
"""
1621
1615
`X509Req.sign` raises `ValueError` when passed a `PKey` with no parts.
1622
1616
"""
1623
- request = self . signable ()
1617
+ request = X509Req ()
1624
1618
key = PKey ()
1625
1619
with pytest .raises (ValueError ):
1626
1620
request .sign (key , GOOD_DIGEST )
@@ -1630,7 +1624,7 @@ def test_sign_with_public_key(self) -> None:
1630
1624
`X509Req.sign` raises `ValueError` when passed a `PKey` with no private
1631
1625
part as the signing key.
1632
1626
"""
1633
- request = self . signable ()
1627
+ request = X509Req ()
1634
1628
key = PKey ()
1635
1629
key .generate_key (TYPE_RSA , 2048 )
1636
1630
request .set_pubkey (key )
@@ -1643,7 +1637,7 @@ def test_sign_with_unknown_digest(self) -> None:
1643
1637
`X509Req.sign` raises `ValueError` when passed a digest name which is
1644
1638
not known.
1645
1639
"""
1646
- request = self . signable ()
1640
+ request = X509Req ()
1647
1641
key = PKey ()
1648
1642
key .generate_key (TYPE_RSA , 2048 )
1649
1643
with pytest .raises (ValueError ):
@@ -1655,7 +1649,7 @@ def test_sign(self) -> None:
1655
1649
valid digest function. `X509Req.verify` can be used to check
1656
1650
the signature.
1657
1651
"""
1658
- request = self . signable ()
1652
+ request = X509Req ()
1659
1653
key = PKey ()
1660
1654
key .generate_key (TYPE_RSA , 2048 )
1661
1655
request .set_pubkey (key )
@@ -1670,18 +1664,6 @@ def test_sign(self) -> None:
1670
1664
with pytest .raises (Error ):
1671
1665
request .verify (key )
1672
1666
1673
-
1674
- class TestX509Req (_PKeyInteractionTestsMixin ):
1675
- """
1676
- Tests for `OpenSSL.crypto.X509Req`.
1677
- """
1678
-
1679
- def signable (self ):
1680
- """
1681
- Create and return a new `X509Req`.
1682
- """
1683
- return X509Req ()
1684
-
1685
1667
def test_construction (self ) -> None :
1686
1668
"""
1687
1669
`X509Req` takes no arguments and returns an `X509Req` instance.
@@ -1848,23 +1830,57 @@ def test_convert_to_cryptography_key(self) -> None:
1848
1830
assert isinstance (crypto_req , x509 .CertificateSigningRequest )
1849
1831
1850
1832
1851
- class TestX509 ( _PKeyInteractionTestsMixin ) :
1833
+ class TestX509 :
1852
1834
"""
1853
1835
Tests for `OpenSSL.crypto.X509`.
1854
1836
"""
1855
1837
1856
1838
pemData = root_cert_pem + root_key_pem
1857
1839
1858
- def signable (self ):
1840
+ def test_sign_with_ungenerated (self ) -> None :
1841
+ """
1842
+ `X509.sign` raises `ValueError` when passed a `PKey` with no parts.
1843
+ """
1844
+ cert = X509 ()
1845
+ key = PKey ()
1846
+ with pytest .raises (ValueError ):
1847
+ cert .sign (key , GOOD_DIGEST )
1848
+
1849
+ def test_sign_with_public_key (self ) -> None :
1859
1850
"""
1860
- Create and return a new `X509`.
1851
+ `X509.sign` raises `ValueError` when passed a `PKey` with no private
1852
+ part as the signing key.
1861
1853
"""
1862
- certificate = X509 ()
1863
- # Fill in placeholder validity values. signable only expects to call
1864
- # set_pubkey and sign.
1865
- certificate .gmtime_adj_notBefore (- 24 * 60 * 60 )
1866
- certificate .gmtime_adj_notAfter (24 * 60 * 60 )
1867
- return certificate
1854
+ cert = X509 ()
1855
+ key = PKey ()
1856
+ key .generate_key (TYPE_RSA , 2048 )
1857
+ cert .set_pubkey (key )
1858
+ pub = cert .get_pubkey ()
1859
+ with pytest .raises (ValueError ):
1860
+ cert .sign (pub , GOOD_DIGEST )
1861
+
1862
+ def test_sign_with_unknown_digest (self ) -> None :
1863
+ """
1864
+ `X509.sign` raises `ValueError` when passed a digest name which is
1865
+ not known.
1866
+ """
1867
+ cert = X509 ()
1868
+ key = PKey ()
1869
+ key .generate_key (TYPE_RSA , 2048 )
1870
+ with pytest .raises (ValueError ):
1871
+ cert .sign (key , BAD_DIGEST )
1872
+
1873
+ def test_sign (self ) -> None :
1874
+ """
1875
+ `X509.sign` succeeds when passed a private key object and a
1876
+ valid digest function. `X509Req.verify` can be used to check
1877
+ the signature.
1878
+ """
1879
+ cert = X509 ()
1880
+ key = PKey ()
1881
+ key .generate_key (TYPE_RSA , 2048 )
1882
+ cert .set_pubkey (key )
1883
+ cert .sign (key , GOOD_DIGEST )
1868
1884
1869
1885
def test_construction (self ) -> None :
1870
1886
"""
@@ -1912,63 +1928,64 @@ def test_serial_number(self) -> None:
1912
1928
certificate .set_serial_number (2 ** 128 + 1 )
1913
1929
assert certificate .get_serial_number () == 2 ** 128 + 1
1914
1930
1915
- def _setBoundTest (self , which ):
1931
+ def _setBoundTest (
1932
+ self ,
1933
+ get : typing .Callable [[X509 ], bytes | None ],
1934
+ set : typing .Callable [[X509 , bytes ], None ],
1935
+ ) -> None :
1916
1936
"""
1917
1937
`X509.set_notBefore` takes a string in the format of an
1918
1938
ASN1 GENERALIZEDTIME and sets the beginning of the certificate's
1919
1939
validity period to it.
1920
1940
"""
1921
1941
certificate = X509 ()
1922
- set = getattr (certificate , "set_not" + which )
1923
- get = getattr (certificate , "get_not" + which )
1924
1942
1925
1943
# Starts with no value.
1926
- assert get () is None
1944
+ assert get (certificate ) is None
1927
1945
1928
1946
# GMT (Or is it UTC?) -exarkun
1929
1947
when = b"20040203040506Z"
1930
- set (when )
1931
- assert get () == when
1948
+ set (certificate , when )
1949
+ assert get (certificate ) == when
1932
1950
1933
1951
# A plus two hours and thirty minutes offset
1934
1952
when = b"20040203040506+0530"
1935
- set (when )
1936
- assert get () == when
1953
+ set (certificate , when )
1954
+ assert get (certificate ) == when
1937
1955
1938
1956
# A minus one hour fifteen minutes offset
1939
1957
when = b"20040203040506-0115"
1940
- set (when )
1941
- assert get () == when
1958
+ set (certificate , when )
1959
+ assert (
1960
+ get (
1961
+ certificate ,
1962
+ )
1963
+ == when
1964
+ )
1942
1965
1943
1966
# An invalid string results in a ValueError
1944
1967
with pytest .raises (ValueError ):
1945
- set (b"foo bar" )
1946
-
1947
- # The wrong number of arguments results in a TypeError.
1948
- with pytest .raises (TypeError ):
1949
- set ()
1950
- with pytest .raises (TypeError ):
1951
- set (b"20040203040506Z" , b"20040203040506Z" )
1952
- with pytest .raises (TypeError ):
1953
- get (b"foo bar" )
1954
-
1955
- # XXX ASN1_TIME (not GENERALIZEDTIME)
1968
+ set (certificate , b"foo bar" )
1956
1969
1957
1970
def test_set_notBefore (self ) -> None :
1958
1971
"""
1959
1972
`X509.set_notBefore` takes a string in the format of an
1960
1973
ASN1 GENERALIZEDTIME and sets the beginning of the certificate's
1961
1974
validity period to it.
1962
1975
"""
1963
- self ._setBoundTest ("Before" )
1976
+ self ._setBoundTest (
1977
+ lambda c : c .get_notBefore (), lambda c , v : c .set_notBefore (v )
1978
+ )
1964
1979
1965
1980
def test_set_notAfter (self ) -> None :
1966
1981
"""
1967
1982
`X509.set_notAfter` takes a string in the format of an ASN1
1968
1983
GENERALIZEDTIME and sets the end of the certificate's validity period
1969
1984
to it.
1970
1985
"""
1971
- self ._setBoundTest ("After" )
1986
+ self ._setBoundTest (
1987
+ lambda c : c .get_notAfter (), lambda c , v : c .set_notAfter (v )
1988
+ )
1972
1989
1973
1990
def test_get_notBefore (self ) -> None :
1974
1991
"""
@@ -2463,7 +2480,7 @@ def test_load_locations_parameters(
2463
2480
capath : str | bytes | None ,
2464
2481
call_cafile : object ,
2465
2482
call_capath : object ,
2466
- monkeypatch ,
2483
+ monkeypatch : pytest . MonkeyPatch ,
2467
2484
) -> None :
2468
2485
class LibMock :
2469
2486
def load_locations (
@@ -3244,7 +3261,7 @@ def test_untrusted_chain_wrong_args(
3244
3261
X509StoreContext (store , self .intermediate_server_cert , chain = chain )
3245
3262
3246
3263
def test_failure_building_untrusted_chain_raises (
3247
- self , monkeypatch
3264
+ self , monkeypatch : pytest . MonkeyPatch
3248
3265
) -> None :
3249
3266
"""
3250
3267
Creating ``X509StoreContext`` raises ``OpenSSL.crypto.Error`` when
@@ -3625,30 +3642,36 @@ def test_delegatedEq(self) -> None:
3625
3642
The result of comparison using C{==} is delegated to the right-hand
3626
3643
operand if it is of an unrelated type.
3627
3644
"""
3645
+ called = False
3628
3646
3629
3647
class Delegate :
3630
- def __eq__ (self , other ):
3631
- # Do something crazy and obvious.
3632
- return [self ]
3648
+ def __eq__ (self , other : object ) -> bool :
3649
+ nonlocal called
3650
+ called = True
3651
+ return False
3633
3652
3634
3653
a = self .anInstance ()
3635
3654
b = Delegate ()
3636
- assert (a == b ) == [b ] # type: ignore[comparison-overlap]
3655
+ assert not (a == b )
3656
+ assert called
3637
3657
3638
3658
def test_delegateNe (self ) -> None :
3639
3659
"""
3640
3660
The result of comparison using C{!=} is delegated to the right-hand
3641
3661
operand if it is of an unrelated type.
3642
3662
"""
3663
+ called = False
3643
3664
3644
3665
class Delegate :
3645
- def __ne__ (self , other ):
3646
- # Do something crazy and obvious.
3647
- return [self ]
3666
+ def __ne__ (self , other : object ) -> bool :
3667
+ nonlocal called
3668
+ called = True
3669
+ return False
3648
3670
3649
3671
a = self .anInstance ()
3650
3672
b = Delegate ()
3651
- assert (a != b ) == [b ] # type: ignore[comparison-overlap]
3673
+ assert not (a != b ) # type: ignore[comparison-overlap]
3674
+ assert called
3652
3675
3653
3676
3654
3677
class TestEllipticCurveHash :
0 commit comments