summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/tests/time
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/rustix/tests/time')
-rw-r--r--vendor/rustix/tests/time/dynamic_clocks.rs22
-rw-r--r--vendor/rustix/tests/time/main.rs14
-rw-r--r--vendor/rustix/tests/time/monotonic.rs45
-rw-r--r--vendor/rustix/tests/time/timerfd.rs75
-rw-r--r--vendor/rustix/tests/time/timespec.rs26
-rw-r--r--vendor/rustix/tests/time/y2038.rs77
6 files changed, 259 insertions, 0 deletions
diff --git a/vendor/rustix/tests/time/dynamic_clocks.rs b/vendor/rustix/tests/time/dynamic_clocks.rs
new file mode 100644
index 000000000..a7e1e6792
--- /dev/null
+++ b/vendor/rustix/tests/time/dynamic_clocks.rs
@@ -0,0 +1,22 @@
+#![cfg(not(any(target_os = "redox", target_os = "wasi")))]
+
+use rustix::fd::AsFd;
+use rustix::time::{clock_gettime_dynamic, ClockId, DynamicClockId};
+
+#[test]
+fn test_known_clocks() {
+ clock_gettime_dynamic(DynamicClockId::Known(ClockId::Realtime)).unwrap();
+ clock_gettime_dynamic(DynamicClockId::Known(ClockId::Monotonic)).unwrap();
+}
+
+#[test]
+fn test_dynamic_clocks() {
+ let file = std::fs::File::open("Cargo.toml").unwrap();
+ clock_gettime_dynamic(DynamicClockId::Dynamic(file.as_fd())).unwrap_err();
+}
+
+#[cfg(any(target_os = "android", target_os = "linux"))]
+#[test]
+fn test_conditional_clocks() {
+ let _ = clock_gettime_dynamic(DynamicClockId::Tai);
+}
diff --git a/vendor/rustix/tests/time/main.rs b/vendor/rustix/tests/time/main.rs
new file mode 100644
index 000000000..43283bca2
--- /dev/null
+++ b/vendor/rustix/tests/time/main.rs
@@ -0,0 +1,14 @@
+//! Tests for [`rustix::time`].
+
+#![cfg(feature = "time")]
+#![cfg(not(windows))]
+#![cfg_attr(target_os = "wasi", feature(wasi_ext))]
+#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
+
+mod dynamic_clocks;
+#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
+mod monotonic;
+#[cfg(any(target_os = "android", target_os = "linux"))]
+mod timerfd;
+mod timespec;
+mod y2038;
diff --git a/vendor/rustix/tests/time/monotonic.rs b/vendor/rustix/tests/time/monotonic.rs
new file mode 100644
index 000000000..89470f2b4
--- /dev/null
+++ b/vendor/rustix/tests/time/monotonic.rs
@@ -0,0 +1,45 @@
+#[cfg(feature = "thread")]
+use rustix::thread::nanosleep;
+use rustix::time::{clock_gettime, ClockId, Timespec};
+
+/// Attempt to test that the monotonic clock is monotonic. Time may or may not
+/// advance, but it shouldn't regress.
+#[test]
+fn test_monotonic_clock() {
+ let a = clock_gettime(ClockId::Monotonic);
+ let b = clock_gettime(ClockId::Monotonic);
+ if b.tv_sec == a.tv_sec {
+ assert!(b.tv_nsec >= a.tv_nsec);
+ } else {
+ assert!(b.tv_sec > a.tv_sec);
+ }
+}
+
+/// With the "thread" feature, we can sleep so that we're guaranteed that time
+/// has advanced.
+#[cfg(feature = "thread")]
+#[test]
+fn test_monotonic_clock_with_sleep_1s() {
+ let a = clock_gettime(ClockId::Monotonic);
+ let _rem = nanosleep(&Timespec {
+ tv_sec: 1,
+ tv_nsec: 0,
+ });
+ let b = clock_gettime(ClockId::Monotonic);
+ assert!(b.tv_sec > a.tv_sec);
+}
+
+/// With the "thread" feature, we can sleep so that we're guaranteed that time
+/// has advanced.
+#[cfg(feature = "thread")]
+#[test]
+fn test_monotonic_clock_with_sleep_1ms() {
+ let a = clock_gettime(ClockId::Monotonic);
+ let _rem = nanosleep(&Timespec {
+ tv_sec: 0,
+ tv_nsec: 1_000_000,
+ });
+ let b = clock_gettime(ClockId::Monotonic);
+ assert!(b.tv_sec >= a.tv_sec);
+ assert!(b.tv_sec != a.tv_sec || b.tv_nsec > a.tv_nsec);
+}
diff --git a/vendor/rustix/tests/time/timerfd.rs b/vendor/rustix/tests/time/timerfd.rs
new file mode 100644
index 000000000..6ad4dd72e
--- /dev/null
+++ b/vendor/rustix/tests/time/timerfd.rs
@@ -0,0 +1,75 @@
+use rustix::time::{
+ timerfd_create, timerfd_gettime, timerfd_settime, Itimerspec, TimerfdClockId, TimerfdFlags,
+ TimerfdTimerFlags, Timespec,
+};
+
+#[test]
+fn test_timerfd() {
+ let fd = timerfd_create(TimerfdClockId::Monotonic, TimerfdFlags::CLOEXEC).unwrap();
+
+ let set = Itimerspec {
+ it_interval: Timespec {
+ tv_sec: 0,
+ tv_nsec: 0,
+ },
+ it_value: Timespec {
+ tv_sec: 1,
+ tv_nsec: 2,
+ },
+ };
+ let _old: Itimerspec = timerfd_settime(&fd, TimerfdTimerFlags::ABSTIME, &set).unwrap();
+
+ // Wait for the timer to expire.
+ let mut buf = [0_u8; 8];
+ assert_eq!(rustix::io::read(&fd, &mut buf), Ok(8));
+ assert!(u64::from_ne_bytes(buf) >= 1);
+
+ let new = timerfd_gettime(&fd).unwrap();
+
+ // The timer counts down.
+ assert_eq!(set.it_interval.tv_sec, new.it_interval.tv_sec);
+ assert_eq!(set.it_interval.tv_nsec, new.it_interval.tv_nsec);
+ assert!(new.it_value.tv_sec <= set.it_value.tv_sec);
+ assert!(
+ new.it_value.tv_nsec < set.it_value.tv_nsec || new.it_value.tv_sec < set.it_value.tv_sec
+ );
+}
+
+/// Similar, but set an interval for a repeated timer. Don't check that the
+/// times are monotonic because that would race with the timer repeating.
+#[test]
+fn test_timerfd_with_interval() {
+ let fd = timerfd_create(TimerfdClockId::Monotonic, TimerfdFlags::CLOEXEC).unwrap();
+
+ let set = Itimerspec {
+ it_interval: Timespec {
+ tv_sec: 0,
+ tv_nsec: 6,
+ },
+ it_value: Timespec {
+ tv_sec: 1,
+ tv_nsec: 7,
+ },
+ };
+ let _old: Itimerspec = timerfd_settime(&fd, TimerfdTimerFlags::ABSTIME, &set).unwrap();
+
+ // Wait for the timer to expire.
+ let mut buf = [0_u8; 8];
+ assert_eq!(rustix::io::read(&fd, &mut buf), Ok(8));
+ assert!(u64::from_ne_bytes(buf) >= 1);
+
+ let new = timerfd_gettime(&fd).unwrap();
+
+ assert_eq!(set.it_interval.tv_sec, new.it_interval.tv_sec);
+ assert_eq!(set.it_interval.tv_nsec, new.it_interval.tv_nsec);
+
+ // Wait for the timer to expire again.
+ let mut buf = [0_u8; 8];
+ assert_eq!(rustix::io::read(&fd, &mut buf), Ok(8));
+ assert!(u64::from_ne_bytes(buf) >= 1);
+
+ let new = timerfd_gettime(&fd).unwrap();
+
+ assert_eq!(set.it_interval.tv_sec, new.it_interval.tv_sec);
+ assert_eq!(set.it_interval.tv_nsec, new.it_interval.tv_nsec);
+}
diff --git a/vendor/rustix/tests/time/timespec.rs b/vendor/rustix/tests/time/timespec.rs
new file mode 100644
index 000000000..6d892dcfe
--- /dev/null
+++ b/vendor/rustix/tests/time/timespec.rs
@@ -0,0 +1,26 @@
+#[test]
+fn test_timespec_layout() {
+ #[cfg(not(target_os = "redox"))]
+ use rustix::fs::{UTIME_NOW, UTIME_OMIT};
+ use rustix::time::{Nsecs, Secs, Timespec};
+
+ let tv_sec: Secs = 0;
+ let tv_nsec: Nsecs = 0;
+ let _ = Timespec { tv_sec, tv_nsec };
+
+ #[cfg(not(target_os = "redox"))]
+ let _ = Timespec {
+ tv_sec,
+ tv_nsec: UTIME_NOW,
+ };
+ #[cfg(not(target_os = "redox"))]
+ let _ = Timespec {
+ tv_sec,
+ tv_nsec: UTIME_OMIT,
+ };
+ let _ = Timespec { tv_sec, tv_nsec: 0 };
+ let _ = Timespec {
+ tv_sec,
+ tv_nsec: 999_999_999,
+ };
+}
diff --git a/vendor/rustix/tests/time/y2038.rs b/vendor/rustix/tests/time/y2038.rs
new file mode 100644
index 000000000..45a073d19
--- /dev/null
+++ b/vendor/rustix/tests/time/y2038.rs
@@ -0,0 +1,77 @@
+/// Test that `Timespec` and `Secs` support a 64-bit number of seconds,
+/// avoiding the y2038 bug.
+///
+/// The Rust Musl target and libc crate are currently using Musl 1.1. It is
+/// expected to update to Musl 1.2 at some point, at which point it'll gain a
+/// 64-bit `time_t`.
+///
+/// 32-bit Android is [not y2038 compatible]. In theory we could use
+/// `libc::syscall` and call the new syscalls ourselves, however that doesn't
+/// seem worth the effort on a platform that will likely never support add
+/// such support itself.
+///
+/// [not y2038 compatible]: https://android.googlesource.com/platform/bionic/+/refs/heads/master/docs/32-bit-abi.md#is-32_bit-on-lp32-y2038
+#[cfg(not(all(target_env = "musl", target_pointer_width = "32")))]
+#[cfg(not(all(target_os = "android", target_pointer_width = "32")))]
+#[cfg(not(all(target_os = "emscripten", target_pointer_width = "32")))]
+#[test]
+fn test_y2038() {
+ use rustix::time::{Secs, Timespec};
+
+ let tv_sec: i64 = 0;
+ let _ = Timespec { tv_sec, tv_nsec: 0 };
+ let _: Secs = tv_sec;
+
+ #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
+ {
+ use rustix::time::Itimerspec;
+
+ let _ = Itimerspec {
+ it_interval: Timespec { tv_sec, tv_nsec: 0 },
+ it_value: Timespec { tv_sec, tv_nsec: 0 },
+ };
+ }
+}
+
+#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
+#[test]
+fn test_y2038_with_timerfd() {
+ use rustix::time::{
+ timerfd_create, timerfd_gettime, timerfd_settime, Itimerspec, TimerfdClockId, TimerfdFlags,
+ TimerfdTimerFlags, Timespec,
+ };
+
+ let fd = timerfd_create(TimerfdClockId::Monotonic, TimerfdFlags::CLOEXEC).unwrap();
+
+ let set = Itimerspec {
+ it_interval: Timespec {
+ tv_sec: (1_u64 << 32) as _,
+ tv_nsec: 20,
+ },
+ it_value: Timespec {
+ tv_sec: (1_u64 << 32) as _,
+ tv_nsec: 21,
+ },
+ };
+ let _old: Itimerspec = match timerfd_settime(&fd, TimerfdTimerFlags::ABSTIME, &set) {
+ Ok(i) => i,
+
+ // On 32-bit and mips64 platforms, accept `EOVERFLOW`, meaning that
+ // y2038 support in `timerfd` APIs is not available on this platform
+ // or this version of the platform.
+ #[cfg(any(target_pointer_width = "32", target_arch = "mips64"))]
+ Err(rustix::io::Errno::OVERFLOW) => return,
+
+ Err(e) => panic!("unexpected error: {:?}", e),
+ };
+
+ let new = timerfd_gettime(&fd).unwrap();
+
+ // The timer counts down.
+ assert_eq!(set.it_interval.tv_sec, new.it_interval.tv_sec);
+ assert_eq!(set.it_interval.tv_nsec, new.it_interval.tv_nsec);
+ assert!(new.it_value.tv_sec <= set.it_value.tv_sec);
+ assert!(
+ new.it_value.tv_nsec < set.it_value.tv_nsec || new.it_value.tv_sec < set.it_value.tv_sec
+ );
+}