| 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/iomgr/socket_utils_posix.h" |
| 39 |
#include "test/core/util/port.h" |
| 40 |
#include "test/cpp/util/test_config.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]; |
| 79 |
int port = grpc_pick_unused_port_or_die(); |
| 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()); |
| 87 |
if (!grpc_ipv6_loopback_available()) { |
| 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 |
} |