blob: afc56c8dfa727282b7adbdba2f2121d27738937a [file] [log] [blame]
ctiller2845cad2014-12-15 15:14:12 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
ctiller2845cad2014-12-15 15:14:12 -08004 *
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
ctiller2845cad2014-12-15 15:14:12 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
ctiller2845cad2014-12-15 15:14:12 -080010 *
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.
ctiller2845cad2014-12-15 15:14:12 -080016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
ctiller2845cad2014-12-15 15:14:12 -080023
24#include <grpc/byte_buffer.h>
25#include <grpc/support/alloc.h>
26#include <grpc/support/log.h>
27#include <grpc/support/time.h>
28#include <grpc/support/useful.h>
29#include "test/core/end2end/cq_verifier.h"
30
Craig Tillerbaa14a92017-11-03 09:09:36 -070031static void* tag(intptr_t t) { return (void*)t; }
ctiller2845cad2014-12-15 15:14:12 -080032
33static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070034 const char* test_name,
35 grpc_channel_args* client_args,
36 grpc_channel_args* server_args) {
ctiller2845cad2014-12-15 15:14:12 -080037 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050038 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
ctiller2845cad2014-12-15 15:14:12 -080039 f = config.create_fixture(client_args, server_args);
ctiller2845cad2014-12-15 15:14:12 -080040 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070041 config.init_client(&f, client_args);
ctiller2845cad2014-12-15 15:14:12 -080042 return f;
43}
44
Chris Evansed2a5472017-03-27 17:34:51 -050045static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050046 return grpc_timeout_seconds_to_deadline(n);
ctiller2845cad2014-12-15 15:14:12 -080047}
48
Chris Evansed2a5472017-03-27 17:34:51 -050049static gpr_timespec five_seconds_from_now(void) {
50 return n_seconds_from_now(5);
51}
ctiller2845cad2014-12-15 15:14:12 -080052
Craig Tillerbaa14a92017-11-03 09:09:36 -070053static void drain_cq(grpc_completion_queue* cq) {
Craig Tillerdd67dba2015-05-08 16:46:50 -070054 grpc_event ev;
ctiller2845cad2014-12-15 15:14:12 -080055 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080056 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tillerdd67dba2015-05-08 16:46:50 -070057 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
ctiller2845cad2014-12-15 15:14:12 -080058}
59
Craig Tillerbaa14a92017-11-03 09:09:36 -070060static void shutdown_server(grpc_end2end_test_fixture* f) {
ctiller2845cad2014-12-15 15:14:12 -080061 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080062 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
63 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
64 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080065 nullptr)
Craig Tillerf40df232016-03-25 13:38:14 -070066 .type == GRPC_OP_COMPLETE);
ctiller2845cad2014-12-15 15:14:12 -080067 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080068 f->server = nullptr;
ctiller2845cad2014-12-15 15:14:12 -080069}
70
Craig Tillerbaa14a92017-11-03 09:09:36 -070071static void shutdown_client(grpc_end2end_test_fixture* f) {
ctiller2845cad2014-12-15 15:14:12 -080072 if (!f->client) return;
73 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080074 f->client = nullptr;
ctiller2845cad2014-12-15 15:14:12 -080075}
76
Craig Tillerbaa14a92017-11-03 09:09:36 -070077static void end_test(grpc_end2end_test_fixture* f) {
ctiller2845cad2014-12-15 15:14:12 -080078 shutdown_server(f);
79 shutdown_client(f);
80
Craig Tiller5b2a3222015-05-17 14:44:21 -070081 grpc_completion_queue_shutdown(f->cq);
82 drain_cq(f->cq);
83 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080084 grpc_completion_queue_destroy(f->shutdown_cq);
ctiller2845cad2014-12-15 15:14:12 -080085}
86
87/* Request/response with metadata and payload.*/
88static void test_request_response_with_metadata_and_payload(
89 grpc_end2end_test_config config) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070090 grpc_call* c;
91 grpc_call* s;
Craig Tiller28b72422016-10-26 21:15:29 -070092 grpc_slice request_payload_slice =
93 grpc_slice_from_copied_string("hello world");
94 grpc_slice response_payload_slice =
95 grpc_slice_from_copied_string("hello you");
Craig Tillerbaa14a92017-11-03 09:09:36 -070096 grpc_byte_buffer* request_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -070097 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
Craig Tillerbaa14a92017-11-03 09:09:36 -070098 grpc_byte_buffer* response_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -070099 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800100 grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"),
101 grpc_slice_from_static_string("val1"),
102 0,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800103 {{nullptr, nullptr, nullptr, nullptr}}},
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800104 {grpc_slice_from_static_string("key2"),
105 grpc_slice_from_static_string("val2"),
106 0,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800107 {{nullptr, nullptr, nullptr, nullptr}}}};
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800108 grpc_metadata meta_s[2] = {{grpc_slice_from_static_string("key3"),
109 grpc_slice_from_static_string("val3"),
110 0,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800111 {{nullptr, nullptr, nullptr, nullptr}}},
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800112 {grpc_slice_from_static_string("key4"),
113 grpc_slice_from_static_string("val4"),
114 0,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800115 {{nullptr, nullptr, nullptr, nullptr}}}};
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800116 grpc_metadata meta_t[2] = {{grpc_slice_from_static_string("key5"),
117 grpc_slice_from_static_string("val5"),
118 0,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800119 {{nullptr, nullptr, nullptr, nullptr}}},
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800120 {grpc_slice_from_static_string("key6"),
121 grpc_slice_from_static_string("val6"),
122 0,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800123 {{nullptr, nullptr, nullptr, nullptr}}}};
Craig Tillerbe98d242017-11-10 15:26:57 -0800124 grpc_end2end_test_fixture f =
125 begin_test(config, "test_request_response_with_metadata_and_payload",
126 nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700127 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tillerc9903bc2015-02-05 13:11:02 -0800128 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700129 grpc_op* op;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800130 grpc_metadata_array initial_metadata_recv;
131 grpc_metadata_array trailing_metadata_recv;
132 grpc_metadata_array request_metadata_recv;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800133 grpc_byte_buffer* request_payload_recv = nullptr;
134 grpc_byte_buffer* response_payload_recv = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800135 grpc_call_details call_details;
136 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200137 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800138 grpc_slice details;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800139 int was_cancelled = 2;
ctiller2845cad2014-12-15 15:14:12 -0800140
Chris Evansed2a5472017-03-27 17:34:51 -0500141 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700142 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800143 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800144 grpc_slice_from_static_string("/foo"),
145 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800146 nullptr);
ctiller2845cad2014-12-15 15:14:12 -0800147 GPR_ASSERT(c);
148
Craig Tillerc9903bc2015-02-05 13:11:02 -0800149 grpc_metadata_array_init(&initial_metadata_recv);
150 grpc_metadata_array_init(&trailing_metadata_recv);
151 grpc_metadata_array_init(&request_metadata_recv);
152 grpc_call_details_init(&call_details);
ctiller2845cad2014-12-15 15:14:12 -0800153
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700154 memset(ops, 0, sizeof(ops));
Craig Tillerc9903bc2015-02-05 13:11:02 -0800155 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;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700159 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800160 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800161 op++;
162 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800163 op->data.send_message.send_message = request_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700164 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800165 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800166 op++;
167 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700168 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800169 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800170 op++;
171 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800172 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700173 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800174 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800175 op++;
176 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800177 op->data.recv_message.recv_message = &response_payload_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700178 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800179 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800180 op++;
181 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
182 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
183 op->data.recv_status_on_client.status = &status;
184 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700185 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800186 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800187 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800188 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200189 GPR_ASSERT(GRPC_CALL_OK == error);
ctiller2845cad2014-12-15 15:14:12 -0800190
Craig Tillerd6c98df2015-08-18 09:33:44 -0700191 error =
192 grpc_server_request_call(f.server, &s, &call_details,
193 &request_metadata_recv, f.cq, f.cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200194 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700195 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tiller5b2a3222015-05-17 14:44:21 -0700196 cq_verify(cqv);
ctiller2845cad2014-12-15 15:14:12 -0800197
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700198 memset(ops, 0, sizeof(ops));
Craig Tillerc9903bc2015-02-05 13:11:02 -0800199 op = ops;
200 op->op = GRPC_OP_SEND_INITIAL_METADATA;
201 op->data.send_initial_metadata.count = 2;
202 op->data.send_initial_metadata.metadata = meta_s;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700203 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800204 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800205 op++;
Craig Tiller4541f332015-05-19 16:26:33 -0700206 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800207 op->data.recv_message.recv_message = &request_payload_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700208 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800209 op->reserved = nullptr;
Craig Tiller4541f332015-05-19 16:26:33 -0700210 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800211 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200212 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller4541f332015-05-19 16:26:33 -0700213
Mark D. Roth7187ab92016-08-24 13:49:22 -0700214 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
Craig Tillerdad88a72015-05-26 17:01:09 -0700215 cq_verify(cqv);
216
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700217 memset(ops, 0, sizeof(ops));
Craig Tiller4541f332015-05-19 16:26:33 -0700218 op = ops;
219 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
220 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700221 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800222 op->reserved = nullptr;
Craig Tiller4541f332015-05-19 16:26:33 -0700223 op++;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800224 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800225 op->data.send_message.send_message = response_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700226 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800227 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800228 op++;
229 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
230 op->data.send_status_from_server.trailing_metadata_count = 2;
231 op->data.send_status_from_server.trailing_metadata = meta_t;
Craig Tillerd1abc812015-05-06 14:35:19 -0700232 op->data.send_status_from_server.status = GRPC_STATUS_OK;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800233 grpc_slice status_details = grpc_slice_from_static_string("xyz");
234 op->data.send_status_from_server.status_details = &status_details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700235 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800236 op->reserved = nullptr;
Craig Tillerc9903bc2015-02-05 13:11:02 -0800237 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800238 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200239 GPR_ASSERT(GRPC_CALL_OK == error);
ctiller2845cad2014-12-15 15:14:12 -0800240
Mark D. Roth7187ab92016-08-24 13:49:22 -0700241 CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
242 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tiller5b2a3222015-05-17 14:44:21 -0700243 cq_verify(cqv);
ctiller2845cad2014-12-15 15:14:12 -0800244
Craig Tillerd1abc812015-05-06 14:35:19 -0700245 GPR_ASSERT(status == GRPC_STATUS_OK);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800246 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
247 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700248 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
249 config);
Craig Tillerc9903bc2015-02-05 13:11:02 -0800250 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
251 GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you"));
252 GPR_ASSERT(contains_metadata(&request_metadata_recv, "key1", "val1"));
253 GPR_ASSERT(contains_metadata(&request_metadata_recv, "key2", "val2"));
254 GPR_ASSERT(contains_metadata(&initial_metadata_recv, "key3", "val3"));
255 GPR_ASSERT(contains_metadata(&initial_metadata_recv, "key4", "val4"));
256 GPR_ASSERT(contains_metadata(&trailing_metadata_recv, "key5", "val5"));
257 GPR_ASSERT(contains_metadata(&trailing_metadata_recv, "key6", "val6"));
ctiller2845cad2014-12-15 15:14:12 -0800258
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800259 grpc_slice_unref(details);
Craig Tillerc9903bc2015-02-05 13:11:02 -0800260 grpc_metadata_array_destroy(&initial_metadata_recv);
261 grpc_metadata_array_destroy(&trailing_metadata_recv);
262 grpc_metadata_array_destroy(&request_metadata_recv);
263 grpc_call_details_destroy(&call_details);
ctiller2845cad2014-12-15 15:14:12 -0800264
Craig Tillerdd36b152017-03-31 08:27:28 -0700265 grpc_call_unref(c);
266 grpc_call_unref(s);
ctiller2845cad2014-12-15 15:14:12 -0800267
Craig Tiller5b2a3222015-05-17 14:44:21 -0700268 cq_verifier_destroy(cqv);
Craig Tillerc9903bc2015-02-05 13:11:02 -0800269
270 grpc_byte_buffer_destroy(request_payload);
271 grpc_byte_buffer_destroy(response_payload);
272 grpc_byte_buffer_destroy(request_payload_recv);
273 grpc_byte_buffer_destroy(response_payload_recv);
274
275 end_test(&f);
276 config.tear_down_data(&f);
ctiller2845cad2014-12-15 15:14:12 -0800277}
278
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800279void trailing_metadata(grpc_end2end_test_config config) {
ctiller2845cad2014-12-15 15:14:12 -0800280 test_request_response_with_metadata_and_payload(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800281}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700282
283void trailing_metadata_pre_init(void) {}