From 16d8970d46d52de41dc4cbd860f56a1bf7a5de9a Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Fri, 12 Jul 2024 09:08:41 -0400 Subject: [PATCH] tests: tidy up test server usage * Remove the `CHAIN` const and tuple from `TEST_SERVER` - this is now encapsulated in the `ClientConfig` that's returned from `make_configs()` and no tests are constructing a config from scratch. Similarly the domain name is always `"foobar.com"` (this is baked into the vendored end-entity certificate). Let's just use a const for that. * Remove `start_server()` - it's too small to be of much utility. Let's just ref the `lazy_static!` directly. --- tests/test.rs | 21 +++++---------------- tests/utils.rs | 3 +++ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/tests/test.rs b/tests/test.rs index 1da4879..53013dc 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -14,10 +14,8 @@ use tokio::sync::oneshot; use tokio::{runtime, time}; use tokio_rustls::{LazyConfigAcceptor, TlsAcceptor, TlsConnector}; -const CHAIN: &[u8] = include_bytes!("certs/end.chain"); - lazy_static! { - static ref TEST_SERVER: (SocketAddr, &'static str, &'static [u8]) = { + static ref TEST_SERVER: SocketAddr = { let (config, _) = utils::make_configs(); let acceptor = TlsAcceptor::from(Arc::new(config)); @@ -59,15 +57,10 @@ lazy_static! { runtime.block_on(done); }); - let addr = recv.recv().unwrap(); - (addr, "foobar.com", CHAIN) + recv.recv().unwrap() }; } -fn start_server() -> &'static (SocketAddr, &'static str, &'static [u8]) { - &TEST_SERVER -} - async fn start_client(addr: SocketAddr, domain: &str, config: Arc) -> io::Result<()> { const FILE: &[u8] = include_bytes!("../README.md"); @@ -88,8 +81,6 @@ async fn start_client(addr: SocketAddr, domain: &str, config: Arc) #[tokio::test] async fn pass() -> io::Result<()> { - let (addr, domain, _) = start_server(); - // TODO: not sure how to resolve this right now but since // TcpStream::bind now returns a future it creates a race // condition until its ready sometimes. @@ -99,20 +90,18 @@ async fn pass() -> io::Result<()> { let (_, config) = utils::make_configs(); let config = Arc::new(config); - start_client(*addr, domain, config).await?; + start_client(*TEST_SERVER, utils::TEST_SERVER_DOMAIN, config).await?; Ok(()) } #[tokio::test] async fn fail() -> io::Result<()> { - let (addr, domain, _) = start_server(); - let (_, config) = utils::make_configs(); let config = Arc::new(config); - assert_ne!(domain, &"google.com"); - let ret = start_client(*addr, "google.com", config).await; + assert_ne!(utils::TEST_SERVER_DOMAIN, "google.com"); + let ret = start_client(*TEST_SERVER, "google.com", config).await; assert!(ret.is_err()); Ok(()) diff --git a/tests/utils.rs b/tests/utils.rs index 3262cc4..06f3f82 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -60,4 +60,7 @@ mod utils { Ok(()) } + + #[allow(dead_code)] + pub const TEST_SERVER_DOMAIN: &str = "foobar.com"; }