From c5726b7ef6aaa30fab31203cf45de3e740d275f8 Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Fri, 12 Jul 2024 08:56:52 -0400 Subject: [PATCH] tests: return bare configs from make_configs() Let the callers put the configs into an `Arc`. This will allow re-using the setup logic from `utils::make_configs()` in contexts where customization of the client or server config is required. --- src/common/test_stream.rs | 5 +++-- tests/test.rs | 9 ++++++--- tests/utils.rs | 5 ++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/common/test_stream.rs b/src/common/test_stream.rs index 9b9264ad..639a7ec0 100644 --- a/src/common/test_stream.rs +++ b/src/common/test_stream.rs @@ -1,5 +1,6 @@ use std::io::{self, Cursor, Read, Write}; use std::pin::Pin; +use std::sync::Arc; use std::task::{Context, Poll}; use futures_util::future::poll_fn; @@ -291,10 +292,10 @@ async fn stream_eof() -> io::Result<()> { fn make_pair() -> (ServerConnection, ClientConnection) { let (sconfig, cconfig) = utils::make_configs(); - let server = ServerConnection::new(sconfig).unwrap(); + let server = ServerConnection::new(Arc::new(sconfig)).unwrap(); let domain = pki_types::ServerName::try_from("foobar.com").unwrap(); - let client = ClientConnection::new(cconfig, domain).unwrap(); + let client = ClientConnection::new(Arc::new(cconfig), domain).unwrap(); (server, client) } diff --git a/tests/test.rs b/tests/test.rs index b1b97635..8cd3904d 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -155,7 +155,7 @@ async fn test_lazy_config_acceptor() -> io::Result<()> { .unwrap() .to_owned(); tokio::spawn(async move { - let connector = crate::TlsConnector::from(cconfig); + let connector = crate::TlsConnector::from(Arc::new(cconfig)); let mut client = connector.connect(domain, cstream).await.unwrap(); client.write_all(b"hello, world!").await.unwrap(); @@ -175,7 +175,7 @@ async fn test_lazy_config_acceptor() -> io::Result<()> { Vec::<&[u8]>::new() ); - let mut stream = start.into_stream(sconfig).await.unwrap(); + let mut stream = start.into_stream(Arc::new(sconfig)).await.unwrap(); let mut buf = [0; 13]; stream.read_exact(&mut buf).await.unwrap(); assert_eq!(&buf[..], b"hello, world!"); @@ -279,7 +279,10 @@ async fn acceptor_alert() { panic!("timeout"); }; - let err = start_handshake.into_stream(sconfig).await.unwrap_err(); + let err = start_handshake + .into_stream(Arc::new(sconfig)) + .await + .unwrap_err(); assert_eq!(err.to_string(), "peer is incompatible: Tls12NotOffered"); diff --git a/tests/utils.rs b/tests/utils.rs index 9e489bd9..3262cc41 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -1,13 +1,12 @@ mod utils { use std::io::{BufReader, Cursor, IoSlice}; - use std::sync::Arc; use rustls::{ClientConfig, RootCertStore, ServerConfig}; use rustls_pemfile::{certs, rsa_private_keys}; use tokio::io::{self, AsyncWrite, AsyncWriteExt}; #[allow(dead_code)] - pub fn make_configs() -> (Arc, Arc) { + pub fn make_configs() -> (ServerConfig, ClientConfig) { const CERT: &str = include_str!("certs/end.cert"); const CHAIN: &str = include_str!("certs/end.chain"); const RSA: &str = include_str!("certs/end.rsa"); @@ -34,7 +33,7 @@ mod utils { .with_root_certificates(client_root_cert_store) .with_no_client_auth(); - (Arc::new(sconfig), Arc::new(cconfig)) + (sconfig, cconfig) } #[allow(dead_code)]