Debian Codegraph @

crt/aws-c-http/tests/test_hpack.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/testing/aws_test_harness.h>
6
7 #include <aws/http/private/hpack.h>
8
9 #include <aws/http/request_response.h>
10
11 /* #TODO test that buffer is resized if space is insufficient */
12
13 AWS_TEST_CASE(hpack_encode_integer, test_hpack_encode_integer)
14 static int test_hpack_encode_integer(struct aws_allocator *allocator, void *ctx) {
15 (void)allocator;
16 (void)ctx;
17 /* Test encoding integers
18 Test cases taken from https://httpwg.org/specs/rfc7541.html#integer.representation.examples */
19
20 uint8_t zeros[4];
21 AWS_ZERO_ARRAY(zeros);
22
23 struct aws_byte_buf output;
24 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 4));
25
26 /* Test 10 in 5 bits */
27 aws_byte_buf_secure_zero(&output);
28 ASSERT_SUCCESS(aws_hpack_encode_integer(10, 0, 5, &output));
29 /**
30 * Expected:
31 * 0 1 2 3 4 5 6 7
32 * +---+---+---+---+---+---+---+---+
33 * | X | X | X | 0 | 1 | 0 | 1 | 0 | 10
34 * +---+---+---+---+---+---+---+---+
35 */
36 ASSERT_UINT_EQUALS(1, output.len);
37 ASSERT_UINT_EQUALS(10, output.buffer[0]);
38 ASSERT_BIN_ARRAYS_EQUALS(zeros, 3, &output.buffer[1], 3);
39
40 /* Test full first byte (6 bits) */
41 aws_byte_buf_secure_zero(&output);
42 ASSERT_SUCCESS(aws_hpack_encode_integer(63, 0, 6, &output));
43 /**
44 * Expected:
45 * 0 1 2 3 4 5 6 7
46 * +---+---+---+---+---+---+---+---+
47 * | X | X | 1 | 1 | 1 | 1 | 1 | 1 | 63
48 * +---+---+---+---+---+---+---+---+
49 * | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
50 * +---+---+---+---+---+---+---+---+
51 */
52 ASSERT_UINT_EQUALS(2, output.len);
53 ASSERT_UINT_EQUALS(63, output.buffer[0]);
54 ASSERT_UINT_EQUALS(0, output.buffer[1]);
55 ASSERT_BIN_ARRAYS_EQUALS(zeros, 2, &output.buffer[2], 2);
56
57 /* Test 42 in 8 bits */
58 aws_byte_buf_secure_zero(&output);
59 ASSERT_SUCCESS(aws_hpack_encode_integer(42, 0, 8, &output));
60 /**
61 * Expected:
62 * 0 1 2 3 4 5 6 7
63 * +---+---+---+---+---+---+---+---+
64 * | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 42
65 * +---+---+---+---+---+---+---+---+
66 */
67 ASSERT_UINT_EQUALS(1, output.len);
68 ASSERT_UINT_EQUALS(42, output.buffer[0]);
69 ASSERT_BIN_ARRAYS_EQUALS(zeros, 3, &output.buffer[1], 3);
70
71 /* Test 1337 with 5bit prefix */
72 aws_byte_buf_secure_zero(&output);
73 ASSERT_SUCCESS(aws_hpack_encode_integer(1337, 0, 5, &output));
74 /**
75 * Expected:
76 * 0 1 2 3 4 5 6 7
77 * +---+---+---+---+---+---+---+---+
78 * | X | X | X | 1 | 1 | 1 | 1 | 1 | 31
79 * | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 154
80 * | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10
81 * +---+---+---+---+---+---+---+---+
82 */
83 ASSERT_UINT_EQUALS(3, output.len);
84 ASSERT_UINT_EQUALS(UINT8_MAX >> 3, output.buffer[0]);
85 ASSERT_UINT_EQUALS(154, output.buffer[1]);
86 ASSERT_UINT_EQUALS(10, output.buffer[2]);
87 ASSERT_UINT_EQUALS(0, output.buffer[3]);
88
89 aws_byte_buf_clean_up(&output);
90 return AWS_OP_SUCCESS;
91 }
92
93 struct decode_fixture {
94 struct aws_hpack_decoder hpack;
95 bool one_byte_at_a_time;
96 };
97
98 static int s_decode_fixture_setup(struct aws_allocator *allocator, void *ctx) {
99 struct decode_fixture *fixture = ctx;
100
101 aws_hpack_decoder_init(&fixture->hpack, allocator, NULL);
102
103 return AWS_OP_SUCCESS;
104 }
105
106 static int s_decode_fixture_teardown(struct aws_allocator *allocator, int setup_result, void *ctx) {
107 (void)allocator;
108 if (setup_result) {
109 return AWS_OP_ERR;
110 }
111
112 struct decode_fixture *fixture = ctx;
113 aws_hpack_decoder_clean_up(&fixture->hpack);
114 return AWS_OP_SUCCESS;
115 }
116
117 /* Call aws_hpack_decode_integer() either one-byte-at-a-time, or all at once */
118 static int s_decode_integer(
119 struct decode_fixture *fixture,
120 struct aws_byte_cursor *to_decode,
121 uint8_t prefix_size,
122 uint64_t *integer,
123 bool *complete) {
124
125 if (fixture->one_byte_at_a_time) {
126 do {
127 struct aws_byte_cursor one_byte = aws_byte_cursor_advance(to_decode, 1);
128 if (aws_hpack_decode_integer(&fixture->hpack, &one_byte, prefix_size, integer, complete)) {
129 return AWS_OP_ERR;
130 }
131 ASSERT_UINT_EQUALS(0, one_byte.len);
132 } while (!*complete && to_decode->len);
133
134 return AWS_OP_SUCCESS;
135
136 } else {
137 return aws_hpack_decode_integer(&fixture->hpack, to_decode, prefix_size, integer, complete);
138 }
139 }
140
141 /* Call aws_hpack_decode_string() either one-byte-at-a-time, or all at once */
142 static int s_decode_string(
143 struct decode_fixture *fixture,
144 struct aws_byte_cursor *to_decode,
145 struct aws_byte_buf *output,
146 bool *complete) {
147
148 if (fixture->one_byte_at_a_time) {
149 do {
150 struct aws_byte_cursor one_byte = aws_byte_cursor_advance(to_decode, 1);
151 if (aws_hpack_decode_string(&fixture->hpack, &one_byte, output, complete)) {
152 return AWS_OP_ERR;
153 }
154 ASSERT_UINT_EQUALS(0, one_byte.len);
155 } while (!*complete && to_decode->len);
156
157 return AWS_OP_SUCCESS;
158
159 } else {
160 return aws_hpack_decode_string(&fixture->hpack, to_decode, output, complete);
161 }
162 }
163
164 /* declare 2 tests, where the first decodes the input all at once,
165 * and the other decodes the input one byte at a time. */
166 #define TEST_DECODE_ONE_BYTE_AT_A_TIME(NAME) \
167 static struct decode_fixture s_##NAME##_fixture = {.one_byte_at_a_time = false}; \
168 static struct decode_fixture s_##NAME##_one_byte_at_a_time_fixture = {.one_byte_at_a_time = true}; \
169 AWS_TEST_CASE_FIXTURE(NAME, s_decode_fixture_setup, s_test_##NAME, s_decode_fixture_teardown, &s_##NAME##_fixture) \
170 AWS_TEST_CASE_FIXTURE( \
171 NAME##_one_byte_at_a_time, \
172 s_decode_fixture_setup, \
173 s_test_##NAME, \
174 s_decode_fixture_teardown, \
175 &s_##NAME##_one_byte_at_a_time_fixture) \
176 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
177
178 /* RFC-7541 - Integer Representation Examples - C.1.1. Encoding 10 Using a 5-Bit Prefix */
179 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_5bits) {
180 (void)allocator;
181 struct decode_fixture *fixture = ctx;
182
183 /* Layout:
184 * 0 1 2 3 4 5 6 7
185 * +---+---+---+---+---+---+---+---+
186 * | X | X | X | 0 | 1 | 0 | 1 | 0 | 10
187 * +---+---+---+---+---+---+---+---+
188 */
189 uint8_t test_0[] = {10};
190 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_0, AWS_ARRAY_SIZE(test_0));
191 uint64_t result;
192 bool complete;
193 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
194 ASSERT_TRUE(complete);
195 ASSERT_UINT_EQUALS(0, to_decode.len);
196 ASSERT_UINT_EQUALS(10, result);
197 return AWS_OP_SUCCESS;
198 }
199
200 /* Encoding 63 across a 6-bit prefix + one byte */
201 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_14bits) {
202 (void)allocator;
203 struct decode_fixture *fixture = ctx;
204
205 /* Layout:
206 * 0 1 2 3 4 5 6 7
207 * +---+---+---+---+---+---+---+---+
208 * | X | X | 1 | 1 | 1 | 1 | 1 | 1 | 63
209 * +---+---+---+---+---+---+---+---+
210 * | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
211 * +---+---+---+---+---+---+---+---+
212 */
213 uint8_t test_1[] = {63, 0};
214 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_1, AWS_ARRAY_SIZE(test_1));
215 uint64_t result;
216 bool complete;
217 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 6, &result, &complete));
218 ASSERT_TRUE(complete);
219 ASSERT_UINT_EQUALS(0, to_decode.len);
220 ASSERT_UINT_EQUALS(63, result);
221 return AWS_OP_SUCCESS;
222 }
223
224 /* RFC-7541 - Integer Representation Examples - C.1.3. Encoding 42 Starting at an Octet Boundary */
225 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_8bits) {
226 (void)allocator;
227 struct decode_fixture *fixture = ctx;
228
229 /* Layout:
230 * 0 1 2 3 4 5 6 7
231 * +---+---+---+---+---+---+---+---+
232 * | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 42
233 * +---+---+---+---+---+---+---+---+
234 */
235 uint8_t test_2[] = {42};
236 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_2, AWS_ARRAY_SIZE(test_2));
237 uint64_t result;
238 bool complete;
239 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 8, &result, &complete));
240 ASSERT_TRUE(complete);
241 ASSERT_UINT_EQUALS(0, to_decode.len);
242 ASSERT_UINT_EQUALS(42, result);
243 return AWS_OP_SUCCESS;
244 }
245
246 /* RFC-7541 - Integer Representation Examples - C.1.2. Encoding 1337 Using a 5-Bit Prefix */
247 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_21bits) {
248 (void)allocator;
249 struct decode_fixture *fixture = ctx;
250
251 /* Layout:
252 * 0 1 2 3 4 5 6 7
253 * +---+---+---+---+---+---+---+---+
254 * | X | X | X | 1 | 1 | 1 | 1 | 1 | 31
255 * | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 154
256 * | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10
257 * +---+---+---+---+---+---+---+---+
258 */
259 uint8_t test_3[] = {UINT8_MAX >> 3, 154, 10};
260 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_3, AWS_ARRAY_SIZE(test_3));
261 uint64_t result;
262 bool complete;
263 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
264 ASSERT_TRUE(complete);
265 ASSERT_UINT_EQUALS(0, to_decode.len);
266 ASSERT_UINT_EQUALS(1337, result);
267 return AWS_OP_SUCCESS;
268 }
269
270 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_ongoing) {
271 (void)allocator;
272 struct decode_fixture *fixture = ctx;
273
274 /* Test number ending with continue byte
275 * Layout:
276 * 0 1 2 3 4 5 6 7
277 * +---+---+---+---+---+---+---+---+
278 * | X | X | X | 1 | 1 | 1 | 1 | 1 | 31
279 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
280 * +---+---+---+---+---+---+---+---+
281 */
282 uint8_t test_4[] = {UINT8_MAX >> 3, UINT8_MAX};
283 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_4, AWS_ARRAY_SIZE(test_4));
284 uint64_t result;
285 bool complete;
286 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
287 ASSERT_FALSE(complete);
288 ASSERT_UINT_EQUALS(0, to_decode.len);
289 return AWS_OP_SUCCESS;
290 }
291
292 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_too_big) {
293 (void)allocator;
294 struct decode_fixture *fixture = ctx;
295
296 /* Test number too big
297 * Layout:
298 * 0 1 2 3 4 5 6 7
299 * +---+---+---+---+---+---+---+---+
300 * | X | X | X | 1 | 1 | 1 | 1 | 1 | 31
301 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
302 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
303 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
304 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
305 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
306 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
307 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
308 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
309 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
310 * | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
311 * +---+---+---+---+---+---+---+---+
312 */
313 uint8_t test_5[] = {
314 UINT8_MAX >> 3,
315 UINT8_MAX,
316 UINT8_MAX,
317 UINT8_MAX,
318 UINT8_MAX,
319 UINT8_MAX,
320 UINT8_MAX,
321 UINT8_MAX,
322 UINT8_MAX,
323 UINT8_MAX,
324 UINT8_MAX,
325 };
326 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_5, AWS_ARRAY_SIZE(test_5));
327 uint64_t result;
328 bool complete;
329 ASSERT_FAILS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
330 ASSERT_UINT_EQUALS(AWS_ERROR_OVERFLOW_DETECTED, aws_last_error());
331 return AWS_OP_SUCCESS;
332 }
333
334 /* Test that decoder properly resets itself between integers.
335 * Trying every type of transition:
336 * - from 1 byte to 1 byte
337 * - from 1 byte to multibyte
338 * - from multibyte to multibyte
339 * - from multibyte to 1 byte */
340 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_few_in_a_row) {
341 (void)allocator;
342 struct decode_fixture *fixture = ctx;
343
344 uint8_t input[] = {
345 /* 10 with 5-bit prefix
346 * +---+---+---+---+---+---+---+---+
347 * | X | X | X | 0 | 1 | 0 | 1 | 0 |
348 * +---+---+---+---+---+---+---+---+ */
349 10,
350 /* 42 with 8-bit prefix
351 * +---+---+---+---+---+---+---+---+
352 * | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
353 * +---+---+---+---+---+---+---+---+
354 */
355 42,
356 /* 63 with 6-bit prefix
357 * +---+---+---+---+---+---+---+---+
358 * | X | X | 1 | 1 | 1 | 1 | 1 | 1 |
359 * +---+---+---+---+---+---+---+---+
360 * | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
361 * +---+---+---+---+---+---+---+---+
362 */
363 63,
364 0,
365 /* 1337 with 5-bit prefix
366 * +---+---+---+---+---+---+---+---+
367 * | X | X | X | 1 | 1 | 1 | 1 | 1 |
368 * | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
369 * | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
370 * +---+---+---+---+---+---+---+---+
371 */
372 UINT8_MAX >> 3,
373 154,
374 10,
375 /* 10 with 5-bit prefix
376 * +---+---+---+---+---+---+---+---+
377 * | X | X | X | 0 | 1 | 0 | 1 | 0 |
378 * +---+---+---+---+---+---+---+---+
379 */
380 10,
381 };
382
383 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
384 uint64_t result;
385 bool complete;
386
387 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
388 ASSERT_TRUE(complete);
389 ASSERT_UINT_EQUALS(10, result);
390
391 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 8, &result, &complete));
392 ASSERT_TRUE(complete);
393 ASSERT_UINT_EQUALS(42, result);
394
395 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 6, &result, &complete));
396 ASSERT_TRUE(complete);
397 ASSERT_UINT_EQUALS(63, result);
398
399 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
400 ASSERT_TRUE(complete);
401 ASSERT_UINT_EQUALS(1337, result);
402
403 ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
404 ASSERT_TRUE(complete);
405 ASSERT_UINT_EQUALS(10, result);
406
407 ASSERT_UINT_EQUALS(0, to_decode.len);
408 return AWS_OP_SUCCESS;
409 }
410
411 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_blank) {
412 struct decode_fixture *fixture = ctx;
413
414 uint8_t input[] = {0};
415 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
416 struct aws_byte_buf output;
417 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 4));
418 bool complete;
419 ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
420
421 ASSERT_TRUE(complete);
422 ASSERT_UINT_EQUALS(0, to_decode.len);
423 ASSERT_BIN_ARRAYS_EQUALS("", 0, output.buffer, output.len);
424
425 aws_byte_buf_clean_up(&output);
426 return AWS_OP_SUCCESS;
427 }
428
429 /* Test a string that is NOT Huffman encoded */
430 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_uncompressed) {
431 struct decode_fixture *fixture = ctx;
432
433 uint8_t input[] = {5, 'h', 'e', 'l', 'l', 'o'};
434 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
435 struct aws_byte_buf output;
436 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 5));
437 bool complete;
438 ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
439
440 ASSERT_TRUE(complete);
441 ASSERT_UINT_EQUALS(0, to_decode.len);
442 ASSERT_BIN_ARRAYS_EQUALS("hello", 5, output.buffer, output.len);
443
444 aws_byte_buf_clean_up(&output);
445 return AWS_OP_SUCCESS;
446 }
447
448 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_huffman) {
449 struct decode_fixture *fixture = ctx;
450
451 /* This is Huffman-encoded "www.example.com", copied from:
452 * RFC-7541 - Request Examples with Huffman Coding - C.4.1. First Request */
453 uint8_t input[] = {0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff};
454 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
455
456 const char *expected = "www.example.com";
457
458 struct aws_byte_buf output;
459 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, strlen(expected)));
460 bool complete;
461 ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
462
463 ASSERT_TRUE(complete);
464 ASSERT_UINT_EQUALS(0, to_decode.len);
465 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), output.buffer, output.len);
466
467 aws_byte_buf_clean_up(&output);
468 return AWS_OP_SUCCESS;
469 }
470
471 /* Test that partial input doesn't register as "complete" */
472 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_ongoing) {
473 struct decode_fixture *fixture = ctx;
474
475 uint8_t input[] = {5, 'h', 'e', 'l'};
476 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
477 struct aws_byte_buf output;
478 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 5));
479 bool complete;
480 ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
481
482 ASSERT_FALSE(complete);
483 ASSERT_UINT_EQUALS(0, to_decode.len);
484
485 aws_byte_buf_clean_up(&output);
486 return AWS_OP_SUCCESS;
487 }
488
489 /* Test that output buffer is gets resized if it's too small */
490 TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_short_buffer) {
491 struct decode_fixture *fixture = ctx;
492
493 uint8_t input[] = {5, 'h', 'e', 'l', 'l', 'o'};
494 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
495
496 struct aws_byte_buf output;
497 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 1)); /* Note buffer is initially too small */
498 bool complete;
499 ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
500
501 ASSERT_TRUE(complete);
502 ASSERT_UINT_EQUALS(0, to_decode.len);
503 ASSERT_BIN_ARRAYS_EQUALS("hello", 5, output.buffer, output.len);
504
505 aws_byte_buf_clean_up(&output);
506 return AWS_OP_SUCCESS;
507 }
508
509 #define DEFINE_STATIC_HEADER(_name, _header, _value) \
510 static const struct aws_http_header _name = { \
511 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_header), \
512 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \
513 }
514
515 AWS_TEST_CASE(hpack_static_table_find, test_hpack_static_table_find)
516 static int test_hpack_static_table_find(struct aws_allocator *allocator, void *ctx) {
517 (void)ctx;
518
519 aws_http_library_init(allocator);
520 struct aws_hpack_context context;
521 aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
522 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, 0));
523
524 bool found_value = false;
525
526 DEFINE_STATIC_HEADER(s_authority, ":authority", "amazon.com");
527 DEFINE_STATIC_HEADER(s_get, ":method", "GET");
528 DEFINE_STATIC_HEADER(s_other_method, ":method", "TEAPOT");
529 DEFINE_STATIC_HEADER(s_garbage, "colden's favorite ice cream flavor", "cookie dough");
530
531 /* Test header without value */
532 ASSERT_UINT_EQUALS(1, aws_hpack_find_index(&context, &s_authority, false, &found_value));
533 ASSERT_FALSE(found_value);
534
535 /* Test header with value */
536 ASSERT_UINT_EQUALS(2, aws_hpack_find_index(&context, &s_get, true, &found_value));
537 ASSERT_TRUE(found_value);
538 ASSERT_UINT_EQUALS(2, aws_hpack_find_index(&context, &s_other_method, true, &found_value));
539 ASSERT_FALSE(found_value);
540
541 /* Check invalid header */
542 ASSERT_UINT_EQUALS(0, aws_hpack_find_index(&context, &s_garbage, true, &found_value));
543
544 aws_hpack_context_clean_up(&context);
545 aws_http_library_clean_up();
546 return AWS_OP_SUCCESS;
547 }
548
549 AWS_TEST_CASE(hpack_static_table_get, test_hpack_static_table_get)
550 static int test_hpack_static_table_get(struct aws_allocator *allocator, void *ctx) {
551 (void)ctx;
552
553 aws_http_library_init(allocator);
554 struct aws_hpack_context context;
555 aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
556 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, 0));
557
558 const struct aws_http_header *found = NULL;
559
560 DEFINE_STATIC_HEADER(s_get, ":path", "/index.html");
561 DEFINE_STATIC_HEADER(s_age, "age", "25");
562
563 found = aws_hpack_get_header(&context, 21);
564 ASSERT_NOT_NULL(found);
565 ASSERT_TRUE(aws_byte_cursor_eq(&s_age.name, &found->name));
566 ASSERT_NULL(found->value.ptr);
567 ASSERT_UINT_EQUALS(0, found->value.len);
568
569 found = aws_hpack_get_header(&context, 5);
570 ASSERT_NOT_NULL(found);
571 ASSERT_TRUE(aws_byte_cursor_eq(&s_get.name, &found->name));
572 ASSERT_TRUE(aws_byte_cursor_eq(&s_get.value, &found->value));
573
574 found = aws_hpack_get_header(&context, 69);
575 ASSERT_NULL(found);
576
577 aws_hpack_context_clean_up(&context);
578 aws_http_library_clean_up();
579 return AWS_OP_SUCCESS;
580 }
581
582 AWS_TEST_CASE(hpack_dynamic_table_find, test_hpack_dynamic_table_find)
583 static int test_hpack_dynamic_table_find(struct aws_allocator *allocator, void *ctx) {
584 (void)ctx;
585
586 aws_http_library_init(allocator);
587 struct aws_hpack_context context;
588 aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
589
590 bool found_value = false;
591
592 DEFINE_STATIC_HEADER(s_herp, "herp", "derp");
593 DEFINE_STATIC_HEADER(s_herp2, "herp", "something else");
594 DEFINE_STATIC_HEADER(s_fizz, "fizz", "buzz");
595
596 /* Test single header */
597 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_herp));
598 ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &s_herp, true, &found_value));
599 ASSERT_TRUE(found_value);
600 ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &s_herp2, true, &found_value));
601 ASSERT_FALSE(found_value);
602
603 /* Test 2 headers */
604 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_fizz));
605 ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &s_fizz, true, &found_value));
606 ASSERT_TRUE(found_value);
607 ASSERT_UINT_EQUALS(63, aws_hpack_find_index(&context, &s_herp, true, &found_value));
608 ASSERT_TRUE(found_value);
609 ASSERT_UINT_EQUALS(63, aws_hpack_find_index(&context, &s_herp2, true, &found_value));
610 ASSERT_FALSE(found_value);
611
612 /* Test resizing up doesn't break anything */
613 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, 8 * 1024 * 1024));
614
615 /* Check invalid header */
616 DEFINE_STATIC_HEADER(s_garbage, "colden's mother's maiden name", "nice try mr hacker");
617 ASSERT_UINT_EQUALS(0, aws_hpack_find_index(&context, &s_garbage, true, &found_value));
618
619 /* Test resizing so only the first element stays */
620 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, aws_hpack_get_header_size(&s_fizz)));
621
622 ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &s_fizz, true, &found_value));
623 ASSERT_TRUE(found_value);
624 ASSERT_UINT_EQUALS(0, aws_hpack_find_index(&context, &s_herp, true, &found_value));
625 ASSERT_FALSE(found_value);
626
627 aws_hpack_context_clean_up(&context);
628 aws_http_library_clean_up();
629 return AWS_OP_SUCCESS;
630 }
631
632 AWS_TEST_CASE(hpack_dynamic_table_get, test_hpack_dynamic_table_get)
633 static int test_hpack_dynamic_table_get(struct aws_allocator *allocator, void *ctx) {
634 (void)ctx;
635
636 aws_http_library_init(allocator);
637 struct aws_hpack_context context;
638 aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
639
640 const struct aws_http_header *found = NULL;
641
642 DEFINE_STATIC_HEADER(s_herp, "herp", "derp");
643 DEFINE_STATIC_HEADER(s_fizz, "fizz", "buzz");
644 DEFINE_STATIC_HEADER(s_status, ":status", "418");
645
646 /* Make the dynamic table only big enough for 2 headers */
647 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(
648 &context, aws_hpack_get_header_size(&s_fizz) + aws_hpack_get_header_size(&s_status)));
649
650 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_herp));
651 found = aws_hpack_get_header(&context, 62);
652 ASSERT_NOT_NULL(found);
653 ASSERT_TRUE(aws_byte_cursor_eq(&s_herp.name, &found->name));
654 ASSERT_TRUE(aws_byte_cursor_eq(&s_herp.value, &found->value));
655
656 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_fizz));
657 found = aws_hpack_get_header(&context, 62);
658 ASSERT_NOT_NULL(found);
659 ASSERT_TRUE(aws_byte_cursor_eq(&s_fizz.name, &found->name));
660 ASSERT_TRUE(aws_byte_cursor_eq(&s_fizz.value, &found->value));
661 found = aws_hpack_get_header(&context, 63);
662 ASSERT_NOT_NULL(found);
663 ASSERT_TRUE(aws_byte_cursor_eq(&s_herp.name, &found->name));
664 ASSERT_TRUE(aws_byte_cursor_eq(&s_herp.value, &found->value));
665
666 /* This one will result in the first header being evicted */
667 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_status));
668 found = aws_hpack_get_header(&context, 62);
669 ASSERT_NOT_NULL(found);
670 ASSERT_TRUE(aws_byte_cursor_eq(&s_status.name, &found->name));
671 ASSERT_TRUE(aws_byte_cursor_eq(&s_status.value, &found->value));
672 found = aws_hpack_get_header(&context, 63);
673 ASSERT_NOT_NULL(found);
674 ASSERT_TRUE(aws_byte_cursor_eq(&s_fizz.name, &found->name));
675 ASSERT_TRUE(aws_byte_cursor_eq(&s_fizz.value, &found->value));
676 found = aws_hpack_get_header(&context, 64);
677 ASSERT_NULL(found);
678
679 /* Test resizing to evict entries */
680 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, aws_hpack_get_header_size(&s_status)));
681
682 found = aws_hpack_get_header(&context, 62);
683 ASSERT_NOT_NULL(found);
684 ASSERT_TRUE(aws_byte_cursor_eq(&s_status.name, &found->name));
685 ASSERT_TRUE(aws_byte_cursor_eq(&s_status.value, &found->value));
686 found = aws_hpack_get_header(&context, 63);
687 ASSERT_NULL(found);
688
689 aws_hpack_context_clean_up(&context);
690 aws_http_library_clean_up();
691 return AWS_OP_SUCCESS;
692 }
693
694 static int s_check_header(
695 const struct aws_http_header *header_field,
696 const char *name,
697 const char *value,
698 enum aws_http_header_compression compression) {
699
700 ASSERT_BIN_ARRAYS_EQUALS(name, strlen(name), header_field->name.ptr, header_field->name.len);
701 ASSERT_BIN_ARRAYS_EQUALS(value, strlen(value), header_field->value.ptr, header_field->value.len);
702 ASSERT_INT_EQUALS(compression, header_field->compression);
703 return AWS_OP_SUCCESS;
704 }
705
706 AWS_TEST_CASE(hpack_decode_indexed_from_dynamic_table, test_hpack_decode_indexed_from_dynamic_table)
707 static int test_hpack_decode_indexed_from_dynamic_table(struct aws_allocator *allocator, void *ctx) {
708 (void)ctx;
709
710 aws_http_library_init(allocator);
711 struct aws_hpack_decoder decoder;
712 aws_hpack_decoder_init(&decoder, allocator, NULL);
713
714 /* clang-format off */
715 uint8_t input[] = {
716 0x48, 0x03, '3', '0', '2', /* ":status: 302" - stored to dynamic table */
717 0x40, 0x01, 'a', 0x01, 'b', /* "a: b" - stored to dynamic table */
718 /* So at this point dynamic table should look like:
719 * INDEX NAME VALUE
720 * 62 a b
721 * 63 :status 302
722 */
723 0xbf, /* ":status: 302" - indexed from dynamic table */
724 };
725 /* clang-format on */
726 struct aws_hpack_decode_result result;
727 struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input));
728 /* Three entries in total, decode them all, and check the result */
729 /* First entry */
730 ASSERT_SUCCESS(aws_hpack_decode(&decoder, &input_cursor, &result));
731 ASSERT_TRUE(result.type == AWS_HPACK_DECODE_T_HEADER_FIELD);
732 ASSERT_SUCCESS(s_check_header(&result.data.header_field, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
733 /* Second entry */
734 ASSERT_SUCCESS(aws_hpack_decode(&decoder, &input_cursor, &result));
735 ASSERT_TRUE(result.type == AWS_HPACK_DECODE_T_HEADER_FIELD);
736 ASSERT_SUCCESS(s_check_header(&result.data.header_field, "a", "b", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
737 /* Third entry */
738 ASSERT_SUCCESS(aws_hpack_decode(&decoder, &input_cursor, &result));
739 ASSERT_TRUE(result.type == AWS_HPACK_DECODE_T_HEADER_FIELD);
740 ASSERT_SUCCESS(s_check_header(&result.data.header_field, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
741
742 /* Check the input is fully consumed */
743 ASSERT_TRUE(input_cursor.len == 0);
744
745 /* Clean up */
746 aws_hpack_decoder_clean_up(&decoder);
747 aws_http_library_clean_up();
748 return AWS_OP_SUCCESS;
749 }
750
751 /* Test header with empty value */
752 AWS_TEST_CASE(hpack_dynamic_table_empty_value, test_hpack_dynamic_table_empty_value)
753 static int test_hpack_dynamic_table_empty_value(struct aws_allocator *allocator, void *ctx) {
754 (void)ctx;
755
756 aws_http_library_init(allocator);
757 struct aws_hpack_context context;
758 aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
759
760 DEFINE_STATIC_HEADER(header1, ":status", "302");
761 DEFINE_STATIC_HEADER(empty_value_header, "c", "");
762 DEFINE_STATIC_HEADER(header2, "a", "b");
763
764 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &header1));
765 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &empty_value_header));
766 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &header2));
767 /*
768 So at this point dynamic table should look like:
769 * INDEX NAME VALUE
770 * 62 a b
771 * 63 "c" ""
772 * 64 :status 302
773 */
774 bool found_value = false;
775 ASSERT_UINT_EQUALS(64, aws_hpack_find_index(&context, &header1, true, &found_value));
776 ASSERT_UINT_EQUALS(63, aws_hpack_find_index(&context, &empty_value_header, true, &found_value));
777 ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &header2, true, &found_value));
778
779 /* Clean up */
780 aws_hpack_context_clean_up(&context);
781 aws_http_library_clean_up();
782 return AWS_OP_SUCCESS;
783 }
784
785 /* Test header with empty name and value */
786 AWS_TEST_CASE(hpack_dynamic_table_with_empty_header, test_hpack_dynamic_table_with_empty_header)
787 static int test_hpack_dynamic_table_with_empty_header(struct aws_allocator *allocator, void *ctx) {
788 (void)ctx;
789
790 aws_http_library_init(allocator);
791 struct aws_hpack_context context;
792 aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
793 DEFINE_STATIC_HEADER(header1, ":status", "302");
794 DEFINE_STATIC_HEADER(empty_header, "", "");
795 DEFINE_STATIC_HEADER(header2, "a", "b");
796
797 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &header1));
798 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &empty_header));
799 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &header2));
800 /*
801 So at this point dynamic table should look like:
802 * INDEX NAME VALUE
803 * 62 a b
804 * 63 "" ""
805 * 64 :status 302
806 */
807 bool found_value = false;
808 ASSERT_UINT_EQUALS(64, aws_hpack_find_index(&context, &header1, true, &found_value));
809 ASSERT_UINT_EQUALS(63, aws_hpack_find_index(&context, &empty_header, true, &found_value));
810 ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &header2, true, &found_value));
811
812 /* Clean up */
813 aws_hpack_context_clean_up(&context);
814 aws_http_library_clean_up();
815 return AWS_OP_SUCCESS;
816 }
817
818 AWS_TEST_CASE(hpack_dynamic_table_growth_corner_case, test_hpack_dynamic_table_growth_corner_case)
819 static int test_hpack_dynamic_table_growth_corner_case(struct aws_allocator *allocator, void *ctx) {
820 (void)ctx;
821
822 aws_http_library_init(allocator);
823 struct aws_hpack_context context;
824 aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
825
826 DEFINE_STATIC_HEADER(h1, "herp", "derp");
827 DEFINE_STATIC_HEADER(h2, "fizz", "buzz");
828
829 const size_t h1_size = aws_hpack_get_header_size(&h1);
830 const size_t h2_size = aws_hpack_get_header_size(&h2);
831
832 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &h1));
833 ASSERT_UINT_EQUALS(1, aws_hpack_get_dynamic_table_num_elements(&context));
834
835 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, h1_size + h2_size + 1));
836 ASSERT_UINT_EQUALS(1, aws_hpack_get_dynamic_table_num_elements(&context));
837
838 ASSERT_SUCCESS(aws_hpack_insert_header(&context, &h2));
839 ASSERT_UINT_EQUALS(2, aws_hpack_get_dynamic_table_num_elements(&context));
840
841 aws_hpack_context_clean_up(&context);
842 aws_http_library_clean_up();
843 return AWS_OP_SUCCESS;
844 }
845
846 AWS_TEST_CASE(hpack_dynamic_table_size_update_from_setting, test_hpack_dynamic_table_size_update_from_setting)
847 static int test_hpack_dynamic_table_size_update_from_setting(struct aws_allocator *allocator, void *ctx) {
848 (void)ctx;
849
850 aws_http_library_init(allocator);
851 struct aws_hpack_encoder encoder;
852 aws_hpack_encoder_init(&encoder, allocator, NULL);
853
854 /* let's pretend multiple times max size update happened from encoder setting */
855 aws_hpack_encoder_update_max_table_size(&encoder, 10);
856 aws_hpack_encoder_update_max_table_size(&encoder, 0);
857 aws_hpack_encoder_update_max_table_size(&encoder, 1337);
858
859 /* encode a header block */
860 struct aws_http_headers *headers = aws_http_headers_new(allocator);
861 /* the 2 entry of static table */
862 DEFINE_STATIC_HEADER(header, ":method", "GET");
863 ASSERT_SUCCESS(aws_http_headers_add_header(headers, &header));
864 struct aws_byte_buf output;
865 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 5));
866 ASSERT_SUCCESS(aws_hpack_encode_header_block(&encoder, headers, &output));
867
868 /* Check the output result, it should contain two dynamic table size updates, besides the header */
869 /**
870 * Expected first table size update (0 0 1) for dynamic table size update, rest is the integer with 5-bit Prefix:
871 * size is 0
872 * 0 1 2 3 4 5 6 7
873 * +---+---+---+---+---+---+---+---+
874 * | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 32
875 * +---+---+---+---+---+---+---+---+
876 *
877 * Expected second table size update:
878 * size is 1337
879 * 0 1 2 3 4 5 6 7
880 * +---+---+---+---+---+---+---+---+
881 * | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 63
882 * | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 154
883 * | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10
884 * +---+---+---+---+---+---+---+---+
885 *
886 * Expected header block: (1) for indexed header field, rest is the index, which is 2
887 * 0 1 2 3 4 5 6 7
888 * +---+---+---+---+---+---+---+---+
889 * | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 130
890 * +---+---+---+---+---+---+---+---+
891 */
892 ASSERT_UINT_EQUALS(5, output.len);
893 ASSERT_UINT_EQUALS(32, output.buffer[0]);
894 ASSERT_UINT_EQUALS(63, output.buffer[1]);
895 ASSERT_UINT_EQUALS(154, output.buffer[2]);
896 ASSERT_UINT_EQUALS(10, output.buffer[3]);
897 ASSERT_UINT_EQUALS(130, output.buffer[4]);
898
899 /* clean up */
900 aws_byte_buf_clean_up(&output);
901 aws_http_headers_release(headers);
902 aws_hpack_encoder_clean_up(&encoder);
903 aws_http_library_clean_up();
904 return AWS_OP_SUCCESS;
905 }