blob: f0fccf461507cfad025be478a5589b25b5316e1f [file] [log] [blame]
Craig Tiller16a6ea62015-02-20 09:08:54 -08001/*
2 *
Craig Tiller2e190362016-03-25 14:33:26 -07003 * Copyright 2015-2016, Google Inc.
Craig Tiller16a6ea62015-02-20 09:08:54 -08004 * 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 Tiller16a6ea62015-02-20 09:08:54 -080038#include <assert.h>
Craig Tiller16a6ea62015-02-20 09:08:54 -080039#include <signal.h>
Craig Tillerf40df232016-03-25 13:38:14 -070040#include <stdio.h>
Craig Tiller16a6ea62015-02-20 09:08:54 -080041#include <stdlib.h>
Craig Tillerf40df232016-03-25 13:38:14 -070042#include <string.h>
Craig Tiller16a6ea62015-02-20 09:08:54 -080043#include <sys/types.h>
44#include <sys/wait.h>
Craig Tillerf40df232016-03-25 13:38:14 -070045#include <unistd.h>
Craig Tiller16a6ea62015-02-20 09:08:54 -080046
Craig Tiller16a6ea62015-02-20 09:08:54 -080047#include <grpc/support/alloc.h>
48#include <grpc/support/host_port.h>
49#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070050#include <grpc/support/string_util.h>
Craig Tiller16a6ea62015-02-20 09:08:54 -080051#include "test/core/util/port.h"
52
yang-g9e2f90c2015-08-21 15:35:03 -070053extern "C" {
54#include "src/core/iomgr/socket_utils_posix.h"
55#include "src/core/support/string.h"
56}
57
Yang Gao6baa9b62015-03-17 10:49:39 -070058int test_client(const char* root, const char* host, int port) {
Craig Tiller16a6ea62015-02-20 09:08:54 -080059 int status;
60 pid_t cli;
61 cli = fork();
62 if (cli == 0) {
Yang Gao6baa9b62015-03-17 10:49:39 -070063 char* binary_path;
64 char* port_arg;
Craig Tiller16a6ea62015-02-20 09:08:54 -080065 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 Gao6baa9b62015-03-17 10:49:39 -070082int main(int argc, char** argv) {
83 char* me = argv[0];
84 char* lslash = strrchr(me, '/');
Craig Tiller16a6ea62015-02-20 09:08:54 -080085 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 Gao6baa9b62015-03-17 10:49:39 -0700108 char* binary_path;
109 char* port_arg;
Craig Tiller16a6ea62015-02-20 09:08:54 -0800110 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}