Skip to content

Commit 06f8735

Browse files
committed
tests: use a fixed SystemTime for certificate validation
Fixing the `SystemTime` that we pass to the certificate verifier for the real world and mock verification tests will ensure that the tests don't start to fail just because the vendored certificates have expired.
1 parent 3bf8828 commit 06f8735

File tree

3 files changed

+15
-4
lines changed
  • rustls-platform-verifier/src

3 files changed

+15
-4
lines changed

rustls-platform-verifier/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use rustls::ClientConfig;
66
use std::sync::Arc;
7+
use std::time::{Duration, SystemTime};
78

89
mod verification;
910
pub use verification::Verifier;
@@ -71,3 +72,12 @@ pub fn tls_config() -> ClientConfig {
7172
pub fn verifier_for_dbg(root: &[u8]) -> Arc<dyn rustls::client::ServerCertVerifier> {
7273
Arc::new(Verifier::new_with_fake_root(root))
7374
}
75+
76+
/// Return a fixed [SystemTime] for certificate validation purposes.
77+
///
78+
/// We fix the "now" value used for certificate validation to a fixed point in time at which
79+
/// we know the test certificates are valid. This must be updated if the test certificates
80+
/// are regenerated.
81+
pub fn verification_time() -> SystemTime {
82+
SystemTime::UNIX_EPOCH + Duration::from_secs(1_704_304_988)
83+
}

rustls-platform-verifier/src/tests/verification_mock/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use super::TestCase;
2424
use crate::tests::assert_cert_error_eq;
2525
use crate::verification::{EkuError, Verifier};
26+
use crate::verification_time;
2627
use rustls::{client::ServerCertVerifier, CertificateError, Error as TlsError};
2728
use std::convert::TryFrom;
2829
use std::net::IpAddr;
@@ -95,7 +96,7 @@ pub(super) fn verification_without_mock_root() {
9596
&server_name,
9697
&mut std::iter::empty(),
9798
&[],
98-
std::time::SystemTime::now(),
99+
verification_time(),
99100
);
100101

101102
assert_eq!(
@@ -289,7 +290,7 @@ fn test_with_mock_root<E: std::error::Error + PartialEq + 'static>(test_case: &T
289290
&server_name,
290291
&mut std::iter::empty(),
291292
test_case.stapled_ocsp.unwrap_or(&[]),
292-
std::time::SystemTime::now(),
293+
verification_time(),
293294
);
294295

295296
assert_cert_error_eq(

rustls-platform-verifier/src/tests/verification_real_world/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//! Thus we don't expect these tests to be flaky w.r.t. that, except for
4343
//! potentially poor performance.
4444
use super::TestCase;
45-
use crate::{tests::assert_cert_error_eq, Verifier};
45+
use crate::{tests::assert_cert_error_eq, verification_time, Verifier};
4646
use rustls::{client::ServerCertVerifier, CertificateError, Error as TlsError};
4747
use std::convert::TryFrom;
4848

@@ -145,7 +145,7 @@ fn real_world_test<E: std::error::Error>(test_case: &TestCase<E>) {
145145
&server_name,
146146
&mut std::iter::empty(),
147147
stapled_ocsp,
148-
std::time::SystemTime::now(),
148+
verification_time(),
149149
)
150150
.map(|_| ());
151151

0 commit comments

Comments
 (0)