blob: e581f1fc20eafede6d23178c446b142679dae38d [file] [log] [blame]
Yang Gao26839c72015-05-04 13:49:38 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Yang Gao26839c72015-05-04 13:49:38 -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
Yang Gao26839c72015-05-04 13:49:38 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yang Gao26839c72015-05-04 13:49:38 -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.
Yang Gao26839c72015-05-04 13:49:38 -070016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
Yang Gao26839c72015-05-04 13:49:38 -070023
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>
Mark D. Rothe127a392016-10-27 08:27:15 -070029
30#include "src/core/lib/channel/channel_args.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080031#include "src/core/lib/slice/slice_internal.h"
Mark D. Rothe127a392016-10-27 08:27:15 -070032#include "src/core/lib/transport/metadata.h"
Mark D. Rothea846a02016-11-03 11:32:54 -070033#include "src/core/lib/transport/service_config.h"
Mark D. Rothe127a392016-10-27 08:27:15 -070034
Yang Gao26839c72015-05-04 13:49:38 -070035#include "test/core/end2end/cq_verifier.h"
36
Craig Tillerbaa14a92017-11-03 09:09:36 -070037static void* tag(intptr_t t) { return (void*)t; }
Yang Gao26839c72015-05-04 13:49:38 -070038
39static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070040 const char* test_name,
41 grpc_channel_args* client_args,
42 grpc_channel_args* server_args) {
Yang Gao26839c72015-05-04 13:49:38 -070043 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050044 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Mark D. Roth2df502e2016-05-04 11:22:24 -070045 // We intentionally do not pass the client and server args to
46 // create_fixture(), since we don't want the limit enforced on the
47 // proxy, only on the backend server.
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080048 f = config.create_fixture(nullptr, nullptr);
Yang Gao26839c72015-05-04 13:49:38 -070049 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070050 config.init_client(&f, client_args);
Yang Gao26839c72015-05-04 13:49:38 -070051 return f;
52}
53
Chris Evansed2a5472017-03-27 17:34:51 -050054static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050055 return grpc_timeout_seconds_to_deadline(n);
Yang Gao26839c72015-05-04 13:49:38 -070056}
57
Chris Evansed2a5472017-03-27 17:34:51 -050058static gpr_timespec five_seconds_from_now(void) {
59 return n_seconds_from_now(5);
60}
Yang Gao26839c72015-05-04 13:49:38 -070061
Craig Tillerbaa14a92017-11-03 09:09:36 -070062static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller667ca402015-05-05 08:47:12 -070063 grpc_event ev;
Yang Gao26839c72015-05-04 13:49:38 -070064 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080065 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller667ca402015-05-05 08:47:12 -070066 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Yang Gao26839c72015-05-04 13:49:38 -070067}
68
Craig Tillerbaa14a92017-11-03 09:09:36 -070069static void shutdown_server(grpc_end2end_test_fixture* f) {
Yang Gao26839c72015-05-04 13:49:38 -070070 if (!f->server) return;
Yuchen Zeng27688662017-06-19 16:53:43 -070071 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
72 grpc_event ev = grpc_completion_queue_next(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080073 f->cq, grpc_timeout_seconds_to_deadline(5), nullptr);
Yuchen Zeng27688662017-06-19 16:53:43 -070074 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
75 GPR_ASSERT(ev.tag == tag(1000));
Yang Gao26839c72015-05-04 13:49:38 -070076 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080077 f->server = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -070078}
79
Craig Tillerbaa14a92017-11-03 09:09:36 -070080static void shutdown_client(grpc_end2end_test_fixture* f) {
Yang Gao26839c72015-05-04 13:49:38 -070081 if (!f->client) return;
82 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080083 f->client = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -070084}
85
Craig Tillerbaa14a92017-11-03 09:09:36 -070086static void end_test(grpc_end2end_test_fixture* f) {
Yang Gao26839c72015-05-04 13:49:38 -070087 shutdown_server(f);
88 shutdown_client(f);
89
Craig Tillerbc0ec332015-05-11 12:11:32 -070090 grpc_completion_queue_shutdown(f->cq);
91 drain_cq(f->cq);
92 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080093 grpc_completion_queue_destroy(f->shutdown_cq);
Yang Gao26839c72015-05-04 13:49:38 -070094}
95
Mark D. Roth274c8ed2016-10-04 09:21:42 -070096// Test with request larger than the limit.
97// If send_limit is true, applies send limit on client; otherwise, applies
98// recv limit on server.
99static void test_max_message_length_on_request(grpc_end2end_test_config config,
Mark D. Rothda091f72016-10-04 10:39:19 -0700100 bool send_limit,
Mark D. Roth40b4c782017-01-26 14:36:43 -0800101 bool use_service_config,
102 bool use_string_json_value) {
103 gpr_log(GPR_INFO,
104 "testing request with send_limit=%d use_service_config=%d "
105 "use_string_json_value=%d",
106 send_limit, use_service_config, use_string_json_value);
Mark D. Rothaf00d8b2016-08-23 12:48:16 -0700107
Yang Gao26839c72015-05-04 13:49:38 -0700108 grpc_end2end_test_fixture f;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800109 grpc_call* c = nullptr;
110 grpc_call* s = nullptr;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700111 cq_verifier* cqv;
Yang Gao26839c72015-05-04 13:49:38 -0700112 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700113 grpc_op* op;
Craig Tiller28b72422016-10-26 21:15:29 -0700114 grpc_slice request_payload_slice =
115 grpc_slice_from_copied_string("hello world");
Craig Tillerbaa14a92017-11-03 09:09:36 -0700116 grpc_byte_buffer* request_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700117 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800118 grpc_byte_buffer* recv_payload = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -0700119 grpc_metadata_array initial_metadata_recv;
120 grpc_metadata_array trailing_metadata_recv;
121 grpc_metadata_array request_metadata_recv;
122 grpc_call_details call_details;
123 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200124 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800125 grpc_slice details;
Yang Gao26839c72015-05-04 13:49:38 -0700126 int was_cancelled = 2;
127
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800128 grpc_channel_args* client_args = nullptr;
129 grpc_channel_args* server_args = nullptr;
Mark D. Rothda091f72016-10-04 10:39:19 -0700130 if (use_service_config) {
131 // We don't currently support service configs on the server side.
132 GPR_ASSERT(send_limit);
Mark D. Roth9ec28af2016-11-03 12:32:39 -0700133 grpc_arg arg;
134 arg.type = GRPC_ARG_STRING;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800135 arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
Mark D. Roth9ec28af2016-11-03 12:32:39 -0700136 arg.value.string =
Mark D. Roth40b4c782017-01-26 14:36:43 -0800137 use_string_json_value
Yash Tibrewal40422d52017-11-06 14:39:17 -0800138 ? const_cast<char*>(
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700139 "{\n"
140 " \"methodConfig\": [ {\n"
141 " \"name\": [\n"
142 " { \"service\": \"service\", \"method\": \"method\" }\n"
143 " ],\n"
144 " \"maxRequestMessageBytes\": \"5\"\n"
145 " } ]\n"
146 "}")
Yash Tibrewal40422d52017-11-06 14:39:17 -0800147 : const_cast<char*>(
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700148 "{\n"
149 " \"methodConfig\": [ {\n"
150 " \"name\": [\n"
151 " { \"service\": \"service\", \"method\": \"method\" }\n"
152 " ],\n"
153 " \"maxRequestMessageBytes\": 5\n"
154 " } ]\n"
155 "}");
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800156 client_args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
Mark D. Rothda091f72016-10-04 10:39:19 -0700157 } else {
158 // Set limit via channel args.
Mark D. Rothe127a392016-10-27 08:27:15 -0700159 grpc_arg arg;
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700160 arg.key = send_limit
Yash Tibrewal40422d52017-11-06 14:39:17 -0800161 ? const_cast<char*>(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH)
162 : const_cast<char*>(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH);
Mark D. Rothe127a392016-10-27 08:27:15 -0700163 arg.type = GRPC_ARG_INTEGER;
164 arg.value.integer = 5;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800165 grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
Mark D. Rothda091f72016-10-04 10:39:19 -0700166 if (send_limit) {
Mark D. Rothe127a392016-10-27 08:27:15 -0700167 client_args = args;
Mark D. Rothda091f72016-10-04 10:39:19 -0700168 } else {
Mark D. Rothe127a392016-10-27 08:27:15 -0700169 server_args = args;
Mark D. Rothda091f72016-10-04 10:39:19 -0700170 }
171 }
Yang Gao26839c72015-05-04 13:49:38 -0700172
Mark D. Rothda091f72016-10-04 10:39:19 -0700173 f = begin_test(config, "test_max_request_message_length", client_args,
Mark D. Rothe127a392016-10-27 08:27:15 -0700174 server_args);
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800175 {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800176 grpc_core::ExecCtx exec_ctx;
177 if (client_args != nullptr) grpc_channel_args_destroy(client_args);
178 if (server_args != nullptr) grpc_channel_args_destroy(server_args);
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800179 }
Mark D. Rothe127a392016-10-27 08:27:15 -0700180
Craig Tillerbc0ec332015-05-11 12:11:32 -0700181 cqv = cq_verifier_create(f.cq);
Yang Gao26839c72015-05-04 13:49:38 -0700182
Muxi Yan5ebd3272016-10-31 07:27:07 -0700183 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800184 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800185 grpc_slice_from_static_string("/service/method"),
186 get_host_override_slice("foo.test.google.fr:1234", config),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800187 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
Yang Gao26839c72015-05-04 13:49:38 -0700188 GPR_ASSERT(c);
189
190 grpc_metadata_array_init(&initial_metadata_recv);
191 grpc_metadata_array_init(&trailing_metadata_recv);
192 grpc_metadata_array_init(&request_metadata_recv);
193 grpc_call_details_init(&call_details);
194
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700195 memset(ops, 0, sizeof(ops));
Yang Gao26839c72015-05-04 13:49:38 -0700196 op = ops;
197 op->op = GRPC_OP_SEND_INITIAL_METADATA;
198 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700199 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800200 op->reserved = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -0700201 op++;
202 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800203 op->data.send_message.send_message = request_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700204 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800205 op->reserved = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -0700206 op++;
207 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700208 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800209 op->reserved = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -0700210 op++;
211 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800212 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700213 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800214 op->reserved = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -0700215 op++;
216 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
217 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
218 op->data.recv_status_on_client.status = &status;
219 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700220 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800221 op->reserved = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -0700222 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800223 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200224 GPR_ASSERT(GRPC_CALL_OK == error);
Yang Gao26839c72015-05-04 13:49:38 -0700225
Mark D. Rothaf00d8b2016-08-23 12:48:16 -0700226 if (send_limit) {
Mark D. Roth9717e5f2016-09-07 08:45:26 -0700227 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Mark D. Rothaf00d8b2016-08-23 12:48:16 -0700228 cq_verify(cqv);
229 goto done;
230 }
231
Craig Tillerd6c98df2015-08-18 09:33:44 -0700232 error =
233 grpc_server_request_call(f.server, &s, &call_details,
234 &request_metadata_recv, f.cq, f.cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200235 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700236 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700237 cq_verify(cqv);
Yang Gao26839c72015-05-04 13:49:38 -0700238
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700239 memset(ops, 0, sizeof(ops));
Yang Gao26839c72015-05-04 13:49:38 -0700240 op = ops;
241 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
242 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700243 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800244 op->reserved = nullptr;
Yang Gao26839c72015-05-04 13:49:38 -0700245 op++;
Craig Tiller93b94472015-11-02 14:18:30 -0800246 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800247 op->data.recv_message.recv_message = &recv_payload;
Craig Tiller93b94472015-11-02 14:18:30 -0800248 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800249 op->reserved = nullptr;
Craig Tiller93b94472015-11-02 14:18:30 -0800250 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800251 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200252 GPR_ASSERT(GRPC_CALL_OK == error);
Yang Gao26839c72015-05-04 13:49:38 -0700253
Mark D. Roth7187ab92016-08-24 13:49:22 -0700254 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
255 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700256 cq_verify(cqv);
Yang Gao26839c72015-05-04 13:49:38 -0700257
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800258 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700259 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
260 config);
Yang Gao26839c72015-05-04 13:49:38 -0700261 GPR_ASSERT(was_cancelled == 1);
Mark D. Rothaf00d8b2016-08-23 12:48:16 -0700262
263done:
Mark D. Roth17a93b32017-04-13 07:14:47 -0700264 GPR_ASSERT(status == GRPC_STATUS_RESOURCE_EXHAUSTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800265 GPR_ASSERT(
266 grpc_slice_str_cmp(
267 details, send_limit
268 ? "Sent message larger than max (11 vs. 5)"
269 : "Received message larger than max (11 vs. 5)") == 0);
Yang Gao26839c72015-05-04 13:49:38 -0700270
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800271 grpc_slice_unref(details);
Yang Gao26839c72015-05-04 13:49:38 -0700272 grpc_metadata_array_destroy(&initial_metadata_recv);
273 grpc_metadata_array_destroy(&trailing_metadata_recv);
274 grpc_metadata_array_destroy(&request_metadata_recv);
275 grpc_call_details_destroy(&call_details);
Craig Tillera4cdf8a2015-05-20 12:33:01 -0700276 grpc_byte_buffer_destroy(request_payload);
Mark D. Rothaf00d8b2016-08-23 12:48:16 -0700277 grpc_byte_buffer_destroy(recv_payload);
Yang Gao26839c72015-05-04 13:49:38 -0700278
Craig Tillerdd36b152017-03-31 08:27:28 -0700279 grpc_call_unref(c);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800280 if (s != nullptr) grpc_call_unref(s);
Yang Gao26839c72015-05-04 13:49:38 -0700281
Craig Tillerbc0ec332015-05-11 12:11:32 -0700282 cq_verifier_destroy(cqv);
Yang Gao26839c72015-05-04 13:49:38 -0700283
284 end_test(&f);
285 config.tear_down_data(&f);
286}
287
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700288// Test with response larger than the limit.
289// If send_limit is true, applies send limit on server; otherwise, applies
290// recv limit on client.
291static void test_max_message_length_on_response(grpc_end2end_test_config config,
Mark D. Rothda091f72016-10-04 10:39:19 -0700292 bool send_limit,
Mark D. Roth40b4c782017-01-26 14:36:43 -0800293 bool use_service_config,
294 bool use_string_json_value) {
295 gpr_log(GPR_INFO,
296 "testing response with send_limit=%d use_service_config=%d "
297 "use_string_json_value=%d",
298 send_limit, use_service_config, use_string_json_value);
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700299
300 grpc_end2end_test_fixture f;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800301 grpc_call* c = nullptr;
302 grpc_call* s = nullptr;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700303 cq_verifier* cqv;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700304 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700305 grpc_op* op;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700306 grpc_slice response_payload_slice =
307 grpc_slice_from_copied_string("hello world");
Craig Tillerbaa14a92017-11-03 09:09:36 -0700308 grpc_byte_buffer* response_payload =
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700309 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800310 grpc_byte_buffer* recv_payload = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700311 grpc_metadata_array initial_metadata_recv;
312 grpc_metadata_array trailing_metadata_recv;
313 grpc_metadata_array request_metadata_recv;
314 grpc_call_details call_details;
315 grpc_status_code status;
316 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800317 grpc_slice details;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700318 int was_cancelled = 2;
319
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800320 grpc_channel_args* client_args = nullptr;
321 grpc_channel_args* server_args = nullptr;
Mark D. Rothda091f72016-10-04 10:39:19 -0700322 if (use_service_config) {
323 // We don't currently support service configs on the server side.
324 GPR_ASSERT(!send_limit);
Mark D. Roth9ec28af2016-11-03 12:32:39 -0700325 grpc_arg arg;
326 arg.type = GRPC_ARG_STRING;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800327 arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
328 arg.value.string = const_cast<char*>(
Mark D. Roth40b4c782017-01-26 14:36:43 -0800329 use_string_json_value
330 ? "{\n"
331 " \"methodConfig\": [ {\n"
332 " \"name\": [\n"
333 " { \"service\": \"service\", \"method\": \"method\" }\n"
334 " ],\n"
335 " \"maxResponseMessageBytes\": \"5\"\n"
336 " } ]\n"
337 "}"
338 : "{\n"
339 " \"methodConfig\": [ {\n"
340 " \"name\": [\n"
341 " { \"service\": \"service\", \"method\": \"method\" }\n"
342 " ],\n"
343 " \"maxResponseMessageBytes\": 5\n"
344 " } ]\n"
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700345 "}");
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800346 client_args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
Mark D. Rothda091f72016-10-04 10:39:19 -0700347 } else {
348 // Set limit via channel args.
Mark D. Rothe127a392016-10-27 08:27:15 -0700349 grpc_arg arg;
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700350 arg.key = send_limit
Yash Tibrewal40422d52017-11-06 14:39:17 -0800351 ? const_cast<char*>(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH)
352 : const_cast<char*>(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH);
Mark D. Rothe127a392016-10-27 08:27:15 -0700353 arg.type = GRPC_ARG_INTEGER;
354 arg.value.integer = 5;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800355 grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
Mark D. Rothda091f72016-10-04 10:39:19 -0700356 if (send_limit) {
Mark D. Rothe127a392016-10-27 08:27:15 -0700357 server_args = args;
Mark D. Rothda091f72016-10-04 10:39:19 -0700358 } else {
Mark D. Rothe127a392016-10-27 08:27:15 -0700359 client_args = args;
Mark D. Rothda091f72016-10-04 10:39:19 -0700360 }
361 }
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700362
Mark D. Rothda091f72016-10-04 10:39:19 -0700363 f = begin_test(config, "test_max_response_message_length", client_args,
Mark D. Rothe127a392016-10-27 08:27:15 -0700364 server_args);
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800365 {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800366 grpc_core::ExecCtx exec_ctx;
367 if (client_args != nullptr) grpc_channel_args_destroy(client_args);
368 if (server_args != nullptr) grpc_channel_args_destroy(server_args);
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800369 }
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700370 cqv = cq_verifier_create(f.cq);
371
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800372 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800373 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800374 grpc_slice_from_static_string("/service/method"),
375 get_host_override_slice("foo.test.google.fr:1234", config),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800376 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700377 GPR_ASSERT(c);
378
379 grpc_metadata_array_init(&initial_metadata_recv);
380 grpc_metadata_array_init(&trailing_metadata_recv);
381 grpc_metadata_array_init(&request_metadata_recv);
382 grpc_call_details_init(&call_details);
383
384 memset(ops, 0, sizeof(ops));
385 op = ops;
386 op->op = GRPC_OP_SEND_INITIAL_METADATA;
387 op->data.send_initial_metadata.count = 0;
388 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800389 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700390 op++;
391 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
392 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800393 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700394 op++;
395 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800396 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700397 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800398 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700399 op++;
400 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800401 op->data.recv_message.recv_message = &recv_payload;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700402 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800403 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700404 op++;
405 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
406 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
407 op->data.recv_status_on_client.status = &status;
408 op->data.recv_status_on_client.status_details = &details;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700409 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800410 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700411 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800412 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700413 GPR_ASSERT(GRPC_CALL_OK == error);
414
415 error =
416 grpc_server_request_call(f.server, &s, &call_details,
417 &request_metadata_recv, f.cq, f.cq, tag(101));
418 GPR_ASSERT(GRPC_CALL_OK == error);
419 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
420 cq_verify(cqv);
421
422 memset(ops, 0, sizeof(ops));
423 op = ops;
424 op->op = GRPC_OP_SEND_INITIAL_METADATA;
425 op->data.send_initial_metadata.count = 0;
426 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800427 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700428 op++;
429 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
430 op->data.recv_close_on_server.cancelled = &was_cancelled;
431 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800432 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700433 op++;
434 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800435 op->data.send_message.send_message = response_payload;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700436 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800437 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700438 op++;
439 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
440 op->data.send_status_from_server.trailing_metadata_count = 0;
441 op->data.send_status_from_server.status = GRPC_STATUS_OK;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800442 grpc_slice status_details = grpc_slice_from_static_string("xyz");
443 op->data.send_status_from_server.status_details = &status_details;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700444 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800445 op->reserved = nullptr;
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700446 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800447 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700448 GPR_ASSERT(GRPC_CALL_OK == error);
449
450 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
451 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
452 cq_verify(cqv);
453
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800454 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method"));
455 GPR_ASSERT(0 ==
456 grpc_slice_str_cmp(call_details.host, "foo.test.google.fr:1234"));
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700457
Mark D. Roth17a93b32017-04-13 07:14:47 -0700458 GPR_ASSERT(status == GRPC_STATUS_RESOURCE_EXHAUSTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800459 GPR_ASSERT(
460 grpc_slice_str_cmp(
461 details, send_limit
462 ? "Sent message larger than max (11 vs. 5)"
463 : "Received message larger than max (11 vs. 5)") == 0);
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700464
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800465 grpc_slice_unref(details);
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700466 grpc_metadata_array_destroy(&initial_metadata_recv);
467 grpc_metadata_array_destroy(&trailing_metadata_recv);
468 grpc_metadata_array_destroy(&request_metadata_recv);
469 grpc_call_details_destroy(&call_details);
470 grpc_byte_buffer_destroy(response_payload);
471 grpc_byte_buffer_destroy(recv_payload);
472
Craig Tillerdd36b152017-03-31 08:27:28 -0700473 grpc_call_unref(c);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800474 if (s != nullptr) grpc_call_unref(s);
Mark D. Roth274c8ed2016-10-04 09:21:42 -0700475
476 cq_verifier_destroy(cqv);
477
478 end_test(&f);
479 config.tear_down_data(&f);
480}
481
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800482void max_message_length(grpc_end2end_test_config config) {
Mark D. Rothda091f72016-10-04 10:39:19 -0700483 test_max_message_length_on_request(config, false /* send_limit */,
Mark D. Roth40b4c782017-01-26 14:36:43 -0800484 false /* use_service_config */,
485 false /* use_string_json_value */);
Mark D. Rothda091f72016-10-04 10:39:19 -0700486 test_max_message_length_on_request(config, true /* send_limit */,
Mark D. Roth40b4c782017-01-26 14:36:43 -0800487 false /* use_service_config */,
488 false /* use_string_json_value */);
Mark D. Rothda091f72016-10-04 10:39:19 -0700489 test_max_message_length_on_response(config, false /* send_limit */,
Mark D. Roth40b4c782017-01-26 14:36:43 -0800490 false /* use_service_config */,
491 false /* use_string_json_value */);
Mark D. Rothda091f72016-10-04 10:39:19 -0700492 test_max_message_length_on_response(config, true /* send_limit */,
Mark D. Roth40b4c782017-01-26 14:36:43 -0800493 false /* use_service_config */,
494 false /* use_string_json_value */);
Mark D. Rothe127a392016-10-27 08:27:15 -0700495 test_max_message_length_on_request(config, true /* send_limit */,
Mark D. Roth40b4c782017-01-26 14:36:43 -0800496 true /* use_service_config */,
497 false /* use_string_json_value */);
498 test_max_message_length_on_request(config, true /* send_limit */,
499 true /* use_service_config */,
500 true /* use_string_json_value */);
Mark D. Rothe127a392016-10-27 08:27:15 -0700501 test_max_message_length_on_response(config, false /* send_limit */,
Mark D. Roth40b4c782017-01-26 14:36:43 -0800502 true /* use_service_config */,
503 false /* use_string_json_value */);
504 test_max_message_length_on_response(config, false /* send_limit */,
505 true /* use_service_config */,
506 true /* use_string_json_value */);
Yang Gao26839c72015-05-04 13:49:38 -0700507}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700508
509void max_message_length_pre_init(void) {}