| •1 |
#![cfg(any())] |
| 2 |
use crate::{client::TlsStream, TlsConnector}; |
| 3 |
use async_std::net::TcpStream; |
| 4 |
use async_std::sync::Arc; |
| 5 |
use futures_executor::block_on; |
| 6 |
use futures_util::io::{AsyncReadExt, AsyncWriteExt}; |
| 7 |
use rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore}; |
| 8 |
use std::io; |
| 9 |
use std::net::ToSocketAddrs; |
| 10 |
|
| 11 |
async fn get( |
| 12 |
config: Arc<ClientConfig>, |
| 13 |
domain: &str, |
| 14 |
rtt0: bool, |
| 15 |
) -> io::Result<(TlsStream<TcpStream>, String)> { |
| 16 |
let connector = TlsConnector::from(config).early_data(rtt0); |
| 17 |
let input = format!("GET / HTTP/1.0\r\nHost: {}\r\n\r\n", domain); |
| 18 |
|
| 19 |
let addr = (domain, 443).to_socket_addrs()?.next().unwrap(); |
| 20 |
let mut buf = Vec::new(); |
| 21 |
|
| 22 |
let stream = TcpStream::connect(&addr).await?; |
| 23 |
let mut stream = connector.connect(domain, stream).await?; |
| 24 |
stream.write_all(input.as_bytes()).await?; |
| 25 |
stream.read_to_end(&mut buf).await?; |
| 26 |
|
| 27 |
Ok((stream, String::from_utf8(buf).unwrap())) |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_0rtt() { |
| 32 |
let mut root_certs = RootCertStore::empty(); |
| 33 |
root_certs.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| { |
| 34 |
OwnedTrustAnchor::from_subject_spki_name_constraints( |
| 35 |
ta.subject, |
| 36 |
ta.spki, |
| 37 |
ta.name_constraints, |
| 38 |
) |
| 39 |
})); |
| 40 |
let mut config = ClientConfig::builder() |
| 41 |
.with_safe_defaults() |
| 42 |
.with_root_certificates(root_certs) |
| 43 |
.with_no_client_auth(); |
| 44 |
|
| 45 |
config.enable_early_data = true; |
| 46 |
let config = Arc::new(config); |
| 47 |
let domain = "mozilla-modern.badssl.com"; |
| 48 |
|
| 49 |
let (_, output) = block_on(get(config.clone(), domain, false)).unwrap(); |
| 50 |
assert!(output.contains("<title>mozilla-modern.badssl.com</title>")); |
| 51 |
|
| 52 |
let (io, output) = block_on(get(config.clone(), domain, true)).unwrap(); |
| 53 |
assert!(output.contains("<title>mozilla-modern.badssl.com</title>")); |
| 54 |
|
| 55 |
assert_eq!(io.early_data.0, 0); |
| 56 |
} |