Skip to content

Commit f72218e

Browse files
crlorentzenCraig Lorentzen
andauthored
Add +/- 1 second tolerance for clock adjustments (#1481)
* Add +/- 1 second tolerance for clock adjustmens * Fix E501 Line too long (107 > 79) * simplify and standardize est_gmtime_adj * Fix utc_now() * fix: remove extra utcnow() call --------- Co-authored-by: Craig Lorentzen <crlorent@amazon.com>
1 parent 358cbf2 commit f72218e

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

tests/test_crypto.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,16 +1995,17 @@ def test_gmtime_adj_notBefore(self) -> None:
19951995
current time plus the number of seconds passed in.
19961996
"""
19971997
cert = load_certificate(FILETYPE_PEM, self.pemData)
1998-
not_before_min = utcnow().replace(microsecond=0) + timedelta(
1999-
seconds=100
2000-
)
1998+
utc_now = utcnow().replace(microsecond=0)
1999+
# -1 second tolerance for clock adjustments
2000+
not_before_min = utc_now + timedelta(seconds=99)
20012001
cert.gmtime_adj_notBefore(100)
20022002
not_before_str = cert.get_notBefore()
20032003
assert not_before_str is not None
20042004
not_before = datetime.strptime(
20052005
not_before_str.decode(), "%Y%m%d%H%M%SZ"
20062006
)
2007-
not_before_max = utcnow() + timedelta(seconds=100)
2007+
# +1 second tolerance for clock adjustments
2008+
not_before_max = utc_now + timedelta(seconds=101)
20082009
assert not_before_min <= not_before <= not_before_max
20092010

20102011
def test_gmtime_adj_notAfter_wrong_args(self) -> None:
@@ -2023,14 +2024,15 @@ def test_gmtime_adj_notAfter(self) -> None:
20232024
to be the current time plus the number of seconds passed in.
20242025
"""
20252026
cert = load_certificate(FILETYPE_PEM, self.pemData)
2026-
not_after_min = utcnow().replace(microsecond=0) + timedelta(
2027-
seconds=100
2028-
)
2027+
utc_now = utcnow().replace(microsecond=0)
2028+
# -1 second tolerance for clock adjustments
2029+
not_after_min = utc_now + timedelta(seconds=99)
20292030
cert.gmtime_adj_notAfter(100)
20302031
not_after_str = cert.get_notAfter()
20312032
assert not_after_str is not None
20322033
not_after = datetime.strptime(not_after_str.decode(), "%Y%m%d%H%M%SZ")
2033-
not_after_max = utcnow() + timedelta(seconds=100)
2034+
# +1 second tolerance for clock adjustments
2035+
not_after_max = utc_now + timedelta(seconds=101)
20342036
assert not_after_min <= not_after <= not_after_max
20352037

20362038
def test_has_expired(self) -> None:

0 commit comments

Comments
 (0)