|
|
|
@ -23,6 +23,10 @@ |
|
|
|
|
#include <sys/un.h> |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h> |
|
|
|
|
|
|
|
|
|
#include "absl/strings/match.h" |
|
|
|
|
|
|
|
|
|
#include <grpc/grpc.h> |
|
|
|
|
#include <grpc/support/log.h> |
|
|
|
|
|
|
|
|
@ -39,15 +43,15 @@ static void test_grpc_parse_unix(const char* uri_text, const char* pathname) { |
|
|
|
|
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text); |
|
|
|
|
if (!uri.ok()) { |
|
|
|
|
gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str()); |
|
|
|
|
GPR_ASSERT(uri.ok()); |
|
|
|
|
ASSERT_TRUE(uri.ok()); |
|
|
|
|
} |
|
|
|
|
grpc_resolved_address addr; |
|
|
|
|
|
|
|
|
|
GPR_ASSERT(1 == grpc_parse_uri(*uri, &addr)); |
|
|
|
|
ASSERT_TRUE(grpc_parse_uri(*uri, &addr)); |
|
|
|
|
struct sockaddr_un* addr_un = |
|
|
|
|
reinterpret_cast<struct sockaddr_un*>(addr.addr); |
|
|
|
|
GPR_ASSERT(AF_UNIX == addr_un->sun_family); |
|
|
|
|
GPR_ASSERT(0 == strcmp(addr_un->sun_path, pathname)); |
|
|
|
|
ASSERT_EQ(AF_UNIX, addr_un->sun_family); |
|
|
|
|
ASSERT_STREQ(addr_un->sun_path, pathname); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_grpc_parse_unix_abstract(const char* uri_text, |
|
|
|
@ -56,16 +60,16 @@ static void test_grpc_parse_unix_abstract(const char* uri_text, |
|
|
|
|
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text); |
|
|
|
|
if (!uri.ok()) { |
|
|
|
|
gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str()); |
|
|
|
|
GPR_ASSERT(uri.ok()); |
|
|
|
|
ASSERT_TRUE(uri.ok()); |
|
|
|
|
} |
|
|
|
|
grpc_resolved_address addr; |
|
|
|
|
|
|
|
|
|
GPR_ASSERT(1 == grpc_parse_uri(*uri, &addr)); |
|
|
|
|
ASSERT_TRUE(grpc_parse_uri(*uri, &addr)); |
|
|
|
|
struct sockaddr_un* addr_un = |
|
|
|
|
reinterpret_cast<struct sockaddr_un*>(addr.addr); |
|
|
|
|
GPR_ASSERT(AF_UNIX == addr_un->sun_family); |
|
|
|
|
GPR_ASSERT('\0' == addr_un->sun_path[0]); |
|
|
|
|
GPR_ASSERT(0 == strncmp(addr_un->sun_path + 1, pathname, strlen(pathname))); |
|
|
|
|
ASSERT_EQ(AF_UNIX, addr_un->sun_family); |
|
|
|
|
ASSERT_EQ('\0', addr_un->sun_path[0]); |
|
|
|
|
ASSERT_TRUE(absl::StartsWith(addr_un->sun_path + 1, pathname)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#else /* GRPC_HAVE_UNIX_SOCKET */ |
|
|
|
@ -82,18 +86,18 @@ static void test_grpc_parse_ipv4(const char* uri_text, const char* host, |
|
|
|
|
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text); |
|
|
|
|
if (!uri.ok()) { |
|
|
|
|
gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str()); |
|
|
|
|
GPR_ASSERT(uri.ok()); |
|
|
|
|
ASSERT_TRUE(uri.ok()); |
|
|
|
|
} |
|
|
|
|
grpc_resolved_address addr; |
|
|
|
|
char ntop_buf[GRPC_INET_ADDRSTRLEN]; |
|
|
|
|
|
|
|
|
|
GPR_ASSERT(1 == grpc_parse_ipv4(*uri, &addr)); |
|
|
|
|
ASSERT_TRUE(grpc_parse_ipv4(*uri, &addr)); |
|
|
|
|
grpc_sockaddr_in* addr_in = reinterpret_cast<grpc_sockaddr_in*>(addr.addr); |
|
|
|
|
GPR_ASSERT(GRPC_AF_INET == addr_in->sin_family); |
|
|
|
|
GPR_ASSERT(nullptr != grpc_inet_ntop(GRPC_AF_INET, &addr_in->sin_addr, |
|
|
|
|
ntop_buf, sizeof(ntop_buf))); |
|
|
|
|
GPR_ASSERT(0 == strcmp(ntop_buf, host)); |
|
|
|
|
GPR_ASSERT(grpc_ntohs(addr_in->sin_port) == port); |
|
|
|
|
ASSERT_EQ(GRPC_AF_INET, addr_in->sin_family); |
|
|
|
|
ASSERT_NE(nullptr, grpc_inet_ntop(GRPC_AF_INET, &addr_in->sin_addr, ntop_buf, |
|
|
|
|
sizeof(ntop_buf))); |
|
|
|
|
ASSERT_STREQ(ntop_buf, host); |
|
|
|
|
ASSERT_EQ(grpc_ntohs(addr_in->sin_port), port); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_grpc_parse_ipv6(const char* uri_text, const char* host, |
|
|
|
@ -102,18 +106,18 @@ static void test_grpc_parse_ipv6(const char* uri_text, const char* host, |
|
|
|
|
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text); |
|
|
|
|
if (!uri.ok()) { |
|
|
|
|
gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str()); |
|
|
|
|
GPR_ASSERT(uri.ok()); |
|
|
|
|
ASSERT_TRUE(uri.ok()); |
|
|
|
|
} |
|
|
|
|
grpc_resolved_address addr; |
|
|
|
|
char ntop_buf[GRPC_INET6_ADDRSTRLEN]; |
|
|
|
|
GPR_ASSERT(1 == grpc_parse_ipv6(*uri, &addr)); |
|
|
|
|
ASSERT_TRUE(grpc_parse_ipv6(*uri, &addr)); |
|
|
|
|
grpc_sockaddr_in6* addr_in6 = reinterpret_cast<grpc_sockaddr_in6*>(addr.addr); |
|
|
|
|
GPR_ASSERT(GRPC_AF_INET6 == addr_in6->sin6_family); |
|
|
|
|
GPR_ASSERT(nullptr != grpc_inet_ntop(GRPC_AF_INET6, &addr_in6->sin6_addr, |
|
|
|
|
ntop_buf, sizeof(ntop_buf))); |
|
|
|
|
GPR_ASSERT(0 == strcmp(ntop_buf, host)); |
|
|
|
|
GPR_ASSERT(grpc_ntohs(addr_in6->sin6_port) == port); |
|
|
|
|
GPR_ASSERT(addr_in6->sin6_scope_id == scope_id); |
|
|
|
|
ASSERT_EQ(GRPC_AF_INET6, addr_in6->sin6_family); |
|
|
|
|
ASSERT_NE(nullptr, grpc_inet_ntop(GRPC_AF_INET6, &addr_in6->sin6_addr, |
|
|
|
|
ntop_buf, sizeof(ntop_buf))); |
|
|
|
|
ASSERT_STREQ(ntop_buf, host); |
|
|
|
|
ASSERT_EQ(grpc_ntohs(addr_in6->sin6_port), port); |
|
|
|
|
ASSERT_EQ(addr_in6->sin6_scope_id, scope_id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Test parsing invalid ipv6 addresses (valid uri_text but invalid ipv6 addr) */ |
|
|
|
@ -122,14 +126,13 @@ static void test_grpc_parse_ipv6_invalid(const char* uri_text) { |
|
|
|
|
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(uri_text); |
|
|
|
|
if (!uri.ok()) { |
|
|
|
|
gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str()); |
|
|
|
|
GPR_ASSERT(uri.ok()); |
|
|
|
|
ASSERT_TRUE(uri.ok()); |
|
|
|
|
} |
|
|
|
|
grpc_resolved_address addr; |
|
|
|
|
GPR_ASSERT(!grpc_parse_ipv6(*uri, &addr)); |
|
|
|
|
ASSERT_FALSE(grpc_parse_ipv6(*uri, &addr)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
grpc::testing::TestEnvironment env(&argc, argv); |
|
|
|
|
TEST(ParseAddressTest, MainTest) { |
|
|
|
|
grpc_init(); |
|
|
|
|
|
|
|
|
|
test_grpc_parse_unix("unix:/path/name", "/path/name"); |
|
|
|
@ -145,3 +148,9 @@ int main(int argc, char** argv) { |
|
|
|
|
|
|
|
|
|
grpc_shutdown(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
grpc::testing::TestEnvironment env(&argc, argv); |
|
|
|
|
::testing::InitGoogleTest(&argc, argv); |
|
|
|
|
return RUN_ALL_TESTS(); |
|
|
|
|
} |
|
|
|
|