diff --git a/Cargo.lock b/Cargo.lock index 8ac382cf..0af24cf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,7 +99,6 @@ dependencies = [ "ip_network_table", "jni", "libc", - "mock_instant", "nix", "parking_lot", "rand_core", @@ -675,12 +674,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "mock_instant" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c1a54de846c4006b88b1516731cc1f6026eb5dc4bcb186aa071ef66d40524ec" - [[package]] name = "nix" version = "0.29.0" diff --git a/boringtun/Cargo.toml b/boringtun/Cargo.toml index efab3782..6abf6cd9 100644 --- a/boringtun/Cargo.toml +++ b/boringtun/Cargo.toml @@ -17,8 +17,7 @@ default = [] device = ["socket2", "thiserror"] jni-bindings = ["ffi-bindings", "jni"] ffi-bindings = ["tracing-subscriber"] -# mocks std::time::Instant with mock_instant -mock-instant = ["mock_instant"] +mock-instant = [] # Deprecated. [dependencies] base64 = "0.22" @@ -41,7 +40,6 @@ aead = "0.5.0-pre.2" blake2 = "0.10" hmac = "0.12" jni = { version = "0.19.0", optional = true } -mock_instant = { version = "0.3", optional = true } socket2 = { version = "0.5.8", features = ["all"], optional = true } thiserror = { version = "1", optional = true } diff --git a/boringtun/src/lib.rs b/boringtun/src/lib.rs index dc8e3bad..6cc52d9f 100644 --- a/boringtun/src/lib.rs +++ b/boringtun/src/lib.rs @@ -14,9 +14,6 @@ pub mod ffi; pub mod jni; pub mod noise; -#[cfg(not(feature = "mock-instant"))] -pub(crate) mod sleepyinstant; - #[cfg(any(feature = "ffi-bindings", feature = "device"))] pub(crate) mod serialization; diff --git a/boringtun/src/sleepyinstant/mod.rs b/boringtun/src/sleepyinstant/mod.rs deleted file mode 100644 index 542beea3..00000000 --- a/boringtun/src/sleepyinstant/mod.rs +++ /dev/null @@ -1,77 +0,0 @@ -#![forbid(unsafe_code)] -//! Attempts to provide the same functionality as std::time::Instant, except it -//! uses a timer which accounts for time when the system is asleep -use std::time::Duration; - -#[cfg(target_os = "windows")] -mod windows; -#[cfg(target_os = "windows")] -use windows as inner; - -#[cfg(unix)] -mod unix; -#[cfg(unix)] -use unix as inner; - -/// A measurement of a monotonically nondecreasing clock. -/// Opaque and useful only with [`Duration`]. -/// -/// Instants are always guaranteed, barring [platform bugs], to be no less than any previously -/// measured instant when created, and are often useful for tasks such as measuring -/// benchmarks or timing how long an operation takes. -/// -/// Note, however, that instants are **not** guaranteed to be **steady**. In other -/// words, each tick of the underlying clock might not be the same length (e.g. -/// some seconds may be longer than others). An instant may jump forwards or -/// experience time dilation (slow down or speed up), but it will never go -/// backwards. -/// -/// Instants are opaque types that can only be compared to one another. There is -/// no method to get "the number of seconds" from an instant. Instead, it only -/// allows measuring the duration between two instants (or comparing two -/// instants). -/// -/// The size of an `Instant` struct may vary depending on the target operating -/// system. -/// -#[derive(Clone, Copy, Debug)] -pub struct Instant { - t: inner::Instant, -} - -impl Instant { - /// Returns an instant corresponding to "now". - pub fn now() -> Self { - Self { - t: inner::Instant::now(), - } - } - - /// Returns the amount of time elapsed from another instant to this one, - /// or zero duration if that instant is later than this one. - /// - /// # Panics - /// - /// panics when `earlier` was later than `self`. - pub fn duration_since(&self, earlier: Instant) -> Duration { - self.t.duration_since(earlier.t) - } - - /// Returns the amount of time elapsed since this instant was created. - pub fn elapsed(&self) -> Duration { - Self::now().duration_since(*self) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn time_increments_after_sleep() { - let sleep_time = Duration::from_millis(10); - let start = Instant::now(); - std::thread::sleep(sleep_time); - assert!(start.elapsed() >= sleep_time); - } -} diff --git a/boringtun/src/sleepyinstant/unix.rs b/boringtun/src/sleepyinstant/unix.rs deleted file mode 100644 index 8488c2f4..00000000 --- a/boringtun/src/sleepyinstant/unix.rs +++ /dev/null @@ -1,48 +0,0 @@ -use std::time::Duration; - -use nix::sys::time::TimeSpec; -use nix::time::{clock_gettime, ClockId}; - -#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))] -const CLOCK_ID: ClockId = ClockId::CLOCK_MONOTONIC; -#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))] -const CLOCK_ID: ClockId = ClockId::CLOCK_BOOTTIME; - -#[derive(Clone, Copy, Debug)] -pub(crate) struct Instant { - t: TimeSpec, -} - -impl Instant { - pub(crate) fn now() -> Self { - // std::time::Instant unwraps as well, so feel safe doing so here - let t = clock_gettime(CLOCK_ID).unwrap(); - Self { t } - } - - fn checked_duration_since(&self, earlier: Instant) -> Option { - const NANOSECOND: nix::libc::c_long = 1_000_000_000; - let (tv_sec, tv_nsec) = if self.t.tv_nsec() < earlier.t.tv_nsec() { - ( - self.t.tv_sec() - earlier.t.tv_sec() - 1, - self.t.tv_nsec() - earlier.t.tv_nsec() + NANOSECOND, - ) - } else { - ( - self.t.tv_sec() - earlier.t.tv_sec(), - self.t.tv_nsec() - earlier.t.tv_nsec(), - ) - }; - - if tv_sec < 0 { - None - } else { - Some(Duration::new(tv_sec as _, tv_nsec as _)) - } - } - - pub(crate) fn duration_since(&self, earlier: Instant) -> Duration { - self.checked_duration_since(earlier) - .unwrap_or(Duration::ZERO) - } -} diff --git a/boringtun/src/sleepyinstant/windows.rs b/boringtun/src/sleepyinstant/windows.rs deleted file mode 100644 index ac852292..00000000 --- a/boringtun/src/sleepyinstant/windows.rs +++ /dev/null @@ -1 +0,0 @@ -pub(crate) use std::time::Instant;