| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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 )); |
| 45 |
|
| 46 |
struct aws_byte_buf buffer; |
| 47 |
|
| 48 |
|
| 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 )); |
| 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 |
|
| 75 |
uint8_t expected[] = { |
| 76 |
0x00, 0x00, 0x08, |
| 77 |
AWS_H2_FRAME_T_DATA, |
| 78 |
AWS_H2_FRAME_F_END_STREAM | AWS_H2_FRAME_F_PADDED, |
| 79 |
0x76, 0x54, 0x32, 0x10, |
| 80 |
|
| 81 |
0x02, |
| 82 |
'h', 'e', 'l', 'l', 'o', |
| 83 |
0x00, 0x00 |
| 84 |
}; |
| 85 |
|
| 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 , |
| 95 |
body, |
| 96 |
true , |
| 97 |
2 , |
| 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 |
|
| 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 )); |
| 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 |
|
| 131 |
aws_input_stream_tester_set_max_bytes_per_read(body, 1); |
| 132 |
|
| 133 |
|
| 134 |
uint8_t expected[] = { |
| 135 |
0x00, 0x00, 0x01, |
| 136 |
AWS_H2_FRAME_T_DATA, |
| 137 |
0x0, |
| 138 |
0x76, 0x54, 0x32, 0x10, |
| 139 |
|
| 140 |
'h', |
| 141 |
}; |
| 142 |
|
| 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 , |
| 152 |
body, |
| 153 |
true , |
| 154 |
0 , |
| 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 |
|
| 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 )); |
| 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 , |
| 197 |
body, |
| 198 |
true , |
| 199 |
0 , |
| 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 |
|
| 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 , headers, true , 2 , &priority); |
| 237 |
ASSERT_NOT_NULL(frame); |
| 238 |
|
| 239 |
|
| 240 |
uint8_t expected[] = { |
| 241 |
0x00, 0x00, 12, |
| 242 |
AWS_H2_FRAME_T_HEADERS, |
| 243 |
AWS_H2_FRAME_F_END_STREAM | AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_PADDED | AWS_H2_FRAME_F_PRIORITY, |
| 244 |
0x76, 0x54, 0x32, 0x10, |
| 245 |
|
| 246 |
0x02, |
| 247 |
0x81, 0x23, 0x45, 0x67, |
| 248 |
0x09, |
| 249 |
0x48, 0x82, 0x64, 0x02, |
| 250 |
0x00, 0x00 |
| 251 |
}; |
| 252 |
|
| 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 , &priority); |
| 270 |
ASSERT_NOT_NULL(frame); |
| 271 |
|
| 272 |
|
| 273 |
uint8_t expected[] = { |
| 274 |
0x00, 0x00, 0x05, |
| 275 |
AWS_H2_FRAME_T_PRIORITY, |
| 276 |
0x0, |
| 277 |
0x76, 0x54, 0x32, 0x10, |
| 278 |
|
| 279 |
0x81, 0x23, 0x45, 0x67, |
| 280 |
0x09, |
| 281 |
}; |
| 282 |
|
| 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 , 0xFEEDBEEF ); |
| 294 |
ASSERT_NOT_NULL(frame); |
| 295 |
|
| 296 |
|
| 297 |
uint8_t expected[] = { |
| 298 |
0x00, 0x00, 0x04, |
| 299 |
AWS_H2_FRAME_T_RST_STREAM, |
| 300 |
0x0, |
| 301 |
0x76, 0x54, 0x32, 0x10, |
| 302 |
|
| 303 |
0xFE, 0xED, 0xBE, 0xEF, |
| 304 |
}; |
| 305 |
|
| 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}, |
| 317 |
{.id = 0x0000, .value = 0x00000000}, |
| 318 |
{.id = 0xFFFF, .value = 0xFFFFFFFF}, |
| 319 |
}; |
| 320 |
|
| 321 |
struct aws_h2_frame *frame = |
| 322 |
aws_h2_frame_new_settings(allocator, settings, AWS_ARRAY_SIZE(settings), false ); |
| 323 |
ASSERT_NOT_NULL(frame); |
| 324 |
|
| 325 |
|
| 326 |
uint8_t expected[] = { |
| 327 |
0x00, 0x00, 18, |
| 328 |
AWS_H2_FRAME_T_SETTINGS, |
| 329 |
0x0, |
| 330 |
0x00, 0x00, 0x00, 0x00, |
| 331 |
|
| 332 |
0x00, 0x02, |
| 333 |
0x00, 0x00, 0x00, 0x01, |
| 334 |
0x00, 0x00, |
| 335 |
0x00, 0x00, 0x00, 0x00, |
| 336 |
0xFF, 0xFF, |
| 337 |
0xFF, 0xFF, 0xFF, 0xFF, |
| 338 |
}; |
| 339 |
|
| 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 , 0 , true ); |
| 351 |
ASSERT_NOT_NULL(frame); |
| 352 |
|
| 353 |
|
| 354 |
uint8_t expected[] = { |
| 355 |
0x00, 0x00, 0x00, |
| 356 |
AWS_H2_FRAME_T_SETTINGS, |
| 357 |
AWS_H2_FRAME_F_ACK, |
| 358 |
0x00, 0x00, 0x00, 0x00, |
| 359 |
|
| 360 |
}; |
| 361 |
|
| 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 , 0x76543210 , headers, 2 ); |
| 383 |
ASSERT_NOT_NULL(frame); |
| 384 |
|
| 385 |
|
| 386 |
uint8_t expected[] = { |
| 387 |
0x00, 0x00, 24, |
| 388 |
AWS_H2_FRAME_T_PUSH_PROMISE, |
| 389 |
AWS_H2_FRAME_F_END_HEADERS | AWS_H2_FRAME_F_PADDED, |
| 390 |
0x00, 0x00, 0x00, 0x01, |
| 391 |
|
| 392 |
0x02, |
| 393 |
0x76, 0x54, 0x32, 0x10, |
| 394 |
|
| 395 |
|
| 396 |
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, |
| 397 |
|
| 398 |
0x00, 0x00, |
| 399 |
}; |
| 400 |
|
| 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 , opaque_data); |
| 414 |
ASSERT_NOT_NULL(frame); |
| 415 |
|
| 416 |
|
| 417 |
uint8_t expected[] = { |
| 418 |
0x00, 0x00, 0x08, |
| 419 |
AWS_H2_FRAME_T_PING, |
| 420 |
AWS_H2_FRAME_F_ACK, |
| 421 |
0x00, 0x00, 0x00, 0x00, |
| 422 |
|
| 423 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 424 |
}; |
| 425 |
|
| 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 , |
| 438 |
0xFFEEDDCC , |
| 439 |
aws_byte_cursor_from_c_str("goodbye") ); |
| 440 |
ASSERT_NOT_NULL(frame); |
| 441 |
|
| 442 |
|
| 443 |
uint8_t expected[] = { |
| 444 |
0x00, 0x00, 15, |
| 445 |
AWS_H2_FRAME_T_GOAWAY, |
| 446 |
0x0, |
| 447 |
0x00, 0x00, 0x00, 0x00, |
| 448 |
|
| 449 |
0x77, 0x66, 0x55, 0x44, |
| 450 |
0xFF, 0xEE, 0xDD, 0xCC, |
| 451 |
'g','o','o','d','b','y','e', |
| 452 |
}; |
| 453 |
|
| 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 , 0x7FFFFFFF ); |
| 465 |
ASSERT_NOT_NULL(frame); |
| 466 |
|
| 467 |
|
| 468 |
uint8_t expected[] = { |
| 469 |
0x00, 0x00, 0x04, |
| 470 |
AWS_H2_FRAME_T_WINDOW_UPDATE, |
| 471 |
0x0, |
| 472 |
0x76, 0x54, 0x32, 0x10, |
| 473 |
|
| 474 |
0x7F, 0xFF, 0xFF, 0xFF, |
| 475 |
}; |
| 476 |
|
| 477 |
|
| 478 |
ASSERT_SUCCESS(s_encode_frame(allocator, frame, expected, sizeof(expected))); |
| 479 |
aws_h2_frame_destroy(frame); |
| 480 |
return AWS_OP_SUCCESS; |
| 481 |
} |