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 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
|
/* BEGIN_HEADER */
#include <stdlib.h>
#include "mps_reader.h"
/*
* Compile-time configuration for test suite.
*/
/* Comment/Uncomment this to disable/enable the
* testing of the various MPS layers.
* This can be useful for time-consuming instrumentation
* tasks such as the conversion of E-ACSL annotations
* into runtime assertions. */
#define TEST_SUITE_MPS_READER
/* End of compile-time configuration. */
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_SSL_PROTO_TLS1_3
* END_DEPENDENCIES
*/
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_no_pausing_single_step_single_round( int with_acc )
{
/* This test exercises the most basic use of the MPS reader:
* - The 'producing' layer provides a buffer
* - The 'consuming' layer fetches it in a single go.
* - After processing, the consuming layer commits the data
* and the reader is moved back to producing mode.
*
* Parameters:
* - with_acc: 0 if the reader should be initialized without accumulator.
* 1 if the reader should be initialized with accumulator.
*
* Whether the accumulator is present or not should not matter,
* since the consumer's request can be fulfilled from the data
* that the producer has provided.
*/
unsigned char bufA[100];
unsigned char acc[10];
unsigned char *tmp;
int paused;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i;
/* Preparation (lower layer) */
if( with_acc == 0 )
mbedtls_mps_reader_init( &rd, NULL, 0 );
else
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */
/* Consume exactly what's available */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, bufA, 100 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, &paused ) == 0 );
TEST_ASSERT( paused == 0 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_no_pausing_single_step_multiple_rounds( int with_acc )
{
/* This test exercises multiple rounds of the basic use of the MPS reader:
* - The 'producing' layer provides a buffer
* - The 'consuming' layer fetches it in a single go.
* - After processing, the consuming layer commits the data
* and the reader is moved back to producing mode.
*
* Parameters:
* - with_acc: 0 if the reader should be initialized without accumulator.
* 1 if the reader should be initialized with accumulator.
*
* Whether the accumulator is present or not should not matter,
* since the consumer's request can be fulfilled from the data
* that the producer has provided.
*/
unsigned char bufA[100], bufB[100];
unsigned char acc[10];
unsigned char *tmp;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i;
for( size_t i=0; (unsigned) i < sizeof( bufB ); i++ )
bufB[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */
if( with_acc == 0 )
mbedtls_mps_reader_init( &rd, NULL, 0 );
else
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */
/* Consume exactly what's available */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, bufA, 100 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Preparation */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
/* Consumption */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, bufB, 100 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_no_pausing_multiple_steps_single_round( int with_acc )
{
/* This test exercises one round of the following:
* - The 'producing' layer provides a buffer
* - The 'consuming' layer fetches it in multiple calls
* to `mbedtls_mps_reader_get()`, without committing in between.
* - After processing, the consuming layer commits the data
* and the reader is moved back to producing mode.
*
* Parameters:
* - with_acc: 0 if the reader should be initialized without accumulator.
* 1 if the reader should be initialized with accumulator.
*
* Whether the accumulator is present or not should not matter,
* since the consumer's requests can be fulfilled from the data
* that the producer has provided.
*/
/* Lower layer provides data that the upper layer fully consumes
* through multiple `get` calls. */
unsigned char buf[100];
unsigned char acc[10];
unsigned char *tmp;
mbedtls_mps_size_t tmp_len;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i;
/* Preparation (lower layer) */
if( with_acc == 0 )
mbedtls_mps_reader_init( &rd, NULL, 0 );
else
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, buf, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 70, buf + 10, 70 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 );
ASSERT_COMPARE( tmp, tmp_len, buf + 80, 20 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_no_pausing_multiple_steps_multiple_rounds( int with_acc )
{
/* This test exercises one round of fetching a buffer in multiple chunks
* and passing it back to the producer afterwards, followed by another
* single-step sequence of feed-fetch-commit-reclaim.
*/
unsigned char bufA[100], bufB[100];
unsigned char acc[10];
unsigned char *tmp;
mbedtls_mps_size_t tmp_len;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i;
for( size_t i=0; (unsigned) i < sizeof( bufB ); i++ )
bufB[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */
if( with_acc == 0 )
mbedtls_mps_reader_init( &rd, NULL, 0 );
else
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 70, bufA + 10, 70 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 );
ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 20 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Preparation */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
/* Consumption */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, bufB, 100 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_pausing_needed_disabled()
{
/* This test exercises the behaviour of the MPS reader when a read request
* of the consumer exceeds what has been provided by the producer, and when
* no accumulator is available in the reader.
*
* In this case, we expect the reader to fail.
*/
unsigned char buf[100];
unsigned char *tmp;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i;
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, NULL, 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf, 50 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_pausing_needed_buffer_too_small()
{
/* This test exercises the behaviour of the MPS reader with accumulator
* in the situation where a read request goes beyond the bounds of the
* current read buffer, _and_ the reader's accumulator is too small to
* hold the requested amount of data.
*
* In this case, we expect mbedtls_mps_reader_reclaim() to fail,
* but it should be possible to continue fetching data as if
* there had been no excess request via mbedtls_mps_reader_get()
* and the call to mbedtls_mps_reader_reclaim() had been rejected
* because of data remaining.
*/
unsigned char buf[100];
unsigned char acc[10];
unsigned char *tmp;
mbedtls_mps_reader rd;
mbedtls_mps_size_t tmp_len;
for( size_t i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i;
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf, 50 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, buf + 50, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, &tmp_len ) == 0 );
ASSERT_COMPARE( tmp, tmp_len, buf + 50, 50 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_reclaim_overflow()
{
/* This test exercises the behaviour of the MPS reader with accumulator
* in the situation where upon calling mbedtls_mps_reader_reclaim(), the
* uncommitted data together with the excess data missing in the last
* call to mbedtls_mps_reader_get() exceeds the bounds of the type
* holding the buffer length.
*/
unsigned char buf[100];
unsigned char acc[50];
unsigned char *tmp;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i;
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf, 50 );
/* Excess request */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, (mbedtls_mps_size_t) -1, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_pausing( int option )
{
/* This test exercises the behaviour of the reader when the
* accumulator is used to fulfill a consumer's request.
*
* More detailed:
* - The producer feeds some data.
* - The consumer asks for more data than what's available.
* - The reader remembers the request and goes back to
* producing mode, waiting for more data from the producer.
* - The producer provides another chunk of data which is
* sufficient to fulfill the original read request.
* - The consumer retries the original read request, which
* should now succeed.
*
* This test comes in multiple variants controlled by the
* `option` parameter and documented below.
*/
unsigned char bufA[100], bufB[100];
unsigned char *tmp;
unsigned char acc[40];
int paused;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i;
for( size_t i=0; (unsigned) i < sizeof( bufB ); i++ )
bufB[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */
/* Ask for more than what's available. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 80, bufA, 80 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
switch( option )
{
case 0: /* Single uncommitted fetch at pausing */
case 1:
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
break;
default: /* Multiple uncommitted fetches at pausing */
break;
}
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Preparation */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, &paused ) == 0 );
TEST_ASSERT( paused == 1 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
/* Consumption */
switch( option )
{
case 0: /* Single fetch at pausing, re-fetch with commit. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
break;
case 1: /* Single fetch at pausing, re-fetch without commit. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
break;
case 2: /* Multiple fetches at pausing, repeat without commit. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
break;
case 3: /* Multiple fetches at pausing, repeat with commit 1. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
break;
case 4: /* Multiple fetches at pausing, repeat with commit 2. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
break;
case 5: /* Multiple fetches at pausing, repeat with commit 3. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
break;
default:
TEST_ASSERT( 0 );
}
/* In all cases, fetch the rest of the second buffer. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 90, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 90, bufB + 10, 90 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_pausing_multiple_feeds( int option )
{
/* This test exercises the behaviour of the MPS reader
* in the following situation:
* - The consumer has asked for more than what's available, so the
* reader pauses and waits for further input data via
* `mbedtls_mps_reader_feed()`
* - Multiple such calls to `mbedtls_mps_reader_feed()` are necessary
* to fulfill the original request, and the reader needs to do
* the necessary bookkeeping under the hood.
*
* This test comes in a few variants differing in the number and
* size of feed calls that the producer issues while the reader is
* accumulating the necessary data - see the comments below.
*/
unsigned char bufA[100], bufB[100];
unsigned char *tmp;
unsigned char acc[70];
mbedtls_mps_reader rd;
mbedtls_mps_size_t fetch_len;
for( size_t i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i;
for( size_t i=0; (unsigned) i < sizeof( bufB ); i++ )
bufB[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */
/* Ask for more than what's available. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 80, bufA, 80 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* 20 left, ask for 70 -> 50 overhead */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Preparation */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
switch( option )
{
case 0: /* 10 + 10 + 80 byte feed */
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, 10 ) ==
MBEDTLS_ERR_MPS_READER_NEED_MORE );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 10, 10 ) ==
MBEDTLS_ERR_MPS_READER_NEED_MORE );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 20, 80 ) == 0 );
break;
case 1: /* 50 x 1byte */
for( size_t num_feed = 0; num_feed < 49; num_feed++ )
{
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + num_feed, 1 ) ==
MBEDTLS_ERR_MPS_READER_NEED_MORE );
}
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 49, 1 ) == 0 );
break;
case 2: /* 49 x 1byte + 51bytes */
for( size_t num_feed = 0; num_feed < 49; num_feed++ )
{
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + num_feed, 1 ) ==
MBEDTLS_ERR_MPS_READER_NEED_MORE );
}
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 49, 51 ) == 0 );
break;
default:
TEST_ASSERT( 0 );
break;
}
/* Consumption */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
ASSERT_COMPARE( tmp + 20, 50, bufB, 50 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 1000, &tmp, &fetch_len ) == 0 );
switch( option )
{
case 0:
TEST_ASSERT( fetch_len == 50 );
break;
case 1:
TEST_ASSERT( fetch_len == 0 );
break;
case 2:
TEST_ASSERT( fetch_len == 50 );
break;
default:
TEST_ASSERT( 0 );
break;
}
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_reclaim_data_left( int option )
{
/* This test exercises the behaviour of the MPS reader when a
* call to mbedtls_mps_reader_reclaim() is made before all data
* provided by the producer has been fetched and committed. */
unsigned char buf[100];
unsigned char *tmp;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i;
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, NULL, 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */
switch( option )
{
case 0:
/* Fetch (but not commit) the entire buffer. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ), &tmp, NULL )
== 0 );
ASSERT_COMPARE( tmp, 100, buf, 100 );
break;
case 1:
/* Fetch (but not commit) parts of the buffer. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
&tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 );
break;
case 2:
/* Fetch and commit parts of the buffer, then
* fetch but not commit the rest of the buffer. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
&tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
&tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, sizeof( buf ) / 2,
buf + sizeof( buf ) / 2,
sizeof( buf ) / 2 );
break;
default:
TEST_ASSERT( 0 );
break;
}
/* Wrapup */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_DATA_LEFT );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_reclaim_data_left_retry()
{
/* This test exercises the behaviour of the MPS reader when an attempt
* by the producer to reclaim the reader fails because of more data pending
* to be processed, and the consumer subsequently fetches more data. */
unsigned char buf[100];
unsigned char *tmp;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i;
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, NULL, 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf, 50 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf + 50, 50 );
/* Preparation */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_DATA_LEFT );
/* Consumption */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf + 50, 50 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_multiple_pausing( int option )
{
/* This test exercises the behaviour of the MPS reader
* in the following situation:
* - A read request via `mbedtls_mps_reader_get()` can't
* be served and the reader is paused to accumulate
* the desired amount of data from the producer.
* - Once enough data is available, the consumer successfully
* reads the data from the reader, but afterwards exceeds
* the available data again - pausing is necessary for a
* second time.
*/
unsigned char bufA[100], bufB[20], bufC[10];
unsigned char *tmp;
unsigned char acc[50];
mbedtls_mps_size_t tmp_len;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i;
for( size_t i=0; (unsigned) i < sizeof( bufB ); i++ )
bufB[i] = ~ ((unsigned char) i);
for( size_t i=0; (unsigned) i < sizeof( bufC ); i++ )
bufC[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */
/* Ask for more than what's available. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 80, bufA, 80 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Preparation */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
switch( option )
{
case 0: /* Fetch same chunks, commit afterwards, and
* then exceed bounds of new buffer; accumulator
* large enough. */
/* Consume */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, &tmp_len ) == 0 );
ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Prepare */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );;
/* Consume */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufB + 10, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufC, 10 );
break;
case 1: /* Fetch same chunks, commit afterwards, and
* then exceed bounds of new buffer; accumulator
* not large enough. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 51, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Prepare */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
break;
case 2: /* Fetch same chunks, don't commit afterwards, and
* then exceed bounds of new buffer; accumulator
* large enough. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Prepare */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );;
/* Consume */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
ASSERT_COMPARE( tmp + 20, 20, bufB, 20 );
ASSERT_COMPARE( tmp + 40, 10, bufC, 10 );
break;
case 3: /* Fetch same chunks, don't commit afterwards, and
* then exceed bounds of new buffer; accumulator
* not large enough. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 21, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Prepare */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
break;
default:
TEST_ASSERT( 0 );
break;
}
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER:MBEDTLS_MPS_STATE_VALIDATION */
void mbedtls_mps_reader_random_usage( int num_out_chunks,
int max_chunk_size,
int max_request,
int acc_size )
{
/* Randomly pass a reader object back and forth between lower and
* upper layer and let each of them call the respective reader API
* functions in a random fashion.
*
* On the lower layer, we're tracking and concatenating
* the data passed to successful feed calls.
*
* For the upper layer, we track and concatenate buffers
* obtained from successful get calls.
*
* As long as the lower layer calls reclaim at least once, (resetting the
* fetched but not-yet-committed data), this should always lead to the same
* stream of outgoing/incoming data for the lower/upper layers, even if
* most of the random calls fail.
*
* NOTE: This test uses rand() for random data, which is not optimal.
* Instead, it would be better to get the random data from a
* static buffer. This both eases reproducibility and allows
* simple conversion to a fuzz target.
*/
int ret;
unsigned char *acc = NULL;
unsigned char *outgoing = NULL, *incoming = NULL;
unsigned char *cur_chunk = NULL;
size_t cur_out_chunk, out_pos, in_commit, in_fetch;
int rand_op; /* Lower layer:
* - Reclaim (0)
* - Feed (1)
* Upper layer:
* - Get, do tolerate smaller output (0)
* - Get, don't tolerate smaller output (1)
* - Commit (2) */
int mode = 0; /* Lower layer (0) or Upper layer (1) */
int reclaimed = 1; /* Have to call reclaim at least once before
* returning the reader to the upper layer. */
mbedtls_mps_reader rd;
if( acc_size > 0 )
{
ASSERT_ALLOC( acc, acc_size );
}
/* This probably needs to be changed because we want
* our tests to be deterministic. */
// srand( time( NULL ) );
ASSERT_ALLOC( outgoing, num_out_chunks * max_chunk_size );
ASSERT_ALLOC( incoming, num_out_chunks * max_chunk_size );
mbedtls_mps_reader_init( &rd, acc, acc_size );
cur_out_chunk = 0;
in_commit = 0;
in_fetch = 0;
out_pos = 0;
while( cur_out_chunk < (unsigned) num_out_chunks )
{
if( mode == 0 )
{
/* Choose randomly between reclaim and feed */
rand_op = rand() % 2;
if( rand_op == 0 )
{
/* Reclaim */
ret = mbedtls_mps_reader_reclaim( &rd, NULL );
if( ret == 0 )
{
TEST_ASSERT( cur_chunk != NULL );
mbedtls_free( cur_chunk );
cur_chunk = NULL;
}
reclaimed = 1;
}
else
{
/* Feed reader with a random chunk */
unsigned char *tmp = NULL;
size_t tmp_size;
if( cur_out_chunk == (unsigned) num_out_chunks )
continue;
tmp_size = ( rand() % max_chunk_size ) + 1;
ASSERT_ALLOC( tmp, tmp_size );
TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL, tmp, tmp_size ) == 0 );
ret = mbedtls_mps_reader_feed( &rd, tmp, tmp_size );
if( ret == 0 || ret == MBEDTLS_ERR_MPS_READER_NEED_MORE )
{
cur_out_chunk++;
memcpy( outgoing + out_pos, tmp, tmp_size );
out_pos += tmp_size;
}
if( ret == 0 )
{
TEST_ASSERT( cur_chunk == NULL );
cur_chunk = tmp;
}
else
{
mbedtls_free( tmp );
}
}
/* Randomly switch to consumption mode if reclaim
* was called at least once. */
if( reclaimed == 1 && rand() % 3 == 0 )
{
in_fetch = 0;
mode = 1;
}
}
else
{
/* Choose randomly between get tolerating fewer data,
* get not tolerating fewer data, and commit. */
rand_op = rand() % 3;
if( rand_op == 0 || rand_op == 1 )
{
mbedtls_mps_size_t get_size, real_size;
unsigned char *chunk_get;
get_size = ( rand() % max_request ) + 1;
if( rand_op == 0 )
{
ret = mbedtls_mps_reader_get( &rd, get_size, &chunk_get,
&real_size );
}
else
{
real_size = get_size;
ret = mbedtls_mps_reader_get( &rd, get_size, &chunk_get, NULL );
}
/* Check if output is in accordance with what was written */
if( ret == 0 )
{
memcpy( incoming + in_commit + in_fetch,
chunk_get, real_size );
TEST_ASSERT( memcmp( incoming + in_commit + in_fetch,
outgoing + in_commit + in_fetch,
real_size ) == 0 );
in_fetch += real_size;
}
}
else if( rand_op == 2 ) /* Commit */
{
ret = mbedtls_mps_reader_commit( &rd );
if( ret == 0 )
{
in_commit += in_fetch;
in_fetch = 0;
}
}
/* Randomly switch back to preparation */
if( rand() % 3 == 0 )
{
reclaimed = 0;
mode = 0;
}
}
}
/* Cleanup */
mbedtls_mps_reader_free( &rd );
mbedtls_free( incoming );
mbedtls_free( outgoing );
mbedtls_free( acc );
mbedtls_free( cur_chunk );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_reader_inconsistent_usage( int option )
{
/* This test exercises the behaviour of the MPS reader
* in the following situation:
* - The consumer asks for more data than what's available
* - The reader is paused and receives more data from the
* producer until the original read request can be fulfilled.
* - The consumer does not repeat the original request but
* requests data in a different way.
*
* The reader does not guarantee that inconsistent read requests
* after pausing will succeed, and this test triggers some cases
* where the request fails.
*/
unsigned char bufA[100], bufB[100];
unsigned char *tmp;
unsigned char acc[40];
mbedtls_mps_reader rd;
int success = 0;
for( size_t i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i;
for( size_t i=0; (unsigned) i < sizeof( bufB ); i++ )
bufB[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Preparation */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
/* Consumption */
switch( option )
{
case 0:
/* Ask for buffered data in a single chunk, no commit */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
ASSERT_COMPARE( tmp + 20, 10, bufB, 10 );
success = 1;
break;
case 1:
/* Ask for buffered data in a single chunk, with commit */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
ASSERT_COMPARE( tmp + 20, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
success = 1;
break;
case 2:
/* Ask for more than was requested when pausing, #1 */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 31, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
break;
case 3:
/* Ask for more than was requested when pausing #2 */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, (mbedtls_mps_size_t) -1, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
break;
case 4:
/* Asking for buffered data in different
* chunks than before CAN fail. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
break;
case 5:
/* Asking for buffered data different chunks
* than before NEED NOT fail - no commits */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
success = 1;
break;
case 6:
/* Asking for buffered data different chunks
* than before NEED NOT fail - intermediate commit */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
success = 1;
break;
case 7:
/* Asking for buffered data different chunks
* than before NEED NOT fail - end commit */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
success = 1;
break;
case 8:
/* Asking for buffered data different chunks
* than before NEED NOT fail - intermediate & end commit */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
success = 1;
break;
default:
TEST_ASSERT( 0 );
break;
}
if( success == 1 )
{
/* In all succeeding cases, fetch the rest of the second buffer. */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 90, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 90, bufB + 10, 90 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
}
/* Wrapup */
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
void mbedtls_mps_reader_feed_empty()
{
/* This test exercises the behaviour of the reader when it is
* fed with a NULL buffer. */
unsigned char buf[100];
unsigned char *tmp;
mbedtls_mps_reader rd;
for( size_t i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i;
/* Preparation (lower layer) */
mbedtls_mps_reader_init( &rd, NULL, 0 );
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, NULL, sizeof( buf ) ) ==
MBEDTLS_ERR_MPS_READER_INVALID_ARG );
/* Subsequent feed-calls should still succeed. */
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, buf, 100 );
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_mps_reader_free( &rd );
}
/* END_CASE */
|