Skip to content

Commit

Permalink
Add tests ensuring zero-Duration timeouts result in errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Feb 19, 2018
1 parent 1ad094d commit d17d645
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/libstd/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,26 @@ mod tests {
drop(listener);
}

// Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
// when passed zero Durations
#[test]
fn test_timeout_zero_duration() {
let addr = next_test_ip4();

let listener = t!(TcpListener::bind(&addr));
let stream = t!(TcpStream::connect(&addr));

let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);

let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);

drop(listener);
}

#[test]
fn nodelay() {
let addr = next_test_ip4();
Expand Down
17 changes: 17 additions & 0 deletions src/libstd/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,23 @@ mod tests {
assert!(start.elapsed() > Duration::from_millis(400));
}

// Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
// when passed zero Durations
#[test]
fn test_timeout_zero_duration() {
let addr = next_test_ip4();

let socket = t!(UdpSocket::bind(&addr));

let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);

let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);
}

#[test]
fn connect_send_recv() {
let addr = next_test_ip4();
Expand Down
41 changes: 40 additions & 1 deletion src/libstd/sys/unix/ext/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ impl IntoRawFd for UnixDatagram {
#[cfg(all(test, not(target_os = "emscripten")))]
mod test {
use thread;
use io;
use io::{self, ErrorKind};
use io::prelude::*;
use time::Duration;
use sys_common::io::test::tmpdir;
Expand Down Expand Up @@ -1613,6 +1613,27 @@ mod test {
assert!(kind == io::ErrorKind::WouldBlock || kind == io::ErrorKind::TimedOut);
}

// Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
// when passed zero Durations
#[test]
fn test_unix_stream_timeout_zero_duration() {
let dir = tmpdir();
let socket_path = dir.path().join("sock");

let listener = or_panic!(UnixListener::bind(&socket_path));
let stream = or_panic!(UnixStream::connect(&socket_path));

let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);

let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);

drop(listener);
}

#[test]
fn test_unix_datagram() {
let dir = tmpdir();
Expand Down Expand Up @@ -1712,6 +1733,24 @@ mod test {
thread.join().unwrap();
}

// Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
// when passed zero Durations
#[test]
fn test_unix_datagram_timeout_zero_duration() {
let dir = tmpdir();
let path = dir.path().join("sock");

let datagram = or_panic!(UnixDatagram::bind(&path));

let result = datagram.set_write_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);

let result = datagram.set_read_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);
}

#[test]
fn abstract_namespace_not_allowed() {
assert!(UnixStream::connect("\0asdf").is_err());
Expand Down

0 comments on commit d17d645

Please sign in to comment.