1 use crate::io
::ErrorKind
;
2 use crate::net
::test
::{next_test_ip4, next_test_ip6}
;
4 use crate::sync
::mpsc
::channel
;
5 use crate::sys_common
::AsInner
;
7 use crate::time
::{Duration, Instant}
;
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());
18 Err(e
) => panic
!("received error for `{}`: {}", stringify
!($e
), e
),
25 match UdpSocket
::bind("1.1.1.1:9999") {
27 Err(e
) => assert_eq
!(e
.kind(), ErrorKind
::AddrNotAvailable
),
32 fn socket_smoke_test_ip4() {
33 each_ip(&mut |server_ip
, client_ip
| {
34 let (tx1
, rx1
) = channel();
35 let (tx2
, rx2
) = channel();
37 let _t
= thread
::spawn(move || {
38 let client
= t
!(UdpSocket
::bind(&client_ip
));
40 t
!(client
.send_to(&[99], &server_ip
));
41 tx2
.send(()).unwrap();
44 let server
= t
!(UdpSocket
::bind(&server_ip
));
45 tx1
.send(()).unwrap();
47 let (nread
, src
) = t
!(server
.recv_from(&mut buf
));
49 assert_eq
!(buf
[0], 99);
50 assert_eq
!(src
, client_ip
);
57 each_ip(&mut |addr
, _
| {
58 let server
= t
!(UdpSocket
::bind(&addr
));
59 assert_eq
!(addr
, t
!(server
.local_addr()));
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()));
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
));
79 let _t
= thread
::spawn(move || {
81 assert_eq
!(sock2
.recv_from(&mut buf
).unwrap(), (1, addr1
));
82 assert_eq
!(buf
[0], 1);
83 t
!(sock2
.send_to(&[2], &addr1
));
86 let sock3
= t
!(sock1
.try_clone());
88 let (tx1
, rx1
) = channel();
89 let (tx2
, rx2
) = channel();
90 let _t
= thread
::spawn(move || {
92 t
!(sock3
.send_to(&[1], &addr2
));
93 tx2
.send(()).unwrap();
95 tx1
.send(()).unwrap();
97 assert_eq
!(sock1
.recv_from(&mut buf
).unwrap(), (1, addr2
));
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();
110 let _t
= thread
::spawn(move || {
111 t
!(sock2
.send_to(&[1], &addr1
));
113 t
!(sock2
.send_to(&[2], &addr1
));
117 let sock3
= t
!(sock1
.try_clone());
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();
126 let mut buf
= [0, 0];
127 t
!(sock1
.recv_from(&mut buf
));
128 tx1
.send(()).unwrap();
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
));
140 let (tx
, rx
) = channel();
141 let (serv_tx
, serv_rx
) = channel();
143 let _t
= thread
::spawn(move || {
144 let mut buf
= [0, 1];
146 t
!(sock2
.recv_from(&mut buf
));
147 serv_tx
.send(()).unwrap();
150 let sock3
= t
!(sock1
.try_clone());
152 let (done
, rx
) = channel();
153 let tx2
= tx
.clone();
154 let _t
= thread
::spawn(move || {
155 match sock3
.send_to(&[1], &addr2
) {
157 let _
= tx2
.send(());
161 done
.send(()).unwrap();
163 match sock1
.send_to(&[2], &addr2
) {
172 serv_rx
.recv().unwrap();
178 let name
= if cfg
!(windows
) { "socket" }
else { "fd" }
;
179 let socket_addr
= next_test_ip4();
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
);
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)]
193 let addr
= next_test_ip4();
195 let stream
= t
!(UdpSocket
::bind(&addr
));
196 let dur
= Duration
::new(15410, 0);
198 assert_eq
!(None
, t
!(stream
.read_timeout()));
200 t
!(stream
.set_read_timeout(Some(dur
)));
201 assert_eq
!(Some(dur
), t
!(stream
.read_timeout()));
203 assert_eq
!(None
, t
!(stream
.write_timeout()));
205 t
!(stream
.set_write_timeout(Some(dur
)));
206 assert_eq
!(Some(dur
), t
!(stream
.write_timeout()));
208 t
!(stream
.set_read_timeout(None
));
209 assert_eq
!(None
, t
!(stream
.read_timeout()));
211 t
!(stream
.set_write_timeout(None
));
212 assert_eq
!(None
, t
!(stream
.write_timeout()));
216 fn test_read_timeout() {
217 let addr
= next_test_ip4();
219 let stream
= t
!(UdpSocket
::bind(&addr
));
220 t
!(stream
.set_read_timeout(Some(Duration
::from_millis(1000))));
222 let mut buf
= [0; 10];
224 let start
= Instant
::now();
226 let kind
= stream
.recv_from(&mut buf
).err().expect("expected error").kind();
227 if kind
!= ErrorKind
::Interrupted
{
229 kind
== ErrorKind
::WouldBlock
|| kind
== ErrorKind
::TimedOut
,
230 "unexpected_error: {:?}",
236 assert
!(start
.elapsed() > Duration
::from_millis(400));
240 fn test_read_with_timeout() {
241 let addr
= next_test_ip4();
243 let stream
= t
!(UdpSocket
::bind(&addr
));
244 t
!(stream
.set_read_timeout(Some(Duration
::from_millis(1000))));
246 t
!(stream
.send_to(b
"hello world", &addr
));
248 let mut buf
= [0; 11];
249 t
!(stream
.recv_from(&mut buf
));
250 assert_eq
!(b
"hello world", &buf
[..]);
252 let start
= Instant
::now();
254 let kind
= stream
.recv_from(&mut buf
).err().expect("expected error").kind();
255 if kind
!= ErrorKind
::Interrupted
{
257 kind
== ErrorKind
::WouldBlock
|| kind
== ErrorKind
::TimedOut
,
258 "unexpected_error: {:?}",
264 assert
!(start
.elapsed() > Duration
::from_millis(400));
267 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
268 // when passed zero Durations
270 fn test_timeout_zero_duration() {
271 let addr
= next_test_ip4();
273 let socket
= t
!(UdpSocket
::bind(&addr
));
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
);
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
);
285 fn connect_send_recv() {
286 let addr
= next_test_ip4();
288 let socket
= t
!(UdpSocket
::bind(&addr
));
289 t
!(socket
.connect(addr
));
291 t
!(socket
.send(b
"hello world"));
293 let mut buf
= [0; 11];
294 t
!(socket
.recv(&mut buf
));
295 assert_eq
!(b
"hello world", &buf
[..]);
299 fn connect_send_peek_recv() {
300 each_ip(&mut |addr
, _
| {
301 let socket
= t
!(UdpSocket
::bind(&addr
));
302 t
!(socket
.connect(addr
));
304 t
!(socket
.send(b
"hello world"));
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);
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);
322 each_ip(&mut |addr
, _
| {
323 let socket
= t
!(UdpSocket
::bind(&addr
));
324 t
!(socket
.send_to(b
"hello world", &addr
));
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);
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);
344 let addr
= next_test_ip4();
346 let stream
= t
!(UdpSocket
::bind(&addr
));
348 t
!(stream
.set_ttl(ttl
));
349 assert_eq
!(ttl
, t
!(stream
.ttl()));
353 fn set_nonblocking() {
354 each_ip(&mut |addr
, _
| {
355 let socket
= t
!(UdpSocket
::bind(&addr
));
357 t
!(socket
.set_nonblocking(true));
358 t
!(socket
.set_nonblocking(false));
360 t
!(socket
.connect(addr
));
362 t
!(socket
.set_nonblocking(false));
363 t
!(socket
.set_nonblocking(true));
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
),