55
66import datetime
77import os .path
8+ from datetime import timezone
89from unittest import mock
910
1011import aiohttp
@@ -89,7 +90,7 @@ def cache():
8990def now_fixed ():
9091 with mock .patch ("autograph_utils._now" ) as m :
9192 # A common static time used in a lot of tests.
92- m .return_value = datetime .datetime (2019 , 10 , 23 , 16 , 16 )
93+ m .return_value = datetime .datetime (2019 , 10 , 23 , 16 , 16 , tzinfo = timezone . utc )
9394 # Yield the mock so someone can change the time if they want
9495 yield m
9596
@@ -107,8 +108,8 @@ def mock_cert(real_cert):
107108 """
108109
109110 mock_cert = mock .MagicMock (wraps = real_cert )
110- mock_cert .not_valid_before = real_cert .not_valid_before
111- mock_cert .not_valid_after = real_cert .not_valid_after
111+ mock_cert .not_valid_before_utc = real_cert .not_valid_before_utc
112+ mock_cert .not_valid_after_utc = real_cert .not_valid_after_utc
112113 mock_cert .signature = real_cert .signature
113114 mock_cert .tbs_certificate_bytes = real_cert .tbs_certificate_bytes
114115 mock_cert .signature_hash_algorithm = real_cert .signature_hash_algorithm
@@ -180,21 +181,21 @@ async def test_verify_signature_bad_numbers(aiohttp_session, mock_with_x5u, cach
180181
181182
182183async def test_verify_x5u_expired (aiohttp_session , mock_with_x5u , cache , now_fixed ):
183- now_fixed .return_value = datetime .datetime (2022 , 10 , 23 , 16 , 16 , 16 )
184+ now_fixed .return_value = datetime .datetime (2022 , 10 , 23 , 16 , 16 , 16 , tzinfo = timezone . utc )
184185 s = SignatureVerifier (aiohttp_session , cache , DEV_ROOT_HASH )
185186 with pytest .raises (autograph_utils .CertificateExpired ) as excinfo :
186187 await s .verify (SIGNED_DATA , SAMPLE_SIGNATURE , FAKE_CERT_URL )
187188
188- assert excinfo .value .detail == "Certificate expired on 2021-07-05 21:57:15"
189+ assert excinfo .value .detail == "Certificate expired on 2021-07-05 21:57:15+00:00 "
189190
190191
191192async def test_verify_x5u_too_soon (aiohttp_session , mock_with_x5u , cache , now_fixed ):
192- now_fixed .return_value = datetime .datetime (2010 , 10 , 23 , 16 , 16 , 16 )
193+ now_fixed .return_value = datetime .datetime (2010 , 10 , 23 , 16 , 16 , 16 , tzinfo = timezone . utc )
193194 s = SignatureVerifier (aiohttp_session , cache , DEV_ROOT_HASH )
194195 with pytest .raises (autograph_utils .CertificateNotYetValid ) as excinfo :
195196 await s .verify (SIGNED_DATA , SAMPLE_SIGNATURE , FAKE_CERT_URL )
196197
197- assert excinfo .value .detail == "Certificate is not valid until 2016-07-06 21:57:15"
198+ assert excinfo .value .detail == "Certificate is not valid until 2016-07-06 21:57:15+00:00 "
198199
199200
200201async def test_verify_x5u_screwy_dates (aiohttp_session , mock_with_x5u , cache , now_fixed ):
@@ -204,16 +205,16 @@ async def test_verify_x5u_screwy_dates(aiohttp_session, mock_with_x5u, cache, no
204205 CERT_LIST [0 ], backend = default_backend ()
205206 )
206207 bad_cert = mock_cert (leaf_cert )
207- bad_cert .not_valid_before = leaf_cert .not_valid_after
208- bad_cert .not_valid_after = leaf_cert .not_valid_before
208+ bad_cert .not_valid_before_utc = leaf_cert .not_valid_after_utc
209+ bad_cert .not_valid_after_utc = leaf_cert .not_valid_before_utc
209210 with mock .patch ("autograph_utils.x509.load_pem_x509_certificate" ) as x509 :
210211 x509 .return_value = bad_cert
211212 with pytest .raises (autograph_utils .BadCertificate ) as excinfo :
212213 await s .verify (SIGNED_DATA , SAMPLE_SIGNATURE , FAKE_CERT_URL )
213214
214215 assert excinfo .value .detail == (
215- "Bad certificate: not_before (2021-07-05 21:57:15) "
216- "after not_after (2016-07-06 21:57:15)"
216+ "Bad certificate: not_before (2021-07-05 21:57:15+00:00 ) "
217+ "after not_after (2016-07-06 21:57:15+00:00 )"
217218 )
218219
219220
0 commit comments