tokio-rustls/src/test_0rtt.rs

49 lines
1.5 KiB
Rust
Raw Normal View History

2019-02-18 12:01:37 +00:00
use std::io;
use std::sync::Arc;
use std::net::ToSocketAddrs;
2019-08-10 16:00:49 +00:00
use tokio::prelude::*;
use tokio::net::TcpStream;
2019-02-18 12:41:52 +00:00
use rustls::ClientConfig;
2019-05-18 16:48:56 +00:00
use crate::{ TlsConnector, client::TlsStream };
2019-02-18 12:01:37 +00:00
2019-05-18 16:48:56 +00:00
async fn get(config: Arc<ClientConfig>, domain: &str, rtt0: bool)
2019-02-18 12:41:52 +00:00
-> io::Result<(TlsStream<TcpStream>, String)>
2019-02-18 12:01:37 +00:00
{
2019-05-18 16:48:56 +00:00
let connector = TlsConnector::from(config).early_data(rtt0);
2019-02-18 12:01:37 +00:00
let input = format!("GET / HTTP/1.0\r\nHost: {}\r\n\r\n", domain);
let addr = (domain, 443)
.to_socket_addrs()?
.next().unwrap();
2019-05-18 16:48:56 +00:00
let domain = webpki::DNSNameRef::try_from_ascii_str(&domain).unwrap();
let mut buf = Vec::new();
let stream = TcpStream::connect(&addr).await?;
let mut stream = connector.connect(domain, stream).await?;
stream.write_all(input.as_bytes()).await?;
stream.flush().await?;
2019-05-18 16:48:56 +00:00
stream.read_to_end(&mut buf).await?;
2019-02-18 12:01:37 +00:00
2019-05-18 16:48:56 +00:00
Ok((stream, String::from_utf8(buf).unwrap()))
2019-02-18 12:01:37 +00:00
}
2019-08-10 16:00:49 +00:00
#[tokio::test]
async fn test_0rtt() -> io::Result<()> {
2019-02-18 12:01:37 +00:00
let mut config = ClientConfig::new();
config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
config.enable_early_data = true;
let config = Arc::new(config);
let domain = "mozilla-modern.badssl.com";
2019-08-10 16:00:49 +00:00
let (_, output) = get(config.clone(), domain, false).await?;
2019-02-18 12:01:37 +00:00
assert!(output.contains("<title>mozilla-modern.badssl.com</title>"));
2019-08-10 16:00:49 +00:00
let (io, output) = get(config.clone(), domain, true).await?;
2019-02-18 12:01:37 +00:00
assert!(output.contains("<title>mozilla-modern.badssl.com</title>"));
assert_eq!(io.early_data.0, 0);
2019-08-10 16:00:49 +00:00
Ok(())
2019-02-18 12:01:37 +00:00
}