Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 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 | #include "test/core/end2end/end2end_tests.h" |
| 35 | |
| 36 | #include <stdbool.h> |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include <grpc/byte_buffer.h> |
| 41 | #include <grpc/support/alloc.h> |
| 42 | #include <grpc/support/log.h> |
| 43 | #include <grpc/support/time.h> |
| 44 | #include <grpc/support/useful.h> |
| 45 | #include "src/core/lib/channel/channel_stack_builder.h" |
| 46 | #include "src/core/lib/surface/channel_init.h" |
| 47 | #include "test/core/end2end/cq_verifier.h" |
| 48 | |
| 49 | enum { TIMEOUT = 200000 }; |
| 50 | |
| 51 | static bool g_enable_filter = false; |
| 52 | |
| 53 | static void *tag(intptr_t t) { return (void *)t; } |
| 54 | |
| 55 | static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 56 | const char *test_name, |
| 57 | grpc_channel_args *client_args, |
| 58 | grpc_channel_args *server_args) { |
| 59 | grpc_end2end_test_fixture f; |
| 60 | gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
| 61 | f = config.create_fixture(client_args, server_args); |
| 62 | config.init_server(&f, server_args); |
| 63 | config.init_client(&f, client_args); |
| 64 | return f; |
| 65 | } |
| 66 | |
| 67 | static gpr_timespec n_seconds_time(int n) { |
| 68 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
| 69 | } |
| 70 | |
| 71 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
| 72 | |
| 73 | static void drain_cq(grpc_completion_queue *cq) { |
| 74 | grpc_event ev; |
| 75 | do { |
| 76 | ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); |
| 77 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 78 | } |
| 79 | |
| 80 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 81 | if (!f->server) return; |
| 82 | grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 83 | GPR_ASSERT(grpc_completion_queue_pluck( |
| 84 | f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) |
| 85 | .type == GRPC_OP_COMPLETE); |
| 86 | grpc_server_destroy(f->server); |
| 87 | f->server = NULL; |
| 88 | } |
| 89 | |
| 90 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 91 | if (!f->client) return; |
| 92 | grpc_channel_destroy(f->client); |
| 93 | f->client = NULL; |
| 94 | } |
| 95 | |
| 96 | static void end_test(grpc_end2end_test_fixture *f) { |
| 97 | shutdown_server(f); |
| 98 | shutdown_client(f); |
| 99 | |
| 100 | grpc_completion_queue_shutdown(f->cq); |
| 101 | drain_cq(f->cq); |
| 102 | grpc_completion_queue_destroy(f->cq); |
| 103 | } |
| 104 | |
Craig Tiller | 6c8ae9a | 2016-04-06 15:50:38 -0700 | [diff] [blame] | 105 | /* Simple request via a server filter that always closes the stream.*/ |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 106 | static void test_request(grpc_end2end_test_config config) { |
| 107 | grpc_call *c; |
| 108 | grpc_call *s; |
| 109 | gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); |
| 110 | grpc_byte_buffer *request_payload = |
| 111 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 112 | gpr_timespec deadline = five_seconds_time(); |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 113 | grpc_end2end_test_fixture f = |
| 114 | begin_test(config, "filter_causes_close", NULL, NULL); |
| 115 | cq_verifier *cqv = cq_verifier_create(f.cq); |
| 116 | grpc_op ops[6]; |
| 117 | grpc_op *op; |
| 118 | grpc_metadata_array initial_metadata_recv; |
| 119 | grpc_metadata_array trailing_metadata_recv; |
| 120 | grpc_metadata_array request_metadata_recv; |
| 121 | grpc_byte_buffer *request_payload_recv = NULL; |
| 122 | grpc_call_details call_details; |
| 123 | grpc_status_code status; |
| 124 | grpc_call_error error; |
| 125 | char *details = NULL; |
| 126 | size_t details_capacity = 0; |
| 127 | |
| 128 | c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 129 | "/foo", "foo.test.google.fr", deadline, NULL); |
| 130 | GPR_ASSERT(c); |
| 131 | |
| 132 | grpc_metadata_array_init(&initial_metadata_recv); |
| 133 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 134 | grpc_metadata_array_init(&request_metadata_recv); |
| 135 | grpc_call_details_init(&call_details); |
| 136 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 137 | memset(ops, 0, sizeof(ops)); |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 138 | op = ops; |
| 139 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 140 | op->data.send_initial_metadata.count = 0; |
| 141 | op->data.send_initial_metadata.metadata = NULL; |
| 142 | op->flags = 0; |
| 143 | op->reserved = NULL; |
| 144 | op++; |
| 145 | op->op = GRPC_OP_SEND_MESSAGE; |
| 146 | op->data.send_message = request_payload; |
| 147 | op->flags = 0; |
| 148 | op->reserved = NULL; |
| 149 | op++; |
| 150 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 151 | op->flags = 0; |
| 152 | op->reserved = NULL; |
| 153 | op++; |
| 154 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 155 | op->data.recv_initial_metadata = &initial_metadata_recv; |
| 156 | op->flags = 0; |
| 157 | op->reserved = NULL; |
| 158 | op++; |
| 159 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 160 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 161 | op->data.recv_status_on_client.status = &status; |
| 162 | op->data.recv_status_on_client.status_details = &details; |
| 163 | op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 164 | op->flags = 0; |
| 165 | op->reserved = NULL; |
| 166 | op++; |
| 167 | error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); |
| 168 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 169 | |
| 170 | error = |
| 171 | grpc_server_request_call(f.server, &s, &call_details, |
| 172 | &request_metadata_recv, f.cq, f.cq, tag(101)); |
| 173 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 174 | |
| 175 | cq_expect_completion(cqv, tag(1), 1); |
| 176 | cq_verify(cqv); |
| 177 | |
| 178 | GPR_ASSERT(status == GRPC_STATUS_PERMISSION_DENIED); |
Craig Tiller | 86df5a8 | 2016-05-09 11:23:05 -0700 | [diff] [blame] | 179 | GPR_ASSERT(0 == strcmp(details, "Failure that's not preventable.")); |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 180 | |
| 181 | gpr_free(details); |
| 182 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 183 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 184 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 185 | grpc_call_details_destroy(&call_details); |
| 186 | |
| 187 | grpc_call_destroy(c); |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 188 | |
| 189 | cq_verifier_destroy(cqv); |
| 190 | |
| 191 | grpc_byte_buffer_destroy(request_payload); |
| 192 | grpc_byte_buffer_destroy(request_payload_recv); |
| 193 | |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 194 | end_test(&f); |
| 195 | config.tear_down_data(&f); |
| 196 | } |
| 197 | |
| 198 | /******************************************************************************* |
| 199 | * Test filter - always closes incoming requests |
| 200 | */ |
| 201 | |
| 202 | typedef struct { grpc_closure *recv_im_ready; } call_data; |
| 203 | |
Craig Tiller | c708f8a | 2016-04-04 22:13:15 -0700 | [diff] [blame] | 204 | typedef struct { uint8_t unused; } channel_data; |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 205 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 206 | static void recv_im_ready(grpc_exec_ctx *exec_ctx, void *arg, |
| 207 | grpc_error *error) { |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 208 | grpc_call_element *elem = arg; |
| 209 | call_data *calld = elem->call_data; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 210 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 211 | // close the stream with an error. |
Julien Boeuf | 02fb54e | 2016-04-15 16:25:24 -0700 | [diff] [blame] | 212 | gpr_slice message = |
Craig Tiller | 86df5a8 | 2016-05-09 11:23:05 -0700 | [diff] [blame] | 213 | gpr_slice_from_copied_string("Failure that's not preventable."); |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 214 | grpc_transport_stream_op op; |
| 215 | memset(&op, 0, sizeof(op)); |
| 216 | grpc_transport_stream_op_add_close(&op, GRPC_STATUS_PERMISSION_DENIED, |
| 217 | &message); |
| 218 | grpc_call_next_op(exec_ctx, elem, &op); |
| 219 | } |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 220 | grpc_exec_ctx_sched( |
Craig Tiller | 86df5a8 | 2016-05-09 11:23:05 -0700 | [diff] [blame] | 221 | exec_ctx, calld->recv_im_ready, |
| 222 | GRPC_ERROR_CREATE_REFERENCING("Forced call to close", &error, 1), NULL); |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static void start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 226 | grpc_call_element *elem, |
| 227 | grpc_transport_stream_op *op) { |
| 228 | call_data *calld = elem->call_data; |
| 229 | if (op->recv_initial_metadata != NULL) { |
| 230 | calld->recv_im_ready = op->recv_initial_metadata_ready; |
| 231 | op->recv_initial_metadata_ready = grpc_closure_create(recv_im_ready, elem); |
| 232 | } |
| 233 | grpc_call_next_op(exec_ctx, elem, op); |
| 234 | } |
| 235 | |
| 236 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 237 | grpc_call_element_args *args) {} |
| 238 | |
Craig Tiller | 6c8ae9a | 2016-04-06 15:50:38 -0700 | [diff] [blame] | 239 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 240 | const grpc_call_final_info *final_info, |
Craig Tiller | 6c8ae9a | 2016-04-06 15:50:38 -0700 | [diff] [blame] | 241 | void *and_free_memory) {} |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 242 | |
| 243 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
| 244 | grpc_channel_element *elem, |
| 245 | grpc_channel_element_args *args) {} |
| 246 | |
| 247 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 248 | grpc_channel_element *elem) {} |
| 249 | |
| 250 | static const grpc_channel_filter test_filter = { |
| 251 | start_transport_stream_op, |
| 252 | grpc_channel_next_op, |
| 253 | sizeof(call_data), |
| 254 | init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 255 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
Craig Tiller | a446ee2 | 2016-04-04 13:09:30 -0700 | [diff] [blame] | 256 | destroy_call_elem, |
| 257 | sizeof(channel_data), |
| 258 | init_channel_elem, |
| 259 | destroy_channel_elem, |
| 260 | grpc_call_next_get_peer, |
| 261 | "filter_causes_close"}; |
| 262 | |
| 263 | /******************************************************************************* |
| 264 | * Registration |
| 265 | */ |
| 266 | |
| 267 | static bool maybe_add_filter(grpc_channel_stack_builder *builder, void *arg) { |
| 268 | if (g_enable_filter) { |
| 269 | return grpc_channel_stack_builder_prepend_filter(builder, &test_filter, |
| 270 | NULL, NULL); |
| 271 | } else { |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void init_plugin(void) { |
| 277 | grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, 0, maybe_add_filter, |
| 278 | NULL); |
| 279 | } |
| 280 | |
| 281 | static void destroy_plugin(void) {} |
| 282 | |
| 283 | void filter_causes_close(grpc_end2end_test_config config) { |
| 284 | g_enable_filter = true; |
| 285 | test_request(config); |
| 286 | g_enable_filter = false; |
| 287 | } |
| 288 | |
| 289 | void filter_causes_close_pre_init(void) { |
| 290 | grpc_register_plugin(init_plugin, destroy_plugin); |
| 291 | } |