diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/sys/mod.rs | 2 | ||||
-rw-r--r-- | test/sys/test_timerfd.rs | 61 | ||||
-rw-r--r-- | test/test_unistd.rs | 47 |
3 files changed, 110 insertions, 0 deletions
diff --git a/test/sys/mod.rs b/test/sys/mod.rs index 3dc430ee..c4391c72 100644 --- a/test/sys/mod.rs +++ b/test/sys/mod.rs @@ -41,3 +41,5 @@ mod test_pthread; target_os = "netbsd", target_os = "openbsd"))] mod test_ptrace; +#[cfg(any(target_os = "android", target_os = "linux"))] +mod test_timerfd; diff --git a/test/sys/test_timerfd.rs b/test/sys/test_timerfd.rs new file mode 100644 index 00000000..24fb2ac0 --- /dev/null +++ b/test/sys/test_timerfd.rs @@ -0,0 +1,61 @@ +use nix::sys::time::{TimeSpec, TimeValLike}; +use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags}; +use std::time::Instant; + +#[test] +pub fn test_timerfd_oneshot() { + let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap(); + + let before = Instant::now(); + + timer + .set( + Expiration::OneShot(TimeSpec::seconds(1)), + TimerSetTimeFlags::empty(), + ) + .unwrap(); + + timer.wait().unwrap(); + + let millis = before.elapsed().as_millis(); + assert!(millis > 900); +} + +#[test] +pub fn test_timerfd_interval() { + let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap(); + + let before = Instant::now(); + timer + .set( + Expiration::IntervalDelayed(TimeSpec::seconds(1), TimeSpec::seconds(2)), + TimerSetTimeFlags::empty(), + ) + .unwrap(); + + timer.wait().unwrap(); + + let start_delay = before.elapsed().as_millis(); + assert!(start_delay > 900); + + timer.wait().unwrap(); + + let interval_delay = before.elapsed().as_millis(); + assert!(interval_delay > 2900); +} + +#[test] +pub fn test_timerfd_unset() { + let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap(); + + timer + .set( + Expiration::OneShot(TimeSpec::seconds(1)), + TimerSetTimeFlags::empty(), + ) + .unwrap(); + + timer.unset().unwrap(); + + assert!(timer.get().unwrap() == None); +} diff --git a/test/test_unistd.rs b/test/test_unistd.rs index d44cb067..8fe1d432 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -7,6 +7,8 @@ use nix::unistd::ForkResult::*; use nix::sys::signal::{SaFlags, SigAction, SigHandler, SigSet, Signal, sigaction}; use nix::sys::wait::*; use nix::sys::stat::{self, Mode, SFlag}; +#[cfg(not(target_os = "redox"))] +use nix::pty::{posix_openpt, grantpt, unlockpt, ptsname}; use nix::errno::Errno; #[cfg(not(target_os = "redox"))] use nix::Error; @@ -19,6 +21,8 @@ use std::fs::{self, File}; use std::io::Write; use std::mem; use std::os::unix::prelude::*; +#[cfg(not(target_os = "redox"))] +use std::path::Path; use tempfile::{tempdir, tempfile}; use libc::{_exit, off_t}; @@ -964,3 +968,46 @@ fn test_setfsuid() { // open the temporary file with the current thread filesystem UID fs::File::open(temp_path_2).unwrap(); } + +#[test] +#[cfg(not(target_os = "redox"))] +fn test_ttyname() { + let fd = posix_openpt(OFlag::O_RDWR).expect("posix_openpt failed"); + assert!(fd.as_raw_fd() > 0); + + // on linux, we can just call ttyname on the pty master directly, but + // apparently osx requires that ttyname is called on a slave pty (can't + // find this documented anywhere, but it seems to empirically be the case) + grantpt(&fd).expect("grantpt failed"); + unlockpt(&fd).expect("unlockpt failed"); + let sname = unsafe { ptsname(&fd) }.expect("ptsname failed"); + let fds = open( + Path::new(&sname), + OFlag::O_RDWR, + stat::Mode::empty(), + ).expect("open failed"); + assert!(fds > 0); + + let name = ttyname(fds).expect("ttyname failed"); + assert!(name.starts_with("/dev")); +} + +#[test] +#[cfg(not(target_os = "redox"))] +fn test_ttyname_not_pty() { + let fd = File::open("/dev/zero").unwrap(); + assert!(fd.as_raw_fd() > 0); + assert_eq!(ttyname(fd.as_raw_fd()), Err(Error::Sys(Errno::ENOTTY))); +} + +#[test] +#[cfg(all(not(target_os = "redox"), not(target_env = "musl")))] +fn test_ttyname_invalid_fd() { + assert_eq!(ttyname(-1), Err(Error::Sys(Errno::EBADF))); +} + +#[test] +#[cfg(all(not(target_os = "redox"), target_env = "musl"))] +fn test_ttyname_invalid_fd() { + assert_eq!(ttyname(-1), Err(Error::Sys(Errno::ENOTTY))); +} |