|
| 1 | +use futures::{stream, try_join, SinkExt as _, StreamExt as _}; |
| 2 | +use test_programs::p3::wasi::sockets::types::{ |
| 3 | + ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, TcpSocket, |
| 4 | +}; |
| 5 | +use test_programs::p3::{sockets::attempt_random_port, wit_stream}; |
| 6 | +use wit_bindgen_rt::async_support::StreamReader; |
| 7 | + |
| 8 | +struct Component; |
| 9 | + |
| 10 | +test_programs::p3::export!(Component); |
| 11 | + |
| 12 | +/// Bind a socket and let the system determine a port. |
| 13 | +fn test_tcp_bind_ephemeral_port(ip: IpAddress) { |
| 14 | + let bind_addr = IpSocketAddress::new(ip, 0); |
| 15 | + |
| 16 | + let sock = TcpSocket::new(ip.family()); |
| 17 | + sock.bind(bind_addr).unwrap(); |
| 18 | + |
| 19 | + let bound_addr = sock.local_address().unwrap(); |
| 20 | + |
| 21 | + assert_eq!(bind_addr.ip(), bound_addr.ip()); |
| 22 | + assert_ne!(bind_addr.port(), bound_addr.port()); |
| 23 | +} |
| 24 | + |
| 25 | +/// Bind a socket on a specified port. |
| 26 | +fn test_tcp_bind_specific_port(ip: IpAddress) { |
| 27 | + let sock = TcpSocket::new(ip.family()); |
| 28 | + |
| 29 | + let bind_addr = attempt_random_port(ip, |bind_addr| sock.bind(bind_addr)).unwrap(); |
| 30 | + |
| 31 | + let bound_addr = sock.local_address().unwrap(); |
| 32 | + |
| 33 | + assert_eq!(bind_addr.ip(), bound_addr.ip()); |
| 34 | + assert_eq!(bind_addr.port(), bound_addr.port()); |
| 35 | +} |
| 36 | + |
| 37 | +/// Two sockets may not be actively bound to the same address at the same time. |
| 38 | +fn test_tcp_bind_addrinuse(ip: IpAddress) { |
| 39 | + let bind_addr = IpSocketAddress::new(ip, 0); |
| 40 | + |
| 41 | + let sock1 = TcpSocket::new(ip.family()); |
| 42 | + sock1.bind(bind_addr).unwrap(); |
| 43 | + sock1.listen().unwrap(); |
| 44 | + |
| 45 | + let bound_addr = sock1.local_address().unwrap(); |
| 46 | + |
| 47 | + let sock2 = TcpSocket::new(ip.family()); |
| 48 | + assert_eq!(sock2.bind(bound_addr), Err(ErrorCode::AddressInUse)); |
| 49 | +} |
| 50 | + |
| 51 | +// The WASI runtime should set SO_REUSEADDR for us |
| 52 | +async fn test_tcp_bind_reuseaddr(ip: IpAddress) { |
| 53 | + let client = TcpSocket::new(ip.family()); |
| 54 | + |
| 55 | + let bind_addr = { |
| 56 | + let listener1 = TcpSocket::new(ip.family()); |
| 57 | + |
| 58 | + let bind_addr = attempt_random_port(ip, |bind_addr| listener1.bind(bind_addr)).unwrap(); |
| 59 | + |
| 60 | + let mut accept = listener1.listen().unwrap(); |
| 61 | + |
| 62 | + let connect_addr = |
| 63 | + IpSocketAddress::new(IpAddress::new_loopback(ip.family()), bind_addr.port()); |
| 64 | + client.connect(connect_addr).unwrap(); |
| 65 | + |
| 66 | + let mut sock = accept.next().await.unwrap(); |
| 67 | + assert_eq!(sock.len(), 1); |
| 68 | + let sock = sock.pop().unwrap(); |
| 69 | + let (mut data_tx, data_rx) = wit_stream::new(); |
| 70 | + sock.send(data_rx).unwrap(); |
| 71 | + data_tx.send(vec![0; 10]).await.unwrap(); |
| 72 | + |
| 73 | + bind_addr |
| 74 | + }; |
| 75 | + |
| 76 | + { |
| 77 | + let listener2 = TcpSocket::new(ip.family()); |
| 78 | + |
| 79 | + // If SO_REUSEADDR was configured correctly, the following lines shouldn't be |
| 80 | + // affected by the TIME_WAIT state of the just closed `listener1` socket: |
| 81 | + listener2.bind(bind_addr).unwrap(); |
| 82 | + listener2.listen().unwrap(); |
| 83 | + } |
| 84 | + |
| 85 | + drop(client); |
| 86 | +} |
| 87 | + |
| 88 | +// Try binding to an address that is not configured on the system. |
| 89 | +fn test_tcp_bind_addrnotavail(ip: IpAddress) { |
| 90 | + let bind_addr = IpSocketAddress::new(ip, 0); |
| 91 | + |
| 92 | + let sock = TcpSocket::new(ip.family()); |
| 93 | + |
| 94 | + assert_eq!(sock.bind(bind_addr), Err(ErrorCode::AddressNotBindable)); |
| 95 | +} |
| 96 | + |
| 97 | +/// Bind should validate the address family. |
| 98 | +fn test_tcp_bind_wrong_family(family: IpAddressFamily) { |
| 99 | + let wrong_ip = match family { |
| 100 | + IpAddressFamily::Ipv4 => IpAddress::IPV6_LOOPBACK, |
| 101 | + IpAddressFamily::Ipv6 => IpAddress::IPV4_LOOPBACK, |
| 102 | + }; |
| 103 | + |
| 104 | + let sock = TcpSocket::new(family); |
| 105 | + let result = sock.bind(IpSocketAddress::new(wrong_ip, 0)); |
| 106 | + |
| 107 | + assert!(matches!(result, Err(ErrorCode::InvalidArgument))); |
| 108 | +} |
| 109 | + |
| 110 | +/// Bind only works on unicast addresses. |
| 111 | +fn test_tcp_bind_non_unicast() { |
| 112 | + let ipv4_broadcast = IpSocketAddress::new(IpAddress::IPV4_BROADCAST, 0); |
| 113 | + let ipv4_multicast = IpSocketAddress::new(IpAddress::Ipv4((224, 254, 0, 0)), 0); |
| 114 | + let ipv6_multicast = IpSocketAddress::new(IpAddress::Ipv6((0xff00, 0, 0, 0, 0, 0, 0, 0)), 0); |
| 115 | + |
| 116 | + let sock_v4 = TcpSocket::new(IpAddressFamily::Ipv4); |
| 117 | + let sock_v6 = TcpSocket::new(IpAddressFamily::Ipv6); |
| 118 | + |
| 119 | + assert!(matches!( |
| 120 | + sock_v4.bind(ipv4_broadcast), |
| 121 | + Err(ErrorCode::InvalidArgument) |
| 122 | + )); |
| 123 | + assert!(matches!( |
| 124 | + sock_v4.bind(ipv4_multicast), |
| 125 | + Err(ErrorCode::InvalidArgument) |
| 126 | + )); |
| 127 | + assert!(matches!( |
| 128 | + sock_v6.bind(ipv6_multicast), |
| 129 | + Err(ErrorCode::InvalidArgument) |
| 130 | + )); |
| 131 | +} |
| 132 | + |
| 133 | +fn test_tcp_bind_dual_stack() { |
| 134 | + let sock = TcpSocket::new(IpAddressFamily::Ipv6); |
| 135 | + let addr = IpSocketAddress::new(IpAddress::IPV4_MAPPED_LOOPBACK, 0); |
| 136 | + |
| 137 | + // Binding an IPv4-mapped-IPv6 address on a ipv6-only socket should fail: |
| 138 | + assert!(matches!(sock.bind(addr), Err(ErrorCode::InvalidArgument))); |
| 139 | +} |
| 140 | + |
| 141 | +impl test_programs::p3::exports::wasi::cli::run::Guest for Component { |
| 142 | + async fn run() -> Result<(), ()> { |
| 143 | + const RESERVED_IPV4_ADDRESS: IpAddress = IpAddress::Ipv4((192, 0, 2, 0)); // Reserved for documentation and examples. |
| 144 | + const RESERVED_IPV6_ADDRESS: IpAddress = |
| 145 | + IpAddress::Ipv6((0x2001, 0x0db8, 0, 0, 0, 0, 0, 0)); // Reserved for documentation and examples. |
| 146 | + |
| 147 | + test_tcp_bind_ephemeral_port(IpAddress::IPV4_LOOPBACK); |
| 148 | + test_tcp_bind_ephemeral_port(IpAddress::IPV6_LOOPBACK); |
| 149 | + test_tcp_bind_ephemeral_port(IpAddress::IPV4_UNSPECIFIED); |
| 150 | + test_tcp_bind_ephemeral_port(IpAddress::IPV6_UNSPECIFIED); |
| 151 | + |
| 152 | + test_tcp_bind_specific_port(IpAddress::IPV4_LOOPBACK); |
| 153 | + test_tcp_bind_specific_port(IpAddress::IPV6_LOOPBACK); |
| 154 | + test_tcp_bind_specific_port(IpAddress::IPV4_UNSPECIFIED); |
| 155 | + test_tcp_bind_specific_port(IpAddress::IPV6_UNSPECIFIED); |
| 156 | + |
| 157 | + test_tcp_bind_reuseaddr(IpAddress::IPV4_LOOPBACK).await; |
| 158 | + test_tcp_bind_reuseaddr(IpAddress::IPV6_LOOPBACK).await; |
| 159 | + |
| 160 | + test_tcp_bind_addrinuse(IpAddress::IPV4_LOOPBACK); |
| 161 | + test_tcp_bind_addrinuse(IpAddress::IPV6_LOOPBACK); |
| 162 | + test_tcp_bind_addrinuse(IpAddress::IPV4_UNSPECIFIED); |
| 163 | + test_tcp_bind_addrinuse(IpAddress::IPV6_UNSPECIFIED); |
| 164 | + |
| 165 | + test_tcp_bind_addrnotavail(RESERVED_IPV4_ADDRESS); |
| 166 | + test_tcp_bind_addrnotavail(RESERVED_IPV6_ADDRESS); |
| 167 | + |
| 168 | + test_tcp_bind_wrong_family(IpAddressFamily::Ipv4); |
| 169 | + test_tcp_bind_wrong_family(IpAddressFamily::Ipv6); |
| 170 | + |
| 171 | + test_tcp_bind_non_unicast(); |
| 172 | + |
| 173 | + test_tcp_bind_dual_stack(); |
| 174 | + |
| 175 | + Ok(()) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +fn main() {} |
0 commit comments