Debian Codegraph @

crt/aws-c-http/tests/test_h2_encoder.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 #include <aws/testing/aws_test_harness.h>
7
8 #include <aws/http/private/h2_frames.h>
9 #include <aws/io/stream.h>
10
11 static int s_fixture_init(struct aws_allocator *allocator, void *ctx) {
12 (void)ctx;
13 aws_http_library_init(allocator);
14 return AWS_OP_SUCCESS;
15 }
16
17 static int s_fixture_clean_up(struct aws_allocator *allocator, int setup_res, void *ctx) {
18 (void)allocator;
19 (void)ctx;
20 (void)setup_res;
21 aws_http_library_clean_up();
22 return AWS_OP_SUCCESS;
23 }
24
25 #define TEST_CASE(NAME) \
26 AWS_TEST_CASE_FIXTURE(NAME, s_fixture_init, s_test_##NAME, s_fixture_clean_up, NULL); \
27 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
28
29 #define DEFINE_STATIC_HEADER(_key, _value, _behavior) \
30 { \
31 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_key), \
32 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \
33 .compression = AWS_HTTP_HEADER_COMPRESSION_##_behavior, \
34 }
35
36 /* Run the given frame's encoder and check that it outputs the expected bytes */
37 static int s_encode_frame(
38 struct aws_allocator *allocator,
39 struct aws_h2_frame *frame,
40 const uint8_t *expected,
41 size_t expected_size) {
42
43 struct aws_h2_frame_encoder encoder;
44 ASSERT_SUCCESS(aws_h2_frame_encoder_init(&encoder, allocator, NULL /*logging_id*/));
45
46 struct aws_byte_buf buffer;
47
48 /* Allocate more room than necessary, easier to debug the full output than a failed aws_h2_encode_frame() call */
49 ASSERT_SUCCESS(aws_byte_buf_init(&buffer, allocator, expected_size * 2));
50
51 bool frame_complete;
52 ASSERT_SUCCESS(aws_h2_encode_frame(&encoder, frame, &buffer, &frame_complete));
53 ASSERT_BIN_ARRAYS_EQUALS(expected, expected_size, buffer.buffer, buffer.len);
54 ASSERT_UINT_EQUALS(true, frame_complete);
55
56 aws_byte_buf_clean_up(&buffer);
57 aws_h2_frame_encoder_clean_up(&encoder);
58 return AWS_OP_SUCCESS;
59 }
60
61 TEST_CASE(h2_encoder_data) {
62 (void)ctx;
63
64 struct aws_h2_frame_encoder encoder;
65 ASSERT_SUCCESS(aws_h2_frame_encoder_init(&encoder, allocator, NULL /*logging_id*/));
66
67 struct aws_byte_buf output;
68 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 1024));
69
70 struct aws_byte_cursor body_src = aws_byte_cursor_from_c_str("hello");
71 struct aws_input_stream *body = aws_input_stream_new_from_cursor(allocator, &body_src);
72 ASSERT_NOT_NULL(body);
73
74 /* clang-format off */
75 uint8_t expected[] = {
76 0x00, 0x00, 0x08, /* Length (24) */
77 AWS_H2_FRAME_T_DATA, /* Type (8) */
78 AWS_H2_FRAME_F_END_STREAM | AWS_H2_FRAME_F_PADDED, /* Flags (8) */
79 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
80 /* DATA */
81 0x02, /* Pad Length (8) - F_PADDED */
82 'h', 'e', 'l', 'l', 'o', /* Data (*) */
83 0x00, 0x00 /* Padding (*) - F_PADDED */
84 };
85 /* clang-format on */
86
87 bool body_complete;
88 bool body_stalled;
89 bool body_failed;
90 int32_t stream_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX;
91 uint32_t connection_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX;
92 ASSERT_SUCCESS(aws_h2_encode_data_frame(
93 &encoder,
94 0x76543210 /*stream_id*/,
95 body,
96 true /*body_ends_stream*/,
97 2 /*pad_length*/,
98 &stream_window_size_peer,
99 &connection_window_size_peer,
100 &output,
101 &body_complete,
102 &body_stalled,
103 &body_failed));
104
105 ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), output.buffer, output.len);
106 ASSERT_TRUE(body_complete);
107 ASSERT_FALSE(body_stalled);
108 ASSERT_FALSE(body_failed);
109
110 aws_byte_buf_clean_up(&output);
111 aws_input_stream_release(body);
112 aws_h2_frame_encoder_clean_up(&encoder);
113 return AWS_OP_SUCCESS;
114 }
115
116 /* Test that we set body_stalled to true if the aws_input_stream is unable to fill the available space */
117 TEST_CASE(h2_encoder_data_stalled) {
118 (void)ctx;
119
120 struct aws_h2_frame_encoder encoder;
121 ASSERT_SUCCESS(aws_h2_frame_encoder_init(&encoder, allocator, NULL /*logging_id*/));
122
123 struct aws_byte_buf output;
124 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 1024));
125
126 struct aws_byte_cursor body_src = aws_byte_cursor_from_c_str("hello");
127 struct aws_input_stream *body = aws_input_stream_new_tester(allocator, body_src);
128 ASSERT_NOT_NULL(body);
129
130 /* Run encoder where body produces only 1 byte */
131 aws_input_stream_tester_set_max_bytes_per_read(body, 1);
132
133 /* clang-format off */
134 uint8_t expected[] = {
135 0x00, 0x00, 0x01, /* Length (24) */
136 AWS_H2_FRAME_T_DATA, /* Type (8) */
137 0x0, /* Flags (8) */
138 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
139 /* DATA */
140 'h', /* Data (*) */
141 };
142 /* clang-format on */
143
144 bool body_complete;
145 bool body_stalled;
146 bool body_failed;
147 int32_t stream_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX;
148 uint32_t connection_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX;
149 ASSERT_SUCCESS(aws_h2_encode_data_frame(
150 &encoder,
151 0x76543210 /*stream_id*/,
152 body,
153 true /*body_ends_stream*/,
154 0 /*pad_length*/,
155 &stream_window_size_peer,
156 &connection_window_size_peer,
157 &output,
158 &body_complete,
159 &body_stalled,
160 &body_failed));
161
162 ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), output.buffer, output.len);
163 ASSERT_FALSE(body_complete);
164 ASSERT_TRUE(body_stalled);
165 ASSERT_FALSE(body_failed);
166
167 aws_byte_buf_clean_up(&output);
168 aws_input_stream_release(body);
169 aws_h2_frame_encoder_clean_up(&encoder);
170 return AWS_OP_SUCCESS;
171 }
172
173 /* Run encoder where body produces zero bytes. The encoder should not even bother writing a frame. */
174 TEST_CASE(h2_encoder_data_stalled_completely) {
175 (void)ctx;
176
177 struct aws_h2_frame_encoder encoder;
178 ASSERT_SUCCESS(aws_h2_frame_encoder_init(&encoder, allocator, NULL /*logging_id*/));
179
180 struct aws_byte_buf output;
181 ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 1024));
182
183 struct aws_byte_cursor body_src = aws_byte_cursor_from_c_str("hello");
184 struct aws_input_stream *body = aws_input_stream_new_tester(allocator, body_src);
185 ASSERT_NOT_NULL(body);
186
187 aws_input_stream_tester_set_max_bytes_per_read(body, 0);
188
189 bool body_complete;
190 bool body_stalled;
191 bool body_failed;
192 int32_t stream_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX;
193 uint32_t connection_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX;
194 ASSERT_SUCCESS(aws_h2_encode_data_frame(
195 &encoder,
196 0x76543210 /*stream_id*/,
197 body,
198 true /*body_ends_stream*/,
199 0 /*pad_length*/,
200 &stream_window_size_peer,
201 &connection_window_size_peer,
202 &output,
203 &body_complete,
204 &body_stalled,
205 &body_failed));
206
207 ASSERT_FALSE(body_complete);
208 ASSERT_TRUE(body_stalled);
209 ASSERT_FALSE(body_failed);
210 ASSERT_UINT_EQUALS(0, output.len);
211
212 /* clean up */
213 aws_byte_buf_clean_up(&output);
214 aws_input_stream_release(body);
215 aws_h2_frame_encoder_clean_up(&encoder);
216 return AWS_OP_SUCCESS;
217 }
218
219 TEST_CASE(h2_encoder_headers) {
220 (void)ctx;
221
222 struct aws_http_headers *headers = aws_http_headers_new(allocator);
223 ASSERT_NOT_NULL(headers);
224
225 struct aws_http_header h = DEFINE_STATIC_HEADER(":status", "302", USE_CACHE);
226
227 ASSERT_SUCCESS(aws_http_headers_add_header(headers, &h));
228
229 struct aws_h2_frame_priority_settings priority = {
230 .stream_dependency_exclusive = true,
231 .stream_dependency = 0x01234567,
232 .weight = 9,
233 };
234
235 struct aws_h2_frame *frame = aws_h2_frame_new_headers(
236 allocator, 0x76543210 /*stream_id*/, headers, true /*end_stream*/, 2 /*pad_length*/, &priority);
237 ASSERT_NOT_NULL(frame);
238
239 /* clang-format off */
240 uint8_t expected[] = {
241 0x00, 0x00, 12, /* Length (24) */
242 AWS_H2_FRAME_T_HEADERS, /* Type (8) */
243 AWS_H2_FRAME_F_END_STREAM | AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_PADDED | AWS_H2_FRAME_F_PRIORITY, /* Flags (8) */
244 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
245 /* HEADERS */
246 0x02, /* Pad Length (8) - F_PADDED */
247 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) - F_PRIORITY*/
248 0x09, /* Weight (8) - F_PRIORITY */
249 0x48, 0x82, 0x64, 0x02, /* ":status: 302" - indexed name, huffman-compressed value */
250 0x00, 0x00 /* Padding (*) - F_PADDED */
251 };
252 /* clang-format on */
253
254 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
255 aws_h2_frame_destroy(frame);
256 aws_http_headers_release(headers);
257 return AWS_OP_SUCCESS;
258 }
259
260 TEST_CASE(h2_encoder_priority) {
261 (void)ctx;
262
263 struct aws_h2_frame_priority_settings priority = {
264 .stream_dependency_exclusive = true,
265 .stream_dependency = 0x01234567,
266 .weight = 9,
267 };
268
269 struct aws_h2_frame *frame = aws_h2_frame_new_priority(allocator, 0x76543210 /*stream_id*/, &priority);
270 ASSERT_NOT_NULL(frame);
271
272 /* clang-format off */
273 uint8_t expected[] = {
274 0x00, 0x00, 0x05, /* Length (24) */
275 AWS_H2_FRAME_T_PRIORITY, /* Type (8) */
276 0x0, /* Flags (8) */
277 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
278 /* PRIORITY */
279 0x81, 0x23, 0x45, 0x67, /* Exclusive (1) | Stream Dependency (31) */
280 0x09, /* Weight (8) */
281 };
282 /* clang-format on */
283
284 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
285 aws_h2_frame_destroy(frame);
286 return AWS_OP_SUCCESS;
287 }
288
289 TEST_CASE(h2_encoder_rst_stream) {
290 (void)ctx;
291
292 struct aws_h2_frame *frame =
293 aws_h2_frame_new_rst_stream(allocator, 0x76543210 /*stream_id*/, 0xFEEDBEEF /*error_code*/);
294 ASSERT_NOT_NULL(frame);
295
296 /* clang-format off */
297 uint8_t expected[] = {
298 0x00, 0x00, 0x04, /* Length (24) */
299 AWS_H2_FRAME_T_RST_STREAM, /* Type (8) */
300 0x0, /* Flags (8) */
301 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
302 /* RST_STREAM */
303 0xFE, 0xED, 0xBE, 0xEF, /* Error Code (32) */
304 };
305 /* clang-format on */
306
307 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
308 aws_h2_frame_destroy(frame);
309 return AWS_OP_SUCCESS;
310 }
311
312 TEST_CASE(h2_encoder_settings) {
313 (void)ctx;
314
315 struct aws_http2_setting settings[] = {
316 {.id = AWS_HTTP2_SETTINGS_ENABLE_PUSH, .value = 1}, /* real world value */
317 {.id = 0x0000, .value = 0x00000000}, /* min value */
318 {.id = 0xFFFF, .value = 0xFFFFFFFF}, /* max value */
319 };
320
321 struct aws_h2_frame *frame =
322 aws_h2_frame_new_settings(allocator, settings, AWS_ARRAY_SIZE(settings), false /*ack*/);
323 ASSERT_NOT_NULL(frame);
324
325 /* clang-format off */
326 uint8_t expected[] = {
327 0x00, 0x00, 18, /* Length (24) */
328 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
329 0x0, /* Flags (8) */
330 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
331 /* SETTINGS */
332 0x00, 0x02, /* Identifier (16) */
333 0x00, 0x00, 0x00, 0x01, /* Value (32) */
334 0x00, 0x00, /* Identifier (16) */
335 0x00, 0x00, 0x00, 0x00, /* Value (32) */
336 0xFF, 0xFF, /* Identifier (16) */
337 0xFF, 0xFF, 0xFF, 0xFF, /* Value (32) */
338 };
339 /* clang-format on */
340
341 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
342 aws_h2_frame_destroy(frame);
343 return AWS_OP_SUCCESS;
344 }
345
346 TEST_CASE(h2_encoder_settings_ack) {
347 (void)ctx;
348
349 struct aws_h2_frame *frame =
350 aws_h2_frame_new_settings(allocator, NULL /*settings_array*/, 0 /*num_settings*/, true /*ack*/);
351 ASSERT_NOT_NULL(frame);
352
353 /* clang-format off */
354 uint8_t expected[] = {
355 0x00, 0x00, 0x00, /* Length (24) */
356 AWS_H2_FRAME_T_SETTINGS, /* Type (8) */
357 AWS_H2_FRAME_F_ACK, /* Flags (8) */
358 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
359 /* SETTINGS */
360 };
361 /* clang-format on */
362
363 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
364 aws_h2_frame_destroy(frame);
365 return AWS_OP_SUCCESS;
366 }
367
368 TEST_CASE(h2_encoder_push_promise) {
369 (void)ctx;
370
371 struct aws_http_header headers_array[] = {
372 DEFINE_STATIC_HEADER(":method", "GET", USE_CACHE),
373 DEFINE_STATIC_HEADER(":scheme", "http", USE_CACHE),
374 DEFINE_STATIC_HEADER(":path", "/", USE_CACHE),
375 DEFINE_STATIC_HEADER(":authority", "www.example.com", USE_CACHE),
376 };
377 struct aws_http_headers *headers = aws_http_headers_new(allocator);
378 ASSERT_NOT_NULL(headers);
379 ASSERT_SUCCESS(aws_http_headers_add_array(headers, headers_array, AWS_ARRAY_SIZE(headers_array)));
380
381 struct aws_h2_frame *frame = aws_h2_frame_new_push_promise(
382 allocator, 0x00000001 /*stream_id*/, 0x76543210 /*promised_stream_id*/, headers, 2 /*pad_length*/);
383 ASSERT_NOT_NULL(frame);
384
385 /* clang-format off */
386 uint8_t expected[] = {
387 0x00, 0x00, 24, /* Length (24) */
388 AWS_H2_FRAME_T_PUSH_PROMISE,/* Type (8) */
389 AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_PADDED, /* Flags (8) */
390 0x00, 0x00, 0x00, 0x01, /* Reserved (1) | Stream Identifier (31) */
391 /* PUSH_PROMISE */
392 0x02, /* Pad Length (8) | F_PADDED */
393 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Promised Stream ID (31) */
394
395 /* Header Block Fragment (*) (values from RFC-7541 example C.4.1) */
396 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
397
398 0x00, 0x00, /* Padding (*) | F_PADDED*/
399 };
400 /* clang-format on */
401
402 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
403 aws_h2_frame_destroy(frame);
404 aws_http_headers_release(headers);
405 return AWS_OP_SUCCESS;
406 }
407
408 TEST_CASE(h2_encoder_ping) {
409 (void)ctx;
410
411 uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7};
412
413 struct aws_h2_frame *frame = aws_h2_frame_new_ping(allocator, true /*ack*/, opaque_data);
414 ASSERT_NOT_NULL(frame);
415
416 /* clang-format off */
417 uint8_t expected[] = {
418 0x00, 0x00, 0x08, /* Length (24) */
419 AWS_H2_FRAME_T_PING, /* Type (8) */
420 AWS_H2_FRAME_F_ACK, /* Flags (8) */
421 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
422 /* PING */
423 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Opaque Data (64) */
424 };
425 /* clang-format on */
426
427 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
428 aws_h2_frame_destroy(frame);
429 return AWS_OP_SUCCESS;
430 }
431
432 TEST_CASE(h2_encoder_goaway) {
433 (void)ctx;
434
435 struct aws_h2_frame *frame = aws_h2_frame_new_goaway(
436 allocator,
437 0x77665544 /*last_stream_id*/,
438 0xFFEEDDCC /*error_code*/,
439 aws_byte_cursor_from_c_str("goodbye") /*debug_data*/);
440 ASSERT_NOT_NULL(frame);
441
442 /* clang-format off */
443 uint8_t expected[] = {
444 0x00, 0x00, 15, /* Length (24) */
445 AWS_H2_FRAME_T_GOAWAY, /* Type (8) */
446 0x0, /* Flags (8) */
447 0x00, 0x00, 0x00, 0x00, /* Reserved (1) | Stream Identifier (31) */
448 /* GOAWAY */
449 0x77, 0x66, 0x55, 0x44, /* Reserved (1) | Last-Stream-ID (31) */
450 0xFF, 0xEE, 0xDD, 0xCC, /* Error Code (32) */
451 'g','o','o','d','b','y','e',/* Additional Debug Data (*) */
452 };
453 /* clang-format on */
454
455 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
456 aws_h2_frame_destroy(frame);
457 return AWS_OP_SUCCESS;
458 }
459
460 TEST_CASE(h2_encoder_window_update) {
461 (void)ctx;
462
463 struct aws_h2_frame *frame =
464 aws_h2_frame_new_window_update(allocator, 0x76543210 /*stream_id*/, 0x7FFFFFFF /*window_size_increment*/);
465 ASSERT_NOT_NULL(frame);
466
467 /* clang-format off */
468 uint8_t expected[] = {
469 0x00, 0x00, 0x04, /* Length (24) */
470 AWS_H2_FRAME_T_WINDOW_UPDATE,/* Type (8) */
471 0x0, /* Flags (8) */
472 0x76, 0x54, 0x32, 0x10, /* Reserved (1) | Stream Identifier (31) */
473 /* WINDOW_UPDATE */
474 0x7F, 0xFF, 0xFF, 0xFF, /* Window Size Increment (31) */
475 };
476 /* clang-format on */
477
478 ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected)));
479 aws_h2_frame_destroy(frame);
480 return AWS_OP_SUCCESS;
481 }