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 <grpc/support/log.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #include <string>
31 #include <vector>
32
33 #include "absl/flags/flag.h"
34 #include "absl/strings/str_cat.h"
35 #include "src/core/lib/gpr/string.h"
36 #include "src/core/lib/iomgr/socket_utils_posix.h"
37 #include "test/core/util/port.h"
38 #include "test/cpp/util/test_config.h"
39
40 ABSL_FLAG(std::string, extra_server_flags, "",
41 "Extra flags to pass to server.");
42
test_client(const char * root,const char * host,int port)43 int test_client(const char* root, const char* host, int port) {
44 int status;
45 pid_t cli;
46 cli = fork();
47 if (cli == 0) {
48 std::string binary_path = absl::StrCat(root, "/interop_client");
49 std::string port_arg = absl::StrCat("--server_port=", port);
50 execl(binary_path.c_str(), binary_path.c_str(), port_arg.c_str(), NULL);
51 return 1;
52 }
53 /* wait for client */
54 gpr_log(GPR_INFO, "Waiting for client: %s", host);
55 if (waitpid(cli, &status, 0) == -1) return 2;
56 if (!WIFEXITED(status)) return 4;
57 if (WEXITSTATUS(status)) return WEXITSTATUS(status);
58 return 0;
59 }
60
main(int argc,char ** argv)61 int main(int argc, char** argv) {
62 grpc::testing::InitTest(&argc, &argv, true);
63 char* me = argv[0];
64 char* lslash = strrchr(me, '/');
65 char root[1024];
66 int port = grpc_pick_unused_port_or_die();
67 int status;
68 pid_t svr;
69 int ret;
70 int do_ipv6 = 1;
71 /* seed rng with pid, so we don't end up with the same random numbers as a
72 concurrently running test binary */
73 srand(getpid());
74 if (!grpc_ipv6_loopback_available()) {
75 gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests.");
76 do_ipv6 = 0;
77 }
78 /* figure out where we are */
79 if (lslash) {
80 memcpy(root, me, lslash - me);
81 root[lslash - me] = 0;
82 } else {
83 strcpy(root, ".");
84 }
85 /* start the server */
86 svr = fork();
87 if (svr == 0) {
88 std::vector<char*> args;
89 std::string command = absl::StrCat(root, "/interop_server");
90 args.push_back(const_cast<char*>(command.c_str()));
91 std::string port_arg = absl::StrCat("--port=", port);
92 args.push_back(const_cast<char*>(port_arg.c_str()));
93 if (!absl::GetFlag(FLAGS_extra_server_flags).empty()) {
94 args.push_back(
95 const_cast<char*>(absl::GetFlag(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