parse_address_with_named_scope_id_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <net/if.h>
20 #include <string.h>
21 #ifdef GRPC_HAVE_UNIX_SOCKET
22 #include <sys/un.h>
23 #endif
24 
25 #include "absl/strings/str_format.h"
26 
27 #include <grpc/grpc.h>
28 #include <grpc/support/log.h>
29 
38 
40  const char* target, const struct sockaddr_in6 result_from_getaddrinfo) {
41  // Get the sockaddr that gRPC's ipv6 resolver resolves this too.
44  if (!uri.ok()) {
45  gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
46  GPR_ASSERT(uri.ok());
47  }
49  GPR_ASSERT(1 == grpc_parse_ipv6(*uri, &addr));
50  grpc_sockaddr_in6* result_from_grpc_parser =
51  reinterpret_cast<grpc_sockaddr_in6*>(addr.addr);
52  // Compare the sockaddr returned from gRPC's ipv6 resolver with that returned
53  // from getaddrinfo.
54  GPR_ASSERT(result_from_grpc_parser->sin6_family == AF_INET6);
55  GPR_ASSERT(result_from_getaddrinfo.sin6_family == AF_INET6);
56  GPR_ASSERT(memcmp(&result_from_grpc_parser->sin6_addr,
57  &result_from_getaddrinfo.sin6_addr, sizeof(in6_addr)) == 0);
58  GPR_ASSERT(result_from_grpc_parser->sin6_scope_id ==
59  result_from_getaddrinfo.sin6_scope_id);
60  GPR_ASSERT(result_from_grpc_parser->sin6_scope_id != 0);
61  // TODO(unknown): compare sin6_flow_info fields? parse_ipv6 zero's this field
62  // as is. Cleanup
63 }
64 
67  if (!uri.ok()) {
68  gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
69  GPR_ASSERT(uri.ok());
70  }
71  std::string host;
73  grpc_core::SplitHostPort(uri->path(), &host, &port);
74  struct addrinfo hints;
75  memset(&hints, 0, sizeof(hints));
76  hints.ai_family = AF_INET6;
77  hints.ai_socktype = SOCK_STREAM;
78  hints.ai_flags = AI_NUMERICHOST;
79  struct addrinfo* result;
80  int res = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
81  if (res != 0) {
83  "getaddrinfo failed to resolve host:%s port:%s. Error: %d.",
84  host.c_str(), port.c_str(), res);
85  abort();
86  }
87  size_t num_addrs_from_getaddrinfo = 0;
88  for (struct addrinfo* resp = result; resp != nullptr; resp = resp->ai_next) {
89  num_addrs_from_getaddrinfo++;
90  }
91  GPR_ASSERT(num_addrs_from_getaddrinfo == 1);
92  GPR_ASSERT(result->ai_family == AF_INET6);
93  struct sockaddr_in6 out =
94  *reinterpret_cast<struct sockaddr_in6*>(result->ai_addr);
95  // Cleanup
96  freeaddrinfo(result);
97  return out;
98 }
99 
100 int main(int argc, char** argv) {
101  grpc::testing::TestEnvironment env(&argc, argv);
102  grpc_init();
103  char* arbitrary_interface_name = static_cast<char*>(gpr_zalloc(IF_NAMESIZE));
104  // Per RFC 3493, an interface index is a "small positive integer starts at 1".
105  // Probe candidate interface index numbers until we find one that the
106  // system recognizes, and then use that for the test.
107  for (size_t i = 1; i < 65536; i++) {
108  if (if_indextoname(i, arbitrary_interface_name) != nullptr) {
110  "Found interface at index %" PRIuPTR
111  " named %s. Will use this for the test",
112  i, arbitrary_interface_name);
113  break;
114  }
115  }
116  GPR_ASSERT(strlen(arbitrary_interface_name) > 0);
118  absl::StrFormat("ipv6:[fe80::1234%%%s]:12345", arbitrary_interface_name);
119  struct sockaddr_in6 result_from_getaddrinfo =
121  // Run the test
123  "Run test_grpc_parse_ipv6_parity_with_getaddrinfo with target: %s",
124  target.c_str());
126  result_from_getaddrinfo);
127  // Cleanup
128  gpr_free(arbitrary_interface_name);
129  grpc_shutdown();
130 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
log.h
sockaddr_utils.h
AF_INET6
#define AF_INET6
Definition: ares_setup.h:208
const
#define const
Definition: bloaty/third_party/zlib/zconf.h:230
generate.env
env
Definition: generate.py:37
memset
return memset(p, 0, total)
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
grpc_parse_ipv6
bool grpc_parse_ipv6(const grpc_core::URI &uri, grpc_resolved_address *resolved_addr)
Definition: parse_address.cc:282
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_resolved_address
Definition: resolved_address.h:34
grpc_core::SplitHostPort
bool SplitHostPort(absl::string_view name, absl::string_view *host, absl::string_view *port)
Definition: host_port.cc:88
grpc_core::URI::Parse
static absl::StatusOr< URI > Parse(absl::string_view uri_text)
Definition: uri_parser.cc:209
sockaddr.h
gen_build_yaml.struct
def struct(**kwargs)
Definition: test/core/end2end/gen_build_yaml.py:30
grpc_core::URI::path
const std::string & path() const
Definition: uri_parser.h:70
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
memory.h
socket_utils.h
parse_address.h
test_grpc_parse_ipv6_parity_with_getaddrinfo
static void test_grpc_parse_ipv6_parity_with_getaddrinfo(const char *target, const struct sockaddr_in6 result_from_getaddrinfo)
Definition: parse_address_with_named_scope_id_test.cc:39
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
sockaddr_in6
Definition: ares_ipv6.h:25
main
int main(int argc, char **argv)
Definition: parse_address_with_named_scope_id_test.cc:100
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
http2_server_health_check.resp
resp
Definition: http2_server_health_check.py:31
grpc.h
sockaddr_in6::sin6_family
unsigned short sin6_family
Definition: ares_ipv6.h:27
host_port.h
addrinfo::ai_family
int ai_family
Definition: ares_ipv6.h:46
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::ExecCtx
Definition: exec_ctx.h:97
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
addrinfo::ai_socktype
int ai_socktype
Definition: ares_ipv6.h:47
exec_ctx.h
addrinfo::ai_flags
int ai_flags
Definition: ares_ipv6.h:45
sockaddr_in6::sin6_addr
struct ares_in6_addr sin6_addr
Definition: ares_ipv6.h:30
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
addrinfo
Definition: ares_ipv6.h:43
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
sockaddr_in6::sin6_scope_id
unsigned int sin6_scope_id
Definition: ares_ipv6.h:31
IF_NAMESIZE
#define IF_NAMESIZE
Definition: ares_ipv6.h:77
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
absl::StatusOr::status
const Status & status() const &
Definition: abseil-cpp/absl/status/statusor.h:678
resolve_with_gettaddrinfo
struct sockaddr_in6 resolve_with_gettaddrinfo(const char *uri_text)
Definition: parse_address_with_named_scope_id_test.cc:65


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:39