Update some test

This commit is contained in:
quininer 2019-08-10 23:43:19 +08:00
parent 7fca72543c
commit bbc6688292
4 changed files with 61 additions and 50 deletions

View File

@ -15,9 +15,9 @@ matrix:
- rust: stable - rust: stable
script: script:
- cargo test - cargo test --test test
- cargo test --features early-data # - cargo test --features early-data
- cd examples/server # - cd examples/server
- cargo check # - cargo check
- cd ../../examples/client # - cd ../../examples/client
- cargo check # - cargo check

View File

@ -19,13 +19,14 @@ appveyor = { repository = "quininer/tokio-rustls" }
smallvec = "0.6" smallvec = "0.6"
tokio-io = "0.2.0-alpha.1" tokio-io = "0.2.0-alpha.1"
futures-core-preview = "0.3.0-alpha.17" futures-core-preview = "0.3.0-alpha.17"
rustls = "0.15" rustls = "0.16"
webpki = "0.19" webpki = "0.21"
[features] [features]
early-data = [] early-data = []
[dev-dependencies] [dev-dependencies]
tokio = "0.2.0-alpha.1" tokio = "0.2.0-alpha.1"
futures-util-preview = "0.3.0-alpha.17"
lazy_static = "1" lazy_static = "1"
webpki-roots = "0.16" webpki-roots = "0.17"

View File

