Debian Codegraph @

crt/aws-c-http/tests/test_h2_headers.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
6 #include <aws/testing/aws_test_harness.h>
7
8 #include <aws/http/private/h2_frames.h>
9 #include <aws/http/private/hpack.h>
10
11 enum { S_BUFFER_SIZE = 128 };
12
13 struct header_test_fixture;
14
15 /* Function type used to init and cleanup a fixture */
16 typedef int(header_init_fn)(struct header_test_fixture *);
17 /* Function used to tear down header instances */
18 typedef int(header_clean_up_fn)(void *);
19
20 /* Header compare function */
21 static int s_header_block_eq(
22 const struct aws_http_headers *l_header_fields,
23 const struct aws_http_headers *r_header_fields) {
24
25 const size_t l_size = aws_http_headers_count(l_header_fields);
26 const size_t r_size = aws_http_headers_count(r_header_fields);
27 ASSERT_UINT_EQUALS(l_size, r_size);
28
29 for (size_t i = 0; i < l_size; ++i) {
30 struct aws_http_header l_field;
31 ASSERT_SUCCESS(aws_http_headers_get_index(l_header_fields, i, &l_field));
32
33 struct aws_http_header r_field;
34 ASSERT_SUCCESS(aws_http_headers_get_index(r_header_fields, i, &r_field));
35
36 ASSERT_INT_EQUALS(l_field.compression, r_field.compression);
37 ASSERT_TRUE(aws_byte_cursor_eq(&l_field.name, &r_field.name));
38 ASSERT_TRUE(aws_byte_cursor_eq(&l_field.value, &r_field.value));
39 }
40
41 return AWS_OP_SUCCESS;
42 }
43
44 /* Contains all of the information required to run a header's test case */
45 struct header_test_fixture {
46 header_init_fn *init;
47 header_clean_up_fn *header_clean_up;
48 header_init_fn *teardown;
49
50 struct aws_allocator *allocator;
51 bool one_byte_at_a_time; /* T: decode one byte at a time. F: decode whole buffer at once */
52
53 struct aws_hpack_encoder encoder;
54 struct aws_hpack_decoder decoder;
55
56 struct aws_http_headers *headers_to_encode;
57 struct aws_byte_buf expected_encoding_buf;
58 struct aws_http_headers *decoded_headers;
59 };
60
61 static int s_header_test_before(struct aws_allocator *allocator, void *ctx) {
62
63 struct header_test_fixture *fixture = ctx;
64 fixture->allocator = allocator;
65
66 aws_http_library_init(allocator);
67
68 aws_hpack_encoder_init(&fixture->encoder, allocator, NULL);
69 aws_hpack_decoder_init(&fixture->decoder, allocator, NULL);
70 fixture->headers_to_encode = aws_http_headers_new(allocator);
71 ASSERT_NOT_NULL(fixture->headers_to_encode);
72
73 ASSERT_SUCCESS(aws_byte_buf_init(&fixture->expected_encoding_buf, allocator, S_BUFFER_SIZE));
74 fixture->decoded_headers = aws_http_headers_new(allocator);
75 ASSERT_NOT_NULL(fixture->decoded_headers);
76
77 return AWS_OP_SUCCESS;
78 }
79
80 static int s_header_test_run(struct aws_allocator *allocator, void *ctx) {
81
82 struct header_test_fixture *fixture = ctx;
83
84 /* Init the in_header & buffer */
85 ASSERT_SUCCESS(fixture->init(fixture));
86
87 /* Encode */
88
89 /* Create the output buffer */
90 struct aws_byte_buf output_buffer;
91 ASSERT_SUCCESS(aws_byte_buf_init(&output_buffer, allocator, S_BUFFER_SIZE));
92
93 /* Encode the headers */
94 ASSERT_SUCCESS(aws_hpack_encode_header_block(&fixture->encoder, fixture->headers_to_encode, &output_buffer));
95
96 /* Compare the encoded output against the expected header block fragment */
97 ASSERT_BIN_ARRAYS_EQUALS(
98 fixture->expected_encoding_buf.buffer,
99 fixture->expected_encoding_buf.len,
100 output_buffer.buffer,
101 output_buffer.len);
102
103 /* Decode */
104 struct aws_byte_cursor payload = aws_byte_cursor_from_buf(&output_buffer);
105 while (payload.len) {
106 struct aws_hpack_decode_result result;
107
108 if (fixture->one_byte_at_a_time) {
109 struct aws_byte_cursor one_byte_payload = aws_byte_cursor_advance(&payload, 1);
110 ASSERT_SUCCESS(aws_hpack_decode(&fixture->decoder, &one_byte_payload, &result));
111 ASSERT_UINT_EQUALS(0, one_byte_payload.len);
112 } else {
113 ASSERT_SUCCESS(aws_hpack_decode(&fixture->decoder, &payload, &result));
114 }
115
116 if (result.type == AWS_HPACK_DECODE_T_HEADER_FIELD) {
117 ASSERT_SUCCESS(aws_http_headers_add_header(fixture->decoded_headers, &result.data.header_field));
118 }
119 }
120
121 /* Compare the headers */
122 ASSERT_SUCCESS(s_header_block_eq(fixture->headers_to_encode, fixture->decoded_headers));
123
124 aws_byte_buf_clean_up(&output_buffer);
125 return AWS_OP_SUCCESS;
126 }
127
128 static int s_header_test_after(struct aws_allocator *allocator, int setup_res, void *ctx) {
129
130 (void)allocator;
131
132 if (!setup_res) {
133 struct header_test_fixture *fixture = ctx;
134
135 /* Tear down the header & buffer */
136 if (fixture->teardown) {
137 fixture->teardown(fixture);
138 }
139
140 /* Tear down the fixture */
141 aws_http_headers_release(fixture->decoded_headers);
142 aws_byte_buf_clean_up(&fixture->expected_encoding_buf);
143 aws_http_headers_release(fixture->headers_to_encode);
144 aws_hpack_decoder_clean_up(&fixture->decoder);
145 aws_hpack_encoder_clean_up(&fixture->encoder);
146 }
147 aws_http_library_clean_up();
148
149 return AWS_OP_SUCCESS;
150 }
151
152 #define HEADER_TEST(t_name, i, t) \
153 static struct header_test_fixture s_##t_name##_fixture = { \
154 .init = (i), \
155 .teardown = (t), \
156 }; \
157 AWS_TEST_CASE_FIXTURE(t_name, s_header_test_before, s_header_test_run, s_header_test_after, &s_##t_name##_fixture) \
158 static struct header_test_fixture s_##t_name##_one_byte_at_a_time_fixture = { \
159 .init = (i), \
160 .teardown = (t), \
161 .one_byte_at_a_time = true, \
162 }; \
163 AWS_TEST_CASE_FIXTURE( \
164 t_name##_one_byte_at_a_time, \
165 s_header_test_before, \
166 s_header_test_run, \
167 s_header_test_after, \
168 &s_##t_name##_one_byte_at_a_time_fixture)
169
170 #define DEFINE_STATIC_HEADER(_key, _value, _behavior) \
171 { \
172 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_key), \
173 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \
174 .compression = AWS_HTTP_HEADER_COMPRESSION_##_behavior, \
175 }
176
177 /* Test HEADERS frame with empty payload */
178 static int s_test_empty_payload(struct header_test_fixture *fixture) {
179 (void)fixture;
180 return AWS_OP_SUCCESS;
181 }
182 HEADER_TEST(h2_header_empty_payload, s_test_empty_payload, NULL);
183
184 /* RFC-7541 - Header Field Representation Examples - C.2.1. Literal Header Field with Indexing */
185 static int s_test_ex_2_1_init(struct header_test_fixture *fixture) {
186
187 aws_hpack_encoder_set_huffman_mode(&fixture->encoder, AWS_HPACK_HUFFMAN_NEVER);
188
189 struct aws_http_header headers[] = {
190 DEFINE_STATIC_HEADER("custom-key", "custom-header", USE_CACHE),
191 };
192 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode, headers, AWS_ARRAY_SIZE(headers)));
193
194 static const uint8_t encoded[] = {
195 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0d,
196 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72,
197 };
198 aws_byte_buf_write(&fixture->expected_encoding_buf, encoded, sizeof(encoded));
199
200 return AWS_OP_SUCCESS;
201 }
202 HEADER_TEST(h2_header_ex_2_1, s_test_ex_2_1_init, NULL);
203
204 /* RFC-7541 - Header Field Representation Examples - C.2.2. Literal Header Field without Indexing */
205 static int s_test_ex_2_2_init(struct header_test_fixture *fixture) {
206
207 aws_hpack_encoder_set_huffman_mode(&fixture->encoder, AWS_HPACK_HUFFMAN_NEVER);
208
209 struct aws_http_header headers[] = {
210 DEFINE_STATIC_HEADER(":path", "/sample/path", NO_CACHE),
211 };
212 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode, headers, AWS_ARRAY_SIZE(headers)));
213
214 static const uint8_t encoded[] = {
215 0x04, 0x0c, 0x2f, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x61, 0x74, 0x68};
216
217 aws_byte_buf_write(&fixture->expected_encoding_buf, encoded, sizeof(encoded));
218
219 return AWS_OP_SUCCESS;
220 }
221 HEADER_TEST(h2_header_ex_2_2, s_test_ex_2_2_init, NULL);
222
223 /* RFC-7541 - Header Field Representation Examples - C.2.3. Literal Header Field Never Indexed */
224 static int s_test_ex_2_3_init(struct header_test_fixture *fixture) {
225
226 aws_hpack_encoder_set_huffman_mode(&fixture->encoder, AWS_HPACK_HUFFMAN_NEVER);
227
228 struct aws_http_header headers[] = {
229 DEFINE_STATIC_HEADER("password", "secret", NO_FORWARD_CACHE),
230 };
231 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode, headers, AWS_ARRAY_SIZE(headers)));
232
233 static const uint8_t encoded[] = {
234 0x10, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74};
235
236 aws_byte_buf_write(&fixture->expected_encoding_buf, encoded, sizeof(encoded));
237
238 return AWS_OP_SUCCESS;
239 }
240 HEADER_TEST(h2_header_ex_2_3, s_test_ex_2_3_init, NULL);
241
242 /* RFC-7541 - Header Field Representation Examples - C.2.3. Indexed Header Field */
243 static int s_test_ex_2_4_init(struct header_test_fixture *fixture) {
244
245 aws_hpack_encoder_set_huffman_mode(&fixture->encoder, AWS_HPACK_HUFFMAN_NEVER);
246
247 struct aws_http_header headers[] = {
248 DEFINE_STATIC_HEADER(":method", "GET", USE_CACHE),
249 };
250 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode, headers, AWS_ARRAY_SIZE(headers)));
251
252 static const uint8_t encoded[] = {
253 0x82,
254 };
255 aws_byte_buf_write(&fixture->expected_encoding_buf, encoded, sizeof(encoded));
256
257 return AWS_OP_SUCCESS;
258 }
259 HEADER_TEST(h2_header_ex_2_4, s_test_ex_2_4_init, NULL);
260
261 struct header_request_response_test_fixture;
262 /* Function type used to init for request and response */
263 typedef int(header_request_response_init_fn)(struct header_request_response_test_fixture *);
264
265 /* Contains all of the information required to run a header's test case */
266 struct header_request_response_test_fixture {
267 header_request_response_init_fn *init;
268 header_clean_up_fn *header_clean_up;
269 header_request_response_init_fn *teardown;
270
271 struct aws_allocator *allocator;
272 bool one_byte_at_a_time; /* T: decode one byte at a time. F: decode whole buffer at once */
273
274 struct aws_hpack_encoder encoder;
275 struct aws_hpack_decoder decoder;
276
277 struct aws_http_headers *headers_to_encode[3];
278 struct aws_byte_buf expected_encoding_buf[3];
279 struct aws_http_header last_entry_dynamic_table[3];
280 size_t dynamic_table_len[3];
281
282 struct aws_http_headers *decoded_headers;
283 };
284
285 static int s_header_request_response_test_before(struct aws_allocator *allocator, void *ctx) {
286
287 struct header_request_response_test_fixture *fixture = ctx;
288 fixture->allocator = allocator;
289
290 aws_http_library_init(allocator);
291
292 aws_hpack_encoder_init(&fixture->encoder, allocator, NULL);
293 aws_hpack_decoder_init(&fixture->decoder, allocator, NULL);
294 for (int i = 0; i < 3; i++) {
295 fixture->headers_to_encode[i] = aws_http_headers_new(allocator);
296 ASSERT_NOT_NULL(fixture->headers_to_encode[i]);
297 ASSERT_SUCCESS(aws_byte_buf_init(&fixture->expected_encoding_buf[i], allocator, S_BUFFER_SIZE));
298 }
299
300 fixture->decoded_headers = aws_http_headers_new(allocator);
301 ASSERT_NOT_NULL(fixture->decoded_headers);
302
303 return AWS_OP_SUCCESS;
304 }
305
306 static int s_encoder_result_check(
307 struct header_request_response_test_fixture *fixture,
308 struct aws_http_headers *headers_to_encode,
309 struct aws_byte_buf expected_encoding_buf,
310 struct aws_byte_buf *output_buffer) {
311 /* Encode the headers */
312 ASSERT_SUCCESS(aws_hpack_encode_header_block(&fixture->encoder, headers_to_encode, output_buffer));
313
314 /* Compare the encoded output against the expected header block fragment */
315 ASSERT_BIN_ARRAYS_EQUALS(
316 expected_encoding_buf.buffer, expected_encoding_buf.len, output_buffer->buffer, output_buffer->len);
317
318 /* Decode */
319 struct aws_byte_cursor payload = aws_byte_cursor_from_buf(output_buffer);
320 while (payload.len) {
321 struct aws_hpack_decode_result result;
322
323 if (fixture->one_byte_at_a_time) {
324 struct aws_byte_cursor one_byte_payload = aws_byte_cursor_advance(&payload, 1);
325 ASSERT_SUCCESS(aws_hpack_decode(&fixture->decoder, &one_byte_payload, &result));
326 ASSERT_UINT_EQUALS(0, one_byte_payload.len);
327 } else {
328 ASSERT_SUCCESS(aws_hpack_decode(&fixture->decoder, &payload, &result));
329 }
330
331 if (result.type == AWS_HPACK_DECODE_T_HEADER_FIELD) {
332 ASSERT_SUCCESS(aws_http_headers_add_header(fixture->decoded_headers, &result.data.header_field));
333 }
334 }
335
336 /* Compare the headers */
337 ASSERT_SUCCESS(s_header_block_eq(headers_to_encode, fixture->decoded_headers));
338 /* Reset state */
339 aws_byte_buf_reset(output_buffer, false);
340 aws_http_headers_clear(fixture->decoded_headers);
341 return AWS_OP_SUCCESS;
342 }
343
344 static int s_dynamic_table_last_entry_check(
345 struct header_request_response_test_fixture *fixture,
346 struct aws_http_header *expected_entry,
347 size_t dynamic_table_len) {
348 /* check the decoder's dynamic table */
349 const struct aws_hpack_context *context = &fixture->decoder.context;
350 /* get the last element in dynamic table, which will be the absolute index plus all the elements in static table */
351 ASSERT_TRUE(dynamic_table_len == aws_hpack_get_dynamic_table_num_elements(context));
352 const struct aws_http_header *back = aws_hpack_get_header(context, dynamic_table_len + 61);
353 ASSERT_TRUE(aws_byte_cursor_eq(&back->name, &expected_entry->name));
354 ASSERT_TRUE(aws_byte_cursor_eq(&back->value, &expected_entry->value));
355 /* check the encoder's dynamic table */
356 context = &fixture->encoder.context;
357 ASSERT_TRUE(dynamic_table_len == aws_hpack_get_dynamic_table_num_elements(context));
358 back = aws_hpack_get_header(context, dynamic_table_len + 61);
359 ASSERT_TRUE(aws_byte_cursor_eq(&back->name, &expected_entry->name));
360 ASSERT_TRUE(aws_byte_cursor_eq(&back->value, &expected_entry->value));
361 return AWS_OP_SUCCESS;
362 }
363
364 static int s_header_request_response_test_run(struct aws_allocator *allocator, void *ctx) {
365
366 struct header_request_response_test_fixture *fixture = ctx;
367
368 /* Init the in_header & buffer */
369 ASSERT_SUCCESS(fixture->init(fixture));
370
371 /* Encode */
372
373 /* Create the output buffer */
374 struct aws_byte_buf output_buffer;
375 ASSERT_SUCCESS(aws_byte_buf_init(&output_buffer, allocator, S_BUFFER_SIZE));
376 /* check three results */
377 for (int i = 0; i < 3; i++) {
378 ASSERT_SUCCESS(s_encoder_result_check(
379 fixture, fixture->headers_to_encode[i], fixture->expected_encoding_buf[i], &output_buffer));
380 ASSERT_SUCCESS(s_dynamic_table_last_entry_check(
381 fixture, &fixture->last_entry_dynamic_table[i], fixture->dynamic_table_len[i]));
382 }
383
384 aws_byte_buf_clean_up(&output_buffer);
385 return AWS_OP_SUCCESS;
386 }
387
388 static int s_header_request_response_test_after(struct aws_allocator *allocator, int setup_res, void *ctx) {
389
390 (void)allocator;
391
392 if (!setup_res) {
393 struct header_request_response_test_fixture *fixture = ctx;
394
395 /* Tear down the header & buffer */
396 if (fixture->teardown) {
397 fixture->teardown(fixture);
398 }
399
400 /* Tear down the fixture */
401 aws_http_headers_release(fixture->decoded_headers);
402 for (int i = 0; i < 3; i++) {
403 aws_byte_buf_clean_up(&fixture->expected_encoding_buf[i]);
404 aws_http_headers_release(fixture->headers_to_encode[i]);
405 }
406 aws_hpack_decoder_clean_up(&fixture->decoder);
407 aws_hpack_encoder_clean_up(&fixture->encoder);
408 }
409 aws_http_library_clean_up();
410
411 return AWS_OP_SUCCESS;
412 }
413
414 #define HEADER_REQUEST_RESPONSE_TEST(t_name, i, t) \
415 static struct header_request_response_test_fixture s_##t_name##_fixture = { \
416 .init = (i), \
417 .teardown = (t), \
418 }; \
419 AWS_TEST_CASE_FIXTURE( \
420 t_name, \
421 s_header_request_response_test_before, \
422 s_header_request_response_test_run, \
423 s_header_request_response_test_after, \
424 &s_##t_name##_fixture) \
425 static struct header_request_response_test_fixture s_##t_name##_one_byte_at_a_time_fixture = { \
426 .init = (i), \
427 .teardown = (t), \
428 .one_byte_at_a_time = true, \
429 }; \
430 AWS_TEST_CASE_FIXTURE( \
431 t_name##_one_byte_at_a_time, \
432 s_header_request_response_test_before, \
433 s_header_request_response_test_run, \
434 s_header_request_response_test_after, \
435 &s_##t_name##_one_byte_at_a_time_fixture)
436
437 /* RFC-7541 - Request Examples without Huffman Coding - C.3 */
438 static int s_test_ex_3_init(struct header_request_response_test_fixture *fixture) {
439
440 aws_hpack_encoder_set_huffman_mode(&fixture->encoder, AWS_HPACK_HUFFMAN_NEVER);
441 int index = 0;
442 /* First Request RFC-7541 C.3.1 */
443 struct aws_http_header headers_1[] = {
444 DEFINE_STATIC_HEADER(":method", "GET", USE_CACHE),
445 DEFINE_STATIC_HEADER(":scheme", "http", USE_CACHE),
446 DEFINE_STATIC_HEADER(":path", "/", USE_CACHE),
447 DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE),
448 };
449 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_1, AWS_ARRAY_SIZE(headers_1)));
450
451 static const uint8_t encoded_1[] = {
452 0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65,
453 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
454 };
455 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_1, sizeof(encoded_1));
456
457 struct aws_http_header last_entry_1 = DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE);
458 fixture->last_entry_dynamic_table[index] = last_entry_1;
459 fixture->dynamic_table_len[index] = 1;
460 index++;
461
462 /* Second Request RFC-7541 C.3.2 */
463 struct aws_http_header headers_2[] = {
464 DEFINE_STATIC_HEADER(":method", "GET", USE_CACHE),
465 DEFINE_STATIC_HEADER(":scheme", "http", USE_CACHE),
466 DEFINE_STATIC_HEADER(":path", "/", USE_CACHE),
467 DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE),
468 DEFINE_STATIC_HEADER("cache-control", "no-cache", USE_CACHE),
469 };
470 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_2, AWS_ARRAY_SIZE(headers_2)));
471
472 static const uint8_t encoded_2[] = {
473 0x82, 0x86, 0x84, 0xbe, 0x58, 0x08, 0x6e, 0x6f, 0x2d, 0x63, 0x61, 0x63, 0x68, 0x65};
474 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_2, sizeof(encoded_2));
475
476 struct aws_http_header last_entry_2 = DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE);
477 fixture->last_entry_dynamic_table[index] = last_entry_2;
478 fixture->dynamic_table_len[index] = 2;
479 index++;
480
481 /* Third Request RFC-7541 C.3.3 */
482 struct aws_http_header headers_3[] = {
483 DEFINE_STATIC_HEADER(":method", "GET", USE_CACHE),
484 DEFINE_STATIC_HEADER(":scheme", "https", USE_CACHE),
485 DEFINE_STATIC_HEADER(":path", "/index.html", USE_CACHE),
486 DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE),
487 DEFINE_STATIC_HEADER("custom-key", "custom-value", USE_CACHE),
488 };
489 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_3, AWS_ARRAY_SIZE(headers_3)));
490
491 static const uint8_t encoded_3[] = {
492 0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65,
493 0x79, 0x0c, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65,
494 };
495 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_3, sizeof(encoded_3));
496
497 struct aws_http_header last_entry_3 = DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE);
498 fixture->last_entry_dynamic_table[index] = last_entry_3;
499 fixture->dynamic_table_len[index] = 3;
500
501 return AWS_OP_SUCCESS;
502 }
503 HEADER_REQUEST_RESPONSE_TEST(h2_header_ex_3, s_test_ex_3_init, NULL);
504
505 /* RFC-7541 - Request Examples with Huffman Coding - C.4 */
506 static int s_test_ex_4_init(struct header_request_response_test_fixture *fixture) {
507
508 aws_hpack_encoder_set_huffman_mode(&fixture->encoder, AWS_HPACK_HUFFMAN_ALWAYS);
509 int index = 0;
510 /* First Request RFC-7541 C.4.1 */
511 struct aws_http_header headers_1[] = {
512 DEFINE_STATIC_HEADER(":method", "GET", USE_CACHE),
513 DEFINE_STATIC_HEADER(":scheme", "http", USE_CACHE),
514 DEFINE_STATIC_HEADER(":path", "/", USE_CACHE),
515 DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE),
516 };
517 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_1, AWS_ARRAY_SIZE(headers_1)));
518
519 static const uint8_t encoded_1[] = {
520 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff};
521
522 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_1, sizeof(encoded_1));
523
524 struct aws_http_header last_entry_1 = DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE);
525 fixture->last_entry_dynamic_table[index] = last_entry_1;
526 fixture->dynamic_table_len[index] = 1;
527 index++;
528
529 /* Second Request RFC-7541 C.4.2 */
530 struct aws_http_header headers_2[] = {
531 DEFINE_STATIC_HEADER(":method", "GET", USE_CACHE),
532 DEFINE_STATIC_HEADER(":scheme", "http", USE_CACHE),
533 DEFINE_STATIC_HEADER(":path", "/", USE_CACHE),
534 DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE),
535 DEFINE_STATIC_HEADER("cache-control", "no-cache", USE_CACHE),
536 };
537 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_2, AWS_ARRAY_SIZE(headers_2)));
538
539 static const uint8_t encoded_2[] = {0x82, 0x86, 0x84, 0xbe, 0x58, 0x86, 0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf};
540 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_2, sizeof(encoded_2));
541
542 struct aws_http_header last_entry_2 = DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE);
543 fixture->last_entry_dynamic_table[index] = last_entry_2;
544 fixture->dynamic_table_len[index] = 2;
545 index++;
546
547 /* Third Request RFC-7541 C.4.3 */
548 struct aws_http_header headers_3[] = {
549 DEFINE_STATIC_HEADER(":method", "GET", USE_CACHE),
550 DEFINE_STATIC_HEADER(":scheme", "https", USE_CACHE),
551 DEFINE_STATIC_HEADER(":path", "/index.html", USE_CACHE),
552 DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE),
553 DEFINE_STATIC_HEADER("custom-key", "custom-value", USE_CACHE),
554 };
555 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_3, AWS_ARRAY_SIZE(headers_3)));
556
557 static const uint8_t encoded_3[] = {
558 0x82, 0x87, 0x85, 0xbf, 0x40, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9,
559 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf,
560 };
561 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_3, sizeof(encoded_3));
562
563 struct aws_http_header last_entry_3 = DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE);
564 fixture->last_entry_dynamic_table[index] = last_entry_3;
565 fixture->dynamic_table_len[index] = 3;
566
567 return AWS_OP_SUCCESS;
568 }
569 HEADER_REQUEST_RESPONSE_TEST(h2_header_ex_4, s_test_ex_4_init, NULL);
570
571 /* RFC-7541 - Response Examples without Huffman Coding - C.5 */
572 static int s_test_ex_5_init(struct header_request_response_test_fixture *fixture) {
573
574 /* set the max table size to 256 */
575 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&fixture->encoder.context, 256));
576 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&fixture->decoder.context, 256));
577
578 aws_hpack_encoder_set_huffman_mode(&fixture->encoder, AWS_HPACK_HUFFMAN_NEVER);
579 int index = 0;
580 /* First Response RFC-7541 C.5.1 */
581 struct aws_http_header headers_1[] = {
582 DEFINE_STATIC_HEADER(":status", "302", USE_CACHE),
583 DEFINE_STATIC_HEADER("cache-control", "private", USE_CACHE),
584 DEFINE_STATIC_HEADER("date", "Mon, 21 Oct 2013 20:13:21 GMT", USE_CACHE),
585 DEFINE_STATIC_HEADER("location", "https://www.example.com", USE_CACHE),
586 };
587 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_1, AWS_ARRAY_SIZE(headers_1)));
588
589 static const uint8_t encoded_1[] = {
590 0x48, 0x03, 0x33, 0x30, 0x32, 0x58, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x61, 0x1d, 0x4d, 0x6f,
591 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a,
592 0x31, 0x33, 0x3a, 0x32, 0x31, 0x20, 0x47, 0x4d, 0x54, 0x6e, 0x17, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
593 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
594 };
595 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_1, sizeof(encoded_1));
596
597 struct aws_http_header last_entry_1 = DEFINE_STATIC_HEADER(":status", "302", USE_CACHE);
598 fixture->last_entry_dynamic_table[index] = last_entry_1;
599 fixture->dynamic_table_len[index] = 4;
600 index++;
601
602 /* Second Response RFC-7541 C.5.2 */
603 struct aws_http_header headers_2[] = {
604 DEFINE_STATIC_HEADER(":status", "307", USE_CACHE),
605 DEFINE_STATIC_HEADER("cache-control", "private", USE_CACHE),
606 DEFINE_STATIC_HEADER("date", "Mon, 21 Oct 2013 20:13:21 GMT", USE_CACHE),
607 DEFINE_STATIC_HEADER("location", "https://www.example.com", USE_CACHE),
608 };
609 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_2, AWS_ARRAY_SIZE(headers_2)));
610
611 static const uint8_t encoded_2[] = {0x48, 0x03, 0x33, 0x30, 0x37, 0xc1, 0xc0, 0xbf};
612 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_2, sizeof(encoded_2));
613
614 struct aws_http_header last_entry_2 = DEFINE_STATIC_HEADER("cache-control", "private", USE_CACHE);
615 fixture->last_entry_dynamic_table[index] = last_entry_2;
616 fixture->dynamic_table_len[index] = 4;
617 index++;
618
619 /* Third Response RFC-7541 C.5.3 */
620 struct aws_http_header headers_3[] = {
621 DEFINE_STATIC_HEADER(":status", "200", USE_CACHE),
622 DEFINE_STATIC_HEADER("cache-control", "private", USE_CACHE),
623 DEFINE_STATIC_HEADER("date", "Mon, 21 Oct 2013 20:13:22 GMT", USE_CACHE),
624 DEFINE_STATIC_HEADER("location", "https://www.example.com", USE_CACHE),
625 DEFINE_STATIC_HEADER("content-encoding", "gzip", USE_CACHE),
626 DEFINE_STATIC_HEADER("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", USE_CACHE),
627 };
628 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_3, AWS_ARRAY_SIZE(headers_3)));
629
630 static const uint8_t encoded_3[] = {
631 0x88, 0xc1, 0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32,
632 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x32, 0x20, 0x47, 0x4d, 0x54, 0xc0,
633 0x5a, 0x04, 0x67, 0x7a, 0x69, 0x70, 0x77, 0x38, 0x66, 0x6f, 0x6f, 0x3d, 0x41, 0x53, 0x44, 0x4a, 0x4b,
634 0x48, 0x51, 0x4b, 0x42, 0x5a, 0x58, 0x4f, 0x51, 0x57, 0x45, 0x4f, 0x50, 0x49, 0x55, 0x41, 0x58, 0x51,
635 0x57, 0x45, 0x4f, 0x49, 0x55, 0x3b, 0x20, 0x6d, 0x61, 0x78, 0x2d, 0x61, 0x67, 0x65, 0x3d, 0x33, 0x36,
636 0x30, 0x30, 0x3b, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31,
637 };
638 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_3, sizeof(encoded_3));
639
640 struct aws_http_header last_entry_3 = DEFINE_STATIC_HEADER("date", "Mon, 21 Oct 2013 20:13:22 GMT", USE_CACHE);
641 fixture->last_entry_dynamic_table[index] = last_entry_3;
642 fixture->dynamic_table_len[index] = 3;
643 return AWS_OP_SUCCESS;
644 }
645 HEADER_REQUEST_RESPONSE_TEST(h2_header_ex_5, s_test_ex_5_init, NULL);
646
647 /* RFC-7541 - Response Examples with Huffman Coding - C.6 */
648 static int s_test_ex_6_init(struct header_request_response_test_fixture *fixture) {
649
650 /* set the max table size to 256 */
651 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&fixture->encoder.context, 256));
652 ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&fixture->decoder.context, 256));
653
654 aws_hpack_encoder_set_huffman_mode(&fixture->encoder, AWS_HPACK_HUFFMAN_ALWAYS);
655
656 int index = 0;
657 /* First Response RFC-7541 C.6.1 */
658 struct aws_http_header headers_1[] = {
659 DEFINE_STATIC_HEADER(":status", "302", USE_CACHE),
660 DEFINE_STATIC_HEADER("cache-control", "private", USE_CACHE),
661 DEFINE_STATIC_HEADER("date", "Mon, 21 Oct 2013 20:13:21 GMT", USE_CACHE),
662 DEFINE_STATIC_HEADER("location", "https://www.example.com", USE_CACHE),
663 };
664 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_1, AWS_ARRAY_SIZE(headers_1)));
665
666 static const uint8_t encoded_1[] = {
667 0x48, 0x82, 0x64, 0x02, 0x58, 0x85, 0xae, 0xc3, 0x77, 0x1a, 0x4b, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10,
668 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x82, 0xa6, 0x2d, 0x1b, 0xff, 0x6e,
669 0x91, 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3,
670 };
671 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_1, sizeof(encoded_1));
672
673 struct aws_http_header last_entry_1 = DEFINE_STATIC_HEADER(":status", "302", USE_CACHE);
674 fixture->last_entry_dynamic_table[index] = last_entry_1;
675 fixture->dynamic_table_len[index] = 4;
676 index++;
677
678 /* Second Response RFC-7541 C.6.2 */
679 struct aws_http_header headers_2[] = {
680 DEFINE_STATIC_HEADER(":status", "307", USE_CACHE),
681 DEFINE_STATIC_HEADER("cache-control", "private", USE_CACHE),
682 DEFINE_STATIC_HEADER("date", "Mon, 21 Oct 2013 20:13:21 GMT", USE_CACHE),
683 DEFINE_STATIC_HEADER("location", "https://www.example.com", USE_CACHE),
684 };
685 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_2, AWS_ARRAY_SIZE(headers_2)));
686
687 static const uint8_t encoded_2[] = {0x48, 0x83, 0x64, 0x0e, 0xff, 0xc1, 0xc0, 0xbf};
688 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_2, sizeof(encoded_2));
689
690 struct aws_http_header last_entry_2 = DEFINE_STATIC_HEADER("cache-control", "private", USE_CACHE);
691 fixture->last_entry_dynamic_table[index] = last_entry_2;
692 fixture->dynamic_table_len[index] = 4;
693 index++;
694
695 /* Third Response RFC-7541 C.6.3 */
696 struct aws_http_header headers_3[] = {
697 DEFINE_STATIC_HEADER(":status", "200", USE_CACHE),
698 DEFINE_STATIC_HEADER("cache-control", "private", USE_CACHE),
699 DEFINE_STATIC_HEADER("date", "Mon, 21 Oct 2013 20:13:22 GMT", USE_CACHE),
700 DEFINE_STATIC_HEADER("location", "https://www.example.com", USE_CACHE),
701 DEFINE_STATIC_HEADER("content-encoding", "gzip", USE_CACHE),
702 DEFINE_STATIC_HEADER("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", USE_CACHE),
703 };
704 ASSERT_SUCCESS(aws_http_headers_add_array(fixture->headers_to_encode[index], headers_3, AWS_ARRAY_SIZE(headers_3)));
705
706 static const uint8_t encoded_3[] = {
707 0x88, 0xc1, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95,
708 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x84, 0xa6, 0x2d, 0x1b, 0xff, 0xc0, 0x5a, 0x83, 0x9b, 0xd9, 0xab,
709 0x77, 0xad, 0x94, 0xe7, 0x82, 0x1d, 0xd7, 0xf2, 0xe6, 0xc7, 0xb3, 0x35, 0xdf, 0xdf, 0xcd, 0x5b,
710 0x39, 0x60, 0xd5, 0xaf, 0x27, 0x08, 0x7f, 0x36, 0x72, 0xc1, 0xab, 0x27, 0x0f, 0xb5, 0x29, 0x1f,
711 0x95, 0x87, 0x31, 0x60, 0x65, 0xc0, 0x03, 0xed, 0x4e, 0xe5, 0xb1, 0x06, 0x3d, 0x50, 0x07};
712 aws_byte_buf_write(&fixture->expected_encoding_buf[index], encoded_3, sizeof(encoded_3));
713
714 struct aws_http_header last_entry_3 = DEFINE_STATIC_HEADER("date", "Mon, 21 Oct 2013 20:13:22 GMT", USE_CACHE);
715 fixture->last_entry_dynamic_table[index] = last_entry_3;
716 fixture->dynamic_table_len[index] = 3;
717
718 return AWS_OP_SUCCESS;
719 }
720 HEADER_REQUEST_RESPONSE_TEST(h2_header_ex_6, s_test_ex_6_init, NULL);