From 22f62df337b015df73104d24999e6f4e30f18d14 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Fri, 9 Jun 2023 21:45:57 +0800 Subject: [PATCH 1/5] Fix windows `Socket::connect_timeout` overflow Signed-off-by: Eval EXEC --- library/std/src/sys/windows/net.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/windows/net.rs b/library/std/src/sys/windows/net.rs index 2404bbe2b89..1ae42cb7eae 100644 --- a/library/std/src/sys/windows/net.rs +++ b/library/std/src/sys/windows/net.rs @@ -159,7 +159,7 @@ impl Socket { } let mut timeout = c::timeval { - tv_sec: timeout.as_secs() as c_long, + tv_sec: cmp::min(timeout.as_secs(), c_long::MAX as u64) as c_long, tv_usec: (timeout.subsec_nanos() / 1000) as c_long, }; From fca9e6e706d1b611c81c97ccdff8265f5f1ee638 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Sun, 11 Jun 2023 08:57:16 +0800 Subject: [PATCH 2/5] Add unit test for `TcpStream::connect_timeout` Signed-off-by: Eval EXEC --- library/std/src/net/tcp/tests.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/std/src/net/tcp/tests.rs b/library/std/src/net/tcp/tests.rs index 7a3c66e4504..08ee451dfcd 100644 --- a/library/std/src/net/tcp/tests.rs +++ b/library/std/src/net/tcp/tests.rs @@ -46,6 +46,26 @@ fn connect_error() { } } +#[test] +fn connect_timeout_error() { + let socket_addr = next_test_ip4(); + let result = TcpStream::connect_timeout(&socket_addr, Duration::MAX); + assert!(!matches!(result, Err(e) if e.kind() == ErrorKind::TimedOut)); + + let _listener = TcpListener::bind(&socket_addr).unwrap(); + assert!(TcpStream::connect_timeout(&socket_addr, Duration::MAX).is_ok()); +} + +#[test] +fn connect_timeout_ok_bind() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); // :0 picks some free port + let port = listener.local_addr().unwrap().port(); // obtain the port it picked + assert!( + TcpStream::connect_timeout(&format!("127.0.0.1:{port}").parse().unwrap(), Duration::MAX) + .is_ok() + ); +} + #[test] fn listen_localhost() { let socket_addr = next_test_ip4(); From f65b5d0ddf1ae0a551756424503c3cfe0e7ad8a6 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Sun, 18 Jun 2023 15:41:50 +0800 Subject: [PATCH 3/5] Add unit test to connect to an unreachable address Signed-off-by: Eval EXEC --- library/std/src/net/tcp/tests.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/library/std/src/net/tcp/tests.rs b/library/std/src/net/tcp/tests.rs index 08ee451dfcd..4209d4b6342 100644 --- a/library/std/src/net/tcp/tests.rs +++ b/library/std/src/net/tcp/tests.rs @@ -46,6 +46,17 @@ fn connect_error() { } } +#[test] +fn connect_timeout_to_unreachable_address() { + let now = Instant::now(); + match TcpStream::connect_timeout(&format!("1.1.1.1:9999").parse().unwrap(), Duration::MAX) { + Ok(..) => panic!("connected to an unreachable address, this is impossible"), + Err(e) => assert_eq!(e.kind(), ErrorKind::TimedOut), + } + + assert!(now.elapsed() > Duration::from_secs(20)); +} + #[test] fn connect_timeout_error() { let socket_addr = next_test_ip4(); From 30e1c1a53c8354d53551f4640a41938a157b4fd0 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Tue, 20 Jun 2023 18:43:12 +0800 Subject: [PATCH 4/5] Ignore `connect_timeout` unit test on SGX platform Co-authored-by: Chris Denton --- library/std/src/net/tcp/tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/net/tcp/tests.rs b/library/std/src/net/tcp/tests.rs index 4209d4b6342..12979db25d3 100644 --- a/library/std/src/net/tcp/tests.rs +++ b/library/std/src/net/tcp/tests.rs @@ -58,6 +58,7 @@ fn connect_timeout_to_unreachable_address() { } #[test] +#[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31 fn connect_timeout_error() { let socket_addr = next_test_ip4(); let result = TcpStream::connect_timeout(&socket_addr, Duration::MAX); From a0c757a13f5a0090f8cb6e1a7ffba6b94dfc2fbf Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Tue, 20 Jun 2023 18:47:31 +0800 Subject: [PATCH 5/5] Remove useless unit tests --- library/std/src/net/tcp/tests.rs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/library/std/src/net/tcp/tests.rs b/library/std/src/net/tcp/tests.rs index 12979db25d3..db367cfa0f7 100644 --- a/library/std/src/net/tcp/tests.rs +++ b/library/std/src/net/tcp/tests.rs @@ -46,17 +46,6 @@ fn connect_error() { } } -#[test] -fn connect_timeout_to_unreachable_address() { - let now = Instant::now(); - match TcpStream::connect_timeout(&format!("1.1.1.1:9999").parse().unwrap(), Duration::MAX) { - Ok(..) => panic!("connected to an unreachable address, this is impossible"), - Err(e) => assert_eq!(e.kind(), ErrorKind::TimedOut), - } - - assert!(now.elapsed() > Duration::from_secs(20)); -} - #[test] #[cfg_attr(target_env = "sgx", ignore)] // FIXME: https://github.com/fortanix/rust-sgx/issues/31 fn connect_timeout_error() { @@ -68,16 +57,6 @@ fn connect_timeout_error() { assert!(TcpStream::connect_timeout(&socket_addr, Duration::MAX).is_ok()); } -#[test] -fn connect_timeout_ok_bind() { - let listener = TcpListener::bind("127.0.0.1:0").unwrap(); // :0 picks some free port - let port = listener.local_addr().unwrap().port(); // obtain the port it picked - assert!( - TcpStream::connect_timeout(&format!("127.0.0.1:{port}").parse().unwrap(), Duration::MAX) - .is_ok() - ); -} - #[test] fn listen_localhost() { let socket_addr = next_test_ip4();