resolve_address_posix_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "src/core/lib/iomgr/resolve_address.h"
  19. #include <string.h>
  20. #include <sys/un.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/sync.h>
  25. #include <grpc/support/time.h>
  26. #include "src/core/lib/gpr/env.h"
  27. #include "src/core/lib/gpr/string.h"
  28. #include "src/core/lib/gpr/useful.h"
  29. #include "src/core/lib/gprpp/thd.h"
  30. #include "src/core/lib/iomgr/executor.h"
  31. #include "src/core/lib/iomgr/iomgr.h"
  32. #include "test/core/util/test_config.h"
  33. static gpr_timespec test_deadline(void) {
  34. return grpc_timeout_seconds_to_deadline(100);
  35. }
  36. typedef struct args_struct {
  37. grpc_core::Thread thd;
  38. gpr_event ev;
  39. grpc_resolved_addresses* addrs;
  40. gpr_atm done_atm;
  41. gpr_mu* mu;
  42. grpc_pollset* pollset;
  43. grpc_pollset_set* pollset_set;
  44. } args_struct;
  45. static void do_nothing(void* arg, grpc_error* error) {}
  46. void args_init(args_struct* args) {
  47. gpr_event_init(&args->ev);
  48. args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
  49. grpc_pollset_init(args->pollset, &args->mu);
  50. args->pollset_set = grpc_pollset_set_create();
  51. grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
  52. args->addrs = nullptr;
  53. }
  54. void args_finish(args_struct* args) {
  55. GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
  56. args->thd.Join();
  57. // Don't need to explicitly destruct args->thd since
  58. // args is actually going to be destructed, not just freed
  59. grpc_resolved_addresses_destroy(args->addrs);
  60. grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
  61. grpc_pollset_set_destroy(args->pollset_set);
  62. grpc_closure do_nothing_cb;
  63. GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
  64. grpc_schedule_on_exec_ctx);
  65. grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
  66. // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
  67. grpc_core::ExecCtx::Get()->Flush();
  68. grpc_pollset_destroy(args->pollset);
  69. gpr_free(args->pollset);
  70. }
  71. static grpc_millis n_sec_deadline(int seconds) {
  72. return grpc_timespec_to_millis_round_up(
  73. grpc_timeout_seconds_to_deadline(seconds));
  74. }
  75. static void actually_poll(void* argsp) {
  76. args_struct* args = static_cast<args_struct*>(argsp);
  77. grpc_millis deadline = n_sec_deadline(10);
  78. while (true) {
  79. grpc_core::ExecCtx exec_ctx;
  80. bool done = gpr_atm_acq_load(&args->done_atm) != 0;
  81. if (done) {
  82. break;
  83. }
  84. grpc_millis time_left = deadline - grpc_core::ExecCtx::Get()->Now();
  85. gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, done, time_left);
  86. GPR_ASSERT(time_left >= 0);
  87. grpc_pollset_worker* worker = nullptr;
  88. gpr_mu_lock(args->mu);
  89. GRPC_LOG_IF_ERROR("pollset_work", grpc_pollset_work(args->pollset, &worker,
  90. n_sec_deadline(1)));
  91. gpr_mu_unlock(args->mu);
  92. grpc_core::ExecCtx::Get()->Flush();
  93. }
  94. gpr_event_set(&args->ev, (void*)1);
  95. }
  96. static void poll_pollset_until_request_done(args_struct* args) {
  97. gpr_atm_rel_store(&args->done_atm, 0);
  98. args->thd = grpc_core::Thread("grpc_poll_pollset", actually_poll, args);
  99. args->thd.Start();
  100. }
  101. static void must_succeed(void* argsp, grpc_error* err) {
  102. args_struct* args = static_cast<args_struct*>(argsp);
  103. GPR_ASSERT(err == GRPC_ERROR_NONE);
  104. GPR_ASSERT(args->addrs != nullptr);
  105. GPR_ASSERT(args->addrs->naddrs > 0);
  106. gpr_atm_rel_store(&args->done_atm, 1);
  107. }
  108. static void must_fail(void* argsp, grpc_error* err) {
  109. args_struct* args = static_cast<args_struct*>(argsp);
  110. GPR_ASSERT(err != GRPC_ERROR_NONE);
  111. gpr_atm_rel_store(&args->done_atm, 1);
  112. }
  113. static void test_unix_socket(void) {
  114. grpc_core::ExecCtx exec_ctx;
  115. args_struct args;
  116. args_init(&args);
  117. poll_pollset_until_request_done(&args);
  118. grpc_resolve_address(
  119. "unix:/path/name", nullptr, args.pollset_set,
  120. GRPC_CLOSURE_CREATE(must_succeed, &args, grpc_schedule_on_exec_ctx),
  121. &args.addrs);
  122. args_finish(&args);
  123. }
  124. static void test_unix_socket_path_name_too_long(void) {
  125. grpc_core::ExecCtx exec_ctx;
  126. args_struct args;
  127. args_init(&args);
  128. const char prefix[] = "unix:/path/name";
  129. size_t path_name_length =
  130. GPR_ARRAY_SIZE(((struct sockaddr_un*)nullptr)->sun_path) + 6;
  131. char* path_name =
  132. static_cast<char*>(gpr_malloc(sizeof(char) * path_name_length));
  133. memset(path_name, 'a', path_name_length);
  134. memcpy(path_name, prefix, strlen(prefix) - 1);
  135. path_name[path_name_length - 1] = '\0';
  136. poll_pollset_until_request_done(&args);
  137. grpc_resolve_address(
  138. path_name, nullptr, args.pollset_set,
  139. GRPC_CLOSURE_CREATE(must_fail, &args, grpc_schedule_on_exec_ctx),
  140. &args.addrs);
  141. gpr_free(path_name);
  142. args_finish(&args);
  143. }
  144. int main(int argc, char** argv) {
  145. grpc::testing::TestEnvironment env(argc, argv);
  146. grpc_init();
  147. {
  148. grpc_core::ExecCtx exec_ctx;
  149. char* resolver_env = gpr_getenv("GRPC_DNS_RESOLVER");
  150. // c-ares resolver doesn't support UDS (ability for native DNS resolver
  151. // to handle this is only expected to be used by servers, which
  152. // unconditionally use the native DNS resolver).
  153. if (resolver_env == nullptr || gpr_stricmp(resolver_env, "native") == 0) {
  154. test_unix_socket();
  155. test_unix_socket_path_name_too_long();
  156. }
  157. gpr_free(resolver_env);
  158. }
  159. grpc_shutdown();
  160. return 0;
  161. }