tokio-rustls/tests/test.rs

126 lines
4.1 KiB
Rust
Raw Normal View History

2017-08-13 10:19:17 +00:00
use std::{ io, thread };
use std::io::{ BufReader, Cursor };
use std::sync::Arc;
use std::sync::mpsc::channel;
2018-08-16 12:44:37 +00:00
use std::net::SocketAddr;
2019-05-19 16:28:27 +00:00
use lazy_static::lazy_static;
2019-08-10 15:43:19 +00:00
use tokio::prelude::*;
use tokio::runtime::current_thread;
use tokio::net::{ TcpListener, TcpStream };
2019-10-01 15:00:49 +00:00
use tokio::io::split;
2019-08-10 15:43:19 +00:00
use futures_util::try_future::TryFutureExt;
2018-08-16 12:44:37 +00:00
use rustls::{ ServerConfig, ClientConfig };
2017-08-13 10:19:17 +00:00
use rustls::internal::pemfile::{ certs, rsa_private_keys };
2018-08-09 02:46:28 +00:00
use tokio_rustls::{ TlsConnector, TlsAcceptor };
2017-08-13 10:19:17 +00:00
const CERT: &str = include_str!("end.cert");
const CHAIN: &str = include_str!("end.chain");
const RSA: &str = include_str!("end.rsa");
2018-08-16 12:44:37 +00:00
lazy_static!{
static ref TEST_SERVER: (SocketAddr, &'static str, &'static str) = {
let cert = certs(&mut BufReader::new(Cursor::new(CERT))).unwrap();
let mut keys = rsa_private_keys(&mut BufReader::new(Cursor::new(RSA))).unwrap();
2018-03-22 11:47:27 +00:00
2018-08-16 12:44:37 +00:00
let mut config = ServerConfig::new(rustls::NoClientAuth::new());
config.set_single_cert(cert, keys.pop().unwrap())
.expect("invalid key or certificate");
2019-05-21 16:54:10 +00:00
let acceptor = TlsAcceptor::from(Arc::new(config));
2017-08-13 10:19:17 +00:00
2018-08-16 12:44:37 +00:00
let (send, recv) = channel();
2017-08-13 10:19:17 +00:00
2018-08-16 12:44:37 +00:00
thread::spawn(move || {
2019-08-10 15:43:19 +00:00
let mut runtime = current_thread::Runtime::new().unwrap();
let handle = runtime.handle();
let done = async move {
2019-05-19 16:28:27 +00:00
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
2019-09-01 05:29:07 +00:00
let listener = TcpListener::bind(&addr).await?;
2019-05-19 16:28:27 +00:00
send.send(listener.local_addr()?).unwrap();
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
2019-05-21 16:54:10 +00:00
let acceptor = acceptor.clone();
2019-08-10 15:43:19 +00:00
let fut = async move {
2019-10-01 15:00:49 +00:00
let stream = acceptor.accept(stream?).await?;
2019-08-10 15:43:19 +00:00
2019-10-01 15:00:49 +00:00
let (mut reader, mut writer) = split(stream);
reader.copy(&mut writer).await?;
2019-08-10 15:43:19 +00:00
Ok(()) as io::Result<()>
}.unwrap_or_else(|err| eprintln!("server: {:?}", err));
2019-08-10 15:43:19 +00:00
handle.spawn(fut).unwrap();
2019-05-19 16:28:27 +00:00
}
Ok(()) as io::Result<()>
}.unwrap_or_else(|err| eprintln!("server: {:?}", err));
2019-05-19 16:28:27 +00:00
runtime.block_on(done);
2018-08-16 12:44:37 +00:00
});
2017-08-13 10:19:17 +00:00
2018-08-16 12:44:37 +00:00
let addr = recv.recv().unwrap();
2019-10-01 15:00:49 +00:00
(addr, "testserver.com", CHAIN)
2018-08-16 12:44:37 +00:00
};
2017-08-13 10:19:17 +00:00
}
2018-08-16 12:44:37 +00:00
fn start_server() -> &'static (SocketAddr, &'static str, &'static str) {
&*TEST_SERVER
}
2019-05-19 16:28:27 +00:00
async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>) -> io::Result<()> {
2018-08-16 12:44:37 +00:00
const FILE: &'static [u8] = include_bytes!("../README.md");
let domain = webpki::DNSNameRef::try_from_ascii_str(domain).unwrap();
2019-02-18 12:01:37 +00:00
let config = TlsConnector::from(config);
2019-05-19 16:28:27 +00:00
let mut buf = vec![0; FILE.len()];
let stream = TcpStream::connect(&addr).await?;
let mut stream = config.connect(domain, stream).await?;
stream.write_all(FILE).await?;
stream.flush().await?;
2019-05-19 16:28:27 +00:00
stream.read_exact(&mut buf).await?;
assert_eq!(buf, FILE);
2017-08-13 10:19:17 +00:00
2019-05-19 16:28:27 +00:00
Ok(())
2017-08-13 10:19:17 +00:00
}
2019-08-10 15:43:19 +00:00
#[tokio::test]
async fn pass() -> io::Result<()> {
2018-08-16 12:44:37 +00:00
let (addr, domain, chain) = start_server();
2017-08-13 10:19:17 +00:00
2019-09-01 05:29:07 +00:00
// 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.
use std::time::*;
let deadline = Instant::now() + Duration::from_secs(1);
tokio::timer::delay(deadline);
2019-02-18 12:01:37 +00:00
let mut config = ClientConfig::new();
let mut chain = BufReader::new(Cursor::new(chain));
config.root_store.add_pem_file(&mut chain).unwrap();
let config = Arc::new(config);
2019-08-10 15:43:19 +00:00
start_client(addr.clone(), domain, config.clone()).await?;
Ok(())
2018-03-22 11:47:27 +00:00
}
2019-08-10 15:43:19 +00:00
#[tokio::test]
async fn fail() -> io::Result<()> {
2018-08-16 12:44:37 +00:00
let (addr, domain, chain) = start_server();
2017-08-13 10:19:17 +00:00
2019-02-18 12:01:37 +00:00
let mut config = ClientConfig::new();
let mut chain = BufReader::new(Cursor::new(chain));
config.root_store.add_pem_file(&mut chain).unwrap();
let config = Arc::new(config);
2018-08-16 12:44:37 +00:00
assert_ne!(domain, &"google.com");
2019-08-10 15:43:19 +00:00
let ret = start_client(addr.clone(), "google.com", config).await;
assert!(ret.is_err());
Ok(())
2017-08-13 10:19:17 +00:00
}