Skip to content

Commit 592553c

Browse files
fix: assert on exact values
1 parent aea508b commit 592553c

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

src/cryptography/x509/extensions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,8 +1271,8 @@ class PrivateKeyUsagePeriod(ExtensionType):
12711271

12721272
def __init__(
12731273
self,
1274-
not_before: datetime.datetime | None = None,
1275-
not_after: datetime.datetime | None = None,
1274+
not_before: datetime.datetime | None,
1275+
not_after: datetime.datetime | None,
12761276
) -> None:
12771277
if (
12781278
not isinstance(not_before, datetime.datetime)

tests/x509/test_x509_ext.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,21 +1878,16 @@ def test_key_cert_sign_crl_sign(self, backend):
18781878
class TestPrivateKeyUsagePeriodExtension:
18791879
def test_not_validity(self):
18801880
with pytest.raises(TypeError):
1881-
x509.PrivateKeyUsagePeriod("notvalidity") # type:ignore[arg-type]
1881+
x509.PrivateKeyUsagePeriod("notValidBefore", "notValidAfter") # type:ignore[arg-type]
18821882

18831883
def test_repr(self):
18841884
period = x509.PrivateKeyUsagePeriod(
18851885
not_before=datetime.datetime(2012, 1, 1),
18861886
not_after=datetime.datetime(2013, 1, 1),
18871887
)
1888-
ext = x509.Extension(
1889-
ExtensionOID.PRIVATE_KEY_USAGE_PERIOD, False, period
1890-
)
1891-
assert repr(ext) == (
1892-
"<Extension(oid=<ObjectIdentifier(oid=2.5.29.16, "
1893-
"name=privateKeyUsagePeriod)>, critical=False, "
1894-
"value=<PrivateKeyUsagePeriod(not_before=2012-01-01 00:00:00, "
1895-
"not_after=2013-01-01 00:00:00)>)>"
1888+
assert repr(period) == (
1889+
"<PrivateKeyUsagePeriod(not_before=2012-01-01 00:00:00, "
1890+
"not_after=2013-01-01 00:00:00)>"
18961891
)
18971892

18981893
def test_eq(self):
@@ -1963,7 +1958,12 @@ def test_public_bytes(self):
19631958
not_after=datetime.datetime(2013, 1, 1, 0, 0, 0),
19641959
)
19651960
serialized = period.public_bytes()
1966-
assert serialized.startswith(b"\x30")
1961+
1962+
assert serialized == (
1963+
b"\x30\x22\x80\x0f\x32\x30\x31\x32\x30\x31\x30\x31\x30"
1964+
b"\x30\x30\x30\x30\x30\x5a\x81\x0f\x32\x30\x31\x33\x30"
1965+
b"\x31\x30\x31\x30\x30\x30\x30\x30\x30\x5a"
1966+
)
19671967

19681968
def test_only_not_before(self):
19691969
period = x509.PrivateKeyUsagePeriod(
@@ -1974,7 +1974,11 @@ def test_only_not_before(self):
19741974
assert period.not_after is None
19751975

19761976
serialized = period.public_bytes()
1977-
assert serialized.startswith(b"\x30")
1977+
1978+
assert serialized == (
1979+
b"\x30\x11\x80\x0f\x32\x30\x31\x32\x30\x31\x30\x31\x30"
1980+
b"\x30\x30\x30\x30\x30\x5a"
1981+
)
19781982

19791983
def test_only_not_after(self):
19801984
period = x509.PrivateKeyUsagePeriod(
@@ -1985,7 +1989,11 @@ def test_only_not_after(self):
19851989
assert period.not_after == datetime.datetime(2013, 1, 1)
19861990

19871991
serialized = period.public_bytes()
1988-
assert serialized.startswith(b"\x30")
1992+
1993+
assert serialized == (
1994+
b"\x30\x11\x81\x0f\x32\x30\x31\x33\x30\x31\x30\x31\x30"
1995+
b"\x30\x30\x30\x30\x30\x5a"
1996+
)
19891997

19901998
def test_load_pem_certificate_with_extension(self, backend):
19911999
cert_path = os.path.join(
@@ -1999,11 +2007,12 @@ def test_load_pem_certificate_with_extension(self, backend):
19992007
ext = cert.extensions.get_extension_for_class(
20002008
x509.PrivateKeyUsagePeriod
20012009
)
2002-
assert ext is not None
20032010
assert ext.critical is False
20042011

2005-
assert ext.value.not_before is not None
2006-
assert ext.value.not_after is not None
2012+
assert ext.value.not_before == datetime.datetime(2024, 1, 1, 0, 0)
2013+
assert ext.value.not_after == datetime.datetime(
2014+
2024, 12, 31, 23, 59, 59
2015+
)
20072016

20082017
def test_load_pem_only_not_before(self, backend):
20092018
cert_path = os.path.join(
@@ -2017,9 +2026,8 @@ def test_load_pem_only_not_before(self, backend):
20172026
ext = cert.extensions.get_extension_for_class(
20182027
x509.PrivateKeyUsagePeriod
20192028
)
2020-
assert ext is not None
20212029

2022-
assert ext.value.not_before is not None
2030+
assert ext.value.not_before == datetime.datetime(2024, 1, 1, 0, 0)
20232031
assert ext.value.not_after is None
20242032

20252033
def test_load_pem_only_not_after(self, backend):
@@ -2034,10 +2042,11 @@ def test_load_pem_only_not_after(self, backend):
20342042
ext = cert.extensions.get_extension_for_class(
20352043
x509.PrivateKeyUsagePeriod
20362044
)
2037-
assert ext is not None
20382045

20392046
assert ext.value.not_before is None
2040-
assert ext.value.not_after is not None
2047+
assert ext.value.not_after == datetime.datetime(
2048+
2024, 12, 31, 23, 59, 59
2049+
)
20412050

20422051
def test_certificate_builder_with_extension(self, backend):
20432052
private_key = rsa.generate_private_key(
@@ -2080,7 +2089,7 @@ def test_certificate_builder_with_extension(self, backend):
20802089
ext = certificate.extensions.get_extension_for_class(
20812090
x509.PrivateKeyUsagePeriod
20822091
)
2083-
assert ext is not None
2092+
20842093
assert ext.critical is True
20852094
assert ext.value.not_before == datetime.datetime(2012, 1, 1)
20862095
assert ext.value.not_after == datetime.datetime(2013, 1, 1)

0 commit comments

Comments
 (0)