]> git.proxmox.com Git - rustc.git/blob - library/std/src/net/udp/tests.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / net / udp / tests.rs
1 use crate::io::ErrorKind;
2 use crate::net::test::{next_test_ip4, next_test_ip6};
3 use crate::net::*;
4 use crate::sync::mpsc::channel;
5 use crate::sys_common::AsInner;
6 use crate::thread;
7 use crate::time::{Duration, Instant};
8
9 fn each_ip(f: &mut dyn FnMut(SocketAddr, SocketAddr)) {
10 f(next_test_ip4(), next_test_ip4());
11 f(next_test_ip6(), next_test_ip6());
12 }
13
14 macro_rules! t {
15 ($e:expr) => {
16 match $e {
17 Ok(t) => t,
18 Err(e) => panic!("received error for `{}`: {}", stringify!($e), e),
19 }
20 };
21 }
22
23 #[test]
24 fn bind_error() {
25 match UdpSocket::bind("1.1.1.1:9999") {
26 Ok(..) => panic!(),
27 Err(e) => assert_eq!(e.kind(), ErrorKind::AddrNotAvailable),
28 }
29 }
30
31 #[test]
32 fn socket_smoke_test_ip4() {
33 each_ip(&mut |server_ip, client_ip| {
34 let (tx1, rx1) = channel();
35 let (tx2, rx2) = channel();
36
37 let _t = thread::spawn(move || {
38 let client = t!(UdpSocket::bind(&client_ip));
39 rx1.recv().unwrap();
40 t!(client.send_to(&[99], &server_ip));
41 tx2.send(()).unwrap();
42 });
43
44 let server = t!(UdpSocket::bind(&server_ip));
45 tx1.send(()).unwrap();
46 let mut buf = [0];
47 let (nread, src) = t!(server.recv_from(&mut buf));
48 assert_eq!(nread, 1);
49 assert_eq!(buf[0], 99);
50 assert_eq!(src, client_ip);
51 rx2.recv().unwrap();
52 })
53 }
54
55 #[test]
56 fn socket_name() {
57 each_ip(&mut |addr, _| {
58 let server = t!(UdpSocket::bind(&addr));
59 assert_eq!(addr, t!(server.local_addr()));
60 })
61 }
62
63 #[test]
64 fn socket_peer() {
65 each_ip(&mut |addr1, addr2| {
66 let server = t!(UdpSocket::bind(&addr1));
67 assert_eq!(server.peer_addr().unwrap_err().kind(), ErrorKind::NotConnected);
68 t!(server.connect(&addr2));
69 assert_eq!(addr2, t!(server.peer_addr()));
70 })
71 }
72
73 #[test]
74 fn udp_clone_smoke() {
75 each_ip(&mut |addr1, addr2| {
76 let sock1 = t!(UdpSocket::bind(&addr1));
77 let sock2 = t!(UdpSocket::bind(&addr2));
78
79 let _t = thread::spawn(move || {
80 let mut buf = [0, 0];
81 assert_eq!(sock2.recv_from(&mut buf).unwrap(), (1, addr1));
82 assert_eq!(buf[0], 1);
83 t!(sock2.send_to(&[2], &addr1));
84 });
85
86 let sock3 = t!(sock1.try_clone());
87
88 let (tx1, rx1) = channel();
89 let (tx2, rx2) = channel();
90 let _t = thread::spawn(move || {
91 rx1.recv().unwrap();
92 t!(sock3.send_to(&[1], &addr2));
93 tx2.send(()).unwrap();
94 });
95 tx1.send(()).unwrap();
96 let mut buf = [0, 0];
97 assert_eq!(sock1.recv_from(&mut buf).unwrap(), (1, addr2));
98 rx2.recv().unwrap();
99 })
100 }
101
102 #[test]
103 fn udp_clone_two_read() {
104 each_ip(&mut |addr1, addr2| {
105 let sock1 = t!(UdpSocket::bind(&addr1));
106 let sock2 = t!(UdpSocket::bind(&addr2));
107 let (tx1, rx) = channel();
108 let tx2 = tx1.clone();
109
110 let _t = thread::spawn(move || {
111 t!(sock2.send_to(&[1], &addr1));
112 rx.recv().unwrap();
113 t!(sock2.send_to(&[2], &addr1));
114 rx.recv().unwrap();
115 });
116
117 let sock3 = t!(sock1.try_clone());
118
119 let (done, rx) = channel();
120 let _t = thread::spawn(move || {
121 let mut buf = [0, 0];
122 t!(sock3.recv_from(&mut buf));
123 tx2.send(()).unwrap();
124 done.send(()).unwrap();
125 });
126 let mut buf = [0, 0];
127 t!(sock1.recv_from(&mut buf));
128 tx1.send(()).unwrap();
129
130 rx.recv().unwrap();
131 })
132 }
133
134 #[test]
135 fn udp_clone_two_write() {
136 each_ip(&mut |addr1, addr2| {
137 let sock1 = t!(UdpSocket::bind(&addr1));
138 let sock2 = t!(UdpSocket::bind(&addr2));
139
140 let (tx, rx) = channel();
141 let (serv_tx, serv_rx) = channel();
142
143 let _t = thread::spawn(move || {
144 let mut buf = [0, 1];
145 rx.recv().unwrap();
146 t!(sock2.recv_from(&mut buf));
147 serv_tx.send(()).unwrap();
148 });
149
150 let sock3 = t!(sock1.try_clone());
151
152 let (done, rx) = channel();
153 let tx2 = tx.clone();
154 let _t = thread::spawn(move || {
155 match sock3.send_to(&[1], &addr2) {
156 Ok(..) => {
157 let _ = tx2.send(());
158 }
159 Err(..) => {}
160 }
161 done.send(()).unwrap();
162 });
163 match sock1.send_to(&[2], &addr2) {
164 Ok(..) => {
165 let _ = tx.send(());
166 }
167 Err(..) => {}
168 }
169 drop(tx);
170
171 rx.recv().unwrap();
172 serv_rx.recv().unwrap();
173 })
174 }
175
176 #[test]
177 fn debug() {
178 let name = if cfg!(windows) { "socket" } else { "fd" };
179 let socket_addr = next_test_ip4();
180
181 let udpsock = t!(UdpSocket::bind(&socket_addr));
182 let udpsock_inner = udpsock.0.socket().as_inner();
183 let compare = format!("UdpSocket {{ addr: {:?}, {}: {:?} }}", socket_addr, name, udpsock_inner);
184 assert_eq!(format!("{:?}", udpsock), compare);
185 }
186
187 // FIXME: re-enabled openbsd/netbsd tests once their socket timeout code
188 // no longer has rounding errors.
189 // VxWorks ignores SO_SNDTIMEO.
190 #[cfg_attr(any(target_os = "netbsd", target_os = "openbsd", target_os = "vxworks"), ignore)]
191 #[test]
192 fn timeouts() {
193 let addr = next_test_ip4();
194
195 let stream = t!(UdpSocket::bind(&addr));
196 let dur = Duration::new(15410, 0);
197
198 assert_eq!(None, t!(stream.read_timeout()));
199
200 t!(stream.set_read_timeout(Some(dur)));
201 assert_eq!(Some(dur), t!(stream.read_timeout()));
202
203 assert_eq!(None, t!(stream.write_timeout()));
204
205 t!(stream.set_write_timeout(Some(dur)));
206 assert_eq!(Some(dur), t!(stream.write_timeout()));
207
208 t!(stream.set_read_timeout(None));
209 assert_eq!(None, t!(stream.read_timeout()));
210
211 t!(stream.set_write_timeout(None));
212 assert_eq!(None, t!(stream.write_timeout()));
213 }
214
215 #[test]
216 fn test_read_timeout() {
217 let addr = next_test_ip4();
218
219 let stream = t!(UdpSocket::bind(&addr));
220 t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
221
222 let mut buf = [0; 10];
223
224 let start = Instant::now();
225 loop {
226 let kind = stream.recv_from(&mut buf).err().expect("expected error").kind();
227 if kind != ErrorKind::Interrupted {
228 assert!(
229 kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
230 "unexpected_error: {:?}",
231 kind
232 );
233 break;
234 }
235 }
236 assert!(start.elapsed() > Duration::from_millis(400));
237 }
238
239 #[test]
240 fn test_read_with_timeout() {
241 let addr = next_test_ip4();
242
243 let stream = t!(UdpSocket::bind(&addr));
244 t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
245
246 t!(stream.send_to(b"hello world", &addr));
247
248 let mut buf = [0; 11];
249 t!(stream.recv_from(&mut buf));
250 assert_eq!(b"hello world", &buf[..]);
251
252 let start = Instant::now();
253 loop {
254 let kind = stream.recv_from(&mut buf).err().expect("expected error").kind();
255 if kind != ErrorKind::Interrupted {
256 assert!(
257 kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
258 "unexpected_error: {:?}",
259 kind
260 );
261 break;
262 }
263 }
264 assert!(start.elapsed() > Duration::from_millis(400));
265 }
266
267 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
268 // when passed zero Durations
269 #[test]
270 fn test_timeout_zero_duration() {
271 let addr = next_test_ip4();
272
273 let socket = t!(UdpSocket::bind(&addr));
274
275 let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
276 let err = result.unwrap_err();
277 assert_eq!(err.kind(), ErrorKind::InvalidInput);
278
279 let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
280 let err = result.unwrap_err();
281 assert_eq!(err.kind(), ErrorKind::InvalidInput);
282 }
283
284 #[test]
285 fn connect_send_recv() {
286 let addr = next_test_ip4();
287
288 let socket = t!(UdpSocket::bind(&addr));
289 t!(socket.connect(addr));
290
291 t!(socket.send(b"hello world"));
292
293 let mut buf = [0; 11];
294 t!(socket.recv(&mut buf));
295 assert_eq!(b"hello world", &buf[..]);
296 }
297
298 #[test]
299 fn connect_send_peek_recv() {
300 each_ip(&mut |addr, _| {
301 let socket = t!(UdpSocket::bind(&addr));
302 t!(socket.connect(addr));
303
304 t!(socket.send(b"hello world"));
305
306 for _ in 1..3 {
307 let mut buf = [0; 11];
308 let size = t!(socket.peek(&mut buf));
309 assert_eq!(b"hello world", &buf[..]);
310 assert_eq!(size, 11);
311 }
312
313 let mut buf = [0; 11];
314 let size = t!(socket.recv(&mut buf));
315 assert_eq!(b"hello world", &buf[..]);
316 assert_eq!(size, 11);
317 })
318 }
319
320 #[test]
321 fn peek_from() {
322 each_ip(&mut |addr, _| {
323 let socket = t!(UdpSocket::bind(&addr));
324 t!(socket.send_to(b"hello world", &addr));
325
326 for _ in 1..3 {
327 let mut buf = [0; 11];
328 let (size, _) = t!(socket.peek_from(&mut buf));
329 assert_eq!(b"hello world", &buf[..]);
330 assert_eq!(size, 11);
331 }
332
333 let mut buf = [0; 11];
334 let (size, _) = t!(socket.recv_from(&mut buf));
335 assert_eq!(b"hello world", &buf[..]);
336 assert_eq!(size, 11);
337 })
338 }
339
340 #[test]
341 fn ttl() {
342 let ttl = 100;
343
344 let addr = next_test_ip4();
345
346 let stream = t!(UdpSocket::bind(&addr));
347
348 t!(stream.set_ttl(ttl));
349 assert_eq!(ttl, t!(stream.ttl()));
350 }
351
352 #[test]
353 fn set_nonblocking() {
354 each_ip(&mut |addr, _| {
355 let socket = t!(UdpSocket::bind(&addr));
356
357 t!(socket.set_nonblocking(true));
358 t!(socket.set_nonblocking(false));
359
360 t!(socket.connect(addr));
361
362 t!(socket.set_nonblocking(false));
363 t!(socket.set_nonblocking(true));
364
365 let mut buf = [0];
366 match socket.recv(&mut buf) {
367 Ok(_) => panic!("expected error"),
368 Err(ref e) if e.kind() == ErrorKind::WouldBlock => {}
369 Err(e) => panic!("unexpected error {}", e),
370 }
371 })
372 }