Skip to content

Commit a9b76e3

Browse files
fixup! integration tests
1 parent 48d832b commit a9b76e3

6 files changed

Lines changed: 497 additions & 88 deletions

File tree

spec/fixtures/wasi-network-p2.wasm

222 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target = "wasm32-wasip1"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "wasi-network"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[workspace]
9+
10+
[dependencies]
11+
miniserde = "0.1.27"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Example WASI program used to test the network integration.
2+
3+
Tests TCP connections, UDP sockets, and DNS resolution. This is only supported
4+
using WASI preview 2.
5+
6+
To update:
7+
8+
```shell
9+
cargo build --target=wasm32-wasip2 --release && \
10+
cp target/wasm32-wasip2/release/wasi-network.wasm \
11+
../wasi-network-p2.wasm
12+
```
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
use miniserde::{json, Serialize};
2+
use std::io::{Write, Read};
3+
use std::net::{TcpStream, UdpSocket, ToSocketAddrs};
4+
use std::time::Duration;
5+
6+
#[derive(Serialize)]
7+
struct TestResult<'a> {
8+
test_type: &'a str,
9+
success: bool,
10+
message: String,
11+
}
12+
13+
fn main() {
14+
let args: Vec<String> = std::env::args().collect();
15+
16+
if args.len() < 2 {
17+
eprintln!("Usage: wasi-network <test_type> [args...]");
18+
eprintln!(" test_type: tcp, udp, dns");
19+
std::process::exit(1);
20+
}
21+
22+
let test_type = &args[1];
23+
let result = match test_type.as_str() {
24+
"tcp" => test_tcp(&args),
25+
"udp" => test_udp(&args),
26+
"dns" => test_dns(&args),
27+
_ => TestResult {
28+
test_type: test_type.as_str(),
29+
success: false,
30+
message: format!("Unknown test type: {}", test_type),
31+
},
32+
};
33+
34+
std::io::stdout().write_all(json::to_string(&result).as_bytes())
35+
.expect("failed to write to stdout");
36+
}
37+
38+
fn test_tcp(args: &[String]) -> TestResult<'static> {
39+
if args.len() < 4 {
40+
return TestResult {
41+
test_type: "tcp",
42+
success: false,
43+
message: "Usage: wasi-network tcp <host> <port>".to_string(),
44+
};
45+
}
46+
47+
let host = &args[2];
48+
let port = &args[3];
49+
let addr = format!("{}:{}", host, port);
50+
51+
match TcpStream::connect_timeout(
52+
&addr.parse().expect("failed to parse address"),
53+
Duration::from_secs(2)
54+
) {
55+
Ok(mut stream) => {
56+
// Try to send and receive some data
57+
match stream.write_all(b"HELLO") {
58+
Ok(_) => {
59+
let mut buf = [0u8; 1024];
60+
match stream.read(&mut buf) {
61+
Ok(n) if n > 0 => {
62+
TestResult {
63+
test_type: "tcp",
64+
success: true,
65+
message: format!("Connected to {} and exchanged data", addr),
66+
}
67+
}
68+
Ok(_) => {
69+
TestResult {
70+
test_type: "tcp",
71+
success: true,
72+
message: format!("Connected to {} (no response)", addr),
73+
}
74+
}
75+
Err(e) => {
76+
TestResult {
77+
test_type: "tcp",
78+
success: false,
79+
message: format!("Failed to read from {}: {}", addr, e),
80+
}
81+
}
82+
}
83+
}
84+
Err(e) => {
85+
TestResult {
86+
test_type: "tcp",
87+
success: false,
88+
message: format!("Failed to write to {}: {}", addr, e),
89+
}
90+
}
91+
}
92+
}
93+
Err(e) => {
94+
TestResult {
95+
test_type: "tcp",
96+
success: false,
97+
message: format!("Failed to connect to {}: {}", addr, e),
98+
}
99+
}
100+
}
101+
}
102+
103+
fn test_udp(args: &[String]) -> TestResult<'static> {
104+
if args.len() < 4 {
105+
return TestResult {
106+
test_type: "udp",
107+
success: false,
108+
message: "Usage: wasi-network udp <host> <port>".to_string(),
109+
};
110+
}
111+
112+
let host = &args[2];
113+
let port = &args[3];
114+
let addr = format!("{}:{}", host, port);
115+
116+
match UdpSocket::bind("0.0.0.0:0") {
117+
Ok(socket) => {
118+
match socket.send_to(b"HELLO", &addr) {
119+
Ok(_) => {
120+
let mut buf = [0u8; 1024];
121+
match socket.recv_from(&mut buf) {
122+
Ok((n, _)) if n > 0 => {
123+
TestResult {
124+
test_type: "udp",
125+
success: true,
126+
message: format!("Sent to {} and received response", addr),
127+
}
128+
}
129+
Ok(_) => {
130+
TestResult {
131+
test_type: "udp",
132+
success: true,
133+
message: format!("Sent to {} (no response)", addr),
134+
}
135+
}
136+
Err(e) => {
137+
TestResult {
138+
test_type: "udp",
139+
success: false,
140+
message: format!("Failed to receive from {}: {}", addr, e),
141+
}
142+
}
143+
}
144+
}
145+
Err(e) => {
146+
TestResult {
147+
test_type: "udp",
148+
success: false,
149+
message: format!("Failed to send to {}: {}", addr, e),
150+
}
151+
}
152+
}
153+
}
154+
Err(e) => {
155+
TestResult {
156+
test_type: "udp",
157+
success: false,
158+
message: format!("Failed to bind UDP socket: {}", e),
159+
}
160+
}
161+
}
162+
}
163+
164+
fn test_dns(args: &[String]) -> TestResult<'static> {
165+
if args.len() < 3 {
166+
return TestResult {
167+
test_type: "dns",
168+
success: false,
169+
message: "Usage: wasi-network dns <hostname>".to_string(),
170+
};
171+
}
172+
173+
let hostname = &args[2];
174+
let addr_with_port = format!("{}:80", hostname);
175+
176+
match addr_with_port.to_socket_addrs() {
177+
Ok(mut addrs) => {
178+
if let Some(addr) = addrs.next() {
179+
TestResult {
180+
test_type: "dns",
181+
success: true,
182+
message: format!("Resolved {} to {}", hostname, addr.ip()),
183+
}
184+
} else {
185+
TestResult {
186+
test_type: "dns",
187+
success: false,
188+
message: format!("No addresses found for {}", hostname),
189+
}
190+
}
191+
}
192+
Err(e) => {
193+
TestResult {
194+
test_type: "dns",
195+
success: false,
196+
message: format!("Failed to resolve {}: {}", hostname, e),
197+
}
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)