Mark D. Roth | 5d11e43 | 2016-06-23 13:14:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, 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 | |
| 105 | // Simple request via a server filter that always fails to initialize |
| 106 | // the call. |
| 107 | static void test_request(grpc_end2end_test_config config) { |
| 108 | grpc_call *c; |
| 109 | grpc_call *s; |
| 110 | gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); |
| 111 | grpc_byte_buffer *request_payload = |
| 112 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 113 | gpr_timespec deadline = five_seconds_time(); |
| 114 | grpc_end2end_test_fixture f = |
| 115 | begin_test(config, "filter_call_init_fails", NULL, NULL); |
| 116 | cq_verifier *cqv = cq_verifier_create(f.cq); |
| 117 | grpc_op ops[6]; |
| 118 | grpc_op *op; |
| 119 | grpc_metadata_array initial_metadata_recv; |
| 120 | grpc_metadata_array trailing_metadata_recv; |
| 121 | grpc_metadata_array request_metadata_recv; |
| 122 | grpc_byte_buffer *request_payload_recv = NULL; |
| 123 | grpc_call_details call_details; |
| 124 | grpc_status_code status; |
| 125 | grpc_call_error error; |
| 126 | char *details = NULL; |
| 127 | size_t details_capacity = 0; |
| 128 | |
| 129 | c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 130 | "/foo", "foo.test.google.fr", deadline, NULL); |
| 131 | GPR_ASSERT(c); |
| 132 | |
| 133 | grpc_metadata_array_init(&initial_metadata_recv); |
| 134 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 135 | grpc_metadata_array_init(&request_metadata_recv); |
| 136 | grpc_call_details_init(&call_details); |
| 137 | |
| 138 | memset(ops, 0, sizeof(ops)); |
| 139 | op = ops; |
| 140 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 141 | op->data.send_initial_metadata.count = 0; |
| 142 | op->data.send_initial_metadata.metadata = NULL; |
| 143 | op->flags = 0; |
| 144 | op->reserved = NULL; |
| 145 | op++; |
| 146 | op->op = GRPC_OP_SEND_MESSAGE; |
| 147 | op->data.send_message = request_payload; |
| 148 | op->flags = 0; |
| 149 | op->reserved = NULL; |
| 150 | op++; |
| 151 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 152 | op->flags = 0; |
| 153 | op->reserved = NULL; |
| 154 | op++; |
| 155 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 156 | op->data.recv_initial_metadata = &initial_metadata_recv; |
| 157 | op->flags = 0; |
| 158 | op->reserved = NULL; |
| 159 | op++; |
| 160 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 161 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 162 | op->data.recv_status_on_client.status = &status; |
| 163 | op->data.recv_status_on_client.status_details = &details; |
| 164 | op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 165 | op->flags = 0; |
| 166 | op->reserved = NULL; |
| 167 | op++; |
| 168 | error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); |
| 169 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 170 | |
| 171 | error = |
| 172 | grpc_server_request_call(f.server, &s, &call_details, |
| 173 | &request_metadata_recv, f.cq, f.cq, tag(101)); |
| 174 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 175 | |
| 176 | cq_expect_completion(cqv, tag(1), 1); |
| 177 | cq_verify(cqv); |
| 178 | |
| 179 | GPR_ASSERT(status == GRPC_STATUS_PERMISSION_DENIED); |
| 180 | GPR_ASSERT(0 == strcmp(details, "access denied")); |
| 181 | |
| 182 | gpr_free(details); |
| 183 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 184 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 185 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 186 | grpc_call_details_destroy(&call_details); |
| 187 | |
| 188 | grpc_call_destroy(c); |
| 189 | |
| 190 | cq_verifier_destroy(cqv); |
| 191 | |
| 192 | grpc_byte_buffer_destroy(request_payload); |
| 193 | grpc_byte_buffer_destroy(request_payload_recv); |
| 194 | |
| 195 | end_test(&f); |
| 196 | config.tear_down_data(&f); |
| 197 | } |
| 198 | |
| 199 | /******************************************************************************* |
| 200 | * Test filter - always fails to initialize a call |
| 201 | */ |
| 202 | |
| 203 | static grpc_error* init_call_elem(grpc_exec_ctx *exec_ctx, |
| 204 | grpc_call_element *elem, |
| 205 | grpc_call_element_args *args) { |
| 206 | return grpc_error_set_int(GRPC_ERROR_CREATE("access denied"), |
| 207 | GRPC_ERROR_INT_GRPC_STATUS, |
| 208 | GRPC_STATUS_PERMISSION_DENIED); |
| 209 | } |
| 210 | |
| 211 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 212 | const grpc_call_stats *stats, |
| 213 | void *and_free_memory) {} |
| 214 | |
| 215 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
| 216 | grpc_channel_element *elem, |
| 217 | grpc_channel_element_args *args) {} |
| 218 | |
| 219 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 220 | grpc_channel_element *elem) {} |
| 221 | |
| 222 | static const grpc_channel_filter test_filter = { |
| 223 | grpc_call_next_op, |
| 224 | grpc_channel_next_op, |
| 225 | 0, |
| 226 | init_call_elem, |
| 227 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 228 | destroy_call_elem, |
| 229 | 0, |
| 230 | init_channel_elem, |
| 231 | destroy_channel_elem, |
| 232 | grpc_call_next_get_peer, |
| 233 | "filter_call_init_fails"}; |
| 234 | |
| 235 | /******************************************************************************* |
| 236 | * Registration |
| 237 | */ |
| 238 | |
| 239 | static bool maybe_add_filter(grpc_channel_stack_builder *builder, void *arg) { |
| 240 | if (g_enable_filter) { |
| 241 | return grpc_channel_stack_builder_prepend_filter(builder, &test_filter, |
| 242 | NULL, NULL); |
| 243 | } else { |
| 244 | return true; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static void init_plugin(void) { |
| 249 | grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, 0, maybe_add_filter, |
| 250 | NULL); |
| 251 | } |
| 252 | |
| 253 | static void destroy_plugin(void) {} |
| 254 | |
| 255 | void filter_call_init_fails(grpc_end2end_test_config config) { |
| 256 | g_enable_filter = true; |
| 257 | test_request(config); |
| 258 | g_enable_filter = false; |
| 259 | } |
| 260 | |
| 261 | void filter_call_init_fails_pre_init(void) { |
| 262 | grpc_register_plugin(init_plugin, destroy_plugin); |
| 263 | } |