const BACKWARD_TOLERANCE: i64 = 3600; const FORWARD_JUMP_LIMIT: i64 = 30 * 24 * 60 * 60;
pub fn clock_manipulated(last_seen: i64, now: i64) -> bool {
let drift = last_seen - now; if drift > BACKWARD_TOLERANCE {
return true;
}
if now - last_seen > FORWARD_JUMP_LIMIT {
return true;
}
false
}
pub fn clock_rolled_back(last_seen: i64, now: i64) -> bool {
last_seen - now > BACKWARD_TOLERANCE
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normal_is_fine() {
assert!(!clock_manipulated(1000, 1100));
}
#[test]
fn backward_beyond_tolerance_flags() {
assert!(clock_manipulated(10_000, 10_000 - 4000));
}
#[test]
fn forward_beyond_30d_flags() {
assert!(clock_manipulated(0, 31 * 24 * 60 * 60));
}
#[test]
fn rolled_back_only_flags_backward_jumps() {
assert!(!clock_rolled_back(1000, 1100));
assert!(!clock_rolled_back(0, 60 * 24 * 60 * 60));
assert!(clock_rolled_back(10_000, 10_000 - 4000));
assert!(!clock_rolled_back(10_000, 10_000 - 100));
}
}