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
script:
- cargo test
- cargo test --features early-data
- cd examples/server
- cargo check
- cd ../../examples/client
- cargo check
- cargo test --test test
# - cargo test --features early-data
# - cd examples/server
# - cargo check
# - cd ../../examples/client
# - cargo check

View File

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

View File

@ -1,10 +1,8 @@
use std::pin::Pin;
use std::task::Poll;
use std::sync::Arc;
use futures::prelude::*;
use futures::task::{ Context, noop_waker_ref };
use futures::executor;
use futures::io::{ AsyncRead, AsyncWrite };
use std::task::{ Poll, Context };
use futures_util::future::poll_fn;
use tokio_io::{ AsyncRead, AsyncWrite, AsyncReadExt, AsyncWriteExt };
use std::io::{ self, Read, Write, BufReader, Cursor };
use webpki::DNSNameRef;
use rustls::internal::pemfile::{ certs, rsa_private_keys };
@ -73,7 +71,7 @@ fn stream_good() -> io::Result<()> {
let fut = async {
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)?;
{
@ -100,18 +98,18 @@ fn stream_good() -> io::Result<()> {
fn stream_bad() -> io::Result<()> {
let fut = async {
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);
let mut bad = Bad(true);
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!(future::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
assert_eq!(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 = poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x00; 1024])).await?; // fill buffer
assert!(r < 1024);
let mut cx = Context::from_waker(noop_waker_ref());
assert!(stream.as_mut_pin().poll_write(&mut cx, &[0x01]).is_pending());
let ret = poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x01]));
assert!(ret.is_pending());
Ok(()) as io::Result<()>
};
@ -127,12 +125,12 @@ fn stream_handshake() -> io::Result<()> {
{
let mut good = Good(&mut server);
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!(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());
@ -152,8 +150,7 @@ fn stream_handshake_eof() -> io::Result<()> {
let mut bad = Bad(false);
let mut stream = Stream::new(&mut bad, &mut client);
let mut cx = Context::from_waker(noop_waker_ref());
let r = stream.complete_io(&mut cx);
let r = poll_fn(|cx| stream.complete_io(&mut cx)).await?;
assert_eq!(r.map_err(|err| err.kind()), Poll::Ready(Err(io::ErrorKind::UnexpectedEof)));
Ok(()) as io::Result<()>
@ -166,7 +163,7 @@ fn stream_handshake_eof() -> io::Result<()> {
fn stream_eof() -> io::Result<()> {
let fut = async {
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 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::net::SocketAddr;
use lazy_static::lazy_static;
use futures::prelude::*;
use futures::executor;
use futures::task::SpawnExt;
use romio::tcp::{ TcpListener, TcpStream };
use tokio::prelude::*;
use tokio::runtime::current_thread;
use tokio::net::{ TcpListener, TcpStream };
use futures_util::try_future::TryFutureExt;
use rustls::{ ServerConfig, ClientConfig };
use rustls::internal::pemfile::{ certs, rsa_private_keys };
use tokio_rustls::{ TlsConnector, TlsAcceptor };
@ -31,31 +31,39 @@ lazy_static!{
let (send, recv) = channel();
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 mut pool = executor::ThreadPool::new()?;
let mut listener = TcpListener::bind(&addr)?;
let listener = TcpListener::bind(&addr)?;
send.send(listener.local_addr()?).unwrap();
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let acceptor = acceptor.clone();
pool.spawn(
async move {
let stream = acceptor.accept(stream?).await?;
let (mut reader, mut write) = stream.split();
reader.copy_into(&mut write).await?;
Ok(()) as io::Result<()>
}
.unwrap_or_else(|err| eprintln!("{:?}", err))
).unwrap();
let fut = async move {
let mut stream = acceptor.accept(stream?).await?;
// TODO split
// 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<()>
};
handle.spawn(fut.unwrap_or_else(|err| eprintln!("{:?}", err))).unwrap();
}
Ok(()) as io::Result<()>
};
executor::block_on(done).unwrap();
runtime.block_on(done.unwrap_or_else(|err| eprintln!("{:?}", err)));
});
let addr = recv.recv().unwrap();
@ -81,12 +89,12 @@ async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>)
assert_eq!(buf, FILE);
stream.close().await?;
stream.shutdown().await?;
Ok(())
}
#[test]
fn pass() {
#[tokio::test]
async fn pass() -> io::Result<()> {
let (addr, domain, chain) = start_server();
let mut config = ClientConfig::new();
@ -94,11 +102,13 @@ fn pass() {
config.root_store.add_pem_file(&mut chain).unwrap();
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]
fn fail() {
#[tokio::test]
async fn fail() -> io::Result<()> {
let (addr, domain, chain) = start_server();
let mut config = ClientConfig::new();
@ -107,5 +117,8 @@ fn fail() {
let config = Arc::new(config);
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(())
}