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