| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/http/private/websocket_decoder.h> |
| 7 |
|
| 8 |
#include <aws/io/logging.h> |
| 9 |
#include <aws/testing/aws_test_harness.h> |
| 10 |
|
| 11 |
#ifdef _MSC_VER |
| 12 |
# pragma warning(disable : 4204) |
| 13 |
#endif |
| 14 |
|
| 15 |
#define DECODER_TEST_CASE(NAME) \ |
| 16 |
AWS_TEST_CASE(NAME, s_test_##NAME); \ |
| 17 |
static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) |
| 18 |
|
| 19 |
struct decoder_tester { |
| 20 |
struct aws_allocator *alloc; |
| 21 |
struct aws_logger logger; |
| 22 |
|
| 23 |
struct aws_websocket_decoder decoder; |
| 24 |
void *specific_test_data; |
| 25 |
|
| 26 |
struct aws_websocket_frame frame; |
| 27 |
size_t on_frame_count; |
| 28 |
size_t fail_on_nth_frame; |
| 29 |
|
| 30 |
struct aws_byte_buf payload; |
| 31 |
size_t on_payload_count; |
| 32 |
size_t fail_on_nth_payload; |
| 33 |
}; |
| 34 |
|
| 35 |
static int s_on_frame(const struct aws_websocket_frame *frame, void *user_data) { |
| 36 |
struct decoder_tester *tester = user_data; |
| 37 |
|
| 38 |
tester->frame = *frame; |
| 39 |
|
| 40 |
tester->on_frame_count++; |
| 41 |
if (tester->on_frame_count == tester->fail_on_nth_frame) { |
| 42 |
return aws_raise_error(AWS_ERROR_HTTP_UNKNOWN); |
| 43 |
} |
| 44 |
|
| 45 |
return AWS_OP_SUCCESS; |
| 46 |
} |
| 47 |
|
| 48 |
static int s_on_payload(struct aws_byte_cursor data, void *user_data) { |
| 49 |
struct decoder_tester *tester = user_data; |
| 50 |
|
| 51 |
ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&tester->payload, &data)); |
| 52 |
|
| 53 |
tester->on_payload_count++; |
| 54 |
if (tester->on_payload_count == tester->fail_on_nth_payload) { |
| 55 |
return aws_raise_error(AWS_ERROR_HTTP_UNKNOWN); |
| 56 |
} |
| 57 |
|
| 58 |
return AWS_OP_SUCCESS; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
static void s_decoder_tester_reset(struct decoder_tester *tester) { |
| 63 |
aws_websocket_decoder_clean_up(&tester->decoder); |
| 64 |
aws_websocket_decoder_init(&tester->decoder, tester->alloc, s_on_frame, s_on_payload, tester); |
| 65 |
AWS_ZERO_STRUCT(tester->frame); |
| 66 |
tester->on_frame_count = 0; |
| 67 |
tester->payload.len = 0; |
| 68 |
tester->on_payload_count = 0; |
| 69 |
} |
| 70 |
|
| 71 |
static int s_decoder_tester_init(struct decoder_tester *tester, struct aws_allocator *alloc) { |
| 72 |
aws_http_library_init(alloc); |
| 73 |
|
| 74 |
AWS_ZERO_STRUCT(*tester); |
| 75 |
tester->alloc = alloc; |
| 76 |
|
| 77 |
struct aws_logger_standard_options logger_options = { |
| 78 |
.level = AWS_LOG_LEVEL_TRACE, |
| 79 |
.file = stderr, |
| 80 |
}; |
| 81 |
ASSERT_SUCCESS(aws_logger_init_standard(&tester->logger, tester->alloc, &logger_options)); |
| 82 |
aws_logger_set(&tester->logger); |
| 83 |
|
| 84 |
ASSERT_SUCCESS(aws_byte_buf_init(&tester->payload, alloc, 1024)); |
| 85 |
|
| 86 |
s_decoder_tester_reset(tester); |
| 87 |
|
| 88 |
return AWS_OP_SUCCESS; |
| 89 |
} |
| 90 |
|
| 91 |
static int s_decoder_tester_clean_up(struct decoder_tester *tester) { |
| 92 |
aws_byte_buf_clean_up(&tester->payload); |
| 93 |
aws_websocket_decoder_clean_up(&tester->decoder); |
| 94 |
aws_http_library_clean_up(); |
| 95 |
aws_logger_clean_up(&tester->logger); |
| 96 |
return AWS_OP_SUCCESS; |
| 97 |
} |
| 98 |
|
| 99 |
static int s_compare_frame(const struct aws_websocket_frame *expected, const struct aws_websocket_frame *decoded) { |
| 100 |
uint8_t a[24]; |
| 101 |
memcpy(a, expected, 24); |
| 102 |
uint8_t b[24]; |
| 103 |
memcpy(b, decoded, 24); |
| 104 |
|
| 105 |
|
| 106 |
ASSERT_UINT_EQUALS(expected->fin, decoded->fin); |
| 107 |
ASSERT_UINT_EQUALS(expected->rsv[0], decoded->rsv[0]); |
| 108 |
ASSERT_UINT_EQUALS(expected->rsv[1], decoded->rsv[1]); |
| 109 |
ASSERT_UINT_EQUALS(expected->rsv[2], decoded->rsv[2]); |
| 110 |
ASSERT_UINT_EQUALS(expected->masked, decoded->masked); |
| 111 |
ASSERT_UINT_EQUALS(expected->opcode, decoded->opcode); |
| 112 |
ASSERT_UINT_EQUALS(expected->payload_length, decoded->payload_length); |
| 113 |
ASSERT_UINT_EQUALS(expected->masking_key[0], decoded->masking_key[0]); |
| 114 |
ASSERT_UINT_EQUALS(expected->masking_key[1], decoded->masking_key[1]); |
| 115 |
ASSERT_UINT_EQUALS(expected->masking_key[2], decoded->masking_key[2]); |
| 116 |
ASSERT_UINT_EQUALS(expected->masking_key[3], decoded->masking_key[3]); |
| 117 |
|
| 118 |
return AWS_OP_SUCCESS; |
| 119 |
}; |
| 120 |
|
| 121 |
DECODER_TEST_CASE(websocket_decoder_sanity_check) { |
| 122 |
(void)ctx; |
| 123 |
struct decoder_tester tester; |
| 124 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 125 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 126 |
return AWS_OP_SUCCESS; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
DECODER_TEST_CASE(websocket_decoder_simplest_frame) { |
| 131 |
(void)ctx; |
| 132 |
struct decoder_tester tester; |
| 133 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 134 |
|
| 135 |
uint8_t input[] = { |
| 136 |
0x89, |
| 137 |
0x00, |
| 138 |
}; |
| 139 |
|
| 140 |
struct aws_websocket_frame expected_frame = { |
| 141 |
.fin = true, |
| 142 |
.opcode = 9, |
| 143 |
}; |
| 144 |
|
| 145 |
bool frame_complete; |
| 146 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 147 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 148 |
|
| 149 |
|
| 150 |
ASSERT_TRUE(frame_complete); |
| 151 |
ASSERT_UINT_EQUALS(1, tester.on_frame_count); |
| 152 |
ASSERT_UINT_EQUALS(0, tester.on_payload_count); |
| 153 |
ASSERT_UINT_EQUALS(0, tester.payload.len); |
| 154 |
ASSERT_UINT_EQUALS(0, input_cursor.len); |
| 155 |
|
| 156 |
ASSERT_SUCCESS(s_compare_frame(&expected_frame, &tester.frame)); |
| 157 |
|
| 158 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 159 |
return AWS_OP_SUCCESS; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
DECODER_TEST_CASE(websocket_decoder_rsv) { |
| 164 |
(void)ctx; |
| 165 |
struct decoder_tester tester; |
| 166 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 167 |
|
| 168 |
|
| 169 |
for (int rsv = 0; rsv < 3; ++rsv) { |
| 170 |
|
| 171 |
uint8_t input[] = { |
| 172 |
0x89, |
| 173 |
0x00, |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
input[0] |= (1 << (6 - rsv)); |
| 179 |
|
| 180 |
struct aws_websocket_frame expected_frame = { |
| 181 |
.fin = true, |
| 182 |
.opcode = 9, |
| 183 |
}; |
| 184 |
expected_frame.rsv[rsv] = true; |
| 185 |
|
| 186 |
bool frame_complete; |
| 187 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 188 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 189 |
|
| 190 |
|
| 191 |
ASSERT_TRUE(frame_complete); |
| 192 |
ASSERT_SUCCESS(s_compare_frame(&expected_frame, &tester.frame)); |
| 193 |
} |
| 194 |
|
| 195 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 196 |
return AWS_OP_SUCCESS; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
DECODER_TEST_CASE(websocket_decoder_data_frame) { |
| 201 |
(void)ctx; |
| 202 |
struct decoder_tester tester; |
| 203 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 204 |
|
| 205 |
uint8_t input[] = { |
| 206 |
0x82, |
| 207 |
0x04, |
| 208 |
|
| 209 |
0x00, |
| 210 |
0x0F, |
| 211 |
0xF0, |
| 212 |
0xFF, |
| 213 |
}; |
| 214 |
|
| 215 |
const uint8_t expected_payload[] = {0x00, 0x0F, 0xF0, 0xFF}; |
| 216 |
|
| 217 |
struct aws_websocket_frame expected_frame = { |
| 218 |
.fin = true, |
| 219 |
.opcode = 2, |
| 220 |
.payload_length = sizeof(expected_payload), |
| 221 |
}; |
| 222 |
|
| 223 |
bool frame_complete; |
| 224 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 225 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 226 |
|
| 227 |
|
| 228 |
ASSERT_TRUE(frame_complete); |
| 229 |
ASSERT_UINT_EQUALS(1, tester.on_frame_count); |
| 230 |
ASSERT_UINT_EQUALS(1, tester.on_payload_count); |
| 231 |
ASSERT_UINT_EQUALS(0, input_cursor.len); |
| 232 |
|
| 233 |
ASSERT_SUCCESS(s_compare_frame(&expected_frame, &tester.frame)); |
| 234 |
|
| 235 |
struct aws_byte_cursor expected_cursor = aws_byte_cursor_from_array(expected_payload, sizeof(expected_payload)); |
| 236 |
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&expected_cursor, &tester.payload)); |
| 237 |
|
| 238 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 239 |
return AWS_OP_SUCCESS; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
DECODER_TEST_CASE(websocket_decoder_stops_at_frame_end) { |
| 244 |
(void)ctx; |
| 245 |
struct decoder_tester tester; |
| 246 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 247 |
|
| 248 |
uint8_t input[] = { |
| 249 |
0x82, |
| 250 |
0x04, |
| 251 |
|
| 252 |
0x00, |
| 253 |
0x0F, |
| 254 |
0xF0, |
| 255 |
0xFF, |
| 256 |
|
| 257 |
0x11, |
| 258 |
0x22, |
| 259 |
}; |
| 260 |
|
| 261 |
bool frame_complete; |
| 262 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 263 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 264 |
|
| 265 |
|
| 266 |
ASSERT_TRUE(frame_complete); |
| 267 |
ASSERT_UINT_EQUALS(1, tester.on_frame_count); |
| 268 |
ASSERT_UINT_EQUALS(1, tester.on_payload_count); |
| 269 |
ASSERT_UINT_EQUALS(2, input_cursor.len); |
| 270 |
|
| 271 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 272 |
return AWS_OP_SUCCESS; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
DECODER_TEST_CASE(websocket_decoder_masking) { |
| 277 |
(void)ctx; |
| 278 |
struct decoder_tester tester; |
| 279 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 280 |
|
| 281 |
|
| 282 |
uint8_t input[] = { |
| 283 |
0x81, |
| 284 |
0x85, |
| 285 |
|
| 286 |
0x37, |
| 287 |
0xfa, |
| 288 |
0x21, |
| 289 |
0x3d, |
| 290 |
|
| 291 |
0x7f, |
| 292 |
0x9f, |
| 293 |
0x4d, |
| 294 |
0x51, |
| 295 |
0x58, |
| 296 |
}; |
| 297 |
|
| 298 |
const char *expected_payload = "Hello"; |
| 299 |
|
| 300 |
struct aws_websocket_frame expected_frame = { |
| 301 |
.fin = true, |
| 302 |
.opcode = 1, |
| 303 |
.masked = true, |
| 304 |
.masking_key = {0x37, 0xfa, 0x21, 0x3d}, |
| 305 |
.payload_length = strlen(expected_payload), |
| 306 |
}; |
| 307 |
|
| 308 |
bool frame_complete; |
| 309 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 310 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 311 |
|
| 312 |
|
| 313 |
ASSERT_TRUE(frame_complete); |
| 314 |
ASSERT_SUCCESS(s_compare_frame(&expected_frame, &tester.frame)); |
| 315 |
ASSERT_TRUE(aws_byte_buf_eq_c_str(&tester.payload, expected_payload)); |
| 316 |
|
| 317 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 318 |
return AWS_OP_SUCCESS; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
DECODER_TEST_CASE(websocket_decoder_extended_length_2byte) { |
| 323 |
(void)ctx; |
| 324 |
struct decoder_tester tester; |
| 325 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 326 |
|
| 327 |
struct length_validity_pair { |
| 328 |
uint16_t len; |
| 329 |
bool valid; |
| 330 |
}; |
| 331 |
|
| 332 |
uint8_t input[4] = { |
| 333 |
0x82, |
| 334 |
0x7E, |
| 335 |
|
| 336 |
}; |
| 337 |
|
| 338 |
struct aws_websocket_frame expected_frame = { |
| 339 |
.fin = true, |
| 340 |
.opcode = 2, |
| 341 |
}; |
| 342 |
|
| 343 |
|
| 344 |
struct length_validity_pair length_validity_pairs[] = { |
| 345 |
{0, false}, |
| 346 |
{1, false}, |
| 347 |
{125, false}, |
| 348 |
{126, true}, |
| 349 |
{127, true}, |
| 350 |
{0x0100, true}, |
| 351 |
{0xFFFF, true}, |
| 352 |
}; |
| 353 |
|
| 354 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(length_validity_pairs); ++i) { |
| 355 |
struct length_validity_pair pair_i = length_validity_pairs[i]; |
| 356 |
s_decoder_tester_reset(&tester); |
| 357 |
|
| 358 |
|
| 359 |
uint16_t network_num = aws_hton16(pair_i.len); |
| 360 |
memcpy(input + 2, &network_num, sizeof(network_num)); |
| 361 |
|
| 362 |
|
| 363 |
expected_frame.payload_length = pair_i.len; |
| 364 |
|
| 365 |
|
| 366 |
bool frame_complete; |
| 367 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 368 |
|
| 369 |
if (pair_i.valid) { |
| 370 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 371 |
|
| 372 |
|
| 373 |
ASSERT_FALSE(frame_complete); |
| 374 |
ASSERT_UINT_EQUALS(0, input_cursor.len); |
| 375 |
ASSERT_UINT_EQUALS(1, tester.on_frame_count); |
| 376 |
ASSERT_UINT_EQUALS(0, tester.on_payload_count); |
| 377 |
ASSERT_SUCCESS(s_compare_frame(&expected_frame, &tester.frame)); |
| 378 |
} else { |
| 379 |
aws_raise_error(-1); |
| 380 |
|
| 381 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 382 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR, aws_last_error()); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 387 |
return AWS_OP_SUCCESS; |
| 388 |
} |
| 389 |
|
| 390 |
DECODER_TEST_CASE(websocket_decoder_extended_length_8byte) { |
| 391 |
(void)ctx; |
| 392 |
struct decoder_tester tester; |
| 393 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 394 |
|
| 395 |
struct length_validity_pair { |
| 396 |
uint64_t len; |
| 397 |
bool valid; |
| 398 |
}; |
| 399 |
|
| 400 |
uint8_t input[10] = { |
| 401 |
0x82, |
| 402 |
0x7F, |
| 403 |
|
| 404 |
}; |
| 405 |
|
| 406 |
struct aws_websocket_frame expected_frame = { |
| 407 |
.fin = true, |
| 408 |
.opcode = 2, |
| 409 |
}; |
| 410 |
|
| 411 |
|
| 412 |
struct length_validity_pair length_validity_pairs[] = { |
| 413 |
{125, false}, |
| 414 |
{126, false}, |
| 415 |
{127, false}, |
| 416 |
{0x0100, false}, |
| 417 |
{0xFFFF, false}, |
| 418 |
{0x0000000000010000, true}, |
| 419 |
{0x7FFFFFFFFFFFFFFF, true}, |
| 420 |
{0x123456789ABCDEF0, true}, |
| 421 |
{0x8000000000000000, false}, |
| 422 |
{0xFFFFFFFFFFFFFFFF, false}, |
| 423 |
}; |
| 424 |
|
| 425 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(length_validity_pairs); ++i) { |
| 426 |
struct length_validity_pair pair_i = length_validity_pairs[i]; |
| 427 |
s_decoder_tester_reset(&tester); |
| 428 |
|
| 429 |
|
| 430 |
uint64_t network_num = aws_hton64(pair_i.len); |
| 431 |
memcpy(input + 2, &network_num, sizeof(network_num)); |
| 432 |
|
| 433 |
|
| 434 |
expected_frame.payload_length = pair_i.len; |
| 435 |
|
| 436 |
|
| 437 |
bool frame_complete; |
| 438 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 439 |
|
| 440 |
if (pair_i.valid) { |
| 441 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 442 |
|
| 443 |
|
| 444 |
ASSERT_FALSE(frame_complete); |
| 445 |
ASSERT_UINT_EQUALS(0, input_cursor.len); |
| 446 |
ASSERT_UINT_EQUALS(1, tester.on_frame_count); |
| 447 |
ASSERT_UINT_EQUALS(0, tester.on_payload_count); |
| 448 |
ASSERT_SUCCESS(s_compare_frame(&expected_frame, &tester.frame)); |
| 449 |
} else { |
| 450 |
aws_raise_error(-1); |
| 451 |
|
| 452 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 453 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR, aws_last_error()); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 458 |
return AWS_OP_SUCCESS; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
DECODER_TEST_CASE(websocket_decoder_1byte_at_a_time) { |
| 463 |
(void)ctx; |
| 464 |
struct decoder_tester tester; |
| 465 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
uint8_t input[] = { |
| 470 |
0x81, |
| 471 |
0xFF, |
| 472 |
|
| 473 |
0x00, |
| 474 |
0x00, |
| 475 |
0x00, |
| 476 |
0x00, |
| 477 |
0x00, |
| 478 |
0x01, |
| 479 |
0x00, |
| 480 |
0x00, |
| 481 |
|
| 482 |
0x37, |
| 483 |
0xfa, |
| 484 |
0x21, |
| 485 |
0x3d, |
| 486 |
|
| 487 |
0x7f, |
| 488 |
0x9f, |
| 489 |
0x4d, |
| 490 |
0x51, |
| 491 |
0x58, |
| 492 |
}; |
| 493 |
|
| 494 |
const char *expected_payload = "Hello"; |
| 495 |
|
| 496 |
struct aws_websocket_frame expected_frame = { |
| 497 |
.fin = true, |
| 498 |
.opcode = 1, |
| 499 |
.masked = true, |
| 500 |
.masking_key = {0x37, 0xfa, 0x21, 0x3d}, |
| 501 |
.payload_length = 0x10000, |
| 502 |
}; |
| 503 |
|
| 504 |
for (size_t i = 0; i < sizeof(input); ++i) { |
| 505 |
bool frame_complete; |
| 506 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input + i, 1); |
| 507 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 508 |
ASSERT_FALSE(frame_complete); |
| 509 |
ASSERT_UINT_EQUALS(0, input_cursor.len); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
ASSERT_UINT_EQUALS(1, tester.on_frame_count); |
| 514 |
ASSERT_UINT_EQUALS(5, tester.on_payload_count); |
| 515 |
ASSERT_SUCCESS(s_compare_frame(&expected_frame, &tester.frame)); |
| 516 |
ASSERT_TRUE(aws_byte_buf_eq_c_str(&tester.payload, expected_payload)); |
| 517 |
|
| 518 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 519 |
return AWS_OP_SUCCESS; |
| 520 |
} |
| 521 |
|
| 522 |
DECODER_TEST_CASE(websocket_decoder_fail_on_unknown_opcode) { |
| 523 |
(void)ctx; |
| 524 |
struct decoder_tester tester; |
| 525 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 526 |
|
| 527 |
uint8_t input[] = { |
| 528 |
0x07, |
| 529 |
0x00, |
| 530 |
}; |
| 531 |
|
| 532 |
bool frame_complete; |
| 533 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 534 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 535 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR, aws_last_error()); |
| 536 |
|
| 537 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 538 |
return AWS_OP_SUCCESS; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
DECODER_TEST_CASE(websocket_decoder_fragmented_message) { |
| 543 |
(void)ctx; |
| 544 |
struct decoder_tester tester; |
| 545 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 546 |
|
| 547 |
uint8_t input[] = { |
| 548 |
|
| 549 |
0x01, |
| 550 |
0x03, |
| 551 |
'h', |
| 552 |
'o', |
| 553 |
't', |
| 554 |
|
| 555 |
|
| 556 |
0x00, |
| 557 |
0x02, |
| 558 |
'd', |
| 559 |
'o', |
| 560 |
|
| 561 |
|
| 562 |
0x89, |
| 563 |
0x00, |
| 564 |
|
| 565 |
|
| 566 |
0x80, |
| 567 |
0x01, |
| 568 |
'g', |
| 569 |
}; |
| 570 |
|
| 571 |
struct aws_websocket_frame expected_frames[] = { |
| 572 |
{ |
| 573 |
.fin = false, |
| 574 |
.opcode = 1, |
| 575 |
.payload_length = 3, |
| 576 |
}, |
| 577 |
{ |
| 578 |
.fin = false, |
| 579 |
.opcode = 0, |
| 580 |
.payload_length = 2, |
| 581 |
}, |
| 582 |
{ |
| 583 |
.fin = true, |
| 584 |
.opcode = 9, |
| 585 |
}, |
| 586 |
{ |
| 587 |
.fin = true, |
| 588 |
.opcode = 0, |
| 589 |
.payload_length = 1, |
| 590 |
}, |
| 591 |
}; |
| 592 |
|
| 593 |
const char *expected_payload = "hotdog"; |
| 594 |
|
| 595 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 596 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(expected_frames); ++i) { |
| 597 |
bool frame_complete; |
| 598 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 599 |
ASSERT_TRUE(frame_complete); |
| 600 |
ASSERT_UINT_EQUALS(i + 1, tester.on_frame_count); |
| 601 |
ASSERT_SUCCESS(s_compare_frame(&expected_frames[i], &tester.frame)); |
| 602 |
} |
| 603 |
|
| 604 |
ASSERT_TRUE(aws_byte_buf_eq_c_str(&tester.payload, expected_payload)); |
| 605 |
|
| 606 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 607 |
return AWS_OP_SUCCESS; |
| 608 |
} |
| 609 |
|
| 610 |
DECODER_TEST_CASE(websocket_decoder_fail_on_bad_fragmentation) { |
| 611 |
(void)ctx; |
| 612 |
struct decoder_tester tester; |
| 613 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 614 |
|
| 615 |
uint8_t input[] = { |
| 616 |
|
| 617 |
0x01, |
| 618 |
0x01, |
| 619 |
'a', |
| 620 |
|
| 621 |
|
| 622 |
0x01, |
| 623 |
0x01, |
| 624 |
'b', |
| 625 |
}; |
| 626 |
|
| 627 |
bool frame_complete; |
| 628 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 629 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 630 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 631 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR, aws_last_error()); |
| 632 |
|
| 633 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 634 |
return AWS_OP_SUCCESS; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
DECODER_TEST_CASE(websocket_decoder_control_frame_cannot_be_fragmented) { |
| 639 |
(void)ctx; |
| 640 |
struct decoder_tester tester; |
| 641 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 642 |
|
| 643 |
uint8_t input[] = { |
| 644 |
0x0A, |
| 645 |
0x00, |
| 646 |
}; |
| 647 |
|
| 648 |
bool frame_complete; |
| 649 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 650 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 651 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR, aws_last_error()); |
| 652 |
|
| 653 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 654 |
return AWS_OP_SUCCESS; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
DECODER_TEST_CASE(websocket_decoder_utf8_text) { |
| 659 |
(void)ctx; |
| 660 |
struct decoder_tester tester; |
| 661 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 662 |
|
| 663 |
uint8_t input[] = { |
| 664 |
|
| 665 |
0x81, |
| 666 |
0x04, |
| 667 |
|
| 668 |
0xF0, |
| 669 |
0x90, |
| 670 |
0x8D, |
| 671 |
0x88, |
| 672 |
}; |
| 673 |
|
| 674 |
struct aws_websocket_frame expected_frame = { |
| 675 |
.fin = true, |
| 676 |
.opcode = AWS_WEBSOCKET_OPCODE_TEXT, |
| 677 |
.payload_length = 4, |
| 678 |
}; |
| 679 |
const char *expected_payload = "\xF0\x90\x8D\x88"; |
| 680 |
|
| 681 |
bool frame_complete; |
| 682 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 683 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 684 |
|
| 685 |
|
| 686 |
ASSERT_TRUE(frame_complete); |
| 687 |
ASSERT_SUCCESS(s_compare_frame(&expected_frame, &tester.frame)); |
| 688 |
ASSERT_TRUE(aws_byte_buf_eq_c_str(&tester.payload, expected_payload)); |
| 689 |
|
| 690 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 691 |
return AWS_OP_SUCCESS; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
DECODER_TEST_CASE(websocket_decoder_fail_on_bad_utf8_text) { |
| 696 |
(void)ctx; |
| 697 |
struct decoder_tester tester; |
| 698 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 699 |
|
| 700 |
{ |
| 701 |
uint8_t input[] = { |
| 702 |
|
| 703 |
0x81, |
| 704 |
0x01, |
| 705 |
|
| 706 |
0xFF, |
| 707 |
}; |
| 708 |
|
| 709 |
bool frame_complete; |
| 710 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 711 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 712 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR, aws_last_error()); |
| 713 |
} |
| 714 |
|
| 715 |
s_decoder_tester_reset(&tester); |
| 716 |
|
| 717 |
{ |
| 718 |
uint8_t input[] = { |
| 719 |
|
| 720 |
0x81, |
| 721 |
0x03, |
| 722 |
|
| 723 |
0xF0, |
| 724 |
0x90, |
| 725 |
0x8D, |
| 726 |
|
| 727 |
}; |
| 728 |
|
| 729 |
bool frame_complete; |
| 730 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 731 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 732 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR, aws_last_error()); |
| 733 |
} |
| 734 |
|
| 735 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 736 |
return AWS_OP_SUCCESS; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
DECODER_TEST_CASE(websocket_decoder_fragmented_utf8_text) { |
| 741 |
(void)ctx; |
| 742 |
struct decoder_tester tester; |
| 743 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
uint8_t input[] = { |
| 748 |
|
| 749 |
0x01, |
| 750 |
0x01, |
| 751 |
|
| 752 |
0xF0, |
| 753 |
|
| 754 |
|
| 755 |
0x00, |
| 756 |
0x02, |
| 757 |
|
| 758 |
0x90, |
| 759 |
0x8D, |
| 760 |
|
| 761 |
|
| 762 |
0x89, |
| 763 |
0x01, |
| 764 |
|
| 765 |
0xFF, |
| 766 |
|
| 767 |
|
| 768 |
0x80, |
| 769 |
0x01, |
| 770 |
|
| 771 |
0x88, |
| 772 |
}; |
| 773 |
|
| 774 |
struct aws_websocket_frame expected_frames[] = { |
| 775 |
{ |
| 776 |
.fin = false, |
| 777 |
.opcode = 1, |
| 778 |
.payload_length = 1, |
| 779 |
}, |
| 780 |
{ |
| 781 |
.fin = false, |
| 782 |
.opcode = 0, |
| 783 |
.payload_length = 2, |
| 784 |
}, |
| 785 |
{ |
| 786 |
.fin = true, |
| 787 |
.opcode = 9, |
| 788 |
.payload_length = 1, |
| 789 |
}, |
| 790 |
{ |
| 791 |
.fin = true, |
| 792 |
.opcode = 0, |
| 793 |
.payload_length = 1, |
| 794 |
}, |
| 795 |
}; |
| 796 |
|
| 797 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 798 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(expected_frames); ++i) { |
| 799 |
bool frame_complete; |
| 800 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 801 |
ASSERT_TRUE(frame_complete); |
| 802 |
ASSERT_UINT_EQUALS(i + 1, tester.on_frame_count); |
| 803 |
ASSERT_SUCCESS(s_compare_frame(&expected_frames[i], &tester.frame)); |
| 804 |
} |
| 805 |
ASSERT_UINT_EQUALS(0, input_cursor.len); |
| 806 |
|
| 807 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 808 |
return AWS_OP_SUCCESS; |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
DECODER_TEST_CASE(websocket_decoder_fail_on_fragmented_bad_utf8_text) { |
| 813 |
(void)ctx; |
| 814 |
struct decoder_tester tester; |
| 815 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
uint8_t input[] = { |
| 820 |
|
| 821 |
0x01, |
| 822 |
0x01, |
| 823 |
|
| 824 |
0xF0, |
| 825 |
|
| 826 |
|
| 827 |
0x00, |
| 828 |
0x01, |
| 829 |
|
| 830 |
0x90, |
| 831 |
|
| 832 |
|
| 833 |
0x89, |
| 834 |
0x01, |
| 835 |
|
| 836 |
0x8D, |
| 837 |
|
| 838 |
|
| 839 |
0x80, |
| 840 |
0x01, |
| 841 |
|
| 842 |
0x8D, |
| 843 |
|
| 844 |
}; |
| 845 |
|
| 846 |
bool frame_complete; |
| 847 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 848 |
|
| 849 |
|
| 850 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 851 |
ASSERT_TRUE(frame_complete); |
| 852 |
|
| 853 |
|
| 854 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 855 |
ASSERT_TRUE(frame_complete); |
| 856 |
|
| 857 |
|
| 858 |
ASSERT_SUCCESS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 859 |
ASSERT_TRUE(frame_complete); |
| 860 |
|
| 861 |
|
| 862 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 863 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR, aws_last_error()); |
| 864 |
|
| 865 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 866 |
return AWS_OP_SUCCESS; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
DECODER_TEST_CASE(websocket_decoder_on_frame_callback_can_fail_decoder) { |
| 871 |
(void)ctx; |
| 872 |
struct decoder_tester tester; |
| 873 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 874 |
|
| 875 |
uint8_t input[] = { |
| 876 |
0x81, |
| 877 |
0x01, |
| 878 |
'a', |
| 879 |
}; |
| 880 |
|
| 881 |
tester.fail_on_nth_frame = 1; |
| 882 |
|
| 883 |
bool frame_complete; |
| 884 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 885 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_UNKNOWN, aws_last_error()); |
| 890 |
|
| 891 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 892 |
return AWS_OP_SUCCESS; |
| 893 |
} |
| 894 |
|
| 895 |
DECODER_TEST_CASE(websocket_decoder_on_payload_callback_can_fail_decoder) { |
| 896 |
(void)ctx; |
| 897 |
struct decoder_tester tester; |
| 898 |
ASSERT_SUCCESS(s_decoder_tester_init(&tester, allocator)); |
| 899 |
|
| 900 |
uint8_t input[] = { |
| 901 |
0x81, |
| 902 |
0x01, |
| 903 |
'a', |
| 904 |
}; |
| 905 |
|
| 906 |
tester.fail_on_nth_payload = 1; |
| 907 |
|
| 908 |
bool frame_complete; |
| 909 |
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input)); |
| 910 |
ASSERT_FAILS(aws_websocket_decoder_process(&tester.decoder, &input_cursor, &frame_complete)); |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_UNKNOWN, aws_last_error()); |
| 915 |
|
| 916 |
ASSERT_SUCCESS(s_decoder_tester_clean_up(&tester)); |
| 917 |
return AWS_OP_SUCCESS; |
| 918 |
} |