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