@ -20,6 +20,9 @@
# include <errno.h>
# include <string.h>
# ifdef GRPC_HAVE_UNIX_SOCKET
# include <sys/un.h>
# endif
# include <gtest/gtest.h>
@ -163,47 +166,94 @@ TEST(SockAddrUtilsTest, SockAddrToString) {
errno = 0x7EADBEEF ;
grpc_resolved_address input4 = MakeAddr4 ( kIPv4 , sizeof ( kIPv4 ) ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input4 , false ) , " 192.0.2.1:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input4 , true ) , " 192.0.2.1:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input4 ) , " ipv4:192.0.2.1:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input4 , false ) . value ( ) , " 192.0.2.1:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input4 , true ) . value ( ) , " 192.0.2.1:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input4 ) . value ( ) , " ipv4:192.0.2.1:12345 " ) ;
grpc_resolved_address input6 = MakeAddr6 ( kIPv6 , sizeof ( kIPv6 ) ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , false ) , " [2001:db8::1]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , true ) , " [2001:db8::1]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6 ) , " ipv6:[2001:db8::1]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , false ) . value ( ) ,
" [2001:db8::1]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , true ) . value ( ) ,
" [2001:db8::1]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6 ) . value ( ) , " ipv6:[2001:db8::1]:12345 " ) ;
SetIPv6ScopeId ( & input6 , 2 ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , false ) , " [2001:db8::1%252]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , true ) , " [2001:db8::1%252]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6 ) , " ipv6:[2001:db8::1%252]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , false ) . value ( ) ,
" [2001:db8::1%252]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , true ) . value ( ) ,
" [2001:db8::1%252]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6 ) . value ( ) ,
" ipv6:[2001:db8::1%252]:12345 " ) ;
SetIPv6ScopeId ( & input6 , 101 ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , false ) ,
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , false ) . value ( ) ,
" [2001:db8::1%25101]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , true ) ,
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6 , true ) . value ( ) ,
" [2001:db8::1%25101]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6 ) , " ipv6:[2001:db8::1%25101]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6 ) . value ( ) ,
" ipv6:[2001:db8::1%25101]:12345 " ) ;
grpc_resolved_address input6x = MakeAddr6 ( kMapped , sizeof ( kMapped ) ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6x , false ) ,
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6x , false ) . value ( ) ,
" [::ffff:192.0.2.1]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6x , true ) , " 192.0.2.1:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6x ) , " ipv4:192.0.2.1:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6x , true ) . value ( ) , " 192.0.2.1:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6x ) . value ( ) , " ipv4:192.0.2.1:12345 " ) ;
grpc_resolved_address input6y =
MakeAddr6 ( kNotQuiteMapped , sizeof ( kNotQuiteMapped ) ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6y , false ) ,
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6y , false ) . value ( ) ,
" [::fffe:c000:263]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6y , true ) , " [::fffe:c000:263]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6y ) , " ipv6:[::fffe:c000:263]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & input6y , true ) . value ( ) ,
" [::fffe:c000:263]:12345 " ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & input6y ) . value ( ) ,
" ipv6:[::fffe:c000:263]:12345 " ) ;
grpc_resolved_address phony ;
memset ( & phony , 0 , sizeof ( phony ) ) ;
grpc_sockaddr * phony_addr = reinterpret_cast < grpc_sockaddr * > ( phony . addr ) ;
phony_addr - > sa_family = 123 ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & phony , false ) , " (sockaddr family=123) " ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & phony , true ) , " (sockaddr family=123) " ) ;
EXPECT_TRUE ( grpc_sockaddr_to_uri ( & phony ) . empty ( ) ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & phony , false ) . status ( ) ,
absl : : InvalidArgumentError ( " Unknown sockaddr family: 123 " ) ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & phony , true ) . status ( ) ,
absl : : InvalidArgumentError ( " Unknown sockaddr family: 123 " ) ) ;
EXPECT_EQ ( grpc_sockaddr_to_uri ( & phony ) . status ( ) ,
absl : : InvalidArgumentError ( " Empty address " ) ) ;
# ifdef GRPC_HAVE_UNIX_SOCKET
grpc_resolved_address inputun ;
struct sockaddr_un * sock_un = reinterpret_cast < struct sockaddr_un * > ( & inputun ) ;
ASSERT_EQ ( grpc_core : : UnixSockaddrPopulate ( " /some/unix/path " , & inputun ) ,
GRPC_ERROR_NONE ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & inputun , true ) . value ( ) , " /some/unix/path " ) ;
std : : string max_filepath ( sizeof ( sock_un - > sun_path ) - 1 , ' x ' ) ;
ASSERT_EQ ( grpc_core : : UnixSockaddrPopulate ( max_filepath , & inputun ) ,
GRPC_ERROR_NONE ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & inputun , true ) . value ( ) , max_filepath ) ;
ASSERT_EQ ( grpc_core : : UnixSockaddrPopulate ( max_filepath , & inputun ) ,
GRPC_ERROR_NONE ) ;
sock_un - > sun_path [ sizeof ( sockaddr_un : : sun_path ) - 1 ] = ' x ' ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & inputun , true ) . status ( ) ,
absl : : InvalidArgumentError ( " UDS path is not null-terminated " ) ) ;
ASSERT_EQ ( grpc_core : : UnixAbstractSockaddrPopulate ( " some_unix_path " , & inputun ) ,
GRPC_ERROR_NONE ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & inputun , true ) . value ( ) ,
absl : : StrCat ( std : : string ( 1 , ' \0 ' ) , " some_unix_path " ) ) ;
std : : string max_abspath ( sizeof ( sock_un - > sun_path ) - 1 , ' \0 ' ) ;
ASSERT_EQ ( grpc_core : : UnixAbstractSockaddrPopulate ( max_abspath , & inputun ) ,
GRPC_ERROR_NONE ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & inputun , true ) . value ( ) ,
absl : : StrCat ( std : : string ( 1 , ' \0 ' ) , max_abspath ) ) ;
ASSERT_EQ ( grpc_core : : UnixAbstractSockaddrPopulate ( " " , & inputun ) ,
GRPC_ERROR_NONE ) ;
inputun . len = sizeof ( sock_un - > sun_family ) ;
EXPECT_EQ ( grpc_sockaddr_to_string ( & inputun , true ) . status ( ) ,
absl : : InvalidArgumentError ( " empty UDS abstract path " ) ) ;
# endif
}
TEST ( SockAddrUtilsTest , SockAddrSetGetPort ) {