1 #![cfg(feature = "full")]
2 #![warn(rust_2018_idioms)]
3 #![cfg(unix)]
4 
5 use tokio::io::{AsyncReadExt, AsyncWriteExt};
6 use tokio::net::{UnixListener, UnixStream};
7 
8 use futures::future::try_join;
9 
10 #[tokio::test]
accept_read_write() -> std::io::Result<()>11 async fn accept_read_write() -> std::io::Result<()> {
12     let dir = tempfile::Builder::new()
13         .prefix("tokio-uds-tests")
14         .tempdir()
15         .unwrap();
16     let sock_path = dir.path().join("connect.sock");
17 
18     let mut listener = UnixListener::bind(&sock_path)?;
19 
20     let accept = listener.accept();
21     let connect = UnixStream::connect(&sock_path);
22     let ((mut server, _), mut client) = try_join(accept, connect).await?;
23 
24     // Write to the client. TODO: Switch to write_all.
25     let write_len = client.write(b"hello").await?;
26     assert_eq!(write_len, 5);
27     drop(client);
28     // Read from the server. TODO: Switch to read_to_end.
29     let mut buf = [0u8; 5];
30     server.read_exact(&mut buf).await?;
31     assert_eq!(&buf, b"hello");
32     let len = server.read(&mut buf).await?;
33     assert_eq!(len, 0);
34     Ok(())
35 }
36 
37 #[tokio::test]
shutdown() -> std::io::Result<()>38 async fn shutdown() -> std::io::Result<()> {
39     let dir = tempfile::Builder::new()
40         .prefix("tokio-uds-tests")
41         .tempdir()
42         .unwrap();
43     let sock_path = dir.path().join("connect.sock");
44 
45     let mut listener = UnixListener::bind(&sock_path)?;
46 
47     let accept = listener.accept();
48     let connect = UnixStream::connect(&sock_path);
49     let ((mut server, _), mut client) = try_join(accept, connect).await?;
50 
51     // Shut down the client
52     AsyncWriteExt::shutdown(&mut client).await?;
53     // Read from the server should return 0 to indicate the channel has been closed.
54     let mut buf = [0u8; 1];
55     let n = server.read(&mut buf).await?;
56     assert_eq!(n, 0);
57     Ok(())
58 }
59