Skip to content

Commit

Permalink
Check all tests in CI
Browse files Browse the repository at this point in the history
Travis didn't compile check tests on platforms that couldn't run tests
in CI, so they bitrotted.  Let's see how bad they are.

Most annoyingly, 32-bit Android defines mode_t as 16 bits, but
stat.st_mode as 32-bits.
  • Loading branch information
asomers committed Apr 5, 2021
1 parent 6af11c1 commit 2ba6999
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 61 deletions.
7 changes: 2 additions & 5 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ task:
- cargo test --target i686-unknown-freebsd
before_cache_script: rm -rf $CARGO_HOME/registry/index

# Test OSX and iOS in a full VM
task:
matrix:
- name: OSX x86_64
Expand Down Expand Up @@ -162,24 +163,20 @@ task:
- name: Linux x32
env:
TARGET: x86_64-unknown-linux-gnux32
CHECK_TESTS: true
- name: NetBSD x86_64
env:
TARGET: x86_64-unknown-netbsd
- name: Fuchsia x86_64
env:
TARGET: x86_64-fuchsia
CHECK_TESTS: true
container:
image: rust:1.40
setup_script:
- rustup target add $TARGET
script:
- cargo +$TOOLCHAIN check --target $TARGET
- cargo +$TOOLCHAIN check --target $TARGET --release
- 'if [ "$CHECK_TESTS" == true ]; then cargo +$TOOLCHAIN check --all-targets --target $TARGET; fi'
# TODO: check the tests, too. The old Travis CI setup didn't do that, so
# they don't build on all platforms.
- cargo +$TOOLCHAIN check --all-targets --target $TARGET
before_cache_script: rm -rf $CARGO_HOME/registry/index

