blob: ec8f9dbe009259effc013b8cab5bdbd568a6e727 [file] [log] [blame]
Craig Tillera446ee22016-04-04 13:09:30 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tillera446ee22016-04-04 13:09:30 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Craig Tillera446ee22016-04-04 13:09:30 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tillera446ee22016-04-04 13:09:30 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Craig Tillera446ee22016-04-04 13:09:30 -070016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdbool.h>
22#include <stdio.h>
23#include <string.h>
24
25#include <grpc/byte_buffer.h>
26#include <grpc/support/alloc.h>
27#include <grpc/support/log.h>
28#include <grpc/support/time.h>
29#include <grpc/support/useful.h>
30#include "src/core/lib/channel/channel_stack_builder.h"
31#include "src/core/lib/surface/channel_init.h"
32#include "test/core/end2end/cq_verifier.h"
33
Craig Tillera446ee22016-04-04 13:09:30 -070034static bool g_enable_filter = false;
35
Craig Tillerbaa14a92017-11-03 09:09:36 -070036static void* tag(intptr_t t) { return (void*)t; }
Craig Tillera446ee22016-04-04 13:09:30 -070037
38static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070039 const char* test_name,
40 grpc_channel_args* client_args,
41 grpc_channel_args* server_args) {
Craig Tillera446ee22016-04-04 13:09:30 -070042 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050043 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Craig Tillera446ee22016-04-04 13:09:30 -070044 f = config.create_fixture(client_args, server_args);
45 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070046 config.init_client(&f, client_args);
Craig Tillera446ee22016-04-04 13:09:30 -070047 return f;
48}
49
Chris Evansed2a5472017-03-27 17:34:51 -050050static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050051 return grpc_timeout_seconds_to_deadline(n);
Craig Tillera446ee22016-04-04 13:09:30 -070052}
53
Chris Evansed2a5472017-03-27 17:34:51 -050054static gpr_timespec five_seconds_from_now(void) {
55 return n_seconds_from_now(5);
56}
Craig Tillera446ee22016-04-04 13:09:30 -070057
Craig Tillerbaa14a92017-11-03 09:09:36 -070058static void drain_cq(grpc_completion_queue* cq) {
Craig Tillera446ee22016-04-04 13:09:30 -070059 grpc_event ev;
60 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080061 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tillera446ee22016-04-04 13:09:30 -070062 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
63}
64
Craig Tillerbaa14a92017-11-03 09:09:36 -070065static void shutdown_server(grpc_end2end_test_fixture* f) {
Craig Tillera446ee22016-04-04 13:09:30 -070066 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080067 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
68 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
69 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080070 nullptr)
Craig Tillera446ee22016-04-04 13:09:30 -070071 .type == GRPC_OP_COMPLETE);
72 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080073 f->server = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -070074}
75
Craig Tillerbaa14a92017-11-03 09:09:36 -070076static void shutdown_client(grpc_end2end_test_fixture* f) {
Craig Tillera446ee22016-04-04 13:09:30 -070077 if (!f->client) return;
78 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080079 f->client = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -070080}
81
Craig Tillerbaa14a92017-11-03 09:09:36 -070082static void end_test(grpc_end2end_test_fixture* f) {
Craig Tillera446ee22016-04-04 13:09:30 -070083 shutdown_server(f);
84 shutdown_client(f);
85
86 grpc_completion_queue_shutdown(f->cq);
87 drain_cq(f->cq);
88 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080089 grpc_completion_queue_destroy(f->shutdown_cq);
Craig Tillera446ee22016-04-04 13:09:30 -070090}
91
Craig Tiller6c8ae9a2016-04-06 15:50:38 -070092/* Simple request via a server filter that always closes the stream.*/
Craig Tillera446ee22016-04-04 13:09:30 -070093static void test_request(grpc_end2end_test_config config) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070094 grpc_call* c;
95 grpc_call* s;
Craig Tiller28b72422016-10-26 21:15:29 -070096 grpc_slice request_payload_slice =
97 grpc_slice_from_copied_string("hello world");
Craig Tillerbaa14a92017-11-03 09:09:36 -070098 grpc_byte_buffer* request_payload =
Craig Tillera446ee22016-04-04 13:09:30 -070099 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
Craig Tillera446ee22016-04-04 13:09:30 -0700100 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800101 begin_test(config, "filter_causes_close", nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700102 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tillera446ee22016-04-04 13:09:30 -0700103 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700104 grpc_op* op;
Craig Tillera446ee22016-04-04 13:09:30 -0700105 grpc_metadata_array initial_metadata_recv;
106 grpc_metadata_array trailing_metadata_recv;
107 grpc_metadata_array request_metadata_recv;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800108 grpc_byte_buffer* request_payload_recv = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -0700109 grpc_call_details call_details;
110 grpc_status_code status;
111 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800112 grpc_slice details;
Craig Tillera446ee22016-04-04 13:09:30 -0700113
Chris Evansed2a5472017-03-27 17:34:51 -0500114 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700115 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800116 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800117 grpc_slice_from_static_string("/foo"),
118 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800119 nullptr);
Craig Tillera446ee22016-04-04 13:09:30 -0700120 GPR_ASSERT(c);
121
122 grpc_metadata_array_init(&initial_metadata_recv);
123 grpc_metadata_array_init(&trailing_metadata_recv);
124 grpc_metadata_array_init(&request_metadata_recv);
125 grpc_call_details_init(&call_details);
126
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700127 memset(ops, 0, sizeof(ops));
Craig Tillera446ee22016-04-04 13:09:30 -0700128 op = ops;
129 op->op = GRPC_OP_SEND_INITIAL_METADATA;
130 op->data.send_initial_metadata.count = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800131 op->data.send_initial_metadata.metadata = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -0700132 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800133 op->reserved = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -0700134 op++;
135 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800136 op->data.send_message.send_message = request_payload;
Craig Tillera446ee22016-04-04 13:09:30 -0700137 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800138 op->reserved = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -0700139 op++;
140 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
141 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800142 op->reserved = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -0700143 op++;
144 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800145 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
Craig Tillera446ee22016-04-04 13:09:30 -0700146 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800147 op->reserved = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -0700148 op++;
149 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
150 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
151 op->data.recv_status_on_client.status = &status;
152 op->data.recv_status_on_client.status_details = &details;
Craig Tillera446ee22016-04-04 13:09:30 -0700153 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800154 op->reserved = nullptr;
Craig Tillera446ee22016-04-04 13:09:30 -0700155 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800156 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Craig Tillera446ee22016-04-04 13:09:30 -0700157 GPR_ASSERT(GRPC_CALL_OK == error);
158
159 error =
160 grpc_server_request_call(f.server, &s, &call_details,
161 &request_metadata_recv, f.cq, f.cq, tag(101));
162 GPR_ASSERT(GRPC_CALL_OK == error);
163
Mark D. Roth7187ab92016-08-24 13:49:22 -0700164 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillera446ee22016-04-04 13:09:30 -0700165 cq_verify(cqv);
166
167 GPR_ASSERT(status == GRPC_STATUS_PERMISSION_DENIED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800168 GPR_ASSERT(0 ==
169 grpc_slice_str_cmp(details, "Failure that's not preventable."));
Craig Tillera446ee22016-04-04 13:09:30 -0700170
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800171 grpc_slice_unref(details);
Craig Tillera446ee22016-04-04 13:09:30 -0700172 grpc_metadata_array_destroy(&initial_metadata_recv);
173 grpc_metadata_array_destroy(&trailing_metadata_recv);
174 grpc_metadata_array_destroy(&request_metadata_recv);
175 grpc_call_details_destroy(&call_details);
176
Craig Tillerdd36b152017-03-31 08:27:28 -0700177 grpc_call_unref(c);
Craig Tillera446ee22016-04-04 13:09:30 -0700178
179 cq_verifier_destroy(cqv);
180
181 grpc_byte_buffer_destroy(request_payload);
182 grpc_byte_buffer_destroy(request_payload_recv);
183
Craig Tillera446ee22016-04-04 13:09:30 -0700184 end_test(&f);
185 config.tear_down_data(&f);
186}
187
188/*******************************************************************************
189 * Test filter - always closes incoming requests
190 */
191
Craig Tillerbaa14a92017-11-03 09:09:36 -0700192typedef struct {
193 grpc_closure* recv_im_ready;
194} call_data;
Craig Tillera446ee22016-04-04 13:09:30 -0700195
Craig Tillerbaa14a92017-11-03 09:09:36 -0700196typedef struct {
197 uint8_t unused;
198} channel_data;
Craig Tillera446ee22016-04-04 13:09:30 -0700199
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800200static void recv_im_ready(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700201 grpc_call_element* elem = (grpc_call_element*)arg;
202 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth764cf042017-09-01 09:00:06 -0700203 GRPC_CLOSURE_RUN(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800204 calld->recv_im_ready,
ncteisen4b36a3d2017-03-13 19:08:06 -0700205 grpc_error_set_int(GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800206 "Failure that's not preventable.", &error, 1),
207 GRPC_ERROR_INT_GRPC_STATUS,
208 GRPC_STATUS_PERMISSION_DENIED));
Craig Tillera446ee22016-04-04 13:09:30 -0700209}
210
Craig Tillere1b51da2017-03-31 15:44:33 -0700211static void start_transport_stream_op_batch(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800212 grpc_call_element* elem, grpc_transport_stream_op_batch* op) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700213 call_data* calld = (call_data*)elem->call_data;
Craig Tiller72920cc2017-03-10 10:20:17 -0800214 if (op->recv_initial_metadata) {
215 calld->recv_im_ready =
216 op->payload->recv_initial_metadata.recv_initial_metadata_ready;
217 op->payload->recv_initial_metadata.recv_initial_metadata_ready =
ncteisen274bbbe2017-06-08 14:57:11 -0700218 GRPC_CLOSURE_CREATE(recv_im_ready, elem, grpc_schedule_on_exec_ctx);
Craig Tillera446ee22016-04-04 13:09:30 -0700219 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800220 grpc_call_next_op(elem, op);
Craig Tillera446ee22016-04-04 13:09:30 -0700221}
222
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800223static grpc_error* init_call_elem(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700224 const grpc_call_element_args* args) {
Mark D. Roth0badbe82016-06-23 10:15:12 -0700225 return GRPC_ERROR_NONE;
226}
Craig Tillera446ee22016-04-04 13:09:30 -0700227
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800228static void destroy_call_elem(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700229 const grpc_call_final_info* final_info,
230 grpc_closure* ignored) {}
Craig Tillera446ee22016-04-04 13:09:30 -0700231
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800232static grpc_error* init_channel_elem(grpc_channel_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700233 grpc_channel_element_args* args) {
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800234 return GRPC_ERROR_NONE;
235}
Craig Tillera446ee22016-04-04 13:09:30 -0700236
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800237static void destroy_channel_elem(grpc_channel_element* elem) {}
Craig Tillera446ee22016-04-04 13:09:30 -0700238
239static const grpc_channel_filter test_filter = {
Craig Tillera0f3abd2017-03-31 15:42:16 -0700240 start_transport_stream_op_batch,
Craig Tillera446ee22016-04-04 13:09:30 -0700241 grpc_channel_next_op,
242 sizeof(call_data),
243 init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700244 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tillera446ee22016-04-04 13:09:30 -0700245 destroy_call_elem,
246 sizeof(channel_data),
247 init_channel_elem,
248 destroy_channel_elem,
Mark D. Rothb2d24882016-10-27 15:44:07 -0700249 grpc_channel_next_get_info,
Craig Tillera446ee22016-04-04 13:09:30 -0700250 "filter_causes_close"};
251
252/*******************************************************************************
253 * Registration
254 */
255
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800256static bool maybe_add_filter(grpc_channel_stack_builder* builder, void* arg) {
Craig Tillera446ee22016-04-04 13:09:30 -0700257 if (g_enable_filter) {
258 return grpc_channel_stack_builder_prepend_filter(builder, &test_filter,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800259 nullptr, nullptr);
Craig Tillera446ee22016-04-04 13:09:30 -0700260 } else {
261 return true;
262 }
263}
264
265static void init_plugin(void) {
266 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, 0, maybe_add_filter,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800267 nullptr);
Craig Tillera446ee22016-04-04 13:09:30 -0700268}
269
270static void destroy_plugin(void) {}
271
272void filter_causes_close(grpc_end2end_test_config config) {
273 g_enable_filter = true;
274 test_request(config);
275 g_enable_filter = false;
276}
277
278void filter_causes_close_pre_init(void) {
279 grpc_register_plugin(init_plugin, destroy_plugin);
280}