2019-02-18 12:01:37 +00:00
|
|
|
use std::io;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::net::ToSocketAddrs;
|
2019-08-10 16:00:49 +00:00
|
|
|
use tokio::prelude::*;
|
|
|
|
use tokio::net::TcpStream;
|
2019-02-18 12:41:52 +00:00
|
|
|
use rustls::ClientConfig;
|
2019-10-01 08:27:42 +00:00
|
|
|
use tokio_rustls::{ TlsConnector, client::TlsStream };
|
2019-02-18 12:01:37 +00:00
|
|
|
|
|
|
|
|
2019-10-01 08:27:42 +00:00
|
|
|
async fn get(config: Arc<ClientConfig>, domain: &str, port: u16)
|
2019-02-18 12:41:52 +00:00
|
|
|
-> io::Result<(TlsStream<TcpStream>, String)>
|
2019-02-18 12:01:37 +00:00
|
|
|
{
|
2019-10-01 08:27:42 +00:00
|
|
|
let connector = TlsConnector::from(config);
|
2019-02-18 12:01:37 +00:00
|
|
|
let input = format!("GET / HTTP/1.0\r\nHost: {}\r\n\r\n", domain);
|
|
|
|
|
2019-10-01 08:27:42 +00:00
|
|
|
let addr = (domain, port)
|
2019-02-18 12:01:37 +00:00
|
|
|
.to_socket_addrs()?
|
|
|
|
.next().unwrap();
|
2019-05-18 16:48:56 +00:00
|
|
|
let domain = webpki::DNSNameRef::try_from_ascii_str(&domain).unwrap();
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
|
|
|
let stream = TcpStream::connect(&addr).await?;
|
|
|
|
let mut stream = connector.connect(domain, stream).await?;
|
|
|
|
stream.write_all(input.as_bytes()).await?;
|
2019-10-01 02:53:11 +00:00
|
|
|
stream.flush().await?;
|
2019-05-18 16:48:56 +00:00
|
|
|
stream.read_to_end(&mut buf).await?;
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2019-05-18 16:48:56 +00:00
|
|
|
Ok((stream, String::from_utf8(buf).unwrap()))
|
2019-02-18 12:01:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 16:00:49 +00:00
|
|
|
#[tokio::test]
|
2019-10-01 08:27:42 +00:00
|
|
|
async fn test_tls12() -> io::Result<()> {
|
2019-02-18 12:01:37 +00:00
|
|
|
let mut config = ClientConfig::new();
|
|
|
|
config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
|
2019-10-01 08:27:42 +00:00
|
|
|
config.versions = vec![rustls::ProtocolVersion::TLSv1_2];
|
2019-02-18 12:01:37 +00:00
|
|
|
let config = Arc::new(config);
|
2019-10-01 08:27:42 +00:00
|
|
|
let domain = "tls-v1-2.badssl.com";
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2019-10-01 08:27:42 +00:00
|
|
|
let (_, output) = get(config.clone(), domain, 1012).await?;
|
|
|
|
assert!(output.contains("<title>tls-v1-2.badssl.com</title>"));
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2019-10-01 08:27:42 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-31 16:04:06 +00:00
|
|
|
#[ignore]
|
2019-10-01 08:27:42 +00:00
|
|
|
#[should_panic]
|
|
|
|
#[test]
|
|
|
|
fn test_tls13() {
|
|
|
|
unimplemented!("todo https://github.com/chromium/badssl.com/pull/373");
|
|
|
|
}
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2019-10-01 08:27:42 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_modern() -> io::Result<()> {
|
|
|
|
let mut config = ClientConfig::new();
|
|
|
|
config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
|
|
|
|
let config = Arc::new(config);
|
|
|
|
let domain = "mozilla-modern.badssl.com";
|
|
|
|
|
|
|
|
let (_, output) = get(config.clone(), domain, 443).await?;
|
|
|
|
assert!(output.contains("<title>mozilla-modern.badssl.com</title>"));
|
2019-08-10 16:00:49 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-02-18 12:01:37 +00:00
|
|
|
}
|