Skip to content

Commit 5690851

Browse files
committed
tests: convert more tests to utils::make_configs()
There's still some improvements left to be made, but this reduces a great deal of duplication in the test code.
1 parent c5726b7 commit 5690851

File tree

2 files changed

+13
-58
lines changed

2 files changed

+13
-58
lines changed

tests/early-data.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#![cfg(feature = "early-data")]
22

3-
use std::io::{self, BufReader, Cursor, Read, Write};
3+
use std::io::{self, Read, Write};
44
use std::net::{SocketAddr, TcpListener};
55
use std::pin::Pin;
66
use std::sync::Arc;
77
use std::task::{Context, Poll};
88
use std::thread;
99

1010
use futures_util::{future::Future, ready};
11-
use rustls::{self, ClientConfig, RootCertStore, ServerConfig, ServerConnection, Stream};
11+
use rustls::{self, ClientConfig, ServerConnection, Stream};
1212
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt, ReadBuf};
1313
use tokio::net::TcpStream;
1414
use tokio_rustls::{client::TlsStream, TlsConnector};
@@ -65,14 +65,7 @@ async fn test_0rtt_vectored() -> io::Result<()> {
6565
}
6666

6767
async fn test_0rtt_impl(vectored: bool) -> io::Result<()> {
68-
let cert_chain = rustls_pemfile::certs(&mut Cursor::new(include_bytes!("certs/end.cert")))
69-
.collect::<io::Result<Vec<_>>>()?;
70-
let key_der =
71-
rustls_pemfile::private_key(&mut Cursor::new(include_bytes!("certs/end.rsa")))?.unwrap();
72-
let mut server = ServerConfig::builder()
73-
.with_no_client_auth()
74-
.with_single_cert(cert_chain, key_der)
75-
.unwrap();
68+
let (mut server, mut client) = utils::make_configs();
7669
server.max_early_data_size = 8192;
7770
let server = Arc::new(server);
7871

@@ -109,25 +102,15 @@ async fn test_0rtt_impl(vectored: bool) -> io::Result<()> {
109102
});
110103
});
111104

112-
let mut chain = BufReader::new(Cursor::new(include_str!("certs/end.chain")));
113-
let mut root_store = RootCertStore::empty();
114-
for cert in rustls_pemfile::certs(&mut chain) {
115-
root_store.add(cert.unwrap()).unwrap();
116-
}
117-
118-
let mut config =
119-
rustls::ClientConfig::builder_with_protocol_versions(&[&rustls::version::TLS13])
120-
.with_root_certificates(root_store)
121-
.with_no_client_auth();
122-
config.enable_early_data = true;
123-
let config = Arc::new(config);
105+
client.enable_early_data = true;
106+
let client = Arc::new(client);
124107
let addr = SocketAddr::from(([127, 0, 0, 1], server_port));
125108

126-
let (io, buf) = send(config.clone(), addr, b"hello", vectored).await?;
109+
let (io, buf) = send(client.clone(), addr, b"hello", vectored).await?;
127110
assert!(!io.get_ref().1.is_early_data_accepted());
128111
assert_eq!("LATE:hello", String::from_utf8_lossy(&buf));
129112

130-
let (io, buf) = send(config, addr, b"world!", vectored).await?;
113+
let (io, buf) = send(client, addr, b"world!", vectored).await?;
131114
assert!(io.get_ref().1.is_early_data_accepted());
132115
assert_eq!("EARLY:world!LATE:", String::from_utf8_lossy(&buf));
133116

tests/test.rs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{BufReader, Cursor, ErrorKind};
1+
use std::io::{Cursor, ErrorKind};
22
use std::net::SocketAddr;
33
use std::sync::mpsc::channel;
44
use std::sync::Arc;
@@ -8,31 +8,17 @@ use std::{io, thread};
88
use futures_util::future::TryFutureExt;
99
use lazy_static::lazy_static;
1010
use rustls::ClientConfig;
11-
use rustls_pemfile::{certs, rsa_private_keys};
1211
use tokio::io::{copy, split, AsyncReadExt, AsyncWriteExt};
1312
use tokio::net::{TcpListener, TcpStream};
1413
use tokio::sync::oneshot;
1514
use tokio::{runtime, time};
1615
use tokio_rustls::{LazyConfigAcceptor, TlsAcceptor, TlsConnector};
1716

18-
const CERT: &str = include_str!("certs/end.cert");
1917
const CHAIN: &[u8] = include_bytes!("certs/end.chain");
20-
const RSA: &str = include_str!("certs/end.rsa");
2118

2219
lazy_static! {
2320
static ref TEST_SERVER: (SocketAddr, &'static str, &'static [u8]) = {
24-
let cert = certs(&mut BufReader::new(Cursor::new(CERT)))
25-
.map(|result| result.unwrap())
26-
.collect();
27-
let key = rsa_private_keys(&mut BufReader::new(Cursor::new(RSA)))
28-
.next()
29-
.unwrap()
30-
.unwrap();
31-
32-
let config = rustls::ServerConfig::builder()
33-
.with_no_client_auth()
34-
.with_single_cert(cert, key.into())
35-
.unwrap();
21+
let (config, _) = utils::make_configs();
3622
let acceptor = TlsAcceptor::from(Arc::new(config));
3723

3824
let (send, recv) = channel();
@@ -102,22 +88,15 @@ async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>)
10288

10389
#[tokio::test]
10490
async fn pass() -> io::Result<()> {
105-
let (addr, domain, chain) = start_server();
91+
let (addr, domain, _) = start_server();
10692

10793
// TODO: not sure how to resolve this right now but since
10894
// TcpStream::bind now returns a future it creates a race
10995
// condition until its ready sometimes.
11096
use std::time::*;
11197
tokio::time::sleep(Duration::from_secs(1)).await;
11298

113-
let mut root_store = rustls::RootCertStore::empty();
114-
for cert in certs(&mut std::io::Cursor::new(*chain)) {
115-
root_store.add(cert.unwrap()).unwrap();
116-
}
117-
118-
let config = rustls::ClientConfig::builder()
119-
.with_root_certificates(root_store)
120-
.with_no_client_auth();
99+
let (_, config) = utils::make_configs();
121100
let config = Arc::new(config);
122101

123102
start_client(*addr, domain, config).await?;
@@ -127,16 +106,9 @@ async fn pass() -> io::Result<()> {
127106

128107
#[tokio::test]
129108
async fn fail() -> io::Result<()> {
130-
let (addr, domain, chain) = start_server();
131-
132-
let mut root_store = rustls::RootCertStore::empty();
133-
for cert in certs(&mut std::io::Cursor::new(*chain)) {
134-
root_store.add(cert.unwrap()).unwrap();
135-
}
109+
let (addr, domain, _) = start_server();
136110

137-
let config = rustls::ClientConfig::builder()
138-
.with_root_certificates(root_store)
139-
.with_no_client_auth();
111+
let (_, config) = utils::make_configs();
140112
let config = Arc::new(config);
141113

142114
assert_ne!(domain, &"google.com");

0 commit comments

Comments
 (0)