resolve_address_posix_test.cc 7.6 KB

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