From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/rustix/tests/net/sockopt.rs | 158 +++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 vendor/rustix/tests/net/sockopt.rs (limited to 'vendor/rustix/tests/net/sockopt.rs') diff --git a/vendor/rustix/tests/net/sockopt.rs b/vendor/rustix/tests/net/sockopt.rs new file mode 100644 index 000000000..8e4cf52e8 --- /dev/null +++ b/vendor/rustix/tests/net/sockopt.rs @@ -0,0 +1,158 @@ +#[test] +fn test_sockopts() { + use rustix::net::{AddressFamily, Protocol, SocketType}; + use std::time::Duration; + + let s = + rustix::net::socket(AddressFamily::INET, SocketType::STREAM, Protocol::default()).unwrap(); + + // On a new socket we shouldn't have a timeout yet. + assert!( + rustix::net::sockopt::get_socket_timeout(&s, rustix::net::sockopt::Timeout::Recv) + .unwrap() + .is_none() + ); + assert_eq!( + rustix::net::sockopt::get_socket_type(&s).unwrap(), + SocketType::STREAM + ); + #[cfg(not(windows))] + assert_eq!( + rustix::net::sockopt::get_socket_broadcast(&s).unwrap(), + false + ); + // On a new socket we shouldn't have a linger yet. + assert!(rustix::net::sockopt::get_socket_linger(&s) + .unwrap() + .is_none()); + #[cfg(any(target_os = "android", target_os = "linux"))] + assert_eq!( + rustix::net::sockopt::get_socket_passcred(&s).unwrap(), + false + ); + assert_ne!(rustix::net::sockopt::get_ip_ttl(&s).unwrap(), 0); + assert_ne!(rustix::net::sockopt::get_ip_ttl(&s).unwrap(), 77); + #[cfg(not(any( + windows, + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + assert_eq!( + rustix::net::sockopt::get_ip_multicast_loop(&s).unwrap(), + true + ); + #[cfg(not(any( + windows, + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + assert_eq!(rustix::net::sockopt::get_ip_multicast_ttl(&s).unwrap(), 1); + assert_eq!(rustix::net::sockopt::get_tcp_nodelay(&s).unwrap(), false); + + // Set a timeout. + rustix::net::sockopt::set_socket_timeout( + &s, + rustix::net::sockopt::Timeout::Recv, + Some(Duration::new(1, 1)), + ) + .unwrap(); + + // Check that we have a timeout of at least the time we set. + if cfg!(not(target_os = "freebsd")) { + assert!( + rustix::net::sockopt::get_socket_timeout(&s, rustix::net::sockopt::Timeout::Recv) + .unwrap() + .unwrap() + >= Duration::new(1, 1) + ); + } else { + // On FreeBSD <= 12, it appears the system rounds the timeout down. + assert!( + rustix::net::sockopt::get_socket_timeout(&s, rustix::net::sockopt::Timeout::Recv) + .unwrap() + .unwrap() + >= Duration::new(1, 0) + ); + } + + #[cfg(not(windows))] + { + // Set the broadcast flag; + rustix::net::sockopt::set_socket_broadcast(&s, true).unwrap(); + + // Check that the broadcast flag is set. This has no effect on stream + // sockets, and not all platforms even remember the value. + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + assert_eq!( + rustix::net::sockopt::get_socket_broadcast(&s).unwrap(), + true + ); + } + + // Set a linger. + rustix::net::sockopt::set_socket_linger(&s, Some(Duration::new(1, 1))).unwrap(); + + // Check that we have a linger of at least the time we set. + assert!( + dbg!(rustix::net::sockopt::get_socket_linger(&s) + .unwrap() + .unwrap()) + >= Duration::new(1, 1) + ); + + #[cfg(any(target_os = "android", target_os = "linux"))] + { + // Set the passcred flag; + rustix::net::sockopt::set_socket_passcred(&s, true).unwrap(); + + // Check that the passcred flag is set. + assert_eq!(rustix::net::sockopt::get_socket_passcred(&s).unwrap(), true); + } + + // Set the ip ttl. + rustix::net::sockopt::set_ip_ttl(&s, 77).unwrap(); + + // Check the ip ttl. + assert_eq!(rustix::net::sockopt::get_ip_ttl(&s).unwrap(), 77); + + #[cfg(not(any( + windows, + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd", + )))] + { + // Set the multicast loop flag; + rustix::net::sockopt::set_ip_multicast_loop(&s, false).unwrap(); + + // Check that the multicast loop flag is set. + assert_eq!( + rustix::net::sockopt::get_ip_multicast_loop(&s).unwrap(), + false + ); + } + + // Set the nodelay flag; + rustix::net::sockopt::set_tcp_nodelay(&s, true).unwrap(); + + // Check that the nodelay flag is set. + assert_eq!(rustix::net::sockopt::get_tcp_nodelay(&s).unwrap(), true); +} -- cgit v1.2.3