MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / test_v4_msg

Function test_v4_msg

tests/net/v4.rs:93–198  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

91#[cfg(not(any(windows, target_os = "espidf", target_os = "redox", target_os = "wasi")))]
92#[test]
93fn test_v4_msg() {
94 crate::init();
95
96 use rustix::io::{IoSlice, IoSliceMut};
97 use rustix::net::{recvmsg, sendmsg};
98
99 fn server(ready: Arc<(Mutex<u16>, Condvar)>) {
100 let connection_socket = socket(AddressFamily::INET, SocketType::STREAM, None).unwrap();
101
102 let name = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 0);
103 bind(&connection_socket, &name).unwrap();
104
105 let who = getsockname(&connection_socket).unwrap();
106 let who = SocketAddrV4::try_from(who).unwrap();
107
108 listen(&connection_socket, 1).unwrap();
109
110 {
111 let (lock, cvar) = &*ready;
112 let mut port = lock.lock().unwrap();
113 *port = who.port();
114 cvar.notify_all();
115 }
116
117 let mut buffer = vec![0; BUFFER_SIZE];
118 let data_socket = accept(&connection_socket).unwrap();
119 let res = recvmsg(
120 &data_socket,
121 &mut [IoSliceMut::new(&mut buffer)],
122 &mut Default::default(),
123 RecvFlags::empty(),
124 )
125 .unwrap();
126 assert_eq!(
127 String::from_utf8_lossy(&buffer[..res.bytes]),
128 "hello, world"
129 );
130 assert_eq!(res.flags, ReturnFlags::empty());
131
132 sendmsg(
133 &data_socket,
134 &[IoSlice::new(b"goodnight, moon")],
135 &mut Default::default(),
136 SendFlags::empty(),
137 )
138 .unwrap();
139 }
140
141 fn client(ready: Arc<(Mutex<u16>, Condvar)>) {
142 let port = {
143 let (lock, cvar) = &*ready;
144 let mut port = lock.lock().unwrap();
145 while *port == 0 {
146 port = cvar.wait(port).unwrap();
147 }
148 *port
149 };
150

Callers

nothing calls this directly

Calls 3

initFunction · 0.70
serverFunction · 0.70
clientFunction · 0.70

Tested by

no test coverage detected