# illumos toolchain isn't available via rustup until 1.50
Expand Down
2 changes: 1 addition & 1 deletion test/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ mod test_pthread;
target_os = "netbsd",
target_os = "openbsd"))]
mod test_ptrace;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg(target_os = "linux")]
mod test_timerfd;
25 changes: 18 additions & 7 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ pub fn test_af_alg_aead() {
target_os = "netbsd"))]
#[test]
pub fn test_sendmsg_ipv4packetinfo() {
use cfg_if::cfg_if;
use nix::sys::uio::IoVec;
use nix::sys::socket::{socket, sendmsg, bind,
AddressFamily, SockType, SockFlag, SockAddr,
Expand All @@ -778,11 +779,21 @@ pub fn test_sendmsg_ipv4packetinfo() {
let iov = [IoVec::from_slice(&slice)];

if let InetAddr::V4(sin) = inet_addr {
let pi = libc::in_pktinfo {
ipi_ifindex: 0, /* Unspecified interface */
ipi_addr: libc::in_addr { s_addr: 0 },
ipi_spec_dst: sin.sin_addr,
};
cfg_if! {
if #[cfg(target_os = "netbsd")] {
drop(sin);
let pi = libc::in_pktinfo {
ipi_ifindex: 0, /* Unspecified interface */
ipi_addr: libc::in_addr { s_addr: 0 },
};
} else {
let pi = libc::in_pktinfo {
ipi_ifindex: 0, /* Unspecified interface */
ipi_addr: libc::in_addr { s_addr: 0 },
ipi_spec_dst: sin.sin_addr,
};
}
}

let cmsg = [ControlMessage::Ipv4PacketInfo(&pi)];

Expand Down Expand Up @@ -1472,13 +1483,13 @@ pub fn test_recv_ipv6pktinfo() {
Some(ControlMessageOwned::Ipv6PacketInfo(pktinfo)) => {
let i = if_nametoindex(lo_name.as_bytes()).expect("if_nametoindex");
assert_eq!(
pktinfo.ipi6_ifindex,
pktinfo.ipi6_ifindex as libc::c_uint,
i,
"unexpected ifindex (expected {}, got {})",
i,
pktinfo.ipi6_ifindex
);
}
},
_ => (),
}
assert!(cmsgs.next().is_none(), "unexpected additional control msg");
Expand Down
70 changes: 33 additions & 37 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,18 @@ fn test_readlink() {

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux_android {
use std::fs::File;
use std::io::prelude::*;
use std::io::{BufRead, BufReader, SeekFrom};
use std::io::SeekFrom;
use std::os::unix::prelude::*;

use libc::loff_t;

use nix::fcntl::*;
use nix::sys::stat::fstat;
use nix::sys::uio::IoVec;
use nix::unistd::{close, pipe, read, write};

use tempfile::{tempfile, NamedTempFile};
use tempfile::tempfile;
#[cfg(any(target_os = "linux"))]
use tempfile::NamedTempFile;

use crate::*;

Expand Down Expand Up @@ -206,6 +205,7 @@ mod linux_android {
close(wr).unwrap();
}

#[cfg(any(target_os = "linux"))]
#[test]
fn test_fallocate() {
let tmp = NamedTempFile::new().unwrap();
Expand All @@ -224,17 +224,11 @@ mod linux_android {
// they run under QEMU.

#[test]
#[cfg(not(any(target_arch = "aarch64",
target_arch = "arm",
target_arch = "armv7",
target_arch = "x86",
target_arch = "mips",
target_arch = "mips64",
target_arch = "mips64el",
target_arch = "powerpc64",
target_arch = "powerpc64le",
target_env = "musl")))]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
fn test_ofd_write_lock() {
use nix::sys::stat::fstat;
use std::mem;

let tmp = NamedTempFile::new().unwrap();

let fd = tmp.as_raw_fd();
Expand All @@ -247,13 +241,14 @@ mod linux_android {
}
let inode = fstat(fd).expect("fstat failed").st_ino as usize;

let mut flock = libc::flock {
l_type: libc::F_WRLCK as libc::c_short,
l_whence: libc::SEEK_SET as libc::c_short,
l_start: 0,
l_len: 0,
l_pid: 0,
let mut flock: libc::flock = unsafe {
mem::zeroed() // required for Linux/mips
};
flock.l_type = libc::F_WRLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
flock.l_start = 0;
flock.l_len = 0;
flock.l_pid = 0;
fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).expect("write lock failed");
assert_eq!(
Some(("OFDLCK".to_string(), "WRITE".to_string())),
Expand All @@ -266,17 +261,11 @@ mod linux_android {
}

#[test]
#[cfg(not(any(target_arch = "aarch64",
target_arch = "arm",
target_arch = "armv7",
target_arch = "x86",
target_arch = "mips",
target_arch = "mips64",
target_arch = "mips64el",
target_arch = "powerpc64",
target_arch = "powerpc64le",
target_env = "musl")))]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
fn test_ofd_read_lock() {
use nix::sys::stat::fstat;
use std::mem;

let tmp = NamedTempFile::new().unwrap();

let fd = tmp.as_raw_fd();
Expand All @@ -289,13 +278,14 @@ mod linux_android {
}
let inode = fstat(fd).expect("fstat failed").st_ino as usize;

let mut flock = libc::flock {
l_type: libc::F_RDLCK as libc::c_short,
l_whence: libc::SEEK_SET as libc::c_short,
l_start: 0,
l_len: 0,
l_pid: 0,
let mut flock: libc::flock = unsafe {
mem::zeroed() // required for Linux/mips
};
flock.l_type = libc::F_RDLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
flock.l_start = 0;
flock.l_len = 0;
flock.l_pid = 0;
fcntl(fd, FcntlArg::F_OFD_SETLKW(&flock)).expect("read lock failed");
assert_eq!(
Some(("OFDLCK".to_string(), "READ".to_string())),
Expand All @@ -307,7 +297,13 @@ mod linux_android {
assert_eq!(None, lock_info(inode));
}

#[cfg(all(target_os = "linux", not(target_env = "musl")))]
fn lock_info(inode: usize) -> Option<(String, String)> {
use std::{
fs::File,
io::BufReader
};

let file = File::open("/proc/locks").expect("open /proc/locks failed");
let buf = BufReader::new(file);

Expand Down
15 changes: 9 additions & 6 deletions test/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use std::time::{Duration, UNIX_EPOCH};
use std::path::Path;

#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
use libc::{S_IFMT, S_IFLNK, mode_t};
use libc::{S_IFMT, S_IFLNK};
#[cfg(not(target_os = "redox"))]
use libc::mode_t;

#[cfg(not(target_os = "redox"))]
use nix::{fcntl, Error};
Expand Down Expand Up @@ -69,6 +71,8 @@ fn assert_stat_results(stat_result: Result<FileStat>) {
}

#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
// (Android's st_blocks is ulonglong which is always non-negative.)
#[cfg_attr(target_os = "android", allow(unused_comparisons))]
fn assert_lstat_results(stat_result: Result<FileStat>) {
let stats = stat_result.expect("stat call failed");
assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
Expand All @@ -86,7 +90,6 @@ fn assert_lstat_results(stat_result: Result<FileStat>) {

// st_blocks depends on whether the machine's file system uses fast
// or slow symlinks, so just make sure it's not negative
// (Android's st_blocks is ulonglong which is always non-negative.)
assert!(stats.st_blocks >= 0);
}

Expand Down Expand Up @@ -159,14 +162,14 @@ fn test_fchmod() {
fchmod(file.as_raw_fd(), mode1).unwrap();

let file_stat1 = stat(&filename).unwrap();
assert_eq!(file_stat1.st_mode & 0o7777, mode1.bits());
assert_eq!(file_stat1.st_mode as mode_t & 0o7777, mode1.bits());

let mut mode2 = Mode::empty();
mode2.insert(Mode::S_IROTH);
fchmod(file.as_raw_fd(), mode2).unwrap();

let file_stat2 = stat(&filename).unwrap();
assert_eq!(file_stat2.st_mode & 0o7777, mode2.bits());
assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits());
}

#[test]
Expand All @@ -186,7 +189,7 @@ fn test_fchmodat() {
fchmodat(Some(dirfd), filename, mode1, FchmodatFlags::FollowSymlink).unwrap();

let file_stat1 = stat(&fullpath).unwrap();
assert_eq!(file_stat1.st_mode & 0o7777, mode1.bits());
assert_eq!(file_stat1.st_mode as mode_t & 0o7777, mode1.bits());

chdir(tempdir.path()).unwrap();

Expand All @@ -195,7 +198,7 @@ fn test_fchmodat() {
fchmodat(None, filename, mode2, FchmodatFlags::FollowSymlink).unwrap();

let file_stat2 = stat(&fullpath).unwrap();
assert_eq!(file_stat2.st_mode & 0o7777, mode2.bits());
assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits());
}

/// Asserts that the atime and mtime in a file's metadata match expected values.
Expand Down
12 changes: 7 additions & 5 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::os::unix::prelude::*;
#[cfg(not(target_os = "redox"))]
use std::path::Path;
use tempfile::{tempdir, tempfile};
use libc::{_exit, off_t};
use libc::{_exit, mode_t, off_t};

use crate::*;

Expand Down Expand Up @@ -102,7 +102,7 @@ fn test_mkfifo() {
mkfifo(&mkfifo_fifo, Mode::S_IRUSR).unwrap();

let stats = stat::stat(&mkfifo_fifo).unwrap();
let typ = stat::SFlag::from_bits_truncate(stats.st_mode);
let typ = stat::SFlag::from_bits_truncate(stats.st_mode as mode_t);
assert!(typ == SFlag::S_IFIFO);
}

Expand Down Expand Up @@ -629,10 +629,10 @@ fn test_sysconf_unsupported() {
#[test]
fn test_pipe() {
let (fd0, fd1) = pipe().unwrap();
let m0 = stat::SFlag::from_bits_truncate(stat::fstat(fd0).unwrap().st_mode);
let m0 = stat::SFlag::from_bits_truncate(stat::fstat(fd0).unwrap().st_mode as mode_t);
// S_IFIFO means it's a pipe
assert_eq!(m0, SFlag::S_IFIFO);
let m1 = stat::SFlag::from_bits_truncate(stat::fstat(fd1).unwrap().st_mode);
let m1 = stat::SFlag::from_bits_truncate(stat::fstat(fd1).unwrap().st_mode as mode_t);
assert_eq!(m1, SFlag::S_IFIFO);
}

Expand Down Expand Up @@ -926,7 +926,9 @@ fn test_linkat_follow_symlink() {
let newfilestat = stat::stat(&newfilepath).unwrap();

// Check the file type of the new link
assert!((stat::SFlag::from_bits_truncate(newfilestat.st_mode) & SFlag::S_IFMT) == SFlag::S_IFREG);
assert_eq!((stat::SFlag::from_bits_truncate(newfilestat.st_mode as mode_t) & SFlag::S_IFMT),
SFlag::S_IFREG
);

// Check the number of hard links to the original file
assert_eq!(newfilestat.st_nlink, 2);
Expand Down

0 comments on commit 2ba6999

Please sign in to comment.