Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | #include <grpc/grpc.h> |
Craig Tiller | 3948412 | 2015-01-21 13:37:23 -0800 | [diff] [blame] | 35 | #include <grpc/grpc_http.h> |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 36 | #include <grpc/grpc_security.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 38 | #include <signal.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include <time.h> |
| 43 | |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 44 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include "test/core/util/test_config.h" |
| 46 | #include <grpc/support/alloc.h> |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 47 | #include <grpc/support/cmdline.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include <grpc/support/host_port.h> |
| 49 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | #include <grpc/support/time.h> |
| 51 | #include "test/core/util/port.h" |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 52 | #include "test/core/end2end/data/ssl_test_data.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | |
| 54 | static grpc_completion_queue *cq; |
| 55 | static grpc_server *server; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 56 | static int got_sigint = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | typedef struct { |
| 59 | gpr_refcount pending_ops; |
| 60 | gpr_intmax bytes_read; |
| 61 | } call_state; |
| 62 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 63 | static void request_call(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | call_state *tag = gpr_malloc(sizeof(*tag)); |
| 65 | gpr_ref_init(&tag->pending_ops, 2); |
| 66 | tag->bytes_read = 0; |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 67 | grpc_server_request_call_old(server, tag); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void assert_read_ok(call_state *s, grpc_byte_buffer *b) { |
| 71 | grpc_byte_buffer_reader *bb_reader = NULL; |
| 72 | gpr_slice read_slice; |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 73 | unsigned i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | |
| 75 | bb_reader = grpc_byte_buffer_reader_create(b); |
| 76 | while (grpc_byte_buffer_reader_next(bb_reader, &read_slice)) { |
| 77 | for (i = 0; i < GPR_SLICE_LENGTH(read_slice); i++) { |
| 78 | GPR_ASSERT(GPR_SLICE_START_PTR(read_slice)[i] == s->bytes_read % 256); |
| 79 | s->bytes_read++; |
| 80 | } |
| 81 | gpr_slice_unref(read_slice); |
| 82 | } |
| 83 | grpc_byte_buffer_reader_destroy(bb_reader); |
| 84 | } |
| 85 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 86 | static void sigint_handler(int x) { got_sigint = 1; } |
| 87 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | int main(int argc, char **argv) { |
| 89 | grpc_event *ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | call_state *s; |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 91 | char *addr_buf = NULL; |
| 92 | gpr_cmdline *cl; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 93 | int shutdown_started = 0; |
| 94 | int shutdown_finished = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 96 | int secure = 0; |
| 97 | char *addr = NULL; |
| 98 | |
| 99 | char *fake_argv[1]; |
| 100 | |
Craig Tiller | 3948412 | 2015-01-21 13:37:23 -0800 | [diff] [blame] | 101 | #define MAX_ARGS 4 |
| 102 | grpc_arg arge[MAX_ARGS]; |
| 103 | grpc_arg *e; |
| 104 | grpc_channel_args args = {0, NULL}; |
| 105 | |
| 106 | grpc_http_server_page home_page = {"/", "text/html", |
Craig Tiller | 5996341 | 2015-01-21 14:04:01 -0800 | [diff] [blame] | 107 | "<head>\n" |
| 108 | "<title>Echo Server</title>\n" |
| 109 | "</head>\n" |
| 110 | "<body>\n" |
| 111 | "Welcome to the world of the future!\n" |
| 112 | "</body>\n"}; |
Craig Tiller | 3948412 | 2015-01-21 13:37:23 -0800 | [diff] [blame] | 113 | |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 114 | GPR_ASSERT(argc >= 1); |
| 115 | fake_argv[0] = argv[0]; |
| 116 | grpc_test_init(1, fake_argv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | |
| 118 | grpc_init(); |
| 119 | srand(clock()); |
Craig Tiller | 3948412 | 2015-01-21 13:37:23 -0800 | [diff] [blame] | 120 | memset(arge, 0, sizeof(arge)); |
| 121 | args.args = arge; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 123 | cl = gpr_cmdline_create("echo server"); |
| 124 | gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr); |
| 125 | gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure); |
| 126 | gpr_cmdline_parse(cl, argc, argv); |
| 127 | gpr_cmdline_destroy(cl); |
| 128 | |
Craig Tiller | 3948412 | 2015-01-21 13:37:23 -0800 | [diff] [blame] | 129 | e = &arge[args.num_args++]; |
| 130 | e->type = GRPC_ARG_POINTER; |
| 131 | e->key = GRPC_ARG_SERVE_OVER_HTTP; |
| 132 | e->value.pointer.p = &home_page; |
| 133 | |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 134 | if (addr == NULL) { |
| 135 | gpr_join_host_port(&addr_buf, "::", grpc_pick_unused_port_or_die()); |
| 136 | addr = addr_buf; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | } |
| 138 | gpr_log(GPR_INFO, "creating server on: %s", addr); |
| 139 | |
| 140 | cq = grpc_completion_queue_create(); |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 141 | if (secure) { |
Craig Tiller | 0a20771 | 2015-01-23 08:12:38 -0800 | [diff] [blame] | 142 | grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key, |
| 143 | test_server1_cert}; |
| 144 | grpc_server_credentials *ssl_creds = |
| 145 | grpc_ssl_server_credentials_create(NULL, &pem_key_cert_pair, 1); |
Craig Tiller | 103481e | 2015-01-27 10:18:04 -0800 | [diff] [blame] | 146 | server = grpc_secure_server_create(ssl_creds, cq, &args); |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 147 | GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr)); |
| 148 | grpc_server_credentials_release(ssl_creds); |
| 149 | } else { |
Craig Tiller | 103481e | 2015-01-27 10:18:04 -0800 | [diff] [blame] | 150 | server = grpc_server_create(cq, &args); |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 151 | GPR_ASSERT(grpc_server_add_http2_port(server, addr)); |
| 152 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 153 | grpc_server_start(server); |
| 154 | |
Craig Tiller | 8a84be3 | 2015-01-21 12:29:41 -0800 | [diff] [blame] | 155 | gpr_free(addr_buf); |
| 156 | addr = addr_buf = NULL; |
| 157 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | request_call(); |
| 159 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 160 | signal(SIGINT, sigint_handler); |
| 161 | while (!shutdown_finished) { |
| 162 | if (got_sigint && !shutdown_started) { |
| 163 | gpr_log(GPR_INFO, "Shutting down due to SIGINT"); |
| 164 | grpc_server_shutdown(server); |
| 165 | grpc_completion_queue_shutdown(cq); |
| 166 | shutdown_started = 1; |
| 167 | } |
Craig Tiller | c1f1162 | 2015-02-25 09:09:59 -0800 | [diff] [blame^] | 168 | ev = grpc_completion_queue_next( |
| 169 | cq, gpr_time_add(gpr_now(), gpr_time_from_seconds(1))); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 170 | if (!ev) continue; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 171 | s = ev->tag; |
| 172 | switch (ev->type) { |
| 173 | case GRPC_SERVER_RPC_NEW: |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 174 | if (ev->call != NULL) { |
| 175 | /* initial ops are already started in request_call */ |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 176 | grpc_call_server_accept_old(ev->call, cq, s); |
| 177 | grpc_call_server_end_initial_metadata_old(ev->call, |
| 178 | GRPC_WRITE_BUFFER_HINT); |
| 179 | GPR_ASSERT(grpc_call_start_read_old(ev->call, s) == GRPC_CALL_OK); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 180 | request_call(); |
| 181 | } else { |
| 182 | GPR_ASSERT(shutdown_started); |
| 183 | gpr_free(s); |
| 184 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | break; |
| 186 | case GRPC_WRITE_ACCEPTED: |
| 187 | GPR_ASSERT(ev->data.write_accepted == GRPC_OP_OK); |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 188 | GPR_ASSERT(grpc_call_start_read_old(ev->call, s) == GRPC_CALL_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 189 | break; |
| 190 | case GRPC_READ: |
| 191 | if (ev->data.read) { |
| 192 | assert_read_ok(ev->tag, ev->data.read); |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 193 | GPR_ASSERT(grpc_call_start_write_old(ev->call, ev->data.read, s, |
| 194 | GRPC_WRITE_BUFFER_HINT) == |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 195 | GRPC_CALL_OK); |
| 196 | } else { |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 197 | GPR_ASSERT(grpc_call_start_write_status_old(ev->call, GRPC_STATUS_OK, |
| 198 | NULL, s) == GRPC_CALL_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | } |
| 200 | break; |
| 201 | case GRPC_FINISH_ACCEPTED: |
| 202 | case GRPC_FINISHED: |
| 203 | if (gpr_unref(&s->pending_ops)) { |
| 204 | grpc_call_destroy(ev->call); |
| 205 | gpr_free(s); |
| 206 | } |
| 207 | break; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 208 | case GRPC_QUEUE_SHUTDOWN: |
| 209 | GPR_ASSERT(shutdown_started); |
| 210 | shutdown_finished = 1; |
| 211 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | default: |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 213 | GPR_ASSERT(0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 214 | } |
| 215 | grpc_event_finish(ev); |
| 216 | } |
| 217 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 218 | grpc_server_destroy(server); |
| 219 | grpc_completion_queue_destroy(cq); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 220 | grpc_shutdown(); |
| 221 | |
| 222 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 223 | } |