Imported Upstream version 1.41.0
[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 <stdio.h>
20 #include <string.h>
21
22 #include <string>
23
24 #include "absl/strings/str_format.h"
25
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>
31
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"
36
37 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
38
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"},
88     {"Jolly", "Cotton"},
89     /*
90     {"Lalia", "Took"},
91     {"Largo", "Baggins"},
92     {"Laura", "Baggins"},
93     {"Lily", "Goodbody"},
94     {"Lily", "Cotton"},
95     {"Linda", "Proudfoot"},
96     {"Lobelia", "Sackville-Baggins"},
97     {"Longo", "Baggins"},
98     {"Lotho", "Sackville-Baggins"},
99     {"Madoc", "Brandybuck"},
100     {"Malva", "Brandybuck"},
101     {"Marigold", "Cotton"},
102     {"Marmadas", "Brandybuck"},
103     {"Marmadoc", "Brandybuck"},
104     {"Marroc", "Brandybuck"},
105     {"May", "Gamgee"},
106     {"Melilot", "Brandybuck"},
107     {"Menegilda", "Brandybuck"},
108     {"Mentha", "Brandybuck"},
109     {"Meriadoc", "Brandybuck"},
110     {"Merimac", "Brandybuck"},
111     {"Merimas", "Brandybuck"},
112     {"Merry", "Gardner"},
113     {"Milo", "Burrows"},
114     {"Mimosa", "Baggins"},
115     {"Minto", "Burrows"},
116     {"Mirabella", "Brandybuck"},
117     {"Moro", "Burrows"},
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"},
126     {"Paladin", "Took"},
127     {"Pansy", "Bolger"},
128     {"Pearl", "Took"},
129     {"Peony", "Burrows"},
130     {"Peregrin", "Took"},
131     {"Pervinca", "Took"},
132     {"Pimpernel", "Took"},
133     {"Pippin", "Gardner"},
134     {"Polo", "Baggins"},
135     {"Ponto", "Baggins"},
136     {"Porto", "Baggins"},
137     {"Posco", "Baggins"},
138     {"Poppy", "Bolger"},
139     {"Primrose", "Gardner"},
140     {"Primula", "Baggins"},
141     {"Prisca", "Bolger"},
142     {"Reginard", "Took"},
143     {"Robin", "Smallburrow"},
144     {"Robin", "Gardner"},
145     {"Rorimac", "Brandybuck"},
146     {"Rosa", "Took"},
147     {"Rosamunda", "Bolger"},
148     {"Rose", "Gardner"},
149     {"Ruby", "Baggins"},
150     {"Ruby", "Gardner"},
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"},
163     {"Ted", "Sandyman"},
164     {"Tobold", "Hornblower"},
165     {"Togo", "Goodbody"},
166     {"Tolman", "Cotton"},
167     {"Tolman", "Gardner"},
168     {"Widow", "Rumble"},
169     {"Wilcome", "Cotton"},
170     {"Wilcome", "Cotton"},
171     {"Wilibald", "Bolger"},
172     {"Will", "Whitfoot"},
173     {"Wiseman", "Gamwich"}*/
174 };
175
176 const char* dragons[] = {"Ancalagon", "Glaurung", "Scatha",
177                          "Smaug the Magnificent"};
178
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);
188   return f;
189 }
190
191 static gpr_timespec n_seconds_from_now(int n) {
192   return grpc_timeout_seconds_to_deadline(n);
193 }
194
195 static gpr_timespec five_seconds_from_now(void) {
196   return n_seconds_from_now(5);
197 }
198
199 static void drain_cq(grpc_completion_queue* cq) {
200   grpc_event ev;
201   do {
202     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
203   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
204 }
205
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),
211                                          nullptr)
212                  .type == GRPC_OP_COMPLETE);
213   grpc_server_destroy(f->server);
214   f->server = nullptr;
215 }
216
217 static void shutdown_client(grpc_end2end_test_fixture* f) {
218   if (!f->client) return;
219   grpc_channel_destroy(f->client);
220   f->client = nullptr;
221 }
222
223 static void end_test(grpc_end2end_test_fixture* f) {
224   shutdown_server(f);
225   shutdown_client(f);
226
227   grpc_completion_queue_shutdown(f->cq);
228   drain_cq(f->cq);
229   grpc_completion_queue_destroy(f->cq);
230   grpc_completion_queue_destroy(f->shutdown_cq);
231 }
232
233 static void simple_request_body(grpc_end2end_test_config /*config*/,
234                                 grpc_end2end_test_fixture f, size_t index) {
235   grpc_call* c;
236   grpc_call* s;
237   cq_verifier* cqv = cq_verifier_create(f.cq);
238   grpc_op ops[6];
239   grpc_op* op;
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];
247   grpc_slice details;
248   int was_cancelled = 2;
249
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)]);
260
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,
264                                deadline, nullptr);
265   GPR_ASSERT(c);
266
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);
271
272   memset(ops, 0, sizeof(ops));
273   op = 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;
277   op->flags = 0;
278   op->reserved = nullptr;
279   op++;
280   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
281   op->flags = 0;
282   op->reserved = nullptr;
283   op++;
284   op->op = GRPC_OP_RECV_INITIAL_METADATA;
285   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
286   op->flags = 0;
287   op->reserved = nullptr;
288   op++;
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;
293   op->flags = 0;
294   op->reserved = nullptr;
295   op++;
296   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
297                                 nullptr);
298   GPR_ASSERT(GRPC_CALL_OK == error);
299
300   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);
305   cq_verify(cqv);
306
307   memset(ops, 0, sizeof(ops));
308   op = ops;
309   op->op = GRPC_OP_SEND_INITIAL_METADATA;
310   op->data.send_initial_metadata.count = 0;
311   op->flags = 0;
312   op->reserved = nullptr;
313   op++;
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;
319   op->flags = 0;
320   op->reserved = nullptr;
321   op++;
322   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
323   op->data.recv_close_on_server.cancelled = &was_cancelled;
324   op->flags = 0;
325   op->reserved = nullptr;
326   op++;
327   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
328                                 nullptr);
329   GPR_ASSERT(GRPC_CALL_OK == error);
330
331   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
332   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
333   cq_verify(cqv);
334
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);
339
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);
345
346   grpc_call_unref(c);
347   grpc_call_unref(s);
348
349   cq_verifier_destroy(cqv);
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) {}