diff --git a/mbedtls/tests/client_server.rs b/mbedtls/tests/client_server.rs index 154d2cbc8..ca8fd363b 100644 --- a/mbedtls/tests/client_server.rs +++ b/mbedtls/tests/client_server.rs @@ -252,7 +252,7 @@ where #[cfg(unix)] mod test { - use std::thread; + use crate::support::thread_spawn_named; #[test] fn client_server_test() { @@ -348,8 +348,8 @@ mod test { // TLS tests using certificates let (c, s) = crate::support::net::create_tcp_pair().unwrap(); - let c = thread::spawn(move || super::client(c, min_c, max_c, exp_ver, false).unwrap()); - let s = thread::spawn(move || super::server(s, min_s, max_s, exp_ver, false).unwrap()); + let c = thread_spawn_named("client_tls_cert", move || super::client(c, min_c, max_c, exp_ver, false).unwrap()); + let s = thread_spawn_named("server_tls_cert", move || super::server(s, min_s, max_s, exp_ver, false).unwrap()); c.join().unwrap(); s.join().unwrap(); @@ -357,8 +357,8 @@ mod test { // TLS tests using PSK let (c, s) = crate::support::net::create_tcp_pair().unwrap(); - let c = thread::spawn(move || super::client(c, min_c, max_c, exp_ver, true).unwrap()); - let s = thread::spawn(move || super::server(s, min_s, max_s, exp_ver, true).unwrap()); + let c = thread_spawn_named("client_tls_psk", move || super::client(c, min_c, max_c, exp_ver, true).unwrap()); + let s = thread_spawn_named("server_tls_psk", move || super::server(s, min_s, max_s, exp_ver, true).unwrap()); c.join().unwrap(); s.join().unwrap(); @@ -372,10 +372,10 @@ mod test { let s = UdpSocket::bind("127.0.0.1:12340").expect("could not bind UdpSocket"); let s = ConnectedUdpSocket::connect(s, "127.0.0.1:12341").expect("could not connect UdpSocket"); - let s = thread::spawn(move || super::server(s, min_s, max_s, exp_ver, false).unwrap()); + let s = thread_spawn_named("server_dtls_cert", move || super::server(s, min_s, max_s, exp_ver, false).unwrap()); let c = UdpSocket::bind("127.0.0.1:12341").expect("could not bind UdpSocket"); let c = ConnectedUdpSocket::connect(c, "127.0.0.1:12340").expect("could not connect UdpSocket"); - let c = thread::spawn(move || super::client(c, min_c, max_c, exp_ver, false).unwrap()); + let c = thread_spawn_named("client_dtls_cert", move || super::client(c, min_c, max_c, exp_ver, false).unwrap()); s.join().unwrap(); c.join().unwrap(); @@ -389,10 +389,10 @@ mod test { let s = UdpSocket::bind("127.0.0.1:12340").expect("could not bind UdpSocket"); let s = ConnectedUdpSocket::connect(s, "127.0.0.1:12341").expect("could not connect UdpSocket"); - let s = thread::spawn(move || super::server(s, min_s, max_s, exp_ver, true).unwrap()); + let s = thread_spawn_named("client_dtls_psk", move || super::server(s, min_s, max_s, exp_ver, true).unwrap()); let c = UdpSocket::bind("127.0.0.1:12341").expect("could not bind UdpSocket"); let c = ConnectedUdpSocket::connect(c, "127.0.0.1:12340").expect("could not connect UdpSocket"); - let c = thread::spawn(move || super::client(c, min_c, max_c, exp_ver, true).unwrap()); + let c = thread_spawn_named("client_dtls_psk", move || super::client(c, min_c, max_c, exp_ver, true).unwrap()); s.join().unwrap(); c.join().unwrap(); @@ -411,14 +411,14 @@ mod test { let data_to_write = expected_data.clone(); assert_eq!(expected_data, data_to_write); let (c, s) = crate::support::net::create_tcp_pair().unwrap(); - let c = thread::spawn(move || { + let c = thread_spawn_named("client", move || { super::with_client(c, move |mut session| { let ret = session.write_all(&data_to_write); assert!(ret.is_ok()); }) }); - let s = thread::spawn(move || { + let s = thread_spawn_named("server", move || { super::with_server(s, move |mut session| { let mut buf = vec![0; buffer_size]; match session.read_exact(&mut buf) { @@ -450,7 +450,7 @@ mod test { let data_to_write = expected_data.clone(); assert_eq!(expected_data, data_to_write); let (c, s) = crate::support::net::create_tcp_pair().unwrap(); - let c = thread::spawn(move || { + let c = thread_spawn_named("client", move || { super::with_client(c, move |mut session| { let ret = session.write_all(&data_to_write); assert!(ret.is_err()); @@ -460,7 +460,7 @@ mod test { }) }); - let s = thread::spawn(move || { + let s = thread_spawn_named("server", move || { super::with_server(s, move |mut session| { let mut buf = vec![0; buffer_size]; match session.read_exact(&mut buf) { diff --git a/mbedtls/tests/hyper.rs b/mbedtls/tests/hyper.rs index a5d8391b3..47df243d7 100644 --- a/mbedtls/tests/hyper.rs +++ b/mbedtls/tests/hyper.rs @@ -9,6 +9,12 @@ use std::net::SocketAddr; use std::sync::{Arc, Mutex}; use std::time::Duration; +// needed to have common code for `mod support` in unit and integrations tests +extern crate mbedtls; + +mod support; +use support::thread_spawn_named; + // Native TLS compatibility - to move to native tls client in the future #[derive(Clone)] pub struct TlsStream { @@ -254,12 +260,12 @@ mod tests { let clone1 = client.clone(); let clone2 = client.clone(); - let t1 = std::thread::spawn(move || { + let t1 = thread_spawn_named("client1", move || { let response = clone1.get("https://google.com").send().unwrap(); assert_eq!(response.status, hyper::status::StatusCode::Ok); }); - let t2 = std::thread::spawn(move || { + let t2 = thread_spawn_named("client2", move || { clone2.post("https://google.com").body("foo=bar").send().unwrap(); }); @@ -353,12 +359,10 @@ mod tests { let client1 = client.clone(); // If this fails due to EWOULDBLOCK it means not enough threads were created. - let t1 = std::thread::Builder::new() - .spawn(move || { + let t1 = thread_spawn_named("client", move || { let response = client1.post(&format!("https://{}/path", local_addr)).body("foo=bar").send(); println!("{:?}", response); - }) - .unwrap(); + }); t1.join().unwrap(); handler.close().unwrap(); diff --git a/mbedtls/tests/ssl_conf_ca_cb.rs b/mbedtls/tests/ssl_conf_ca_cb.rs index 54e847e65..c8b0baa61 100644 --- a/mbedtls/tests/ssl_conf_ca_cb.rs +++ b/mbedtls/tests/ssl_conf_ca_cb.rs @@ -59,8 +59,8 @@ mod test { use super::*; use crate::support::keys; use crate::support::net::create_tcp_pair; + use crate::support::thread_spawn_named; use mbedtls::error::codes; - use std::thread; // This callback should accept any valid self-signed certificate fn self_signed_ca_callback(child: &MbedtlsList) -> TlsResult> { @@ -74,8 +74,8 @@ mod test { let ca_callback = |_: &MbedtlsList| -> TlsResult> { Ok(Certificate::from_pem_multiple(keys::ROOT_CA_CERT.as_bytes()).unwrap()) }; - let c = thread::spawn(move || super::client(c, ca_callback).unwrap()); - let s = thread::spawn(move || super::server(s, keys::PEM_CERT.as_bytes(), keys::PEM_KEY.as_bytes()).unwrap()); + let c = thread_spawn_named("client", move || super::client(c, ca_callback).unwrap()); + let s = thread_spawn_named("server", move || super::server(s, keys::PEM_CERT.as_bytes(), keys::PEM_KEY.as_bytes()).unwrap()); c.join().unwrap(); s.join().unwrap(); } @@ -85,11 +85,11 @@ mod test { let (c, s) = create_tcp_pair().unwrap(); let ca_callback = |_: &MbedtlsList| -> TlsResult> { Ok(MbedtlsList::::new()) }; - let c = thread::spawn(move || { + let c = thread_spawn_named("client", move || { let result = super::client(c, ca_callback); assert_eq!(result, Err(codes::X509CertVerifyFailed.into())); }); - let s = thread::spawn(move || super::server(s, keys::PEM_CERT.as_bytes(), keys::PEM_KEY.as_bytes()).unwrap()); + let s = thread_spawn_named("server", move || super::server(s, keys::PEM_CERT.as_bytes(), keys::PEM_KEY.as_bytes()).unwrap()); c.join().unwrap(); s.join().unwrap(); } @@ -97,8 +97,8 @@ mod test { #[test] fn callback_self_signed() { let (c, s) = create_tcp_pair().unwrap(); - let c = thread::spawn(move || super::client(c, self_signed_ca_callback).unwrap()); - let s = thread::spawn(move || super::server(s, keys::PEM_SELF_SIGNED_CERT, keys::PEM_SELF_SIGNED_KEY).unwrap()); + let c = thread_spawn_named("client", move || super::client(c, self_signed_ca_callback).unwrap()); + let s = thread_spawn_named("server", move || super::server(s, keys::PEM_SELF_SIGNED_CERT, keys::PEM_SELF_SIGNED_KEY).unwrap()); c.join().unwrap(); s.join().unwrap(); } @@ -109,11 +109,11 @@ mod test { // be rejected by the client, because the ca_callback should only accept // self-signed certificates. let (c, s) = create_tcp_pair().unwrap(); - let c = thread::spawn(move || { + let c = thread_spawn_named("client", move || { let result = super::client(c, self_signed_ca_callback); assert_eq!(result, Err(codes::X509CertVerifyFailed.into())); }); - let s = thread::spawn(move || super::server(s, keys::PEM_CERT.as_bytes(), keys::PEM_KEY.as_bytes()).unwrap()); + let s = thread_spawn_named("server", move || super::server(s, keys::PEM_CERT.as_bytes(), keys::PEM_KEY.as_bytes()).unwrap()); c.join().unwrap(); s.join().unwrap(); } @@ -123,12 +123,12 @@ mod test { // We set up the server to supply a self-signed certificate with an invalid // signature. It should be rejected by the client. let (c, s) = create_tcp_pair().unwrap(); - let c = thread::spawn(move || { + let c = thread_spawn_named("client", move || { let result = super::client(c, self_signed_ca_callback); assert_eq!(result, Err(codes::X509CertVerifyFailed.into())); }); let s = - thread::spawn(move || super::server(s, keys::PEM_SELF_SIGNED_CERT_INVALID_SIG, keys::PEM_SELF_SIGNED_KEY).unwrap()); + thread_spawn_named("server", move || super::server(s, keys::PEM_SELF_SIGNED_CERT_INVALID_SIG, keys::PEM_SELF_SIGNED_KEY).unwrap()); c.join().unwrap(); s.join().unwrap(); } diff --git a/mbedtls/tests/ssl_conf_verify.rs b/mbedtls/tests/ssl_conf_verify.rs index 08532f859..4fe10df71 100644 --- a/mbedtls/tests/ssl_conf_verify.rs +++ b/mbedtls/tests/ssl_conf_verify.rs @@ -83,14 +83,14 @@ fn server(conn: TcpStream) -> TlsResult<()> { #[cfg(unix)] mod test { use crate::support::net::create_tcp_pair; - use std::thread; + use crate::support::thread_spawn_named; #[test] fn callback_set_verify_flags() { let (c, s) = create_tcp_pair().unwrap(); - let c = thread::spawn(move || super::client(c, super::Test::CallbackSetVerifyFlags).unwrap()); - let s = thread::spawn(move || super::server(s).unwrap()); + let c = thread_spawn_named("client", move || super::client(c, super::Test::CallbackSetVerifyFlags).unwrap()); + let s = thread_spawn_named("server", move || super::server(s).unwrap()); c.join().unwrap(); s.join().unwrap(); } @@ -99,8 +99,8 @@ mod test { fn callback_error() { let (c, s) = create_tcp_pair().unwrap(); - let c = thread::spawn(move || super::client(c, super::Test::CallbackError).unwrap()); - let s = thread::spawn(move || super::server(s).unwrap()); + let c = thread_spawn_named("client", move || super::client(c, super::Test::CallbackError).unwrap()); + let s = thread_spawn_named("server", move || super::server(s).unwrap()); c.join().unwrap(); s.join().unwrap(); } diff --git a/mbedtls/tests/support/mod.rs b/mbedtls/tests/support/mod.rs index b236a64da..367a4b031 100644 --- a/mbedtls/tests/support/mod.rs +++ b/mbedtls/tests/support/mod.rs @@ -7,6 +7,9 @@ * according to those terms. */ #![allow(dead_code)] + +use std::thread::{Builder, JoinHandle}; + #[cfg(all(feature = "std", feature = "async"))] pub mod custom_write_all; pub mod entropy; @@ -15,3 +18,12 @@ pub mod keys; #[cfg(sys_std_component = "net")] pub mod net; pub mod rand; + +pub fn thread_spawn_named(name: S, f: F) -> JoinHandle +where + F: FnOnce() -> T + Send + 'static, + T: Send + 'static, + S: Into, +{ + Builder::new().name(name.into()).spawn(f).unwrap() +}