xref: /DragonOS/user/apps/test_socket/src/test_unix_stream_pair.rs (revision 401699735b5ec29768c3c0c47df6c529991f108f)
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