make early data test work

This commit is contained in:
quininer 2019-05-19 00:48:56 +08:00
parent 4d673f9a72
commit f7472e89a2
6 changed files with 33 additions and 34 deletions

View File

@ -25,6 +25,7 @@ webpki = "0.19"
early-data = [] early-data = []
[dev-dependencies] [dev-dependencies]
romio = "0.3.0-alpha.8"
tokio = "0.1.6" tokio = "0.1.6"
lazy_static = "1" lazy_static = "1"
webpki-roots = "0.16" webpki-roots = "0.16"

View File

@ -1,6 +1,5 @@
use super::*; use super::*;
use rustls::Session; use rustls::Session;
use std::io::Write;
/// A wrapper around an underlying raw stream which implements the TLS or SSL /// A wrapper around an underlying raw stream which implements the TLS or SSL
/// protocol. /// protocol.
@ -149,6 +148,8 @@ where
match this.state { match this.state {
#[cfg(feature = "early-data")] #[cfg(feature = "early-data")]
TlsState::EarlyData => { TlsState::EarlyData => {
use std::io::Write;
let (pos, data) = &mut this.early_data; let (pos, data) = &mut this.early_data;
// write early data // write early data

View File

@ -225,5 +225,5 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> Stream<'a, IO, S> {
} }
} }
#[cfg(test)] // #[cfg(test)]
mod test_stream; // mod test_stream;

View File

@ -1,5 +1,7 @@
//! Asynchronous TLS/SSL streams for Tokio using [Rustls](https://github.com/ctz/rustls). //! Asynchronous TLS/SSL streams for Tokio using [Rustls](https://github.com/ctz/rustls).
#![feature(async_await)]
macro_rules! try_ready { macro_rules! try_ready {
( $e:expr ) => { ( $e:expr ) => {
match $e { match $e {
@ -10,19 +12,19 @@ macro_rules! try_ready {
} }
} }
pub mod client;
mod common; mod common;
pub mod client;
pub mod server; pub mod server;
use common::Stream; use std::{ io, mem };
use std::pin::Pin;
use std::task::{ Poll, Context };
use std::future::Future;
use futures::io::{ AsyncRead, AsyncWrite, Initializer };
use rustls::{ClientConfig, ClientSession, ServerConfig, ServerSession};
use std::sync::Arc; use std::sync::Arc;
use std::{io, mem}; use std::pin::Pin;
use std::future::Future;
use std::task::{ Poll, Context };
use futures::io::{ AsyncRead, AsyncWrite, Initializer };
use rustls::{ ClientConfig, ClientSession, ServerConfig, ServerSession };
use webpki::DNSNameRef; use webpki::DNSNameRef;
use common::Stream;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
enum TlsState { enum TlsState {
@ -200,8 +202,6 @@ impl<IO: AsyncRead + AsyncWrite + Unpin> Future for Accept<IO> {
} }
} }
/*
#[cfg(feature = "early-data")] #[cfg(feature = "early-data")]
#[cfg(test)] #[cfg(test)]
mod test_0rtt; mod test_0rtt;
*/

View File

@ -1,36 +1,31 @@
extern crate tokio;
extern crate webpki;
extern crate webpki_roots;
use std::io; use std::io;
use std::sync::Arc; use std::sync::Arc;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use self::tokio::io as aio; use futures::executor;
use self::tokio::prelude::*; use futures::prelude::*;
use self::tokio::net::TcpStream; use romio::tcp::TcpStream;
use rustls::ClientConfig; use rustls::ClientConfig;
use ::{ TlsConnector, client::TlsStream }; use crate::{ TlsConnector, client::TlsStream };
fn get(config: Arc<ClientConfig>, domain: &str, rtt0: bool) async fn get(config: Arc<ClientConfig>, domain: &str, rtt0: bool)
-> io::Result<(TlsStream<TcpStream>, String)> -> io::Result<(TlsStream<TcpStream>, String)>
{ {
let config = TlsConnector::from(config).early_data(rtt0); let connector = TlsConnector::from(config).early_data(rtt0);
let input = format!("GET / HTTP/1.0\r\nHost: {}\r\n\r\n", domain); let input = format!("GET / HTTP/1.0\r\nHost: {}\r\n\r\n", domain);
let addr = (domain, 443) let addr = (domain, 443)
.to_socket_addrs()? .to_socket_addrs()?
.next().unwrap(); .next().unwrap();
TcpStream::connect(&addr)
.and_then(move |stream| {
let domain = webpki::DNSNameRef::try_from_ascii_str(&domain).unwrap(); let domain = webpki::DNSNameRef::try_from_ascii_str(&domain).unwrap();
config.connect(domain, stream) let mut buf = Vec::new();
})
.and_then(move |stream| aio::write_all(stream, input)) let stream = TcpStream::connect(&addr).await?;
.and_then(move |(stream, _)| aio::read_to_end(stream, Vec::new())) let mut stream = connector.connect(domain, stream).await?;
.map(|(stream, buf)| (stream, String::from_utf8(buf).unwrap())) stream.write_all(input.as_bytes()).await?;
.wait() stream.read_to_end(&mut buf).await?;
Ok((stream, String::from_utf8(buf).unwrap()))
} }
#[test] #[test]
@ -41,10 +36,10 @@ fn test_0rtt() {
let config = Arc::new(config); let config = Arc::new(config);
let domain = "mozilla-modern.badssl.com"; let domain = "mozilla-modern.badssl.com";
let (_, output) = get(config.clone(), domain, false).unwrap(); let (_, output) = executor::block_on(get(config.clone(), domain, false)).unwrap();
assert!(output.contains("<title>mozilla-modern.badssl.com</title>")); assert!(output.contains("<title>mozilla-modern.badssl.com</title>"));
let (io, output) = get(config.clone(), domain, true).unwrap(); let (io, output) = executor::block_on(get(config.clone(), domain, true)).unwrap();
assert!(output.contains("<title>mozilla-modern.badssl.com</title>")); assert!(output.contains("<title>mozilla-modern.badssl.com</title>"));
assert_eq!(io.early_data.0, 0); assert_eq!(io.early_data.0, 0);

View File

@ -1,3 +1,5 @@
#![cfg(not(test))]
#[macro_use] extern crate lazy_static; #[macro_use] extern crate lazy_static;
extern crate rustls; extern crate rustls;
extern crate tokio; extern crate tokio;