|
| 1 | +use anyhow::{Context, Result}; |
1 | 2 | use core::str;
|
2 |
| - |
3 | 3 | use test_programs::wasi::sockets::network::{IpSocketAddress, Network};
|
4 | 4 | use test_programs::wasi::sockets::tcp::{ShutdownType, TcpSocket};
|
5 | 5 | use test_programs::wasi::tls::types::ClientHandshake;
|
6 | 6 |
|
7 |
| -fn test_tls_sample_application() { |
| 7 | +fn make_tls_request(domain: &str) -> Result<String> { |
8 | 8 | const PORT: u16 = 443;
|
9 |
| - const DOMAIN: &'static str = "example.com"; |
10 | 9 |
|
11 |
| - let request = format!("GET / HTTP/1.1\r\nHost: {DOMAIN}\r\n\r\n"); |
| 10 | + let request = |
| 11 | + format!("GET / HTTP/1.1\r\nHost: {domain}\r\nUser-Agent: wasmtime-wasi-rust\r\n\r\n"); |
12 | 12 |
|
13 | 13 | let net = Network::default();
|
14 | 14 |
|
15 | 15 | let Some(ip) = net
|
16 |
| - .permissive_blocking_resolve_addresses(DOMAIN) |
| 16 | + .permissive_blocking_resolve_addresses(domain) |
17 | 17 | .unwrap()
|
18 | 18 | .first()
|
19 | 19 | .map(|a| a.to_owned())
|
20 | 20 | else {
|
21 |
| - eprintln!("DNS lookup failed."); |
22 |
| - return; |
| 21 | + return Err(anyhow::anyhow!("DNS lookup failed.")); |
23 | 22 | };
|
24 | 23 |
|
25 | 24 | let socket = TcpSocket::new(ip.family()).unwrap();
|
26 | 25 | let (tcp_input, tcp_output) = socket
|
27 | 26 | .blocking_connect(&net, IpSocketAddress::new(ip, PORT))
|
28 |
| - .unwrap(); |
| 27 | + .context("failed to connect")?; |
29 | 28 |
|
30 | 29 | let (client_connection, tls_input, tls_output) =
|
31 |
| - ClientHandshake::new(DOMAIN, tcp_input, tcp_output) |
| 30 | + ClientHandshake::new(domain, tcp_input, tcp_output) |
32 | 31 | .blocking_finish()
|
33 |
| - .unwrap(); |
| 32 | + .map_err(|_| anyhow::anyhow!("failed to finish handshake"))?; |
34 | 33 |
|
35 | 34 | tls_output.blocking_write_util(request.as_bytes()).unwrap();
|
36 | 35 | client_connection
|
37 | 36 | .blocking_close_output(&tls_output)
|
38 |
| - .unwrap(); |
39 |
| - socket.shutdown(ShutdownType::Send).unwrap(); |
40 |
| - let response = tls_input.blocking_read_to_end().unwrap(); |
41 |
| - let response = String::from_utf8(response).unwrap(); |
| 37 | + .map_err(|_| anyhow::anyhow!("failed to close tls connection"))?; |
| 38 | + socket.shutdown(ShutdownType::Send)?; |
| 39 | + let response = tls_input |
| 40 | + .blocking_read_to_end() |
| 41 | + .map_err(|_| anyhow::anyhow!("failed to read output"))?; |
| 42 | + String::from_utf8(response).context("error converting response") |
| 43 | +} |
| 44 | + |
| 45 | +fn test_tls_sample_application() { |
| 46 | + // since this is testing remote endpoint to ensure system cert store works |
| 47 | + // the test uses a couple different endpoints to reduce the number of flakes |
| 48 | + const DOMAINS: &'static [&'static str] = &["example.com", "api.github.com"]; |
42 | 49 |
|
43 |
| - assert!(response.contains("HTTP/1.1 200 OK")); |
| 50 | + for &domain in DOMAINS { |
| 51 | + match make_tls_request(domain) { |
| 52 | + Ok(r) => { |
| 53 | + assert!(r.contains("HTTP/1.1 200 OK")); |
| 54 | + return; |
| 55 | + } |
| 56 | + Err(e) => { |
| 57 | + eprintln!("Failed to make TLS request to {domain}: {e}"); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + panic!("All TLS requests failed."); |
44 | 62 | }
|
45 | 63 |
|
46 | 64 | fn main() {
|
|
0 commit comments