Skip to content

Commit

Permalink
Merge #660
Browse files Browse the repository at this point in the history
660: Fix double close bugs in test_lseek and test_lseek64 r=asomers

std::fs::File closes the underlying file descriptor on Drop, without
checking for errors.  test_lseek and test_lseek64 also manually close
the file descriptor.  That works for single threaded test runs.  But for
multithreaded runs, it causes EBADF errors in other tests.  Fix the
tests by consuming the File with into_raw_fd(), so its drop method will
never be called.
  • Loading branch information
bors[bot] committed Jul 9, 2017
2 parents 1b1f15c + a50b476 commit 386c50c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
12 changes: 12 additions & 0 deletions test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ mod test_stat;
mod test_unistd;

use nixtest::assert_size_of;
use std::os::unix::io::RawFd;
use nix::unistd::read;

/// Helper function analogous to std::io::Read::read_exact, but for `RawFD`s
fn read_exact(f: RawFd, buf: &mut [u8]) {
let mut len = 0;
while len < buf.len() {
// get_mut would be better than split_at_mut, but it requires nightly
let (_, remaining) = buf.split_at_mut(len);
len += read(f, remaining).unwrap();
}
}

#[test]
pub fn test_size_of_long() {
Expand Down
24 changes: 7 additions & 17 deletions test/test_pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ use nix::fcntl::{O_RDWR, open};
use nix::pty::*;
use nix::sys::stat;
use nix::sys::termios::*;
use nix::unistd::{read, write, close};

/// Helper function analogous to std::io::Read::read_exact, but for `RawFD`s
fn read_exact(f: RawFd, buf: &mut [u8]) {
let mut len = 0;
while len < buf.len() {
// get_mut would be better than split_at_mut, but it requires nightly
let (_, remaining) = buf.split_at_mut(len);
len += read(f, remaining).unwrap();
}
}
use nix::unistd::{write, close};

/// Test equivalence of `ptsname` and `ptsname_r`
#[test]
Expand Down Expand Up @@ -115,21 +105,21 @@ fn test_openpty() {
let string = "foofoofoo\n";
let mut buf = [0u8; 10];
write(pty.master, string.as_bytes()).unwrap();
read_exact(pty.slave, &mut buf);
::read_exact(pty.slave, &mut buf);

assert_eq!(&buf, string.as_bytes());

// Read the echo as well
let echoed_string = "foofoofoo\r\n";
let mut buf = [0u8; 11];
read_exact(pty.master, &mut buf);
::read_exact(pty.master, &mut buf);
assert_eq!(&buf, echoed_string.as_bytes());

let string2 = "barbarbarbar\n";
let echoed_string2 = "barbarbarbar\r\n";
let mut buf = [0u8; 14];
write(pty.slave, string2.as_bytes()).unwrap();
read_exact(pty.master, &mut buf);
::read_exact(pty.master, &mut buf);

assert_eq!(&buf, echoed_string2.as_bytes());

Expand Down Expand Up @@ -160,20 +150,20 @@ fn test_openpty_with_termios() {
let string = "foofoofoo\n";
let mut buf = [0u8; 10];
write(pty.master, string.as_bytes()).unwrap();
read_exact(pty.slave, &mut buf);
::read_exact(pty.slave, &mut buf);

assert_eq!(&buf, string.as_bytes());

// read the echo as well
let echoed_string = "foofoofoo\n";
read_exact(pty.master, &mut buf);
::read_exact(pty.master, &mut buf);
assert_eq!(&buf, echoed_string.as_bytes());

let string2 = "barbarbarbar\n";
let echoed_string2 = "barbarbarbar\n";
let mut buf = [0u8; 13];
write(pty.slave, string2.as_bytes()).unwrap();
read_exact(pty.master, &mut buf);
::read_exact(pty.master, &mut buf);

assert_eq!(&buf, echoed_string2.as_bytes());

Expand Down
26 changes: 14 additions & 12 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nix::sys::stat;
use std::iter;
use std::ffi::CString;
use std::fs::File;
use std::io::{Write, Read};
use std::io::Write;
use std::os::unix::prelude::*;
use std::env::current_dir;
use tempfile::tempfile;
Expand Down Expand Up @@ -185,16 +185,17 @@ fn test_getcwd() {
fn test_lseek() {
const CONTENTS: &'static [u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write(CONTENTS).unwrap();
tmp.write_all(CONTENTS).unwrap();
let tmpfd = tmp.into_raw_fd();

let offset: off_t = 5;
lseek(tmp.as_raw_fd(), offset, Whence::SeekSet).unwrap();
lseek(tmpfd, offset, Whence::SeekSet).unwrap();

let mut buf = String::new();
tmp.read_to_string(&mut buf).unwrap();
assert_eq!(b"f123456", buf.as_bytes());
let mut buf = [0u8; 7];
::read_exact(tmpfd, &mut buf);
assert_eq!(b"f123456", &buf);

close(tmp.as_raw_fd()).unwrap();
close(tmpfd).unwrap();
}

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand All @@ -203,14 +204,15 @@ fn test_lseek64() {
const CONTENTS: &'static [u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write(CONTENTS).unwrap();
let tmpfd = tmp.into_raw_fd();

lseek64(tmp.as_raw_fd(), 5, Whence::SeekSet).unwrap();
lseek64(tmpfd, 5, Whence::SeekSet).unwrap();

let mut buf = String::new();
tmp.read_to_string(&mut buf).unwrap();
assert_eq!(b"f123456", buf.as_bytes());
let mut buf = [0u8; 7];
::read_exact(tmpfd, &mut buf);
assert_eq!(b"f123456", &buf);

close(tmp.as_raw_fd()).unwrap();
close(tmpfd).unwrap();
}

execve_test_factory!(test_execve, execve, b"/bin/sh", b"/system/bin/sh");
Expand Down

0 comments on commit 386c50c

Please sign in to comment.