@@ -9,12 +9,10 @@ test_programs::p3::export!(Component);
99const SOME_PORT : u16 = 47 ; // If the tests pass, this will never actually be connected to.
1010
1111fn test_udp_connect_disconnect_reconnect ( family : IpAddressFamily ) {
12- let unspecified_addr = IpSocketAddress :: new ( IpAddress :: new_unspecified ( family) , 0 ) ;
1312 let remote1 = IpSocketAddress :: new ( IpAddress :: new_loopback ( family) , 4321 ) ;
1413 let remote2 = IpSocketAddress :: new ( IpAddress :: new_loopback ( family) , 4320 ) ;
1514
1615 let client = UdpSocket :: create ( family) . unwrap ( ) ;
17- client. bind ( unspecified_addr) . unwrap ( ) ;
1816
1917 assert_eq ! ( client. disconnect( ) , Err ( ErrorCode :: InvalidState ) ) ;
2018 assert_eq ! ( client. get_remote_address( ) , Err ( ErrorCode :: InvalidState ) ) ;
@@ -43,7 +41,6 @@ fn test_udp_connect_unspec(family: IpAddressFamily) {
4341 let ip = IpAddress :: new_unspecified ( family) ;
4442 let addr = IpSocketAddress :: new ( ip, SOME_PORT ) ;
4543 let sock = UdpSocket :: create ( family) . unwrap ( ) ;
46- sock. bind_unspecified ( ) . unwrap ( ) ;
4744
4845 assert ! ( matches!(
4946 sock. connect( addr) ,
@@ -55,7 +52,6 @@ fn test_udp_connect_unspec(family: IpAddressFamily) {
5552fn test_udp_connect_port_0 ( family : IpAddressFamily ) {
5653 let addr = IpSocketAddress :: new ( IpAddress :: new_loopback ( family) , 0 ) ;
5754 let sock = UdpSocket :: create ( family) . unwrap ( ) ;
58- sock. bind_unspecified ( ) . unwrap ( ) ;
5955
6056 assert ! ( matches!(
6157 sock. connect( addr) ,
@@ -72,14 +68,37 @@ fn test_udp_connect_wrong_family(family: IpAddressFamily) {
7268 let remote_addr = IpSocketAddress :: new ( wrong_ip, SOME_PORT ) ;
7369
7470 let sock = UdpSocket :: create ( family) . unwrap ( ) ;
75- sock. bind_unspecified ( ) . unwrap ( ) ;
7671
7772 assert ! ( matches!(
7873 sock. connect( remote_addr) ,
7974 Err ( ErrorCode :: InvalidArgument )
8075 ) ) ;
8176}
8277
78+ /// Connect should perform implicit bind.
79+ fn test_udp_connect_without_bind ( family : IpAddressFamily ) {
80+ let remote_addr = IpSocketAddress :: new ( IpAddress :: new_loopback ( family) , SOME_PORT ) ;
81+
82+ let sock = UdpSocket :: create ( family) . unwrap ( ) ;
83+
84+ assert ! ( matches!( sock. get_local_address( ) , Err ( _) ) ) ;
85+ assert ! ( matches!( sock. connect( remote_addr) , Ok ( _) ) ) ;
86+ assert ! ( matches!( sock. get_local_address( ) , Ok ( _) ) ) ;
87+ }
88+
89+ /// Connect should work in combination with an explicit bind.
90+ fn test_udp_connect_with_bind ( family : IpAddressFamily ) {
91+ let remote_addr = IpSocketAddress :: new ( IpAddress :: new_loopback ( family) , SOME_PORT ) ;
92+
93+ let sock = UdpSocket :: create ( family) . unwrap ( ) ;
94+
95+ sock. bind_unspecified ( ) . unwrap ( ) ;
96+
97+ assert ! ( matches!( sock. get_local_address( ) , Ok ( _) ) ) ;
98+ assert ! ( matches!( sock. connect( remote_addr) , Ok ( _) ) ) ;
99+ assert ! ( matches!( sock. get_local_address( ) , Ok ( _) ) ) ;
100+ }
101+
83102fn test_udp_connect_dual_stack ( ) {
84103 // Set-up:
85104 let v4_server = UdpSocket :: create ( IpAddressFamily :: Ipv4 ) . unwrap ( ) ;
@@ -123,6 +142,12 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
123142 test_udp_connect_wrong_family ( IpAddressFamily :: Ipv4 ) ;
124143 test_udp_connect_wrong_family ( IpAddressFamily :: Ipv6 ) ;
125144
145+ test_udp_connect_without_bind ( IpAddressFamily :: Ipv4 ) ;
146+ test_udp_connect_without_bind ( IpAddressFamily :: Ipv6 ) ;
147+
148+ test_udp_connect_with_bind ( IpAddressFamily :: Ipv4 ) ;
149+ test_udp_connect_with_bind ( IpAddressFamily :: Ipv6 ) ;
150+
126151 test_udp_connect_dual_stack ( ) ;
127152 Ok ( ( ) )
128153 }
0 commit comments