gtestify test/core/address_utils (#30193)

* Fix

* Fix

* Fix

* Fix
pull/30233/head
Esun Kim 2 years ago committed by GitHub
parent 7565f58a39
commit 87c0bb1a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 136
      CMakeLists.txt
  2. 44
      build_autogenerated.yaml
  3. 5
      test/core/address_utils/BUILD
  4. 65
      test/core/address_utils/parse_address_test.cc
  5. 38
      test/core/address_utils/parse_address_with_named_scope_id_test.cc
  6. 92
      tools/run_tests/generated/tests.json

136
CMakeLists.txt generated

@ -860,10 +860,6 @@ if(gRPC_BUILD_TESTS)
add_dependencies(buildtests_c multiple_server_queues_test)
add_dependencies(buildtests_c murmur_hash_test)
add_dependencies(buildtests_c no_server_test)
add_dependencies(buildtests_c parse_address_test)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
add_dependencies(buildtests_c parse_address_with_named_scope_id_test)
endif()
add_dependencies(buildtests_c parser_test)
add_dependencies(buildtests_c percent_encoding_test)
add_dependencies(buildtests_c public_headers_must_be_c89)
@ -1096,6 +1092,10 @@ if(gRPC_BUILD_TESTS)
add_dependencies(buildtests_cxx orphanable_test)
add_dependencies(buildtests_cxx out_of_bounds_bad_client_test)
add_dependencies(buildtests_cxx overload_test)
add_dependencies(buildtests_cxx parse_address_test)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
add_dependencies(buildtests_cxx parse_address_with_named_scope_id_test)
endif()
add_dependencies(buildtests_cxx parsed_metadata_test)
add_dependencies(buildtests_cxx periodic_update_test)
add_dependencies(buildtests_cxx pid_controller_test)
@ -5989,62 +5989,6 @@ target_link_libraries(no_server_test
endif()
if(gRPC_BUILD_TESTS)
add_executable(parse_address_test
test/core/address_utils/parse_address_test.cc
)
target_include_directories(parse_address_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
)
target_link_libraries(parse_address_test
${_gRPC_ALLTARGETS_LIBRARIES}
grpc_test_util
)
endif()
if(gRPC_BUILD_TESTS)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
add_executable(parse_address_with_named_scope_id_test
test/core/address_utils/parse_address_with_named_scope_id_test.cc
)
target_include_directories(parse_address_with_named_scope_id_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
)
target_link_libraries(parse_address_with_named_scope_id_test
${_gRPC_ALLTARGETS_LIBRARIES}
grpc_test_util
)
endif()
endif()
if(gRPC_BUILD_TESTS)
add_executable(parser_test
test/core/end2end/data/client_certs.cc
test/core/end2end/data/server1_cert.cc
@ -13722,6 +13666,78 @@ target_link_libraries(overload_test
endif()
if(gRPC_BUILD_TESTS)
add_executable(parse_address_test
test/core/address_utils/parse_address_test.cc
third_party/googletest/googletest/src/gtest-all.cc
third_party/googletest/googlemock/src/gmock-all.cc
)
target_include_directories(parse_address_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
third_party/googletest/googletest/include
third_party/googletest/googletest
third_party/googletest/googlemock/include
third_party/googletest/googlemock
${_gRPC_PROTO_GENS_DIR}
)
target_link_libraries(parse_address_test
${_gRPC_PROTOBUF_LIBRARIES}
${_gRPC_ALLTARGETS_LIBRARIES}
grpc_test_util
)
endif()
if(gRPC_BUILD_TESTS)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
add_executable(parse_address_with_named_scope_id_test
test/core/address_utils/parse_address_with_named_scope_id_test.cc
third_party/googletest/googletest/src/gtest-all.cc
third_party/googletest/googlemock/src/gmock-all.cc
)
target_include_directories(parse_address_with_named_scope_id_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
third_party/googletest/googletest/include
third_party/googletest/googletest
third_party/googletest/googlemock/include
third_party/googletest/googlemock
${_gRPC_PROTO_GENS_DIR}
)
target_link_libraries(parse_address_with_named_scope_id_test
${_gRPC_PROTOBUF_LIBRARIES}
${_gRPC_ALLTARGETS_LIBRARIES}
grpc_test_util
)
endif()
endif()
if(gRPC_BUILD_TESTS)
add_executable(parsed_metadata_test
test/core/transport/parsed_metadata_test.cc
third_party/googletest/googletest/src/gtest-all.cc

@ -3818,27 +3818,6 @@ targets:
- test/core/end2end/no_server_test.cc
deps:
- grpc_test_util
- name: parse_address_test
build: test
language: c
headers: []
src:
- test/core/address_utils/parse_address_test.cc
deps:
- grpc_test_util
- name: parse_address_with_named_scope_id_test
build: test
language: c
headers: []
src:
- test/core/address_utils/parse_address_with_named_scope_id_test.cc
deps:
- grpc_test_util
platforms:
- linux
- posix
- mac
uses_polling: false
- name: parser_test
build: test
language: c
@ -7179,6 +7158,29 @@ targets:
- test/core/gprpp/overload_test.cc
deps: []
uses_polling: false
- name: parse_address_test
gtest: true
build: test
language: c++
headers: []
src:
- test/core/address_utils/parse_address_test.cc
deps:
- grpc_test_util
- name: parse_address_with_named_scope_id_test
gtest: true
build: test
language: c++
headers: []
src:
- test/core/address_utils/parse_address_with_named_scope_id_test.cc
deps:
- grpc_test_util
platforms:
- linux
- posix
- mac
uses_polling: false
- name: parsed_metadata_test
gtest: true
build: test

@ -39,6 +39,10 @@ grpc_cc_test(
grpc_cc_test(
name = "parse_address_test",
srcs = ["parse_address_test.cc"],
external_deps = [
"absl/strings",
"gtest",
],
language = "C++",
deps = [
"//:gpr",
@ -63,6 +67,7 @@ grpc_fuzzer(
grpc_cc_test(
name = "parse_address_with_named_scope_id_test",
srcs = ["parse_address_with_named_scope_id_test.cc"],
external_deps = ["gtest"],
language = "C++",
tags = ["no_windows"],
uses_event_engine = False,

@ -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();
}

@ -22,6 +22,8 @@
#include <sys/un.h>
#endif
#include <gtest/gtest.h>
#include "absl/strings/str_format.h"
#include <grpc/grpc.h>
@ -43,21 +45,22 @@ static void test_grpc_parse_ipv6_parity_with_getaddrinfo(
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(target);
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_ipv6(*uri, &addr));
ASSERT_TRUE(grpc_parse_ipv6(*uri, &addr));
grpc_sockaddr_in6* result_from_grpc_parser =
reinterpret_cast<grpc_sockaddr_in6*>(addr.addr);
// Compare the sockaddr returned from gRPC's ipv6 resolver with that returned
// from getaddrinfo.
GPR_ASSERT(result_from_grpc_parser->sin6_family == AF_INET6);
GPR_ASSERT(result_from_getaddrinfo.sin6_family == AF_INET6);
GPR_ASSERT(memcmp(&result_from_grpc_parser->sin6_addr,
&result_from_getaddrinfo.sin6_addr, sizeof(in6_addr)) == 0);
GPR_ASSERT(result_from_grpc_parser->sin6_scope_id ==
result_from_getaddrinfo.sin6_scope_id);
GPR_ASSERT(result_from_grpc_parser->sin6_scope_id != 0);
ASSERT_EQ(result_from_grpc_parser->sin6_family, AF_INET6);
ASSERT_EQ(result_from_getaddrinfo.sin6_family, AF_INET6);
ASSERT_EQ(memcmp(&result_from_grpc_parser->sin6_addr,
&result_from_getaddrinfo.sin6_addr, sizeof(in6_addr)),
0);
ASSERT_EQ(result_from_grpc_parser->sin6_scope_id,
result_from_getaddrinfo.sin6_scope_id);
ASSERT_NE(result_from_grpc_parser->sin6_scope_id, 0);
// TODO(unknown): compare sin6_flow_info fields? parse_ipv6 zero's this field
// as is. Cleanup
}
@ -66,7 +69,7 @@ struct sockaddr_in6 resolve_with_gettaddrinfo(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());
EXPECT_TRUE(uri.ok());
}
std::string host;
std::string port;
@ -88,8 +91,8 @@ struct sockaddr_in6 resolve_with_gettaddrinfo(const char* uri_text) {
for (struct addrinfo* resp = result; resp != nullptr; resp = resp->ai_next) {
num_addrs_from_getaddrinfo++;
}
GPR_ASSERT(num_addrs_from_getaddrinfo == 1);
GPR_ASSERT(result->ai_family == AF_INET6);
EXPECT_EQ(num_addrs_from_getaddrinfo, 1);
EXPECT_EQ(result->ai_family, AF_INET6);
struct sockaddr_in6 out =
*reinterpret_cast<struct sockaddr_in6*>(result->ai_addr);
// Cleanup
@ -97,8 +100,7 @@ struct sockaddr_in6 resolve_with_gettaddrinfo(const char* uri_text) {
return out;
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
TEST(ParseAddressWithNamedScopeIdTest, MainTest) {
grpc_init();
char* arbitrary_interface_name = static_cast<char*>(gpr_zalloc(IF_NAMESIZE));
// Per RFC 3493, an interface index is a "small positive integer starts at 1".
@ -113,7 +115,7 @@ int main(int argc, char** argv) {
break;
}
}
GPR_ASSERT(strlen(arbitrary_interface_name) > 0);
ASSERT_GT(strlen(arbitrary_interface_name), 0);
std::string target =
absl::StrFormat("ipv6:[fe80::1234%%%s]:12345", arbitrary_interface_name);
struct sockaddr_in6 result_from_getaddrinfo =
@ -128,3 +130,9 @@ int main(int argc, char** argv) {
gpr_free(arbitrary_interface_name);
grpc_shutdown();
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

@ -1441,52 +1441,6 @@
],
"uses_polling": true
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix",
"windows"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": false,
"language": "c",
"name": "parse_address_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": true
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": false,
"language": "c",
"name": "parse_address_with_named_scope_id_test",
"platforms": [
"linux",
"mac",
"posix"
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,
@ -5493,6 +5447,52 @@
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix",
"windows"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": true,
"language": "c++",
"name": "parse_address_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": true
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": true,
"language": "c++",
"name": "parse_address_with_named_scope_id_test",
"platforms": [
"linux",
"mac",
"posix"
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,

Loading…
Cancel
Save