Skip to content

Commit

Permalink
Add test for abstract unix sockets
Browse files Browse the repository at this point in the history
This commit duplicates the existing test_unix_msg() tests. The
duplicated version uses an abstract unix socket instead of a path based
one.

To make the code-duplication not too bad, a helper function
do_test_unix_msg() is introduced that does "the actual work" and just
needs as input a socket address.

This new test currently fails. This reproduces
bytecodealliance#660.

Signed-off-by: Uli Schlachter <[email protected]>
  • Loading branch information
psychon committed May 6, 2023
1 parent b5e9770 commit 93f94e0
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions tests/net/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,19 @@ fn test_unix() {
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
#[test]
fn test_unix_msg() {
fn do_test_unix_msg(addr: SocketAddrUnix) {
use rustix::io::{IoSlice, IoSliceMut};
use rustix::net::{recvmsg, sendmsg_noaddr, RecvFlags, SendFlags};
use std::string::ToString;

let tmpdir = tempfile::tempdir().unwrap();
let path = tmpdir.path().join("scp_4804");

let connection_socket = socket(
AddressFamily::UNIX,
SocketType::SEQPACKET,
Protocol::default(),
)
.unwrap();

let name = SocketAddrUnix::new(&path).unwrap();
bind_unix(&connection_socket, &name).unwrap();
listen(&connection_socket, 1).unwrap();

let server = {
let path = path.clone();
let connection_socket = socket(
AddressFamily::UNIX,
SocketType::SEQPACKET,
Protocol::default(),
)
.unwrap();
bind_unix(&connection_socket, &addr).unwrap();
listen(&connection_socket, 1).unwrap();

move || {
let mut buffer = vec![0; BUFFER_SIZE];
Expand Down Expand Up @@ -199,13 +190,10 @@ fn test_unix_msg() {
)
.unwrap();
}

unlinkat(cwd(), path, AtFlags::empty()).unwrap();
}
};

let client = move || {
let addr = SocketAddrUnix::new(path).unwrap();
let mut buffer = vec![0; BUFFER_SIZE];
let runs: &[(&[&str], i32)] = &[
(&["1", "2"], 3),
Expand Down Expand Up @@ -288,6 +276,30 @@ fn test_unix_msg() {
server.join().unwrap();
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
#[test]
fn test_unix_msg() {
let tmpdir = tempfile::tempdir().unwrap();
let path = tmpdir.path().join("scp_4804");

let name = SocketAddrUnix::new(&path).unwrap();
do_test_unix_msg(name);

unlinkat(cwd(), path, AtFlags::empty()).unwrap();
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
#[test]
fn test_abstract_unix_msg() {
use std::os::unix::ffi::OsStrExt;

let tmpdir = tempfile::tempdir().unwrap();
let path = tmpdir.path().join("scp_4804");

let name = SocketAddrUnix::new_abstract_name(path.as_os_str().as_bytes()).unwrap();
do_test_unix_msg(name);
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
#[test]
fn test_unix_msg_with_scm_rights() {
Expand Down

0 comments on commit 93f94e0

Please sign in to comment.