diff options
author | Carl Lerche <me@carllerche.com> | 2015-02-21 14:33:27 -0800 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2015-02-21 14:33:27 -0800 |
commit | 60ccee778b2adca488706c586bd61c5affa90de3 (patch) | |
tree | d8738241a4e5fa8fe02331f33ab190b980d507fd /test/test_unistd.rs | |
parent | b2454c9d793dfeb946d5e12f51ad571d32fa9987 (diff) | |
download | nix-60ccee778b2adca488706c586bd61c5affa90de3.zip |
Cleanup readv & writev + tests
Diffstat (limited to 'test/test_unistd.rs')
-rw-r--r-- | test/test_unistd.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/test/test_unistd.rs b/test/test_unistd.rs new file mode 100644 index 00000000..c648cdeb --- /dev/null +++ b/test/test_unistd.rs @@ -0,0 +1,65 @@ +use nix::unistd::*; +use nix::unistd::Fork::*; +use nix::sys::wait::*; +use std::ffi::CString; + +#[test] +fn test_fork_and_waitpid() { + let pid = fork(); + match pid { + Ok(Child) => {} // ignore child here + Ok(Parent(child_pid)) => { + // assert that child was created and pid > 0 + assert!(child_pid > 0); + let wait_status = waitpid(child_pid, 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_pid), + + // panic, must never happen + Ok(WaitStatus::StillAlive) => 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") + } +} + +#[test] +fn test_execve() { + // The `exec`d process will write to `writer`, and we'll read that + // data from `reader`. + let (reader, writer) = pipe().unwrap(); + + match fork().unwrap() { + Child => { + // Close stdout. + close(1).unwrap(); + // Make `writer` be the stdout of the new process. + dup(writer).unwrap(); + // exec! + execve(&CString::new(b"/bin/sh").unwrap(), + &[CString::new(b"").unwrap(), + CString::new(b"-c").unwrap(), + CString::new(b"echo nix!!! && env").unwrap()], + &[CString::new(b"foo=bar").unwrap(), + CString::new(b"baz=quux").unwrap()]).unwrap(); + }, + Parent(child_pid) => { + // Wait for the child to exit. + waitpid(child_pid, None).unwrap(); + // Read 1024 bytes. + let mut buf = [0u8; 1024]; + read(reader, &mut buf).unwrap(); + // It should contain the things we printed using `/bin/sh`. + let string = String::from_utf8_lossy(&buf); + assert!(string.contains("nix!!!")); + assert!(string.contains("foo=bar")); + assert!(string.contains("baz=quux")); + } + } +} |