@ -1,10 +1,8 @@
use std::pin::Pin; use std::pin::Pin;
use std::task::Poll;
use std::sync::Arc; use std::sync::Arc;
use futures::prelude::*; use std::task::{ Poll, Context };
use futures::task::{ Context, noop_waker_ref }; use futures_util::future::poll_fn;
use futures::executor; use tokio_io::{ AsyncRead, AsyncWrite, AsyncReadExt, AsyncWriteExt };
use futures::io::{ AsyncRead, AsyncWrite };
use std::io::{ self, Read, Write, BufReader, Cursor }; use std::io::{ self, Read, Write, BufReader, Cursor };
use webpki::DNSNameRef; use webpki::DNSNameRef;
use rustls::internal::pemfile::{ certs, rsa_private_keys }; use rustls::internal::pemfile::{ certs, rsa_private_keys };
@ -73,7 +71,7 @@ fn stream_good() -> io::Result<()> {
let fut = async { let fut = async {
let (mut server, mut client) = make_pair(); let (mut server, mut client) = make_pair();
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?;
io::copy(&mut Cursor::new(FILE), &mut server)?; io::copy(&mut Cursor::new(FILE), &mut server)?;
{ {
@ -100,18 +98,18 @@ fn stream_good() -> io::Result<()> {
fn stream_bad() -> io::Result<()> { fn stream_bad() -> io::Result<()> {
let fut = async { let fut = async {
let (mut server, mut client) = make_pair(); let (mut server, mut client) = make_pair();
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?;
client.set_buffer_limit(1024); client.set_buffer_limit(1024);
let mut bad = Bad(true); let mut bad = Bad(true);
let mut stream = Stream::new(&mut bad, &mut client); let mut stream = Stream::new(&mut bad, &mut client);
assert_eq!(future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x42; 8])).await?, 8); assert_eq!(poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x42; 8])).await?, 8);
assert_eq!(future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x42; 8])).await?, 8); assert_eq!(poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x42; 8])).await?, 8);
let r = future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x00; 1024])).await?; // fill buffer let r = poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x00; 1024])).await?; // fill buffer
assert!(r < 1024); assert!(r < 1024);
let mut cx = Context::from_waker(noop_waker_ref()); let ret = poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x01]));
assert!(stream.as_mut_pin().poll_write(&mut cx, &[0x01]).is_pending()); assert!(ret.is_pending());
Ok(()) as io::Result<()> Ok(()) as io::Result<()>
}; };
@ -127,12 +125,12 @@ fn stream_handshake() -> io::Result<()> {
{ {
let mut good = Good(&mut server); let mut good = Good(&mut server);
let mut stream = Stream::new(&mut good, &mut client); let mut stream = Stream::new(&mut good, &mut client);
let (r, w) = future::poll_fn(|cx| stream.complete_io(cx)).await?; let (r, w) = poll_fn(|cx| stream.complete_io(cx)).await?;
assert!(r > 0); assert!(r > 0);
assert!(w > 0); assert!(w > 0);
future::poll_fn(|cx| stream.complete_io(cx)).await?; // finish server handshake poll_fn(|cx| stream.complete_io(cx)).await?; // finish server handshake
} }
assert!(!server.is_handshaking()); assert!(!server.is_handshaking());
@ -152,8 +150,7 @@ fn stream_handshake_eof() -> io::Result<()> {
let mut bad = Bad(false); let mut bad = Bad(false);
let mut stream = Stream::new(&mut bad, &mut client); let mut stream = Stream::new(&mut bad, &mut client);
let mut cx = Context::from_waker(noop_waker_ref()); let r = poll_fn(|cx| stream.complete_io(&mut cx)).await?;
let r = stream.complete_io(&mut cx);
assert_eq!(r.map_err(|err| err.kind()), Poll::Ready(Err(io::ErrorKind::UnexpectedEof))); assert_eq!(r.map_err(|err| err.kind()), Poll::Ready(Err(io::ErrorKind::UnexpectedEof)));
Ok(()) as io::Result<()> Ok(()) as io::Result<()>
@ -166,7 +163,7 @@ fn stream_handshake_eof() -> io::Result<()> {
fn stream_eof() -> io::Result<()> { fn stream_eof() -> io::Result<()> {
let fut = async { let fut = async {
let (mut server, mut client) = make_pair(); let (mut server, mut client) = make_pair();
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?;
let mut good = Good(&mut server); let mut good = Good(&mut server);
let mut stream = Stream::new(&mut good, &mut client).set_eof(true); let mut stream = Stream::new(&mut good, &mut client).set_eof(true);

View File

@ -6,10 +6,10 @@ use std::sync::Arc;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::net::SocketAddr; use std::net::SocketAddr;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use futures::prelude::*; use tokio::prelude::*;
use futures::executor; use tokio::runtime::current_thread;
use futures::task::SpawnExt; use tokio::net::{ TcpListener, TcpStream };
use romio::tcp::{ TcpListener, TcpStream }; use futures_util::try_future::TryFutureExt;
use rustls::{ ServerConfig, ClientConfig }; use rustls::{ ServerConfig, ClientConfig };
use rustls::internal::pemfile::{ certs, rsa_private_keys }; use rustls::internal::pemfile::{ certs, rsa_private_keys };
use tokio_rustls::{ TlsConnector, TlsAcceptor }; use tokio_rustls::{ TlsConnector, TlsAcceptor };
@ -31,31 +31,39 @@ lazy_static!{
let (send, recv) = channel(); let (send, recv) = channel();
thread::spawn(move || { thread::spawn(move || {
let done = async { let mut runtime = current_thread::Runtime::new().unwrap();
let handle = runtime.handle();
let done = async move {
let addr = SocketAddr::from(([127, 0, 0, 1], 0)); let addr = SocketAddr::from(([127, 0, 0, 1], 0));
let mut pool = executor::ThreadPool::new()?; let listener = TcpListener::bind(&addr)?;
let mut listener = TcpListener::bind(&addr)?;
send.send(listener.local_addr()?).unwrap(); send.send(listener.local_addr()?).unwrap();
let mut incoming = listener.incoming(); let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await { while let Some(stream) = incoming.next().await {
let acceptor = acceptor.clone(); let acceptor = acceptor.clone();
pool.spawn( let fut = async move {
async move { let mut stream = acceptor.accept(stream?).await?;
let stream = acceptor.accept(stream?).await?;
let (mut reader, mut write) = stream.split(); // TODO split
reader.copy_into(&mut write).await?; // let (mut reader, mut write) = stream.split();
// reader.copy(&mut write).await?;
let mut buf = vec![0; 8192];
let n = stream.read(&mut buf).await?;
stream.write(&buf[..n]).await?;
Ok(()) as io::Result<()> Ok(()) as io::Result<()>
} };
.unwrap_or_else(|err| eprintln!("{:?}", err))
).unwrap(); handle.spawn(fut.unwrap_or_else(|err| eprintln!("{:?}", err))).unwrap();
} }
Ok(()) as io::Result<()> Ok(()) as io::Result<()>
}; };
executor::block_on(done).unwrap(); runtime.block_on(done.unwrap_or_else(|err| eprintln!("{:?}", err)));
}); });
let addr = recv.recv().unwrap(); let addr = recv.recv().unwrap();
@ -81,12 +89,12 @@ async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>)
assert_eq!(buf, FILE); assert_eq!(buf, FILE);
stream.close().await?; stream.shutdown().await?;
Ok(()) Ok(())
} }
#[test] #[tokio::test]
fn pass() { async fn pass() -> io::Result<()> {
let (addr, domain, chain) = start_server(); let (addr, domain, chain) = start_server();
let mut config = ClientConfig::new(); let mut config = ClientConfig::new();
@ -94,11 +102,13 @@ fn pass() {
config.root_store.add_pem_file(&mut chain).unwrap(); config.root_store.add_pem_file(&mut chain).unwrap();
let config = Arc::new(config); let config = Arc::new(config);
executor::block_on(start_client(addr.clone(), domain, config.clone())).unwrap(); start_client(addr.clone(), domain, config.clone()).await?;
Ok(())
} }
#[test] #[tokio::test]
fn fail() { async fn fail() -> io::Result<()> {
let (addr, domain, chain) = start_server(); let (addr, domain, chain) = start_server();
let mut config = ClientConfig::new(); let mut config = ClientConfig::new();
@ -107,5 +117,8 @@ fn fail() {
let config = Arc::new(config); let config = Arc::new(config);
assert_ne!(domain, &"google.com"); assert_ne!(domain, &"google.com");
assert!(executor::block_on(start_client(addr.clone(), "google.com", config)).is_err()); let ret = start_client(addr.clone(), "google.com", config).await;
assert!(ret.is_err());
Ok(())
} }