2019-10-01 15:00:49 +00:00
|
|
|
#![cfg(feature = "early-data")]
|
|
|
|
|
2020-02-24 19:42:22 +00:00
|
|
|
use std::io::{ self, BufRead, BufReader, Cursor };
|
2019-10-01 15:00:49 +00:00
|
|
|
use std::process::{ Command, Child, Stdio };
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::marker::Unpin;
|
|
|
|
use std::pin::{ Pin };
|
|
|
|
use std::task::{ Context, Poll };
|
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::prelude::*;
|
|
|
|
use tokio::net::TcpStream;
|
2019-11-26 22:57:32 +00:00
|
|
|
use tokio::time::delay_for;
|
2019-10-01 15:00:49 +00:00
|
|
|
use futures_util::{ future, ready };
|
|
|
|
use rustls::ClientConfig;
|
|
|
|
use tokio_rustls::{ TlsConnector, client::TlsStream };
|
2019-11-26 22:57:32 +00:00
|
|
|
use std::future::Future;
|
2019-10-01 15:00:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct Read1<T>(T);
|
|
|
|
|
|
|
|
impl<T: AsyncRead + Unpin> Future for Read1<T> {
|
|
|
|
type Output = io::Result<()>;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let mut buf = [0];
|
|
|
|
ready!(Pin::new(&mut self.0).poll_read(cx, &mut buf))?;
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn send(config: Arc<ClientConfig>, addr: SocketAddr, data: &[u8])
|
|
|
|
-> io::Result<TlsStream<TcpStream>>
|
|
|
|
{
|
|
|
|
let connector = TlsConnector::from(config)
|
|
|
|
.early_data(true);
|
|
|
|
let stream = TcpStream::connect(&addr).await?;
|
|
|
|
let domain = webpki::DNSNameRef::try_from_ascii_str("testserver.com").unwrap();
|
|
|
|
|
|
|
|
let mut stream = connector.connect(domain, stream).await?;
|
|
|
|
stream.write_all(data).await?;
|
|
|
|
stream.flush().await?;
|
|
|
|
|
2019-10-10 17:01:27 +00:00
|
|
|
// sleep 1s
|
2019-10-10 14:52:31 +00:00
|
|
|
//
|
|
|
|
// see https://www.mail-archive.com/openssl-users@openssl.org/msg84451.html
|
2019-10-10 17:01:27 +00:00
|
|
|
let sleep1 = delay_for(Duration::from_secs(1));
|
|
|
|
let mut stream = match future::select(Read1(stream), sleep1).await {
|
2019-10-10 14:52:31 +00:00
|
|
|
future::Either::Right((_, Read1(stream))) => stream,
|
|
|
|
future::Either::Left((Err(err), _)) => return Err(err),
|
|
|
|
future::Either::Left((Ok(_), _)) => unreachable!(),
|
2019-10-01 15:00:49 +00:00
|
|
|
};
|
|
|
|
|
2019-10-10 14:52:31 +00:00
|
|
|
stream.shutdown().await?;
|
2019-10-01 15:00:49 +00:00
|
|
|
|
|
|
|
Ok(stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DropKill(Child);
|
|
|
|
|
|
|
|
impl Drop for DropKill {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.0.kill().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_0rtt() -> io::Result<()> {
|
|
|
|
let mut handle = Command::new("openssl")
|
|
|
|
.arg("s_server")
|
|
|
|
.arg("-early_data")
|
|
|
|
.arg("-tls1_3")
|
|
|
|
.args(&["-cert", "./tests/end.cert"])
|
|
|
|
.args(&["-key", "./tests/end.rsa"])
|
|
|
|
.args(&["-port", "12354"])
|
2020-02-24 19:42:22 +00:00
|
|
|
.stdin(Stdio::piped())
|
2019-10-01 15:00:49 +00:00
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.map(DropKill)?;
|
|
|
|
|
|
|
|
// wait openssl server
|
2019-10-10 17:01:27 +00:00
|
|
|
delay_for(Duration::from_secs(1)).await;
|
2019-10-01 15:00:49 +00:00
|
|
|
|
|
|
|
let mut config = ClientConfig::new();
|
|
|
|
let mut chain = BufReader::new(Cursor::new(include_str!("end.chain")));
|
|
|
|
config.root_store.add_pem_file(&mut chain).unwrap();
|
|
|
|
config.versions = vec![rustls::ProtocolVersion::TLSv1_3];
|
|
|
|
config.enable_early_data = true;
|
|
|
|
let config = Arc::new(config);
|
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 12354));
|
|
|
|
|
|
|
|
let io = send(config.clone(), addr, b"hello").await?;
|
|
|
|
assert!(!io.get_ref().1.is_early_data_accepted());
|
|
|
|
|
|
|
|
let io = send(config, addr, b"world!").await?;
|
|
|
|
assert!(io.get_ref().1.is_early_data_accepted());
|
|
|
|
|
|
|
|
let stdout = handle.0.stdout.as_mut().unwrap();
|
|
|
|
let mut lines = BufReader::new(stdout).lines();
|
|
|
|
|
2020-01-11 17:06:15 +00:00
|
|
|
let has_msg1 = lines.by_ref()
|
|
|
|
.any(|line| line.unwrap().contains("hello"));
|
|
|
|
let has_msg2 = lines.by_ref()
|
|
|
|
.any(|line| line.unwrap().contains("world!"));
|
2019-10-01 15:00:49 +00:00
|
|
|
|
2020-01-11 17:06:15 +00:00
|
|
|
assert!(has_msg1 && has_msg2);
|
2019-10-10 17:24:27 +00:00
|
|
|
|
2019-10-01 15:00:49 +00:00
|
|
|
Ok(())
|
|
|
|
}
|