Skip to content

Commit

Permalink
tests: tidy up test server usage
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
cpu committed Jul 13, 2024
1 parent 5690851 commit 16d8970
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
21 changes: 5 additions & 16 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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<ClientConfig>) -> io::Result<()> {
const FILE: &[u8] = include_bytes!("../README.md");

Expand All @@ -88,8 +81,6 @@ async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>)

#[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.
Expand All @@ -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(())
Expand Down
3 changes: 3 additions & 0 deletions tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ mod utils {

Ok(())
}

#[allow(dead_code)]
pub const TEST_SERVER_DOMAIN: &str = "foobar.com";
}

0 comments on commit 16d8970

Please sign in to comment.