Line data Source code
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 :
47 : #include <grpc/support/alloc.h>
48 : #include <grpc/support/host_port.h>
49 : #include <grpc/support/log.h>
50 : #include <grpc/support/string_util.h>
51 : #include "test/core/util/port.h"
52 :
53 : extern "C" {
54 : #include "src/core/iomgr/socket_utils_posix.h"
55 : #include "src/core/support/string.h"
56 : }
57 :
58 4 : int test_client(const char* root, const char* host, int port) {
59 : int status;
60 : pid_t cli;
61 4 : cli = fork();
62 4 : if (cli == 0) {
63 : char* binary_path;
64 : char* port_arg;
65 0 : gpr_asprintf(&binary_path, "%s/interop_client", root);
66 0 : gpr_asprintf(&port_arg, "--server_port=%d", port);
67 :
68 0 : execl(binary_path, binary_path, port_arg, NULL);
69 :
70 0 : gpr_free(binary_path);
71 0 : gpr_free(port_arg);
72 0 : return 1;
73 : }
74 : /* wait for client */
75 4 : gpr_log(GPR_INFO, "Waiting for client: %s", host);
76 4 : if (waitpid(cli, &status, 0) == -1) return 2;
77 4 : if (!WIFEXITED(status)) return 4;
78 4 : if (WEXITSTATUS(status)) return WEXITSTATUS(status);
79 4 : return 0;
80 : }
81 :
82 1 : int main(int argc, char** argv) {
83 1 : char* me = argv[0];
84 1 : char* lslash = strrchr(me, '/');
85 : char root[1024];
86 1 : int port = grpc_pick_unused_port_or_die();
87 : int status;
88 : pid_t svr;
89 : int ret;
90 1 : 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 1 : srand(getpid());
94 1 : if (!grpc_ipv6_loopback_available()) {
95 0 : gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests.");
96 0 : do_ipv6 = 0;
97 : }
98 : /* figure out where we are */
99 1 : if (lslash) {
100 1 : memcpy(root, me, lslash - me);
101 1 : root[lslash - me] = 0;
102 : } else {
103 0 : strcpy(root, ".");
104 : }
105 : /* start the server */
106 1 : svr = fork();
107 1 : if (svr == 0) {
108 : char* binary_path;
109 : char* port_arg;
110 0 : gpr_asprintf(&binary_path, "%s/interop_server", root);
111 0 : gpr_asprintf(&port_arg, "--port=%d", port);
112 :
113 0 : execl(binary_path, binary_path, port_arg, NULL);
114 :
115 0 : gpr_free(binary_path);
116 0 : gpr_free(port_arg);
117 0 : return 1;
118 : }
119 : /* wait a little */
120 1 : sleep(2);
121 : /* start the clients */
122 1 : ret = test_client(root, "127.0.0.1", port);
123 1 : if (ret != 0) return ret;
124 1 : ret = test_client(root, "::ffff:127.0.0.1", port);
125 1 : if (ret != 0) return ret;
126 1 : ret = test_client(root, "localhost", port);
127 1 : if (ret != 0) return ret;
128 1 : if (do_ipv6) {
129 1 : ret = test_client(root, "::1", port);
130 1 : if (ret != 0) return ret;
131 : }
132 : /* wait for server */
133 1 : gpr_log(GPR_INFO, "Waiting for server");
134 1 : kill(svr, SIGINT);
135 1 : if (waitpid(svr, &status, 0) == -1) return 2;
136 1 : if (!WIFEXITED(status)) return 4;
137 1 : if (WEXITSTATUS(status)) return WEXITSTATUS(status);
138 1 : return 0;
139 : }
|