Skip to content

Commit

Permalink
tests: return bare configs from make_configs()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cpu committed Jul 13, 2024
1 parent 5222475 commit c5726b7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/common/test_stream.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 6 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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!");
Expand Down Expand Up @@ -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");

Expand Down
5 changes: 2 additions & 3 deletions tests/utils.rs
Original file line number Diff line number Diff line change
@@ -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<ServerConfig>, Arc<ClientConfig>) {
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");
Expand All @@ -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)]
Expand Down

0 comments on commit c5726b7

Please sign in to comment.