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