Debian Codegraph @

crt/aws-c-http/tests/test_h2_decoder.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 "h2_test_helper.h"
6
7 #include <aws/http/private/h2_decoder.h>
8
9 struct fixture {
10 struct aws_allocator *allocator;
11 struct h2_decode_tester decode;
12
13 /* If true, run decoder over input one byte at a time */
14 bool one_byte_at_a_time;
15
16 bool is_server;
17 bool skip_connection_preface;
18 };
19
20 static int s_fixture_init(struct fixture *fixture, struct aws_allocator *allocator) {
21 fixture->allocator = allocator;
22
23 struct h2_decode_tester_options options = {
24 .alloc = allocator,
25 .is_server = fixture->is_server,
26 .skip_connection_preface = fixture->skip_connection_preface,
27 };
28 ASSERT_SUCCESS(h2_decode_tester_init(&fixture->decode, &options));
29
30 return AWS_OP_SUCCESS;
31 }
32
33 static void s_fixture_clean_up(struct fixture *fixture) {
34 h2_decode_tester_clean_up(&fixture->decode);
35 }
36
37 static int s_fixture_test_setup(struct aws_allocator *allocator, void *ctx) {
38 aws_http_library_init(allocator);
39
40 struct fixture *fixture = ctx;
41 ASSERT_SUCCESS(s_fixture_init(fixture, allocator));
42 return AWS_OP_SUCCESS;
43 }
44
45 static int s_fixture_test_teardown(struct aws_allocator *allocator, int setup_result, void *ctx) {
46 (void)allocator;
47 if (setup_result) {
48 return AWS_OP_ERR;
49 }
50
51 struct fixture *fixture = ctx;
52 s_fixture_clean_up(fixture);
53 aws_http_library_clean_up();
54 return AWS_OP_SUCCESS;
55 }
56
57 /* declare 1 test using the fixture */
58 #define TEST_CASE(NAME) \
59 static struct fixture s_fixture_##NAME; \
60 AWS_TEST_CASE_FIXTURE(NAME, s_fixture_test_setup, s_test_##NAME, s_fixture_test_teardown, &s_fixture_##NAME); \
61 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
62
63 /* declare 2 tests, where:
64 * 1) NAME runs the decoder over input all at once
65 * 2) NAME_one_byte_at_a_time runs the decoder on one byte of input at a time. */
66 #define H2_DECODER_TEST_CASE_IMPL(NAME, IS_SERVER, SKIP_PREFACE) \
67 static struct fixture s_fixture_##NAME = { \
68 .is_server = (IS_SERVER), \
69 .skip_connection_preface = (SKIP_PREFACE), \
70 }; \
71 AWS_TEST_CASE_FIXTURE(NAME, s_fixture_test_setup, s_test_##NAME, s_fixture_test_teardown, &s_fixture_##NAME); \
72 static struct fixture s_fixture_##NAME##_one_byte_at_a_time = { \
73 .one_byte_at_a_time = true, \
74 .is_server = (IS_SERVER), \
75 .skip_connection_preface = (SKIP_PREFACE), \
76 }; \
77 AWS_TEST_CASE_FIXTURE( \
78 NAME##_one_byte_at_a_time, \
79 s_fixture_test_setup, \
80 s_test_##NAME, \
81 s_fixture_test_teardown, \
82 &s_fixture_##NAME##_one_byte_at_a_time) \
83 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
84
85 #define H2_DECODER_ON_CLIENT_TEST(NAME) H2_DECODER_TEST_CASE_IMPL(NAME, false /*server*/, true /*skip_preface*/)
86 #define H2_DECODER_ON_SERVER_TEST(NAME) H2_DECODER_TEST_CASE_IMPL(NAME, true /*server*/, true /*skip_preface*/)
87 #define H2_DECODER_ON_CLIENT_PREFACE_TEST(NAME) H2_DECODER_TEST_CASE_IMPL(NAME, false, false)
88 #define H2_DECODER_ON_SERVER_PREFACE_TEST(NAME) H2_DECODER_TEST_CASE_IMPL(NAME, true, false)
89
90 /* Make sure fixture works */
91 TEST_CASE(h2_decoder_sanity_check) {
92 (void)allocator;
93 struct fixture *fixture = ctx;
94 ASSERT_NOT_NULL(fixture);
95 return AWS_OP_SUCCESS;
96 }
97
98 /* Run aws_h2_decode() on input in special ways determined by the fixture */
99 static struct aws_h2err s_decode_all(struct fixture *fixture, struct aws_byte_cursor input) {
100 if (fixture->one_byte_at_a_time) {
101 /* Decode input one byte at a time */
102 while (input.len) {
103 struct aws_byte_cursor one_byte = aws_byte_cursor_advance(&input, 1);
104 struct aws_h2err err = aws_h2_decode(fixture->decode.decoder, &one_byte);
105 if (aws_h2err_failed(err)) {
106 return err;
107 }
108 AWS_FATAL_ASSERT(0 == one_byte.len);
109 }
110
111 } else {
112 /* Decode buffer all at once */
113 struct aws_h2err err = aws_h2_decode(fixture->decode.decoder, &input);
114 if (aws_h2err_failed(err)) {
115 return err;
116 }
117 AWS_FATAL_ASSERT(0 == input.len);
118 }
119
120 return AWS_H2ERR_SUCCESS;
121 }
122
123 /* Test DATA frame */
124 H2_DECODER_ON_CLIENT_TEST(h2_decoder_data) {
125 (void)allocator;
126 struct fixture *fixture = ctx;
127
128 /* clang-format off */
129 uint8_t input[] = {
130 0x00, 0x00, 0x05, /* Length (24) */
131 AWS_H2_FRAME_T_DATA, /* Type (8) */
132 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
133 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
134 /* DATA */
135 'h', 'e', 'l', 'l', 'o', /* Data (*) */
136 };
137 /* clang-format on */
138
139 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
140
141 /* Validate. */
142 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
143 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_DATA, 0x76543210 /*stream_id*/));
144 ASSERT_UINT_EQUALS(5, frame->data_payload_len);
145 ASSERT_TRUE(frame->end_stream);
146 ASSERT_TRUE(aws_byte_buf_eq_c_str(&frame->data, "hello"));
147 return AWS_OP_SUCCESS;
148 }
149
150 /* Test padded DATA frame */
151 H2_DECODER_ON_CLIENT_TEST(h2_decoder_data_padded) {
152 (void)allocator;
153 struct fixture *fixture = ctx;
154
155 /* clang-format off */
156 uint8_t input[] = {
157 0x00, 0x00, 0x08, /* Length (24) */
158 AWS_H2_FRAME_T_DATA, /* Type (8) */
159 AWS_H2_FRAME_F_PADDED, /* Flags (8) */
160 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
161 /* DATA */
162 0x02, /* Pad Length (8) - F_PADDED */
163 'h', 'e', 'l', 'l', 'o', /* Data (*) */
164 0x00, 0x00, /* Padding (*) - F_PADDED */
165 };
166 /* clang-format on */
167
168 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
169
170 /* Validate. */
171 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
172 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_DATA, 0x76543210 /*stream_id*/));
173 ASSERT_UINT_EQUALS(8, frame->data_payload_len);
174 ASSERT_FALSE(frame->end_stream);
175 ASSERT_TRUE(aws_byte_buf_eq_c_str(&frame->data, "hello"));
176 return AWS_OP_SUCCESS;
177 }
178
179 /* OK for PADDED frame to have pad length of zero */
180 H2_DECODER_ON_CLIENT_TEST(h2_decoder_data_pad_length_zero) {
181 (void)allocator;
182 struct fixture *fixture = ctx;
183
184 /* clang-format off */
185 uint8_t input[] = {
186 0x00, 0x00, 0x06, /* Length (24) */
187 AWS_H2_FRAME_T_DATA, /* Type (8) */
188 AWS_H2_FRAME_F_PADDED | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
189 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
190 /* DATA */
191 0x00, /* Pad Length (8) - F_PADDED */
192 'h', 'e', 'l', 'l', 'o', /* Data (*) */
193 /* Padding (*) - F_PADDED */
194 };
195 /* clang-format on */
196
197 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
198
199 /* Validate. */
200 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
201 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_DATA, 0x76543210 /*stream_id*/));
202 ASSERT_UINT_EQUALS(6, frame->data_payload_len);
203 ASSERT_TRUE(frame->end_stream);
204 ASSERT_TRUE(aws_byte_buf_eq_c_str(&frame->data, "hello"));
205 return AWS_OP_SUCCESS;
206 }
207
208 /* OK for DATA frame to have no data */
209 H2_DECODER_ON_CLIENT_TEST(h2_decoder_data_empty) {
210 (void)allocator;
211 struct fixture *fixture = ctx;
212
213 /* clang-format off */
214 uint8_t input[] = {
215 0x00, 0x00, 0x00, /* Length (24) */
216 AWS_H2_FRAME_T_DATA, /* Type (8) */
217 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
218 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
219 /* DATA */
220 /* Data (*) */
221 };
222 /* clang-format on */
223
224 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
225
226 /* Validate. */
227 ASSERT_UINT_EQUALS(1, h2_decode_tester_frame_count(&fixture->decode));
228 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
229 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_DATA, 0x76543210 /*stream_id*/));
230 ASSERT_TRUE(frame->data.len == 0);
231 ASSERT_TRUE(frame->end_stream);
232
233 return AWS_OP_SUCCESS;
234 }
235
236 /* OK for padded DATA frame to have no data */
237 H2_DECODER_ON_CLIENT_TEST(h2_decoder_data_empty_padded) {
238 (void)allocator;
239 struct fixture *fixture = ctx;
240
241 /* clang-format off */
242 uint8_t input[] = {
243 0x00, 0x00, 0x03, /* Length (24) */
244 AWS_H2_FRAME_T_DATA, /* Type (8) */
245 AWS_H2_FRAME_F_PADDED, /* Flags (8) */
246 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
247 /* DATA */
248 0x02, /* Pad Length (8) - F_PADDED */
249 /* Data (*) */
250 0x00, 0x00, /* Padding (*) - F_PADDED */
251 };
252 /* clang-format on */
253
254 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
255
256 /* Validate. */
257 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
258 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_DATA, 0x76543210 /*stream_id*/));
259 ASSERT_UINT_EQUALS(3, frame->data_payload_len);
260 ASSERT_FALSE(frame->end_stream);
261 ASSERT_TRUE(aws_byte_buf_eq_c_str(&frame->data, ""));
262 return AWS_OP_SUCCESS;
263 }
264
265 /* Unexpected flags should be ignored.
266 * DATA frames only support END_STREAM and PADDED*/
267 H2_DECODER_ON_CLIENT_TEST(h2_decoder_data_ignores_unknown_flags) {
268 (void)allocator;
269 struct fixture *fixture = ctx;
270
271 /* clang-format off */
272 uint8_t input[] = {
273 0x00, 0x00, 0x08, /* Length (24) */
274 AWS_H2_FRAME_T_DATA, /* Type (8) */
275 0xFF, /* Flags (8) */
276 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
277 /* DATA */
278 0x02, /* Pad Length (8) - F_PADDED */
279 'h', 'e', 'l', 'l', 'o', /* Data (*) */
280 0x00, 0x00, /* Padding (*) - F_PADDED */
281 };
282 /* clang-format on */
283
284 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
285
286 /* Validate. */
287 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
288 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_DATA, 0x76543210 /*stream_id*/));
289 ASSERT_UINT_EQUALS(8, frame->data_payload_len);
290 ASSERT_TRUE(frame->end_stream);
291 ASSERT_TRUE(aws_byte_buf_eq_c_str(&frame->data, "hello"));
292 return AWS_OP_SUCCESS;
293 }
294
295 H2_DECODER_ON_CLIENT_TEST(h2_decoder_data_payload_max_size_update) {
296 (void)allocator;
297 struct fixture *fixture = ctx;
298 /* The initial max size is set as 16384. Let's create a data frame with 16500 bytes data, and update the setting to
299 * make it valid */
300 aws_h2_decoder_set_setting_max_frame_size(fixture->decode.decoder, 16500);
301 /* clang-format off */
302 uint8_t input[16509] = {
303 0x00, 0x40, 0x74, /* Length (24) */
304 AWS_H2_FRAME_T_DATA, /* Type (8) */
305 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
306 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
307 /* DATA */
308 };
309 /* clang-format on */
310 /* set the data and expected to 16500 'a' */
311 char expected[16500];
312 for (int i = 9; i < 16509; i++) {
313 input[i] = 'a';
314 expected[i - 9] = 'a';
315 }
316 struct aws_byte_cursor expected_cursor = aws_byte_cursor_from_array(expected, sizeof(expected));
317
318 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
319
320 /* Validate. */
321 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
322 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_DATA, 0x76543210 /*stream_id*/));
323 ASSERT_UINT_EQUALS(16500, frame->data_payload_len);
324 ASSERT_TRUE(frame->end_stream);
325 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&expected_cursor, &frame->data));
326 return AWS_OP_SUCCESS;
327 }
328
329 /* The size of a frame payload is limited by the maximum size. An endpoint MUST send an error code of FRAME_SIZE_ERROR
330 * if a frame exceeds the size */
331 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_data_payload_exceed_max_size) {
332 (void)allocator;
333 struct fixture *fixture = ctx;
334 /* The initial max size is set as 16384. Let's create a data frame with 16500 bytes data, which will be invalid in
335 * this case */
336 /* clang-format off */
337 uint8_t input[16509] = {
338 0x00, 0x40, 0x74, /* Length (24) */
339 AWS_H2_FRAME_T_DATA, /* Type (8) */
340 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
341 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
342 /* DATA */
343 };
344 /* clang-format on */
345
346 ASSERT_H2ERR_ERROR(
347 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
348
349 return AWS_OP_SUCCESS;
350 }
351
352 /* DATA frames MUST specify a stream-id */
353 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_data_requires_stream_id) {
354 (void)allocator;
355 struct fixture *fixture = ctx;
356
357 /* clang-format off */
358 uint8_t input[] = {
359 0x00, 0x00, 0x05, /* Length (24) */
360 AWS_H2_FRAME_T_DATA, /* Type (8) */
361 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
362 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
363 /* DATA */
364 'h', 'e', 'l', 'l', 'o', /* Data (*) */
365 };
366 /* clang-format on */
367
368 ASSERT_H2ERR_ERROR(
369 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
370
371 return AWS_OP_SUCCESS;
372 }
373
374 /* Error if frame is padded, but not big enough to contain the padding length */
375 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_payload_too_small_for_pad_length) {
376 (void)allocator;
377 struct fixture *fixture = ctx;
378
379 /* clang-format off */
380 uint8_t input[] = {
381 0x00, 0x00, 0x00, /* Length (24) */
382 AWS_H2_FRAME_T_DATA, /* Type (8) */
383 AWS_H2_FRAME_F_PADDED, /* Flags (8) */
384 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
385 /* DATA */
386 /* Pad Length (8) - F_PADDED */
387 /* Data (*) */
388 /* Padding (*) - F_PADDED */
389 };
390 /* clang-format on */
391
392 ASSERT_H2ERR_ERROR(
393 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
394 return AWS_OP_SUCCESS;
395 }
396
397 /* The most-significant-bit of the encoded stream ID is reserved, and should be ignored when decoding */
398 H2_DECODER_ON_CLIENT_TEST(h2_decoder_stream_id_ignores_reserved_bit) {
399 (void)allocator;
400 struct fixture *fixture = ctx;
401
402 /* clang-format off */
403 uint8_t input[] = {
404 0x00, 0x00, 0x05, /* Length (24) */
405 AWS_H2_FRAME_T_DATA, /* Type (8) */
406 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
407 0xFF, 0xFF, 0xFF, 0xFF, /* Reserved (1) | Stream Identifier (31) */
408 /* DATA */
409 'h', 'e', 'l', 'l', 'o', /* Data (*) */
410 };
411 /* clang-format on */
412
413 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
414
415 /* Validate. */
416 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
417 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_DATA, 0x7FFFFFFF /*stream_id*/));
418 ASSERT_UINT_EQUALS(5, frame->data_payload_len);
419 ASSERT_TRUE(frame->end_stream);
420 ASSERT_TRUE(aws_byte_buf_eq_c_str(&frame->data, "hello"));
421 return AWS_OP_SUCCESS;
422 }
423
424 static int s_check_header(
425 struct h2_decoded_frame *frame,
426 size_t header_idx,
427 const char *name,
428 const char *value,
429 enum aws_http_header_compression compression) {
430
431 struct aws_http_header header_field;
432 ASSERT_SUCCESS(aws_http_headers_get_index(frame->headers, header_idx, &header_field));
433
434 ASSERT_BIN_ARRAYS_EQUALS(name, strlen(name), header_field.name.ptr, header_field.name.len);
435 ASSERT_BIN_ARRAYS_EQUALS(value, strlen(value), header_field.value.ptr, header_field.value.len);
436 ASSERT_INT_EQUALS(compression, header_field.compression);
437 return AWS_OP_SUCCESS;
438 }
439
440 /* Test a simple HEADERS frame
441 * Note that we're not stressing the HPACK decoder here, that's done in other test files */
442 H2_DECODER_ON_CLIENT_TEST(h2_decoder_headers) {
443 (void)allocator;
444 struct fixture *fixture = ctx;
445
446 /* clang-format off */
447 uint8_t input[] = {
448 0x00, 0x00, 11, /* Length (24) */
449 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
450 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
451 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
452 /* HEADERS */
453 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
454 0x7a, 0x04, 't', 'e', 's', 't' /* "user-agent: test" - indexed name, uncompressed value */
455 };
456 /* clang-format on */
457
458 /* Decode */
459 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
460
461 /* Validate */
462 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
463 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
464 ASSERT_FALSE(frame->headers_malformed);
465 ASSERT_UINT_EQUALS(2, aws_http_headers_count(frame->headers));
466 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
467 ASSERT_SUCCESS(s_check_header(frame, 1, "user-agent", "test", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
468 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
469 ASSERT_TRUE(frame->end_stream);
470 return AWS_OP_SUCCESS;
471 }
472
473 /* Test a HEADERS frame with padding */
474 H2_DECODER_ON_CLIENT_TEST(h2_decoder_headers_padded) {
475 (void)allocator;
476 struct fixture *fixture = ctx;
477
478 /* clang-format off */
479 uint8_t input[] = {
480 0x00, 0x00, 0x08, /* Length (24) */
481 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
482 AWS_H2_FRAME_F_PADDED | AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
483 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
484 /* HEADERS */
485 0x02, /* Pad Length (8) - F_PADDED */
486 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
487 0x00, 0x00, /* Padding (*) - F_PADDED */
488 };
489 /* clang-format on */
490
491 /* Decode */
492 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
493
494 /* Validate */
495 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
496 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
497 ASSERT_FALSE(frame->headers_malformed);
498 ASSERT_UINT_EQUALS(1, aws_http_headers_count(frame->headers));
499 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
500 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
501 return AWS_OP_SUCCESS;
502 }
503
504 /* Test a HEADERS frame with priority information
505 * Note that priority information is ignored for now.
506 * We're not testing that it was reported properly, just that decoder can properly consume it */
507 H2_DECODER_ON_CLIENT_TEST(h2_decoder_headers_priority) {
508 (void)allocator;
509 struct fixture *fixture = ctx;
510
511 /* clang-format off */
512 uint8_t input[] = {
513 0x00, 0x00, 10, /* Length (24) */
514 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
515 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_PRIORITY, /* Flags (8) */
516 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
517 /* HEADERS */
518 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) - F_PRIORITY*/
519 0x09, /* Weight (8) - F_PRIORITY */
520 0x48, 0x03, '3', '0', '2' /* ":status: 302" - indexed name, uncompressed value */
521 };
522 /* clang-format on */
523
524 /* Decode */
525 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
526
527 /* Validate */
528 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
529 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
530 ASSERT_FALSE(frame->headers_malformed);
531 ASSERT_UINT_EQUALS(1, aws_http_headers_count(frame->headers));
532 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
533 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
534 return AWS_OP_SUCCESS;
535 }
536
537 /* Test a HEADERS frame with ALL flags set.
538 * Unexpected flags should be ignored, but HEADERS supports: priority and padding and end-headers and end-stream */
539 H2_DECODER_ON_CLIENT_TEST(h2_decoder_headers_ignores_unknown_flags) {
540 (void)allocator;
541 struct fixture *fixture = ctx;
542
543 /* clang-format off */
544 uint8_t input[] = {
545 0x00, 0x00, 13, /* Length (24) */
546 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
547 0xFF, /* Flags (8) */
548 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
549 /* HEADERS */
550 0x02, /* Pad Length (8) - F_PADDED */
551 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) - F_PRIORITY*/
552 0x09, /* Weight (8) - F_PRIORITY */
553 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
554 0x00, 0x00 /* Padding (*) - F_PADDED */
555 };
556 /* clang-format on */
557
558 /* Decode */
559 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
560
561 /* Validate */
562 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
563 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
564 ASSERT_FALSE(frame->headers_malformed);
565 ASSERT_UINT_EQUALS(1, aws_http_headers_count(frame->headers));
566 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
567 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
568 ASSERT_TRUE(frame->end_stream);
569 return AWS_OP_SUCCESS;
570 }
571
572 H2_DECODER_ON_CLIENT_TEST(h2_decoder_headers_response_informational) {
573 (void)allocator;
574 struct fixture *fixture = ctx;
575
576 /* clang-format off */
577 uint8_t input[] = {
578 0x00, 0x00, 0x05, /* Length (24) */
579 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
580 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
581 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
582 /* HEADERS */
583 0x48, 0x03, '1', '0', '0', /* ":status: 100" - indexed name, uncompressed value */
584 };
585 /* clang-format on */
586
587 /* Decode */
588 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
589
590 /* Validate */
591 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
592 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
593 ASSERT_FALSE(frame->headers_malformed);
594 ASSERT_UINT_EQUALS(1, aws_http_headers_count(frame->headers));
595 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "100", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
596 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_INFORMATIONAL, frame->header_block_type);
597 ASSERT_FALSE(frame->end_stream);
598 return AWS_OP_SUCCESS;
599 }
600
601 /* Test decoding a request frame (Note: must use decoder on server) */
602 H2_DECODER_ON_SERVER_TEST(h2_decoder_headers_request) {
603 (void)allocator;
604 struct fixture *fixture = ctx;
605
606 /* clang-format off */
607 uint8_t input[] = {
608 0x00, 0x00, 21, /* Length (24) */
609 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
610 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
611 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
612 /* HEADERS */
613 0x82, /* ":method: GET" - indexed */
614 0x86, /* ":scheme: http" - indexed */
615 0x41, 10, 'a', 'm', 'a', 'z', 'o', 'n', '.', 'c', 'o', 'm', /* ":authority: amazon.com" - indexed name */
616 0x84, /* ":path: /" - indexed */
617 0x7a, 0x04, 't', 'e', 's', 't' /* "user-agent: test" - indexed name, uncompressed value */
618 };
619 /* clang-format on */
620
621 /* Decode */
622 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
623
624 /* Validate */
625 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
626 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 1 /*stream_id*/));
627 ASSERT_FALSE(frame->headers_malformed);
628 ASSERT_UINT_EQUALS(5, aws_http_headers_count(frame->headers));
629 ASSERT_SUCCESS(s_check_header(frame, 0, ":method", "GET", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
630 ASSERT_SUCCESS(s_check_header(frame, 1, ":scheme", "http", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
631 ASSERT_SUCCESS(s_check_header(frame, 2, ":authority", "amazon.com", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
632 ASSERT_SUCCESS(s_check_header(frame, 3, ":path", "/", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
633 ASSERT_SUCCESS(s_check_header(frame, 4, "user-agent", "test", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
634 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
635 ASSERT_TRUE(frame->end_stream);
636 return AWS_OP_SUCCESS;
637 }
638
639 H2_DECODER_ON_SERVER_TEST(h2_decoder_headers_cookies) {
640 (void)allocator;
641 struct fixture *fixture = ctx;
642
643 /* clang-format off */
644 uint8_t input[] = {
645 /* HEADERS FRAME*/
646 0x00, 0x00, 0x06, /* Length (24) */
647 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
648 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
649 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
650 /* HEADERS */
651 0x82, /* ":method: GET" - indexed */
652 0x60, 0x03, 'a', '=', 'b', /* "cache: a=b" - indexed name, uncompressed value */
653
654 /* CONTINUATION FRAME*/
655 0x00, 0x00, 16, /* Length (24) */
656 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
657 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
658 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
659 /* PAYLOAD */
660 0x7a, 0x04, 't', 'e', 's', 't', /* "user-agent: test" - indexed name, uncompressed value */
661 0x60, 0x03, 'c', '=', 'd', /* "cache: c=d" - indexed name, uncompressed value */
662 0x60, 0x03, 'e', '=', 'f', /* "cache: e=f" - indexed name, uncompressed value */
663 };
664 /* clang-format on */
665
666 /* Decode */
667 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
668
669 /* Validate */
670 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
671 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
672 ASSERT_FALSE(frame->headers_malformed);
673 /* two sepaprate cookie headers are concatenated and moved as the last header*/
674 ASSERT_UINT_EQUALS(3, aws_http_headers_count(frame->headers));
675 ASSERT_SUCCESS(s_check_header(frame, 0, ":method", "GET", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
676 ASSERT_SUCCESS(s_check_header(frame, 1, "user-agent", "test", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
677 ASSERT_SUCCESS(s_check_header(frame, 2, "cookie", "a=b; c=d; e=f", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
678 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
679 ASSERT_TRUE(frame->end_stream);
680
681 return AWS_OP_SUCCESS;
682 }
683
684 /* A trailing header has no pseudo-headers, and always ends the stream */
685 H2_DECODER_ON_CLIENT_TEST(h2_decoder_headers_trailer) {
686 (void)allocator;
687 struct fixture *fixture = ctx;
688
689 /* clang-format off */
690 uint8_t input[] = {
691 0x00, 0x00, 0x06, /* Length (24) */
692 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
693 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
694 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
695 /* HEADERS */
696 0x7a, 0x04, 't', 'e', 's', 't' /* "user-agent: test" - indexed name, uncompressed value */
697 };
698 /* clang-format on */
699
700 /* Decode */
701 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
702
703 /* Validate */
704 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
705 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
706 ASSERT_FALSE(frame->headers_malformed);
707 ASSERT_UINT_EQUALS(1, aws_http_headers_count(frame->headers));
708 ASSERT_SUCCESS(s_check_header(frame, 0, "user-agent", "test", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
709 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_TRAILING, frame->header_block_type);
710 ASSERT_TRUE(frame->end_stream);
711 return AWS_OP_SUCCESS;
712 }
713
714 /* A trailing header can be empty */
715 H2_DECODER_ON_CLIENT_TEST(h2_decoder_headers_empty_trailer) {
716 (void)allocator;
717 struct fixture *fixture = ctx;
718
719 /* clang-format off */
720 uint8_t input[] = {
721 0x00, 0x00, 0x00, /* Length (24) */
722 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
723 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
724 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
725 /* HEADERS - none */
726 };
727 /* clang-format on */
728
729 /* Decode */
730 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
731
732 /* Validate */
733 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
734 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
735 ASSERT_FALSE(frame->headers_malformed);
736 ASSERT_UINT_EQUALS(0, aws_http_headers_count(frame->headers));
737 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_TRAILING, frame->header_block_type);
738 ASSERT_TRUE(frame->end_stream);
739 return AWS_OP_SUCCESS;
740 }
741
742 /* HEADERS must specify a valid stream-id */
743 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_headers_requires_stream_id) {
744 (void)allocator;
745 struct fixture *fixture = ctx;
746
747 /* clang-format off */
748 uint8_t input[] = {
749 0x00, 0x00, 0x05, /* Length (24) */
750 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
751 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
752 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
753 /* HEADERS */
754 0x48, 0x03, '3', '0', '2' /* ":status: 302" - indexed name, uncompressed value */
755 };
756 /* clang-format on */
757
758 /* Decode */
759 ASSERT_H2ERR_ERROR(
760 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
761
762 return AWS_OP_SUCCESS;
763 }
764
765 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_headers_payload_too_small_for_padding) {
766 (void)allocator;
767 struct fixture *fixture = ctx;
768
769 /* clang-format off */
770 uint8_t input[] = {
771 0x00, 0x00, 0x00, /* Length (24) */
772 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
773 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_PRIORITY | AWS_H2_FRAME_F_PADDED | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
774 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
775 /* HEADERS */
776 0x02, /* Pad Length (8) - F_PADDED */
777 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) - F_PRIORITY*/
778 0x09, /* Weight (8) - F_PRIORITY */
779 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
780 0x00, 0x00 /* Padding (*) - F_PADDED */
781 };
782 /* clang-format on */
783
784 /* Decode */
785 ASSERT_H2ERR_ERROR(
786 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
787
788 return AWS_OP_SUCCESS;
789 }
790
791 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_headers_payload_too_small_for_priority) {
792 (void)allocator;
793 struct fixture *fixture = ctx;
794
795 /* clang-format off */
796 uint8_t input[] = {
797 0x00, 0x00, 0x05, /* Length (24) */
798 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
799 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_PRIORITY | AWS_H2_FRAME_F_PADDED | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
800 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
801 /* HEADERS */
802 0x02, /* Pad Length (8) - F_PADDED */
803 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) - F_PRIORITY*/
804 0x09, /* Weight (8) - F_PRIORITY */
805 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
806 0x00, 0x00 /* Padding (*) - F_PADDED */
807 };
808 /* clang-format on */
809
810 /* Decode */
811 ASSERT_H2ERR_ERROR(
812 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
813
814 return AWS_OP_SUCCESS;
815 }
816
817 /* Message is malformed if a header-name is blank.
818 * A malformed message is a Stream Error, not a Connection Error, so the decoder should continue */
819 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_headers_blank_name) {
820 (void)allocator;
821 struct fixture *fixture = ctx;
822
823 /* clang-format off */
824 uint8_t input[] = {
825 0x00, 0x00, 0x09, /* Length (24) */
826 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
827 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
828 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
829 /* HEADERS */
830 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
831 0x40, 0x00, 0x01, 'a', /* ": a" - literal blank name, uncompressed value */
832 };
833 /* clang-format on */
834
835 /* Decode */
836 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
837
838 /* Validate */
839 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
840 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
841 ASSERT_TRUE(frame->headers_malformed);
842
843 ASSERT_TRUE(frame->end_stream);
844 return AWS_OP_SUCCESS;
845 }
846
847 /* Message is malformed if a header-name has illegal characters.
848 * A malformed message is a Stream Error, not a Connection Error, so the decoder should continue */
849 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_headers_illegal_name) {
850 (void)allocator;
851 struct fixture *fixture = ctx;
852
853 /* clang-format off */
854 uint8_t input[] = {
855 0x00, 0x00, 10, /* Length (24) */
856 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
857 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
858 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
859 /* HEADERS */
860 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
861 0x40, 0x01, ',', 0x01, 'a', /* ",: a" - literal name with illegal character */
862 };
863 /* clang-format on */
864
865 /* Decode */
866 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
867
868 /* Validate */
869 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
870 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
871 ASSERT_TRUE(frame->headers_malformed);
872
873 ASSERT_TRUE(frame->end_stream);
874 return AWS_OP_SUCCESS;
875 }
876
877 /* Message is malformed if server receives a response.
878 * A malformed message is a Stream Error, not a Connection Error, so the decoder should continue */
879 H2_DECODER_ON_SERVER_TEST(h2_decoder_malformed_headers_response_to_server) {
880 (void)allocator;
881 struct fixture *fixture = ctx;
882
883 /* clang-format off */
884 uint8_t input[] = {
885 0x00, 0x00, 0x05, /* Length (24) */
886 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
887 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
888 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
889 /* HEADERS */
890 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
891 };
892 /* clang-format on */
893
894 /* Decode */
895 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
896
897 /* Validate */
898 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
899 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
900 ASSERT_TRUE(frame->headers_malformed);
901
902 ASSERT_TRUE(frame->end_stream);
903 return AWS_OP_SUCCESS;
904 }
905
906 /* Message is malformed if client cannot receive requests in HEADERS
907 * (though they can get requests in PUSH_PROMISE frames).
908 * A malformed message is a Stream Error, not a Connection Error, so the decoder should continue */
909 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_headers_request_to_client) {
910 (void)allocator;
911 struct fixture *fixture = ctx;
912
913 /* clang-format off */
914 uint8_t input[] = {
915 0x00, 0x00, 0x03, /* Length (24) */
916 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
917 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
918 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
919 /* HEADERS */
920 0x82, /* ":method: GET" - indexed */
921 0x86, /* ":scheme: http" - indexed */
922 0x84, /* ":path: /" - indexed */
923 };
924 /* clang-format on */
925
926 /* Decode */
927 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
928
929 /* Validate */
930 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
931 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 1 /*stream_id*/));
932 ASSERT_TRUE(frame->headers_malformed);
933
934 ASSERT_TRUE(frame->end_stream);
935 return AWS_OP_SUCCESS;
936 }
937
938 /* Message is malformed if it contains both request and response pseudo-headers.
939 * A malformed message is a Stream Error, not a Connection Error, so the decoder should continue */
940 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_headers_mixed_pseudoheaders) {
941 (void)allocator;
942 struct fixture *fixture = ctx;
943
944 /* clang-format off */
945 uint8_t input[] = {
946 0x00, 0x00, 0x06, /* Length (24) */
947 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
948 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
949 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
950 /* HEADERS */
951 0x82, /* ":method: GET" - REQUEST PSEUDO-HEADER */
952 0x48, 0x03, '3', '0', '2', /* ":status: 302" - RESPONSE PSEUDO-HEADER */
953 };
954 /* clang-format on */
955
956 /* Decode */
957 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
958
959 /* Validate */
960 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
961 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 1 /*stream_id*/));
962 ASSERT_TRUE(frame->headers_malformed);
963
964 ASSERT_TRUE(frame->end_stream);
965 return AWS_OP_SUCCESS;
966 }
967
968 /* Message is malformed if pseudo-headers come after regular headers.
969 * A malformed message is a Stream Error, not a Connection Error, so the decoder should continue */
970 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_headers_late_pseudoheaders) {
971 (void)allocator;
972 struct fixture *fixture = ctx;
973
974 /* clang-format off */
975 uint8_t input[] = {
976 0x00, 0x00, 11, /* Length (24) */
977 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
978 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
979 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
980 /* HEADERS */
981 0x7a, 0x04, 't', 'e', 's', 't', /* "user-agent: test" - REGULAR HEADER */
982 0x48, 0x03, '3', '0', '2', /* ":status: 302" - PSEUDO-HEADER after regular header*/
983 };
984 /* clang-format on */
985
986 /* Decode */
987 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
988
989 /* Validate */
990 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
991 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 1 /*stream_id*/));
992 ASSERT_TRUE(frame->headers_malformed);
993
994 ASSERT_TRUE(frame->end_stream);
995 return AWS_OP_SUCCESS;
996 }
997
998 /* Message is malformed if trailing header does not end stream. */
999 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_headers_trailer_must_end_stream) {
1000 (void)allocator;
1001 struct fixture *fixture = ctx;
1002
1003 /* clang-format off */
1004 uint8_t input[] = {
1005 0x00, 0x00, 0x00, /* Length (24) */
1006 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1007 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1008 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1009 /* HEADERS - blank*/
1010 };
1011 /* clang-format on */
1012
1013 /* Decode */
1014 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1015
1016 /* Validate */
1017 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1018 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
1019 ASSERT_TRUE(frame->headers_malformed);
1020 ASSERT_FALSE(frame->end_stream);
1021 return AWS_OP_SUCCESS;
1022 }
1023
1024 /* Even if a header-block is malformed, we still process its fields, which may mutate the hpack tables. */
1025 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_header_continues_hpack_parsing) {
1026 (void)allocator;
1027 struct fixture *fixture = ctx;
1028
1029 /* clang-format off */
1030 uint8_t input[] = {
1031 /* FRAME 1 - malformed HEADERS */
1032 0x00, 0x00, 15, /* Length (24) */
1033 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1034 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1035 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
1036 /* HEADERS */
1037 0x48, 0x03, '3', '0', '2', /* ":status: 302" - stored to dynamic table */
1038 0x40, 0x01, ',', 0x01, 'a', /* ",: a" - INVALID character - stored to dynamic table */
1039 0x40, 0x01, 'b', 0x01, 'c', /* "b: c" - stored to dynamic table */
1040
1041 /* So at this point dynamic table should look like:
1042 * INDEX NAME VALUE
1043 * 62 b c
1044 * 63 , a
1045 * 64 :status 302
1046 */
1047
1048 /* FRAME 2 - valid HEADERS referencing entry from malformed HEADERS */
1049 0x00, 0x00, 2, /* Length (24) */
1050 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1051 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1052 0x00, 0x00, 0x00, 0x03, /* Reserved (1) | Stream Identifier (31) */
1053 /* HEADERS */
1054 0xc0, /* ":status: 302" - indexed from dynamic table */
1055 0xbe, /* "b: c" - indexed from dynamic table */
1056 };
1057 /* clang-format on */
1058
1059 /* Decode */
1060 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1061
1062 /* Validate */
1063 ASSERT_UINT_EQUALS(2, h2_decode_tester_frame_count(&fixture->decode));
1064
1065 /* frame 1 should be malformed */
1066 struct h2_decoded_frame *frame = h2_decode_tester_get_frame(&fixture->decode, 0);
1067 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 1 /*stream_id*/));
1068 ASSERT_TRUE(frame->headers_malformed);
1069
1070 /* frame 2 should be able to index fields stored by previous malformed frame */
1071 frame = h2_decode_tester_get_frame(&fixture->decode, 1);
1072 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 3 /*stream_id*/));
1073 ASSERT_FALSE(frame->headers_malformed);
1074 ASSERT_UINT_EQUALS(2, aws_http_headers_count(frame->headers));
1075 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1076 ASSERT_SUCCESS(s_check_header(frame, 1, "b", "c", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1077 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
1078
1079 return AWS_OP_SUCCESS;
1080 }
1081
1082 /* Test CONTINUATION frame.
1083 * Decoder requires that a HEADERS or PUSH_PROMISE frame be sent first */
1084 H2_DECODER_ON_CLIENT_TEST(h2_decoder_continuation) {
1085 (void)allocator;
1086 struct fixture *fixture = ctx;
1087
1088 /* clang-format off */
1089 uint8_t input[] = {
1090 /* HEADERS FRAME*/
1091 0x00, 0x00, 0x05, /* Length (24) */
1092 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1093 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
1094 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1095 /* PAYLOAD */
1096 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
1097
1098 /* CONTINUATION FRAME*/
1099 0x00, 0x00, 0x09, /* Length (24) */
1100 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
1101 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1102 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1103 /* PAYLOAD */
1104 0x58, 0x07, 'p', 'r', 'i', 'v', 'a', 't', 'e', /* "cache-control: private" */
1105 };
1106 /* clang-format on */
1107
1108 /* Decode */
1109 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1110
1111 /* Validate */
1112 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1113 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
1114 ASSERT_FALSE(frame->headers_malformed);
1115 ASSERT_UINT_EQUALS(2, aws_http_headers_count(frame->headers));
1116 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1117 ASSERT_SUCCESS(s_check_header(frame, 1, "cache-control", "private", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1118 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
1119 ASSERT_TRUE(frame->end_stream);
1120 return AWS_OP_SUCCESS;
1121 }
1122
1123 /* Try setting ALL the flags on CONTINUATION frame.
1124 * Only END_HEADERS and should trigger.
1125 * Continuation doesn't support PRIORITY and PADDING like HEADERS does, so they should just be ignored */
1126 H2_DECODER_ON_CLIENT_TEST(h2_decoder_continuation_ignores_unknown_flags) {
1127 (void)allocator;
1128 struct fixture *fixture = ctx;
1129
1130 /* clang-format off */
1131 uint8_t input[] = {
1132 /* HEADERS FRAME*/
1133 0x00, 0x00, 0x05, /* Length (24) */
1134 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1135 0x0, /* Flags (8) */
1136 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1137 /* PAYLOAD */
1138 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
1139
1140 /* CONTINUATION FRAME*/
1141 0x00, 0x00, 0x09, /* Length (24) */
1142 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
1143 0xFF, /* Flags (8) */
1144 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1145 /* PAYLOAD */
1146 0x58, 0x07, 'p', 'r', 'i', 'v', 'a', 't', 'e', /* "cache-control: private" */
1147 };
1148 /* clang-format on */
1149
1150 /* Decode */
1151 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1152
1153 /* Validate */
1154 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1155 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
1156 ASSERT_FALSE(frame->headers_malformed);
1157 ASSERT_UINT_EQUALS(2, aws_http_headers_count(frame->headers));
1158 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1159 ASSERT_SUCCESS(s_check_header(frame, 1, "cache-control", "private", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1160 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
1161 return AWS_OP_SUCCESS;
1162 }
1163
1164 /* Test that we an handle a header-field whose encoding is spread across multiple frames.
1165 * Throw some padding in to make it extra complicated */
1166 H2_DECODER_ON_CLIENT_TEST(h2_decoder_continuation_header_field_spans_frames) {
1167 (void)allocator;
1168 struct fixture *fixture = ctx;
1169
1170 /* clang-format off */
1171 uint8_t input[] = {
1172 /* HEADERS FRAME*/
1173 0x00, 0x00, 0x06, /* Length (24) */
1174 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1175 AWS_H2_FRAME_F_PADDED, /* Flags (8) */
1176 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1177 /* PAYLOAD */
1178 0x02, /* Pad Length (8) - F_PADDED */
1179 0x48, 0x03, '3', /* ":status: 302" - beginning 3/5 bytes encoded in this frame. */
1180 0x00, 0x00, /* Padding (*) - F_PADDED */
1181
1182 /* CONTINUATION FRAME*/
1183 0x00, 0x00, 0x02, /* Length (24) */
1184 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
1185 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1186 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1187 /* PAYLOAD */
1188 '0', '2', /* :status: 302" - last 2/5 bytes encoded in this frame*/
1189 };
1190 /* clang-format on */
1191
1192 /* Decode */
1193 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1194
1195 /* Validate */
1196 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1197 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
1198 ASSERT_FALSE(frame->headers_malformed);
1199 ASSERT_UINT_EQUALS(1, aws_http_headers_count(frame->headers));
1200 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1201 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
1202 ASSERT_FALSE(frame->end_stream);
1203 return AWS_OP_SUCCESS;
1204 }
1205
1206 /* Test having multiple CONTINUATION frames in a row */
1207 H2_DECODER_ON_CLIENT_TEST(h2_decoder_continuation_many_frames) {
1208 (void)allocator;
1209 struct fixture *fixture = ctx;
1210
1211 /* clang-format off */
1212 uint8_t input[] = {
1213 /* HEADERS FRAME*/
1214 0x00, 0x00, 0x05, /* Length (24) */
1215 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1216 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
1217 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1218 /* PAYLOAD */
1219 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
1220
1221 /* CONTINUATION FRAME*/
1222 0x00, 0x00, 0x09, /* Length (24) */
1223 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
1224 0x0, /* Flags (8) */
1225 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1226 /* PAYLOAD */
1227 0x58, 0x07, 'p', 'r', 'i', 'v', 'a', 't', 'e', /* "cache-control: private" */
1228
1229 /* CONTINUATION FRAME*/
1230 0x00, 0x00, 0x08, /* Length (24) */
1231 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
1232 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1233 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1234 /* PAYLOAD */
1235 0x40, 0x02, 'h', 'i', 0x03, 'm', 'o', 'm', /* "hi: mom" */
1236 };
1237 /* clang-format on */
1238
1239 /* Decode */
1240 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1241
1242 /* Validate */
1243 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1244 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
1245 ASSERT_FALSE(frame->headers_malformed);
1246 ASSERT_UINT_EQUALS(3, aws_http_headers_count(frame->headers));
1247 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1248 ASSERT_SUCCESS(s_check_header(frame, 1, "cache-control", "private", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1249 ASSERT_SUCCESS(s_check_header(frame, 2, "hi", "mom", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1250 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
1251 ASSERT_TRUE(frame->end_stream);
1252 return AWS_OP_SUCCESS;
1253 }
1254
1255 /* Test having HEADERS and CONTINUATION frames with empty header-block-fragments */
1256 H2_DECODER_ON_CLIENT_TEST(h2_decoder_continuation_empty_payloads) {
1257 (void)allocator;
1258 struct fixture *fixture = ctx;
1259
1260 /* clang-format off */
1261 uint8_t input[] = {
1262 /* HEADERS FRAME*/
1263 0x00, 0x00, 0x00, /* Length (24) */
1264 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1265 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
1266 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1267 /* PAYLOAD */
1268
1269 /* CONTINUATION FRAME*/
1270 0x00, 0x00, 0x00, /* Length (24) */
1271 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
1272 0x0, /* Flags (8) */
1273 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1274 /* PAYLOAD */
1275
1276 /* CONTINUATION FRAME*/
1277 0x00, 0x00, 0x05, /* Length (24) */
1278 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
1279 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1280 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1281 /* PAYLOAD */
1282 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
1283 };
1284 /* clang-format on */
1285
1286 /* Decode */
1287 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1288
1289 /* Validate */
1290 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1291 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_HEADERS, 0x76543210 /*stream_id*/));
1292 ASSERT_FALSE(frame->headers_malformed);
1293 ASSERT_UINT_EQUALS(1, aws_http_headers_count(frame->headers));
1294 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1295 ASSERT_INT_EQUALS(AWS_HTTP_HEADER_BLOCK_MAIN, frame->header_block_type);
1296 ASSERT_TRUE(frame->end_stream);
1297 return AWS_OP_SUCCESS;
1298 }
1299
1300 /* Once a header-block starts, it's illegal for any frame but a CONTINUATION on that same stream to arrive.
1301 * This test sends a different frame type next */
1302 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_continuation_frame_expected) {
1303 (void)allocator;
1304 struct fixture *fixture = ctx;
1305
1306 /* clang-format off */
1307 uint8_t input[] = {
1308 /* HEADERS FRAME*/
1309 0x00, 0x00, 0x05, /* Length (24) */
1310 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1311 0x0, /* Flags (8) */
1312 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1313 /* PAYLOAD */
1314 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
1315
1316 /* HEADERS FRAME*/
1317 0x00, 0x00, 0x05, /* Length (24) */
1318 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1319 0x0, /* Flags (8) */
1320 0x76, 0x54, 0x32, 0x12, /* Reserved (1) | Stream Identifier (31) */
1321 /* PAYLOAD */
1322 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
1323 };
1324 /* clang-format on */
1325
1326 /* Decode */
1327 ASSERT_H2ERR_ERROR(
1328 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1329
1330 return AWS_OP_SUCCESS;
1331 }
1332
1333 /* Once a header-block starts, it's illegal for any frame but a CONTINUATION on that same stream to arrive.
1334 * This test sends a different stream-id next */
1335 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_continuation_frame_same_stream_expected) {
1336 (void)allocator;
1337 struct fixture *fixture = ctx;
1338
1339 /* clang-format off */
1340 uint8_t input[] = {
1341 /* HEADERS FRAME*/
1342 0x00, 0x00, 0x05, /* Length (24) */
1343 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1344 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
1345 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1346 /* PAYLOAD */
1347 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
1348
1349 /* CONTINUATION FRAME*/
1350 0x00, 0x00, 0x09, /* Length (24) */
1351 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
1352 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1353 0x76, 0x54, 0x32, 0x12, /* Reserved (1) | Stream Identifier (31) */
1354 /* PAYLOAD */
1355 0x58, 0x07, 'p', 'r', 'i', 'v', 'a', 't', 'e', /* "cache-control: private" */
1356 };
1357 /* clang-format on */
1358
1359 /* Decode */
1360 ASSERT_H2ERR_ERROR(
1361 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1362
1363 return AWS_OP_SUCCESS;
1364 }
1365
1366 /* It's an error for a header-block to end with a partially decoded header-field */
1367 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_partial_header) {
1368 (void)allocator;
1369 struct fixture *fixture = ctx;
1370
1371 /* clang-format off */
1372 uint8_t input[] = {
1373 /* HEADERS FRAME*/
1374 0x00, 0x00, 0x03, /* Length (24) */
1375 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1376 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1377 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1378 /* PAYLOAD */
1379 0x48, 0x03, '3', /* ":status: 302" - Note that final 2 characters are not encoded */
1380 };
1381 /* clang-format on */
1382
1383 /* Decode */
1384 ASSERT_H2ERR_ERROR(
1385 AWS_HTTP2_ERR_COMPRESSION_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1386
1387 return AWS_OP_SUCCESS;
1388 }
1389
1390 /* Ensure that random HPACK decoding errors are reported as ERROR_COMPRESSION */
1391 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_bad_hpack_data) {
1392 (void)allocator;
1393 struct fixture *fixture = ctx;
1394
1395 /* clang-format off */
1396 uint8_t input[] = {
1397 /* HEADERS FRAME*/
1398 0x00, 0x00, 32, /* Length (24) */
1399 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
1400 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1401 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1402 /* PAYLOAD */
1403 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* indexed header field, with index bigger than 64bits */
1404 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1405 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1406 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1407 };
1408 /* clang-format on */
1409
1410 /* Decode */
1411 ASSERT_H2ERR_ERROR(
1412 AWS_HTTP2_ERR_COMPRESSION_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1413
1414 return AWS_OP_SUCCESS;
1415 }
1416
1417 /* Test PRIORITY frame */
1418 H2_DECODER_ON_CLIENT_TEST(h2_decoder_priority) {
1419 (void)allocator;
1420 struct fixture *fixture = ctx;
1421
1422 /* clang-format off */
1423 uint8_t input[] = {
1424 0x00, 0x00, 0x05, /* Length (24) */
1425 AWS_H2_FRAME_T_PRIORITY, /* Type (8) */
1426 0x00, /* Flags (8) */
1427 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1428 /* PRIORITY */
1429 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) */
1430 0x09, /* Weight (8) */
1431 };
1432 /* clang-format on */
1433
1434 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1435
1436 /* Our implementation currently chooses to ignore PRIORITY frames, so no callbacks should have fired */
1437 ASSERT_UINT_EQUALS(0, h2_decode_tester_frame_count(&fixture->decode));
1438 return AWS_OP_SUCCESS;
1439 }
1440
1441 /* Unknown flags should be ignored. PRIORITY frames don't have any flags. */
1442 H2_DECODER_ON_CLIENT_TEST(h2_decoder_priority_ignores_unknown_flags) {
1443 (void)allocator;
1444 struct fixture *fixture = ctx;
1445
1446 /* clang-format off */
1447 uint8_t input[] = {
1448 0x00, 0x00, 0x05, /* Length (24) */
1449 AWS_H2_FRAME_T_PRIORITY, /* Type (8) */
1450 0xFF, /* Flags (8) */
1451 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1452 /* PRIORITY */
1453 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) */
1454 0x09, /* Weight (8) */
1455 };
1456 /* clang-format on */
1457
1458 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1459
1460 /* Our implementation currently chooses to ignore PRIORITY frames, so no callbacks should have fired */
1461 ASSERT_UINT_EQUALS(0, h2_decode_tester_frame_count(&fixture->decode));
1462 return AWS_OP_SUCCESS;
1463 }
1464
1465 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_priority_requires_stream_id) {
1466 (void)allocator;
1467 struct fixture *fixture = ctx;
1468
1469 /* clang-format off */
1470 uint8_t input[] = {
1471 0x00, 0x00, 0x05, /* Length (24) */
1472 AWS_H2_FRAME_T_PRIORITY, /* Type (8) */
1473 0x00, /* Flags (8) */
1474 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1475 /* PRIORITY */
1476 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) */
1477 0x09, /* Weight (8) */
1478 };
1479 /* clang-format on */
1480
1481 ASSERT_H2ERR_ERROR(
1482 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1483
1484 return AWS_OP_SUCCESS;
1485 }
1486
1487 /* Test PRIORITY frame */
1488 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_priority_payload_too_small) {
1489 (void)allocator;
1490 struct fixture *fixture = ctx;
1491
1492 /* clang-format off */
1493 uint8_t input[] = {
1494 0x00, 0x00, 0x04, /* Length (24) */
1495 AWS_H2_FRAME_T_PRIORITY, /* Type (8) */
1496 0x00, /* Flags (8) */
1497 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1498 /* PRIORITY */
1499 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) */
1500 /* Weight (8) */
1501 };
1502 /* clang-format on */
1503
1504 ASSERT_H2ERR_ERROR(
1505 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1506
1507 return AWS_OP_SUCCESS;
1508 }
1509
1510 /* Test PRIORITY frame */
1511 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_priority_payload_too_large) {
1512 (void)allocator;
1513 struct fixture *fixture = ctx;
1514
1515 /* clang-format off */
1516 uint8_t input[] = {
1517 0x00, 0x00, 0x06, /* Length (24) */
1518 AWS_H2_FRAME_T_PRIORITY, /* Type (8) */
1519 0x00, /* Flags (8) */
1520 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1521 /* PRIORITY */
1522 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) */
1523 0x09, /* Weight (8) */
1524 0x00, /* TOO MUCH PAYLOAD*/
1525 };
1526 /* clang-format on */
1527
1528 ASSERT_H2ERR_ERROR(
1529 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1530
1531 return AWS_OP_SUCCESS;
1532 }
1533
1534 /* Test RST_STREAM frame */
1535 H2_DECODER_ON_CLIENT_TEST(h2_decoder_rst_stream) {
1536 (void)allocator;
1537 struct fixture *fixture = ctx;
1538
1539 /* clang-format off */
1540 uint8_t input[] = {
1541 0x00, 0x00, 0x04, /* Length (24) */
1542 AWS_H2_FRAME_T_RST_STREAM, /* Type (8) */
1543 0x00, /* Flags (8) */
1544 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1545 /* RST_STREAM */
1546 0xFF, 0xEE, 0xDD, 0xCC, /* Error Code (32) */
1547 };
1548 /* clang-format on */
1549
1550 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1551
1552 /* Validate */
1553 ASSERT_UINT_EQUALS(1, h2_decode_tester_frame_count(&fixture->decode));
1554 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1555 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_RST_STREAM, 0x76543210 /*stream_id*/));
1556 ASSERT_UINT_EQUALS(0xFFEEDDCC, frame->error_code);
1557 return AWS_OP_SUCCESS;
1558 }
1559
1560 /* Unknown flags should be ignored. RST_STREAM frame doesn't support any flags */
1561 H2_DECODER_ON_CLIENT_TEST(h2_decoder_rst_stream_ignores_unknown_flags) {
1562 (void)allocator;
1563 struct fixture *fixture = ctx;
1564
1565 /* clang-format off */
1566 uint8_t input[] = {
1567 0x00, 0x00, 0x04, /* Length (24) */
1568 AWS_H2_FRAME_T_RST_STREAM, /* Type (8) */
1569 0xFF, /* Flags (8) */
1570 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1571 /* RST_STREAM */
1572 0xFF, 0xEE, 0xDD, 0xCC, /* Error Code (32) */
1573 };
1574 /* clang-format on */
1575
1576 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1577
1578 /* Validate */
1579 ASSERT_UINT_EQUALS(1, h2_decode_tester_frame_count(&fixture->decode));
1580 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1581 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_RST_STREAM, 0x76543210 /*stream_id*/));
1582 ASSERT_UINT_EQUALS(0xFFEEDDCC, frame->error_code);
1583 return AWS_OP_SUCCESS;
1584 }
1585
1586 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_rst_stream_requires_stream_id) {
1587 (void)allocator;
1588 struct fixture *fixture = ctx;
1589
1590 /* clang-format off */
1591 uint8_t input[] = {
1592 0x00, 0x00, 0x04, /* Length (24) */
1593 AWS_H2_FRAME_T_RST_STREAM, /* Type (8) */
1594 0x00, /* Flags (8) */
1595 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1596 /* RST_STREAM */
1597 0xFF, 0xEE, 0xDD, 0xCC, /* Error Code (32) */
1598 };
1599 /* clang-format on */
1600
1601 ASSERT_H2ERR_ERROR(
1602 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1603
1604 return AWS_OP_SUCCESS;
1605 }
1606
1607 /* Payload must be 4 bytes exactly */
1608 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_rst_stream_payload_too_small) {
1609 (void)allocator;
1610 struct fixture *fixture = ctx;
1611
1612 /* clang-format off */
1613 uint8_t input[] = {
1614 0x00, 0x00, 0x03, /* Length (24) */
1615 AWS_H2_FRAME_T_RST_STREAM, /* Type (8) */
1616 0x00, /* Flags (8) */
1617 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1618 /* RST_STREAM */
1619 0xFF, 0xEE, 0xDD, /* Error Code (32) <-- missing one byte */
1620 };
1621 /* clang-format on */
1622
1623 ASSERT_H2ERR_ERROR(
1624 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1625
1626 return AWS_OP_SUCCESS;
1627 }
1628
1629 /* Payload must be 4 bytes exactly */
1630 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_rst_stream_payload_too_large) {
1631 (void)allocator;
1632 struct fixture *fixture = ctx;
1633
1634 /* clang-format off */
1635 uint8_t input[] = {
1636 0x00, 0x00, 0x05, /* Length (24) */
1637 AWS_H2_FRAME_T_RST_STREAM, /* Type (8) */
1638 0x00, /* Flags (8) */
1639 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
1640 /* RST_STREAM */
1641 0xFF, 0xEE, 0xDD, 0xCC, /* Error Code (32) */
1642 0x00, /* TOO MUCH PAYLOAD */
1643 };
1644 /* clang-format on */
1645
1646 ASSERT_H2ERR_ERROR(
1647 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1648
1649 return AWS_OP_SUCCESS;
1650 }
1651
1652 /* Test SETTINGS frame */
1653 H2_DECODER_ON_CLIENT_TEST(h2_decoder_settings) {
1654 (void)allocator;
1655 struct fixture *fixture = ctx;
1656
1657 /* clang-format off */
1658 uint8_t input[] = {
1659 0x00, 0x00, 12, /* Length (24) */
1660 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1661 0x00, /* Flags (8) */
1662 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1663 /* SETTINGS */
1664 0x00, 0x05, /* Identifier (16) */
1665 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) */
1666 0x00, 0x02, /* Identifier (16) */
1667 0x00, 0x00, 0x00, 0x01, /* Value (32) */
1668 };
1669 /* clang-format on */
1670
1671 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1672
1673 /* Validate. */
1674 ASSERT_UINT_EQUALS(1, h2_decode_tester_frame_count(&fixture->decode));
1675 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1676 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_SETTINGS, 0 /*stream_id*/));
1677 ASSERT_FALSE(frame->ack);
1678 ASSERT_UINT_EQUALS(2, aws_array_list_length(&frame->settings));
1679
1680 struct aws_http2_setting setting;
1681 aws_array_list_get_at(&frame->settings, &setting, 0);
1682 ASSERT_UINT_EQUALS(0x0005, setting.id);
1683 ASSERT_UINT_EQUALS(0x00FFFFFF, setting.value);
1684
1685 aws_array_list_get_at(&frame->settings, &setting, 1);
1686 ASSERT_UINT_EQUALS(0x0002, setting.id);
1687 ASSERT_UINT_EQUALS(0x00000001, setting.value);
1688
1689 return AWS_OP_SUCCESS;
1690 }
1691
1692 /* Test SETTINGS frame */
1693 H2_DECODER_ON_CLIENT_TEST(h2_decoder_settings_empty) {
1694 (void)allocator;
1695 struct fixture *fixture = ctx;
1696
1697 /* clang-format off */
1698 uint8_t input[] = {
1699 0x00, 0x00, 0x00, /* Length (24) */
1700 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1701 0x00, /* Flags (8) */
1702 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1703 /* SETTINGS */
1704 };
1705 /* clang-format on */
1706
1707 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1708
1709 /* Validate. */
1710 ASSERT_UINT_EQUALS(1, h2_decode_tester_frame_count(&fixture->decode));
1711 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1712 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_SETTINGS, 0 /*stream_id*/));
1713 ASSERT_FALSE(frame->ack);
1714 ASSERT_UINT_EQUALS(0, aws_array_list_length(&frame->settings));
1715
1716 return AWS_OP_SUCCESS;
1717 }
1718
1719 /* SETTINGS frame with ACK flag set */
1720 H2_DECODER_ON_CLIENT_TEST(h2_decoder_settings_ack) {
1721 (void)allocator;
1722 struct fixture *fixture = ctx;
1723
1724 /* clang-format off */
1725 uint8_t input[] = {
1726 0x00, 0x00, 0x00, /* Length (24) */
1727 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1728 AWS_H2_FRAME_F_ACK, /* Flags (8) */
1729 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1730 /* SETTINGS */
1731 };
1732 /* clang-format on */
1733
1734 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1735
1736 /* Validate. */
1737 ASSERT_UINT_EQUALS(1, h2_decode_tester_frame_count(&fixture->decode));
1738 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1739 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_SETTINGS, 0 /*stream_id*/));
1740 ASSERT_TRUE(frame->ack);
1741 ASSERT_UINT_EQUALS(0, aws_array_list_length(&frame->settings));
1742
1743 return AWS_OP_SUCCESS;
1744 }
1745
1746 /* Decoder must ignore settings with unknown IDs */
1747 H2_DECODER_ON_CLIENT_TEST(h2_decoder_settings_ignores_unknown_ids) {
1748 (void)allocator;
1749 struct fixture *fixture = ctx;
1750
1751 /* clang-format off */
1752 uint8_t input[] = {
1753 0x00, 0x00, 18, /* Length (24) */
1754 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1755 0x00, /* Flags (8) */
1756 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1757 /* SETTINGS */
1758 0x00, 0x00, /* Identifier (16) <-- SHOULD IGNORE. 0 is invalid ID */
1759 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) */
1760 0x00, 0x01, /* Identifier (16) <-- This is OK */
1761 0x00, 0x00, 0x00, 0x01, /* Value (32) */
1762 0x00, AWS_HTTP2_SETTINGS_END_RANGE, /* Identifier (16) <-- SHOULD IGNORE */
1763 0x00, 0x00, 0x00, 0x01, /* Value (32) */
1764 };
1765 /* clang-format on */
1766
1767 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1768
1769 /* Validate. */
1770 ASSERT_UINT_EQUALS(1, h2_decode_tester_frame_count(&fixture->decode));
1771 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1772 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_SETTINGS, 0 /*stream_id*/));
1773 ASSERT_FALSE(frame->ack);
1774 ASSERT_UINT_EQUALS(1, aws_array_list_length(&frame->settings));
1775
1776 struct aws_http2_setting setting;
1777 aws_array_list_get_at(&frame->settings, &setting, 0);
1778 ASSERT_UINT_EQUALS(0x0001, setting.id);
1779 ASSERT_UINT_EQUALS(0x00000001, setting.value);
1780
1781 return AWS_OP_SUCCESS;
1782 }
1783
1784 /* Unexpected flags should be ignored.
1785 * SETTINGS frames only support ACK */
1786 H2_DECODER_ON_CLIENT_TEST(h2_decoder_settings_ignores_unknown_flags) {
1787 (void)allocator;
1788 struct fixture *fixture = ctx;
1789
1790 /* clang-format off */
1791 uint8_t input[] = {
1792 0x00, 0x00, 0x00, /* Length (24) */
1793 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1794 0xFF, /* Flags (8) */
1795 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1796 /* SETTINGS */
1797 };
1798 /* clang-format on */
1799
1800 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1801
1802 /* Validate. */
1803 ASSERT_UINT_EQUALS(1, h2_decode_tester_frame_count(&fixture->decode));
1804 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1805 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_SETTINGS, 0 /*stream_id*/));
1806 ASSERT_TRUE(frame->ack);
1807 ASSERT_UINT_EQUALS(0, aws_array_list_length(&frame->settings));
1808
1809 return AWS_OP_SUCCESS;
1810 }
1811
1812 /* Error if SETTINGS ACK frame has any individual settings in it */
1813 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_settings_ack_with_data) {
1814 (void)allocator;
1815 struct fixture *fixture = ctx;
1816
1817 /* clang-format off */
1818 uint8_t input[] = {
1819 0x00, 0x00, 0x06, /* Length (24) */
1820 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1821 AWS_H2_FRAME_F_ACK, /* Flags (8) */
1822 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1823 /* SETTINGS */
1824 0x00, 0x05, /* Identifier (16) */
1825 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) */
1826 };
1827 /* clang-format on */
1828
1829 ASSERT_H2ERR_ERROR(
1830 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1831
1832 return AWS_OP_SUCCESS;
1833 }
1834
1835 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_settings_forbids_stream_id) {
1836 (void)allocator;
1837 struct fixture *fixture = ctx;
1838
1839 /* clang-format off */
1840 uint8_t input[] = {
1841 0x00, 0x00, 12, /* Length (24) */
1842 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1843 0x00, /* Flags (8) */
1844 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
1845 /* SETTINGS */
1846 0x00, 0x05, /* Identifier (16) */
1847 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) */
1848 0x00, 0x02, /* Identifier (16) */
1849 0x00, 0x00, 0x00, 0x01, /* Value (32) */
1850 };
1851 /* clang-format on */
1852
1853 ASSERT_H2ERR_ERROR(
1854 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1855
1856 return AWS_OP_SUCCESS;
1857 }
1858
1859 /* Error if SETTINGS payload is not a multiple of 6 */
1860 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_settings_payload_size) {
1861 (void)allocator;
1862 struct fixture *fixture = ctx;
1863
1864 /* clang-format off */
1865 uint8_t input[] = {
1866 0x00, 0x00, 0x08, /* Length (24) */
1867 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1868 0x00, /* Flags (8) */
1869 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1870 /* SETTINGS */
1871 0x00, 0x05, /* Identifier (16) */
1872 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) */
1873 0x00, 0x02, /* Identifier (16) */
1874 /* Value (32) <-- MISSING */
1875 };
1876 /* clang-format on */
1877
1878 ASSERT_H2ERR_ERROR(
1879 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1880
1881 return AWS_OP_SUCCESS;
1882 }
1883
1884 /* Error if SETTINGS has invalid values */
1885 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_settings_invalid_values_enable_push) {
1886 (void)allocator;
1887 struct fixture *fixture = ctx;
1888
1889 /* clang-format off */
1890 uint8_t input[] = {
1891 0x00, 0x00, 12, /* Length (24) */
1892 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1893 0x00, /* Flags (8) */
1894 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1895 /* SETTINGS */
1896 0x00, 0x05, /* Identifier (16) */
1897 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) */
1898 0x00, 0x02, /* Identifier (16) */
1899 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) <-- INVALID value FOR ENABLE_PUSH */
1900 };
1901 /* clang-format on */
1902
1903 ASSERT_H2ERR_ERROR(
1904 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1905
1906 return AWS_OP_SUCCESS;
1907 }
1908
1909 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_settings_invalid_values_initial_window_size) {
1910 (void)allocator;
1911 struct fixture *fixture = ctx;
1912
1913 /* clang-format off */
1914 uint8_t input[] = {
1915 0x00, 0x00, 12, /* Length (24) */
1916 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1917 0x00, /* Flags (8) */
1918 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1919 /* SETTINGS */
1920 0x00, 0x05, /* Identifier (16) */
1921 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) */
1922 0x00, 0x04, /* Identifier (16) */
1923 0x80, 0xFF, 0xFF, 0xFF, /* Value (32) <-- INVALID value FOR INITIAL_WINDOW_SIZE */
1924 };
1925 /* clang-format on */
1926
1927 ASSERT_H2ERR_ERROR(
1928 AWS_HTTP2_ERR_FLOW_CONTROL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1929
1930 return AWS_OP_SUCCESS;
1931 }
1932
1933 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_settings_invalid_values_max_frame_size) {
1934 (void)allocator;
1935 struct fixture *fixture = ctx;
1936
1937 /* clang-format off */
1938 uint8_t input[] = {
1939 0x00, 0x00, 12, /* Length (24) */
1940 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
1941 0x00, /* Flags (8) */
1942 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
1943 /* SETTINGS */
1944 0x00, 0x05, /* Identifier (16) */
1945 0x00, 0xFF, 0xFF, 0xFF, /* Value (32) */
1946 0x00, 0x05, /* Identifier (16) */
1947 0x00, 0x00, 0x00, 0x00, /* Value (32) <-- INVALID value FOR MAX_FRAME_SIZE */
1948 };
1949 /* clang-format on */
1950
1951 ASSERT_H2ERR_ERROR(
1952 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1953
1954 return AWS_OP_SUCCESS;
1955 }
1956
1957 H2_DECODER_ON_CLIENT_TEST(h2_decoder_push_promise) {
1958 (void)allocator;
1959 struct fixture *fixture = ctx;
1960
1961 /* clang-format off */
1962 uint8_t input[] = {
1963 0x00, 0x00, 0x07, /* Length (24) */
1964 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
1965 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
1966 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
1967 /* PUSH_PROMISE */
1968 0x80, 0x00, 0x00, 0x02, /* Reserved (1) | Promised Stream ID (31) */
1969 0x82, /* ":method: GET" - indexed header field */
1970 0x87, /* ":scheme: https" - indexed header field */
1971 0x85, /* ":path: /index.html" - indexed header field */
1972 };
1973 /* clang-format on */
1974
1975 /* Decode */
1976 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
1977
1978 /* Validate */
1979 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
1980 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_PUSH_PROMISE, 0x1 /*stream_id*/));
1981 ASSERT_UINT_EQUALS(2, frame->promised_stream_id);
1982 ASSERT_FALSE(frame->headers_malformed);
1983 ASSERT_UINT_EQUALS(3, aws_http_headers_count(frame->headers));
1984 ASSERT_SUCCESS(s_check_header(frame, 0, ":method", "GET", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1985 ASSERT_SUCCESS(s_check_header(frame, 1, ":scheme", "https", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1986 ASSERT_SUCCESS(s_check_header(frame, 2, ":path", "/index.html", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
1987 ASSERT_FALSE(frame->end_stream);
1988 return AWS_OP_SUCCESS;
1989 }
1990
1991 /* Unknown flags should be ignored.
1992 * PUSH_PROMISE supports END_HEADERS and PADDED */
1993 H2_DECODER_ON_CLIENT_TEST(h2_decoder_push_promise_ignores_unknown_flags) {
1994 (void)allocator;
1995 struct fixture *fixture = ctx;
1996
1997 /* clang-format off */
1998 uint8_t input[] = {
1999 0x00, 0x00, 10, /* Length (24) */
2000 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
2001 0xFF, /* Flags (8) */
2002 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2003 /* PUSH_PROMISE */
2004 0x02, /* Pad Length (8) - F_PADDED */
2005 0x00, 0x00, 0x00, 0x02, /* Reserved (1) | Promised Stream ID (31) */
2006 0x82, /* ":method: GET" - indexed header field */
2007 0x87, /* ":scheme: https" - indexed header field */
2008 0x85, /* ":path: /index.html" - indexed header field */
2009 0x00, 0x00, /* Padding (*) - F_PADDED */
2010 };
2011 /* clang-format on */
2012
2013 /* Decode */
2014 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2015
2016 /* Validate */
2017 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2018 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_PUSH_PROMISE, 0x1 /*stream_id*/));
2019 ASSERT_UINT_EQUALS(2, frame->promised_stream_id);
2020 ASSERT_FALSE(frame->headers_malformed);
2021 ASSERT_UINT_EQUALS(3, aws_http_headers_count(frame->headers));
2022 ASSERT_SUCCESS(s_check_header(frame, 0, ":method", "GET", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2023 ASSERT_SUCCESS(s_check_header(frame, 1, ":scheme", "https", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2024 ASSERT_SUCCESS(s_check_header(frame, 2, ":path", "/index.html", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2025 ASSERT_FALSE(frame->end_stream);
2026 return AWS_OP_SUCCESS;
2027 }
2028
2029 H2_DECODER_ON_CLIENT_TEST(h2_decoder_push_promise_continuation) {
2030 (void)allocator;
2031 struct fixture *fixture = ctx;
2032
2033 /* clang-format off */
2034 uint8_t input[] = {
2035 /* PUSH_PROMISE FRAME */
2036 0x00, 0x00, 0x08, /* Length (24) */
2037 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
2038 AWS_H2_FRAME_F_PADDED, /* Flags (8) */
2039 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2040 /* PAYLOAD */
2041 0x02, /* Pad Length (8) - F_PADDED */
2042 0x00, 0x00, 0x00, 0x02, /* Reserved (1) | Promised Stream ID (31) */
2043 0x82, /* ":method: GET" - indexed header field */
2044 0x00, 0x00, /* Padding (*) - F_PADDED */
2045
2046 /* CONTINUATION FRAME - empty payload just for kicks */
2047 0x00, 0x00, 0x00, /* Length (24) */
2048 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
2049 0x00, /* Flags (8) */
2050 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2051 /* PAYLOAD */
2052
2053
2054 /* CONTINUATION FRAME */
2055 0x00, 0x00, 0x02, /* Length (24) */
2056 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
2057 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
2058 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2059 /* PAYLOAD */
2060 0x87, /* ":scheme: https" - indexed header field */
2061 0x85, /* ":path: /index.html" - indexed header field */
2062 };
2063 /* clang-format on */
2064
2065 /* Decode */
2066 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2067
2068 /* Validate */
2069 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2070 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_PUSH_PROMISE, 0x1 /*stream_id*/));
2071 ASSERT_UINT_EQUALS(2, frame->promised_stream_id);
2072 ASSERT_FALSE(frame->headers_malformed);
2073 ASSERT_UINT_EQUALS(3, aws_http_headers_count(frame->headers));
2074 ASSERT_SUCCESS(s_check_header(frame, 0, ":method", "GET", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2075 ASSERT_SUCCESS(s_check_header(frame, 1, ":scheme", "https", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2076 ASSERT_SUCCESS(s_check_header(frame, 2, ":path", "/index.html", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2077 ASSERT_FALSE(frame->end_stream);
2078 return AWS_OP_SUCCESS;
2079 }
2080
2081 /* Once a header-block starts, it's illegal for any frame but a CONTINUATION on that same stream to arrive.
2082 * This test sends a different frame type next */
2083 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_push_promise_continuation_expected) {
2084 (void)allocator;
2085 struct fixture *fixture = ctx;
2086
2087 /* clang-format off */
2088 uint8_t input[] = {
2089 /* PUSH_PROMISE FRAME */
2090 0x00, 0x00, 0x07, /* Length (24) */
2091 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
2092 0x00, /* Flags (8) */
2093 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2094 /* PAYLOAD */
2095 0x00, 0x00, 0x00, 0x02, /* Reserved (1) | Promised Stream ID (31) */
2096 0x82, /* ":method: GET" - indexed header field */
2097 0x87, /* ":scheme: https" - indexed header field */
2098 0x85, /* ":path: /index.html" - indexed header field */
2099
2100 /* DATA FRAME <-- ERROR should be CONTINUATION because PUSH_PROMISE lacked END_HEADERS flag */
2101 0x00, 0x00, 0x05, /* Length (24) */
2102 AWS_H2_FRAME_T_DATA, /* Type (8) */
2103 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
2104 0x00, 0x00, 0x00, 0x02, /* Reserved (1) | Stream Identifier (31) */
2105 /* DATA */
2106 'h', 'e', 'l', 'l', 'o', /* Data (*) */
2107 };
2108 /* clang-format on */
2109
2110 /* Decode */
2111 ASSERT_H2ERR_ERROR(
2112 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2113
2114 return AWS_OP_SUCCESS;
2115 }
2116
2117 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_push_promise_requires_stream_id) {
2118 (void)allocator;
2119 struct fixture *fixture = ctx;
2120
2121 /* clang-format off */
2122 uint8_t input[] = {
2123 0x00, 0x00, 0x07, /* Length (24) */
2124 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
2125 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
2126 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2127 /* PUSH_PROMISE */
2128 0x00, 0x00, 0x00, 0x02, /* Reserved (1) | Promised Stream ID (31) */
2129 0x82, /* ":method: GET" - indexed header field */
2130 0x87, /* ":scheme: https" - indexed header field */
2131 0x85, /* ":path: /index.html" - indexed header field */
2132 };
2133 /* clang-format on */
2134
2135 /* Decode */
2136 ASSERT_H2ERR_ERROR(
2137 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2138
2139 return AWS_OP_SUCCESS;
2140 }
2141
2142 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_push_promise_must_be_request_1) {
2143 (void)allocator;
2144 struct fixture *fixture = ctx;
2145
2146 /* clang-format off */
2147 uint8_t input[] = {
2148 0x00, 0x00, 0x09, /* Length (24) */
2149 AWS_H2_FRAME_T_PUSH_PROMISE, /* Type (8) */
2150 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
2151 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2152 /* PUSH_PROMISE */
2153 0x00, 0x00, 0x00, 0x02, /* Reserved (1) | Promised Stream ID (31) */
2154 0x48, 0x03, '3', '0', '2', /* ":status: 302" - RESPONSE pseudo-header is incorrect */
2155 };
2156 /* clang-format on */
2157
2158 /* Decode */
2159 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2160
2161 /* Validate */
2162 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2163 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_PUSH_PROMISE, 1 /*stream_id*/));
2164 ASSERT_UINT_EQUALS(2, frame->promised_stream_id);
2165 ASSERT_TRUE(frame->headers_malformed);
2166 return AWS_OP_SUCCESS;
2167 }
2168
2169 /* Malformed if PUSH_PROMISE missing request pseudo-headers */
2170 H2_DECODER_ON_CLIENT_TEST(h2_decoder_malformed_push_promise_must_be_request_2) {
2171 (void)allocator;
2172 struct fixture *fixture = ctx;
2173
2174 /* clang-format off */
2175 uint8_t input[] = {
2176 0x00, 0x00, 0x04, /* Length (24) */
2177 AWS_H2_FRAME_T_PUSH_PROMISE, /* Type (8) */
2178 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
2179 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2180 /* PUSH_PROMISE */
2181 0x00, 0x00, 0x00, 0x02, /* Reserved (1) | Promised Stream ID (31) */
2182 /* No headers */
2183 };
2184 /* clang-format on */
2185
2186 /* Decode */
2187 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2188
2189 /* Validate */
2190 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2191 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_PUSH_PROMISE, 1 /*stream_id*/));
2192 ASSERT_UINT_EQUALS(2, frame->promised_stream_id);
2193 ASSERT_TRUE(frame->headers_malformed);
2194 return AWS_OP_SUCCESS;
2195 }
2196
2197 /* Promised stream ID must be valid */
2198 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_push_promise_requires_promised_stream_id) {
2199 (void)allocator;
2200 struct fixture *fixture = ctx;
2201
2202 /* clang-format off */
2203 uint8_t input[] = {
2204 0x00, 0x00, 0x07, /* Length (24) */
2205 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
2206 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
2207 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2208 /* PUSH_PROMISE */
2209 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Promised Stream ID (31) */
2210 0x82, /* ":method: GET" - indexed header field */
2211 0x87, /* ":scheme: https" - indexed header field */
2212 0x85, /* ":path: /index.html" - indexed header field */
2213 };
2214 /* clang-format on */
2215
2216 /* Decode */
2217 ASSERT_H2ERR_ERROR(
2218 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2219
2220 return AWS_OP_SUCCESS;
2221 }
2222
2223 /* Promised stream will be invalid, if enable_push is set to 0 */
2224 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_push_promise_with_enable_push_0) {
2225 (void)allocator;
2226 struct fixture *fixture = ctx;
2227
2228 /* clang-format off */
2229 uint8_t input[] = {
2230 0x00, 0x00, 0x07, /* Length (24) */
2231 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
2232 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
2233 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2234 /* PUSH_PROMISE */
2235 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Promised Stream ID (31) */
2236 0x82, /* ":method: GET" - indexed header field */
2237 0x87, /* ":scheme: https" - indexed header field */
2238 0x85, /* ":path: /index.html" - indexed header field */
2239 };
2240 /* clang-format on */
2241 aws_h2_decoder_set_setting_enable_push(fixture->decode.decoder, (uint32_t)0);
2242 /* Decode */
2243 ASSERT_H2ERR_ERROR(
2244 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2245
2246 return AWS_OP_SUCCESS;
2247 }
2248
2249 /* Test PING frame */
2250 H2_DECODER_ON_CLIENT_TEST(h2_decoder_ping) {
2251 (void)allocator;
2252 struct fixture *fixture = ctx;
2253
2254 /* clang-format off */
2255 uint8_t input[] = {
2256 0x00, 0x00, 0x08, /* Length (24) */
2257 AWS_H2_FRAME_T_PING, /* Type (8) */
2258 0x0, /* Flags (8) */
2259 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2260 /* PING */
2261 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g' /* Opaque Data (64) */
2262 };
2263 /* clang-format on */
2264
2265 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2266
2267 /* Validate. */
2268 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2269 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_PING, 0x0 /*stream_id*/));
2270 ASSERT_BIN_ARRAYS_EQUALS("pingpong", AWS_HTTP2_PING_DATA_SIZE, frame->ping_opaque_data, AWS_HTTP2_PING_DATA_SIZE);
2271 ASSERT_FALSE(frame->ack);
2272 return AWS_OP_SUCCESS;
2273 }
2274
2275 /* Test PING frame with ALL flags set (ACK is only supported flag) */
2276 H2_DECODER_ON_CLIENT_TEST(h2_decoder_ping_ack) {
2277 (void)allocator;
2278 struct fixture *fixture = ctx;
2279
2280 /* clang-format off */
2281 uint8_t input[] = {
2282 0x00, 0x00, 0x08, /* Length (24) */
2283 AWS_H2_FRAME_T_PING, /* Type (8) */
2284 0xFF, /* Flags (8) */
2285 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2286 /* PING */
2287 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g' /* Opaque Data (64) */
2288 };
2289 /* clang-format on */
2290
2291 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2292
2293 /* Validate. */
2294 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2295 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_PING, 0x0 /*stream_id*/));
2296 ASSERT_BIN_ARRAYS_EQUALS("pingpong", AWS_HTTP2_PING_DATA_SIZE, frame->ping_opaque_data, AWS_HTTP2_PING_DATA_SIZE);
2297 ASSERT_TRUE(frame->ack);
2298 return AWS_OP_SUCCESS;
2299 }
2300
2301 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_ping_forbids_stream_id) {
2302 (void)allocator;
2303 struct fixture *fixture = ctx;
2304
2305 /* clang-format off */
2306 uint8_t input[] = {
2307 0x00, 0x00, 0x08, /* Length (24) */
2308 AWS_H2_FRAME_T_PING, /* Type (8) */
2309 0x0, /* Flags (8) */
2310 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2311 /* PING */
2312 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g' /* Opaque Data (64) */
2313 };
2314 /* clang-format on */
2315
2316 ASSERT_H2ERR_ERROR(
2317 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2318
2319 return AWS_OP_SUCCESS;
2320 }
2321
2322 /* PING payload MUST be 8 bytes */
2323 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_ping_payload_too_small) {
2324 (void)allocator;
2325 struct fixture *fixture = ctx;
2326
2327 /* clang-format off */
2328 uint8_t input[] = {
2329 0x00, 0x00, 0x00, /* Length (24) */
2330 AWS_H2_FRAME_T_PING, /* Type (8) */
2331 0x0, /* Flags (8) */
2332 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2333 /* PING */
2334 /* Opaque Data (64) <-- MISSING */
2335 };
2336 /* clang-format on */
2337
2338 ASSERT_H2ERR_ERROR(
2339 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2340
2341 return AWS_OP_SUCCESS;
2342 }
2343
2344 /* PING payload MUST be 8 bytes */
2345 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_ping_payload_too_large) {
2346 (void)allocator;
2347 struct fixture *fixture = ctx;
2348
2349 /* clang-format off */
2350 uint8_t input[] = {
2351 0x00, 0x00, 0x09, /* Length (24) */
2352 AWS_H2_FRAME_T_PING, /* Type (8) */
2353 0x0, /* Flags (8) */
2354 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2355 /* PING */
2356 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g', 0x00 /* Opaque Data (64) <-- ERROR: TOO LARGE */
2357 };
2358 /* clang-format on */
2359
2360 ASSERT_H2ERR_ERROR(
2361 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2362
2363 return AWS_OP_SUCCESS;
2364 }
2365
2366 /* Test GOAWAY frame */
2367 H2_DECODER_ON_CLIENT_TEST(h2_decoder_goaway) {
2368 (void)allocator;
2369 struct fixture *fixture = ctx;
2370
2371 /* clang-format off */
2372 uint8_t input[] = {
2373 0x00, 0x00, 11, /* Length (24) */
2374 AWS_H2_FRAME_T_GOAWAY, /* Type (8) */
2375 0xFF, /* Flags (8) <-- set all flags, all of which should be ignored */
2376 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2377 /* GOAWAY */
2378 0xFF, 0x00, 0x00, 0x01, /* Reserved (1) | Last Stream ID (31) */
2379 0xFE, 0xED, 0xBE, 0xEF, /* Error Code (32) */
2380 'b', 'y', 'e' /* Additional Debug Data (*) */
2381 };
2382 /* clang-format on */
2383
2384 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2385
2386 /* Validate. */
2387 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2388 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_GOAWAY, 0x0 /*stream_id*/));
2389 ASSERT_UINT_EQUALS(0x7F000001, frame->goaway_last_stream_id);
2390 ASSERT_UINT_EQUALS(0xFEEDBEEF, frame->error_code);
2391 ASSERT_BIN_ARRAYS_EQUALS("bye", 3, frame->data.buffer, frame->data.len);
2392
2393 return AWS_OP_SUCCESS;
2394 }
2395
2396 /* Test GOAWAY frame with no debug data */
2397 H2_DECODER_ON_CLIENT_TEST(h2_decoder_goaway_empty) {
2398 (void)allocator;
2399 struct fixture *fixture = ctx;
2400
2401 /* clang-format off */
2402 uint8_t input[] = {
2403 0x00, 0x00, 0x08, /* Length (24) */
2404 AWS_H2_FRAME_T_GOAWAY, /* Type (8) */
2405 0x00, /* Flags (8) */
2406 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2407 /* GOAWAY */
2408 0xFF, 0x00, 0x00, 0x01, /* Reserved (1) | Last Stream ID (31) */
2409 0xFE, 0xED, 0xBE, 0xEF, /* Error Code (32) */
2410 /* Additional Debug Data (*) */
2411 };
2412 /* clang-format on */
2413
2414 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2415
2416 /* Validate. */
2417 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2418 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_GOAWAY, 0x0 /*stream_id*/));
2419 ASSERT_UINT_EQUALS(0x7F000001, frame->goaway_last_stream_id);
2420 ASSERT_UINT_EQUALS(0xFEEDBEEF, frame->error_code);
2421 ASSERT_BIN_ARRAYS_EQUALS("", 0, frame->data.buffer, frame->data.len);
2422
2423 return AWS_OP_SUCCESS;
2424 }
2425
2426 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_goaway_forbids_stream_id) {
2427 (void)allocator;
2428 struct fixture *fixture = ctx;
2429
2430 /* clang-format off */
2431 uint8_t input[] = {
2432 0x00, 0x00, 11, /* Length (24) */
2433 AWS_H2_FRAME_T_GOAWAY, /* Type (8) */
2434 0xFF, /* Flags (8) <-- set all flags, all of which should be ignored */
2435 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2436 /* GOAWAY */
2437 0xFF, 0x00, 0x00, 0x01, /* Reserved (1) | Last Stream ID (31) */
2438 0xFE, 0xED, 0xBE, 0xEF, /* Error Code (32) */
2439 'b', 'y', 'e' /* Additional Debug Data (*) */
2440 };
2441 /* clang-format on */
2442
2443 ASSERT_H2ERR_ERROR(
2444 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2445
2446 return AWS_OP_SUCCESS;
2447 }
2448
2449 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_goaway_payload_too_small) {
2450 (void)allocator;
2451 struct fixture *fixture = ctx;
2452
2453 /* clang-format off */
2454 uint8_t input[] = {
2455 0x00, 0x00, 0x00, /* Length (24) */
2456 AWS_H2_FRAME_T_GOAWAY, /* Type (8) */
2457 0x00, /* Flags (8) */
2458 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2459 /* GOAWAY */
2460 /* Reserved (1) | Last Stream ID (31) <-- MISSING */
2461 /* Error Code (32) <-- MISSING */
2462 /* Additional Debug Data (*) */
2463 };
2464 /* clang-format on */
2465
2466 ASSERT_H2ERR_ERROR(
2467 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2468
2469 return AWS_OP_SUCCESS;
2470 }
2471
2472 /* Test WINDOW_UPDATE frame on stream 0 */
2473 H2_DECODER_ON_CLIENT_TEST(h2_decoder_window_update_connection) {
2474 (void)allocator;
2475 struct fixture *fixture = ctx;
2476
2477 /* clang-format off */
2478 uint8_t input[] = {
2479 0x00, 0x00, 0x04, /* Length (24) */
2480 AWS_H2_FRAME_T_WINDOW_UPDATE,/* Type (8) */
2481 0xFF, /* Flags (8) <-- set all flags, all of which should be ignored */
2482 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2483 /* WINDOW_UPDATE */
2484 0xFF, 0x00, 0x00, 0x01, /* Reserved (1) | Window Size Increment (31) */
2485 };
2486 /* clang-format on */
2487
2488 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2489
2490 /* Validate. */
2491 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2492 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_WINDOW_UPDATE, 0x0 /*stream_id*/));
2493 ASSERT_UINT_EQUALS(0x7F000001, frame->window_size_increment);
2494
2495 return AWS_OP_SUCCESS;
2496 }
2497
2498 /* Test WINDOW_UPDATE frame on a specific stream.
2499 * This the only frame type whose stream-id can be zero OR non-zero*/
2500 H2_DECODER_ON_CLIENT_TEST(h2_decoder_window_update_stream) {
2501 (void)allocator;
2502 struct fixture *fixture = ctx;
2503
2504 /* clang-format off */
2505 uint8_t input[] = {
2506 0x00, 0x00, 0x04, /* Length (24) */
2507 AWS_H2_FRAME_T_WINDOW_UPDATE,/* Type (8) */
2508 0x00, /* Flags (8) */
2509 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2510 /* WINDOW_UPDATE */
2511 0xFF, 0x00, 0x00, 0x01, /* Reserved (1) | Window Size Increment (31) */
2512 };
2513 /* clang-format on */
2514
2515 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2516
2517 /* Validate. */
2518 struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(&fixture->decode);
2519 ASSERT_SUCCESS(h2_decoded_frame_check_finished(frame, AWS_H2_FRAME_T_WINDOW_UPDATE, 0x1 /*stream_id*/));
2520 ASSERT_UINT_EQUALS(0x7F000001, frame->window_size_increment);
2521
2522 return AWS_OP_SUCCESS;
2523 }
2524
2525 /* WINDOW_UPDATE payload must always be 4 bytes */
2526 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_window_update_payload_too_small) {
2527 (void)allocator;
2528 struct fixture *fixture = ctx;
2529
2530 /* clang-format off */
2531 uint8_t input[] = {
2532 0x00, 0x00, 0x00, /* Length (24) */
2533 AWS_H2_FRAME_T_WINDOW_UPDATE,/* Type (8) */
2534 0x00, /* Flags (8) */
2535 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2536 /* WINDOW_UPDATE */
2537 /* Reserved (1) | Window Size Increment (31) <-- MISSING */
2538 };
2539 /* clang-format on */
2540
2541 ASSERT_H2ERR_ERROR(
2542 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2543
2544 return AWS_OP_SUCCESS;
2545 }
2546
2547 /* WINDOW_UPDATE payload must always be 4 bytes */
2548 H2_DECODER_ON_CLIENT_TEST(h2_decoder_err_window_update_payload_too_large) {
2549 (void)allocator;
2550 struct fixture *fixture = ctx;
2551
2552 /* clang-format off */
2553 uint8_t input[] = {
2554 0x00, 0x00, 0x08, /* Length (24) */
2555 AWS_H2_FRAME_T_WINDOW_UPDATE,/* Type (8) */
2556 0x00, /* Flags (8) */
2557 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2558 /* WINDOW_UPDATE */
2559 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Window Size Increment (31) */
2560 0x00, 0x00, 0x00, 0x02, /* ERROR TOO BIG */
2561 };
2562 /* clang-format on */
2563
2564 ASSERT_H2ERR_ERROR(
2565 AWS_HTTP2_ERR_FRAME_SIZE_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2566
2567 return AWS_OP_SUCCESS;
2568 }
2569
2570 /* Frames of unknown type must be ignored */
2571 H2_DECODER_ON_CLIENT_TEST(h2_decoder_unknown_frame_type_ignored) {
2572 (void)allocator;
2573 struct fixture *fixture = ctx;
2574
2575 /* clang-format off */
2576 uint8_t input[] = {
2577 /* UNKNOWN FRAME WITHOUT FLAGS OR STREAM-ID */
2578 0x00, 0x00, 0x04, /* Length (24) */
2579 0xFF, /* Type (8) */
2580 0x00, /* Flags (8) */
2581 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2582 0xFF, 0xFF, 0xFF, 0xFF, /* Payload (*) */
2583
2584 /* UNKNOWN FRAME WITH FLAGS AND STREAM-ID */
2585 0x00, 0x00, 0x04, /* Length (24) */
2586 0xFF, /* Type (8) */
2587 0xFF, /* Flags (8) */
2588 0xFF, 0xFF, 0xFF, 0xFF, /* Reserved (1) | Stream Identifier (31) */
2589 0xFF, 0xFF, 0xFF, 0xFF, /* Payload (*) */
2590
2591 /* UNKNOWN FRAME WITH NO PAYLOAD */
2592 0x00, 0x00, 0x00, /* Length (24) */
2593 0xFF, /* Type (8) */
2594 0xFF, /* Flags (8) */
2595 0xFF, 0xFF, 0xFF, 0xFF, /* Reserved (1) | Stream Identifier (31) */
2596 /* Payload (*) */
2597 };
2598 /* clang-format on */
2599
2600 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2601
2602 /* No callbacks should have fired about any of these frames */
2603 ASSERT_UINT_EQUALS(0, h2_decode_tester_frame_count(&fixture->decode));
2604 return AWS_OP_SUCCESS;
2605 }
2606
2607 static int s_get_finished_frame_i(
2608 struct fixture *fixture,
2609 size_t i,
2610 enum aws_h2_frame_type type,
2611 uint32_t stream_id,
2612 struct h2_decoded_frame **out_frame) {
2613
2614 ASSERT_TRUE(i < h2_decode_tester_frame_count(&fixture->decode));
2615 *out_frame = h2_decode_tester_get_frame(&fixture->decode, i);
2616 ASSERT_SUCCESS(h2_decoded_frame_check_finished(*out_frame, type, stream_id));
2617 return AWS_OP_SUCCESS;
2618 }
2619
2620 /* Test processing many different frame types in a row.
2621 * (most other tests just operate on 1 frame) */
2622 H2_DECODER_ON_CLIENT_TEST(h2_decoder_many_frames_in_a_row) {
2623 (void)allocator;
2624 struct fixture *fixture = ctx;
2625
2626 /* clang-format off */
2627 uint8_t input[] = {
2628 /* HEADERS FRAME*/
2629 0x00, 0x00, 0x05, /* Length (24) */
2630 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
2631 0x00, /* Flags (8) */
2632 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2633 /* Payload */
2634 0x48, 0x03, '3', '0', '2', /* ":status: 302" - indexed name, uncompressed value */
2635
2636 /* CONTINUATION FRAME*/
2637 0x00, 0x00, 0x09, /* Length (24) */
2638 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
2639 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
2640 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2641 /* Payload */
2642 0x58, 0x07, 'p', 'r', 'i', 'v', 'a', 't', 'e', /* "cache-control: private" */
2643
2644 /* SETTINGS ACK FRAME*/
2645 0x00, 0x00, 0x00, /* Length (24) */
2646 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
2647 AWS_H2_FRAME_F_ACK, /* Flags (8) */
2648 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2649
2650 /* PUSH_PROMISE FRAME */
2651 0x00, 0x00, 0x05, /* Length (24) */
2652 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
2653 0x00, /* Flags (8) */
2654 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2655 /* Payload */
2656 0x80, 0x00, 0x00, 0x02, /* Reserved (1) | Promised Stream ID (31) */
2657 0x82, /* ":method: GET" - indexed header field */
2658
2659 /* CONTINUATION FRAME */
2660 0x00, 0x00, 0x02, /* Length (24) */
2661 AWS_H2_FRAME_T_CONTINUATION,/* Type (8) */
2662 AWS_H2_FRAME_F_END_HEADERS, /* Flags (8) */
2663 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2664 /* Payload */
2665 0x87, /* ":scheme: https" - indexed header field */
2666 0x85, /* ":path: /index.html" - indexed header field */
2667
2668 /* PRIORITY FRAME */
2669 0x00, 0x00, 0x05, /* Length (24) */
2670 AWS_H2_FRAME_T_PRIORITY, /* Type (8) */
2671 0x00, /* Flags (8) */
2672 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2673 /* Payload */
2674 0x00, 0x00, 0x00, 0x02, /* Exclusive (1) | Stream Dependency (31) */
2675 0x09, /* Weight (8) */
2676
2677 /* WINDOW_UPDATE FRAME */
2678 0x00, 0x00, 0x04, /* Length (24) */
2679 AWS_H2_FRAME_T_WINDOW_UPDATE,/* Type (8) */
2680 0x00, /* Flags (8) */
2681 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2682 /* WINDOW_UPDATE */
2683 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Window Size Increment (31) */
2684
2685 /* DATA FRAME */
2686 0x00, 0x00, 0x01, /* Length (24) */
2687 AWS_H2_FRAME_T_DATA, /* Type (8) */
2688 AWS_H2_FRAME_F_END_STREAM, /* Flags (8) */
2689 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
2690 /* Payload */
2691 'h', /* Data (*) */
2692
2693 /* UNKNOWN FRAME */
2694 0x00, 0x00, 0x01, /* Length (24) */
2695 0xFF, /* Type (8) */
2696 0xFF, /* Flags (8) */
2697 0xFF, 0xFF, 0xFF, 0xFF, /* Reserved (1) | Stream Identifier (31) */
2698 /* Payload (*) */
2699 'z',
2700
2701 /* RST_STREAM FRAME */
2702 0x00, 0x00, 0x04, /* Length (24) */
2703 AWS_H2_FRAME_T_RST_STREAM, /* Type (8) */
2704 0x00, /* Flags (8) */
2705 0x00, 0x00, 0x00, 0x02, /* Reserved (1) | Stream Identifier (31) */
2706 /* Payload */
2707 0xFF, 0xEE, 0xDD, 0xCC, /* Error Code (32) */
2708
2709 /* GOAWAY FRAME */
2710 0x00, 0x00, 11, /* Length (24) */
2711 AWS_H2_FRAME_T_GOAWAY, /* Type (8) */
2712 0xFF, /* Flags (8) <-- set all flags, all of which should be ignored */
2713 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2714 /* Payload */
2715 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Last Stream ID (31) */
2716 0xFE, 0xED, 0xBE, 0xEF, /* Error Code (32) */
2717 'b', 'y', 'e', /* Additional Debug Data (*) */
2718
2719 /* PING ACK FRAME */
2720 0x00, 0x00, 0x08, /* Length (24) */
2721 AWS_H2_FRAME_T_PING, /* Type (8) */
2722 AWS_H2_FRAME_F_ACK, /* Flags (8) */
2723 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2724 /* Payload */
2725 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g', /* Opaque Data (64) */
2726 };
2727 /* clang-format on */
2728
2729 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2730
2731 size_t frame_i = 0;
2732 struct h2_decoded_frame *frame;
2733
2734 /* Validate HEADERS (and its CONTINUATION) */
2735 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, frame_i++, AWS_H2_FRAME_T_HEADERS, 0x1 /*stream-id*/, &frame));
2736 ASSERT_UINT_EQUALS(2, aws_http_headers_count(frame->headers));
2737 ASSERT_SUCCESS(s_check_header(frame, 0, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2738 ASSERT_SUCCESS(s_check_header(frame, 1, "cache-control", "private", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2739 ASSERT_FALSE(frame->end_stream);
2740
2741 /* Validate SETTINGS ACK */
2742 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, frame_i++, AWS_H2_FRAME_T_SETTINGS, 0x0 /*stream-id*/, &frame));
2743 ASSERT_TRUE(frame->ack);
2744
2745 /* Validate PUSH_PROMISE (and its CONTINUATION) */
2746 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, frame_i++, AWS_H2_FRAME_T_PUSH_PROMISE, 0x1 /*stream-id*/, &frame));
2747 ASSERT_UINT_EQUALS(2, frame->promised_stream_id);
2748 ASSERT_UINT_EQUALS(3, aws_http_headers_count(frame->headers));
2749 ASSERT_SUCCESS(s_check_header(frame, 0, ":method", "GET", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2750 ASSERT_SUCCESS(s_check_header(frame, 1, ":scheme", "https", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2751 ASSERT_SUCCESS(s_check_header(frame, 2, ":path", "/index.html", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
2752 ASSERT_FALSE(frame->end_stream);
2753
2754 /* PRIORITY frame is ignored by decoder */
2755
2756 /* Validate WINDOW_UPDATE */
2757 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, frame_i++, AWS_H2_FRAME_T_WINDOW_UPDATE, 0x0 /*stream-id*/, &frame));
2758 ASSERT_UINT_EQUALS(0x1, frame->window_size_increment);
2759
2760 /* Validate DATA */
2761 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, frame_i++, AWS_H2_FRAME_T_DATA, 0x1 /*stream-id*/, &frame));
2762 ASSERT_BIN_ARRAYS_EQUALS("h", 1, frame->data.buffer, frame->data.len);
2763 ASSERT_TRUE(frame->end_stream);
2764
2765 /* UNKNOWN frame is ignored */
2766
2767 /* Validate RST_STREAM */
2768 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, frame_i++, AWS_H2_FRAME_T_RST_STREAM, 0x2 /*stream-id*/, &frame));
2769 ASSERT_UINT_EQUALS(0xFFEEDDCC, frame->error_code);
2770
2771 /* Validate GOAWAY */
2772 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, frame_i++, AWS_H2_FRAME_T_GOAWAY, 0x0 /*stream-id*/, &frame));
2773 ASSERT_UINT_EQUALS(0x1, frame->goaway_last_stream_id);
2774 ASSERT_UINT_EQUALS(0xFEEDBEEF, frame->error_code);
2775 ASSERT_BIN_ARRAYS_EQUALS("bye", 3, frame->data.buffer, frame->data.len);
2776
2777 /* Validate PING */
2778 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, frame_i++, AWS_H2_FRAME_T_PING, 0x0 /*stream-id*/, &frame));
2779 ASSERT_TRUE(frame->ack);
2780
2781 /* Ensure no further frames reported */
2782 ASSERT_UINT_EQUALS(frame_i, h2_decode_tester_frame_count(&fixture->decode));
2783
2784 return AWS_OP_SUCCESS;
2785 }
2786
2787 /* Test that client can decode a proper connection preface sent by the server.
2788 * A server connection preface is just a settings frame */
2789 H2_DECODER_ON_CLIENT_PREFACE_TEST(h2_decoder_preface_from_server) {
2790 (void)allocator;
2791 struct fixture *fixture = ctx;
2792
2793 /* clang-format off */
2794 uint8_t input[] = {
2795 /* SETTINGS FRAME - empty settings frame is acceptable in preface */
2796 0x00, 0x00, 0x00, /* Length (24) */
2797 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
2798 0x00, /* Flags (8) */
2799 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2800
2801 /* PING FRAME - send another frame to be sure decoder is now functioning normally */
2802 0x00, 0x00, 0x08, /* Length (24) */
2803 AWS_H2_FRAME_T_PING, /* Type (8) */
2804 0x0, /* Flags (8) */
2805 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2806 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g' /* Opaque Data (64) */
2807 };
2808 /* clang-format on */
2809
2810 /* Decode */
2811 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2812
2813 /* Validate */
2814 ASSERT_UINT_EQUALS(2, h2_decode_tester_frame_count(&fixture->decode));
2815
2816 struct h2_decoded_frame *frame;
2817 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, 0, AWS_H2_FRAME_T_SETTINGS, 0 /*stream-id*/, &frame));
2818 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, 1, AWS_H2_FRAME_T_PING, 0 /*stream-id*/, &frame));
2819
2820 return AWS_OP_SUCCESS;
2821 }
2822
2823 /* The server must send a SETTINGS frame first.
2824 * It's an error to send any other frame type */
2825 H2_DECODER_ON_CLIENT_PREFACE_TEST(h2_decoder_err_bad_preface_from_server_1) {
2826 (void)allocator;
2827 struct fixture *fixture = ctx;
2828
2829 /* clang-format off */
2830 uint8_t input[] = {
2831 /* PING FRAME - but should be SETTINGS */
2832 0x00, 0x00, 0x08, /* Length (24) */
2833 AWS_H2_FRAME_T_PING, /* Type (8) */
2834 0x0, /* Flags (8) */
2835 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2836 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g' /* Opaque Data (64) */
2837 };
2838 /* clang-format on */
2839
2840 /* Decode */
2841 ASSERT_H2ERR_ERROR(
2842 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2843
2844 return AWS_OP_SUCCESS;
2845 }
2846
2847 /* The server must send a SETTINGS frame first.
2848 * It's an error if SETTINGS frame is an ACK */
2849 H2_DECODER_ON_CLIENT_PREFACE_TEST(h2_decoder_err_bad_preface_from_server_2) {
2850 (void)allocator;
2851 struct fixture *fixture = ctx;
2852
2853 /* clang-format off */
2854 uint8_t input[] = {
2855 /* SETTINGS FRAME */
2856 0x00, 0x00, 0x00, /* Length (24) */
2857 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
2858 AWS_H2_FRAME_F_ACK, /* Flags (8) <-- Preface SETTINGS should not have ACK */
2859 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2860 };
2861 /* clang-format on */
2862
2863 /* Decode */
2864 ASSERT_H2ERR_ERROR(
2865 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2866
2867 return AWS_OP_SUCCESS;
2868 }
2869
2870 /* The server mustn't send the "client connection preface string" */
2871 H2_DECODER_ON_CLIENT_PREFACE_TEST(h2_decoder_err_bad_preface_from_server_3) {
2872 (void)allocator;
2873 struct fixture *fixture = ctx;
2874
2875 const struct aws_byte_cursor input = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n");
2876
2877 /* Decode */
2878 ASSERT_H2ERR_ERROR(AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, input));
2879
2880 return AWS_OP_SUCCESS;
2881 }
2882
2883 /* Test that client can decode a proper connection preface sent by the client. */
2884 H2_DECODER_ON_SERVER_PREFACE_TEST(h2_decoder_preface_from_client) {
2885 (void)allocator;
2886 struct fixture *fixture = ctx;
2887
2888 /* clang-format off */
2889 uint8_t input[] = {
2890 /* Client connection preface string */
2891 'P','R','I',' ','*',' ','H','T','T','P','/','2','.','0','\r','\n','\r','\n','S','M','\r','\n','\r','\n',
2892
2893 /* SETTINGS FRAME - empty settings frame is acceptable in preface */
2894 0x00, 0x00, 0x00, /* Length (24) */
2895 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
2896 0x00, /* Flags (8) */
2897 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2898
2899 /* PING FRAME - send another frame to be sure decoder is now functioning normally */
2900 0x00, 0x00, 0x08, /* Length (24) */
2901 AWS_H2_FRAME_T_PING, /* Type (8) */
2902 0x0, /* Flags (8) */
2903 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2904 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g' /* Opaque Data (64) */
2905 };
2906 /* clang-format on */
2907
2908 /* Decode */
2909 ASSERT_H2ERR_SUCCESS(s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2910
2911 /* Validate */
2912 ASSERT_UINT_EQUALS(2, h2_decode_tester_frame_count(&fixture->decode));
2913
2914 struct h2_decoded_frame *frame;
2915 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, 0, AWS_H2_FRAME_T_SETTINGS, 0 /*stream-id*/, &frame));
2916 ASSERT_SUCCESS(s_get_finished_frame_i(fixture, 1, AWS_H2_FRAME_T_PING, 0 /*stream-id*/, &frame));
2917
2918 return AWS_OP_SUCCESS;
2919 }
2920
2921 /* Should fail because we're not sending the "client connection preface string" */
2922 H2_DECODER_ON_SERVER_PREFACE_TEST(h2_decoder_err_bad_preface_from_client_1) {
2923 (void)allocator;
2924 struct fixture *fixture = ctx;
2925
2926 /* clang-format off */
2927 uint8_t input[] = {
2928 /* SETTINGS FRAME - empty settings frame is acceptable in preface */
2929 0x00, 0x00, 0x00, /* Length (24) */
2930 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
2931 0x00, /* Flags (8) */
2932 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2933 };
2934 /* clang-format on */
2935
2936 /* Decode */
2937 ASSERT_H2ERR_ERROR(
2938 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2939
2940 return AWS_OP_SUCCESS;
2941 }
2942
2943 /* Should fail because we're sending something different from (and shorter than) "client connection preface string" */
2944 H2_DECODER_ON_SERVER_PREFACE_TEST(h2_decoder_err_bad_preface_from_client_2) {
2945 (void)allocator;
2946 struct fixture *fixture = ctx;
2947
2948 /* This is the shortest valid HTTP query I can come up with */
2949 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("GET / HTTP/1.0\r\n\r\n");
2950
2951 /* Decode */
2952 ASSERT_H2ERR_ERROR(AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, input));
2953
2954 return AWS_OP_SUCCESS;
2955 }
2956
2957 /* Should fail because we're not sending SETTINGS as the first frame */
2958 H2_DECODER_ON_SERVER_PREFACE_TEST(h2_decoder_err_bad_preface_from_client_3) {
2959 (void)allocator;
2960 struct fixture *fixture = ctx;
2961
2962 /* clang-format off */
2963 uint8_t input[] = {
2964 /* Client connection preface string */
2965 'P','R','I',' ','*',' ','H','T','T','P','/','2','.','0','\r','\n','\r','\n','S','M','\r','\n','\r','\n',
2966
2967 /* PING FRAME - but should be SETTINGS */
2968 0x00, 0x00, 0x08, /* Length (24) */
2969 AWS_H2_FRAME_T_PING, /* Type (8) */
2970 0x0, /* Flags (8) */
2971 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
2972 /* Payload */
2973 'p', 'i', 'n', 'g', 'p', 'o', 'n', 'g' /* Opaque Data (64) */
2974 };
2975 /* clang-format on */
2976
2977 /* Decode */
2978 ASSERT_H2ERR_ERROR(
2979 AWS_HTTP2_ERR_PROTOCOL_ERROR, s_decode_all(fixture, aws_byte_cursor_from_array(input, sizeof(input))));
2980
2981 return AWS_OP_SUCCESS;
2982 }