1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#![cfg(all(unix, not(target_os = "unknown")))]
use async_std::io;
use async_std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
use async_std::prelude::*;
use async_std::task;
use std::time::Duration;
const JULIUS_CAESAR: &[u8] = b"
Friends, Romans, countrymen - lend me your ears!
I come not to praise Caesar, but to bury him.
";
#[test]
fn send_recv() -> io::Result<()> {
task::block_on(async {
let (socket1, socket2) = UnixDatagram::pair().unwrap();
socket1.send(JULIUS_CAESAR).await?;
let mut buf = vec![0; 1024];
let n = socket2.recv(&mut buf).await?;
assert_eq!(&buf[..n], JULIUS_CAESAR);
Ok(())
})
}
#[test]
fn into_raw_fd_datagram() -> io::Result<()> {
use async_std::os::unix::io::{FromRawFd, IntoRawFd};
task::block_on(async {
let (socket1, socket2) = UnixDatagram::pair().unwrap();
socket1.send(JULIUS_CAESAR).await?;
let mut buf = vec![0; 1024];
let socket2 = unsafe { UnixDatagram::from_raw_fd(socket2.into_raw_fd()) };
let n = socket2.recv(&mut buf).await?;
assert_eq!(&buf[..n], JULIUS_CAESAR);
Ok(())
})
}
#[test]
fn into_raw_fd_stream() -> io::Result<()> {
use async_std::os::unix::io::{FromRawFd, IntoRawFd};
task::block_on(async {
let (mut socket1, socket2) = UnixStream::pair().unwrap();
socket1.write(JULIUS_CAESAR).await?;
let mut buf = vec![0; 1024];
let mut socket2 = unsafe { UnixStream::from_raw_fd(socket2.into_raw_fd()) };
let n = socket2.read(&mut buf).await?;
assert_eq!(&buf[..n], JULIUS_CAESAR);
Ok(())
})
}
const PING: &[u8] = b"ping";
const PONG: &[u8] = b"pong";
const TEST_TIMEOUT: Duration = Duration::from_secs(3);
#[test]
fn socket_ping_pong() {
let tmp_dir = tempfile::Builder::new()
.prefix("socket_ping_pong")
.tempdir()
.expect("Temp dir not created");
let sock_path = tmp_dir.as_ref().join("sock");
let iter_cnt = 16;
let listener =
task::block_on(async { UnixListener::bind(&sock_path).await.expect("Socket bind") });
let server_handle = std::thread::spawn(move || {
task::block_on(async { ping_pong_server(listener, iter_cnt).await }).unwrap()
});
let client_handle = std::thread::spawn(move || {
task::block_on(async { ping_pong_client(&sock_path, iter_cnt).await }).unwrap()
});
client_handle.join().unwrap();
server_handle.join().unwrap();
}
async fn ping_pong_server(listener: UnixListener, iterations: u32) -> std::io::Result<()> {
let mut incoming = listener.incoming();
let mut buf = [0; 1024];
for _ix in 0..iterations {
if let Some(s) = incoming.next().await {
let mut s = s?;
let n = s.read(&mut buf[..]).await?;
assert_eq!(&buf[..n], PING);
s.write_all(&PONG).await?;
}
}
Ok(())
}
async fn ping_pong_client(socket: &std::path::PathBuf, iterations: u32) -> std::io::Result<()> {
let mut buf = [0; 1024];
for _ix in 0..iterations {
let mut socket = UnixStream::connect(&socket).await?;
socket.write_all(&PING).await?;
let n = async_std::io::timeout(TEST_TIMEOUT, socket.read(&mut buf[..])).await?;
assert_eq!(&buf[..n], PONG);
}
Ok(())
}
#[test]
fn uds_clone() -> io::Result<()> {
task::block_on(async {
let tmp_dir = tempfile::Builder::new()
.prefix("socket_ping_pong")
.tempdir()
.expect("Temp dir not created");
let sock_path = tmp_dir.as_ref().join("sock");
let input = UnixListener::bind(&sock_path).await?;
let mut writer = UnixStream::connect(&sock_path).await?;
let mut reader = input.incoming().next().await.unwrap()?;
writer.write(b"original").await.unwrap();
let mut original_buf = [0; 8];
reader.read(&mut original_buf).await?;
assert_eq!(&original_buf, b"original");
writer.clone().write(b"clone").await.unwrap();
let mut clone_buf = [0; 5];
reader.clone().read(&mut clone_buf).await?;
assert_eq!(&clone_buf, b"clone");
Ok(())
})
}
|