diff --git a/Cargo.lock b/Cargo.lock index 042b65de85..4ab551af9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2285,6 +2285,7 @@ dependencies = [ "litebox", "litebox_broker_local_userland", "litebox_broker_transport_windows_userland", + "litebox_broker_userland", "litebox_common_linux", "litebox_platform_windows_userland", "litebox_shim_linux", diff --git a/litebox_broker_userland/Cargo.toml b/litebox_broker_userland/Cargo.toml index d950b5eb7b..459eae2cb7 100644 --- a/litebox_broker_userland/Cargo.toml +++ b/litebox_broker_userland/Cargo.toml @@ -12,6 +12,8 @@ litebox_broker_protocol = { path = "../litebox_broker_protocol", version = "0.1. litebox_broker_transport = { path = "../litebox_broker_transport", version = "0.1.0" } litebox_platform = { path = "../litebox_platform", version = "0.1.0" } litebox_util_log = { path = "../litebox_util_log", version = "0.1.0" } +# Bound TCP sockets that never listen, for `test-support`. +socket2 = { version = "0.6.3", default-features = false, optional = true } [target.'cfg(target_os = "linux")'.dependencies] libc = { version = "0.2.177", default-features = false } @@ -31,6 +33,9 @@ socket2 = { version = "0.6.3", default-features = false } windows-sys = { version = "0.60.2", features = ["Win32_Foundation", "Win32_Media", "Win32_Networking_WinSock", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_JobObjects", "Win32_System_Memory", "Win32_System_Threading"] } [features] +# Loopback ports that refuse what is sent to them, for tests here and in crates +# that use the broker. +test-support = ["dep:socket2"] lock_tracing = [ "litebox_platform/lock_tracing", "litebox_broker_platform_linux_userland/lock_tracing", diff --git a/litebox_broker_userland/src/lib.rs b/litebox_broker_userland/src/lib.rs index 2f6971ef34..bc3a2b9d45 100644 --- a/litebox_broker_userland/src/lib.rs +++ b/litebox_broker_userland/src/lib.rs @@ -38,4 +38,6 @@ pub mod hybrid_vsock; pub mod random; pub mod stdio; mod terminal; +#[cfg(all(any(test, feature = "test-support"), any(target_os = "linux", windows)))] +pub mod test_support; mod timer; diff --git a/litebox_broker_userland/src/socket/host/windows.rs b/litebox_broker_userland/src/socket/host/windows.rs index 19aceb5880..a89d1a6378 100644 --- a/litebox_broker_userland/src/socket/host/windows.rs +++ b/litebox_broker_userland/src/socket/host/windows.rs @@ -943,6 +943,7 @@ mod tests { }; use crate::fine_timer::DEFAULT_TIMER_PERIOD; use crate::socket::direct::RawParts; + use crate::test_support::refusing_udp_address; use litebox_broker_core::BrokerError; use litebox_platform::buffer::RawBytes; @@ -1046,11 +1047,7 @@ mod tests { /// destination as the peer. #[test] fn a_failed_reconnect_keeps_the_peers_icmp_errors() { - let closed = std::net::UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let std::net::SocketAddr::V4(peer) = closed.local_addr().unwrap() else { - unreachable!() - }; - drop(closed); + let (_refusing, peer) = refusing_udp_address(); let socket = Socket::udp().unwrap(); socket.connect(peer).unwrap(); let unconnectable = std::net::SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 9); diff --git a/litebox_broker_userland/src/socket/tests/mod.rs b/litebox_broker_userland/src/socket/tests/mod.rs index 4d602a79a3..5c0224946c 100644 --- a/litebox_broker_userland/src/socket/tests/mod.rs +++ b/litebox_broker_userland/src/socket/tests/mod.rs @@ -592,25 +592,6 @@ fn wait_until_connected( } } -fn wait_until_failed( - process: &litebox_broker_core::BrokerProcess, - handle: ObjectHandle, - publications: &Receiver<(ObjectHandle, ReadinessFlags)>, -) -> SocketError { - // Like a guest, observe the failure through readiness, which queues it. - // Readiness can show the transport's error before the broker has seen the - // failure, so check once more to queue it before `status` consumes it. - wait_until_ready(process, publications, handle, ReadinessFlags::ERROR); - process.check_readiness(handle).unwrap(); - match litebox_broker_core::socket::status(process, handle) - .unwrap() - .status - { - SocketConnectionStatus::Failed(error) => error, - status => panic!("unexpected connect status: {status:?}"), - } -} - fn wait_for_end_of_stream( process: &litebox_broker_core::BrokerProcess, handle: ObjectHandle, diff --git a/litebox_broker_userland/src/socket/tests/tcp.rs b/litebox_broker_userland/src/socket/tests/tcp.rs index f96c525fdd..80e6a63949 100644 --- a/litebox_broker_userland/src/socket/tests/tcp.rs +++ b/litebox_broker_userland/src/socket/tests/tcp.rs @@ -389,33 +389,6 @@ fn provider_drives_a_loopback_tcp_socket() { .contains(ReadinessFlags::ERROR) ); - let refused_listener = TcpListener::bind("127.0.0.1:0").unwrap(); - let refused_address = refused_listener.local_addr().unwrap(); - drop(refused_listener); - let refused_handle = create_socket(&process, readiness.clone()); - let refused_connect = litebox_broker_core::socket::connect( - &process, - refused_handle, - gateway_address(socket_address_v4(refused_address)), - ) - .unwrap(); - assert!(matches!( - refused_connect, - SocketOutcome::Completed( - SocketConnectionStatus::Connecting | SocketConnectionStatus::Failed(_) - ) - )); - assert_eq!( - wait_until_failed(&process, refused_handle, &publications), - SocketError::ConnectionRefused - ); - assert!( - process - .check_readiness(refused_handle) - .unwrap() - .contains(ReadinessFlags::ERROR) - ); - let abort_handle = create_socket(&process, readiness); let abort_connect = litebox_broker_core::socket::connect( &process, @@ -456,11 +429,6 @@ fn provider_drives_a_loopback_tcp_socket() { retirements.recv_timeout(TEST_TIMEOUT).unwrap(), unconnected_handle ); - process.close_object_reference(refused_handle).unwrap(); - assert_eq!( - retirements.recv_timeout(TEST_TIMEOUT).unwrap(), - refused_handle - ); release_read_shutdown_server.send(()).unwrap(); read_shutdown_server.join().unwrap(); server.join().unwrap(); @@ -3460,9 +3428,7 @@ fn host_reports_a_connect_pending_only_while_it_is() { assert_eq!(pending.connect(address), Err(Errno::INPROGRESS)); assert_eq!(pending.connect_pending(), Ok(true)); - let closed = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let address = socket_address_v4(closed.local_addr().unwrap()); - drop(closed); + let (_refusing, address) = crate::test_support::refusing_tcp_address(); let refused = Socket::tcp().unwrap(); match refused.connect(address) { Err(Errno::INPROGRESS) => { diff --git a/litebox_broker_userland/src/socket/tests/udp.rs b/litebox_broker_userland/src/socket/tests/udp.rs index 6924fc5d8d..1d22dfb7d8 100644 --- a/litebox_broker_userland/src/socket/tests/udp.rs +++ b/litebox_broker_userland/src/socket/tests/udp.rs @@ -3,18 +3,6 @@ use super::*; -// The TCP listener keeps the port reserved after the UDP socket closes; the range avoids host -// ephemeral ports. -fn udp_socket_with_reserved_port() -> (TcpListener, UdpSocket) { - (20_000..32_768) - .find_map(|port| { - let reservation = TcpListener::bind((Ipv4Addr::LOCALHOST, port)).ok()?; - let socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).ok()?; - Some((reservation, socket)) - }) - .expect("no free TCP/UDP port below the host ephemeral range") -} - fn gateway_udp_policy() -> SocketPolicy { SocketPolicy::guest_network() .with_udp_destination_rules(&[DestinationRule::new( @@ -1374,11 +1362,9 @@ fn provider_preserves_udp_datagram_semantics() { assert_eq!(&packet[..received], b"old peer"); assert_eq!(socket_address_v4(preserved_source), source); - let (_reservation, refused_socket) = udp_socket_with_reserved_port(); - let refused_address = gateway_address(socket_address_v4(refused_socket.local_addr().unwrap())); - drop(refused_socket); + let (_refusing, refused_address) = crate::test_support::refusing_udp_address(); assert_eq!( - litebox_broker_core::socket::connect(&process, handle, refused_address), + litebox_broker_core::socket::connect(&process, handle, gateway_address(refused_address)), Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) ); assert_eq!( @@ -1386,6 +1372,7 @@ fn provider_preserves_udp_datagram_semantics() { Ok(SocketOutcome::Completed(7)) ); // Leave the error with the transport so that the failed send queues it. + // A guest cannot arrange this: waiting for the error queues it. wait_for_readiness_publication(&publications, handle, ReadinessFlags::ERROR); assert_eq!( send_datagram( @@ -1407,29 +1394,6 @@ fn provider_preserves_udp_datagram_semantics() { .unwrap() .contains(ReadinessFlags::ERROR) ); - assert_eq!( - send_datagram(&process, handle, b"refused again", SendFlags::NONE, None,), - Ok(SocketOutcome::Completed(13)) - ); - wait_until_ready(&process, &publications, handle, ReadinessFlags::ERROR); - assert_eq!( - litebox_broker_core::socket::connect( - &process, - handle, - SocketAddrV4::new(Ipv4Addr::BROADCAST, 9), - ), - Ok(SocketOutcome::Failed(SocketError::InvalidArgument)) - ); - assert_eq!( - litebox_broker_core::socket::take_error(&process, handle), - Ok(Some(SocketError::ConnectionRefused)) - ); - assert!( - !process - .check_readiness(handle) - .unwrap() - .contains(ReadinessFlags::ERROR) - ); assert_eq!( litebox_broker_core::socket::connect(&process, handle, server_address), Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) @@ -1459,123 +1423,6 @@ fn provider_preserves_udp_datagram_semantics() { assert_eq!(retirements.recv_timeout(TEST_TIMEOUT).unwrap(), handle); } -/// A connected socket shut for reading still reports the ICMP errors its -/// datagrams cause. -#[test] -fn read_shutdown_udp_socket_reports_icmp_errors() { - let provider = Arc::new(UserlandSocketProvider::new(2, 2).unwrap()); - let socket_policy = SocketPolicy::deny() - .with_udp_destination_rules(&[DestinationRule::new( - CallerCredential::Unauthenticated, - Ipv4Cidr::new(Ipv4Address(HOST_GATEWAY_IPV4_ADDRESS.octets()), 32).unwrap(), - DestinationPortRange::new(Port(1), Port(u16::MAX)).unwrap(), - )]) - .unwrap(); - let broker = test_broker_core( - PolicyEngine::with_unauthenticated_rights(ObjectRights::all()) - .with_socket_policy(socket_policy), - BrokerCoreLimits::new_with_all_limits(4, 0, 2, 2), - provider.clone(), - ) - .unwrap(); - let process = broker - .create_process(CallerCredential::Unauthenticated) - .unwrap(); - let (published, publications) = channel(); - let (retired, retirements) = channel(); - let readiness = Arc::new(TestReadinessSink { published, retired }); - let handle = create_udp_socket(&process, readiness); - wait_until_ready(&process, &publications, handle, ReadinessFlags::WRITE); - let (_reservation, refused_socket) = udp_socket_with_reserved_port(); - let refused_address = gateway_address(socket_address_v4(refused_socket.local_addr().unwrap())); - drop(refused_socket); - assert_eq!( - litebox_broker_core::socket::connect(&process, handle, refused_address), - Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) - ); - assert_eq!( - litebox_broker_core::socket::shutdown(&process, handle, ShutdownMode::Read), - Ok(SocketOutcome::Completed(())) - ); - - // The second error shows that taking the first keeps the socket watched. - for _ in 0..2 { - assert_eq!( - send_datagram(&process, handle, b"refused", SendFlags::NONE, None), - Ok(SocketOutcome::Completed(7)) - ); - wait_until_ready(&process, &publications, handle, ReadinessFlags::ERROR); - assert_eq!( - litebox_broker_core::socket::take_error(&process, handle), - Ok(Some(SocketError::ConnectionRefused)) - ); - assert!( - !process - .check_readiness(handle) - .unwrap() - .contains(ReadinessFlags::ERROR) - ); - } - - process.close_object_reference(handle).unwrap(); - assert_eq!(retirements.recv_timeout(TEST_TIMEOUT).unwrap(), handle); -} - -/// A send that reports a connected socket's pending ICMP error takes it, and -/// the socket keeps reporting the errors that follow. -#[test] -fn udp_send_takes_a_pending_icmp_error() { - let provider = Arc::new(UserlandSocketProvider::new(1, 1).unwrap()); - let broker = test_broker_core( - PolicyEngine::with_unauthenticated_rights(ObjectRights::all()) - .with_socket_policy(gateway_udp_policy()), - BrokerCoreLimits::new_with_all_limits(2, 0, 1, 1), - provider, - ) - .unwrap(); - let process = broker - .create_process(CallerCredential::Unauthenticated) - .unwrap(); - let (published, publications) = channel(); - let (retired, _retirements) = channel(); - let handle = create_udp_socket(&process, Arc::new(TestReadinessSink { published, retired })); - let (_reservation, refused_socket) = udp_socket_with_reserved_port(); - let refused_address = gateway_address(socket_address_v4(refused_socket.local_addr().unwrap())); - drop(refused_socket); - assert_eq!( - litebox_broker_core::socket::connect(&process, handle, refused_address), - Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) - ); - assert_eq!( - send_datagram(&process, handle, b"refused", SendFlags::NONE, None), - Ok(SocketOutcome::Completed(7)) - ); - wait_until_ready(&process, &publications, handle, ReadinessFlags::ERROR); - assert_eq!( - send_datagram(&process, handle, b"refused", SendFlags::NONE, None), - Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) - ); - assert!( - !process - .check_readiness(handle) - .unwrap() - .contains(ReadinessFlags::ERROR) - ); - assert_eq!( - litebox_broker_core::socket::take_error(&process, handle), - Ok(None) - ); - assert_eq!( - send_datagram(&process, handle, b"refused", SendFlags::NONE, None), - Ok(SocketOutcome::Completed(7)) - ); - wait_until_ready(&process, &publications, handle, ReadinessFlags::ERROR); - assert_eq!( - litebox_broker_core::socket::take_error(&process, handle), - Ok(Some(SocketError::ConnectionRefused)) - ); -} - /// A connect that Winsock fails leaves an unconnected socket, which reports /// no ICMP errors. #[cfg(windows)] @@ -1617,9 +1464,7 @@ fn failed_udp_connect_keeps_icmp_errors_unreported() { let server = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); server.set_read_timeout(Some(TEST_TIMEOUT)).unwrap(); let server_address = gateway_address(socket_address_v4(server.local_addr().unwrap())); - let refused_socket = UdpSocket::bind("127.0.0.1:0").unwrap(); - let refused_address = gateway_address(socket_address_v4(refused_socket.local_addr().unwrap())); - drop(refused_socket); + let (_refusing, refused_address) = crate::test_support::refusing_udp_address(); assert_eq!( send_datagram( @@ -1645,7 +1490,7 @@ fn failed_udp_connect_keeps_icmp_errors_unreported() { handle, b"refused", SendFlags::NONE, - Some(refused_address) + Some(gateway_address(refused_address)) ), Ok(SocketOutcome::Completed(7)) ); @@ -3076,7 +2921,7 @@ fn udp_error_consumed_before_its_event_is_applied_is_not_reported() { let (published, publications) = channel(); let (retired, _retirements) = channel(); let socket = create_udp_socket(&process, Arc::new(TestReadinessSink { published, retired })); - let (_reservation, host) = udp_socket_with_reserved_port(); + let host = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); host.set_read_timeout(Some(TEST_TIMEOUT)).unwrap(); let host_address = host.local_addr().unwrap(); let gateway = gateway_address(socket_address_v4(host_address)); @@ -3091,7 +2936,9 @@ fn udp_error_consumed_before_its_event_is_applied_is_not_reported() { let (_, endpoint) = host.recv_from(&mut [0; 5]).unwrap(); host.send_to(b"queued", endpoint).unwrap(); wait_until_ready(&process, &publications, socket, ReadinessFlags::READ); - drop(host); + // Connected to itself, the host socket refuses the guest's datagrams, as a closed port would, + // and keeps its port. + host.connect(host_address).unwrap(); // The queued datagram leaves the endpoint armed only for errors, so the // ICMP error is its next event. @@ -3144,7 +2991,6 @@ fn udp_error_consumed_before_its_event_is_applied_is_not_reported() { })) ); assert_eq!(&payload, b"queued"); - let host = UdpSocket::bind(host_address).unwrap(); host.send_to(b"later", endpoint).unwrap(); wait_until_ready(&process, &publications, socket, ReadinessFlags::READ); let mut payload = [0; 5]; diff --git a/litebox_broker_userland/src/test_support.rs b/litebox_broker_userland/src/test_support.rs new file mode 100644 index 0000000000..5f8c932a2a --- /dev/null +++ b/litebox_broker_userland/src/test_support.rs @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +//! Loopback addresses that refuse what is sent to them, for tests of the +//! broker's host sockets, and of the runners that use them. +//! +//! Each address comes with what holds its port, so that no test running in +//! parallel can be given the port while it is kept. A port freed before use +//! could be. + +use std::net::{Ipv4Addr, SocketAddrV4}; + +/// Returns a loopback TCP address that refuses connections, with the socket +/// that holds it. +/// +/// The socket is bound but never listens, so connecting to the address is +/// refused, and no other socket can be given the port while the socket is +/// kept. +/// +/// # Panics +/// +/// Panics if the socket cannot be made. +pub fn refusing_tcp_address() -> (impl Sized, SocketAddrV4) { + let socket = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None).unwrap(); + socket + .bind(&SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0).into()) + .unwrap(); + let address = socket.local_addr().unwrap().as_socket_ipv4().unwrap(); + (socket, address) +} + +/// Returns a loopback UDP address to which datagrams are refused, with what +/// reserves it. +/// +/// On Linux, a socket connected to itself holds the port: it receives only its +/// own datagrams, and it sends none, so Linux refuses others, with ICMP port +/// unreachable, as for a closed port. No other socket can take the port while +/// the socket is kept. +/// +/// Winsock discards datagrams from other addresses on a connected socket, so +/// on Windows the port is left unbound instead: it is below Windows' dynamic +/// port range (49152-65535 by default), so no socket is given it without +/// asking for it, and a TCP listener on the same port keeps other calls from +/// choosing it. +/// +/// # Panics +/// +/// Panics if the port cannot be reserved. +pub fn refusing_udp_address() -> (impl Sized, SocketAddrV4) { + #[cfg(target_os = "linux")] + { + let socket = std::net::UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); + let std::net::SocketAddr::V4(address) = socket.local_addr().unwrap() else { + unreachable!() + }; + socket.connect(address).unwrap(); + (socket, address) + } + #[cfg(windows)] + { + (20_000..32_768) + .find_map(|port| { + let reservation = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, port)).ok()?; + std::net::UdpSocket::bind((Ipv4Addr::LOCALHOST, port)).ok()?; + Some((reservation, SocketAddrV4::new(Ipv4Addr::LOCALHOST, port))) + }) + .expect("no free TCP and UDP port below the dynamic port range") + } +} diff --git a/litebox_runner_linux_on_windows_userland/Cargo.toml b/litebox_runner_linux_on_windows_userland/Cargo.toml index 3bfedd2625..08e3146726 100644 --- a/litebox_runner_linux_on_windows_userland/Cargo.toml +++ b/litebox_runner_linux_on_windows_userland/Cargo.toml @@ -19,6 +19,7 @@ litebox_shim_linux = { version = "0.1.0", path = "../litebox_shim_linux", defaul tracing-subscriber = { version = "0.3.22", features = ["env-filter"] } [target.'cfg(all(windows, target_arch = "x86_64"))'.dev-dependencies] +litebox_broker_userland = { version = "0.1.0", path = "../litebox_broker_userland", features = ["test-support"] } litebox_syscall_rewriter = { version = "0.1.0", path = "../litebox_syscall_rewriter", default-features = false, features = ["std"] } socket2 = { version = "0.6.3", default-features = false } tar = "0.4" diff --git a/litebox_runner_linux_on_windows_userland/tests/loader.rs b/litebox_runner_linux_on_windows_userland/tests/loader.rs index 76d6a14560..5a6539c4ba 100644 --- a/litebox_runner_linux_on_windows_userland/tests/loader.rs +++ b/litebox_runner_linux_on_windows_userland/tests/loader.rs @@ -274,7 +274,7 @@ fn reset(stream: std::net::TcpStream) { /// Runs `tcp_broker`, a TCP client of servers on the host gateway, through the broker's Windows /// sockets: connects, socket options, partial and blocking transfers, peeks, backpressure, -/// shutdown, resets, faulting receives, refusals, and policy denials. +/// shutdown, resets, faulting receives, and policy denials. /// /// Built with `gcc -static -m64` from `litebox_runner_linux_userland/tests/tcp_broker.c`. #[test] @@ -339,17 +339,12 @@ fn test_tcp_with_windows_broker() { } }); - let refused_port = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)) - .unwrap() - .local_addr() - .unwrap() - .port(); run_progs_with_broker_arguments( &broker, &runner, &["tcp_broker"], &GATEWAY_TCP, - &[&port.to_string(), &refused_port.to_string()], + &[&port.to_string()], ); server.join().unwrap(); } @@ -366,9 +361,8 @@ fn test_udp_with_windows_broker() { let (broker, runner) = build_windows_broker(); let server = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let port = server.local_addr().unwrap().port(); - // Bound until the guest is ready to be refused, so that nothing else takes the port. - let refused = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let refused_port = refused.local_addr().unwrap().port(); + let (_refusing, refused_address) = + litebox_broker_userland::test_support::refusing_udp_address(); let server = std::thread::spawn(move || { let mut packet = [0_u8; 64]; let (received, client) = server.recv_from(&mut packet).unwrap(); @@ -400,7 +394,6 @@ fn test_udp_with_windows_broker() { let (received, source) = server.recv_from(&mut packet).unwrap(); assert_eq!(&packet[..received], b"preserved"); assert_eq!(source, client); - drop(refused); server.send_to(b"preserved-reply", client).unwrap(); }); @@ -409,7 +402,7 @@ fn test_udp_with_windows_broker() { &runner, &["udp_broker"], &GATEWAY_UDP, - &[&port.to_string(), &refused_port.to_string()], + &[&port.to_string(), &refused_address.port().to_string()], ); server.join().unwrap(); } @@ -496,12 +489,8 @@ fn test_tcp_connection_states_with_windows_broker() { .collect::>() }); - // Freed after the other listeners are bound, so none of them can reuse it. - let refused_port = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)) - .unwrap() - .local_addr() - .unwrap() - .port(); + let (_refusing, refused_address) = + litebox_broker_userland::test_support::refusing_tcp_address(); run_progs_with_broker_arguments( &broker, @@ -511,7 +500,7 @@ fn test_tcp_connection_states_with_windows_broker() { &[ &reset_port.to_string(), &unresponsive_port.to_string(), - &refused_port.to_string(), + &refused_address.port().to_string(), &half_closed_port.to_string(), &open_port.to_string(), &greeting_port.to_string(), diff --git a/litebox_runner_linux_on_windows_userland/tests/test-bins/tcp_broker b/litebox_runner_linux_on_windows_userland/tests/test-bins/tcp_broker index e4da0d1698..e85919c771 100755 Binary files a/litebox_runner_linux_on_windows_userland/tests/test-bins/tcp_broker and b/litebox_runner_linux_on_windows_userland/tests/test-bins/tcp_broker differ diff --git a/litebox_runner_linux_on_windows_userland/tests/test-bins/udp_broker b/litebox_runner_linux_on_windows_userland/tests/test-bins/udp_broker old mode 100644 new mode 100755 index 7fc7bc8d9f..8a0353f68b Binary files a/litebox_runner_linux_on_windows_userland/tests/test-bins/udp_broker and b/litebox_runner_linux_on_windows_userland/tests/test-bins/udp_broker differ diff --git a/litebox_runner_linux_userland/Cargo.toml b/litebox_runner_linux_userland/Cargo.toml index 8be64690d4..9a2c054f03 100644 --- a/litebox_runner_linux_userland/Cargo.toml +++ b/litebox_runner_linux_userland/Cargo.toml @@ -28,7 +28,7 @@ glob = "0.3" litebox_broker_core = { version = "0.1.0", path = "../litebox_broker_core", features = ["test-support"] } litebox_broker_host = { version = "0.1.0", path = "../litebox_broker_host" } litebox_broker_platform_linux_userland = { version = "0.1.0", path = "../litebox_broker_platform_linux_userland" } -litebox_broker_userland = { version = "0.1.0", path = "../litebox_broker_userland" } +litebox_broker_userland = { version = "0.1.0", path = "../litebox_broker_userland", features = ["test-support"] } litebox_platform = { version = "0.1.0", path = "../litebox_platform" } tempfile = { version = "3", default-features = false } diff --git a/litebox_runner_linux_userland/tests/run.rs b/litebox_runner_linux_userland/tests/run.rs index a8f5be2ffa..c1fa40042a 100644 --- a/litebox_runner_linux_userland/tests/run.rs +++ b/litebox_runner_linux_userland/tests/run.rs @@ -2608,9 +2608,6 @@ fn test_runner_broker_tcp_client_with_rewriter() { } }); - let refused_listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let refused_port = refused_listener.local_addr().unwrap().port(); - drop(refused_listener); let mut runner = Runner::new(&target, "broker_tcp_client_rewriter"); let control_socket_path = unique_test_socket_path("runner-broker-tcp-control"); let broker = spawn_test_broker( @@ -2624,11 +2621,10 @@ fn test_runner_broker_tcp_client_with_rewriter() { ); runner .arg(port.to_string()) - .arg(refused_port.to_string()) .broker_socket(&control_socket_path) .run(); - // Nine sockets and two epoll instances. - assert_eq!(broker.next_close_object_count(), 11); + // Eight sockets and two epoll instances. + assert_eq!(broker.next_close_object_count(), 10); broker.join(); server.join().unwrap(); } @@ -2719,12 +2715,8 @@ fn test_runner_broker_tcp_connection_states_with_rewriter() { .collect::>() }); - // Freed after the other listeners are bound, so none of them can reuse it. - let refused_port = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)) - .unwrap() - .local_addr() - .unwrap() - .port(); + let (_refusing, refused_address) = + litebox_broker_userland::test_support::refusing_tcp_address(); let mut runner = Runner::new(&target, "broker_tcp_states_rewriter"); let control_socket_path = unique_test_socket_path("runner-broker-tcp-states-control"); @@ -2740,7 +2732,7 @@ fn test_runner_broker_tcp_connection_states_with_rewriter() { runner .arg(reset_port.to_string()) .arg(unresponsive_address.port().to_string()) - .arg(refused_port.to_string()) + .arg(refused_address.port().to_string()) .arg(half_closed_port.to_string()) .arg(open_port.to_string()) .arg(greeting_port.to_string()) @@ -2762,8 +2754,8 @@ fn test_runner_broker_udp_with_rewriter() { let target = common::compile("./tests/udp_broker.c", "broker_udp_rewriter", false, false); let server = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let port = server.local_addr().unwrap().port(); - let refused = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let refused_port = refused.local_addr().unwrap().port(); + let (_refusing, refused_address) = + litebox_broker_userland::test_support::refusing_udp_address(); let mut runner = Runner::new(&target, "broker_udp_rewriter"); let server = std::thread::spawn(move || { let mut packet = [0_u8; 64]; @@ -2820,7 +2812,6 @@ fn test_runner_broker_udp_with_rewriter() { let (received, source) = server.recv_from(&mut packet).unwrap(); assert_eq!(&packet[..received], b"preserved"); assert_eq!(source, client); - drop(refused); server.send_to(b"preserved-reply", client).unwrap(); }); @@ -2836,7 +2827,7 @@ fn test_runner_broker_udp_with_rewriter() { ); runner .arg(port.to_string()) - .arg(refused_port.to_string()) + .arg(refused_address.port().to_string()) .broker_socket(&control_socket_path) .run(); assert_eq!(broker.next_close_object_count(), 3); diff --git a/litebox_runner_linux_userland/tests/tcp_broker.c b/litebox_runner_linux_userland/tests/tcp_broker.c index bf86dff09b..880814359b 100644 --- a/litebox_runner_linux_userland/tests/tcp_broker.c +++ b/litebox_runner_linux_userland/tests/tcp_broker.c @@ -27,7 +27,7 @@ enum { }; int main(int argc, char **argv) { - assert(argc == 3); + assert(argc == 2); int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); assert(fd >= 0); @@ -319,27 +319,6 @@ int main(int argc, char **argv) { assert(errno == ENOTCONN); assert(close(fd) == 0); - fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); - assert(fd >= 0); - address.sin_port = htons((uint16_t)strtoul(argv[2], NULL, 10)); - result = connect(fd, (const struct sockaddr *)&address, sizeof(address)); - assert(result == -1); - if (errno == EINPROGRESS) { - struct pollfd poll_fd = {.fd = fd, .events = POLLOUT}; - assert(poll(&poll_fd, 1, 5000) == 1); - socket_error = 0; - error_length = sizeof(socket_error); - assert(getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_error, &error_length) == 0); - assert(socket_error == ECONNREFUSED); - poll_fd.revents = 0; - assert(poll(&poll_fd, 1, 0) == 1); - assert((poll_fd.revents & POLLERR) == 0); - assert((poll_fd.revents & (POLLIN | POLLOUT | POLLHUP)) != 0); - } else { - assert(errno == ECONNREFUSED); - } - assert(close(fd) == 0); - fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); assert(fd >= 0); assert(inet_pton(AF_INET, "192.0.2.1", &address.sin_addr) == 1); diff --git a/litebox_runner_linux_userland/tests/udp_broker.c b/litebox_runner_linux_userland/tests/udp_broker.c index 5828071c7c..76ce46b209 100644 --- a/litebox_runner_linux_userland/tests/udp_broker.c +++ b/litebox_runner_linux_userland/tests/udp_broker.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,28 @@ static void retract_readable(int fd) { assert(poll(&ready, 1, 0) == 0); } +static bool error_reported(int fd) { + struct pollfd failed = {.fd = fd, .events = POLLERR}; + assert(poll(&failed, 1, 0) >= 0); + return (failed.revents & POLLERR) != 0; +} + +// Sends a datagram to a connected socket's peer, which refuses it, and waits +// for the ICMP error it causes to be reported. +static void send_refused(int fd) { + assert(send(fd, "x", 1, 0) == 1); + struct pollfd failed = {.fd = fd, .events = POLLERR}; + assert(poll(&failed, 1, 5000) == 1); + assert((failed.revents & POLLERR) != 0); +} + +static int socket_error(int fd) { + int error = -1; + socklen_t length = sizeof(error); + assert(getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &length) == 0); + return error; +} + int main(int argc, char **argv) { assert(argc == 3); int fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); @@ -288,17 +311,55 @@ int main(int argc, char **argv) { assert(memcmp(reply, "preserved-reply", 15) == 0); retract_readable(fd); - assert(send(refused_fd, "x", 1, 0) == 1); - struct pollfd failed = {.fd = refused_fd, .events = POLLERR}; - assert(poll(&failed, 1, 5000) == 1); - assert((failed.revents & POLLERR) != 0); + // A connected socket reports the ICMP error that a datagram to its peer + // causes until a receive, SO_ERROR, or send takes it. + send_refused(refused_fd); assert(recv(refused_fd, reply, sizeof(reply), 0) == -1); assert(errno == ECONNREFUSED); - int socket_error = -1; - socklen_t socket_error_length = sizeof(socket_error); - assert(getsockopt(refused_fd, SOL_SOCKET, SO_ERROR, &socket_error, - &socket_error_length) == 0); - assert(socket_error == 0); + assert(!error_reported(refused_fd)); + assert(socket_error(refused_fd) == 0); + + // A send takes the error, failing with it in place of sending. + send_refused(refused_fd); + assert(send(refused_fd, "x", 1, 0) == -1); + assert(errno == ECONNREFUSED); + assert(!error_reported(refused_fd)); + assert(socket_error(refused_fd) == 0); + + // A send that fails for another reason leaves the error. + send_refused(refused_fd); + struct sockaddr_in zero_port = refused; + zero_port.sin_port = 0; + assert(sendto(refused_fd, "x", 1, 0, (const struct sockaddr *)&zero_port, + sizeof(zero_port)) == -1); + assert(errno == EINVAL); + assert(error_reported(refused_fd)); + assert(socket_error(refused_fd) == ECONNREFUSED); + assert(!error_reported(refused_fd)); + + // A socket shut down for reading still reports each error. + assert(shutdown(refused_fd, SHUT_RD) == 0); + send_refused(refused_fd); + assert(recv(refused_fd, reply, sizeof(reply), 0) == -1); + assert(errno == ECONNREFUSED); + assert(!error_reported(refused_fd)); + send_refused(refused_fd); + assert(socket_error(refused_fd) == ECONNREFUSED); + + // A connect that fails leaves the error. It comes last, as Linux sends + // later datagrams to the address of a connect that failed. + send_refused(refused_fd); + // Linux refuses a broadcast peer without SO_BROADCAST, and the broker + // refuses any. + struct sockaddr_in broadcast = { + .sin_family = AF_INET, + .sin_port = htons(9), + .sin_addr.s_addr = htonl(INADDR_BROADCAST), + }; + assert(connect(refused_fd, (const struct sockaddr *)&broadcast, + sizeof(broadcast)) == -1); + assert(error_reported(refused_fd)); + assert(socket_error(refused_fd) == ECONNREFUSED); assert(close(refused_fd) == 0); assert(shutdown(fd, SHUT_WR) == 0);