ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
ctiller | 33023c4 | 2014-12-12 16:28:33 -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 "test/core/end2end/end2end_tests.h" |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 38 | |
| 39 | #include <grpc/byte_buffer.h> |
| 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/time.h> |
| 43 | #include <grpc/support/useful.h> |
| 44 | #include "test/core/end2end/cq_verifier.h" |
| 45 | |
| 46 | enum { TIMEOUT = 200000 }; |
| 47 | |
| 48 | static void *tag(gpr_intptr t) { return (void *)t; } |
| 49 | |
| 50 | static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 51 | const char *test_name, |
| 52 | grpc_channel_args *client_args, |
| 53 | grpc_channel_args *server_args) { |
| 54 | grpc_end2end_test_fixture f; |
| 55 | gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
| 56 | f = config.create_fixture(client_args, server_args); |
| 57 | config.init_client(&f, client_args); |
| 58 | config.init_server(&f, server_args); |
| 59 | return f; |
| 60 | } |
| 61 | |
| 62 | static gpr_timespec n_seconds_time(int n) { |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 63 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 66 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 67 | |
| 68 | static void drain_cq(grpc_completion_queue *cq) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 69 | grpc_event ev; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 70 | do { |
| 71 | ev = grpc_completion_queue_next(cq, five_seconds_time()); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 72 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 76 | if (!f->server) return; |
| 77 | grpc_server_shutdown(f->server); |
| 78 | grpc_server_destroy(f->server); |
| 79 | f->server = NULL; |
| 80 | } |
| 81 | |
| 82 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 83 | if (!f->client) return; |
| 84 | grpc_channel_destroy(f->client); |
| 85 | f->client = NULL; |
| 86 | } |
| 87 | |
| 88 | static void end_test(grpc_end2end_test_fixture *f) { |
| 89 | shutdown_server(f); |
| 90 | shutdown_client(f); |
| 91 | |
| 92 | grpc_completion_queue_shutdown(f->server_cq); |
| 93 | drain_cq(f->server_cq); |
| 94 | grpc_completion_queue_destroy(f->server_cq); |
| 95 | grpc_completion_queue_shutdown(f->client_cq); |
| 96 | drain_cq(f->client_cq); |
| 97 | grpc_completion_queue_destroy(f->client_cq); |
| 98 | } |
| 99 | |
| 100 | /* Request/response with metadata and payload.*/ |
| 101 | static void test_request_response_with_metadata_and_payload( |
| 102 | grpc_end2end_test_config config) { |
| 103 | grpc_call *c; |
| 104 | grpc_call *s; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 105 | gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); |
| 106 | gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); |
| 107 | grpc_byte_buffer *request_payload = |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame^] | 108 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 109 | grpc_byte_buffer *response_payload = |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame^] | 110 | grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 111 | gpr_timespec deadline = five_seconds_time(); |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 112 | grpc_metadata meta_c[2] = { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 113 | {"key1-bin", |
| 114 | "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc", |
| 115 | 13, |
| 116 | {{NULL, NULL, NULL}}}, |
| 117 | {"key2-bin", |
| 118 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d", |
| 119 | 14, |
| 120 | {{NULL, NULL, NULL}}}}; |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 121 | grpc_metadata meta_s[2] = { |
| 122 | {"key3-bin", |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 123 | "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee", |
| 124 | 15, |
| 125 | {{NULL, NULL, NULL}}}, |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 126 | {"key4-bin", |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 127 | "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", |
| 128 | 16, |
| 129 | {{NULL, NULL, NULL}}}}; |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 130 | grpc_end2end_test_fixture f = begin_test(config, "test_request_response_with_metadata_and_payload", NULL, NULL); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 131 | cq_verifier *v_client = cq_verifier_create(f.client_cq); |
| 132 | cq_verifier *v_server = cq_verifier_create(f.server_cq); |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 133 | grpc_op ops[6]; |
| 134 | grpc_op *op; |
| 135 | grpc_metadata_array initial_metadata_recv; |
| 136 | grpc_metadata_array trailing_metadata_recv; |
| 137 | grpc_metadata_array request_metadata_recv; |
| 138 | grpc_byte_buffer *request_payload_recv = NULL; |
| 139 | grpc_byte_buffer *response_payload_recv = NULL; |
| 140 | grpc_call_details call_details; |
| 141 | grpc_status_code status; |
| 142 | char *details = NULL; |
| 143 | size_t details_capacity = 0; |
| 144 | int was_cancelled = 2; |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 145 | |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 146 | c = grpc_channel_create_call(f.client, f.client_cq, "/foo", |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 147 | "foo.test.google.fr", deadline); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 148 | GPR_ASSERT(c); |
| 149 | |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 150 | grpc_metadata_array_init(&initial_metadata_recv); |
| 151 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 152 | grpc_metadata_array_init(&request_metadata_recv); |
| 153 | grpc_call_details_init(&call_details); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 154 | |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 155 | op = ops; |
| 156 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 157 | op->data.send_initial_metadata.count = 2; |
| 158 | op->data.send_initial_metadata.metadata = meta_c; |
| 159 | op++; |
| 160 | op->op = GRPC_OP_SEND_MESSAGE; |
| 161 | op->data.send_message = request_payload; |
| 162 | op++; |
| 163 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 164 | op++; |
| 165 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 166 | op->data.recv_initial_metadata = &initial_metadata_recv; |
| 167 | op++; |
| 168 | op->op = GRPC_OP_RECV_MESSAGE; |
| 169 | op->data.recv_message = &response_payload_recv; |
| 170 | op++; |
| 171 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 172 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 173 | op->data.recv_status_on_client.status = &status; |
| 174 | op->data.recv_status_on_client.status_details = &details; |
| 175 | op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 176 | op++; |
| 177 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1))); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 178 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 179 | GPR_ASSERT(GRPC_CALL_OK == |
| 180 | grpc_server_request_call(f.server, &s, &call_details, |
| 181 | &request_metadata_recv, f.server_cq, |
| 182 | f.server_cq, tag(101))); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 183 | cq_expect_completion(v_server, tag(101), 1); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 184 | cq_verify(v_server); |
| 185 | |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 186 | op = ops; |
| 187 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 188 | op->data.send_initial_metadata.count = 2; |
| 189 | op->data.send_initial_metadata.metadata = meta_s; |
| 190 | op++; |
Craig Tiller | 4541f33 | 2015-05-19 16:26:33 -0700 | [diff] [blame] | 191 | op->op = GRPC_OP_RECV_MESSAGE; |
| 192 | op->data.recv_message = &request_payload_recv; |
| 193 | op++; |
| 194 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102))); |
| 195 | |
| 196 | cq_expect_completion(v_server, tag(102), 1); |
| 197 | cq_verify(v_server); |
| 198 | |
| 199 | op = ops; |
| 200 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 201 | op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 202 | op++; |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 203 | op->op = GRPC_OP_SEND_MESSAGE; |
| 204 | op->data.send_message = response_payload; |
| 205 | op++; |
| 206 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 207 | op->data.send_status_from_server.trailing_metadata_count = 0; |
Craig Tiller | d1abc81 | 2015-05-06 14:35:19 -0700 | [diff] [blame] | 208 | op->data.send_status_from_server.status = GRPC_STATUS_OK; |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 209 | op->data.send_status_from_server.status_details = "xyz"; |
| 210 | op++; |
Craig Tiller | 4541f33 | 2015-05-19 16:26:33 -0700 | [diff] [blame] | 211 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(103))); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 212 | |
Craig Tiller | 4541f33 | 2015-05-19 16:26:33 -0700 | [diff] [blame] | 213 | cq_expect_completion(v_server, tag(103), 1); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 214 | cq_verify(v_server); |
| 215 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 216 | cq_expect_completion(v_client, tag(1), 1); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 217 | cq_verify(v_client); |
| 218 | |
Craig Tiller | d1abc81 | 2015-05-06 14:35:19 -0700 | [diff] [blame] | 219 | GPR_ASSERT(status == GRPC_STATUS_OK); |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 220 | GPR_ASSERT(0 == strcmp(details, "xyz")); |
| 221 | GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 222 | GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); |
Craig Tiller | e801eb3 | 2015-02-10 14:01:14 -0800 | [diff] [blame] | 223 | GPR_ASSERT(was_cancelled == 0); |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 224 | GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); |
| 225 | GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you")); |
| 226 | GPR_ASSERT(contains_metadata( |
| 227 | &request_metadata_recv, "key1-bin", |
| 228 | "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc")); |
| 229 | GPR_ASSERT(contains_metadata( |
| 230 | &request_metadata_recv, "key2-bin", |
| 231 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d")); |
| 232 | GPR_ASSERT(contains_metadata( |
| 233 | &initial_metadata_recv, "key3-bin", |
| 234 | "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee")); |
| 235 | GPR_ASSERT(contains_metadata( |
| 236 | &initial_metadata_recv, "key4-bin", |
| 237 | "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff")); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 238 | |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 239 | gpr_free(details); |
| 240 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 241 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 242 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 243 | grpc_call_details_destroy(&call_details); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 244 | |
| 245 | grpc_call_destroy(c); |
| 246 | grpc_call_destroy(s); |
| 247 | |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 248 | cq_verifier_destroy(v_client); |
| 249 | cq_verifier_destroy(v_server); |
Craig Tiller | bea3840 | 2015-02-05 13:01:33 -0800 | [diff] [blame] | 250 | |
| 251 | grpc_byte_buffer_destroy(request_payload); |
| 252 | grpc_byte_buffer_destroy(response_payload); |
| 253 | grpc_byte_buffer_destroy(request_payload_recv); |
| 254 | grpc_byte_buffer_destroy(response_payload_recv); |
| 255 | |
| 256 | end_test(&f); |
| 257 | config.tear_down_data(&f); |
ctiller | 33023c4 | 2014-12-12 16:28:33 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void grpc_end2end_tests(grpc_end2end_test_config config) { |
| 261 | test_request_response_with_metadata_and_payload(config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 262 | } |