Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions litebox_broker_userland/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions litebox_broker_userland/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 2 additions & 5 deletions litebox_broker_userland/src/socket/host/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
19 changes: 0 additions & 19 deletions litebox_broker_userland/src/socket/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 1 addition & 35 deletions litebox_broker_userland/src/socket/tests/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) => {
Expand Down
172 changes: 9 additions & 163 deletions litebox_broker_userland/src/socket/tests/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1374,18 +1362,17 @@ 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!(
send_datagram(&process, handle, b"refused", SendFlags::NONE, None,),
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(
Expand All @@ -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))
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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(
Expand All @@ -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))
);
Expand Down Expand Up @@ -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));
Expand All @@ -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.
Expand Down Expand Up @@ -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];
Expand Down
Loading
Loading