Skip to content

Commit

Permalink
refactor: cfg for test/* (#2230)
Browse files Browse the repository at this point in the history
* refactor: cfg for test/*

* one more netbsdlike

* more cfg
  • Loading branch information
SteveLauC authored Dec 9, 2023
1 parent 2ab5558 commit 6961f0f
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 175 deletions.
4 changes: 2 additions & 2 deletions test/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macro_rules! skip {
}

cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux"))] {
if #[cfg(linux_android)] {
#[macro_export] macro_rules! require_capability {
($name:expr, $capname:ident) => {
use ::caps::{Capability, CapSet, has_cap};
Expand Down Expand Up @@ -87,7 +87,7 @@ macro_rules! skip_if_not_root {
}

cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux"))] {
if #[cfg(linux_android)] {
#[macro_export] macro_rules! skip_if_seccomp {
($name:expr) => {
if let Ok(s) = std::fs::read_to_string("/proc/self/status") {
Expand Down
11 changes: 2 additions & 9 deletions test/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,8 @@ mod test_fanotify;
#[cfg(target_os = "linux")]
mod test_inotify;
mod test_pthread;
#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
apple_targets,
target_os = "netbsd",
target_os = "openbsd"
))]

#[cfg(any(linux_android, freebsdlike, netbsdlike, apple_targets))]
mod test_ptrace;
#[cfg(linux_android)]
mod test_timerfd;
59 changes: 11 additions & 48 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,7 @@ mod recvfrom {
}
}

#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "netbsd",
))]
#[cfg(any(linux_android, target_os = "freebsd", target_os = "netbsd"))]
#[test]
pub fn udp_sendmmsg() {
use std::io::IoSlice;
Expand Down Expand Up @@ -623,12 +618,7 @@ mod recvfrom {
assert_eq!(AddressFamily::Inet, from.unwrap().family().unwrap());
}

#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "netbsd",
))]
#[cfg(any(linux_android, target_os = "freebsd", target_os = "netbsd"))]
#[test]
pub fn udp_recvmmsg() {
use nix::sys::socket::{recvmmsg, MsgFlags};
Expand Down Expand Up @@ -704,12 +694,7 @@ mod recvfrom {
send_thread.join().unwrap();
}

#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "netbsd",
))]
#[cfg(any(linux_android, target_os = "freebsd", target_os = "netbsd"))]
#[test]
pub fn udp_recvmmsg_dontwait_short_read() {
use nix::sys::socket::{recvmmsg, MsgFlags};
Expand Down Expand Up @@ -1275,12 +1260,7 @@ pub fn test_sendmsg_ipv6packetinfo() {
//
// Note that binding to 0.0.0.0 is *required* on FreeBSD; sendmsg
// returns EINVAL otherwise. (See FreeBSD's ip(4) man page.)
#[cfg(any(
target_os = "netbsd",
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
))]
#[cfg(any(freebsdlike, netbsdlike))]
#[test]
pub fn test_sendmsg_ipv4sendsrcaddr() {
use nix::sys::socket::{
Expand Down Expand Up @@ -1429,12 +1409,7 @@ pub fn test_sendmsg_empty_cmsgs() {
}
}

#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
))]
#[cfg(any(linux_android, freebsdlike))]
#[test]
fn test_scm_credentials() {
use nix::sys::socket::{
Expand Down Expand Up @@ -1777,12 +1752,7 @@ fn loopback_address(
})
}

#[cfg(any(
target_os = "android",
apple_targets,
target_os = "linux",
target_os = "netbsd",
))]
#[cfg(any(linux_android, apple_targets, target_os = "netbsd"))]
// qemu doesn't seem to be emulating this correctly in these architectures
#[cfg_attr(
all(
Expand Down Expand Up @@ -1987,7 +1957,7 @@ pub fn test_recvif() {
}
}

#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
#[cfg(any(linux_android, target_os = "freebsd"))]
#[cfg_attr(qemu, ignore)]
#[test]
pub fn test_recvif_ipv4() {
Expand Down Expand Up @@ -2073,7 +2043,7 @@ pub fn test_recvif_ipv4() {
}
}

#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
#[cfg(any(linux_android, target_os = "freebsd"))]
#[cfg_attr(qemu, ignore)]
#[test]
pub fn test_recvif_ipv6() {
Expand Down Expand Up @@ -2159,14 +2129,7 @@ pub fn test_recvif_ipv6() {
}
}

#[cfg(any(
target_os = "android",
target_os = "freebsd",
apple_targets,
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
))]
#[cfg(any(linux_android, target_os = "freebsd", apple_targets, netbsdlike))]
// qemu doesn't seem to be emulating this correctly in these architectures
#[cfg_attr(
all(
Expand Down Expand Up @@ -2463,7 +2426,7 @@ fn test_recvmmsg_timestampns() {
// Disable the test on emulated platforms because it fails in Cirrus-CI. Lack
// of QEMU support is suspected.
#[cfg_attr(qemu, ignore)]
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
#[cfg(any(linux_android, target_os = "fuchsia"))]
#[test]
fn test_recvmsg_rxq_ovfl() {
use nix::sys::socket::sockopt::{RcvBuf, RxqOvfl};
Expand Down Expand Up @@ -2557,7 +2520,7 @@ fn test_recvmsg_rxq_ovfl() {
assert_eq!(drop_counter, 1);
}

#[cfg(any(target_os = "linux", target_os = "android",))]
#[cfg(linux_android)]
mod linux_errqueue {
use super::FromStr;
use nix::sys::socket::*;
Expand Down
17 changes: 6 additions & 11 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn test_so_tcp_maxseg() {
// platforms keep it even lower. This might fail if you've tuned your initial MSS to be larger
// than 700
cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux"))] {
if #[cfg(linux_android)] {
let segsize: u32 = 873;
assert!(initial < segsize);
setsockopt(&rsock, sockopt::TcpMaxSeg, &segsize).unwrap();
Expand All @@ -152,7 +152,7 @@ fn test_so_tcp_maxseg() {
// Actual max segment size takes header lengths into account, max IPv4 options (60 bytes) + max
// TCP options (40 bytes) are subtracted from the requested maximum as a lower boundary.
cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux"))] {
if #[cfg(linux_android)] {
assert!((segsize - 100) <= actual);
assert!(actual <= segsize);
} else {
Expand All @@ -177,7 +177,7 @@ fn test_so_type() {

/// getsockopt(_, sockopt::SockType) should gracefully handle unknown socket
/// types. Regression test for https://github.com/nix-rust/nix/issues/1819
#[cfg(any(target_os = "android", target_os = "linux",))]
#[cfg(linux_android)]
#[test]
fn test_so_type_unknown() {
use nix::errno::Errno;
Expand Down Expand Up @@ -254,12 +254,7 @@ fn test_so_tcp_keepalive() {
setsockopt(&fd, sockopt::KeepAlive, &true).unwrap();
assert!(getsockopt(&fd, sockopt::KeepAlive).unwrap());

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux"
))]
#[cfg(any(linux_android, freebsdlike))]
{
let x = getsockopt(&fd, sockopt::TcpKeepIdle).unwrap();
setsockopt(&fd, sockopt::TcpKeepIdle, &(x + 1)).unwrap();
Expand Down Expand Up @@ -303,7 +298,7 @@ fn test_get_mtu() {
}

#[test]
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
#[cfg(any(linux_android, target_os = "freebsd"))]
fn test_ttl_opts() {
let fd4 = socket(
AddressFamily::Inet,
Expand Down Expand Up @@ -369,7 +364,7 @@ fn test_dontfrag_opts() {
}

#[test]
#[cfg(any(target_os = "android", apple_targets, target_os = "linux",))]
#[cfg(any(linux_android, apple_targets))]
// Disable the test under emulation because it fails in Cirrus-CI. Lack
// of QEMU support is suspected.
#[cfg_attr(qemu, ignore)]
Expand Down
15 changes: 3 additions & 12 deletions test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ mod test_fcntl;
#[cfg(linux_android)]
mod test_kmod;
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
freebsdlike,
target_os = "fushsia",
target_os = "linux",
target_os = "netbsd"
Expand All @@ -32,20 +31,12 @@ mod test_poll;
mod test_pty;
mod test_resource;
#[cfg(any(
target_os = "android",
linux_android,
target_os = "dragonfly",
all(target_os = "freebsd", fbsd14),
target_os = "linux"
))]
mod test_sched;
#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
apple_targets,
target_os = "linux",
solarish
))]
#[cfg(any(linux_android, freebsdlike, apple_targets, solarish))]
mod test_sendfile;
mod test_stat;
mod test_time;
Expand Down
12 changes: 4 additions & 8 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,9 @@ fn test_readlink() {
/// The from_offset should be updated by the call to reflect
/// the 3 bytes read (6).
#[cfg(any(
target_os = "linux",
linux_android,
// Not available until FreeBSD 13.0
all(target_os = "freebsd", fbsd14),
target_os = "android"
))]
#[test]
// QEMU does not support copy_file_range. Skip under qemu
Expand Down Expand Up @@ -473,8 +472,7 @@ mod linux_android {
}

#[cfg(any(
target_os = "linux",
target_os = "android",
linux_android,
target_os = "emscripten",
target_os = "fuchsia",
target_os = "wasi",
Expand Down Expand Up @@ -511,13 +509,11 @@ mod test_posix_fadvise {
}

#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "dragonfly",
linux_android,
freebsdlike,
target_os = "emscripten",
target_os = "fuchsia",
target_os = "wasi",
target_os = "freebsd"
))]
mod test_posix_fallocate {

Expand Down
6 changes: 1 addition & 5 deletions test/test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use nix::net::if_::*;
#[cfg(linux_android)]
const LOOPBACK: &[u8] = b"lo";

#[cfg(not(any(
target_os = "android",
target_os = "linux",
target_os = "haiku"
)))]
#[cfg(not(any(linux_android, target_os = "haiku")))]
const LOOPBACK: &[u8] = b"lo0";

#[cfg(target_os = "haiku")]
Expand Down
7 changes: 1 addition & 6 deletions test/test_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ fn test_poll() {
// ppoll(2) is the same as poll except for how it handles timeouts and signals.
// Repeating the test for poll(2) should be sufficient to check that our
// bindings are correct.
#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux"
))]
#[cfg(any(linux_android, freebsdlike))]
#[test]
fn test_ppoll() {
use nix::poll::ppoll;
Expand Down
9 changes: 2 additions & 7 deletions test/test_sendfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ use nix::sys::sendfile::*;
use tempfile::tempfile;

cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux"))] {
if #[cfg(linux_android)] {
use nix::unistd::{pipe, read};
use std::os::unix::io::AsRawFd;
} else if #[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
apple_targets,
solarish
))] {
} else if #[cfg(any(freebsdlike, apple_targets, solarish))] {
use std::net::Shutdown;
use std::os::unix::net::UnixStream;
}
Expand Down
6 changes: 2 additions & 4 deletions test/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ fn test_mkdirat_fail() {

#[test]
#[cfg(not(any(
target_os = "dragonfly",
target_os = "freebsd",
freebsdlike,
apple_targets,
target_os = "haiku",
target_os = "redox"
Expand All @@ -381,9 +380,8 @@ fn test_mknod() {

#[test]
#[cfg(not(any(
target_os = "dragonfly",
target_os = "freebsd",
solarish,
freebsdlike,
apple_targets,
target_os = "haiku",
target_os = "redox"
Expand Down
Loading

0 comments on commit 6961f0f

Please sign in to comment.