use tempdir::TempDir; use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, net::{UnixListener, UnixStream}, }; use tokio_send_fd::SendFd; const SOCKET_NAME: &str = "tokio_send_fd_test.sock"; #[tokio::test] async fn send_tokio_stream_test() { let tmp_dir = TempDir::new("tokio-send-fd").unwrap(); let sock_path = tmp_dir.path().join(SOCKET_NAME); let sock_path1 = sock_path.clone(); let sock_path2 = sock_path.clone(); println!("Start listening at: {:?}", sock_path1); let listener = UnixListener::bind(sock_path1).unwrap(); let j1 = tokio::spawn(async move { let (stream, _) = listener.accept().await.unwrap(); println!("Incoming peer connection"); let (left, mut right) = UnixStream::pair().unwrap(); println!("Sending peer fd"); stream.send_stream(left).await.unwrap(); println!("Succesfullt sent peer fd"); let mut buffer = [0u8; 4]; println!("Reading data from the peer"); assert!(right.read(&mut buffer).await.unwrap() == 4); println!("Message sent through a socket: {:?}", buffer); }); let j2 = tokio::spawn(async move { println!("Connection to the sender"); let stream = UnixStream::connect(sock_path2).await.unwrap(); println!("Succesfully connected to the sender. Reading file descriptor"); let mut peer_stream = stream.recv_stream().await.unwrap(); println!("Sending data to the peer"); let buffer: [u8; 4] = [0, 0, 0, 42]; peer_stream.write(&buffer).await.unwrap(); println!("Succesfully sent data to the peer"); }); tokio::try_join!(j1, j2).unwrap(); let _ = std::fs::remove_dir(sock_path); }