tls_api_test_2/
google.rs

1use std::any;
2use std::net::ToSocketAddrs;
3
4use tls_api::runtime::AsyncReadExt;
5use tls_api::runtime::AsyncWriteExt;
6use tls_api::TlsConnector;
7use tls_api::TlsConnectorBuilder;
8
9use crate::block_on;
10use crate::TcpStream;
11
12async fn test_google_impl<C: TlsConnector>() {
13    drop(env_logger::try_init());
14
15    if !C::IMPLEMENTED {
16        eprintln!(
17            "connector {} is not implemented; skipping",
18            any::type_name::<C>()
19        );
20        return;
21    }
22
23    // First up, resolve google.com
24    let addr = t!("google.com:443".to_socket_addrs()).next().unwrap();
25
26    let connector: C = C::builder().expect("builder").build().expect("build");
27    let tcp_stream = t!(TcpStream::connect(addr).await);
28    let mut tls_stream = t!(connector.connect("google.com", tcp_stream).await);
29
30    info!("handshake complete");
31
32    t!(tls_stream.write_all(b"GET / HTTP/1.0\r\n\r\n").await);
33    let mut result = vec![];
34    let res = tls_stream.read_to_end(&mut result).await;
35
36    // Google will not send close_notify and just close the connection.
37    // This means that they are not confirming to TLS exactly, that connections to google.com
38    // are vulnerable to truncation attacks and that we need to suppress error about this here.
39    match res {
40        Ok(_) => {}
41        Err(e)
42            if e.to_string()
43                .contains("peer closed connection without sending TLS close_notify") => {}
44        Err(e) => panic!("{}", e),
45    }
46
47    println!("{}", String::from_utf8_lossy(&result));
48    assert!(
49        result.starts_with(b"HTTP/1.0"),
50        "wrong result: {:?}",
51        result
52    );
53    assert!(result.ends_with(b"</HTML>\r\n") || result.ends_with(b"</html>"));
54}
55
56/// Download google.com front page.
57pub fn test_google<C: TlsConnector>() {
58    block_on(test_google_impl::<C>())
59}