• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
26 
27 #include "absl/flags/flag.h"
28 #include "absl/flags/parse.h"
29 #include "absl/strings/str_format.h"
30 
31 #include <grpc/grpc.h>
32 #include <grpc/support/alloc.h>
33 #include <grpc/support/log.h>
34 #include <grpc/support/sync.h>
35 #include <grpc/support/time.h>
36 
37 #include "src/core/lib/config/config_vars.h"
38 #include "src/core/lib/gpr/string.h"
39 #include "src/core/lib/gpr/useful.h"
40 #include "src/core/lib/gprpp/crash.h"
41 #include "src/core/lib/gprpp/env.h"
42 #include "src/core/lib/gprpp/thd.h"
43 #include "src/core/lib/gprpp/time.h"
44 #include "src/core/lib/iomgr/executor.h"
45 #include "src/core/lib/iomgr/iomgr.h"
46 #include "src/core/lib/iomgr/pollset.h"
47 #include "src/core/lib/iomgr/resolve_address.h"
48 #include "test/core/util/cmdline.h"
49 #include "test/core/util/test_config.h"
50 
test_deadline(void)51 static gpr_timespec test_deadline(void) {
52   return grpc_timeout_seconds_to_deadline(100);
53 }
54 
55 typedef struct args_struct {
56   grpc_core::Thread thd;
57   gpr_event ev;
58   gpr_mu* mu;
59   bool done;              // guarded by mu
60   grpc_pollset* pollset;  // guarded by mu
61   grpc_pollset_set* pollset_set;
62 } args_struct;
63 
do_nothing(void *,grpc_error_handle)64 static void do_nothing(void* /*arg*/, grpc_error_handle /*error*/) {}
65 
args_init(args_struct * args)66 void args_init(args_struct* args) {
67   gpr_event_init(&args->ev);
68   args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
69   grpc_pollset_init(args->pollset, &args->mu);
70   args->pollset_set = grpc_pollset_set_create();
71   grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
72   args->done = false;
73 }
74 
args_finish(args_struct * args)75 void args_finish(args_struct* args) {
76   ASSERT_TRUE(gpr_event_wait(&args->ev, test_deadline()));
77   args->thd.Join();
78   // Don't need to explicitly destruct args->thd since
79   // args is actually going to be destructed, not just freed
80   grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
81   grpc_pollset_set_destroy(args->pollset_set);
82   grpc_closure do_nothing_cb;
83   GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
84                     grpc_schedule_on_exec_ctx);
85   grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
86   // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
87   grpc_core::ExecCtx::Get()->Flush();
88   grpc_pollset_destroy(args->pollset);
89   gpr_free(args->pollset);
90 }
91 
n_sec_deadline(int seconds)92 static grpc_core::Timestamp n_sec_deadline(int seconds) {
93   return grpc_core::Timestamp::FromTimespecRoundUp(
94       grpc_timeout_seconds_to_deadline(seconds));
95 }
96 
actually_poll(void * argsp)97 static void actually_poll(void* argsp) {
98   args_struct* args = static_cast<args_struct*>(argsp);
99   grpc_core::Timestamp deadline = n_sec_deadline(10);
100   while (true) {
101     grpc_core::ExecCtx exec_ctx;
102     {
103       grpc_core::MutexLockForGprMu lock(args->mu);
104       if (args->done) {
105         break;
106       }
107       grpc_core::Duration time_left = deadline - grpc_core::Timestamp::Now();
108       gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, args->done,
109               time_left.millis());
110       ASSERT_GE(time_left, grpc_core::Duration::Zero());
111       grpc_pollset_worker* worker = nullptr;
112       GRPC_LOG_IF_ERROR(
113           "pollset_work",
114           grpc_pollset_work(args->pollset, &worker, n_sec_deadline(1)));
115     }
116   }
117   gpr_event_set(&args->ev, reinterpret_cast<void*>(1));
118 }
119 
poll_pollset_until_request_done(args_struct * args)120 static void poll_pollset_until_request_done(args_struct* args) {
121   args->thd = grpc_core::Thread("grpc_poll_pollset", actually_poll, args);
122   args->thd.Start();
123 }
124 
125 namespace {
126 
MustSucceed(args_struct * args,absl::StatusOr<std::vector<grpc_resolved_address>> result)127 void MustSucceed(args_struct* args,
128                  absl::StatusOr<std::vector<grpc_resolved_address>> result) {
129   ASSERT_TRUE(result.ok());
130   ASSERT_FALSE(result->empty());
131   grpc_core::MutexLockForGprMu lock(args->mu);
132   args->done = true;
133   GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(args->pollset, nullptr));
134 }
135 
136 }  // namespace
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_core::GetDNSResolver()->LookupHostname(
144       [&args](absl::StatusOr<std::vector<grpc_resolved_address>> result) {
145         MustSucceed(&args, std::move(result));
146       },
147       target, /*port number=*/"1", grpc_core::kDefaultDNSRequestTimeout,
148       args.pollset_set,
149       /*name_server=*/"");
150   grpc_core::ExecCtx::Get()->Flush();
151   args_finish(&args);
152 }
153 
test_named_and_numeric_scope_ids(void)154 static void test_named_and_numeric_scope_ids(void) {
155   char* arbitrary_interface_name = static_cast<char*>(gpr_zalloc(IF_NAMESIZE));
156   int interface_index = 0;
157   // Probe candidate interface index numbers until we find one that the
158   // system recognizes, and then use that for the test.
159   for (size_t i = 1; i < 65536; i++) {
160     if (if_indextoname(i, arbitrary_interface_name) != nullptr) {
161       gpr_log(GPR_DEBUG,
162               "Found interface at index %" PRIuPTR
163               " named %s. Will use this for the test",
164               i, arbitrary_interface_name);
165       interface_index = static_cast<int>(i);
166       break;
167     }
168   }
169   ASSERT_GT(strlen(arbitrary_interface_name), 0);
170   // Test resolution of an ipv6 address with a named scope ID
171   gpr_log(GPR_DEBUG, "test resolution with a named scope ID");
172   std::string target_with_named_scope_id =
173       absl::StrFormat("fe80::1234%%%s", arbitrary_interface_name);
174   resolve_address_must_succeed(target_with_named_scope_id.c_str());
175   gpr_free(arbitrary_interface_name);
176   // Test resolution of an ipv6 address with a numeric scope ID
177   gpr_log(GPR_DEBUG, "test resolution with a numeric scope ID");
178   std::string target_with_numeric_scope_id =
179       absl::StrFormat("fe80::1234%%%d", interface_index);
180   resolve_address_must_succeed(target_with_numeric_scope_id.c_str());
181 }
182 
183 ABSL_FLAG(std::string, resolver, "", "Resolver type (ares or native)");
184 
TEST(ResolveAddressUsingAresResolverPosixTest,MainTest)185 TEST(ResolveAddressUsingAresResolverPosixTest, MainTest) {
186   // First set the resolver type based off of --resolver
187   std::string resolver_type = absl::GetFlag(FLAGS_resolver);
188   // In case that there are more than one argument on the command line,
189   // --resolver will always be the first one, so only parse the first argument
190   // (other arguments may be unknown to cl)
191   grpc_core::ConfigVars::Overrides overrides;
192   if (resolver_type == "native") {
193     overrides.dns_resolver = "native";
194   } else if (resolver_type == "ares") {
195     overrides.dns_resolver = "ares";
196   } else {
197     gpr_log(GPR_ERROR, "--resolver was not set to ares or native");
198     ASSERT_TRUE(false);
199   }
200   grpc_core::ConfigVars::SetOverrides(overrides);
201 
202   grpc_init();
203   {
204     grpc_core::ExecCtx exec_ctx;
205     test_named_and_numeric_scope_ids();
206   }
207   grpc_shutdown();
208 }
209 
main(int argc,char ** argv)210 int main(int argc, char** argv) {
211   grpc::testing::TestEnvironment env(&argc, argv);
212   ::testing::InitGoogleTest(&argc, argv);
213   absl::ParseCommandLine(argc, argv);
214   return RUN_ALL_TESTS();
215 }
216