xref: /DragonOS/user/apps/test_socket/src/test_unix_stream_pair.rs (revision c635d8a9cfe25bc11779f323ef0c7d7a0f597d4a)
1 use std::io::{Error, Read, Write};
2 use std::os::unix::net::UnixStream;
3 use std::str;
4 
5 const MSG: &str = "Hello, unix stream socketpair!";
6 
7 pub fn test_unix_stream_pair() -> std::io::Result<()> {
8     let (mut sock0, mut sock1) = UnixStream::pair()?;
9 
10     sock1.write_all(MSG.as_bytes())?;
11 
12     let mut buffer = [0; 1024];
13     let nbytes = sock0.read(&mut buffer).expect("read error");
14     let received_msg = str::from_utf8(&buffer[..nbytes]).unwrap();
15 
16     if received_msg == MSG {
17         Ok(())
18     } else {
19         Err(Error::from_raw_os_error(-1))
20     }
21 }
22