@@ -194,6 +194,94 @@ fn net_v6_connect() {
194194 assert_eq ! ( request, & response[ ..n] ) ;
195195}
196196
197+ /// Test `connect_unspec`.
198+ #[ test]
199+ fn net_v4_connect_unspec ( ) {
200+ const SOME_PORT : u16 = 47 ;
201+ let localhost_addr = SocketAddrV4 :: new ( Ipv4Addr :: LOCALHOST , SOME_PORT ) ;
202+
203+ let socket = rustix:: net:: socket ( AddressFamily :: INET , SocketType :: DGRAM , None ) . unwrap ( ) ;
204+
205+ rustix:: net:: connect_v4 ( & socket, & localhost_addr) . expect ( "connect_v4" ) ;
206+ assert_eq ! ( getsockname_v4( & socket) . unwrap( ) . ip( ) , & Ipv4Addr :: LOCALHOST ) ;
207+ assert_eq ! ( getpeername_v4( & socket) . unwrap( ) , localhost_addr) ;
208+
209+ match rustix:: net:: connect_unspec ( & socket) {
210+ // BSD platforms return an error even if the socket was disconnected successfully.
211+ #[ cfg( bsd) ]
212+ Err ( rustix:: io:: Errno :: INVAL | rustix:: io:: Errno :: AFNOSUPPORT ) => { }
213+ r => r. expect ( "connect_unspec" ) ,
214+ }
215+ assert_eq ! (
216+ getsockname_v4( & socket) . unwrap( ) . ip( ) ,
217+ & Ipv4Addr :: UNSPECIFIED
218+ ) ;
219+ assert_eq ! ( getpeername_v4( & socket) , Err ( rustix:: io:: Errno :: NOTCONN ) ) ;
220+
221+ rustix:: net:: connect_v4 ( & socket, & localhost_addr) . expect ( "connect_v4" ) ;
222+ assert_eq ! ( getsockname_v4( & socket) . unwrap( ) . ip( ) , & Ipv4Addr :: LOCALHOST ) ;
223+ assert_eq ! ( getpeername_v4( & socket) . unwrap( ) , localhost_addr) ;
224+
225+ fn getsockname_v4 < Fd : rustix:: fd:: AsFd > ( sockfd : Fd ) -> rustix:: io:: Result < SocketAddrV4 > {
226+ match rustix:: net:: getsockname ( sockfd) ? {
227+ SocketAddrAny :: V4 ( addr_v4) => Ok ( addr_v4) ,
228+ _ => Err ( rustix:: io:: Errno :: AFNOSUPPORT ) ,
229+ }
230+ }
231+
232+ fn getpeername_v4 < Fd : rustix:: fd:: AsFd > ( sockfd : Fd ) -> rustix:: io:: Result < SocketAddrV4 > {
233+ match rustix:: net:: getpeername ( sockfd) ? {
234+ Some ( SocketAddrAny :: V4 ( addr_v4) ) => Ok ( addr_v4) ,
235+ None => Err ( rustix:: io:: Errno :: NOTCONN ) ,
236+ _ => Err ( rustix:: io:: Errno :: AFNOSUPPORT ) ,
237+ }
238+ }
239+ }
240+
241+ /// Test `connect_unspec`.
242+ #[ test]
243+ fn net_v6_connect_unspec ( ) {
244+ const SOME_PORT : u16 = 47 ;
245+ let localhost_addr = SocketAddrV6 :: new ( Ipv6Addr :: LOCALHOST , SOME_PORT , 0 , 0 ) ;
246+
247+ let socket = rustix:: net:: socket ( AddressFamily :: INET6 , SocketType :: DGRAM , None ) . unwrap ( ) ;
248+
249+ rustix:: net:: connect_v6 ( & socket, & localhost_addr) . expect ( "connect_v6" ) ;
250+ assert_eq ! ( getsockname_v6( & socket) . unwrap( ) . ip( ) , & Ipv6Addr :: LOCALHOST ) ;
251+ assert_eq ! ( getpeername_v6( & socket) . unwrap( ) , localhost_addr) ;
252+
253+ match rustix:: net:: connect_unspec ( & socket) {
254+ // BSD platforms return an error even if the socket was disconnected successfully.
255+ #[ cfg( bsd) ]
256+ Err ( rustix:: io:: Errno :: INVAL | rustix:: io:: Errno :: AFNOSUPPORT ) => { }
257+ r => r. expect ( "connect_unspec" ) ,
258+ }
259+ assert_eq ! (
260+ getsockname_v6( & socket) . unwrap( ) . ip( ) ,
261+ & Ipv6Addr :: UNSPECIFIED
262+ ) ;
263+ assert_eq ! ( getpeername_v6( & socket) , Err ( rustix:: io:: Errno :: NOTCONN ) ) ;
264+
265+ rustix:: net:: connect_v6 ( & socket, & localhost_addr) . expect ( "connect_v6" ) ;
266+ assert_eq ! ( getsockname_v6( & socket) . unwrap( ) . ip( ) , & Ipv6Addr :: LOCALHOST ) ;
267+ assert_eq ! ( getpeername_v6( & socket) . unwrap( ) , localhost_addr) ;
268+
269+ fn getsockname_v6 < Fd : rustix:: fd:: AsFd > ( sockfd : Fd ) -> rustix:: io:: Result < SocketAddrV6 > {
270+ match rustix:: net:: getsockname ( sockfd) ? {
271+ SocketAddrAny :: V6 ( addr_v6) => Ok ( addr_v6) ,
272+ _ => Err ( rustix:: io:: Errno :: AFNOSUPPORT ) ,
273+ }
274+ }
275+
276+ fn getpeername_v6 < Fd : rustix:: fd:: AsFd > ( sockfd : Fd ) -> rustix:: io:: Result < SocketAddrV6 > {
277+ match rustix:: net:: getpeername ( sockfd) ? {
278+ Some ( SocketAddrAny :: V6 ( addr_v6) ) => Ok ( addr_v6) ,
279+ None => Err ( rustix:: io:: Errno :: NOTCONN ) ,
280+ _ => Err ( rustix:: io:: Errno :: AFNOSUPPORT ) ,
281+ }
282+ }
283+ }
284+
197285/// Test `bind_any`.
198286#[ test]
199287fn net_v4_bind_any ( ) {
0 commit comments