interop_test.cc
Go to the documentation of this file.
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 
19 #include <assert.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 
28 #include <string>
29 #include <vector>
30 
31 #include "absl/flags/flag.h"
32 #include "absl/strings/str_cat.h"
33 
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/log.h>
36 
39 #include "test/core/util/port.h"
41 
42 ABSL_FLAG(std::vector<std::string>, extra_client_flags, {},
43  "Extra flags to pass to clients.");
44 
45 ABSL_FLAG(std::vector<std::string>, extra_server_flags, {},
46  "Extra flags to pass to server.");
47 
48 int test_client(const char* root, const char* host, int port) {
49  int status;
50  pid_t cli;
51  cli = fork();
52  if (cli == 0) {
53  std::vector<char*> args;
54  std::string command = absl::StrCat(root, "/interop_client");
55  args.push_back(const_cast<char*>(command.c_str()));
56  std::string port_arg = absl::StrCat("--server_port=", port);
57  args.push_back(const_cast<char*>(port_arg.c_str()));
58  auto extra_client_flags = absl::GetFlag(FLAGS_extra_client_flags);
59  for (size_t i = 0; i < extra_client_flags.size(); i++) {
60  args.push_back(const_cast<char*>(extra_client_flags[i].c_str()));
61  }
62  args.push_back(nullptr);
63  execv(args[0], args.data());
64  return 1;
65  }
66  /* wait for client */
67  gpr_log(GPR_INFO, "Waiting for client: %s", host);
68  if (waitpid(cli, &status, 0) == -1) return 2;
69  if (!WIFEXITED(status)) return 4;
70  if (WEXITSTATUS(status)) return WEXITSTATUS(status);
71  return 0;
72 }
73 
74 int main(int argc, char** argv) {
75  grpc::testing::InitTest(&argc, &argv, true);
76  char* me = argv[0];
77  char* lslash = strrchr(me, '/');
78  char root[1024];
80  int status;
81  pid_t svr;
82  int ret;
83  int do_ipv6 = 1;
84  /* seed rng with pid, so we don't end up with the same random numbers as a
85  concurrently running test binary */
86  srand(getpid());
88  gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests.");
89  do_ipv6 = 0;
90  }
91  /* figure out where we are */
92  if (lslash) {
93  memcpy(root, me, lslash - me);
94  root[lslash - me] = 0;
95  } else {
96  strcpy(root, ".");
97  }
98  /* start the server */
99  svr = fork();
100  if (svr == 0) {
101  std::vector<char*> args;
102  std::string command = absl::StrCat(root, "/interop_server");
103  args.push_back(const_cast<char*>(command.c_str()));
104  std::string port_arg = absl::StrCat("--port=", port);
105  args.push_back(const_cast<char*>(port_arg.c_str()));
106  auto extra_server_flags = absl::GetFlag(FLAGS_extra_server_flags);
107  for (size_t i = 0; i < extra_server_flags.size(); i++) {
108  args.push_back(const_cast<char*>(extra_server_flags[i].c_str()));
109  }
110  args.push_back(nullptr);
111  execv(args[0], args.data());
112  return 1;
113  }
114  /* wait a little */
115  sleep(10);
116  /* start the clients */
117  ret = test_client(root, "127.0.0.1", port);
118  if (ret != 0) return ret;
119  ret = test_client(root, "::ffff:127.0.0.1", port);
120  if (ret != 0) return ret;
121  ret = test_client(root, "localhost", port);
122  if (ret != 0) return ret;
123  if (do_ipv6) {
124  ret = test_client(root, "::1", port);
125  if (ret != 0) return ret;
126  }
127  /* wait for server */
128  gpr_log(GPR_INFO, "Waiting for server");
129  kill(svr, SIGINT);
130  if (waitpid(svr, &status, 0) == -1) return 2;
131  if (!WIFEXITED(status)) return 4;
132  if (WEXITSTATUS(status)) return WEXITSTATUS(status);
133  return 0;
134 }
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
ABSL_FLAG
ABSL_FLAG(std::vector< std::string >, extra_client_flags, {}, "Extra flags to pass to clients.")
log.h
port.h
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
status
absl::Status status
Definition: rls.cc:251
grpc_ipv6_loopback_available
int grpc_ipv6_loopback_available(void)
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
root
RefCountedPtr< grpc_tls_certificate_provider > root
Definition: xds_server_config_fetcher.cc:223
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_client
int test_client(const char *root, const char *host, int port)
Definition: interop_test.cc:48
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
main
int main(int argc, char **argv)
Definition: interop_test.cc:74
socket_utils_posix.h
test_config.h
command
const char * command
Definition: grpc_tool.cc:247
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:07