3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "absl/strings/str_format.h"
26 #include <grpc/byte_buffer.h>
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/gpr/useful.h"
34 #include "test/core/end2end/cq_verifier.h"
35 #include "test/core/end2end/end2end_tests.h"
37 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
39 const char* hobbits[][2] = {
40 {"Adaldrida", "Brandybuck"}, {"Adamanta", "Took"},
41 {"Adalgrim", "Took"}, {"Adelard", "Took"},
42 {"Amaranth", "Brandybuck"}, {"Andwise", "Roper"},
43 {"Angelica", "Baggins"}, {"Asphodel", "Burrows"},
44 {"Balbo", "Baggins"}, {"Bandobras", "Took"},
45 {"Belba", "Bolger"}, {"Bell", "Gamgee"},
46 {"Belladonna", "Baggins"}, {"Berylla", "Baggins"},
47 {"Bilbo", "Baggins"}, {"Bilbo", "Gardner"},
48 {"Bill", "Butcher"}, {"Bingo", "Baggins"},
49 {"Bodo", "Proudfoot"}, {"Bowman", "Cotton"},
50 {"Bungo", "Baggins"}, {"Camellia", "Sackville"},
51 {"Carl", "Cotton"}, {"Celandine", "Brandybuck"},
52 {"Chica", "Baggins"}, {"Daddy", "Twofoot"},
53 {"Daisy", "Boffin"}, {"Diamond", "Took"},
54 {"Dinodas", "Brandybuck"}, {"Doderic", "Brandybuck"},
55 {"Dodinas", "Brandybuck"}, {"Donnamira", "Boffin"},
56 {"Dora", "Baggins"}, {"Drogo", "Baggins"},
57 {"Dudo", "Baggins"}, {"Eglantine", "Took"},
58 {"Elanor", "Fairbairn"}, {"Elfstan", "Fairbairn"},
59 {"Esmeralda", "Brandybuck"}, {"Estella", "Brandybuck"},
60 {"Everard", "Took"}, {"Falco", "Chubb-Baggins"},
61 {"Faramir", "Took"}, {"Farmer", "Maggot"},
62 {"Fastolph", "Bolger"}, {"Ferdibrand", "Took"},
63 {"Ferdinand", "Took"}, {"Ferumbras", "Took"},
64 {"Ferumbras", "Took"}, {"Filibert", "Bolger"},
65 {"Firiel", "Fairbairn"}, {"Flambard", "Took"},
66 {"Folco", "Boffin"}, {"Fortinbras", "Took"},
67 {"Fortinbras", "Took"}, {"Fosco", "Baggins"},
68 {"Fredegar", "Bolger"}, {"Frodo", "Baggins"},
69 {"Frodo", "Gardner"}, {"Gerontius", "Took"},
70 {"Gilly", "Baggins"}, {"Goldilocks", "Took"},
71 {"Gorbadoc", "Brandybuck"}, {"Gorbulas", "Brandybuck"},
72 {"Gorhendad", "Brandybuck"}, {"Gormadoc", "Brandybuck"},
73 {"Griffo", "Boffin"}, {"Halfast", "Gamgee"},
74 {"Halfred", "Gamgee"}, {"Halfred", "Greenhand"},
75 {"Hanna", "Brandybuck"}, {"Hamfast", "Gamgee"},
76 {"Hamfast", "Gardner"}, {"Hamson", "Gamgee"},
77 {"Harding", "Gardner"}, {"Hilda", "Brandybuck"},
78 {"Hildibrand", "Took"}, {"Hildifons", "Took"},
79 {"Hildigard", "Took"}, {"Hildigrim", "Took"},
80 {"Hob", "Gammidge"}, {"Hob", "Hayward"},
81 {"Hobson", "Gamgee"}, {"Holfast", "Gardner"},
82 {"Holman", "Cotton"}, {"Holman", "Greenhand"},
83 {"Hugo", "Boffin"}, {"Hugo", "Bracegirdle"},
84 {"Ilberic", "Brandybuck"}, {"Isembard", "Took"},
85 {"Isembold", "Took"}, {"Isengar", "Took"},
86 {"Isengrim", "Took"}, {"Isengrim", "Took"},
87 {"Isumbras", "Took"}, {"Isumbras", "Took"},
95 {"Linda", "Proudfoot"},
96 {"Lobelia", "Sackville-Baggins"},
98 {"Lotho", "Sackville-Baggins"},
99 {"Madoc", "Brandybuck"},
100 {"Malva", "Brandybuck"},
101 {"Marigold", "Cotton"},
102 {"Marmadas", "Brandybuck"},
103 {"Marmadoc", "Brandybuck"},
104 {"Marroc", "Brandybuck"},
106 {"Melilot", "Brandybuck"},
107 {"Menegilda", "Brandybuck"},
108 {"Mentha", "Brandybuck"},
109 {"Meriadoc", "Brandybuck"},
110 {"Merimac", "Brandybuck"},
111 {"Merimas", "Brandybuck"},
112 {"Merry", "Gardner"},
114 {"Mimosa", "Baggins"},
115 {"Minto", "Burrows"},
116 {"Mirabella", "Brandybuck"},
118 {"Mosco", "Burrows"},
119 {"Mungo", "Baggins"},
120 {"Myrtle", "Burrows"},
121 {"Odo", "Proudfoot"},
122 {"Odovacar", "Bolger"},
123 {"Olo", "Proudfoot"},
124 {"Orgulas", "Brandybuck"},
125 {"Otho", "Sackville-Baggins"},
129 {"Peony", "Burrows"},
130 {"Peregrin", "Took"},
131 {"Pervinca", "Took"},
132 {"Pimpernel", "Took"},
133 {"Pippin", "Gardner"},
135 {"Ponto", "Baggins"},
136 {"Porto", "Baggins"},
137 {"Posco", "Baggins"},
139 {"Primrose", "Gardner"},
140 {"Primula", "Baggins"},
141 {"Prisca", "Bolger"},
142 {"Reginard", "Took"},
143 {"Robin", "Smallburrow"},
144 {"Robin", "Gardner"},
145 {"Rorimac", "Brandybuck"},
147 {"Rosamunda", "Bolger"},
151 {"Rudigar", "Bolger"},
152 {"Rufus", "Burrows"},
153 {"Sadoc", "Brandybuck"},
154 {"Salvia", "Bolger"},
155 {"Samwise", "Gamgee"},
156 {"Sancho", "Proudfoot"},
157 {"Saradas", "Brandybuck"},
158 {"Saradoc", "Brandybuck"},
159 {"Seredic", "Brandybuck"},
160 {"Sigismond", "Took"},
161 {"Smeagol", "Gollum"},
162 {"Tanta", "Baggins"},
164 {"Tobold", "Hornblower"},
165 {"Togo", "Goodbody"},
166 {"Tolman", "Cotton"},
167 {"Tolman", "Gardner"},
169 {"Wilcome", "Cotton"},
170 {"Wilcome", "Cotton"},
171 {"Wilibald", "Bolger"},
172 {"Will", "Whitfoot"},
173 {"Wiseman", "Gamwich"}*/
176 const char* dragons[] = {"Ancalagon", "Glaurung", "Scatha",
177 "Smaug the Magnificent"};
179 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
180 const char* test_name,
181 grpc_channel_args* client_args,
182 grpc_channel_args* server_args) {
183 grpc_end2end_test_fixture f;
184 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
185 f = config.create_fixture(client_args, server_args);
186 config.init_server(&f, server_args);
187 config.init_client(&f, client_args);
191 static gpr_timespec n_seconds_from_now(int n) {
192 return grpc_timeout_seconds_to_deadline(n);
195 static gpr_timespec five_seconds_from_now(void) {
196 return n_seconds_from_now(5);
199 static void drain_cq(grpc_completion_queue* cq) {
202 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
203 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
206 static void shutdown_server(grpc_end2end_test_fixture* f) {
207 if (!f->server) return;
208 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
209 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
210 grpc_timeout_seconds_to_deadline(5),
212 .type == GRPC_OP_COMPLETE);
213 grpc_server_destroy(f->server);
217 static void shutdown_client(grpc_end2end_test_fixture* f) {
218 if (!f->client) return;
219 grpc_channel_destroy(f->client);
223 static void end_test(grpc_end2end_test_fixture* f) {
227 grpc_completion_queue_shutdown(f->cq);
229 grpc_completion_queue_destroy(f->cq);
230 grpc_completion_queue_destroy(f->shutdown_cq);
233 static void simple_request_body(grpc_end2end_test_config /*config*/,
234 grpc_end2end_test_fixture f, size_t index) {
237 cq_verifier* cqv = cq_verifier_create(f.cq);
240 grpc_metadata_array initial_metadata_recv;
241 grpc_metadata_array trailing_metadata_recv;
242 grpc_metadata_array request_metadata_recv;
243 grpc_call_details call_details;
244 grpc_status_code status;
245 grpc_call_error error;
246 grpc_metadata extra_metadata[3];
248 int was_cancelled = 2;
250 memset(extra_metadata, 0, sizeof(extra_metadata));
251 extra_metadata[0].key = grpc_slice_from_static_string("hobbit-first-name");
252 extra_metadata[0].value = grpc_slice_from_static_string(
253 hobbits[index % GPR_ARRAY_SIZE(hobbits)][0]);
254 extra_metadata[1].key = grpc_slice_from_static_string("hobbit-second-name");
255 extra_metadata[1].value = grpc_slice_from_static_string(
256 hobbits[index % GPR_ARRAY_SIZE(hobbits)][1]);
257 extra_metadata[2].key = grpc_slice_from_static_string("dragon");
258 extra_metadata[2].value =
259 grpc_slice_from_static_string(dragons[index % GPR_ARRAY_SIZE(dragons)]);
261 gpr_timespec deadline = five_seconds_from_now();
262 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
263 grpc_slice_from_static_string("/foo"), nullptr,
267 grpc_metadata_array_init(&initial_metadata_recv);
268 grpc_metadata_array_init(&trailing_metadata_recv);
269 grpc_metadata_array_init(&request_metadata_recv);
270 grpc_call_details_init(&call_details);
272 memset(ops, 0, sizeof(ops));
274 op->op = GRPC_OP_SEND_INITIAL_METADATA;
275 op->data.send_initial_metadata.count = GPR_ARRAY_SIZE(extra_metadata);
276 op->data.send_initial_metadata.metadata = extra_metadata;
278 op->reserved = nullptr;
280 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
282 op->reserved = nullptr;
284 op->op = GRPC_OP_RECV_INITIAL_METADATA;
285 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
287 op->reserved = nullptr;
289 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
290 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
291 op->data.recv_status_on_client.status = &status;
292 op->data.recv_status_on_client.status_details = &details;
294 op->reserved = nullptr;
296 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
298 GPR_ASSERT(GRPC_CALL_OK == error);
301 grpc_server_request_call(f.server, &s, &call_details,
302 &request_metadata_recv, f.cq, f.cq, tag(101));
303 GPR_ASSERT(GRPC_CALL_OK == error);
304 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
307 memset(ops, 0, sizeof(ops));
309 op->op = GRPC_OP_SEND_INITIAL_METADATA;
310 op->data.send_initial_metadata.count = 0;
312 op->reserved = nullptr;
314 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
315 op->data.send_status_from_server.trailing_metadata_count = 0;
316 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
317 grpc_slice status_details = grpc_slice_from_static_string("xyz");
318 op->data.send_status_from_server.status_details = &status_details;
320 op->reserved = nullptr;
322 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
323 op->data.recv_close_on_server.cancelled = &was_cancelled;
325 op->reserved = nullptr;
327 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
329 GPR_ASSERT(GRPC_CALL_OK == error);
331 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
332 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
335 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
336 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
337 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
338 GPR_ASSERT(was_cancelled == 0);
340 grpc_slice_unref(details);
341 grpc_metadata_array_destroy(&initial_metadata_recv);
342 grpc_metadata_array_destroy(&trailing_metadata_recv);
343 grpc_metadata_array_destroy(&request_metadata_recv);
344 grpc_call_details_destroy(&call_details);
349 cq_verifier_destroy(cqv);
352 static void test_size(grpc_end2end_test_config config, int encode_size,
355 grpc_end2end_test_fixture f;
357 grpc_channel_args server_args;
359 grpc_channel_args client_args;
361 server_arg.type = GRPC_ARG_INTEGER;
362 server_arg.key = const_cast<char*>(GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER);
363 server_arg.value.integer = decode_size;
364 server_args.num_args = 1;
365 server_args.args = &server_arg;
367 client_arg.type = GRPC_ARG_INTEGER;
368 client_arg.key = const_cast<char*>(GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER);
369 client_arg.value.integer = encode_size;
370 client_args.num_args = 1;
371 client_args.args = &client_arg;
374 absl::StrFormat("test_size:e=%d:d=%d", encode_size, decode_size);
375 f = begin_test(config, name.c_str(),
376 encode_size != 4096 ? &client_args : nullptr,
377 decode_size != 4096 ? &server_args : nullptr);
378 for (i = 0; i < 4 * GPR_ARRAY_SIZE(hobbits); i++) {
379 simple_request_body(config, f, i);
382 config.tear_down_data(&f);
385 void hpack_size(grpc_end2end_test_config config) {
386 static const int interesting_sizes[] = {4096, 0, 100,
387 1000, 32768, 4 * 1024 * 1024};
390 for (i = 0; i < GPR_ARRAY_SIZE(interesting_sizes); i++) {
391 for (j = 0; j < GPR_ARRAY_SIZE(interesting_sizes); j++) {
392 test_size(config, interesting_sizes[i], interesting_sizes[j]);
397 void hpack_size_pre_init(void) {}