Skip to content

Commit 800454b

Browse files
authored
wasi-tls: Add a second endpoint to make the test less flakey (#10377)
* Add a second endpoint to make the test less flakey Signed-off-by: James Sturtevant <jstur@microsoft.com> * Don't fail if something goes wrong during processing Signed-off-by: James Sturtevant <jstur@microsoft.com> * Fix clippy message Signed-off-by: James Sturtevant <jstur@microsoft.com> --------- Signed-off-by: James Sturtevant <jstur@microsoft.com>
1 parent 03e9799 commit 800454b

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

crates/test-programs/src/bin/tls_sample_application.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,64 @@
1+
use anyhow::{Context, Result};
12
use core::str;
2-
33
use test_programs::wasi::sockets::network::{IpSocketAddress, Network};
44
use test_programs::wasi::sockets::tcp::{ShutdownType, TcpSocket};
55
use test_programs::wasi::tls::types::ClientHandshake;
66

7-
fn test_tls_sample_application() {
7+
fn make_tls_request(domain: &str) -> Result<String> {
88
const PORT: u16 = 443;
9-
const DOMAIN: &'static str = "example.com";
109

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");
1212

1313
let net = Network::default();
1414

1515
let Some(ip) = net
16-
.permissive_blocking_resolve_addresses(DOMAIN)
16+
.permissive_blocking_resolve_addresses(domain)
1717
.unwrap()
1818
.first()
1919
.map(|a| a.to_owned())
2020
else {
21-
eprintln!("DNS lookup failed.");
22-
return;
21+
return Err(anyhow::anyhow!("DNS lookup failed."));
2322
};
2423

2524
let socket = TcpSocket::new(ip.family()).unwrap();
2625
let (tcp_input, tcp_output) = socket
2726
.blocking_connect(&net, IpSocketAddress::new(ip, PORT))
28-
.unwrap();
27+
.context("failed to connect")?;
2928

3029
let (client_connection, tls_input, tls_output) =
31-
ClientHandshake::new(DOMAIN, tcp_input, tcp_output)
30+
ClientHandshake::new(domain, tcp_input, tcp_output)
3231
.blocking_finish()
33-
.unwrap();
32+
.map_err(|_| anyhow::anyhow!("failed to finish handshake"))?;
3433

3534
tls_output.blocking_write_util(request.as_bytes()).unwrap();
3635
client_connection
3736
.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"];
4249

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.");
4462
}
4563

4664
fn main() {

0 commit comments

Comments
 (0)