Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkgs/id/src/world_doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,24 @@ pub fn validate_journal_file(path: &Path) -> Result<(usize, u32)> {
Ok((entries, version.unwrap_or(JOURNAL_VERSION)))
}

/// Milliseconds since the unix epoch at 2100-01-01T00:00:00Z. A clock
/// reading at or after this bound is absurd: no schedule or expiry this
/// doctor guards looks anywhere near that far ahead, so the reading can
/// only mean the system clock is wrong.
pub const ABSURD_FUTURE_NOW_MS: u64 = 4_102_444_800_000;

/// The clock reads a positive unix time. A zero (or absurd) reading means the
/// system clock is before the epoch, which breaks every schedule and expiry.
#[must_use]
pub fn check_clock(inputs: &DoctorInputs) -> Finding {
match inputs.now_ms {
Some(now) if now >= ABSURD_FUTURE_NOW_MS => fail(
"clock",
format!(
"the system clock reads {now} ms since the epoch, at or after the absurd-future bound of {ABSURD_FUTURE_NOW_MS} ms (2100-01-01T00:00:00Z)"
),
"set the system clock (schedules and expiries depend on it)",
),
Some(now) if now > 0 => ok("clock", format!("{now} ms since the epoch")),
_ => fail(
"clock",
Expand Down Expand Up @@ -657,4 +670,35 @@ mod tests {

assert_eq!(check_clock(&inputs()).severity, Severity::Ok);
}

#[test]
fn a_clock_at_or_after_2100_fails() {
// One millisecond before the bound is still a plausible reading.
let just_before = check_clock(&DoctorInputs {
now_ms: Some(ABSURD_FUTURE_NOW_MS - 1),
..inputs()
});
assert_eq!(just_before.severity, Severity::Ok);

// At the bound the reading is absurd and fails, naming both the
// reading and the bound, with the zero case's fix text.
let at_bound = check_clock(&DoctorInputs {
now_ms: Some(ABSURD_FUTURE_NOW_MS),
..inputs()
});
assert_eq!(at_bound.severity, Severity::Fail);
assert!(at_bound.detail.contains("4102444800000"), "{at_bound:#?}");
assert!(at_bound.detail.contains("2100-01-01"), "{at_bound:#?}");
assert_eq!(
at_bound.fix,
"set the system clock (schedules and expiries depend on it)"
);

// The largest possible reading fails too.
let max = check_clock(&DoctorInputs {
now_ms: Some(u64::MAX),
..inputs()
});
assert_eq!(max.severity, Severity::Fail);
}
}
Loading