Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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 | |
| 38 | #include <unistd.h> |
| 39 | #include <assert.h> |
| 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | #include <signal.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/wait.h> |
| 46 | |
Craig Tiller | fe8af4e | 2015-02-21 07:04:49 -0800 | [diff] [blame^] | 47 | extern "C" { |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 48 | #include "src/core/iomgr/socket_utils_posix.h" |
| 49 | #include "src/core/support/string.h" |
Craig Tiller | fe8af4e | 2015-02-21 07:04:49 -0800 | [diff] [blame^] | 50 | } |
| 51 | |
Craig Tiller | 16a6ea6 | 2015-02-20 09:08:54 -0800 | [diff] [blame] | 52 | #include <grpc/support/alloc.h> |
| 53 | #include <grpc/support/host_port.h> |
| 54 | #include <grpc/support/log.h> |
| 55 | #include "test/core/util/port.h" |
| 56 | |
| 57 | int test_client(const char *root, const char *host, int port) { |
| 58 | int status; |
| 59 | pid_t cli; |
| 60 | cli = fork(); |
| 61 | if (cli == 0) { |
| 62 | char *binary_path; |
| 63 | char *port_arg; |
| 64 | gpr_asprintf(&binary_path, "%s/interop_client", root); |
| 65 | gpr_asprintf(&port_arg, "--server_port=%d", port); |
| 66 | |
| 67 | execl(binary_path, binary_path, port_arg, NULL); |
| 68 | |
| 69 | gpr_free(binary_path); |
| 70 | gpr_free(port_arg); |
| 71 | return 1; |
| 72 | } |
| 73 | /* wait for client */ |
| 74 | gpr_log(GPR_INFO, "Waiting for client: %s", host); |
| 75 | if (waitpid(cli, &status, 0) == -1) return 2; |
| 76 | if (!WIFEXITED(status)) return 4; |
| 77 | if (WEXITSTATUS(status)) return WEXITSTATUS(status); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int main(int argc, char **argv) { |
| 82 | char *me = argv[0]; |
| 83 | char *lslash = strrchr(me, '/'); |
| 84 | char root[1024]; |
| 85 | int port = grpc_pick_unused_port_or_die(); |
| 86 | int status; |
| 87 | pid_t svr; |
| 88 | int ret; |
| 89 | int do_ipv6 = 1; |
| 90 | /* seed rng with pid, so we don't end up with the same random numbers as a |
| 91 | concurrently running test binary */ |
| 92 | srand(getpid()); |
| 93 | if (!grpc_ipv6_loopback_available()) { |
| 94 | gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests."); |
| 95 | do_ipv6 = 0; |
| 96 | } |
| 97 | /* figure out where we are */ |
| 98 | if (lslash) { |
| 99 | memcpy(root, me, lslash - me); |
| 100 | root[lslash - me] = 0; |
| 101 | } else { |
| 102 | strcpy(root, "."); |
| 103 | } |
| 104 | /* start the server */ |
| 105 | svr = fork(); |
| 106 | if (svr == 0) { |
| 107 | char *binary_path; |
| 108 | char *port_arg; |
| 109 | gpr_asprintf(&binary_path, "%s/interop_server", root); |
| 110 | gpr_asprintf(&port_arg, "--port=%d", port); |
| 111 | |
| 112 | execl(binary_path, binary_path, port_arg, NULL); |
| 113 | |
| 114 | gpr_free(binary_path); |
| 115 | gpr_free(port_arg); |
| 116 | return 1; |
| 117 | } |
| 118 | /* wait a little */ |
| 119 | sleep(2); |
| 120 | /* start the clients */ |
| 121 | ret = test_client(root, "127.0.0.1", port); |
| 122 | if (ret != 0) return ret; |
| 123 | ret = test_client(root, "::ffff:127.0.0.1", port); |
| 124 | if (ret != 0) return ret; |
| 125 | ret = test_client(root, "localhost", port); |
| 126 | if (ret != 0) return ret; |
| 127 | if (do_ipv6) { |
| 128 | ret = test_client(root, "::1", port); |
| 129 | if (ret != 0) return ret; |
| 130 | } |
| 131 | /* wait for server */ |
| 132 | gpr_log(GPR_INFO, "Waiting for server"); |
| 133 | kill(svr, SIGINT); |
| 134 | if (waitpid(svr, &status, 0) == -1) return 2; |
| 135 | if (!WIFEXITED(status)) return 4; |
| 136 | if (WEXITSTATUS(status)) return WEXITSTATUS(status); |
| 137 | return 0; |
| 138 | } |