Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 2e19036 | 2016-03-25 14:33:26 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #ifndef _POSIX_SOURCE |
| 35 | #define _POSIX_SOURCE |
| 36 | #endif |
| 37 | |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 38 | #include <assert.h> |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 39 | #include <signal.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 40 | #include <stdio.h> |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 41 | #include <stdlib.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 42 | #include <string.h> |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include <sys/wait.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 45 | #include <unistd.h> |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 46 | |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 47 | #include <grpc/support/alloc.h> |
| 48 | #include <grpc/support/host_port.h> |
| 49 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 50 | #include <grpc/support/string_util.h> |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 51 | #include "test/core/util/port.h" |
| 52 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 53 | extern "C" { |
| 54 | #include "src/core/iomgr/socket_utils_posix.h" |
| 55 | #include "src/core/support/string.h" |
| 56 | } |
| 57 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 58 | int test_client(const char* root, const char* host, int port) { |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 59 | int status; |
| 60 | pid_t cli; |
| 61 | cli = fork(); |
| 62 | if (cli == 0) { |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 63 | char* binary_path; |
| 64 | char* port_arg; |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 65 | gpr_asprintf(&binary_path, "%s/interop_client", root); |
| 66 | gpr_asprintf(&port_arg, "--server_port=%d", port); |
| 67 | |
| 68 | execl(binary_path, binary_path, port_arg, NULL); |
| 69 | |
| 70 | gpr_free(binary_path); |
| 71 | gpr_free(port_arg); |
| 72 | return 1; |
| 73 | } |
| 74 | /* wait for client */ |
| 75 | gpr_log(GPR_INFO, "Waiting for client: %s", host); |
| 76 | if (waitpid(cli, &status, 0) == -1) return 2; |
| 77 | if (!WIFEXITED(status)) return 4; |
| 78 | if (WEXITSTATUS(status)) return WEXITSTATUS(status); |
| 79 | return 0; |
| 80 | } |
| 81 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 82 | int main(int argc, char** argv) { |
| 83 | char* me = argv[0]; |
| 84 | char* lslash = strrchr(me, '/'); |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 85 | char root[1024]; |
| 86 | int port = grpc_pick_unused_port_or_die(); |
| 87 | int status; |
| 88 | pid_t svr; |
| 89 | int ret; |
| 90 | int do_ipv6 = 1; |
| 91 | /* seed rng with pid, so we don't end up with the same random numbers as a |
| 92 | concurrently running test binary */ |
| 93 | srand(getpid()); |
| 94 | if (!grpc_ipv6_loopback_available()) { |
| 95 | gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests."); |
| 96 | do_ipv6 = 0; |
| 97 | } |
| 98 | /* figure out where we are */ |
| 99 | if (lslash) { |
| 100 | memcpy(root, me, lslash - me); |
| 101 | root[lslash - me] = 0; |
| 102 | } else { |
| 103 | strcpy(root, "."); |
| 104 | } |
| 105 | /* start the server */ |
| 106 | svr = fork(); |
| 107 | if (svr == 0) { |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 108 | char* binary_path; |
| 109 | char* port_arg; |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 110 | gpr_asprintf(&binary_path, "%s/interop_server", root); |
| 111 | gpr_asprintf(&port_arg, "--port=%d", port); |
| 112 | |
| 113 | execl(binary_path, binary_path, port_arg, NULL); |
| 114 | |
| 115 | gpr_free(binary_path); |
| 116 | gpr_free(port_arg); |
| 117 | return 1; |
| 118 | } |
| 119 | /* wait a little */ |
| 120 | sleep(2); |
| 121 | /* start the clients */ |
| 122 | ret = test_client(root, "127.0.0.1", port); |
| 123 | if (ret != 0) return ret; |
| 124 | ret = test_client(root, "::ffff:127.0.0.1", port); |
| 125 | if (ret != 0) return ret; |
| 126 | ret = test_client(root, "localhost", port); |
| 127 | if (ret != 0) return ret; |
| 128 | if (do_ipv6) { |
| 129 | ret = test_client(root, "::1", port); |
| 130 | if (ret != 0) return ret; |
| 131 | } |
| 132 | /* wait for server */ |
| 133 | gpr_log(GPR_INFO, "Waiting for server"); |
| 134 | kill(svr, SIGINT); |
| 135 | if (waitpid(svr, &status, 0) == -1) return 2; |
| 136 | if (!WIFEXITED(status)) return 4; |
| 137 | if (WEXITSTATUS(status)) return WEXITSTATUS(status); |
| 138 | return 0; |
| 139 | } |