blob: 2e9513b9cbe29972443377acefaae0cf874c0b2f [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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; }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039 f = config.create_fixture(client_args, server_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070041 config.init_client(&f, client_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
Craig Tillerbaa14a92017-11-03 09:09:36 -070053static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070054 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080056 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller64be9f72015-05-04 14:53:51 -070057 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058}
59
Craig Tillerbaa14a92017-11-03 09:09:36 -070060static void shutdown_server(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080068 f->server = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069}
70
Craig Tillerbaa14a92017-11-03 09:09:36 -070071static void shutdown_client(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072 if (!f->client) return;
73 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080074 f->client = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075}
76
Craig Tillerbaa14a92017-11-03 09:09:36 -070077static void end_test(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 shutdown_server(f);
79 shutdown_client(f);
80
Craig Tillerbc0ec332015-05-11 12:11:32 -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);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085}
86
Craig Tiller28b72422016-10-26 21:15:29 -070087/* Creates and returns a grpc_slice containing random alphanumeric characters.
88 */
Craig Tillerd41a4a72016-10-26 16:16:06 -070089static grpc_slice generate_random_slice() {
Robbie Shadebd10faa2016-07-15 15:27:01 -040090 size_t i;
Robbie Shade61aed922016-07-15 16:38:44 -040091 static const char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890";
Craig Tillerbaa14a92017-11-03 09:09:36 -070092 char* output;
Craig Tiller68cf8ce2016-10-17 13:33:19 -070093 const size_t output_size = 1024 * 1024;
Craig Tillerbaa14a92017-11-03 09:09:36 -070094 output = (char*)gpr_malloc(output_size);
Craig Tiller68cf8ce2016-10-17 13:33:19 -070095 for (i = 0; i < output_size - 1; ++i) {
Robbie Shade61aed922016-07-15 16:38:44 -040096 output[i] = chars[rand() % (int)(sizeof(chars) - 1)];
Robbie Shade2449e192016-07-15 15:16:25 -040097 }
Craig Tiller68cf8ce2016-10-17 13:33:19 -070098 output[output_size - 1] = '\0';
Craig Tillerd41a4a72016-10-26 16:16:06 -070099 grpc_slice out = grpc_slice_from_copied_string(output);
Craig Tiller68cf8ce2016-10-17 13:33:19 -0700100 gpr_free(output);
101 return out;
Robbie Shade2449e192016-07-15 15:16:25 -0400102}
103
Muxi Yan5ebd3272016-10-31 07:27:07 -0700104static void request_response_with_payload(grpc_end2end_test_config config,
105 grpc_end2end_test_fixture f) {
Robbie Shade2449e192016-07-15 15:16:25 -0400106 /* Create large request and response bodies. These are big enough to require
107 * multiple round trips to deliver to the peer, and their exact contents of
108 * will be verified on completion. */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700109 grpc_slice request_payload_slice = generate_random_slice();
110 grpc_slice response_payload_slice = generate_random_slice();
Robbie Shade2449e192016-07-15 15:16:25 -0400111
Craig Tillerbaa14a92017-11-03 09:09:36 -0700112 grpc_call* c;
113 grpc_call* s;
114 grpc_byte_buffer* request_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700115 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700116 grpc_byte_buffer* response_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700117 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700118 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800119 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700120 grpc_op* op;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800121 grpc_metadata_array initial_metadata_recv;
122 grpc_metadata_array trailing_metadata_recv;
123 grpc_metadata_array request_metadata_recv;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800124 grpc_byte_buffer* request_payload_recv = nullptr;
125 grpc_byte_buffer* response_payload_recv = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800126 grpc_call_details call_details;
127 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200128 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800129 grpc_slice details;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800130 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131
Chris Evansed2a5472017-03-27 17:34:51 -0500132 gpr_timespec deadline = n_seconds_from_now(60);
Muxi Yan5ebd3272016-10-31 07:27:07 -0700133 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800134 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800135 grpc_slice_from_static_string("/foo"),
136 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800137 nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138 GPR_ASSERT(c);
139
Craig Tiller24fc2c42015-02-04 13:01:16 -0800140 grpc_metadata_array_init(&initial_metadata_recv);
141 grpc_metadata_array_init(&trailing_metadata_recv);
142 grpc_metadata_array_init(&request_metadata_recv);
143 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700145 memset(ops, 0, sizeof(ops));
Craig Tiller24fc2c42015-02-04 13:01:16 -0800146 op = ops;
147 op->op = GRPC_OP_SEND_INITIAL_METADATA;
148 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700149 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800150 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800151 op++;
152 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800153 op->data.send_message.send_message = request_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700154 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800155 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800156 op++;
157 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700158 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800159 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800160 op++;
161 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800162 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700163 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800164 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800165 op++;
166 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800167 op->data.recv_message.recv_message = &response_payload_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700168 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800169 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800170 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;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700175 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800176 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800177 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800178 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200179 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800180
Craig Tillerd6c98df2015-08-18 09:33:44 -0700181 error =
182 grpc_server_request_call(f.server, &s, &call_details,
183 &request_metadata_recv, f.cq, f.cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200184 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700185 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700186 cq_verify(cqv);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800187
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700188 memset(ops, 0, sizeof(ops));
Craig Tiller24fc2c42015-02-04 13:01:16 -0800189 op = ops;
190 op->op = GRPC_OP_SEND_INITIAL_METADATA;
191 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700192 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800193 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800194 op++;
Craig Tiller4541f332015-05-19 16:26:33 -0700195 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800196 op->data.recv_message.recv_message = &request_payload_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700197 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800198 op->reserved = nullptr;
Craig Tiller4541f332015-05-19 16:26:33 -0700199 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800200 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200201 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller4541f332015-05-19 16:26:33 -0700202
Mark D. Roth7187ab92016-08-24 13:49:22 -0700203 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
Craig Tiller595cc5b2015-05-24 16:46:13 -0700204 cq_verify(cqv);
Craig Tiller4541f332015-05-19 16:26:33 -0700205
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700206 memset(ops, 0, sizeof(ops));
Craig Tiller4541f332015-05-19 16:26:33 -0700207 op = ops;
208 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
209 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700210 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800211 op->reserved = nullptr;
Craig Tiller4541f332015-05-19 16:26:33 -0700212 op++;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800213 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800214 op->data.send_message.send_message = response_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700215 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800216 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800217 op++;
218 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
219 op->data.send_status_from_server.trailing_metadata_count = 0;
Craig Tillerd1abc812015-05-06 14:35:19 -0700220 op->data.send_status_from_server.status = GRPC_STATUS_OK;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800221 grpc_slice status_details = grpc_slice_from_static_string("xyz");
222 op->data.send_status_from_server.status_details = &status_details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700223 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800224 op->reserved = nullptr;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800225 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800226 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200227 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800228
Mark D. Roth7187ab92016-08-24 13:49:22 -0700229 CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
230 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700231 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800232
Craig Tillerd1abc812015-05-06 14:35:19 -0700233 GPR_ASSERT(status == GRPC_STATUS_OK);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800234 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
235 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700236 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
237 config);
Craig Tillere801eb32015-02-10 14:01:14 -0800238 GPR_ASSERT(was_cancelled == 0);
Robbie Shade2449e192016-07-15 15:16:25 -0400239 GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice));
240 GPR_ASSERT(
241 byte_buffer_eq_slice(response_payload_recv, response_payload_slice));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800243 grpc_slice_unref(details);
Craig Tiller4f972732015-02-05 12:40:20 -0800244 grpc_metadata_array_destroy(&initial_metadata_recv);
245 grpc_metadata_array_destroy(&trailing_metadata_recv);
246 grpc_metadata_array_destroy(&request_metadata_recv);
247 grpc_call_details_destroy(&call_details);
248
Craig Tillerdd36b152017-03-31 08:27:28 -0700249 grpc_call_unref(c);
250 grpc_call_unref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800251
Craig Tillerbc0ec332015-05-11 12:11:32 -0700252 cq_verifier_destroy(cqv);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800253
254 grpc_byte_buffer_destroy(request_payload);
255 grpc_byte_buffer_destroy(response_payload);
256 grpc_byte_buffer_destroy(request_payload_recv);
257 grpc_byte_buffer_destroy(response_payload_recv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258}
259
260/* Client sends a request with payload, server reads then returns a response
261 payload and status. */
262static void test_invoke_request_response_with_payload(
263 grpc_end2end_test_config config) {
Craig Tiller9a576332015-06-17 10:21:49 -0700264 grpc_end2end_test_fixture f = begin_test(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800265 config, "test_invoke_request_response_with_payload", nullptr, nullptr);
Muxi Yan56456c32016-10-30 23:11:11 -0700266 request_response_with_payload(config, f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800267 end_test(&f);
268 config.tear_down_data(&f);
269}
270
271static void test_invoke_10_request_response_with_payload(
272 grpc_end2end_test_config config) {
273 int i;
Craig Tiller9a576332015-06-17 10:21:49 -0700274 grpc_end2end_test_fixture f = begin_test(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800275 config, "test_invoke_10_request_response_with_payload", nullptr, nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276 for (i = 0; i < 10; i++) {
Muxi Yan56456c32016-10-30 23:11:11 -0700277 request_response_with_payload(config, f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278 }
279 end_test(&f);
280 config.tear_down_data(&f);
281}
282
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800283void payload(grpc_end2end_test_config config) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284 test_invoke_request_response_with_payload(config);
285 test_invoke_10_request_response_with_payload(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800286}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700287
288void payload_pre_init(void) {}