interop_test.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. *
  3. * Copyright 2015 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 <assert.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <unistd.h>
  26. #include <string>
  27. #include <vector>
  28. #include "absl/flags/flag.h"
  29. #include "absl/strings/str_cat.h"
  30. #include <grpc/support/alloc.h>
  31. #include <grpc/support/log.h>
  32. #include "src/core/lib/gpr/string.h"
  33. #include "src/core/lib/iomgr/socket_utils_posix.h"
  34. #include "test/core/util/port.h"
  35. #include "test/cpp/util/test_config.h"
  36. ABSL_FLAG(std::vector<std::string>, extra_client_flags, {},
  37. "Extra flags to pass to clients.");
  38. ABSL_FLAG(std::vector<std::string>, extra_server_flags, {},
  39. "Extra flags to pass to server.");
  40. int test_client(const char* root, const char* host, int port) {
  41. int status;
  42. pid_t cli;
  43. cli = fork();
  44. if (cli == 0) {
  45. std::vector<char*> args;
  46. std::string command = absl::StrCat(root, "/interop_client");
  47. args.push_back(const_cast<char*>(command.c_str()));
  48. std::string port_arg = absl::StrCat("--server_port=", port);
  49. args.push_back(const_cast<char*>(port_arg.c_str()));
  50. auto extra_client_flags = absl::GetFlag(FLAGS_extra_client_flags);
  51. for (size_t i = 0; i < extra_client_flags.size(); i++) {
  52. args.push_back(const_cast<char*>(extra_client_flags[i].c_str()));
  53. }
  54. args.push_back(nullptr);
  55. execv(args[0], args.data());
  56. return 1;
  57. }
  58. /* wait for client */
  59. gpr_log(GPR_INFO, "Waiting for client: %s", host);
  60. if (waitpid(cli, &status, 0) == -1) return 2;
  61. if (!WIFEXITED(status)) return 4;
  62. if (WEXITSTATUS(status)) return WEXITSTATUS(status);
  63. return 0;
  64. }
  65. int main(int argc, char** argv) {
  66. grpc::testing::InitTest(&argc, &argv, true);
  67. char* me = argv[0];
  68. char* lslash = strrchr(me, '/');
  69. char root[1024];
  70. int port = grpc_pick_unused_port_or_die();
  71. int status;
  72. pid_t svr;
  73. int ret;
  74. int do_ipv6 = 1;
  75. /* seed rng with pid, so we don't end up with the same random numbers as a
  76. concurrently running test binary */
  77. srand(getpid());
  78. if (!grpc_ipv6_loopback_available()) {
  79. gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests.");
  80. do_ipv6 = 0;
  81. }
  82. /* figure out where we are */
  83. if (lslash) {
  84. memcpy(root, me, lslash - me);
  85. root[lslash - me] = 0;
  86. } else {
  87. strcpy(root, ".");
  88. }
  89. /* start the server */
  90. svr = fork();
  91. if (svr == 0) {
  92. std::vector<char*> args;
  93. std::string command = absl::StrCat(root, "/interop_server");
  94. args.push_back(const_cast<char*>(command.c_str()));
  95. std::string port_arg = absl::StrCat("--port=", port);
  96. args.push_back(const_cast<char*>(port_arg.c_str()));
  97. auto extra_server_flags = absl::GetFlag(FLAGS_extra_server_flags);
  98. for (size_t i = 0; i < extra_server_flags.size(); i++) {
  99. args.push_back(const_cast<char*>(extra_server_flags[i].c_str()));
  100. }
  101. args.push_back(nullptr);
  102. execv(args[0], args.data());
  103. return 1;
  104. }
  105. /* wait a little */
  106. sleep(10);
  107. /* start the clients */
  108. ret = test_client(root, "127.0.0.1", port);
  109. if (ret != 0) return ret;
  110. ret = test_client(root, "::ffff:127.0.0.1", port);
  111. if (ret != 0) return ret;
  112. ret = test_client(root, "localhost", port);
  113. if (ret != 0) return ret;
  114. if (do_ipv6) {
  115. ret = test_client(root, "::1", port);
  116. if (ret != 0) return ret;
  117. }
  118. /* wait for server */
  119. gpr_log(GPR_INFO, "Waiting for server");
  120. kill(svr, SIGINT);
  121. if (waitpid(svr, &status, 0) == -1) return 2;
  122. if (!WIFEXITED(status)) return 4;
  123. if (WEXITSTATUS(status)) return WEXITSTATUS(status);
  124. return 0;
  125. }