diff options
Diffstat (limited to 'test/test_unistd.rs')
-rw-r--r-- | test/test_unistd.rs | 110 |
1 files changed, 75 insertions, 35 deletions
diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 188bfbeb..856693f6 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -1,38 +1,41 @@ +extern crate tempdir; + use nix::unistd::*; use nix::unistd::ForkResult::*; use nix::sys::wait::*; +use nix::sys::stat; +use std::iter; use std::ffi::CString; - use std::io::{Write, Read}; +use std::os::unix::prelude::*; +use std::env::current_dir; use tempfile::tempfile; +use tempdir::TempDir; use libc::off_t; -use std::os::unix::prelude::*; - - #[test] fn test_fork_and_waitpid() { let pid = fork(); match pid { - Ok(Child) => {} // ignore child here - Ok(Parent { child }) => { - // assert that child was created and pid > 0 - assert!(child > 0); - let wait_status = waitpid(child, None); - match wait_status { - // assert that waitpid returned correct status and the pid is the one of the child - Ok(WaitStatus::Exited(pid_t, _)) => assert!(pid_t == child), - - // panic, must never happen - Ok(_) => panic!("Child still alive, should never happen"), - - // panic, waitpid should never fail - Err(_) => panic!("Error: waitpid Failed") - } - - }, - // panic, fork should never fail unless there is a serious problem with the OS - Err(_) => panic!("Error: Fork Failed") + Ok(Child) => {} // ignore child here + Ok(Parent { child }) => { + // assert that child was created and pid > 0 + assert!(child > 0); + let wait_status = waitpid(child, None); + match wait_status { + // assert that waitpid returned correct status and the pid is the one of the child + Ok(WaitStatus::Exited(pid_t, _)) => assert!(pid_t == child), + + // panic, must never happen + Ok(_) => panic!("Child still alive, should never happen"), + + // panic, waitpid should never fail + Err(_) => panic!("Error: waitpid Failed") + } + + }, + // panic, fork should never fail unless there is a serious problem with the OS + Err(_) => panic!("Error: Fork Failed") } } @@ -40,18 +43,37 @@ fn test_fork_and_waitpid() { fn test_wait() { let pid = fork(); match pid { - Ok(Child) => {} // ignore child here - Ok(Parent { child }) => { - let wait_status = wait(); - - // just assert that (any) one child returns with WaitStatus::Exited - assert_eq!(wait_status, Ok(WaitStatus::Exited(child, 0))); - }, - // panic, fork should never fail unless there is a serious problem with the OS - Err(_) => panic!("Error: Fork Failed") + Ok(Child) => {} // ignore child here + Ok(Parent { child }) => { + let wait_status = wait(); + + // just assert that (any) one child returns with WaitStatus::Exited + assert_eq!(wait_status, Ok(WaitStatus::Exited(child, 0))); + }, + // panic, fork should never fail unless there is a serious problem with the OS + Err(_) => panic!("Error: Fork Failed") } } +#[test] +fn test_mkstemp() { + let result = mkstemp("/tmp/nix_tempfile.XXXXXX"); + match result { + Ok((fd, path)) => { + close(fd).unwrap(); + unlink(path.as_path()).unwrap(); + }, + Err(e) => panic!("mkstemp failed: {}", e) + } + + let result = mkstemp("/tmp/"); + match result { + Ok(_) => { + panic!("mkstemp succeeded even though it should fail (provided a directory)"); + }, + Err(_) => {} + } +} #[test] fn test_getpid() { @@ -120,6 +142,24 @@ macro_rules! execve_test_factory( ); #[test] +fn test_getcwd() { + let mut tmp_dir = TempDir::new("test_getcwd").unwrap().into_path(); + assert!(chdir(tmp_dir.as_path()).is_ok()); + assert_eq!(getcwd().unwrap(), current_dir().unwrap()); + + // make path 500 chars longer so that buffer doubling in getcwd kicks in. + // Note: One path cannot be longer than 255 bytes (NAME_MAX) + // whole path cannot be longer than PATH_MAX (usually 4096 on linux, 1024 on macos) + for _ in 0..5 { + let newdir = iter::repeat("a").take(100).collect::<String>(); + tmp_dir.push(newdir); + assert!(mkdir(tmp_dir.as_path(), stat::S_IRWXU).is_ok()); + } + assert!(chdir(tmp_dir.as_path()).is_ok()); + assert_eq!(getcwd().unwrap(), current_dir().unwrap()); +} + +#[test] fn test_lseek() { const CONTENTS: &'static [u8] = b"abcdef123456"; let mut tmp = tempfile().unwrap(); @@ -129,10 +169,10 @@ fn test_lseek() { lseek(tmp.as_raw_fd(), offset, Whence::SeekSet).unwrap(); let mut buf = String::new(); - tmp.read_to_string(&mut buf).unwrap(); - assert_eq!(b"f123456", buf.as_bytes()); + tmp.read_to_string(&mut buf).unwrap(); + assert_eq!(b"f123456", buf.as_bytes()); - close(tmp.as_raw_fd()).unwrap(); + close(tmp.as_raw_fd()).unwrap(); } #[cfg(any(target_os = "linux", target_os = "android"))] |