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.
19 #include "test/core/end2end/end2end_tests.h"
26 #include "absl/strings/str_format.h"
28 #include <grpc/byte_buffer.h>
29 #include <grpc/grpc.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/time.h>
34 #include "src/core/lib/gpr/string.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "test/core/end2end/cq_verifier.h"
38 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
40 const char* hobbits[][2] = {
41 {"Adaldrida", "Brandybuck"}, {"Adamanta", "Took"},
42 {"Adalgrim", "Took"}, {"Adelard", "Took"},
43 {"Amaranth", "Brandybuck"}, {"Andwise", "Roper"},
44 {"Angelica", "Baggins"}, {"Asphodel", "Burrows"},
45 {"Balbo", "Baggins"}, {"Bandobras", "Took"},
46 {"Belba", "Bolger"}, {"Bell", "Gamgee"},
47 {"Belladonna", "Baggins"}, {"Berylla", "Baggins"},
48 {"Bilbo", "Baggins"}, {"Bilbo", "Gardner"},
49 {"Bill", "Butcher"}, {"Bingo", "Baggins"},
50 {"Bodo", "Proudfoot"}, {"Bowman", "Cotton"},
51 {"Bungo", "Baggins"}, {"Camellia", "Sackville"},
52 {"Carl", "Cotton"}, {"Celandine", "Brandybuck"},
53 {"Chica", "Baggins"}, {"Daddy", "Twofoot"},
54 {"Daisy", "Boffin"}, {"Diamond", "Took"},
55 {"Dinodas", "Brandybuck"}, {"Doderic", "Brandybuck"},
56 {"Dodinas", "Brandybuck"}, {"Donnamira", "Boffin"},
57 {"Dora", "Baggins"}, {"Drogo", "Baggins"},
58 {"Dudo", "Baggins"}, {"Eglantine", "Took"},
59 {"Elanor", "Fairbairn"}, {"Elfstan", "Fairbairn"},
60 {"Esmeralda", "Brandybuck"}, {"Estella", "Brandybuck"},
61 {"Everard", "Took"}, {"Falco", "Chubb-Baggins"},
62 {"Faramir", "Took"}, {"Farmer", "Maggot"},
63 {"Fastolph", "Bolger"}, {"Ferdibrand", "Took"},
64 {"Ferdinand", "Took"}, {"Ferumbras", "Took"},
65 {"Ferumbras", "Took"}, {"Filibert", "Bolger"},
66 {"Firiel", "Fairbairn"}, {"Flambard", "Took"},
67 {"Folco", "Boffin"}, {"Fortinbras", "Took"},
68 {"Fortinbras", "Took"}, {"Fosco", "Baggins"},
69 {"Fredegar", "Bolger"}, {"Frodo", "Baggins"},
70 {"Frodo", "Gardner"}, {"Gerontius", "Took"},
71 {"Gilly", "Baggins"}, {"Goldilocks", "Took"},
72 {"Gorbadoc", "Brandybuck"}, {"Gorbulas", "Brandybuck"},
73 {"Gorhendad", "Brandybuck"}, {"Gormadoc", "Brandybuck"},
74 {"Griffo", "Boffin"}, {"Halfast", "Gamgee"},
75 {"Halfred", "Gamgee"}, {"Halfred", "Greenhand"},
76 {"Hanna", "Brandybuck"}, {"Hamfast", "Gamgee"},
77 {"Hamfast", "Gardner"}, {"Hamson", "Gamgee"},
78 {"Harding", "Gardner"}, {"Hilda", "Brandybuck"},
79 {"Hildibrand", "Took"}, {"Hildifons", "Took"},
80 {"Hildigard", "Took"}, {"Hildigrim", "Took"},
81 {"Hob", "Gammidge"}, {"Hob", "Hayward"},
82 {"Hobson", "Gamgee"}, {"Holfast", "Gardner"},
83 {"Holman", "Cotton"}, {"Holman", "Greenhand"},
84 {"Hugo", "Boffin"}, {"Hugo", "Bracegirdle"},
85 {"Ilberic", "Brandybuck"}, {"Isembard", "Took"},
86 {"Isembold", "Took"}, {"Isengar", "Took"},
87 {"Isengrim", "Took"}, {"Isengrim", "Took"},
88 {"Isumbras", "Took"}, {"Isumbras", "Took"},
96 {"Linda", "Proudfoot"},
97 {"Lobelia", "Sackville-Baggins"},
99 {"Lotho", "Sackville-Baggins"},
100 {"Madoc", "Brandybuck"},
101 {"Malva", "Brandybuck"},
102 {"Marigold", "Cotton"},
103 {"Marmadas", "Brandybuck"},
104 {"Marmadoc", "Brandybuck"},
105 {"Marroc", "Brandybuck"},
107 {"Melilot", "Brandybuck"},
108 {"Menegilda", "Brandybuck"},
109 {"Mentha", "Brandybuck"},
110 {"Meriadoc", "Brandybuck"},
111 {"Merimac", "Brandybuck"},
112 {"Merimas", "Brandybuck"},
113 {"Merry", "Gardner"},
115 {"Mimosa", "Baggins"},
116 {"Minto", "Burrows"},
117 {"Mirabella", "Brandybuck"},
119 {"Mosco", "Burrows"},
120 {"Mungo", "Baggins"},
121 {"Myrtle", "Burrows"},
122 {"Odo", "Proudfoot"},
123 {"Odovacar", "Bolger"},
124 {"Olo", "Proudfoot"},
125 {"Orgulas", "Brandybuck"},
126 {"Otho", "Sackville-Baggins"},
130 {"Peony", "Burrows"},
131 {"Peregrin", "Took"},
132 {"Pervinca", "Took"},
133 {"Pimpernel", "Took"},
134 {"Pippin", "Gardner"},
136 {"Ponto", "Baggins"},
137 {"Porto", "Baggins"},
138 {"Posco", "Baggins"},
140 {"Primrose", "Gardner"},
141 {"Primula", "Baggins"},
142 {"Prisca", "Bolger"},
143 {"Reginard", "Took"},
144 {"Robin", "Smallburrow"},
145 {"Robin", "Gardner"},
146 {"Rorimac", "Brandybuck"},
148 {"Rosamunda", "Bolger"},
152 {"Rudigar", "Bolger"},
153 {"Rufus", "Burrows"},
154 {"Sadoc", "Brandybuck"},
155 {"Salvia", "Bolger"},
156 {"Samwise", "Gamgee"},
157 {"Sancho", "Proudfoot"},
158 {"Saradas", "Brandybuck"},
159 {"Saradoc", "Brandybuck"},
160 {"Seredic", "Brandybuck"},
161 {"Sigismond", "Took"},
162 {"Smeagol", "Gollum"},
163 {"Tanta", "Baggins"},
165 {"Tobold", "Hornblower"},
166 {"Togo", "Goodbody"},
167 {"Tolman", "Cotton"},
168 {"Tolman", "Gardner"},
170 {"Wilcome", "Cotton"},
171 {"Wilcome", "Cotton"},
172 {"Wilibald", "Bolger"},
173 {"Will", "Whitfoot"},
174 {"Wiseman", "Gamwich"}*/
177 const char* dragons[] = {"Ancalagon", "Glaurung", "Scatha",
178 "Smaug the Magnificent"};
180 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
181 const char* test_name,
182 grpc_channel_args* client_args,
183 grpc_channel_args* server_args) {
184 grpc_end2end_test_fixture f;
185 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
186 f = config.create_fixture(client_args, server_args);
187 config.init_server(&f, server_args);
188 config.init_client(&f, client_args);
192 static gpr_timespec n_seconds_from_now(int n) {
193 return grpc_timeout_seconds_to_deadline(n);
196 static gpr_timespec five_seconds_from_now(void) {
197 return n_seconds_from_now(5);
200 static void drain_cq(grpc_completion_queue* cq) {
203 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
204 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
207 static void shutdown_server(grpc_end2end_test_fixture* f) {
208 if (!f->server) return;
209 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
210 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
211 grpc_timeout_seconds_to_deadline(5),
213 .type == GRPC_OP_COMPLETE);
214 grpc_server_destroy(f->server);
218 static void shutdown_client(grpc_end2end_test_fixture* f) {
219 if (!f->client) return;
220 grpc_channel_destroy(f->client);
224 static void end_test(grpc_end2end_test_fixture* f) {
228 grpc_completion_queue_shutdown(f->cq);
230 grpc_completion_queue_destroy(f->cq);
231 grpc_completion_queue_destroy(f->shutdown_cq);
234 static void simple_request_body(grpc_end2end_test_config /*config*/,
235 grpc_end2end_test_fixture f, size_t index) {
238 cq_verifier* cqv = cq_verifier_create(f.cq);
241 grpc_metadata_array initial_metadata_recv;
242 grpc_metadata_array trailing_metadata_recv;
243 grpc_metadata_array request_metadata_recv;
244 grpc_call_details call_details;
245 grpc_status_code status;
246 grpc_call_error error;
247 grpc_metadata extra_metadata[3];
249 int was_cancelled = 2;
251 memset(extra_metadata, 0, sizeof(extra_metadata));
252 extra_metadata[0].key = grpc_slice_from_static_string("hobbit-first-name");
253 extra_metadata[0].value = grpc_slice_from_static_string(
254 hobbits[index % GPR_ARRAY_SIZE(hobbits)][0]);
255 extra_metadata[1].key = grpc_slice_from_static_string("hobbit-second-name");
256 extra_metadata[1].value = grpc_slice_from_static_string(
257 hobbits[index % GPR_ARRAY_SIZE(hobbits)][1]);
258 extra_metadata[2].key = grpc_slice_from_static_string("dragon");
259 extra_metadata[2].value =
260 grpc_slice_from_static_string(dragons[index % GPR_ARRAY_SIZE(dragons)]);
262 gpr_timespec deadline = five_seconds_from_now();
263 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
264 grpc_slice_from_static_string("/foo"), nullptr,
268 grpc_metadata_array_init(&initial_metadata_recv);
269 grpc_metadata_array_init(&trailing_metadata_recv);
270 grpc_metadata_array_init(&request_metadata_recv);
271 grpc_call_details_init(&call_details);
273 memset(ops, 0, sizeof(ops));
275 op->op = GRPC_OP_SEND_INITIAL_METADATA;
276 op->data.send_initial_metadata.count = GPR_ARRAY_SIZE(extra_metadata);
277 op->data.send_initial_metadata.metadata = extra_metadata;
279 op->reserved = nullptr;
281 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
283 op->reserved = nullptr;
285 op->op = GRPC_OP_RECV_INITIAL_METADATA;
286 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
288 op->reserved = nullptr;
290 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
291 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
292 op->data.recv_status_on_client.status = &status;
293 op->data.recv_status_on_client.status_details = &details;
295 op->reserved = nullptr;
297 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
299 GPR_ASSERT(GRPC_CALL_OK == error);
302 grpc_server_request_call(f.server, &s, &call_details,
303 &request_metadata_recv, f.cq, f.cq, tag(101));
304 GPR_ASSERT(GRPC_CALL_OK == error);
305 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
308 memset(ops, 0, sizeof(ops));
310 op->op = GRPC_OP_SEND_INITIAL_METADATA;
311 op->data.send_initial_metadata.count = 0;
313 op->reserved = nullptr;
315 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
316 op->data.send_status_from_server.trailing_metadata_count = 0;
317 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
318 grpc_slice status_details = grpc_slice_from_static_string("xyz");
319 op->data.send_status_from_server.status_details = &status_details;
321 op->reserved = nullptr;
323 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
324 op->data.recv_close_on_server.cancelled = &was_cancelled;
326 op->reserved = nullptr;
328 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
330 GPR_ASSERT(GRPC_CALL_OK == error);
332 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
333 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
336 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
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);
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);
350 cq_verifier_destroy(cqv);
353 static void test_size(grpc_end2end_test_config config, int encode_size,
356 grpc_end2end_test_fixture f;
358 grpc_channel_args server_args;
360 grpc_channel_args client_args;
362 server_arg.type = GRPC_ARG_INTEGER;
363 server_arg.key = const_cast<char*>(GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER);
364 server_arg.value.integer = decode_size;
365 server_args.num_args = 1;
366 server_args.args = &server_arg;
368 client_arg.type = GRPC_ARG_INTEGER;
369 client_arg.key = const_cast<char*>(GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER);
370 client_arg.value.integer = encode_size;
371 client_args.num_args = 1;
372 client_args.args = &client_arg;
375 absl::StrFormat("test_size:e=%d:d=%d", encode_size, decode_size);
376 f = begin_test(config, name.c_str(),
377 encode_size != 4096 ? &client_args : nullptr,
378 decode_size != 4096 ? &server_args : nullptr);
379 for (i = 0; i < 4 * GPR_ARRAY_SIZE(hobbits); i++) {
380 simple_request_body(config, f, i);
383 config.tear_down_data(&f);
386 void hpack_size(grpc_end2end_test_config config) {
387 static const int interesting_sizes[] = {4096, 0, 100,
388 1000, 32768, 4 * 1024 * 1024};
391 for (i = 0; i < GPR_ARRAY_SIZE(interesting_sizes); i++) {
392 for (j = 0; j < GPR_ARRAY_SIZE(interesting_sizes); j++) {
393 test_size(config, interesting_sizes[i], interesting_sizes[j]);
398 void hpack_size_pre_init(void) {}