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