| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "h2_test_helper.h" |
| 7 |
|
| 8 |
#include <aws/http/private/h2_decoder.h> |
| 9 |
#include <aws/io/stream.h> |
| 10 |
#include <aws/testing/io_testing_channel.h> |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
static void s_frame_init( |
| 16 |
struct h2_decoded_frame *frame, |
| 17 |
struct aws_allocator *alloc, |
| 18 |
enum aws_h2_frame_type type, |
| 19 |
uint32_t stream_id) { |
| 20 |
|
| 21 |
AWS_ZERO_STRUCT(*frame); |
| 22 |
frame->type = type; |
| 23 |
frame->stream_id = stream_id; |
| 24 |
frame->headers = aws_http_headers_new(alloc); |
| 25 |
AWS_FATAL_ASSERT(0 == aws_array_list_init_dynamic(&frame->settings, alloc, 16, sizeof(struct aws_http2_setting))); |
| 26 |
AWS_FATAL_ASSERT(0 == aws_byte_buf_init(&frame->data, alloc, 1024)); |
| 27 |
} |
| 28 |
|
| 29 |
static void s_frame_clean_up(struct h2_decoded_frame *frame) { |
| 30 |
if (!frame) { |
| 31 |
return; |
| 32 |
} |
| 33 |
aws_http_headers_release(frame->headers); |
| 34 |
aws_array_list_clean_up(&frame->settings); |
| 35 |
aws_byte_buf_clean_up(&frame->data); |
| 36 |
} |
| 37 |
|
| 38 |
int h2_decoded_frame_check_finished( |
| 39 |
const struct h2_decoded_frame *frame, |
| 40 |
enum aws_h2_frame_type expected_type, |
| 41 |
uint32_t expected_stream_id) { |
| 42 |
|
| 43 |
ASSERT_INT_EQUALS(expected_type, frame->type); |
| 44 |
ASSERT_UINT_EQUALS(expected_stream_id, frame->stream_id); |
| 45 |
ASSERT_TRUE(frame->finished); |
| 46 |
return AWS_OP_SUCCESS; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
size_t h2_decode_tester_frame_count(const struct h2_decode_tester *decode_tester) { |
| 54 |
return aws_array_list_length(&decode_tester->frames); |
| 55 |
} |
| 56 |
|
| 57 |
struct h2_decoded_frame *h2_decode_tester_get_frame(const struct h2_decode_tester *decode_tester, size_t i) { |
| 58 |
AWS_FATAL_ASSERT(h2_decode_tester_frame_count(decode_tester) > i); |
| 59 |
struct h2_decoded_frame *frame = NULL; |
| 60 |
aws_array_list_get_at_ptr(&decode_tester->frames, (void **)&frame, i); |
| 61 |
return frame; |
| 62 |
} |
| 63 |
|
| 64 |
struct h2_decoded_frame *h2_decode_tester_latest_frame(const struct h2_decode_tester *decode_tester) { |
| 65 |
size_t frame_count = h2_decode_tester_frame_count(decode_tester); |
| 66 |
AWS_FATAL_ASSERT(frame_count != 0); |
| 67 |
return h2_decode_tester_get_frame(decode_tester, frame_count - 1); |
| 68 |
} |
| 69 |
|
| 70 |
struct h2_decoded_frame *h2_decode_tester_find_frame( |
| 71 |
const struct h2_decode_tester *decode_tester, |
| 72 |
enum aws_h2_frame_type type, |
| 73 |
size_t search_start_idx, |
| 74 |
size_t *out_idx) { |
| 75 |
|
| 76 |
return h2_decode_tester_find_stream_frame(decode_tester, type, UINT32_MAX , search_start_idx, out_idx); |
| 77 |
} |
| 78 |
|
| 79 |
struct h2_decoded_frame *h2_decode_tester_find_stream_frame_any_type( |
| 80 |
const struct h2_decode_tester *decode_tester, |
| 81 |
uint32_t stream_id, |
| 82 |
size_t search_start_idx, |
| 83 |
size_t *out_idx) { |
| 84 |
|
| 85 |
return h2_decode_tester_find_stream_frame( |
| 86 |
decode_tester, AWS_H2_FRAME_TYPE_COUNT , stream_id, search_start_idx, out_idx); |
| 87 |
} |
| 88 |
|
| 89 |
struct h2_decoded_frame *h2_decode_tester_find_stream_frame( |
| 90 |
const struct h2_decode_tester *decode_tester, |
| 91 |
enum aws_h2_frame_type type, |
| 92 |
uint32_t stream_id, |
| 93 |
size_t search_start_idx, |
| 94 |
size_t *out_idx) { |
| 95 |
|
| 96 |
size_t frame_count = h2_decode_tester_frame_count(decode_tester); |
| 97 |
if (out_idx) { |
| 98 |
*out_idx = frame_count; |
| 99 |
} |
| 100 |
|
| 101 |
for (size_t i = search_start_idx; i < frame_count; ++i) { |
| 102 |
struct h2_decoded_frame *frame = h2_decode_tester_get_frame(decode_tester, i); |
| 103 |
if (frame->type == type || type == AWS_H2_FRAME_TYPE_COUNT) { |
| 104 |
if (frame->stream_id == stream_id || stream_id == UINT32_MAX) { |
| 105 |
if (out_idx) { |
| 106 |
*out_idx = i; |
| 107 |
} |
| 108 |
return frame; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
int h2_decode_tester_check_data_across_frames( |
| 116 |
const struct h2_decode_tester *decode_tester, |
| 117 |
uint32_t stream_id, |
| 118 |
struct aws_byte_cursor expected, |
| 119 |
bool expect_end_stream) { |
| 120 |
|
| 121 |
struct aws_byte_buf data; |
| 122 |
ASSERT_SUCCESS(aws_byte_buf_init(&data, decode_tester->alloc, 128)); |
| 123 |
|
| 124 |
bool found_end_stream = false; |
| 125 |
|
| 126 |
for (size_t frame_i = 0; frame_i < h2_decode_tester_frame_count(decode_tester); ++frame_i) { |
| 127 |
struct h2_decoded_frame *frame = h2_decode_tester_get_frame(decode_tester, frame_i); |
| 128 |
|
| 129 |
if (frame->type == AWS_H2_FRAME_T_DATA && frame->stream_id == stream_id) { |
| 130 |
struct aws_byte_cursor frame_data = aws_byte_cursor_from_buf(&frame->data); |
| 131 |
ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&data, &frame_data)); |
| 132 |
|
| 133 |
found_end_stream = frame->end_stream; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
ASSERT_BIN_ARRAYS_EQUALS(expected.ptr, expected.len, data.buffer, data.len); |
| 138 |
ASSERT_UINT_EQUALS(expect_end_stream, found_end_stream); |
| 139 |
|
| 140 |
aws_byte_buf_clean_up(&data); |
| 141 |
return AWS_OP_SUCCESS; |
| 142 |
} |
| 143 |
|
| 144 |
int h2_decode_tester_check_data_str_across_frames( |
| 145 |
const struct h2_decode_tester *decode_tester, |
| 146 |
uint32_t stream_id, |
| 147 |
const char *expected, |
| 148 |
bool expect_end_stream) { |
| 149 |
|
| 150 |
return h2_decode_tester_check_data_across_frames( |
| 151 |
decode_tester, stream_id, aws_byte_cursor_from_c_str(expected), expect_end_stream); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
static void s_begin_new_frame( |
| 156 |
struct h2_decode_tester *decode_tester, |
| 157 |
enum aws_h2_frame_type type, |
| 158 |
uint32_t stream_id, |
| 159 |
struct h2_decoded_frame **out_frame) { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if (aws_array_list_length(&decode_tester->frames) > 0) { |
| 165 |
const struct h2_decoded_frame *prev_frame = h2_decode_tester_latest_frame(decode_tester); |
| 166 |
AWS_FATAL_ASSERT(prev_frame->finished); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
struct h2_decoded_frame new_frame; |
| 171 |
s_frame_init(&new_frame, decode_tester->alloc, type, stream_id); |
| 172 |
AWS_FATAL_ASSERT(0 == aws_array_list_push_back(&decode_tester->frames, &new_frame)); |
| 173 |
|
| 174 |
if (out_frame) { |
| 175 |
aws_array_list_get_at_ptr( |
| 176 |
&decode_tester->frames, (void **)out_frame, aws_array_list_length(&decode_tester->frames) - 1); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
static void s_end_current_frame( |
| 182 |
struct h2_decode_tester *decode_tester, |
| 183 |
enum aws_h2_frame_type type, |
| 184 |
uint32_t stream_id) { |
| 185 |
struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(decode_tester); |
| 186 |
AWS_FATAL_ASSERT(!frame->finished); |
| 187 |
frame->finished = true; |
| 188 |
AWS_FATAL_ASSERT(0 == h2_decoded_frame_check_finished(frame, type, stream_id)); |
| 189 |
} |
| 190 |
|
| 191 |
static struct aws_h2err s_decoder_on_headers_begin(uint32_t stream_id, void *userdata) { |
| 192 |
struct h2_decode_tester *decode_tester = userdata; |
| 193 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_HEADERS, stream_id, NULL ); |
| 194 |
return AWS_H2ERR_SUCCESS; |
| 195 |
} |
| 196 |
|
| 197 |
static struct aws_h2err s_on_header( |
| 198 |
bool is_push_promise, |
| 199 |
uint32_t stream_id, |
| 200 |
const struct aws_http_header *header, |
| 201 |
enum aws_http_header_name name_enum, |
| 202 |
enum aws_http_header_block block_type, |
| 203 |
void *userdata) { |
| 204 |
|
| 205 |
struct h2_decode_tester *decode_tester = userdata; |
| 206 |
struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(decode_tester); |
| 207 |
|
| 208 |
|
| 209 |
if (is_push_promise) { |
| 210 |
AWS_FATAL_ASSERT(AWS_H2_FRAME_T_PUSH_PROMISE == frame->type); |
| 211 |
} else { |
| 212 |
AWS_FATAL_ASSERT(AWS_H2_FRAME_T_HEADERS == frame->type); |
| 213 |
|
| 214 |
|
| 215 |
if (aws_http_headers_count(frame->headers) > 0) { |
| 216 |
AWS_FATAL_ASSERT(frame->header_block_type == block_type); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
AWS_FATAL_ASSERT(!frame->finished); |
| 221 |
AWS_FATAL_ASSERT(frame->stream_id == stream_id); |
| 222 |
AWS_FATAL_ASSERT(aws_http_lowercase_str_to_header_name(header->name) == name_enum); |
| 223 |
|
| 224 |
|
| 225 |
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_headers_add_header(frame->headers, header)); |
| 226 |
frame->header_block_type = block_type; |
| 227 |
|
| 228 |
return AWS_H2ERR_SUCCESS; |
| 229 |
} |
| 230 |
|
| 231 |
static struct aws_h2err s_decoder_on_headers_i( |
| 232 |
uint32_t stream_id, |
| 233 |
const struct aws_http_header *header, |
| 234 |
enum aws_http_header_name name_enum, |
| 235 |
enum aws_http_header_block block_type, |
| 236 |
void *userdata) { |
| 237 |
return s_on_header(false , stream_id, header, name_enum, block_type, userdata); |
| 238 |
} |
| 239 |
|
| 240 |
static struct aws_h2err s_on_headers_end( |
| 241 |
bool is_push_promise, |
| 242 |
uint32_t stream_id, |
| 243 |
bool malformed, |
| 244 |
enum aws_http_header_block block_type, |
| 245 |
void *userdata) { |
| 246 |
|
| 247 |
struct h2_decode_tester *decode_tester = userdata; |
| 248 |
struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(decode_tester); |
| 249 |
|
| 250 |
|
| 251 |
if (!is_push_promise && aws_http_headers_count(frame->headers) > 0) { |
| 252 |
AWS_FATAL_ASSERT(frame->header_block_type == block_type); |
| 253 |
} |
| 254 |
frame->header_block_type = block_type; |
| 255 |
|
| 256 |
frame->headers_malformed = malformed; |
| 257 |
s_end_current_frame( |
| 258 |
decode_tester, is_push_promise ? AWS_H2_FRAME_T_PUSH_PROMISE : AWS_H2_FRAME_T_HEADERS, stream_id); |
| 259 |
return AWS_H2ERR_SUCCESS; |
| 260 |
} |
| 261 |
|
| 262 |
static struct aws_h2err s_decoder_on_headers_end( |
| 263 |
uint32_t stream_id, |
| 264 |
bool malformed, |
| 265 |
enum aws_http_header_block block_type, |
| 266 |
void *userdata) { |
| 267 |
return s_on_headers_end(false , stream_id, malformed, block_type, userdata); |
| 268 |
} |
| 269 |
|
| 270 |
static struct aws_h2err s_decoder_on_push_promise_begin( |
| 271 |
uint32_t stream_id, |
| 272 |
uint32_t promised_stream_id, |
| 273 |
void *userdata) { |
| 274 |
|
| 275 |
struct h2_decode_tester *decode_tester = userdata; |
| 276 |
struct h2_decoded_frame *frame; |
| 277 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_PUSH_PROMISE, stream_id, &frame ); |
| 278 |
|
| 279 |
frame->promised_stream_id = promised_stream_id; |
| 280 |
|
| 281 |
return AWS_H2ERR_SUCCESS; |
| 282 |
} |
| 283 |
|
| 284 |
static struct aws_h2err s_decoder_on_push_promise_i( |
| 285 |
uint32_t stream_id, |
| 286 |
const struct aws_http_header *header, |
| 287 |
enum aws_http_header_name name_enum, |
| 288 |
void *userdata) { |
| 289 |
return s_on_header(true , stream_id, header, name_enum, AWS_HTTP_HEADER_BLOCK_MAIN, userdata); |
| 290 |
} |
| 291 |
|
| 292 |
static struct aws_h2err s_decoder_on_push_promise_end(uint32_t stream_id, bool malformed, void *userdata) { |
| 293 |
return s_on_headers_end(true , stream_id, malformed, AWS_HTTP_HEADER_BLOCK_MAIN, userdata); |
| 294 |
} |
| 295 |
|
| 296 |
static struct aws_h2err s_decoder_on_data_begin( |
| 297 |
uint32_t stream_id, |
| 298 |
uint32_t payload_len, |
| 299 |
uint32_t total_padding_bytes, |
| 300 |
bool end_stream, |
| 301 |
void *userdata) { |
| 302 |
(void)total_padding_bytes; |
| 303 |
struct h2_decode_tester *decode_tester = userdata; |
| 304 |
struct h2_decoded_frame *frame; |
| 305 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_DATA, stream_id, &frame); |
| 306 |
|
| 307 |
frame->data_payload_len = payload_len; |
| 308 |
frame->data_end_stream = end_stream; |
| 309 |
|
| 310 |
return AWS_H2ERR_SUCCESS; |
| 311 |
} |
| 312 |
|
| 313 |
static struct aws_h2err s_decoder_on_data_i(uint32_t stream_id, struct aws_byte_cursor data, void *userdata) { |
| 314 |
(void)stream_id; |
| 315 |
struct h2_decode_tester *decode_tester = userdata; |
| 316 |
struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(decode_tester); |
| 317 |
|
| 318 |
|
| 319 |
AWS_FATAL_ASSERT(AWS_H2_FRAME_T_DATA == frame->type); |
| 320 |
AWS_FATAL_ASSERT(!frame->finished); |
| 321 |
AWS_FATAL_ASSERT(frame->stream_id == stream_id); |
| 322 |
|
| 323 |
|
| 324 |
AWS_FATAL_ASSERT(0 == aws_byte_buf_append_dynamic(&frame->data, &data)); |
| 325 |
|
| 326 |
return AWS_H2ERR_SUCCESS; |
| 327 |
} |
| 328 |
|
| 329 |
static struct aws_h2err s_decoder_on_data_end(uint32_t stream_id, void *userdata) { |
| 330 |
struct h2_decode_tester *decode_tester = userdata; |
| 331 |
|
| 332 |
s_end_current_frame(decode_tester, AWS_H2_FRAME_T_DATA, stream_id); |
| 333 |
struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(decode_tester); |
| 334 |
AWS_FATAL_ASSERT(frame->data.len <= frame->data_payload_len); |
| 335 |
|
| 336 |
return AWS_H2ERR_SUCCESS; |
| 337 |
} |
| 338 |
|
| 339 |
static struct aws_h2err s_decoder_on_end_stream(uint32_t stream_id, void *userdata) { |
| 340 |
struct h2_decode_tester *decode_tester = userdata; |
| 341 |
struct h2_decoded_frame *frame = h2_decode_tester_latest_frame(decode_tester); |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
AWS_FATAL_ASSERT(frame->finished); |
| 348 |
AWS_FATAL_ASSERT(frame->type == AWS_H2_FRAME_T_HEADERS || frame->type == AWS_H2_FRAME_T_DATA); |
| 349 |
AWS_FATAL_ASSERT(frame->stream_id == stream_id); |
| 350 |
|
| 351 |
if (frame->type == AWS_H2_FRAME_T_DATA) { |
| 352 |
AWS_FATAL_ASSERT(frame->data_end_stream); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
frame->end_stream = true; |
| 357 |
|
| 358 |
return AWS_H2ERR_SUCCESS; |
| 359 |
} |
| 360 |
|
| 361 |
static struct aws_h2err s_decoder_on_rst_stream(uint32_t stream_id, uint32_t error_code, void *userdata) { |
| 362 |
struct h2_decode_tester *decode_tester = userdata; |
| 363 |
struct h2_decoded_frame *frame; |
| 364 |
|
| 365 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_RST_STREAM, stream_id, &frame); |
| 366 |
|
| 367 |
|
| 368 |
frame->error_code = error_code; |
| 369 |
|
| 370 |
s_end_current_frame(decode_tester, AWS_H2_FRAME_T_RST_STREAM, stream_id); |
| 371 |
return AWS_H2ERR_SUCCESS; |
| 372 |
} |
| 373 |
|
| 374 |
static struct aws_h2err s_decoder_on_settings( |
| 375 |
const struct aws_http2_setting *settings_array, |
| 376 |
size_t num_settings, |
| 377 |
void *userdata) { |
| 378 |
|
| 379 |
struct h2_decode_tester *decode_tester = userdata; |
| 380 |
struct h2_decoded_frame *frame; |
| 381 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_SETTINGS, 0, &frame); |
| 382 |
|
| 383 |
|
| 384 |
for (size_t i = 0; i < num_settings; i++) { |
| 385 |
AWS_FATAL_ASSERT(0 == aws_array_list_push_back(&frame->settings, &settings_array[i])); |
| 386 |
} |
| 387 |
|
| 388 |
s_end_current_frame(decode_tester, AWS_H2_FRAME_T_SETTINGS, 0); |
| 389 |
return AWS_H2ERR_SUCCESS; |
| 390 |
} |
| 391 |
|
| 392 |
static struct aws_h2err s_decoder_on_settings_ack(void *userdata) { |
| 393 |
struct h2_decode_tester *decode_tester = userdata; |
| 394 |
struct h2_decoded_frame *frame; |
| 395 |
|
| 396 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_SETTINGS, 0 , &frame); |
| 397 |
|
| 398 |
|
| 399 |
frame->ack = true; |
| 400 |
|
| 401 |
s_end_current_frame(decode_tester, AWS_H2_FRAME_T_SETTINGS, 0 ); |
| 402 |
return AWS_H2ERR_SUCCESS; |
| 403 |
} |
| 404 |
|
| 405 |
static struct aws_h2err s_decoder_on_ping(uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE], void *userdata) { |
| 406 |
struct h2_decode_tester *decode_tester = userdata; |
| 407 |
struct h2_decoded_frame *frame; |
| 408 |
|
| 409 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_PING, 0 , &frame); |
| 410 |
|
| 411 |
|
| 412 |
memcpy(frame->ping_opaque_data, opaque_data, AWS_HTTP2_PING_DATA_SIZE); |
| 413 |
|
| 414 |
s_end_current_frame(decode_tester, AWS_H2_FRAME_T_PING, 0 ); |
| 415 |
return AWS_H2ERR_SUCCESS; |
| 416 |
} |
| 417 |
|
| 418 |
static struct aws_h2err s_decoder_on_ping_ack(uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE], void *userdata) { |
| 419 |
struct h2_decode_tester *decode_tester = userdata; |
| 420 |
struct h2_decoded_frame *frame; |
| 421 |
|
| 422 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_PING, 0 , &frame); |
| 423 |
|
| 424 |
|
| 425 |
memcpy(frame->ping_opaque_data, opaque_data, AWS_HTTP2_PING_DATA_SIZE); |
| 426 |
frame->ack = true; |
| 427 |
|
| 428 |
s_end_current_frame(decode_tester, AWS_H2_FRAME_T_PING, 0 ); |
| 429 |
return AWS_H2ERR_SUCCESS; |
| 430 |
} |
| 431 |
|
| 432 |
static struct aws_h2err s_decoder_on_goaway( |
| 433 |
uint32_t last_stream, |
| 434 |
uint32_t error_code, |
| 435 |
struct aws_byte_cursor debug_data, |
| 436 |
void *userdata) { |
| 437 |
|
| 438 |
struct h2_decode_tester *decode_tester = userdata; |
| 439 |
struct h2_decoded_frame *frame; |
| 440 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_GOAWAY, 0, &frame); |
| 441 |
|
| 442 |
frame->goaway_last_stream_id = last_stream; |
| 443 |
frame->error_code = error_code; |
| 444 |
|
| 445 |
AWS_FATAL_ASSERT(0 == aws_byte_buf_append_dynamic(&frame->data, &debug_data)); |
| 446 |
s_end_current_frame(decode_tester, AWS_H2_FRAME_T_GOAWAY, 0); |
| 447 |
|
| 448 |
return AWS_H2ERR_SUCCESS; |
| 449 |
} |
| 450 |
|
| 451 |
static struct aws_h2err s_decoder_on_window_update(uint32_t stream_id, uint32_t window_size_increment, void *userdata) { |
| 452 |
struct h2_decode_tester *decode_tester = userdata; |
| 453 |
struct h2_decoded_frame *frame; |
| 454 |
s_begin_new_frame(decode_tester, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, &frame); |
| 455 |
|
| 456 |
frame->window_size_increment = window_size_increment; |
| 457 |
|
| 458 |
s_end_current_frame(decode_tester, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id); |
| 459 |
|
| 460 |
return AWS_H2ERR_SUCCESS; |
| 461 |
} |
| 462 |
|
| 463 |
static struct aws_h2_decoder_vtable s_decoder_vtable = { |
| 464 |
.on_headers_begin = s_decoder_on_headers_begin, |
| 465 |
.on_headers_i = s_decoder_on_headers_i, |
| 466 |
.on_headers_end = s_decoder_on_headers_end, |
| 467 |
.on_push_promise_begin = s_decoder_on_push_promise_begin, |
| 468 |
.on_push_promise_i = s_decoder_on_push_promise_i, |
| 469 |
.on_push_promise_end = s_decoder_on_push_promise_end, |
| 470 |
.on_data_begin = s_decoder_on_data_begin, |
| 471 |
.on_data_i = s_decoder_on_data_i, |
| 472 |
.on_data_end = s_decoder_on_data_end, |
| 473 |
.on_end_stream = s_decoder_on_end_stream, |
| 474 |
.on_rst_stream = s_decoder_on_rst_stream, |
| 475 |
.on_settings = s_decoder_on_settings, |
| 476 |
.on_settings_ack = s_decoder_on_settings_ack, |
| 477 |
.on_ping = s_decoder_on_ping, |
| 478 |
.on_ping_ack = s_decoder_on_ping_ack, |
| 479 |
.on_goaway = s_decoder_on_goaway, |
| 480 |
.on_window_update = s_decoder_on_window_update, |
| 481 |
}; |
| 482 |
|
| 483 |
int h2_decode_tester_init(struct h2_decode_tester *decode_tester, const struct h2_decode_tester_options *options) { |
| 484 |
AWS_ZERO_STRUCT(*decode_tester); |
| 485 |
decode_tester->alloc = options->alloc; |
| 486 |
|
| 487 |
struct aws_h2_decoder_params decoder_params = { |
| 488 |
.alloc = options->alloc, |
| 489 |
.vtable = &s_decoder_vtable, |
| 490 |
.userdata = decode_tester, |
| 491 |
.is_server = options->is_server, |
| 492 |
.skip_connection_preface = options->skip_connection_preface, |
| 493 |
}; |
| 494 |
decode_tester->decoder = aws_h2_decoder_new(&decoder_params); |
| 495 |
ASSERT_NOT_NULL(decode_tester->decoder); |
| 496 |
|
| 497 |
ASSERT_SUCCESS( |
| 498 |
aws_array_list_init_dynamic(&decode_tester->frames, options->alloc, 16, sizeof(struct h2_decoded_frame))); |
| 499 |
return AWS_OP_SUCCESS; |
| 500 |
} |
| 501 |
|
| 502 |
void h2_decode_tester_clean_up(struct h2_decode_tester *decode_tester) { |
| 503 |
aws_h2_decoder_destroy(decode_tester->decoder); |
| 504 |
|
| 505 |
for (size_t i = 0; i < aws_array_list_length(&decode_tester->frames); ++i) { |
| 506 |
struct h2_decoded_frame *frame; |
| 507 |
aws_array_list_get_at_ptr(&decode_tester->frames, (void **)&frame, i); |
| 508 |
s_frame_clean_up(frame); |
| 509 |
} |
| 510 |
aws_array_list_clean_up(&decode_tester->frames); |
| 511 |
|
| 512 |
AWS_ZERO_STRUCT(*decode_tester); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
int h2_fake_peer_init(struct h2_fake_peer *peer, const struct h2_fake_peer_options *options) { |
| 520 |
AWS_ZERO_STRUCT(*peer); |
| 521 |
peer->alloc = options->alloc; |
| 522 |
peer->testing_channel = options->testing_channel; |
| 523 |
peer->is_server = options->is_server; |
| 524 |
|
| 525 |
ASSERT_SUCCESS(aws_h2_frame_encoder_init(&peer->encoder, peer->alloc, NULL )); |
| 526 |
|
| 527 |
struct h2_decode_tester_options decode_options = {.alloc = options->alloc, .is_server = options->is_server}; |
| 528 |
ASSERT_SUCCESS(h2_decode_tester_init(&peer->decode, &decode_options)); |
| 529 |
return AWS_OP_SUCCESS; |
| 530 |
} |
| 531 |
|
| 532 |
void h2_fake_peer_clean_up(struct h2_fake_peer *peer) { |
| 533 |
if (!peer) { |
| 534 |
return; |
| 535 |
} |
| 536 |
aws_h2_frame_encoder_clean_up(&peer->encoder); |
| 537 |
h2_decode_tester_clean_up(&peer->decode); |
| 538 |
AWS_ZERO_STRUCT(peer); |
| 539 |
} |
| 540 |
|
| 541 |
int h2_fake_peer_decode_messages_from_testing_channel(struct h2_fake_peer *peer) { |
| 542 |
struct aws_byte_buf msg_buf; |
| 543 |
ASSERT_SUCCESS(aws_byte_buf_init(&msg_buf, peer->alloc, 128)); |
| 544 |
ASSERT_SUCCESS(testing_channel_drain_written_messages(peer->testing_channel, &msg_buf)); |
| 545 |
|
| 546 |
struct aws_byte_cursor msg_cursor = aws_byte_cursor_from_buf(&msg_buf); |
| 547 |
ASSERT_H2ERR_SUCCESS(aws_h2_decode(peer->decode.decoder, &msg_cursor)); |
| 548 |
ASSERT_UINT_EQUALS(0, msg_cursor.len); |
| 549 |
|
| 550 |
aws_byte_buf_clean_up(&msg_buf); |
| 551 |
return AWS_OP_SUCCESS; |
| 552 |
} |
| 553 |
|
| 554 |
int h2_fake_peer_send_frame(struct h2_fake_peer *peer, struct aws_h2_frame *frame) { |
| 555 |
ASSERT_NOT_NULL(frame); |
| 556 |
|
| 557 |
bool frame_complete = false; |
| 558 |
while (!frame_complete) { |
| 559 |
struct aws_io_message *msg = aws_channel_acquire_message_from_pool( |
| 560 |
peer->testing_channel->channel, AWS_IO_MESSAGE_APPLICATION_DATA, g_aws_channel_max_fragment_size); |
| 561 |
ASSERT_NOT_NULL(msg); |
| 562 |
|
| 563 |
ASSERT_SUCCESS(aws_h2_encode_frame(&peer->encoder, frame, &msg->message_data, &frame_complete)); |
| 564 |
ASSERT_TRUE(msg->message_data.len != 0); |
| 565 |
|
| 566 |
ASSERT_SUCCESS(testing_channel_push_read_message(peer->testing_channel, msg)); |
| 567 |
} |
| 568 |
|
| 569 |
aws_h2_frame_destroy(frame); |
| 570 |
return AWS_OP_SUCCESS; |
| 571 |
} |
| 572 |
|
| 573 |
int h2_fake_peer_send_data_frame( |
| 574 |
struct h2_fake_peer *peer, |
| 575 |
uint32_t stream_id, |
| 576 |
struct aws_byte_cursor data, |
| 577 |
bool end_stream) { |
| 578 |
return h2_fake_peer_send_data_frame_with_padding_length(peer, stream_id, data, end_stream, 0); |
| 579 |
} |
| 580 |
|
| 581 |
int h2_fake_peer_send_data_frame_with_padding_length( |
| 582 |
struct h2_fake_peer *peer, |
| 583 |
uint32_t stream_id, |
| 584 |
struct aws_byte_cursor data, |
| 585 |
bool end_stream, |
| 586 |
uint8_t padding_length) { |
| 587 |
|
| 588 |
struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(peer->alloc, &data); |
| 589 |
ASSERT_NOT_NULL(body_stream); |
| 590 |
|
| 591 |
struct aws_io_message *msg = aws_channel_acquire_message_from_pool( |
| 592 |
peer->testing_channel->channel, AWS_IO_MESSAGE_APPLICATION_DATA, g_aws_channel_max_fragment_size); |
| 593 |
ASSERT_NOT_NULL(msg); |
| 594 |
|
| 595 |
bool body_complete; |
| 596 |
bool body_stalled; |
| 597 |
bool body_failed; |
| 598 |
int32_t stream_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX; |
| 599 |
uint32_t connection_window_size_peer = AWS_H2_WINDOW_UPDATE_MAX; |
| 600 |
ASSERT_SUCCESS(aws_h2_encode_data_frame( |
| 601 |
&peer->encoder, |
| 602 |
stream_id, |
| 603 |
body_stream, |
| 604 |
end_stream, |
| 605 |
padding_length , |
| 606 |
&stream_window_size_peer, |
| 607 |
&connection_window_size_peer, |
| 608 |
&msg->message_data, |
| 609 |
&body_complete, |
| 610 |
&body_stalled, |
| 611 |
&body_failed)); |
| 612 |
|
| 613 |
ASSERT_TRUE(body_complete); |
| 614 |
ASSERT_FALSE(body_stalled); |
| 615 |
ASSERT_FALSE(body_failed); |
| 616 |
ASSERT_TRUE(msg->message_data.len != 0); |
| 617 |
|
| 618 |
ASSERT_SUCCESS(testing_channel_push_read_message(peer->testing_channel, msg)); |
| 619 |
aws_input_stream_release(body_stream); |
| 620 |
return AWS_OP_SUCCESS; |
| 621 |
} |
| 622 |
|
| 623 |
int h2_fake_peer_send_data_frame_str(struct h2_fake_peer *peer, uint32_t stream_id, const char *data, bool end_stream) { |
| 624 |
return h2_fake_peer_send_data_frame(peer, stream_id, aws_byte_cursor_from_c_str(data), end_stream); |
| 625 |
} |
| 626 |
|
| 627 |
int h2_fake_peer_send_connection_preface(struct h2_fake_peer *peer, struct aws_h2_frame *settings) { |
| 628 |
if (!peer->is_server) { |
| 629 |
|
| 630 |
ASSERT_SUCCESS(testing_channel_push_read_data(peer->testing_channel, aws_h2_connection_preface_client_string)); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
ASSERT_SUCCESS(h2_fake_peer_send_frame(peer, settings)); |
| 635 |
|
| 636 |
return AWS_OP_SUCCESS; |
| 637 |
} |
| 638 |
|
| 639 |
int h2_fake_peer_send_connection_preface_default_settings(struct h2_fake_peer *peer) { |
| 640 |
|
| 641 |
struct aws_h2_frame *settings = aws_h2_frame_new_settings(peer->alloc, NULL, 0, false ); |
| 642 |
ASSERT_NOT_NULL(settings); |
| 643 |
|
| 644 |
ASSERT_SUCCESS(h2_fake_peer_send_connection_preface(peer, settings)); |
| 645 |
return AWS_OP_SUCCESS; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
struct aws_input_stream_tester { |
| 651 |
struct aws_input_stream base; |
| 652 |
struct aws_allocator *allocator; |
| 653 |
|
| 654 |
struct aws_input_stream *cursor_stream; |
| 655 |
|
| 656 |
size_t max_bytes_per_read; |
| 657 |
bool is_reading_broken; |
| 658 |
}; |
| 659 |
|
| 660 |
static int s_aws_input_stream_tester_seek( |
| 661 |
struct aws_input_stream *stream, |
| 662 |
int64_t offset, |
| 663 |
enum aws_stream_seek_basis basis) { |
| 664 |
|
| 665 |
struct aws_input_stream_tester *impl = AWS_CONTAINER_OF(stream, struct aws_input_stream_tester, base); |
| 666 |
return aws_input_stream_seek(impl->cursor_stream, offset, basis); |
| 667 |
} |
| 668 |
|
| 669 |
static int s_aws_input_stream_tester_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) { |
| 670 |
struct aws_input_stream_tester *impl = AWS_CONTAINER_OF(stream, struct aws_input_stream_tester, base); |
| 671 |
|
| 672 |
if (impl->is_reading_broken) { |
| 673 |
return aws_raise_error(AWS_IO_STREAM_READ_FAILED); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
size_t prev_capacity = dest->capacity; |
| 678 |
size_t max_capacity = aws_add_size_saturating(dest->len, impl->max_bytes_per_read); |
| 679 |
dest->capacity = aws_min_size(prev_capacity, max_capacity); |
| 680 |
|
| 681 |
int err = aws_input_stream_read(impl->cursor_stream, dest); |
| 682 |
|
| 683 |
dest->capacity = prev_capacity; |
| 684 |
return err; |
| 685 |
} |
| 686 |
|
| 687 |
static int s_aws_input_stream_tester_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) { |
| 688 |
struct aws_input_stream_tester *impl = AWS_CONTAINER_OF(stream, struct aws_input_stream_tester, base); |
| 689 |
return aws_input_stream_get_status(impl->cursor_stream, status); |
| 690 |
} |
| 691 |
|
| 692 |
static int s_aws_input_stream_tester_get_length(struct aws_input_stream *stream, int64_t *out_length) { |
| 693 |
struct aws_input_stream_tester *impl = AWS_CONTAINER_OF(stream, struct aws_input_stream_tester, base); |
| 694 |
return aws_input_stream_get_length(impl->cursor_stream, out_length); |
| 695 |
} |
| 696 |
|
| 697 |
static void s_aws_input_stream_tester_destroy(struct aws_input_stream_tester *impl) { |
| 698 |
aws_input_stream_release(impl->cursor_stream); |
| 699 |
aws_mem_release(impl->allocator, impl); |
| 700 |
} |
| 701 |
|
| 702 |
static struct aws_input_stream_vtable s_aws_input_stream_tester_vtable = { |
| 703 |
.seek = s_aws_input_stream_tester_seek, |
| 704 |
.read = s_aws_input_stream_tester_read, |
| 705 |
.get_status = s_aws_input_stream_tester_get_status, |
| 706 |
.get_length = s_aws_input_stream_tester_get_length, |
| 707 |
}; |
| 708 |
|
| 709 |
struct aws_input_stream *aws_input_stream_new_tester(struct aws_allocator *alloc, struct aws_byte_cursor cursor) { |
| 710 |
|
| 711 |
struct aws_input_stream_tester *impl = aws_mem_calloc(alloc, 1, sizeof(struct aws_input_stream_tester)); |
| 712 |
AWS_FATAL_ASSERT(impl); |
| 713 |
|
| 714 |
impl->max_bytes_per_read = SIZE_MAX; |
| 715 |
|
| 716 |
impl->cursor_stream = aws_input_stream_new_from_cursor(alloc, &cursor); |
| 717 |
AWS_FATAL_ASSERT(impl->cursor_stream); |
| 718 |
impl->allocator = alloc; |
| 719 |
impl->base.vtable = &s_aws_input_stream_tester_vtable; |
| 720 |
aws_ref_count_init( |
| 721 |
&impl->base.ref_count, impl, (aws_simple_completion_callback *)s_aws_input_stream_tester_destroy); |
| 722 |
return &impl->base; |
| 723 |
} |
| 724 |
|
| 725 |
void aws_input_stream_tester_set_max_bytes_per_read(struct aws_input_stream *input_stream, size_t max_bytes) { |
| 726 |
struct aws_input_stream_tester *impl = AWS_CONTAINER_OF(input_stream, struct aws_input_stream_tester, base); |
| 727 |
impl->max_bytes_per_read = max_bytes; |
| 728 |
} |
| 729 |
|
| 730 |
void aws_input_stream_tester_set_reading_broken(struct aws_input_stream *input_stream, bool is_broken) { |
| 731 |
struct aws_input_stream_tester *impl = AWS_CONTAINER_OF(input_stream, struct aws_input_stream_tester, base); |
| 732 |
impl->is_reading_broken = is_broken; |
| 733 |
} |
| 734 |
|
| 735 |
struct aws_input_stream_tester_upload_impl { |
| 736 |
struct aws_input_stream base; |
| 737 |
size_t position; |
| 738 |
size_t length; |
| 739 |
size_t num_sentence_sent; |
| 740 |
struct aws_allocator *allocator; |
| 741 |
}; |
| 742 |
|
| 743 |
static int s_aws_input_stream_tester_upload_seek( |
| 744 |
struct aws_input_stream *stream, |
| 745 |
int64_t offset, |
| 746 |
enum aws_stream_seek_basis basis) { |
| 747 |
(void)stream; |
| 748 |
(void)offset; |
| 749 |
(void)basis; |
| 750 |
|
| 751 |
|
| 752 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 753 |
return AWS_OP_ERR; |
| 754 |
} |
| 755 |
|
| 756 |
const struct aws_byte_cursor s_test_string = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("This is CRT HTTP test."); |
| 757 |
|
| 758 |
static int s_aws_input_stream_tester_upload_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) { |
| 759 |
(void)stream; |
| 760 |
(void)dest; |
| 761 |
|
| 762 |
struct aws_input_stream_tester_upload_impl *test_input_stream = |
| 763 |
AWS_CONTAINER_OF(stream, struct aws_input_stream_tester_upload_impl, base); |
| 764 |
|
| 765 |
while (dest->len < dest->capacity && test_input_stream->length - test_input_stream->position > 0) { |
| 766 |
size_t buffer_pos = test_input_stream->position % s_test_string.len; |
| 767 |
|
| 768 |
struct aws_byte_cursor source_byte_cursor = { |
| 769 |
.len = s_test_string.len - buffer_pos, |
| 770 |
.ptr = s_test_string.ptr + buffer_pos, |
| 771 |
}; |
| 772 |
|
| 773 |
size_t remaining_in_buffer = |
| 774 |
aws_min_size(dest->capacity - dest->len, test_input_stream->length - test_input_stream->position); |
| 775 |
|
| 776 |
if (remaining_in_buffer < source_byte_cursor.len) { |
| 777 |
source_byte_cursor.len = remaining_in_buffer; |
| 778 |
} |
| 779 |
|
| 780 |
aws_byte_buf_append(dest, &source_byte_cursor); |
| 781 |
buffer_pos += source_byte_cursor.len; |
| 782 |
|
| 783 |
test_input_stream->position += source_byte_cursor.len; |
| 784 |
} |
| 785 |
|
| 786 |
return AWS_OP_SUCCESS; |
| 787 |
} |
| 788 |
|
| 789 |
static int s_aws_input_stream_tester_upload_get_status( |
| 790 |
struct aws_input_stream *stream, |
| 791 |
struct aws_stream_status *status) { |
| 792 |
(void)stream; |
| 793 |
(void)status; |
| 794 |
|
| 795 |
struct aws_input_stream_tester_upload_impl *test_input_stream = |
| 796 |
AWS_CONTAINER_OF(stream, struct aws_input_stream_tester_upload_impl, base); |
| 797 |
|
| 798 |
status->is_end_of_stream = test_input_stream->position == test_input_stream->length; |
| 799 |
status->is_valid = true; |
| 800 |
|
| 801 |
return AWS_OP_SUCCESS; |
| 802 |
} |
| 803 |
|
| 804 |
static int s_aws_input_stream_tester_upload_get_length(struct aws_input_stream *stream, int64_t *out_length) { |
| 805 |
AWS_ASSERT(stream != NULL); |
| 806 |
struct aws_input_stream_tester_upload_impl *test_input_stream = |
| 807 |
AWS_CONTAINER_OF(stream, struct aws_input_stream_tester_upload_impl, base); |
| 808 |
*out_length = (int64_t)test_input_stream->length; |
| 809 |
return AWS_OP_SUCCESS; |
| 810 |
} |
| 811 |
|
| 812 |
static void s_aws_input_stream_tester_upload_destroy(struct aws_input_stream_tester_upload_impl *test_input_stream) { |
| 813 |
aws_mem_release(test_input_stream->allocator, test_input_stream); |
| 814 |
} |
| 815 |
|
| 816 |
static struct aws_input_stream_vtable s_aws_input_stream_tester_upload_vtable = { |
| 817 |
.seek = s_aws_input_stream_tester_upload_seek, |
| 818 |
.read = s_aws_input_stream_tester_upload_read, |
| 819 |
.get_status = s_aws_input_stream_tester_upload_get_status, |
| 820 |
.get_length = s_aws_input_stream_tester_upload_get_length, |
| 821 |
}; |
| 822 |
|
| 823 |
struct aws_input_stream *aws_input_stream_tester_upload_new(struct aws_allocator *alloc, size_t length) { |
| 824 |
|
| 825 |
struct aws_input_stream_tester_upload_impl *test_input_stream = |
| 826 |
aws_mem_calloc(alloc, 1, sizeof(struct aws_input_stream_tester_upload_impl)); |
| 827 |
test_input_stream->base.vtable = &s_aws_input_stream_tester_upload_vtable; |
| 828 |
aws_ref_count_init( |
| 829 |
&test_input_stream->base.ref_count, |
| 830 |
test_input_stream, |
| 831 |
(aws_simple_completion_callback *)s_aws_input_stream_tester_upload_destroy); |
| 832 |
|
| 833 |
struct aws_input_stream *input_stream = &test_input_stream->base; |
| 834 |
|
| 835 |
test_input_stream->position = 0; |
| 836 |
test_input_stream->length = length; |
| 837 |
test_input_stream->allocator = alloc; |
| 838 |
test_input_stream->num_sentence_sent = length / s_test_string.len; |
| 839 |
|
| 840 |
return input_stream; |
| 841 |
} |
| 842 |
|
| 843 |
size_t aws_input_stream_tester_upload_get_num_sentence_sent(struct aws_input_stream *stream) { |
| 844 |
struct aws_input_stream_tester_upload_impl *test_input_stream = |
| 845 |
AWS_CONTAINER_OF(stream, struct aws_input_stream_tester_upload_impl, base); |
| 846 |
return test_input_stream->num_sentence_sent; |
| 847 |
} |