Skip to content

Commit

Permalink
Set thread names in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jethro Beekman committed Jan 9, 2025
1 parent 2a962b0 commit 272f4ef
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
26 changes: 13 additions & 13 deletions mbedtls/tests/client_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ where

#[cfg(unix)]
mod test {
use std::thread;
use crate::support::thread_spawn_named;

#[test]
fn client_server_test() {
Expand Down Expand Up @@ -348,17 +348,17 @@ 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();

// 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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
16 changes: 10 additions & 6 deletions mbedtls/tests/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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();
Expand Down
22 changes: 11 additions & 11 deletions mbedtls/tests/ssl_conf_ca_cb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Certificate>) -> TlsResult<MbedtlsList<Certificate>> {
Expand All @@ -74,8 +74,8 @@ mod test {
let ca_callback = |_: &MbedtlsList<Certificate>| -> TlsResult<MbedtlsList<Certificate>> {
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();
}
Expand All @@ -85,20 +85,20 @@ mod test {
let (c, s) = create_tcp_pair().unwrap();
let ca_callback =
|_: &MbedtlsList<Certificate>| -> TlsResult<MbedtlsList<Certificate>> { Ok(MbedtlsList::<Certificate>::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();
}

#[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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
10 changes: 5 additions & 5 deletions mbedtls/tests/ssl_conf_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
12 changes: 12 additions & 0 deletions mbedtls/tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,3 +18,12 @@ pub mod keys;
#[cfg(sys_std_component = "net")]
pub mod net;
pub mod rand;

pub fn thread_spawn_named<F, T, S>(name: S, f: F) -> JoinHandle<T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
S: Into<String>,
{
Builder::new().name(name.into()).spawn(f).unwrap()
}

0 comments on commit 272f4ef

Please sign in to comment.