Skip to content

Commit c638435

Browse files
committed
net/linux: Add test for UnixDatagram::bind() with abstract address
1 parent 61558e0 commit c638435

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tokio/tests/uds_datagram.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use tokio::try_join;
88

99
use std::future::poll_fn;
1010
use std::io;
11+
#[cfg(any(target_os = "linux", target_os = "android"))]
12+
use std::os::linux::net::SocketAddrExt;
13+
#[cfg(any(target_os = "linux", target_os = "android"))]
14+
use std::os::unix::net as std_net;
1115
use std::sync::Arc;
1216

1317
async fn echo_server(socket: UnixDatagram) -> io::Result<()> {
@@ -71,6 +75,50 @@ async fn echo_from() -> io::Result<()> {
7175
Ok(())
7276
}
7377

78+
#[allow(clippy::bool_assert_comparison)]
79+
#[tokio::test]
80+
#[cfg_attr(miri, ignore)] // No `socket` on miri.
81+
async fn recv_abstract() -> io::Result<()> {
82+
// Generate 128-bit random string prefixed by `\0` as socket path
83+
let abstract_path = format!(
84+
"\0{:016x}{:016x}",
85+
rand::random::<u64>(),
86+
rand::random::<u64>()
87+
);
88+
89+
// On non-Linux platforms, abstract socket paths are rejected
90+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
91+
assert_eq!(
92+
UnixDatagram::bind(&abstract_path).map_err(|e| e.kind()),
93+
Err(io::ErrorKind::InvalidInput)
94+
);
95+
96+
// Remaining test is Linux-only
97+
#[cfg(any(target_os = "linux", target_os = "android"))]
98+
{
99+
// Create socket in abstract namespace
100+
let recv_socket = UnixDatagram::bind(&abstract_path)?;
101+
102+
// Send message from stdlib
103+
//
104+
//FIXME: Use `tokio` native here once passing abstract socket paths to
105+
// `send_to`/`connect` is implemented
106+
let send_socket = std_net::UnixDatagram::unbound()?;
107+
send_socket.send_to_addr(
108+
b"READY=1\n",
109+
&std_net::SocketAddr::from_abstract_name(&abstract_path[1..])?,
110+
)?;
111+
112+
// Receive and validate message
113+
let mut buf = vec![0u8, 32];
114+
let (len, addr) = recv_socket.recv_from(&mut buf).await?;
115+
assert_eq!(&buf[0..len], b"READY=1\n");
116+
assert_eq!(addr.is_unnamed(), true);
117+
}
118+
119+
Ok(())
120+
}
121+
74122
// Even though we use sync non-blocking io we still need a reactor.
75123
#[tokio::test]
76124
#[cfg_attr(miri, ignore)] // No SOCK_DGRAM for `socketpair` in miri.

0 commit comments

Comments
 (0)