Skip to content

Commit 7f6024b

Browse files
authored
Fix sockopt tests (#1534)
* Fix cfgs in sockopt tests `rustix::time` and the txtime socket options are only available with the `time` feature, but they are unconditionally used in sockopt tests. Mark them with `cfg(feature = "time")` to enable compiling sockopt tests with just the `net` feature enabled. Otherwise `cargo test --features net` would end up with a compile error. Fixes: 65b04ae ("Add support for SO_TXTIME / SCM_TXTIME (#1409)") * Test `socket_passcred` only for Unix domain sockets Since Linux 6.16, `SO_PASSCRED` is allowed only for `AF_UNIX`, `AF_NETLINK` and `AF_BLUETOOTH` sockets. The tests used to get/set this option for IP sockets, which doesn't work with new kernels, so make the tests use a Unix domain socket instead. On new kernels, the tests fail with: ``` ---- sockopt::test_sockopts_ipv6 stdout ---- thread 'sockopt::test_sockopts_ipv6' panicked at tests/net/sockopt.rs:44:42: called `Result::unwrap()` on an `Err` value: Os { code: 95, kind: Unsupported, message: "Operation not supported" } ---- sockopt::test_sockopts_ipv4 stdout ---- thread 'sockopt::test_sockopts_ipv4' panicked at tests/net/sockopt.rs:44:42: called `Result::unwrap()` on an `Err` value: Os { code: 95, kind: Unsupported, message: "Operation not supported" } ``` The restriction was introduced to Linux in this commit: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7d8d93fdde50b86bbbf46a203c368ed320e729ab
1 parent 8a2472a commit 7f6024b

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

tests/net/sockopt.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use rustix::io;
99
target_env = "newlib"
1010
))]
1111
use rustix::net::ipproto;
12-
#[cfg(target_os = "linux")]
12+
#[cfg(all(target_os = "linux", feature = "time"))]
1313
use rustix::net::TxTimeFlags;
1414
use rustix::net::{sockopt, AddressFamily, SocketType};
15-
#[cfg(target_os = "linux")]
15+
#[cfg(all(target_os = "linux", feature = "time"))]
1616
use rustix::time::ClockId;
1717
use std::net::Ipv4Addr;
1818
use std::time::Duration;
@@ -40,8 +40,6 @@ fn test_sockopts_socket(s: &OwnedFd) {
4040
assert!(!sockopt::socket_broadcast(s).unwrap());
4141
// On a new socket we shouldn't have a linger yet.
4242
assert!(sockopt::socket_linger(s).unwrap().is_none());
43-
#[cfg(linux_kernel)]
44-
assert!(!sockopt::socket_passcred(s).unwrap());
4543

4644
// On a new socket we shouldn't have an error yet.
4745
assert_eq!(sockopt::socket_error(s).unwrap(), Ok(()));
@@ -119,15 +117,6 @@ fn test_sockopts_socket(s: &OwnedFd) {
119117
// Check that we have a linger of at least the time we set.
120118
assert!(sockopt::socket_linger(s).unwrap().unwrap() >= Duration::new(1, 1));
121119

122-
#[cfg(linux_kernel)]
123-
{
124-
// Set the passcred flag;
125-
sockopt::set_socket_passcred(s, true).unwrap();
126-
127-
// Check that the passcred flag is set.
128-
assert!(sockopt::socket_passcred(s).unwrap());
129-
}
130-
131120
// Set the receive buffer size.
132121
let size = sockopt::socket_recv_buffer_size(s).unwrap();
133122
sockopt::set_socket_recv_buffer_size(s, size * 2).unwrap();
@@ -527,6 +516,18 @@ fn test_sockopts_ipv6() {
527516
test_sockopts_tcp(&s);
528517
}
529518

519+
#[cfg(linux_kernel)]
520+
#[test]
521+
fn test_socket_passcred() {
522+
crate::init();
523+
524+
let s = rustix::net::socket(AddressFamily::UNIX, SocketType::STREAM, None).unwrap();
525+
526+
assert_eq!(sockopt::socket_passcred(&s), Ok(false));
527+
sockopt::set_socket_passcred(&s, true).unwrap();
528+
assert_eq!(sockopt::socket_passcred(&s), Ok(true));
529+
}
530+
530531
#[cfg(any(linux_kernel, target_os = "cygwin"))]
531532
#[test]
532533
fn test_socketopts_ip_mtu() {
@@ -668,7 +669,7 @@ fn test_sockopts_multicast_ifv6() {
668669
}
669670

670671
#[test]
671-
#[cfg(target_os = "linux")]
672+
#[cfg(all(target_os = "linux", feature = "time"))]
672673
fn test_sockopts_txtime() {
673674
crate::init();
674675

0 commit comments

Comments
 (0)