Debian Codegraph @

test/core/end2end/tests/hpack_size.cc

raw
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 <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <string>
24
25 #include "absl/strings/str_format.h"
26
27 #include <grpc/grpc.h>
28 #include <grpc/impl/codegen/propagation_bits.h>
29 #include <grpc/slice.h>
30 #include <grpc/status.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/time.h>
33
34 #include "src/core/lib/gpr/useful.h"
35 #include "src/core/lib/gprpp/time.h"
36 #include "test/core/end2end/cq_verifier.h"
37 #include "test/core/end2end/end2end_tests.h"
38 #include "test/core/util/test_config.h"
39
40 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
41
42 const char* hobbits[][2] = {
43 {"Adaldrida", "Brandybuck"}, {"Adamanta", "Took"},
44 {"Adalgrim", "Took"}, {"Adelard", "Took"},
45 {"Amaranth", "Brandybuck"}, {"Andwise", "Roper"},
46 {"Angelica", "Baggins"}, {"Asphodel", "Burrows"},
47 {"Balbo", "Baggins"}, {"Bandobras", "Took"},
48 {"Belba", "Bolger"}, {"Bell", "Gamgee"},
49 {"Belladonna", "Baggins"}, {"Berylla", "Baggins"},
50 {"Bilbo", "Baggins"}, {"Bilbo", "Gardner"},
51 {"Bill", "Butcher"}, {"Bingo", "Baggins"},
52 {"Bodo", "Proudfoot"}, {"Bowman", "Cotton"},
53 {"Bungo", "Baggins"}, {"Camellia", "Sackville"},
54 {"Carl", "Cotton"}, {"Celandine", "Brandybuck"},
55 {"Chica", "Baggins"}, {"Daddy", "Twofoot"},
56 {"Daisy", "Boffin"}, {"Diamond", "Took"},
57 {"Dinodas", "Brandybuck"}, {"Doderic", "Brandybuck"},
58 {"Dodinas", "Brandybuck"}, {"Donnamira", "Boffin"},
59 {"Dora", "Baggins"}, {"Drogo", "Baggins"},
60 {"Dudo", "Baggins"}, {"Eglantine", "Took"},
61 {"Elanor", "Fairbairn"}, {"Elfstan", "Fairbairn"},
62 {"Esmeralda", "Brandybuck"}, {"Estella", "Brandybuck"},
63 {"Everard", "Took"}, {"Falco", "Chubb-Baggins"},
64 {"Faramir", "Took"}, {"Farmer", "Maggot"},
65 {"Fastolph", "Bolger"}, {"Ferdibrand", "Took"},
66 {"Ferdinand", "Took"}, {"Ferumbras", "Took"},
67 {"Ferumbras", "Took"}, {"Filibert", "Bolger"},
68 {"Firiel", "Fairbairn"}, {"Flambard", "Took"},
69 {"Folco", "Boffin"}, {"Fortinbras", "Took"},
70 {"Fortinbras", "Took"}, {"Fosco", "Baggins"},
71 {"Fredegar", "Bolger"}, {"Frodo", "Baggins"},
72 {"Frodo", "Gardner"}, {"Gerontius", "Took"},
73 {"Gilly", "Baggins"}, {"Goldilocks", "Took"},
74 {"Gorbadoc", "Brandybuck"}, {"Gorbulas", "Brandybuck"},
75 {"Gorhendad", "Brandybuck"}, {"Gormadoc", "Brandybuck"},
76 {"Griffo", "Boffin"}, {"Halfast", "Gamgee"},
77 {"Halfred", "Gamgee"}, {"Halfred", "Greenhand"},
78 {"Hanna", "Brandybuck"}, {"Hamfast", "Gamgee"},
79 {"Hamfast", "Gardner"}, {"Hamson", "Gamgee"},
80 {"Harding", "Gardner"}, {"Hilda", "Brandybuck"},
81 {"Hildibrand", "Took"}, {"Hildifons", "Took"},
82 {"Hildigard", "Took"}, {"Hildigrim", "Took"},
83 {"Hob", "Gammidge"}, {"Hob", "Hayward"},
84 {"Hobson", "Gamgee"}, {"Holfast", "Gardner"},
85 {"Holman", "Cotton"}, {"Holman", "Greenhand"},
86 {"Hugo", "Boffin"}, {"Hugo", "Bracegirdle"},
87 {"Ilberic", "Brandybuck"}, {"Isembard", "Took"},
88 {"Isembold", "Took"}, {"Isengar", "Took"},
89 {"Isengrim", "Took"}, {"Isengrim", "Took"},
90 {"Isumbras", "Took"}, {"Isumbras", "Took"},
91 {"Jolly", "Cotton"},
92 /*
93 {"Lalia", "Took"},
94 {"Largo", "Baggins"},
95 {"Laura", "Baggins"},
96 {"Lily", "Goodbody"},
97 {"Lily", "Cotton"},
98 {"Linda", "Proudfoot"},
99 {"Lobelia", "Sackville-Baggins"},
100 {"Longo", "Baggins"},
101 {"Lotho", "Sackville-Baggins"},
102 {"Madoc", "Brandybuck"},
103 {"Malva", "Brandybuck"},
104 {"Marigold", "Cotton"},
105 {"Marmadas", "Brandybuck"},
106 {"Marmadoc", "Brandybuck"},
107 {"Marroc", "Brandybuck"},
108 {"May", "Gamgee"},
109 {"Melilot", "Brandybuck"},
110 {"Menegilda", "Brandybuck"},
111 {"Mentha", "Brandybuck"},
112 {"Meriadoc", "Brandybuck"},
113 {"Merimac", "Brandybuck"},
114 {"Merimas", "Brandybuck"},
115 {"Merry", "Gardner"},
116 {"Milo", "Burrows"},
117 {"Mimosa", "Baggins"},
118 {"Minto", "Burrows"},
119 {"Mirabella", "Brandybuck"},
120 {"Moro", "Burrows"},
121 {"Mosco", "Burrows"},
122 {"Mungo", "Baggins"},
123 {"Myrtle", "Burrows"},
124 {"Odo", "Proudfoot"},
125 {"Odovacar", "Bolger"},
126 {"Olo", "Proudfoot"},
127 {"Orgulas", "Brandybuck"},
128 {"Otho", "Sackville-Baggins"},
129 {"Paladin", "Took"},
130 {"Pansy", "Bolger"},
131 {"Pearl", "Took"},
132 {"Peony", "Burrows"},
133 {"Peregrin", "Took"},
134 {"Pervinca", "Took"},
135 {"Pimpernel", "Took"},
136 {"Pippin", "Gardner"},
137 {"Polo", "Baggins"},
138 {"Ponto", "Baggins"},
139 {"Porto", "Baggins"},
140 {"Posco", "Baggins"},
141 {"Poppy", "Bolger"},
142 {"Primrose", "Gardner"},
143 {"Primula", "Baggins"},
144 {"Prisca", "Bolger"},
145 {"Reginard", "Took"},
146 {"Robin", "Smallburrow"},
147 {"Robin", "Gardner"},
148 {"Rorimac", "Brandybuck"},
149 {"Rosa", "Took"},
150 {"Rosamunda", "Bolger"},
151 {"Rose", "Gardner"},
152 {"Ruby", "Baggins"},
153 {"Ruby", "Gardner"},
154 {"Rudigar", "Bolger"},
155 {"Rufus", "Burrows"},
156 {"Sadoc", "Brandybuck"},
157 {"Salvia", "Bolger"},
158 {"Samwise", "Gamgee"},
159 {"Sancho", "Proudfoot"},
160 {"Saradas", "Brandybuck"},
161 {"Saradoc", "Brandybuck"},
162 {"Seredic", "Brandybuck"},
163 {"Sigismond", "Took"},
164 {"Smeagol", "Gollum"},
165 {"Tanta", "Baggins"},
166 {"Ted", "Sandyman"},
167 {"Tobold", "Hornblower"},
168 {"Togo", "Goodbody"},
169 {"Tolman", "Cotton"},
170 {"Tolman", "Gardner"},
171 {"Widow", "Rumble"},
172 {"Wilcome", "Cotton"},
173 {"Wilcome", "Cotton"},
174 {"Wilibald", "Bolger"},
175 {"Will", "Whitfoot"},
176 {"Wiseman", "Gamwich"}*/
177 };
178
179 const char* dragons[] = {"Ancalagon", "Glaurung", "Scatha",
180 "Smaug the Magnificent"};
181
182 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
183 const char* test_name,
184 grpc_channel_args* client_args,
185 grpc_channel_args* server_args) {
186 grpc_end2end_test_fixture f;
187 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
188 f = config.create_fixture(client_args, server_args);
189 config.init_server(&f, server_args);
190 config.init_client(&f, client_args);
191 return f;
192 }
193
194 static gpr_timespec n_seconds_from_now(int n) {
195 return grpc_timeout_seconds_to_deadline(n);
196 }
197
198 static gpr_timespec five_seconds_from_now(void) {
199 return n_seconds_from_now(5);
200 }
201
202 static void drain_cq(grpc_completion_queue* cq) {
203 grpc_event ev;
204 do {
205 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
206 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
207 }
208
209 static void shutdown_server(grpc_end2end_test_fixture* f) {
210 if (!f->server) return;
211 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
212 grpc_event ev;
213 do {
214 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
215 nullptr);
216 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
217 grpc_server_destroy(f->server);
218 f->server = nullptr;
219 }
220
221 static void shutdown_client(grpc_end2end_test_fixture* f) {
222 if (!f->client) return;
223 grpc_channel_destroy(f->client);
224 f->client = nullptr;
225 }
226
227 static void end_test(grpc_end2end_test_fixture* f) {
228 shutdown_server(f);
229 shutdown_client(f);
230
231 grpc_completion_queue_shutdown(f->cq);
232 drain_cq(f->cq);
233 grpc_completion_queue_destroy(f->cq);
234 }
235
236 static void simple_request_body(grpc_end2end_test_config /*config*/,
237 grpc_end2end_test_fixture f, size_t index) {
238 grpc_call* c;
239 grpc_call* s;
240 grpc_core::CqVerifier cqv(f.cq);
241 grpc_op ops[6];
242 grpc_op* op;
243 grpc_metadata_array initial_metadata_recv;
244 grpc_metadata_array trailing_metadata_recv;
245 grpc_metadata_array request_metadata_recv;
246 grpc_call_details call_details;
247 grpc_status_code status;
248 grpc_call_error error;
249 grpc_metadata extra_metadata[3];
250 grpc_slice details;
251 int was_cancelled = 2;
252
253 memset(extra_metadata, 0, sizeof(extra_metadata));
254 extra_metadata[0].key = grpc_slice_from_static_string("hobbit-first-name");
255 extra_metadata[0].value = grpc_slice_from_static_string(
256 hobbits[index % GPR_ARRAY_SIZE(hobbits)][0]);
257 extra_metadata[1].key = grpc_slice_from_static_string("hobbit-second-name");
258 extra_metadata[1].value = grpc_slice_from_static_string(
259 hobbits[index % GPR_ARRAY_SIZE(hobbits)][1]);
260 extra_metadata[2].key = grpc_slice_from_static_string("dragon");
261 extra_metadata[2].value =
262 grpc_slice_from_static_string(dragons[index % GPR_ARRAY_SIZE(dragons)]);
263
264 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
265 grpc_slice_from_static_string("/foo"), nullptr,
266 gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
267 GPR_ASSERT(c);
268
269 grpc_metadata_array_init(&initial_metadata_recv);
270 grpc_metadata_array_init(&trailing_metadata_recv);
271 grpc_metadata_array_init(&request_metadata_recv);
272 grpc_call_details_init(&call_details);
273
274 memset(ops, 0, sizeof(ops));
275 op = ops;
276 op->op = GRPC_OP_SEND_INITIAL_METADATA;
277 op->data.send_initial_metadata.count = GPR_ARRAY_SIZE(extra_metadata);
278 op->data.send_initial_metadata.metadata = extra_metadata;
279 op->flags = 0;
280 op->reserved = nullptr;
281 op++;
282 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
283 op->flags = 0;
284 op->reserved = nullptr;
285 op++;
286 op->op = GRPC_OP_RECV_INITIAL_METADATA;
287 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
288 op->flags = 0;
289 op->reserved = nullptr;
290 op++;
291 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
292 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
293 op->data.recv_status_on_client.status = &status;
294 op->data.recv_status_on_client.status_details = &details;
295 op->flags = 0;
296 op->reserved = nullptr;
297 op++;
298 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
299 nullptr);
300 GPR_ASSERT(GRPC_CALL_OK == error);
301
302 error =
303 grpc_server_request_call(f.server, &s, &call_details,
304 &request_metadata_recv, f.cq, f.cq, tag(101));
305 GPR_ASSERT(GRPC_CALL_OK == error);
306 cqv.Expect(tag(101), true);
307 cqv.Verify(grpc_core::Duration::Seconds(120));
308
309 memset(ops, 0, sizeof(ops));
310 op = ops;
311 op->op = GRPC_OP_SEND_INITIAL_METADATA;
312 op->data.send_initial_metadata.count = 0;
313 op->flags = 0;
314 op->reserved = nullptr;
315 op++;
316 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
317 op->data.send_status_from_server.trailing_metadata_count = 0;
318 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
319 grpc_slice status_details = grpc_slice_from_static_string("xyz");
320 op->data.send_status_from_server.status_details = &status_details;
321 op->flags = 0;
322 op->reserved = nullptr;
323 op++;
324 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
325 op->data.recv_close_on_server.cancelled = &was_cancelled;
326 op->flags = 0;
327 op->reserved = nullptr;
328 op++;
329 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
330 nullptr);
331 GPR_ASSERT(GRPC_CALL_OK == error);
332
333 cqv.Expect(tag(102), true);
334 cqv.Expect(tag(1), true);
335 cqv.Verify(grpc_core::Duration::Seconds(120));
336
337 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
338 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
339 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
340 GPR_ASSERT(was_cancelled == 0);
341
342 grpc_slice_unref(details);
343 grpc_metadata_array_destroy(&initial_metadata_recv);
344 grpc_metadata_array_destroy(&trailing_metadata_recv);
345 grpc_metadata_array_destroy(&request_metadata_recv);
346 grpc_call_details_destroy(&call_details);
347
348 grpc_call_unref(c);
349 grpc_call_unref(s);
350 }
351
352 static void test_size(grpc_end2end_test_config config, int encode_size,
353 int decode_size) {
354 size_t i;
355 grpc_end2end_test_fixture f;
356 grpc_arg server_arg;
357 grpc_channel_args server_args;
358 grpc_arg client_arg;
359 grpc_channel_args client_args;
360
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;
366
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;
372
373 std::string name =
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);
380 }
381 end_test(&f);
382 config.tear_down_data(&f);
383 }
384
385 void hpack_size(grpc_end2end_test_config config) {
386 static const int interesting_sizes[] = {4096, 0, 100,
387 1000, 32768, 4 * 1024 * 1024};
388 size_t i, j;
389
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]);
393 }
394 }
395 }
396
397 void hpack_size_pre_init(void) {}