diff options
author | Carl Lerche <me@carllerche.com> | 2019-02-21 11:56:15 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-21 11:56:15 -0800 |
commit | 80162306e71c8561873a9c9496d65f2c1387d119 (patch) | |
tree | 83327ca8d9d1326d54e3c679e1fb4eb16775d4be /tokio-tls | |
parent | ab595d08253dd7ee0422144f8dafffa382700976 (diff) |
chore: apply rustfmt to all crates (#917)
Diffstat (limited to 'tokio-tls')
-rw-r--r-- | tokio-tls/examples/download-rust-lang.rs | 28 | ||||
-rw-r--r-- | tokio-tls/examples/echo.rs | 69 | ||||
-rw-r--r-- | tokio-tls/src/lib.rs | 38 | ||||
-rw-r--r-- | tokio-tls/tests/bad.rs | 15 | ||||
-rw-r--r-- | tokio-tls/tests/google.rs | 41 | ||||
-rw-r--r-- | tokio-tls/tests/smoke.rs | 173 |
6 files changed, 186 insertions, 178 deletions
diff --git a/tokio-tls/examples/download-rust-lang.rs b/tokio-tls/examples/download-rust-lang.rs index 4d8780e8..9d23dc78 100644 --- a/tokio-tls/examples/download-rust-lang.rs +++ b/tokio-tls/examples/download-rust-lang.rs @@ -14,27 +14,31 @@ use tokio::runtime::Runtime; fn main() -> Result<(), Box<std::error::Error>> { let mut runtime = Runtime::new()?; - let addr = "www.rust-lang.org:443".to_socket_addrs()?.next().ok_or("failed to resolve www.rust-lang.org")?; + let addr = "www.rust-lang.org:443" + .to_socket_addrs()? + .next() + .ok_or("failed to resolve www.rust-lang.org")?; let socket = TcpStream::connect(&addr); let cx = TlsConnector::builder().build()?; let cx = tokio_tls::TlsConnector::from(cx); let tls_handshake = socket.and_then(move |socket| { - cx.connect("www.rust-lang.org", socket).map_err(|e| { - io::Error::new(io::ErrorKind::Other, e) - }) + cx.connect("www.rust-lang.org", socket) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) }); let request = tls_handshake.and_then(|socket| { - tokio_io::io::write_all(socket, "\ - GET / HTTP/1.0\r\n\ - Host: www.rust-lang.org\r\n\ - \r\n\ - ".as_bytes()) - }); - let response = request.and_then(|(socket, _)| { - tokio_io::io::read_to_end(socket, Vec::new()) + tokio_io::io::write_all( + socket, + "\ + GET / HTTP/1.0\r\n\ + Host: www.rust-lang.org\r\n\ + \r\n\ + " + .as_bytes(), + ) }); + let response = request.and_then(|(socket, _)| tokio_io::io::read_to_end(socket, Vec::new())); let (_, data) = runtime.block_on(response)?; println!("{}", String::from_utf8_lossy(&data)); diff --git a/tokio-tls/examples/echo.rs b/tokio-tls/examples/echo.rs index 697c14c6..c39d5873 100644 --- a/tokio-tls/examples/echo.rs +++ b/tokio-tls/examples/echo.rs @@ -16,43 +16,42 @@ fn main() -> Result<(), Box<std::error::Error>> { // Create the TLS acceptor. let der = include_bytes!("identity.p12"); let cert = Identity::from_pkcs12(der, "mypass")?; - let tls_acceptor = tokio_tls::TlsAcceptor::from( - native_tls::TlsAcceptor::builder(cert).build()?); + let tls_acceptor = + tokio_tls::TlsAcceptor::from(native_tls::TlsAcceptor::builder(cert).build()?); // Iterate incoming connections - let server = tcp.incoming().for_each(move |tcp| { - - // Accept the TLS connection. - let tls_accept = tls_acceptor.accept(tcp) - .and_then(move |tls| { - // Split up the read and write halves - let (reader, writer) = tls.split(); - - // Copy the data back to the client - let conn = io::copy(reader, writer) - // print what happened - .map(|(n, _, _)| { - println!("wrote {} bytes", n) - }) - // Handle any errors - .map_err(|err| { - println!("IO error {:?}", err) - }); - - // Spawn the future as a concurrent task - tokio::spawn(conn); - - Ok(()) - }) - .map_err(|err| { - println!("TLS accept error: {:?}", err); - }); - tokio::spawn(tls_accept); - - Ok(()) - }).map_err(|err| { - println!("server error {:?}", err); - }); + let server = tcp + .incoming() + .for_each(move |tcp| { + // Accept the TLS connection. + let tls_accept = tls_acceptor + .accept(tcp) + .and_then(move |tls| { + // Split up the read and write halves + let (reader, writer) = tls.split(); + + // Copy the data back to the client + let conn = io::copy(reader, writer) + // print what happened + .map(|(n, _, _)| println!("wrote {} bytes", n)) + // Handle any errors + .map_err(|err| println!("IO error {:?}", err)); + + // Spawn the future as a concurrent task + tokio::spawn(conn); + + Ok(()) + }) + .map_err(|err| { + println!("TLS accept error: {:?}", err); + }); + tokio::spawn(tls_accept); + + Ok(()) + }) + .map_err(|err| { + println!("server error {:?}", err); + }); // Start the runtime and spin up the server tokio::run(server); diff --git a/tokio-tls/src/lib.rs b/tokio-tls/src/lib.rs index 63275c7f..66bc3964 100644 --- a/tokio-tls/src/lib.rs +++ b/tokio-tls/src/lib.rs @@ -25,8 +25,8 @@ extern crate tokio_io; use std::io::{self, Read, Write}; -use futures::{Poll, Future, Async}; -use native_tls::{HandshakeError, Error}; +use futures::{Async, Future, Poll}; +use native_tls::{Error, HandshakeError}; use tokio_io::{AsyncRead, AsyncWrite}; /// A wrapper around an underlying raw stream which implements the TLS or SSL @@ -101,9 +101,7 @@ impl<S: Read + Write> Write for TlsStream<S> { } } - -impl<S: AsyncRead + AsyncWrite> AsyncRead for TlsStream<S> { -} +impl<S: AsyncRead + AsyncWrite> AsyncRead for TlsStream<S> {} impl<S: AsyncRead + AsyncWrite> AsyncWrite for TlsStream<S> { fn shutdown(&mut self) -> Poll<(), io::Error> { @@ -126,7 +124,8 @@ impl TlsConnector { /// provided here to perform the client half of a connection to a /// TLS-powered server. pub fn connect<S>(&self, domain: &str, stream: S) -> Connect<S> - where S: AsyncRead + AsyncWrite, + where + S: AsyncRead + AsyncWrite, { Connect { inner: MidHandshake { @@ -138,9 +137,7 @@ impl TlsConnector { impl From<native_tls::TlsConnector> for TlsConnector { fn from(inner: native_tls::TlsConnector) -> TlsConnector { - TlsConnector { - inner, - } + TlsConnector { inner } } } @@ -156,7 +153,8 @@ impl TlsAcceptor { /// `TcpListener`. That socket is then passed to this function to perform /// the server half of accepting a client connection. pub fn accept<S>(&self, stream: S) -> Accept<S> - where S: AsyncRead + AsyncWrite, + where + S: AsyncRead + AsyncWrite, { Accept { inner: MidHandshake { @@ -168,9 +166,7 @@ impl TlsAcceptor { impl From<native_tls::TlsAcceptor> for TlsAcceptor { fn from(inner: native_tls::TlsAcceptor) -> TlsAcceptor { - TlsAcceptor { - inner, - } + TlsAcceptor { inner } } } @@ -200,16 +196,14 @@ impl<S: AsyncRead + AsyncWrite> Future for MidHandshake<S> { match self.inner.take().expect("cannot poll MidHandshake twice") { Ok(stream) => Ok(TlsStream { inner: stream }.into()), Err(HandshakeError::Failure(e)) => Err(e), - Err(HandshakeError::WouldBlock(s)) => { - match s.handshake() { - Ok(stream) => Ok(TlsStream { inner: stream }.into()), - Err(HandshakeError::Failure(e)) => Err(e), - Err(HandshakeError::WouldBlock(s)) => { - self.inner = Some(Err(HandshakeError::WouldBlock(s))); - Ok(Async::NotReady) - } + Err(HandshakeError::WouldBlock(s)) => match s.handshake() { + Ok(stream) => Ok(TlsStream { inner: stream }.into()), + Err(HandshakeError::Failure(e)) => Err(e), + Err(HandshakeError::WouldBlock(s)) => { + self.inner = Some(Err(HandshakeError::WouldBlock(s))); + Ok(Async::NotReady) } - } + }, } } } diff --git a/tokio-tls/tests/bad.rs b/tokio-tls/tests/bad.rs index 4373fd1c..8b010a2a 100644 --- a/tokio-tls/tests/bad.rs +++ b/tokio-tls/tests/bad.rs @@ -16,10 +16,12 @@ use tokio::net::TcpStream; use tokio::runtime::Runtime; macro_rules! t { - ($e:expr) => (match $e { - Ok(e) => e, - Err(e) => panic!("{} failed with {:?}", stringify!($e), e), - }) + ($e:expr) => { + match $e { + Ok(e) => e, + Err(e) => panic!("{} failed with {:?}", stringify!($e), e), + } + }; } cfg_if! { @@ -100,9 +102,8 @@ fn get_host(host: &'static str) -> Error { let builder = TlsConnector::builder(); let cx = builder.build().unwrap(); let cx = tokio_tls::TlsConnector::from(cx); - cx.connect(host, socket).map_err(|e| { - Error::new(io::ErrorKind::Other, e) - }) + cx.connect(host, socket) + .map_err(|e| Error::new(io::ErrorKind::Other, e)) }); let res = l.block_on(data); diff --git a/tokio-tls/tests/google.rs b/tokio-tls/tests/google.rs index 853ceda5..aa4fc0f0 100644 --- a/tokio-tls/tests/google.rs +++ b/tokio-tls/tests/google.rs @@ -1,9 +1,9 @@ extern crate env_logger; extern crate futures; extern crate native_tls; +extern crate tokio; extern crate tokio_io; extern crate tokio_tls; -extern crate tokio; #[macro_use] extern crate cfg_if; @@ -13,15 +13,17 @@ use std::net::ToSocketAddrs; use futures::Future; use native_tls::TlsConnector; -use tokio_io::io::{flush, read_to_end, write_all}; use tokio::net::TcpStream; use tokio::runtime::Runtime; +use tokio_io::io::{flush, read_to_end, write_all}; macro_rules! t { - ($e:expr) => (match $e { - Ok(e) => e, - Err(e) => panic!("{} failed with {:?}", stringify!($e), e), - }) + ($e:expr) => { + match $e { + Ok(e) => e, + Err(e) => panic!("{} failed with {:?}", stringify!($e), e), + } + }; } cfg_if! { @@ -71,15 +73,15 @@ fn fetch_google() { let mut l = t!(Runtime::new()); let client = TcpStream::connect(&addr); - // Send off the request by first negotiating an SSL handshake, then writing // of our request, then flushing, then finally read off the response. - let data = client.and_then(move |socket| { - let builder = TlsConnector::builder(); - let connector = t!(builder.build()); - let connector = tokio_tls::TlsConnector::from(connector); - connector.connect("google.com", socket).map_err(native2io) - }) + let data = client + .and_then(move |socket| { + let builder = TlsConnector::builder(); + let connector = t!(builder.build()); + let connector = tokio_tls::TlsConnector::from(connector); + connector.connect("google.com", socket).map_err(native2io) + }) .and_then(|socket| write_all(socket, b"GET / HTTP/1.0\r\n\r\n")) .and_then(|(socket, _)| flush(socket)) .and_then(|socket| read_to_end(socket, Vec::new())); @@ -105,12 +107,13 @@ fn wrong_hostname_error() { let mut l = t!(Runtime::new()); let client = TcpStream::connect(&addr); let data = client.and_then(move |socket| { - let builder = TlsConnector::builder(); - let connector = t!(builder.build()); - let connector = tokio_tls::TlsConnector::from(connector); - connector.connect("rust-lang.org", socket) - .map_err(native2io) - }); + let builder = TlsConnector::builder(); + let connector = t!(builder.build()); + let connector = tokio_tls::TlsConnector::from(connector); + connector + .connect("rust-lang.org", socket) + .map_err(native2io) + }); let res = l.block_on(data); assert!(res.is_err()); diff --git a/tokio-tls/tests/smoke.rs b/tokio-tls/tests/smoke.rs index b4e9d2bc..a617d4d5 100644 --- a/tokio-tls/tests/smoke.rs +++ b/tokio-tls/tests/smoke.rs @@ -11,19 +11,21 @@ extern crate cfg_if; use std::io::{self, Read, Write}; use std::process::Command; -use futures::{Future, Poll}; use futures::stream::Stream; -use tokio_io::{AsyncRead, AsyncWrite}; -use tokio_io::io::{read_to_end, copy, shutdown}; -use tokio::runtime::Runtime; +use futures::{Future, Poll}; +use native_tls::{Identity, TlsAcceptor, TlsConnector}; use tokio::net::{TcpListener, TcpStream}; -use native_tls::{TlsConnector, TlsAcceptor, Identity}; +use tokio::runtime::Runtime; +use tokio_io::io::{copy, read_to_end, shutdown}; +use tokio_io::{AsyncRead, AsyncWrite}; macro_rules! t { - ($e:expr) => (match $e { - Ok(e) => e, - Err(e) => panic!("{} failed with {:?}", stringify!($e), e), - }) + ($e:expr) => { + match $e { + Ok(e) => e, + Err(e) => panic!("{} failed with {:?}", stringify!($e), e), + } + }; } #[allow(dead_code)] @@ -45,7 +47,10 @@ fn openssl_keys() -> &'static Keys { let certfile = path.join("test.crt"); let config = path.join("openssl.config"); - File::create(&config).unwrap().write_all(b"\ + File::create(&config) + .unwrap() + .write_all( + b"\ [req]\n\ distinguished_name=dn\n\ [ dn ]\n\ @@ -55,44 +60,60 @@ fn openssl_keys() -> &'static Keys { subjectAltName = @alt_names [alt_names] DNS.1 = localhost - ").unwrap(); + ", + ) + .unwrap(); let subj = "/C=US/ST=Denial/L=Sprintfield/O=Dis/CN=localhost"; let output = t!(Command::new("openssl") - .arg("req") - .arg("-nodes") - .arg("-x509") - .arg("-newkey").arg("rsa:2048") - .arg("-config").arg(&config) - .arg("-extensions").arg("ext") - .arg("-subj").arg(subj) - .arg("-keyout").arg(&keyfile) - .arg("-out").arg(&certfile) - .arg("-days").arg("1") - .output()); + .arg("req") + .arg("-nodes") + .arg("-x509") + .arg("-newkey") + .arg("rsa:2048") + .arg("-config") + .arg(&config) + .arg("-extensions") + .arg("ext") + .arg("-subj") + .arg(subj) + .arg("-keyout") + .arg(&keyfile) + .arg("-out") + .arg(&certfile) + .arg("-days") + .arg("1") + .output()); assert!(output.status.success()); let crtout = t!(Command::new("openssl") - .arg("x509") - .arg("-outform").arg("der") - .arg("-in").arg(&certfile) - .output()); + .arg("x509") + .arg("-outform") + .arg("der") + .arg("-in") + .arg(&certfile) + .output()); assert!(crtout.status.success()); let keyout = t!(Command::new("openssl") - .arg("rsa") - .arg("-outform").arg("der") - .arg("-in").arg(&keyfile) - .output()); + .arg("rsa") + .arg("-outform") + .arg("der") + .arg("-in") + .arg(&keyfile) + .output()); assert!(keyout.status.success()); let pkcs12out = t!(Command::new("openssl") - .arg("pkcs12") - .arg("-export") - .arg("-nodes") - .arg("-inkey").arg(&keyfile) - .arg("-in").arg(&certfile) - .arg("-password").arg("pass:foobar") - .output()); + .arg("pkcs12") + .arg("-export") + .arg("-nodes") + .arg("-inkey") + .arg(&keyfile) + .arg("-in") + .arg(&certfile) + .arg("-password") + .arg("pass:foobar") + .output()); assert!(pkcs12out.status.success()); let keys = Box::new(Keys { @@ -104,9 +125,7 @@ fn openssl_keys() -> &'static Keys { KEYS = Box::into_raw(keys); } }); - unsafe { - &*KEYS - } + unsafe { &*KEYS } } cfg_if! { @@ -509,24 +528,18 @@ fn client_to_server() { // Create a future to accept one socket, connect the ssl stream, and then // read all the data from it. let socket = srv.incoming().take(1).collect(); - let received = socket.map(|mut socket| { - socket.remove(0) - }).and_then(move |socket| { - server_cx.accept(socket).map_err(native2io) - }).and_then(|socket| { - read_to_end(socket, Vec::new()) - }); + let received = socket + .map(|mut socket| socket.remove(0)) + .and_then(move |socket| server_cx.accept(socket).map_err(native2io)) + .and_then(|socket| read_to_end(socket, Vec::new())); // Create a future to connect to our server, connect the ssl stream, and // then write a bunch of data to it. let client = TcpStream::connect(&addr); - let sent = client.and_then(move |socket| { - client_cx.connect("localhost", socket).map_err(native2io) - }).and_then(|socket| { - copy(io::repeat(9).take(AMT), socket) - }).and_then(|(amt, _repeat, socket)| { - shutdown(socket).map(move |_| amt) - }); + let sent = client + .and_then(move |socket| client_cx.connect("localhost", socket).map_err(native2io)) + .and_then(|socket| copy(io::repeat(9).take(AMT), socket)) + .and_then(|(amt, _repeat, socket)| shutdown(socket).map(move |_| amt)); // Finally, run everything! let (amt, (_, data)) = t!(l.block_on(sent.join(received))); @@ -546,22 +559,16 @@ fn server_to_client() { let (server_cx, client_cx) = contexts(); let socket = srv.incoming().take(1).collect(); - let sent = socket.map(|mut socket| { - socket.remove(0) - }).and_then(move |socket| { - server_cx.accept(socket).map_err(native2io) - }).and_then(|socket| { - copy(io::repeat(9).take(AMT), socket) - }).and_then(|(amt, _repeat, socket)| { - shutdown(socket).map(move |_| amt) - }); + let sent = socket + .map(|mut socket| socket.remove(0)) + .and_then(move |socket| server_cx.accept(socket).map_err(native2io)) + .and_then(|socket| copy(io::repeat(9).take(AMT), socket)) + .and_then(|(amt, _repeat, socket)| shutdown(socket).map(move |_| amt)); let client = TcpStream::connect(&addr); - let received = client.and_then(move |socket| { - client_cx.connect("localhost", socket).map_err(native2io) - }).and_then(|socket| { - read_to_end(socket, Vec::new()) - }); + let received = client + .and_then(move |socket| client_cx.connect("localhost", socket).map_err(native2io)) + .and_then(|socket| read_to_end(socket, Vec::new())); // Finally, run everything! let (amt, (_, data)) = t!(l.block_on(sent.join(received))); @@ -608,23 +615,23 @@ fn one_byte_at_a_time() { let (server_cx, client_cx) = contexts(); let socket = srv.incoming().take(1).collect(); - let sent = socket.map(|mut socket| { - socket.remove(0) - }).and_then(move |socket| { - server_cx.accept(OneByte { inner: socket }).map_err(native2io) - }).and_then(|socket| { - copy(io::repeat(9).take(AMT), socket) - }).and_then(|(amt, _repeat, socket)| { - shutdown(socket).map(move |_| amt) - }); + let sent = socket + .map(|mut socket| socket.remove(0)) + .and_then(move |socket| { + server_cx + .accept(OneByte { inner: socket }) + .map_err(native2io) + }) + .and_then(|socket| copy(io::repeat(9).take(AMT), socket)) + .and_then(|(amt, _repeat, socket)| shutdown(socket).map(move |_| amt)); let client = TcpStream::connect(&addr); - let received = client.and_then(move |socket| { - let socket = OneByte { inner: socket }; - client_cx.connect("localhost", socket).map_err(native2io) - }).and_then(|socket| { - read_to_end(socket, Vec::new()) - }); + let received = client + .and_then(move |socket| { + let socket = OneByte { inner: socket }; + client_cx.connect("localhost", socket).map_err(native2io) + }) + .and_then(|socket| read_to_end(socket, Vec::new())); let (amt, (_, data)) = t!(l.block_on(sent.join(received))); assert_eq!(amt, AMT); |