a9be0144fac8b2709f2b62b1adc4b679f0b43561
[platform/upstream/grpc.git] / test / core / end2end / tests / hpack_size.cc
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 <string>
25
26 #include "absl/strings/str_format.h"
27
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>
33
34 #include "src/core/lib/gpr/string.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "test/core/end2end/cq_verifier.h"
37
38 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
39
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"},
89     {"Jolly", "Cotton"},
90     /*
91     {"Lalia", "Took"},
92     {"Largo", "Baggins"},
93     {"Laura", "Baggins"},
94     {"Lily", "Goodbody"},
95     {"Lily", "Cotton"},
96     {"Linda", "Proudfoot"},
97     {"Lobelia", "Sackville-Baggins"},
98     {"Longo", "Baggins"},
99     {"Lotho", "Sackville-Baggins"},
100     {"Madoc", "Brandybuck"},
101     {"Malva", "Brandybuck"},
102     {"Marigold", "Cotton"},
103     {"Marmadas", "Brandybuck"},
104     {"Marmadoc", "Brandybuck"},
105     {"Marroc", "Brandybuck"},
106     {"May", "Gamgee"},
107     {"Melilot", "Brandybuck"},
108     {"Menegilda", "Brandybuck"},
109     {"Mentha", "Brandybuck"},
110     {"Meriadoc", "Brandybuck"},
111     {"Merimac", "Brandybuck"},
112     {"Merimas", "Brandybuck"},
113     {"Merry", "Gardner"},
114     {"Milo", "Burrows"},
115     {"Mimosa", "Baggins"},
116     {"Minto", "Burrows"},
117     {"Mirabella", "Brandybuck"},
118     {"Moro", "Burrows"},
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"},
127     {"Paladin", "Took"},
128     {"Pansy", "Bolger"},
129     {"Pearl", "Took"},
130     {"Peony", "Burrows"},
131     {"Peregrin", "Took"},
132     {"Pervinca", "Took"},
133     {"Pimpernel", "Took"},
134     {"Pippin", "Gardner"},
135     {"Polo", "Baggins"},
136     {"Ponto", "Baggins"},
137     {"Porto", "Baggins"},
138     {"Posco", "Baggins"},
139     {"Poppy", "Bolger"},
140     {"Primrose", "Gardner"},
141     {"Primula", "Baggins"},
142     {"Prisca", "Bolger"},
143     {"Reginard", "Took"},
144     {"Robin", "Smallburrow"},
145     {"Robin", "Gardner"},
146     {"Rorimac", "Brandybuck"},
147     {"Rosa", "Took"},
148     {"Rosamunda", "Bolger"},
149     {"Rose", "Gardner"},
150     {"Ruby", "Baggins"},
151     {"Ruby", "Gardner"},
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"},
164     {"Ted", "Sandyman"},
165     {"Tobold", "Hornblower"},
166     {"Togo", "Goodbody"},
167     {"Tolman", "Cotton"},
168     {"Tolman", "Gardner"},
169     {"Widow", "Rumble"},
170     {"Wilcome", "Cotton"},
171     {"Wilcome", "Cotton"},
172     {"Wilibald", "Bolger"},
173     {"Will", "Whitfoot"},
174     {"Wiseman", "Gamwich"}*/
175 };
176
177 const char* dragons[] = {"Ancalagon", "Glaurung", "Scatha",
178                          "Smaug the Magnificent"};
179
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);
189   return f;
190 }
191
192 static gpr_timespec n_seconds_from_now(int n) {
193   return grpc_timeout_seconds_to_deadline(n);
194 }
195
196 static gpr_timespec five_seconds_from_now(void) {
197   return n_seconds_from_now(5);
198 }
199
200 static void drain_cq(grpc_completion_queue* cq) {
201   grpc_event ev;
202   do {
203     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
204   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
205 }
206
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),
212                                          nullptr)
213                  .type == GRPC_OP_COMPLETE);
214   grpc_server_destroy(f->server);
215   f->server = nullptr;
216 }
217
218 static void shutdown_client(grpc_end2end_test_fixture* f) {
219   if (!f->client) return;
220   grpc_channel_destroy(f->client);
221   f->client = nullptr;
222 }
223
224 static void end_test(grpc_end2end_test_fixture* f) {
225   shutdown_server(f);
226   shutdown_client(f);
227
228   grpc_completion_queue_shutdown(f->cq);
229   drain_cq(f->cq);
230   grpc_completion_queue_destroy(f->cq);
231   grpc_completion_queue_destroy(f->shutdown_cq);
232 }
233
234 static void simple_request_body(grpc_end2end_test_config /*config*/,
235                                 grpc_end2end_test_fixture f, size_t index) {
236   grpc_call* c;
237   grpc_call* s;
238   cq_verifier* cqv = cq_verifier_create(f.cq);
239   grpc_op ops[6];
240   grpc_op* op;
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];
248   grpc_slice details;
249   int was_cancelled = 2;
250
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)]);
261
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,
265                                deadline, nullptr);
266   GPR_ASSERT(c);
267
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);
272
273   memset(ops, 0, sizeof(ops));
274   op = 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;
278   op->flags = 0;
279   op->reserved = nullptr;
280   op++;
281   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
282   op->flags = 0;
283   op->reserved = nullptr;
284   op++;
285   op->op = GRPC_OP_RECV_INITIAL_METADATA;
286   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
287   op->flags = 0;
288   op->reserved = nullptr;
289   op++;
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;
294   op->flags = 0;
295   op->reserved = nullptr;
296   op++;
297   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
298                                 nullptr);
299   GPR_ASSERT(GRPC_CALL_OK == error);
300
301   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);
306   cq_verify(cqv);
307
308   memset(ops, 0, sizeof(ops));
309   op = ops;
310   op->op = GRPC_OP_SEND_INITIAL_METADATA;
311   op->data.send_initial_metadata.count = 0;
312   op->flags = 0;
313   op->reserved = nullptr;
314   op++;
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;
320   op->flags = 0;
321   op->reserved = nullptr;
322   op++;
323   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
324   op->data.recv_close_on_server.cancelled = &was_cancelled;
325   op->flags = 0;
326   op->reserved = nullptr;
327   op++;
328   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
329                                 nullptr);
330   GPR_ASSERT(GRPC_CALL_OK == error);
331
332   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
333   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
334   cq_verify(cqv);
335
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);
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 static void test_size(grpc_end2end_test_config config, int encode_size,
354                       int decode_size) {
355   size_t i;
356   grpc_end2end_test_fixture f;
357   grpc_arg server_arg;
358   grpc_channel_args server_args;
359   grpc_arg client_arg;
360   grpc_channel_args client_args;
361
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;
367
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;
373
374   std::string name =
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);
381   }
382   end_test(&f);
383   config.tear_down_data(&f);
384 }
385
386 void hpack_size(grpc_end2end_test_config config) {
387   static const int interesting_sizes[] = {4096, 0,     100,
388                                           1000, 32768, 4 * 1024 * 1024};
389   size_t i, j;
390
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]);
394     }
395   }
396 }
397
398 void hpack_size_pre_init(void) {}