From 2e7a140223062eefc9122e38b91624cbca60aff4 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Mon, 27 Nov 2023 08:02:51 +0800 Subject: [PATCH 1/3] refactor: cfg for test/* --- test/common/mod.rs | 4 ++-- test/sys/mod.rs | 11 ++------- test/sys/test_socket.rs | 7 +----- test/sys/test_sockopt.rs | 17 +++++--------- test/test.rs | 15 +++--------- test/test_fcntl.rs | 12 ++++------ test/test_net.rs | 6 +---- test/test_poll.rs | 7 +----- test/test_sendfile.rs | 9 ++------ test/test_stat.rs | 6 ++--- test/test_time.rs | 24 +++---------------- test/test_unistd.rs | 50 +++++++++------------------------------- 12 files changed, 38 insertions(+), 130 deletions(-) diff --git a/test/common/mod.rs b/test/common/mod.rs index 7a9cc23e56..9268a79497 100644 --- a/test/common/mod.rs +++ b/test/common/mod.rs @@ -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}; @@ -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") { diff --git a/test/sys/mod.rs b/test/sys/mod.rs index 8812cdd6cd..3a6707ba1d 100644 --- a/test/sys/mod.rs +++ b/test/sys/mod.rs @@ -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; diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index ffa54a98c2..53cd3ece7d 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -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; diff --git a/test/sys/test_sockopt.rs b/test/sys/test_sockopt.rs index ec25a7e93b..4950213983 100644 --- a/test/sys/test_sockopt.rs +++ b/test/sys/test_sockopt.rs @@ -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(); @@ -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 { @@ -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; @@ -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(); @@ -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, @@ -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)] diff --git a/test/test.rs b/test/test.rs index 388a0a42e8..fc6b4b3e32 100644 --- a/test/test.rs +++ b/test/test.rs @@ -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" @@ -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; diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index df323c4394..6572e8af8d 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -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 @@ -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", @@ -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 { diff --git a/test/test_net.rs b/test/test_net.rs index 00708ace38..faba8503fe 100644 --- a/test/test_net.rs +++ b/test/test_net.rs @@ -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")] diff --git a/test/test_poll.rs b/test/test_poll.rs index 4227f59994..fcb325494e 100644 --- a/test/test_poll.rs +++ b/test/test_poll.rs @@ -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; diff --git a/test/test_sendfile.rs b/test/test_sendfile.rs index df3445b5c5..6333bf8662 100644 --- a/test/test_sendfile.rs +++ b/test/test_sendfile.rs @@ -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; } diff --git a/test/test_stat.rs b/test/test_stat.rs index 58238249d9..0dd6fe89c5 100644 --- a/test/test_stat.rs +++ b/test/test_stat.rs @@ -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" @@ -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" diff --git a/test/test_time.rs b/test/test_time.rs index 5f76e61a2d..6d7dfbedc0 100644 --- a/test/test_time.rs +++ b/test/test_time.rs @@ -1,10 +1,4 @@ -#[cfg(any( - target_os = "freebsd", - target_os = "dragonfly", - target_os = "linux", - target_os = "android", - target_os = "emscripten", -))] +#[cfg(any(freebsdlike, linux_android, target_os = "emscripten"))] use nix::time::clock_getcpuclockid; use nix::time::{clock_gettime, ClockId}; @@ -19,13 +13,7 @@ pub fn test_clock_gettime() { clock_gettime(ClockId::CLOCK_REALTIME).expect("assertion failed"); } -#[cfg(any( - target_os = "freebsd", - target_os = "dragonfly", - target_os = "linux", - target_os = "android", - target_os = "emscripten", -))] +#[cfg(any(freebsdlike, linux_android, target_os = "emscripten"))] #[test] pub fn test_clock_getcpuclockid() { let clock_id = clock_getcpuclockid(nix::unistd::Pid::this()).unwrap(); @@ -43,13 +31,7 @@ pub fn test_clock_id_now() { ClockId::CLOCK_REALTIME.now().unwrap(); } -#[cfg(any( - target_os = "freebsd", - target_os = "dragonfly", - target_os = "linux", - target_os = "android", - target_os = "emscripten", -))] +#[cfg(any(freebsdlike, linux_android, target_os = "emscripten"))] #[test] pub fn test_clock_id_pid_cpu_clock_id() { ClockId::pid_cpu_clock_id(nix::unistd::Pid::this()) diff --git a/test/test_unistd.rs b/test/test_unistd.rs index f13bcf7061..77eafd4b2c 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -430,10 +430,7 @@ cfg_if! { if #[cfg(target_os = "android")] { execve_test_factory!(test_execve, execve, CString::new("/system/bin/sh").unwrap().as_c_str()); execve_test_factory!(test_fexecve, fexecve, File::open("/system/bin/sh").unwrap().into_raw_fd()); - } else if #[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "hurd", - target_os = "linux"))] { + } else if #[cfg(any(freebsdlike, target_os = "linux", target_os = "hurd"))] { // These tests frequently fail on musl, probably due to // https://github.com/nix-rust/nix/issues/555 execve_test_factory!(test_execve, execve, CString::new("/bin/sh").unwrap().as_c_str()); @@ -441,8 +438,7 @@ cfg_if! { } else if #[cfg(any(solarish, apple_targets, target_os = "netbsd", - target_os = "openbsd", - target_os = "solaris"))] { + target_os = "openbsd"))] { execve_test_factory!(test_execve, execve, CString::new("/bin/sh").unwrap().as_c_str()); // No fexecve() on ios, macos, NetBSD, OpenBSD. } @@ -610,7 +606,7 @@ fn test_lseek64() { } cfg_if! { - if #[cfg(any(target_os = "android", target_os = "linux"))] { + if #[cfg(linux_android)] { macro_rules! require_acct{ () => { require_capability!("test_acct", CAP_SYS_PACCT); @@ -712,13 +708,7 @@ fn test_sysconf_unsupported() { assert!(open_max.expect("sysconf failed").is_none()) } -#[cfg(any( - target_os = "android", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "linux", - target_os = "openbsd" -))] +#[cfg(any(linux_android, freebsdlike, target_os = "openbsd"))] #[test] fn test_getresuid() { let resuids = getresuid().unwrap(); @@ -727,13 +717,7 @@ fn test_getresuid() { assert_ne!(resuids.saved.as_raw(), libc::uid_t::MAX); } -#[cfg(any( - target_os = "android", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "linux", - target_os = "openbsd" -))] +#[cfg(any(linux_android, freebsdlike, target_os = "openbsd"))] #[test] fn test_getresgid() { let resgids = getresgid().unwrap(); @@ -761,16 +745,12 @@ fn test_pipe() { // pipe2(2) is the same as pipe(2), except it allows setting some flags. Check // that we can set a flag. #[cfg(any( - target_os = "android", - target_os = "dragonfly", - target_os = "emscripten", - target_os = "freebsd", + linux_android, + freebsdlike, solarish, - target_os = "linux", - target_os = "netbsd", - target_os = "openbsd", + netbsdlike, + target_os = "emscripten", target_os = "redox", - target_os = "solaris" ))] #[test] fn test_pipe2() { @@ -1367,11 +1347,7 @@ fn test_faccessat_file_exists() { } #[test] -#[cfg(any( - all(target_os = "linux", not(target_env = "uclibc")), - target_os = "freebsd", - target_os = "dragonfly" -))] +#[cfg(any(all(target_os = "linux", not(target_env = "uclibc")), freebsdlike))] fn test_eaccess_not_existing() { let tempdir = tempdir().unwrap(); let dir = tempdir.path().join("does_not_exist.txt"); @@ -1382,11 +1358,7 @@ fn test_eaccess_not_existing() { } #[test] -#[cfg(any( - all(target_os = "linux", not(target_env = "uclibc")), - target_os = "freebsd", - target_os = "dragonfly" -))] +#[cfg(any(all(target_os = "linux", not(target_env = "uclibc")), freebsdlike))] fn test_eaccess_file_exists() { let tempdir = tempdir().unwrap(); let path = tempdir.path().join("does_exist.txt"); From 4abf7f419cf79c62da3db71f9b9317686b08842f Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Mon, 27 Nov 2023 08:08:02 +0800 Subject: [PATCH 2/3] one more netbsdlike --- test/test_unistd.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 77eafd4b2c..354f816362 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -435,10 +435,7 @@ cfg_if! { // https://github.com/nix-rust/nix/issues/555 execve_test_factory!(test_execve, execve, CString::new("/bin/sh").unwrap().as_c_str()); execve_test_factory!(test_fexecve, fexecve, File::open("/bin/sh").unwrap().into_raw_fd()); - } else if #[cfg(any(solarish, - apple_targets, - target_os = "netbsd", - target_os = "openbsd"))] { + } else if #[cfg(any(solarish, apple_targets, netbsdlike))] { execve_test_factory!(test_execve, execve, CString::new("/bin/sh").unwrap().as_c_str()); // No fexecve() on ios, macos, NetBSD, OpenBSD. } From 0b2e61c2f0ef1662f7ce4ce28328b3466df9a855 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Mon, 4 Dec 2023 08:51:26 +0800 Subject: [PATCH 3/3] more cfg --- test/sys/test_socket.rs | 52 ++++++++--------------------------------- 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 53cd3ece7d..736fae1937 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -618,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}; @@ -699,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}; @@ -1270,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::{ @@ -1424,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::{ @@ -1772,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( @@ -1982,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() { @@ -2068,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() { @@ -2154,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( @@ -2458,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}; @@ -2552,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::*;