|
1 | | -use std::time::Duration; |
2 | | - |
3 | | -use test_programs::wasi::io::streams::StreamError; |
| 1 | +use test_programs::wasi::io::streams::{InputStream, OutputStream, StreamError}; |
4 | 2 | use test_programs::wasi::sockets::network::{IpAddress, IpAddressFamily, IpSocketAddress, Network}; |
5 | 3 | use test_programs::wasi::sockets::tcp::{ShutdownType, TcpSocket}; |
6 | 4 |
|
7 | 5 | /// InputStream::read should return `StreamError::Closed` after the connection has been shut down by the server. |
8 | | -fn test_tcp_read_from_closed_input_stream(net: &Network, family: IpAddressFamily) { |
9 | | - // Set up server & client sockets: |
| 6 | +fn test_tcp_input_stream_should_be_closed_by_remote_shutdown( |
| 7 | + net: &Network, |
| 8 | + family: IpAddressFamily, |
| 9 | +) { |
| 10 | + setup(net, family, |server, client| { |
| 11 | + // Shut down the connection from the server side: |
| 12 | + server.socket.shutdown(ShutdownType::Both).unwrap(); |
| 13 | + drop(server); |
| 14 | + |
| 15 | + // Wait for the shutdown signal to reach the client: |
| 16 | + client.input.subscribe().block(); |
| 17 | + |
| 18 | + // The input stream should immediately signal StreamError::Closed. |
| 19 | + // Notably, it should _not_ return an empty list (the wasi-io equivalent of EWOULDBLOCK) |
| 20 | + // See: https://github.com/bytecodealliance/wasmtime/pull/8968 |
| 21 | + assert!(matches!(client.input.read(10), Err(StreamError::Closed))); |
| 22 | + |
| 23 | + // Stream should still be closed, even when requesting 0 bytes: |
| 24 | + assert!(matches!(client.input.read(0), Err(StreamError::Closed))); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +/// OutputStream should return `StreamError::Closed` after the connection has been locally shut down for sending. |
| 29 | +fn test_tcp_output_stream_should_be_closed_by_local_shutdown( |
| 30 | + net: &Network, |
| 31 | + family: IpAddressFamily, |
| 32 | +) { |
| 33 | + setup(net, family, |_server, client| { |
| 34 | + let message = b"Hi!"; |
| 35 | + |
| 36 | + // The stream should be writable: |
| 37 | + assert!(client.output.check_write().unwrap() as usize >= message.len()); |
| 38 | + |
| 39 | + // Perform the shutdown |
| 40 | + client.socket.shutdown(ShutdownType::Send).unwrap(); |
| 41 | + |
| 42 | + // Stream should be closed: |
| 43 | + assert!(matches!( |
| 44 | + client.output.write(message), |
| 45 | + Err(StreamError::Closed) |
| 46 | + )); |
| 47 | + |
| 48 | + // The stream should remain closed: |
| 49 | + assert!(matches!( |
| 50 | + client.output.check_write(), |
| 51 | + Err(StreamError::Closed) |
| 52 | + )); |
| 53 | + assert!(matches!(client.output.flush(), Err(StreamError::Closed))); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +fn main() { |
| 58 | + let net = Network::default(); |
| 59 | + |
| 60 | + test_tcp_input_stream_should_be_closed_by_remote_shutdown(&net, IpAddressFamily::Ipv4); |
| 61 | + test_tcp_input_stream_should_be_closed_by_remote_shutdown(&net, IpAddressFamily::Ipv6); |
| 62 | + |
| 63 | + test_tcp_output_stream_should_be_closed_by_local_shutdown(&net, IpAddressFamily::Ipv4); |
| 64 | + test_tcp_output_stream_should_be_closed_by_local_shutdown(&net, IpAddressFamily::Ipv6); |
| 65 | +} |
| 66 | + |
| 67 | +struct Connection { |
| 68 | + input: InputStream, |
| 69 | + output: OutputStream, |
| 70 | + socket: TcpSocket, |
| 71 | +} |
| 72 | + |
| 73 | +/// Set up a connected pair of sockets |
| 74 | +fn setup(net: &Network, family: IpAddressFamily, body: impl FnOnce(Connection, Connection)) { |
10 | 75 | let bind_address = IpSocketAddress::new(IpAddress::new_loopback(family), 0); |
11 | 76 | let listener = TcpSocket::new(family).unwrap(); |
12 | 77 | listener.blocking_bind(&net, bind_address).unwrap(); |
13 | 78 | listener.blocking_listen().unwrap(); |
14 | 79 | let bound_address = listener.local_address().unwrap(); |
15 | | - let client = TcpSocket::new(family).unwrap(); |
16 | | - let (connected_input, connected_output) = client.blocking_connect(net, bound_address).unwrap(); |
17 | | - let (accepted, accepted_input, accepted_output) = listener.blocking_accept().unwrap(); |
18 | | - |
19 | | - // Shut down the connection from the server side and give the kernel a bit |
20 | | - // of time to propagate the shutdown signal from the server socket to the |
21 | | - // client socket. |
22 | | - accepted.shutdown(ShutdownType::Both).unwrap(); |
23 | | - drop(accepted_input); |
24 | | - drop(accepted_output); |
25 | | - drop(accepted); |
26 | | - std::thread::sleep(Duration::from_millis(50)); |
27 | | - |
28 | | - // And now the actual test: |
29 | | - |
30 | | - // The input stream should immediately signal StreamError::Closed. |
31 | | - // Notably, it should _not_ return an empty list (the wasi-io equivalent of EWOULDBLOCK) |
32 | | - // See: https://github.com/bytecodealliance/wasmtime/pull/8968 |
33 | | - assert!(matches!(connected_input.read(10), Err(StreamError::Closed))); // If this randomly fails, try tweaking the timeout above. |
34 | | - |
35 | | - // Stream should still be closed, even when requesting 0 bytes: |
36 | | - assert!(matches!(connected_input.read(0), Err(StreamError::Closed))); |
37 | | - |
38 | | - drop(connected_input); |
39 | | - drop(connected_output); |
40 | | - drop(client); |
41 | | -} |
42 | | - |
43 | | -fn main() { |
44 | | - let net = Network::default(); |
| 80 | + let client_socket = TcpSocket::new(family).unwrap(); |
| 81 | + let (client_input, client_output) = client_socket.blocking_connect(net, bound_address).unwrap(); |
| 82 | + let (accepted_socket, accepted_input, accepted_output) = listener.blocking_accept().unwrap(); |
45 | 83 |
|
46 | | - test_tcp_read_from_closed_input_stream(&net, IpAddressFamily::Ipv4); |
47 | | - test_tcp_read_from_closed_input_stream(&net, IpAddressFamily::Ipv6); |
| 84 | + body( |
| 85 | + Connection { |
| 86 | + input: accepted_input, |
| 87 | + output: accepted_output, |
| 88 | + socket: accepted_socket, |
| 89 | + }, |
| 90 | + Connection { |
| 91 | + input: client_input, |
| 92 | + output: client_output, |
| 93 | + socket: client_socket, |
| 94 | + }, |
| 95 | + ); |
48 | 96 | } |
0 commit comments