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