~ubuntu-branches/ubuntu/wily/grpc/wily-proposed

« back to all changes in this revision

Viewing changes to test/cpp/interop/interop_test.cc

  • Committer: Package Import Robot
  • Author(s): Andrew Pollock
  • Date: 2015-05-07 13:28:11 UTC
  • Revision ID: package-import@ubuntu.com-20150507132811-ybm4hfq73tnvvd2e
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
extern "C" {
 
48
#include "src/core/iomgr/socket_utils_posix.h"
 
49
#include "src/core/support/string.h"
 
50
}
 
51
 
 
52
#include <grpc/support/alloc.h>
 
53
#include <grpc/support/host_port.h>
 
54
#include <grpc/support/log.h>
 
55
#include <grpc/support/string_util.h>
 
56
#include "test/core/util/port.h"
 
57
 
 
58
int test_client(const char* root, const char* host, int port) {
 
59
  int status;
 
60
  pid_t cli;
 
61
  cli = fork();
 
62
  if (cli == 0) {
 
63
    char* binary_path;
 
64
    char* port_arg;
 
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
 
 
82
int main(int argc, char** argv) {
 
83
  char* me = argv[0];
 
84
  char* lslash = strrchr(me, '/');
 
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) {
 
108
    char* binary_path;
 
109
    char* port_arg;
 
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
}