| 1 |
use super::Stream; |
| 2 |
use futures_executor::block_on; |
| 3 |
use futures_io::{AsyncRead, AsyncWrite}; |
| 4 |
use futures_util::io::{AsyncReadExt, AsyncWriteExt}; |
| 5 |
use futures_util::task::{noop_waker_ref, Context}; |
| 6 |
use futures_util::{future, ready}; |
| 7 |
use rustls::{ |
| 8 |
Certificate, ClientConfig, ClientConnection, ConnectionCommon, PrivateKey, RootCertStore, |
| 9 |
ServerConfig, ServerConnection, ServerName, |
| 10 |
}; |
| 11 |
use rustls_pemfile::{certs, pkcs8_private_keys}; |
| 12 |
use std::convert::TryFrom; |
| 13 |
use std::io::{self, BufReader, Cursor, Read, Write}; |
| 14 |
use std::pin::Pin; |
| 15 |
use std::sync::Arc; |
| 16 |
use std::task::Poll; |
| 17 |
|
| 18 |
struct Good<'a, D>(&'a mut ConnectionCommon<D>); |
| 19 |
|
| 20 |
impl<'a, D> AsyncRead for Good<'a, D> { |
| 21 |
fn poll_read( |
| 22 |
mut self: Pin<&mut Self>, |
| 23 |
_cx: &mut Context<'_>, |
| 24 |
mut buf: &mut [u8], |
| 25 |
) -> Poll<io::Result<usize>> { |
| 26 |
Poll::Ready(self.0.write_tls(buf.by_ref())) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
impl<'a, D> AsyncWrite for Good<'a, D> { |
| 31 |
fn poll_write( |
| 32 |
mut self: Pin<&mut Self>, |
| 33 |
_cx: &mut Context<'_>, |
| 34 |
mut buf: &[u8], |
| 35 |
) -> Poll<io::Result<usize>> { |
| 36 |
let len = self.0.read_tls(buf.by_ref())?; |
| 37 |
self.0 |
| 38 |
.process_new_packets() |
| 39 |
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 40 |
Poll::Ready(Ok(len)) |
| 41 |
} |
| 42 |
|
| 43 |
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 44 |
self.0 |
| 45 |
.process_new_packets() |
| 46 |
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 47 |
Poll::Ready(Ok(())) |
| 48 |
} |
| 49 |
|
| 50 |
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 51 |
self.0.send_close_notify(); |
| 52 |
self.poll_flush(cx) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
struct Bad(bool); |
| 57 |
|
| 58 |
impl AsyncRead for Bad { |
| 59 |
fn poll_read( |
| 60 |
self: Pin<&mut Self>, |
| 61 |
_cx: &mut Context<'_>, |
| 62 |
_: &mut [u8], |
| 63 |
) -> Poll<io::Result<usize>> { |
| 64 |
Poll::Ready(Ok(0)) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl AsyncWrite for Bad { |
| 69 |
fn poll_write( |
| 70 |
self: Pin<&mut Self>, |
| 71 |
_cx: &mut Context<'_>, |
| 72 |
buf: &[u8], |
| 73 |
) -> Poll<io::Result<usize>> { |
| 74 |
if self.0 { |
| 75 |
Poll::Pending |
| 76 |
} else { |
| 77 |
Poll::Ready(Ok(buf.len())) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 82 |
Poll::Ready(Ok(())) |
| 83 |
} |
| 84 |
|
| 85 |
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 86 |
Poll::Ready(Ok(())) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn stream_good() -> io::Result<()> { |
| 92 |
block_on(_stream_good()) |
| 93 |
} |
| 94 |
|
| 95 |
async fn _stream_good() -> io::Result<()> { |
| 96 |
const FILE: &[u8] = include_bytes!("../../README.md"); |
| 97 |
let (mut server, mut client) = make_pair(); |
| 98 |
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; |
| 99 |
io::copy(&mut Cursor::new(FILE), &mut server.writer())?; |
| 100 |
server.send_close_notify(); |
| 101 |
|
| 102 |
{ |
| 103 |
let mut good = Good(&mut server); |
| 104 |
let mut stream = Stream::new(&mut good, &mut client); |
| 105 |
|
| 106 |
let mut buf = Vec::new(); |
| 107 |
stream.read_to_end(&mut buf).await?; |
| 108 |
assert_eq!(buf, FILE); |
| 109 |
stream.write_all(b"Hello World!").await?; |
| 110 |
stream.conn.send_close_notify(); |
| 111 |
stream.close().await?; |
| 112 |
} |
| 113 |
|
| 114 |
let mut buf = String::new(); |
| 115 |
server.reader().read_to_string(&mut buf)?; |
| 116 |
assert_eq!(buf, "Hello World!"); |
| 117 |
|
| 118 |
Ok(()) as io::Result<()> |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn stream_bad() -> io::Result<()> { |
| 123 |
let fut = async { |
| 124 |
let (mut server, mut client) = make_pair(); |
| 125 |
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; |
| 126 |
client.set_buffer_limit(Some(1024)); |
| 127 |
|
| 128 |
let mut bad = Bad(true); |
| 129 |
let mut stream = Stream::new(&mut bad, &mut client); |
| 130 |
assert_eq!( |
| 131 |
future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x42; 8])).await?, |
| 132 |
8 |
| 133 |
); |
| 134 |
assert_eq!( |
| 135 |
future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x42; 8])).await?, |
| 136 |
8 |
| 137 |
); |
| 138 |
let r = future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x00; 1024])).await?; // fill buffer |
| 139 |
assert!(r < 1024); |
| 140 |
|
| 141 |
let mut cx = Context::from_waker(noop_waker_ref()); |
| 142 |
assert!(stream |
| 143 |
.as_mut_pin() |
| 144 |
.poll_write(&mut cx, &[0x01]) |
| 145 |
.is_pending()); |
| 146 |
|
| 147 |
Ok(()) as io::Result<()> |
| 148 |
}; |
| 149 |
|
| 150 |
block_on(fut) |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn stream_handshake() -> io::Result<()> { |
| 155 |
let fut = async { |
| 156 |
let (mut server, mut client) = make_pair(); |
| 157 |
|
| 158 |
{ |
| 159 |
let mut good = Good(&mut server); |
| 160 |
let mut stream = Stream::new(&mut good, &mut client); |
| 161 |
let (r, w) = future::poll_fn(|cx| stream.complete_io(cx)).await?; |
| 162 |
|
| 163 |
assert!(r > 0); |
| 164 |
assert!(w > 0); |
| 165 |
|
| 166 |
future::poll_fn(|cx| stream.complete_io(cx)).await?; // finish server handshake |
| 167 |
} |
| 168 |
|
| 169 |
assert!(!server.is_handshaking()); |
| 170 |
assert!(!client.is_handshaking()); |
| 171 |
|
| 172 |
Ok(()) as io::Result<()> |
| 173 |
}; |
| 174 |
|
| 175 |
block_on(fut) |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn stream_handshake_eof() -> io::Result<()> { |
| 180 |
let fut = async { |
| 181 |
let (_, mut client) = make_pair(); |
| 182 |
|
| 183 |
let mut bad = Bad(false); |
| 184 |
let mut stream = Stream::new(&mut bad, &mut client); |
| 185 |
|
| 186 |
let mut cx = Context::from_waker(noop_waker_ref()); |
| 187 |
let r = stream.complete_io(&mut cx); |
| 188 |
assert_eq!( |
| 189 |
r.map_err(|err| err.kind()), |
| 190 |
Poll::Ready(Err(io::ErrorKind::UnexpectedEof)) |
| 191 |
); |
| 192 |
|
| 193 |
Ok(()) as io::Result<()> |
| 194 |
}; |
| 195 |
|
| 196 |
block_on(fut) |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn stream_eof() -> io::Result<()> { |
| 201 |
let fut = async { |
| 202 |
let (mut server, mut client) = make_pair(); |
| 203 |
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; |
| 204 |
|
| 205 |
let mut eof_stream = Bad(false); |
| 206 |
let mut stream = Stream::new(&mut eof_stream, &mut client); |
| 207 |
|
| 208 |
let mut buf = Vec::new(); |
| 209 |
let res = stream.read_to_end(&mut buf).await; |
| 210 |
assert_eq!( |
| 211 |
res.err().map(|e| e.kind()), |
| 212 |
Some(std::io::ErrorKind::UnexpectedEof) |
| 213 |
); |
| 214 |
|
| 215 |
Ok(()) as io::Result<()> |
| 216 |
}; |
| 217 |
|
| 218 |
block_on(fut) |
| 219 |
} |
| 220 |
|
| 221 |
fn make_pair() -> (ServerConnection, ClientConnection) { |
| 222 |
const CERT: &str = include_str!("../../tests/end.cert"); |
| 223 |
const CHAIN: &str = include_str!("../../tests/end.chain"); |
| 224 |
const RSA: &str = include_str!("../../tests/end.rsa"); |
| 225 |
|
| 226 |
let cert = certs(&mut BufReader::new(Cursor::new(CERT))).unwrap(); |
| 227 |
let cert = cert.into_iter().map(Certificate).collect(); |
| 228 |
let mut keys = pkcs8_private_keys(&mut BufReader::new(Cursor::new(RSA))).unwrap(); |
| 229 |
let key = PrivateKey(keys.pop().unwrap()); |
| 230 |
let sconfig = ServerConfig::builder() |
| 231 |
.with_safe_defaults() |
| 232 |
.with_no_client_auth() |
| 233 |
.with_single_cert(cert, key) |
| 234 |
.unwrap(); |
| 235 |
let server = ServerConnection::new(Arc::new(sconfig)); |
| 236 |
|
| 237 |
let domain = ServerName::try_from("localhost").unwrap(); |
| 238 |
let mut root_store = RootCertStore::empty(); |
| 239 |
let chain = certs(&mut BufReader::new(Cursor::new(CHAIN))).unwrap(); |
| 240 |
let (added, ignored) = root_store.add_parsable_certificates(&chain); |
| 241 |
assert!(added >= 1 && ignored == 0); |
| 242 |
let cconfig = ClientConfig::builder() |
| 243 |
.with_safe_defaults() |
| 244 |
.with_root_certificates(root_store) |
| 245 |
.with_no_client_auth(); |
| 246 |
let client = ClientConnection::new(Arc::new(cconfig), domain); |
| 247 |
|
| 248 |
(server.unwrap(), client.unwrap()) |
| 249 |
} |
| 250 |
|
| 251 |
fn do_handshake( |
| 252 |
client: &mut ClientConnection, |
| 253 |
server: &mut ServerConnection, |
| 254 |
cx: &mut Context<'_>, |
| 255 |
) -> Poll<io::Result<()>> { |
| 256 |
let mut good = Good(server); |
| 257 |
let mut stream = Stream::new(&mut good, client); |
| 258 |
|
| 259 |
if stream.conn.is_handshaking() { |
| 260 |
ready!(stream.complete_io(cx))?; |
| 261 |
} |
| 262 |
|
| 263 |
if stream.conn.wants_write() { |
| 264 |
ready!(stream.complete_io(cx))?; |
| 265 |
} |
| 266 |
|
| 267 |
Poll::Ready(Ok(())) |
| 268 |
} |