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