1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/testing/aws_test_harness.h>
#include <aws/http/private/hpack.h>
#include <aws/http/request_response.h>
/* #TODO test that buffer is resized if space is insufficient */
AWS_TEST_CASE(hpack_encode_integer, test_hpack_encode_integer)
static int test_hpack_encode_integer(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
(void)ctx;
/* Test encoding integers
Test cases taken from https://httpwg.org/specs/rfc7541.html#integer.representation.examples */
uint8_t zeros[4];
AWS_ZERO_ARRAY(zeros);
struct aws_byte_buf output;
ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 4));
/* Test 10 in 5 bits */
aws_byte_buf_secure_zero(&output);
ASSERT_SUCCESS(aws_hpack_encode_integer(10, 0, 5, &output));
/**
* Expected:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | X | X | X | 0 | 1 | 0 | 1 | 0 | 10
* +---+---+---+---+---+---+---+---+
*/
ASSERT_UINT_EQUALS(1, output.len);
ASSERT_UINT_EQUALS(10, output.buffer[0]);
ASSERT_BIN_ARRAYS_EQUALS(zeros, 3, &output.buffer[1], 3);
/* Test full first byte (6 bits) */
aws_byte_buf_secure_zero(&output);
ASSERT_SUCCESS(aws_hpack_encode_integer(63, 0, 6, &output));
/**
* Expected:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | X | X | 1 | 1 | 1 | 1 | 1 | 1 | 63
* +---+---+---+---+---+---+---+---+
* | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
* +---+---+---+---+---+---+---+---+
*/
ASSERT_UINT_EQUALS(2, output.len);
ASSERT_UINT_EQUALS(63, output.buffer[0]);
ASSERT_UINT_EQUALS(0, output.buffer[1]);
ASSERT_BIN_ARRAYS_EQUALS(zeros, 2, &output.buffer[2], 2);
/* Test 42 in 8 bits */
aws_byte_buf_secure_zero(&output);
ASSERT_SUCCESS(aws_hpack_encode_integer(42, 0, 8, &output));
/**
* Expected:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 42
* +---+---+---+---+---+---+---+---+
*/
ASSERT_UINT_EQUALS(1, output.len);
ASSERT_UINT_EQUALS(42, output.buffer[0]);
ASSERT_BIN_ARRAYS_EQUALS(zeros, 3, &output.buffer[1], 3);
/* Test 1337 with 5bit prefix */
aws_byte_buf_secure_zero(&output);
ASSERT_SUCCESS(aws_hpack_encode_integer(1337, 0, 5, &output));
/**
* Expected:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | X | X | X | 1 | 1 | 1 | 1 | 1 | 31
* | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 154
* | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10
* +---+---+---+---+---+---+---+---+
*/
ASSERT_UINT_EQUALS(3, output.len);
ASSERT_UINT_EQUALS(UINT8_MAX >> 3, output.buffer[0]);
ASSERT_UINT_EQUALS(154, output.buffer[1]);
ASSERT_UINT_EQUALS(10, output.buffer[2]);
ASSERT_UINT_EQUALS(0, output.buffer[3]);
aws_byte_buf_clean_up(&output);
return AWS_OP_SUCCESS;
}
struct decode_fixture {
struct aws_hpack_decoder hpack;
bool one_byte_at_a_time;
};
static int s_decode_fixture_setup(struct aws_allocator *allocator, void *ctx) {
struct decode_fixture *fixture = ctx;
aws_hpack_decoder_init(&fixture->hpack, allocator, NULL);
return AWS_OP_SUCCESS;
}
static int s_decode_fixture_teardown(struct aws_allocator *allocator, int setup_result, void *ctx) {
(void)allocator;
if (setup_result) {
return AWS_OP_ERR;
}
struct decode_fixture *fixture = ctx;
aws_hpack_decoder_clean_up(&fixture->hpack);
return AWS_OP_SUCCESS;
}
/* Call aws_hpack_decode_integer() either one-byte-at-a-time, or all at once */
static int s_decode_integer(
struct decode_fixture *fixture,
struct aws_byte_cursor *to_decode,
uint8_t prefix_size,
uint64_t *integer,
bool *complete) {
if (fixture->one_byte_at_a_time) {
do {
struct aws_byte_cursor one_byte = aws_byte_cursor_advance(to_decode, 1);
if (aws_hpack_decode_integer(&fixture->hpack, &one_byte, prefix_size, integer, complete)) {
return AWS_OP_ERR;
}
ASSERT_UINT_EQUALS(0, one_byte.len);
} while (!*complete && to_decode->len);
return AWS_OP_SUCCESS;
} else {
return aws_hpack_decode_integer(&fixture->hpack, to_decode, prefix_size, integer, complete);
}
}
/* Call aws_hpack_decode_string() either one-byte-at-a-time, or all at once */
static int s_decode_string(
struct decode_fixture *fixture,
struct aws_byte_cursor *to_decode,
struct aws_byte_buf *output,
bool *complete) {
if (fixture->one_byte_at_a_time) {
do {
struct aws_byte_cursor one_byte = aws_byte_cursor_advance(to_decode, 1);
if (aws_hpack_decode_string(&fixture->hpack, &one_byte, output, complete)) {
return AWS_OP_ERR;
}
ASSERT_UINT_EQUALS(0, one_byte.len);
} while (!*complete && to_decode->len);
return AWS_OP_SUCCESS;
} else {
return aws_hpack_decode_string(&fixture->hpack, to_decode, output, complete);
}
}
/* declare 2 tests, where the first decodes the input all at once,
* and the other decodes the input one byte at a time. */
#define TEST_DECODE_ONE_BYTE_AT_A_TIME(NAME) \
static struct decode_fixture s_##NAME##_fixture = {.one_byte_at_a_time = false}; \
static struct decode_fixture s_##NAME##_one_byte_at_a_time_fixture = {.one_byte_at_a_time = true}; \
AWS_TEST_CASE_FIXTURE(NAME, s_decode_fixture_setup, s_test_##NAME, s_decode_fixture_teardown, &s_##NAME##_fixture) \
AWS_TEST_CASE_FIXTURE( \
NAME##_one_byte_at_a_time, \
s_decode_fixture_setup, \
s_test_##NAME, \
s_decode_fixture_teardown, \
&s_##NAME##_one_byte_at_a_time_fixture) \
static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
/* RFC-7541 - Integer Representation Examples - C.1.1. Encoding 10 Using a 5-Bit Prefix */
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_5bits) {
(void)allocator;
struct decode_fixture *fixture = ctx;
/* Layout:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | X | X | X | 0 | 1 | 0 | 1 | 0 | 10
* +---+---+---+---+---+---+---+---+
*/
uint8_t test_0[] = {10};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_0, AWS_ARRAY_SIZE(test_0));
uint64_t result;
bool complete;
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
ASSERT_UINT_EQUALS(10, result);
return AWS_OP_SUCCESS;
}
/* Encoding 63 across a 6-bit prefix + one byte */
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_14bits) {
(void)allocator;
struct decode_fixture *fixture = ctx;
/* Layout:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | X | X | 1 | 1 | 1 | 1 | 1 | 1 | 63
* +---+---+---+---+---+---+---+---+
* | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
* +---+---+---+---+---+---+---+---+
*/
uint8_t test_1[] = {63, 0};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_1, AWS_ARRAY_SIZE(test_1));
uint64_t result;
bool complete;
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 6, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
ASSERT_UINT_EQUALS(63, result);
return AWS_OP_SUCCESS;
}
/* RFC-7541 - Integer Representation Examples - C.1.3. Encoding 42 Starting at an Octet Boundary */
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_8bits) {
(void)allocator;
struct decode_fixture *fixture = ctx;
/* Layout:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 42
* +---+---+---+---+---+---+---+---+
*/
uint8_t test_2[] = {42};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_2, AWS_ARRAY_SIZE(test_2));
uint64_t result;
bool complete;
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 8, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
ASSERT_UINT_EQUALS(42, result);
return AWS_OP_SUCCESS;
}
/* RFC-7541 - Integer Representation Examples - C.1.2. Encoding 1337 Using a 5-Bit Prefix */
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_21bits) {
(void)allocator;
struct decode_fixture *fixture = ctx;
/* Layout:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | X | X | X | 1 | 1 | 1 | 1 | 1 | 31
* | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 154
* | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10
* +---+---+---+---+---+---+---+---+
*/
uint8_t test_3[] = {UINT8_MAX >> 3, 154, 10};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_3, AWS_ARRAY_SIZE(test_3));
uint64_t result;
bool complete;
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
ASSERT_UINT_EQUALS(1337, result);
return AWS_OP_SUCCESS;
}
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_ongoing) {
(void)allocator;
struct decode_fixture *fixture = ctx;
/* Test number ending with continue byte
* Layout:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | X | X | X | 1 | 1 | 1 | 1 | 1 | 31
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* +---+---+---+---+---+---+---+---+
*/
uint8_t test_4[] = {UINT8_MAX >> 3, UINT8_MAX};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_4, AWS_ARRAY_SIZE(test_4));
uint64_t result;
bool complete;
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
ASSERT_FALSE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
return AWS_OP_SUCCESS;
}
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_too_big) {
(void)allocator;
struct decode_fixture *fixture = ctx;
/* Test number too big
* Layout:
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | X | X | X | 1 | 1 | 1 | 1 | 1 | 31
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 127
* +---+---+---+---+---+---+---+---+
*/
uint8_t test_5[] = {
UINT8_MAX >> 3,
UINT8_MAX,
UINT8_MAX,
UINT8_MAX,
UINT8_MAX,
UINT8_MAX,
UINT8_MAX,
UINT8_MAX,
UINT8_MAX,
UINT8_MAX,
UINT8_MAX,
};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(test_5, AWS_ARRAY_SIZE(test_5));
uint64_t result;
bool complete;
ASSERT_FAILS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
ASSERT_UINT_EQUALS(AWS_ERROR_OVERFLOW_DETECTED, aws_last_error());
return AWS_OP_SUCCESS;
}
/* Test that decoder properly resets itself between integers.
* Trying every type of transition:
* - from 1 byte to 1 byte
* - from 1 byte to multibyte
* - from multibyte to multibyte
* - from multibyte to 1 byte */
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_integer_few_in_a_row) {
(void)allocator;
struct decode_fixture *fixture = ctx;
uint8_t input[] = {
/* 10 with 5-bit prefix
* +---+---+---+---+---+---+---+---+
* | X | X | X | 0 | 1 | 0 | 1 | 0 |
* +---+---+---+---+---+---+---+---+ */
10,
/* 42 with 8-bit prefix
* +---+---+---+---+---+---+---+---+
* | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
* +---+---+---+---+---+---+---+---+
*/
42,
/* 63 with 6-bit prefix
* +---+---+---+---+---+---+---+---+
* | X | X | 1 | 1 | 1 | 1 | 1 | 1 |
* +---+---+---+---+---+---+---+---+
* | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
* +---+---+---+---+---+---+---+---+
*/
63,
0,
/* 1337 with 5-bit prefix
* +---+---+---+---+---+---+---+---+
* | X | X | X | 1 | 1 | 1 | 1 | 1 |
* | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
* | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
* +---+---+---+---+---+---+---+---+
*/
UINT8_MAX >> 3,
154,
10,
/* 10 with 5-bit prefix
* +---+---+---+---+---+---+---+---+
* | X | X | X | 0 | 1 | 0 | 1 | 0 |
* +---+---+---+---+---+---+---+---+
*/
10,
};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
uint64_t result;
bool complete;
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(10, result);
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 8, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(42, result);
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 6, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(63, result);
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(1337, result);
ASSERT_SUCCESS(s_decode_integer(fixture, &to_decode, 5, &result, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(10, result);
ASSERT_UINT_EQUALS(0, to_decode.len);
return AWS_OP_SUCCESS;
}
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_blank) {
struct decode_fixture *fixture = ctx;
uint8_t input[] = {0};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
struct aws_byte_buf output;
ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 4));
bool complete;
ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
ASSERT_BIN_ARRAYS_EQUALS("", 0, output.buffer, output.len);
aws_byte_buf_clean_up(&output);
return AWS_OP_SUCCESS;
}
/* Test a string that is NOT Huffman encoded */
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_uncompressed) {
struct decode_fixture *fixture = ctx;
uint8_t input[] = {5, 'h', 'e', 'l', 'l', 'o'};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
struct aws_byte_buf output;
ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 5));
bool complete;
ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
ASSERT_BIN_ARRAYS_EQUALS("hello", 5, output.buffer, output.len);
aws_byte_buf_clean_up(&output);
return AWS_OP_SUCCESS;
}
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_huffman) {
struct decode_fixture *fixture = ctx;
/* This is Huffman-encoded "www.example.com", copied from:
* RFC-7541 - Request Examples with Huffman Coding - C.4.1. First Request */
uint8_t input[] = {0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
const char *expected = "www.example.com";
struct aws_byte_buf output;
ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, strlen(expected)));
bool complete;
ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), output.buffer, output.len);
aws_byte_buf_clean_up(&output);
return AWS_OP_SUCCESS;
}
/* Test that partial input doesn't register as "complete" */
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_ongoing) {
struct decode_fixture *fixture = ctx;
uint8_t input[] = {5, 'h', 'e', 'l'};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
struct aws_byte_buf output;
ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 5));
bool complete;
ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
ASSERT_FALSE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
aws_byte_buf_clean_up(&output);
return AWS_OP_SUCCESS;
}
/* Test that output buffer is gets resized if it's too small */
TEST_DECODE_ONE_BYTE_AT_A_TIME(hpack_decode_string_short_buffer) {
struct decode_fixture *fixture = ctx;
uint8_t input[] = {5, 'h', 'e', 'l', 'l', 'o'};
struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(input, AWS_ARRAY_SIZE(input));
struct aws_byte_buf output;
ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 1)); /* Note buffer is initially too small */
bool complete;
ASSERT_SUCCESS(s_decode_string(fixture, &to_decode, &output, &complete));
ASSERT_TRUE(complete);
ASSERT_UINT_EQUALS(0, to_decode.len);
ASSERT_BIN_ARRAYS_EQUALS("hello", 5, output.buffer, output.len);
aws_byte_buf_clean_up(&output);
return AWS_OP_SUCCESS;
}
#define DEFINE_STATIC_HEADER(_name, _header, _value) \
static const struct aws_http_header _name = { \
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_header), \
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \
}
AWS_TEST_CASE(hpack_static_table_find, test_hpack_static_table_find)
static int test_hpack_static_table_find(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_http_library_init(allocator);
struct aws_hpack_context context;
aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, 0));
bool found_value = false;
DEFINE_STATIC_HEADER(s_authority, ":authority", "amazon.com");
DEFINE_STATIC_HEADER(s_get, ":method", "GET");
DEFINE_STATIC_HEADER(s_other_method, ":method", "TEAPOT");
DEFINE_STATIC_HEADER(s_garbage, "colden's favorite ice cream flavor", "cookie dough");
/* Test header without value */
ASSERT_UINT_EQUALS(1, aws_hpack_find_index(&context, &s_authority, false, &found_value));
ASSERT_FALSE(found_value);
/* Test header with value */
ASSERT_UINT_EQUALS(2, aws_hpack_find_index(&context, &s_get, true, &found_value));
ASSERT_TRUE(found_value);
ASSERT_UINT_EQUALS(2, aws_hpack_find_index(&context, &s_other_method, true, &found_value));
ASSERT_FALSE(found_value);
/* Check invalid header */
ASSERT_UINT_EQUALS(0, aws_hpack_find_index(&context, &s_garbage, true, &found_value));
aws_hpack_context_clean_up(&context);
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(hpack_static_table_get, test_hpack_static_table_get)
static int test_hpack_static_table_get(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_http_library_init(allocator);
struct aws_hpack_context context;
aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, 0));
const struct aws_http_header *found = NULL;
DEFINE_STATIC_HEADER(s_get, ":path", "/index.html");
DEFINE_STATIC_HEADER(s_age, "age", "25");
found = aws_hpack_get_header(&context, 21);
ASSERT_NOT_NULL(found);
ASSERT_TRUE(aws_byte_cursor_eq(&s_age.name, &found->name));
ASSERT_NULL(found->value.ptr);
ASSERT_UINT_EQUALS(0, found->value.len);
found = aws_hpack_get_header(&context, 5);
ASSERT_NOT_NULL(found);
ASSERT_TRUE(aws_byte_cursor_eq(&s_get.name, &found->name));
ASSERT_TRUE(aws_byte_cursor_eq(&s_get.value, &found->value));
found = aws_hpack_get_header(&context, 69);
ASSERT_NULL(found);
aws_hpack_context_clean_up(&context);
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(hpack_dynamic_table_find, test_hpack_dynamic_table_find)
static int test_hpack_dynamic_table_find(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_http_library_init(allocator);
struct aws_hpack_context context;
aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
bool found_value = false;
DEFINE_STATIC_HEADER(s_herp, "herp", "derp");
DEFINE_STATIC_HEADER(s_herp2, "herp", "something else");
DEFINE_STATIC_HEADER(s_fizz, "fizz", "buzz");
/* Test single header */
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_herp));
ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &s_herp, true, &found_value));
ASSERT_TRUE(found_value);
ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &s_herp2, true, &found_value));
ASSERT_FALSE(found_value);
/* Test 2 headers */
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_fizz));
ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &s_fizz, true, &found_value));
ASSERT_TRUE(found_value);
ASSERT_UINT_EQUALS(63, aws_hpack_find_index(&context, &s_herp, true, &found_value));
ASSERT_TRUE(found_value);
ASSERT_UINT_EQUALS(63, aws_hpack_find_index(&context, &s_herp2, true, &found_value));
ASSERT_FALSE(found_value);
/* Test resizing up doesn't break anything */
ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, 8 * 1024 * 1024));
/* Check invalid header */
DEFINE_STATIC_HEADER(s_garbage, "colden's mother's maiden name", "nice try mr hacker");
ASSERT_UINT_EQUALS(0, aws_hpack_find_index(&context, &s_garbage, true, &found_value));
/* Test resizing so only the first element stays */
ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, aws_hpack_get_header_size(&s_fizz)));
ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &s_fizz, true, &found_value));
ASSERT_TRUE(found_value);
ASSERT_UINT_EQUALS(0, aws_hpack_find_index(&context, &s_herp, true, &found_value));
ASSERT_FALSE(found_value);
aws_hpack_context_clean_up(&context);
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(hpack_dynamic_table_get, test_hpack_dynamic_table_get)
static int test_hpack_dynamic_table_get(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_http_library_init(allocator);
struct aws_hpack_context context;
aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
const struct aws_http_header *found = NULL;
DEFINE_STATIC_HEADER(s_herp, "herp", "derp");
DEFINE_STATIC_HEADER(s_fizz, "fizz", "buzz");
DEFINE_STATIC_HEADER(s_status, ":status", "418");
/* Make the dynamic table only big enough for 2 headers */
ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(
&context, aws_hpack_get_header_size(&s_fizz) + aws_hpack_get_header_size(&s_status)));
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_herp));
found = aws_hpack_get_header(&context, 62);
ASSERT_NOT_NULL(found);
ASSERT_TRUE(aws_byte_cursor_eq(&s_herp.name, &found->name));
ASSERT_TRUE(aws_byte_cursor_eq(&s_herp.value, &found->value));
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_fizz));
found = aws_hpack_get_header(&context, 62);
ASSERT_NOT_NULL(found);
ASSERT_TRUE(aws_byte_cursor_eq(&s_fizz.name, &found->name));
ASSERT_TRUE(aws_byte_cursor_eq(&s_fizz.value, &found->value));
found = aws_hpack_get_header(&context, 63);
ASSERT_NOT_NULL(found);
ASSERT_TRUE(aws_byte_cursor_eq(&s_herp.name, &found->name));
ASSERT_TRUE(aws_byte_cursor_eq(&s_herp.value, &found->value));
/* This one will result in the first header being evicted */
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &s_status));
found = aws_hpack_get_header(&context, 62);
ASSERT_NOT_NULL(found);
ASSERT_TRUE(aws_byte_cursor_eq(&s_status.name, &found->name));
ASSERT_TRUE(aws_byte_cursor_eq(&s_status.value, &found->value));
found = aws_hpack_get_header(&context, 63);
ASSERT_NOT_NULL(found);
ASSERT_TRUE(aws_byte_cursor_eq(&s_fizz.name, &found->name));
ASSERT_TRUE(aws_byte_cursor_eq(&s_fizz.value, &found->value));
found = aws_hpack_get_header(&context, 64);
ASSERT_NULL(found);
/* Test resizing to evict entries */
ASSERT_SUCCESS(aws_hpack_resize_dynamic_table(&context, aws_hpack_get_header_size(&s_status)));
found = aws_hpack_get_header(&context, 62);
ASSERT_NOT_NULL(found);
ASSERT_TRUE(aws_byte_cursor_eq(&s_status.name, &found->name));
ASSERT_TRUE(aws_byte_cursor_eq(&s_status.value, &found->value));
found = aws_hpack_get_header(&context, 63);
ASSERT_NULL(found);
aws_hpack_context_clean_up(&context);
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
static int s_check_header(
const struct aws_http_header *header_field,
const char *name,
const char *value,
enum aws_http_header_compression compression) {
ASSERT_BIN_ARRAYS_EQUALS(name, strlen(name), header_field->name.ptr, header_field->name.len);
ASSERT_BIN_ARRAYS_EQUALS(value, strlen(value), header_field->value.ptr, header_field->value.len);
ASSERT_INT_EQUALS(compression, header_field->compression);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(hpack_decode_indexed_from_dynamic_table, test_hpack_decode_indexed_from_dynamic_table)
static int test_hpack_decode_indexed_from_dynamic_table(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_http_library_init(allocator);
struct aws_hpack_decoder decoder;
aws_hpack_decoder_init(&decoder, allocator, NULL);
/* clang-format off */
uint8_t input[] = {
0x48, 0x03, '3', '0', '2', /* ":status: 302" - stored to dynamic table */
0x40, 0x01, 'a', 0x01, 'b', /* "a: b" - stored to dynamic table */
/* So at this point dynamic table should look like:
* INDEX NAME VALUE
* 62 a b
* 63 :status 302
*/
0xbf, /* ":status: 302" - indexed from dynamic table */
};
/* clang-format on */
struct aws_hpack_decode_result result;
struct aws_byte_cursor input_cursor = aws_byte_cursor_from_array(input, sizeof(input));
/* Three entries in total, decode them all, and check the result */
/* First entry */
ASSERT_SUCCESS(aws_hpack_decode(&decoder, &input_cursor, &result));
ASSERT_TRUE(result.type == AWS_HPACK_DECODE_T_HEADER_FIELD);
ASSERT_SUCCESS(s_check_header(&result.data.header_field, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
/* Second entry */
ASSERT_SUCCESS(aws_hpack_decode(&decoder, &input_cursor, &result));
ASSERT_TRUE(result.type == AWS_HPACK_DECODE_T_HEADER_FIELD);
ASSERT_SUCCESS(s_check_header(&result.data.header_field, "a", "b", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
/* Third entry */
ASSERT_SUCCESS(aws_hpack_decode(&decoder, &input_cursor, &result));
ASSERT_TRUE(result.type == AWS_HPACK_DECODE_T_HEADER_FIELD);
ASSERT_SUCCESS(s_check_header(&result.data.header_field, ":status", "302", AWS_HTTP_HEADER_COMPRESSION_USE_CACHE));
/* Check the input is fully consumed */
ASSERT_TRUE(input_cursor.len == 0);
/* Clean up */
aws_hpack_decoder_clean_up(&decoder);
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
/* Test header with empty value */
AWS_TEST_CASE(hpack_dynamic_table_empty_value, test_hpack_dynamic_table_empty_value)
static int test_hpack_dynamic_table_empty_value(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_http_library_init(allocator);
struct aws_hpack_context context;
aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
DEFINE_STATIC_HEADER(header1, ":status", "302");
DEFINE_STATIC_HEADER(empty_value_header, "c", "");
DEFINE_STATIC_HEADER(header2, "a", "b");
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &header1));
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &empty_value_header));
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &header2));
/*
So at this point dynamic table should look like:
* INDEX NAME VALUE
* 62 a b
* 63 "c" ""
* 64 :status 302
*/
bool found_value = false;
ASSERT_UINT_EQUALS(64, aws_hpack_find_index(&context, &header1, true, &found_value));
ASSERT_UINT_EQUALS(63, aws_hpack_find_index(&context, &empty_value_header, true, &found_value));
ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &header2, true, &found_value));
/* Clean up */
aws_hpack_context_clean_up(&context);
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
/* Test header with empty name and value */
AWS_TEST_CASE(hpack_dynamic_table_with_empty_header, test_hpack_dynamic_table_with_empty_header)
static int test_hpack_dynamic_table_with_empty_header(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_http_library_init(allocator);
struct aws_hpack_context context;
aws_hpack_context_init(&context, allocator, AWS_LS_HTTP_GENERAL, NULL);
DEFINE_STATIC_HEADER(header1, ":status", "302");
DEFINE_STATIC_HEADER(empty_header, "", "");
DEFINE_STATIC_HEADER(header2, "a", "b");
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &header1));
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &empty_header));
ASSERT_SUCCESS(aws_hpack_insert_header(&context, &header2));
/*
So at this point dynamic table should look like:
* INDEX NAME VALUE
* 62 a b
* 63 "" ""
* 64 :status 302
*/
bool found_value = false;
ASSERT_UINT_EQUALS(64, aws_hpack_find_index(&context, &header1, true, &found_value));
ASSERT_UINT_EQUALS(63, aws_hpack_find_index(&context, &empty_header, true, &found_value));
ASSERT_UINT_EQUALS(62, aws_hpack_find_index(&context, &header2, true, &found_value));
/* Clean up */
aws_hpack_context_clean_up(&context);
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(hpack_dynamic_table_size_update_from_setting, test_hpack_dynamic_table_size_update_from_setting)
static int test_hpack_dynamic_table_size_update_from_setting(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_http_library_init(allocator);
struct aws_hpack_encoder encoder;
aws_hpack_encoder_init(&encoder, allocator, NULL);
/* let's pretend multiple times max size update happened from encoder setting */
aws_hpack_encoder_update_max_table_size(&encoder, 10);
aws_hpack_encoder_update_max_table_size(&encoder, 0);
aws_hpack_encoder_update_max_table_size(&encoder, 1337);
/* encode a header block */
struct aws_http_headers *headers = aws_http_headers_new(allocator);
/* the 2 entry of static table */
DEFINE_STATIC_HEADER(header, ":method", "GET");
ASSERT_SUCCESS(aws_http_headers_add_header(headers, &header));
struct aws_byte_buf output;
ASSERT_SUCCESS(aws_byte_buf_init(&output, allocator, 5));
ASSERT_SUCCESS(aws_hpack_encode_header_block(&encoder, headers, &output));
/* Check the output result, it should contain two dynamic table size updates, besides the header */
/**
* Expected first table size update (0 0 1) for dynamic table size update, rest is the integer with 5-bit Prefix:
* size is 0
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 32
* +---+---+---+---+---+---+---+---+
*
* Expected second table size update:
* size is 1337
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 63
* | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 154
* | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10
* +---+---+---+---+---+---+---+---+
*
* Expected header block: (1) for indexed header field, rest is the index, which is 2
* 0 1 2 3 4 5 6 7
* +---+---+---+---+---+---+---+---+
* | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 130
* +---+---+---+---+---+---+---+---+
*/
ASSERT_UINT_EQUALS(5, output.len);
ASSERT_UINT_EQUALS(32, output.buffer[0]);
ASSERT_UINT_EQUALS(63, output.buffer[1]);
ASSERT_UINT_EQUALS(154, output.buffer[2]);
ASSERT_UINT_EQUALS(10, output.buffer[3]);
ASSERT_UINT_EQUALS(130, output.buffer[4]);
/* clean up */
aws_byte_buf_clean_up(&output);
aws_http_headers_release(headers);
aws_hpack_encoder_clean_up(&encoder);
aws_http_library_clean_up();
return AWS_OP_SUCCESS;
}
|