1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * 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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * 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.
16 *
17 */
18
19 #include "test/core/end2end/end2end_tests.h"
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <grpc/byte_buffer.h>
25 #include <grpc/byte_buffer_reader.h>
26 #include <grpc/compression.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/string_util.h>
30 #include <grpc/support/time.h>
31
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/surface/call.h"
34 #include "src/core/lib/surface/call_test_only.h"
35 #include "src/core/lib/transport/static_metadata.h"
36 #include "test/core/end2end/cq_verifier.h"
37
tag(intptr_t t)38 static void* tag(intptr_t t) { return (void*)t; }
39
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)40 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
41 const char* test_name,
42 grpc_channel_args* client_args,
43 grpc_channel_args* server_args) {
44 grpc_end2end_test_fixture f;
45 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
46 f = config.create_fixture(client_args, server_args);
47 config.init_server(&f, server_args);
48 config.init_client(&f, client_args);
49 return f;
50 }
51
n_seconds_from_now(int n)52 static gpr_timespec n_seconds_from_now(int n) {
53 return grpc_timeout_seconds_to_deadline(n);
54 }
55
five_seconds_from_now(void)56 static gpr_timespec five_seconds_from_now(void) {
57 return n_seconds_from_now(5);
58 }
59
drain_cq(grpc_completion_queue * cq)60 static void drain_cq(grpc_completion_queue* cq) {
61 grpc_event ev;
62 do {
63 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
64 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
65 }
66
shutdown_server(grpc_end2end_test_fixture * f)67 static void shutdown_server(grpc_end2end_test_fixture* f) {
68 if (!f->server) return;
69 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
70 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
71 grpc_timeout_seconds_to_deadline(5),
72 nullptr)
73 .type == GRPC_OP_COMPLETE);
74 grpc_server_destroy(f->server);
75 f->server = nullptr;
76 }
77
shutdown_client(grpc_end2end_test_fixture * f)78 static void shutdown_client(grpc_end2end_test_fixture* f) {
79 if (!f->client) return;
80 grpc_channel_destroy(f->client);
81 f->client = nullptr;
82 }
83
end_test(grpc_end2end_test_fixture * f)84 static void end_test(grpc_end2end_test_fixture* f) {
85 shutdown_server(f);
86 shutdown_client(f);
87
88 grpc_completion_queue_shutdown(f->cq);
89 drain_cq(f->cq);
90 grpc_completion_queue_destroy(f->cq);
91 grpc_completion_queue_destroy(f->shutdown_cq);
92 }
93
request_with_payload_template(grpc_end2end_test_config config,const char * test_name,uint32_t client_send_flags_bitmask,grpc_compression_algorithm default_client_channel_compression_algorithm,grpc_compression_algorithm default_server_channel_compression_algorithm,grpc_compression_algorithm expected_algorithm_from_client,grpc_compression_algorithm expected_algorithm_from_server,grpc_metadata * client_init_metadata,bool set_server_level,grpc_compression_level server_compression_level,char * user_agent_override)94 static void request_with_payload_template(
95 grpc_end2end_test_config config, const char* test_name,
96 uint32_t client_send_flags_bitmask,
97 grpc_compression_algorithm default_client_channel_compression_algorithm,
98 grpc_compression_algorithm default_server_channel_compression_algorithm,
99 grpc_compression_algorithm expected_algorithm_from_client,
100 grpc_compression_algorithm expected_algorithm_from_server,
101 grpc_metadata* client_init_metadata, bool set_server_level,
102 grpc_compression_level server_compression_level,
103 char* user_agent_override) {
104 grpc_call* c;
105 grpc_call* s;
106 grpc_slice request_payload_slice;
107 grpc_byte_buffer* request_payload;
108 grpc_channel_args* client_args;
109 grpc_channel_args* server_args;
110 grpc_end2end_test_fixture f;
111 grpc_op ops[6];
112 grpc_op* op;
113 grpc_metadata_array initial_metadata_recv;
114 grpc_metadata_array trailing_metadata_recv;
115 grpc_metadata_array request_metadata_recv;
116 grpc_byte_buffer* request_payload_recv = nullptr;
117 grpc_byte_buffer* response_payload;
118 grpc_byte_buffer* response_payload_recv;
119 grpc_call_details call_details;
120 grpc_status_code status;
121 grpc_call_error error;
122 grpc_slice details;
123 int was_cancelled = 2;
124 cq_verifier* cqv;
125 char request_str[1024];
126 char response_str[1024];
127
128 memset(request_str, 'x', 1023);
129 request_str[1023] = '\0';
130
131 memset(response_str, 'y', 1023);
132 response_str[1023] = '\0';
133
134 request_payload_slice = grpc_slice_from_copied_string(request_str);
135 grpc_slice response_payload_slice =
136 grpc_slice_from_copied_string(response_str);
137
138 client_args = grpc_channel_args_set_compression_algorithm(
139 nullptr, default_client_channel_compression_algorithm);
140 server_args = grpc_channel_args_set_compression_algorithm(
141 nullptr, default_server_channel_compression_algorithm);
142
143 if (user_agent_override) {
144 grpc_core::ExecCtx exec_ctx;
145 grpc_channel_args* client_args_old = client_args;
146 grpc_arg arg;
147 arg.key = const_cast<char*>(GRPC_ARG_PRIMARY_USER_AGENT_STRING);
148 arg.type = GRPC_ARG_STRING;
149 arg.value.string = user_agent_override;
150 client_args = grpc_channel_args_copy_and_add(client_args_old, &arg, 1);
151 grpc_channel_args_destroy(client_args_old);
152 }
153
154 f = begin_test(config, test_name, client_args, server_args);
155 cqv = cq_verifier_create(f.cq);
156
157 gpr_timespec deadline = five_seconds_from_now();
158 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
159 grpc_slice_from_static_string("/foo"), nullptr,
160 deadline, nullptr);
161 GPR_ASSERT(c);
162
163 grpc_metadata_array_init(&initial_metadata_recv);
164 grpc_metadata_array_init(&trailing_metadata_recv);
165 grpc_metadata_array_init(&request_metadata_recv);
166 grpc_call_details_init(&call_details);
167
168 memset(ops, 0, sizeof(ops));
169 op = ops;
170 op->op = GRPC_OP_SEND_INITIAL_METADATA;
171 if (client_init_metadata != nullptr) {
172 op->data.send_initial_metadata.count = 1;
173 op->data.send_initial_metadata.metadata = client_init_metadata;
174 } else {
175 op->data.send_initial_metadata.count = 0;
176 }
177 op->flags = 0;
178 op->reserved = nullptr;
179 op++;
180 op->op = GRPC_OP_RECV_INITIAL_METADATA;
181 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
182 op->flags = 0;
183 op->reserved = nullptr;
184 op++;
185 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
186 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
187 op->data.recv_status_on_client.status = &status;
188 op->data.recv_status_on_client.status_details = &details;
189 op->flags = 0;
190 op->reserved = nullptr;
191 op++;
192 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
193 nullptr);
194 GPR_ASSERT(GRPC_CALL_OK == error);
195
196 error =
197 grpc_server_request_call(f.server, &s, &call_details,
198 &request_metadata_recv, f.cq, f.cq, tag(100));
199 GPR_ASSERT(GRPC_CALL_OK == error);
200 CQ_EXPECT_COMPLETION(cqv, tag(100), true);
201 cq_verify(cqv);
202
203 GPR_ASSERT(GPR_BITCOUNT(grpc_call_test_only_get_encodings_accepted_by_peer(
204 s)) == GRPC_COMPRESS_ALGORITHMS_COUNT);
205 GPR_ASSERT(GPR_BITGET(grpc_call_test_only_get_encodings_accepted_by_peer(s),
206 GRPC_COMPRESS_NONE) != 0);
207 GPR_ASSERT(GPR_BITGET(grpc_call_test_only_get_encodings_accepted_by_peer(s),
208 GRPC_COMPRESS_DEFLATE) != 0);
209 GPR_ASSERT(GPR_BITGET(grpc_call_test_only_get_encodings_accepted_by_peer(s),
210 GRPC_COMPRESS_GZIP) != 0);
211
212 memset(ops, 0, sizeof(ops));
213 op = ops;
214 op->op = GRPC_OP_SEND_INITIAL_METADATA;
215 op->data.send_initial_metadata.count = 0;
216 if (set_server_level) {
217 op->data.send_initial_metadata.maybe_compression_level.is_set = true;
218 op->data.send_initial_metadata.maybe_compression_level.level =
219 server_compression_level;
220 }
221 op->flags = 0;
222 op->reserved = nullptr;
223 op++;
224 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
225 op->data.recv_close_on_server.cancelled = &was_cancelled;
226 op->flags = 0;
227 op->reserved = nullptr;
228 op++;
229 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(101),
230 nullptr);
231 GPR_ASSERT(GRPC_CALL_OK == error);
232
233 for (int i = 0; i < 2; i++) {
234 request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
235 response_payload = grpc_raw_byte_buffer_create(&response_payload_slice, 1);
236
237 memset(ops, 0, sizeof(ops));
238 op = ops;
239 op->op = GRPC_OP_SEND_MESSAGE;
240 op->data.send_message.send_message = request_payload;
241 op->flags = client_send_flags_bitmask;
242 op->reserved = nullptr;
243 op++;
244 op->op = GRPC_OP_RECV_MESSAGE;
245 op->data.recv_message.recv_message = &response_payload_recv;
246 op->flags = 0;
247 op->reserved = nullptr;
248 op++;
249 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
250 nullptr);
251 GPR_ASSERT(GRPC_CALL_OK == error);
252
253 memset(ops, 0, sizeof(ops));
254 op = ops;
255 op->op = GRPC_OP_RECV_MESSAGE;
256 op->data.recv_message.recv_message = &request_payload_recv;
257 op->flags = 0;
258 op->reserved = nullptr;
259 op++;
260 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
261 tag(102), nullptr);
262 GPR_ASSERT(GRPC_CALL_OK == error);
263 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
264 cq_verify(cqv);
265
266 GPR_ASSERT(request_payload_recv->type == GRPC_BB_RAW);
267 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, request_str));
268 GPR_ASSERT(request_payload_recv->data.raw.compression ==
269 expected_algorithm_from_client);
270
271 memset(ops, 0, sizeof(ops));
272 op = ops;
273 op->op = GRPC_OP_SEND_MESSAGE;
274 op->data.send_message.send_message = response_payload;
275 op->flags = 0;
276 op->reserved = nullptr;
277 op++;
278 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
279 tag(103), nullptr);
280 GPR_ASSERT(GRPC_CALL_OK == error);
281 CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
282 CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
283 cq_verify(cqv);
284
285 GPR_ASSERT(response_payload_recv->type == GRPC_BB_RAW);
286 GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, response_str));
287 if (server_compression_level > GRPC_COMPRESS_LEVEL_NONE) {
288 const grpc_compression_algorithm algo_for_server_level =
289 grpc_call_compression_for_level(s, server_compression_level);
290 GPR_ASSERT(response_payload_recv->data.raw.compression ==
291 algo_for_server_level);
292 } else {
293 GPR_ASSERT(response_payload_recv->data.raw.compression ==
294 expected_algorithm_from_server);
295 }
296
297 grpc_byte_buffer_destroy(request_payload);
298 grpc_byte_buffer_destroy(response_payload);
299 grpc_byte_buffer_destroy(request_payload_recv);
300 grpc_byte_buffer_destroy(response_payload_recv);
301 }
302
303 grpc_slice_unref(request_payload_slice);
304 grpc_slice_unref(response_payload_slice);
305
306 memset(ops, 0, sizeof(ops));
307 op = ops;
308 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
309 op->flags = 0;
310 op->reserved = nullptr;
311 op++;
312 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
313 nullptr);
314 GPR_ASSERT(GRPC_CALL_OK == error);
315
316 memset(ops, 0, sizeof(ops));
317 op = ops;
318 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
319 op->data.send_status_from_server.trailing_metadata_count = 0;
320 op->data.send_status_from_server.status = GRPC_STATUS_OK;
321 grpc_slice status_details = grpc_slice_from_static_string("xyz");
322 op->data.send_status_from_server.status_details = &status_details;
323 op->flags = 0;
324 op->reserved = nullptr;
325 op++;
326 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
327 nullptr);
328 GPR_ASSERT(GRPC_CALL_OK == error);
329
330 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
331 CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
332 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
333 CQ_EXPECT_COMPLETION(cqv, tag(104), 1);
334 cq_verify(cqv);
335
336 GPR_ASSERT(status == GRPC_STATUS_OK);
337 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
338 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
339 GPR_ASSERT(was_cancelled == 0);
340
341 grpc_slice_unref(details);
342 grpc_metadata_array_destroy(&initial_metadata_recv);
343 grpc_metadata_array_destroy(&trailing_metadata_recv);
344 grpc_metadata_array_destroy(&request_metadata_recv);
345 grpc_call_details_destroy(&call_details);
346
347 grpc_call_unref(c);
348 grpc_call_unref(s);
349
350 cq_verifier_destroy(cqv);
351
352 {
353 grpc_core::ExecCtx exec_ctx;
354 grpc_channel_args_destroy(client_args);
355 grpc_channel_args_destroy(server_args);
356 }
357
358 end_test(&f);
359 config.tear_down_data(&f);
360 }
361
362 typedef struct workaround_cronet_compression_config {
363 char* user_agent_override;
364 grpc_compression_algorithm expected_algorithm_from_server;
365 } workaround_cronet_compression_config;
366
367 static workaround_cronet_compression_config workaround_configs[] = {
368 {nullptr, GRPC_COMPRESS_GZIP},
369 {const_cast<char*>(
370 "grpc-objc/1.3.0-dev grpc-c/3.0.0-dev (ios; cronet_http; gentle)"),
371 GRPC_COMPRESS_NONE},
372 {const_cast<char*>(
373 "grpc-objc/1.3.0-dev grpc-c/3.0.0-dev (ios; chttp2; gentle)"),
374 GRPC_COMPRESS_GZIP},
375 {const_cast<char*>(
376 "grpc-objc/1.4.0 grpc-c/3.0.0-dev (ios; cronet_http; gentle)"),
377 GRPC_COMPRESS_GZIP}};
378 static const size_t workaround_configs_num =
379 sizeof(workaround_configs) / sizeof(*workaround_configs);
380
test_workaround_cronet_compression(grpc_end2end_test_config config)381 static void test_workaround_cronet_compression(
382 grpc_end2end_test_config config) {
383 for (uint32_t i = 0; i < workaround_configs_num; i++) {
384 request_with_payload_template(
385 config, "test_invoke_request_with_compressed_payload", 0,
386 GRPC_COMPRESS_GZIP, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_GZIP,
387 workaround_configs[i].expected_algorithm_from_server, nullptr, false,
388 /* ignored */ GRPC_COMPRESS_LEVEL_NONE,
389 workaround_configs[i].user_agent_override);
390 }
391 }
392
workaround_cronet_compression(grpc_end2end_test_config config)393 void workaround_cronet_compression(grpc_end2end_test_config config) {
394 if (config.feature_mask & FEATURE_MASK_SUPPORTS_WORKAROUNDS) {
395 test_workaround_cronet_compression(config);
396 }
397 }
398
workaround_cronet_compression_pre_init(void)399 void workaround_cronet_compression_pre_init(void) {}
400