1 /*
2 *
3 * Copyright 2016 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 #include <sys/un.h>
22
23 #include <string>
24
25 #include "absl/strings/str_format.h"
26
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/sync.h>
31 #include <grpc/support/time.h>
32
33 #include "src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
34 #include "src/core/lib/gpr/env.h"
35 #include "src/core/lib/gpr/string.h"
36 #include "src/core/lib/gpr/useful.h"
37 #include "src/core/lib/gprpp/thd.h"
38 #include "src/core/lib/iomgr/executor.h"
39 #include "src/core/lib/iomgr/iomgr.h"
40 #include "src/core/lib/iomgr/resolve_address.h"
41 #include "test/core/util/cmdline.h"
42 #include "test/core/util/test_config.h"
43
test_deadline(void)44 static gpr_timespec test_deadline(void) {
45 return grpc_timeout_seconds_to_deadline(100);
46 }
47
48 typedef struct args_struct {
49 grpc_core::Thread thd;
50 gpr_event ev;
51 grpc_resolved_addresses* addrs;
52 gpr_mu* mu;
53 bool done; // guarded by mu
54 grpc_pollset* pollset; // guarded by mu
55 grpc_pollset_set* pollset_set;
56 } args_struct;
57
do_nothing(void *,grpc_error_handle)58 static void do_nothing(void* /*arg*/, grpc_error_handle /*error*/) {}
59
args_init(args_struct * args)60 void args_init(args_struct* args) {
61 gpr_event_init(&args->ev);
62 args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
63 grpc_pollset_init(args->pollset, &args->mu);
64 args->pollset_set = grpc_pollset_set_create();
65 grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
66 args->addrs = nullptr;
67 args->done = false;
68 }
69
args_finish(args_struct * args)70 void args_finish(args_struct* args) {
71 GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
72 args->thd.Join();
73 // Don't need to explicitly destruct args->thd since
74 // args is actually going to be destructed, not just freed
75 grpc_resolved_addresses_destroy(args->addrs);
76 grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
77 grpc_pollset_set_destroy(args->pollset_set);
78 grpc_closure do_nothing_cb;
79 GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
80 grpc_schedule_on_exec_ctx);
81 grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
82 // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
83 grpc_core::ExecCtx::Get()->Flush();
84 grpc_pollset_destroy(args->pollset);
85 gpr_free(args->pollset);
86 }
87
n_sec_deadline(int seconds)88 static grpc_millis n_sec_deadline(int seconds) {
89 return grpc_timespec_to_millis_round_up(
90 grpc_timeout_seconds_to_deadline(seconds));
91 }
92
actually_poll(void * argsp)93 static void actually_poll(void* argsp) {
94 args_struct* args = static_cast<args_struct*>(argsp);
95 grpc_millis deadline = n_sec_deadline(10);
96 while (true) {
97 grpc_core::ExecCtx exec_ctx;
98 {
99 grpc_core::MutexLockForGprMu lock(args->mu);
100 if (args->done) {
101 break;
102 }
103 grpc_millis time_left = deadline - grpc_core::ExecCtx::Get()->Now();
104 gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, args->done, time_left);
105 GPR_ASSERT(time_left >= 0);
106 grpc_pollset_worker* worker = nullptr;
107 GRPC_LOG_IF_ERROR(
108 "pollset_work",
109 grpc_pollset_work(args->pollset, &worker, n_sec_deadline(1)));
110 }
111 }
112 gpr_event_set(&args->ev, reinterpret_cast<void*>(1));
113 }
114
poll_pollset_until_request_done(args_struct * args)115 static void poll_pollset_until_request_done(args_struct* args) {
116 args->thd = grpc_core::Thread("grpc_poll_pollset", actually_poll, args);
117 args->thd.Start();
118 }
119
must_succeed(void * argsp,grpc_error_handle err)120 static void must_succeed(void* argsp, grpc_error_handle err) {
121 args_struct* args = static_cast<args_struct*>(argsp);
122 GPR_ASSERT(err == GRPC_ERROR_NONE);
123 GPR_ASSERT(args->addrs != nullptr);
124 GPR_ASSERT(args->addrs->naddrs > 0);
125 grpc_core::MutexLockForGprMu lock(args->mu);
126 args->done = true;
127 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(args->pollset, nullptr));
128 }
129
must_fail(void * argsp,grpc_error_handle err)130 static void must_fail(void* argsp, grpc_error_handle err) {
131 args_struct* args = static_cast<args_struct*>(argsp);
132 GPR_ASSERT(err != GRPC_ERROR_NONE);
133 grpc_core::MutexLockForGprMu lock(args->mu);
134 args->done = true;
135 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(args->pollset, nullptr));
136 }
137
resolve_address_must_succeed(const char * target)138 static void resolve_address_must_succeed(const char* target) {
139 grpc_core::ExecCtx exec_ctx;
140 args_struct args;
141 args_init(&args);
142 poll_pollset_until_request_done(&args);
143 grpc_resolve_address(
144 target, "1" /* port number */, args.pollset_set,
145 GRPC_CLOSURE_CREATE(must_succeed, &args, grpc_schedule_on_exec_ctx),
146 &args.addrs);
147 grpc_core::ExecCtx::Get()->Flush();
148 args_finish(&args);
149 }
150
test_named_and_numeric_scope_ids(void)151 static void test_named_and_numeric_scope_ids(void) {
152 char* arbitrary_interface_name = static_cast<char*>(gpr_zalloc(IF_NAMESIZE));
153 int interface_index = 0;
154 // Probe candidate interface index numbers until we find one that the
155 // system recognizes, and then use that for the test.
156 for (size_t i = 1; i < 65536; i++) {
157 if (if_indextoname(i, arbitrary_interface_name) != nullptr) {
158 gpr_log(GPR_DEBUG,
159 "Found interface at index %" PRIuPTR
160 " named %s. Will use this for the test",
161 i, arbitrary_interface_name);
162 interface_index = static_cast<int>(i);
163 break;
164 }
165 }
166 GPR_ASSERT(strlen(arbitrary_interface_name) > 0);
167 // Test resolution of an ipv6 address with a named scope ID
168 gpr_log(GPR_DEBUG, "test resolution with a named scope ID");
169 std::string target_with_named_scope_id =
170 absl::StrFormat("fe80::1234%%%s", arbitrary_interface_name);
171 resolve_address_must_succeed(target_with_named_scope_id.c_str());
172 gpr_free(arbitrary_interface_name);
173 // Test resolution of an ipv6 address with a numeric scope ID
174 gpr_log(GPR_DEBUG, "test resolution with a numeric scope ID");
175 std::string target_with_numeric_scope_id =
176 absl::StrFormat("fe80::1234%%%d", interface_index);
177 resolve_address_must_succeed(target_with_numeric_scope_id.c_str());
178 }
179
main(int argc,char ** argv)180 int main(int argc, char** argv) {
181 // First set the resolver type based off of --resolver
182 const char* resolver_type = nullptr;
183 gpr_cmdline* cl = gpr_cmdline_create("resolve address test");
184 gpr_cmdline_add_string(cl, "resolver", "Resolver type (ares or native)",
185 &resolver_type);
186 // In case that there are more than one argument on the command line,
187 // --resolver will always be the first one, so only parse the first argument
188 // (other arguments may be unknown to cl)
189 gpr_cmdline_parse(cl, argc > 2 ? 2 : argc, argv);
190 grpc_core::UniquePtr<char> resolver =
191 GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
192 if (strlen(resolver.get()) != 0) {
193 gpr_log(GPR_INFO, "Warning: overriding resolver setting of %s",
194 resolver.get());
195 }
196 if (resolver_type != nullptr && gpr_stricmp(resolver_type, "native") == 0) {
197 GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "native");
198 } else if (resolver_type != nullptr &&
199 gpr_stricmp(resolver_type, "ares") == 0) {
200 GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "ares");
201 } else {
202 gpr_log(GPR_ERROR, "--resolver_type was not set to ares or native");
203 abort();
204 }
205 grpc::testing::TestEnvironment env(argc, argv);
206 grpc_init();
207
208 {
209 grpc_core::ExecCtx exec_ctx;
210 test_named_and_numeric_scope_ids();
211 // c-ares resolver doesn't support UDS (ability for native DNS resolver
212 // to handle this is only expected to be used by servers, which
213 // unconditionally use the native DNS resolver).
214 grpc_core::UniquePtr<char> resolver =
215 GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
216 }
217 gpr_cmdline_destroy(cl);
218
219 grpc_shutdown();
220 return 0;
221 }
222