1#line 2 "suites/host_test.function" 2 3/** 4 * \brief Verifies that string is in string parameter format i.e. "<str>" 5 * It also strips enclosing '"' from the input string. 6 * 7 * \param str String parameter. 8 * 9 * \return 0 if success else 1 10 */ 11int verify_string( char **str ) 12{ 13 if( ( *str )[0] != '"' || 14 ( *str )[strlen( *str ) - 1] != '"' ) 15 { 16 mbedtls_fprintf( stderr, 17 "Expected string (with \"\") for parameter and got: %s\n", *str ); 18 return( -1 ); 19 } 20 21 ( *str )++; 22 ( *str )[strlen( *str ) - 1] = '\0'; 23 24 return( 0 ); 25} 26 27/** 28 * \brief Verifies that string is an integer. Also gives the converted 29 * integer value. 30 * 31 * \param str Input string. 32 * \param value Pointer to int for output value. 33 * 34 * \return 0 if success else 1 35 */ 36int verify_int( char *str, int32_t *value ) 37{ 38 size_t i; 39 int minus = 0; 40 int digits = 1; 41 int hex = 0; 42 43 for( i = 0; i < strlen( str ); i++ ) 44 { 45 if( i == 0 && str[i] == '-' ) 46 { 47 minus = 1; 48 continue; 49 } 50 51 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) && 52 str[i - 1] == '0' && ( str[i] == 'x' || str[i] == 'X' ) ) 53 { 54 hex = 1; 55 continue; 56 } 57 58 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) || 59 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) || 60 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) ) 61 { 62 digits = 0; 63 break; 64 } 65 } 66 67 if( digits ) 68 { 69 if( hex ) 70 *value = strtol( str, NULL, 16 ); 71 else 72 *value = strtol( str, NULL, 10 ); 73 74 return( 0 ); 75 } 76 77 mbedtls_fprintf( stderr, 78 "Expected integer for parameter and got: %s\n", str ); 79 return( KEY_VALUE_MAPPING_NOT_FOUND ); 80} 81 82 83/** 84 * \brief Usage string. 85 * 86 */ 87#define USAGE \ 88 "Usage: %s [OPTIONS] files...\n\n" \ 89 " Command line arguments:\n" \ 90 " files... One or more test data files. If no file is\n" \ 91 " specified the following default test case\n" \ 92 " file is used:\n" \ 93 " %s\n\n" \ 94 " Options:\n" \ 95 " -v | --verbose Display full information about each test\n" \ 96 " -h | --help Display this information\n\n", \ 97 argv[0], \ 98 "TESTCASE_FILENAME" 99 100 101/** 102 * \brief Read a line from the passed file pointer. 103 * 104 * \param f FILE pointer 105 * \param buf Pointer to memory to hold read line. 106 * \param len Length of the buf. 107 * 108 * \return 0 if success else -1 109 */ 110int get_line( FILE *f, char *buf, size_t len ) 111{ 112 char *ret; 113 int i = 0, str_len = 0, has_string = 0; 114 115 /* Read until we get a valid line */ 116 do 117 { 118 ret = fgets( buf, len, f ); 119 if( ret == NULL ) 120 return( -1 ); 121 122 str_len = strlen( buf ); 123 124 /* Skip empty line and comment */ 125 if ( str_len == 0 || buf[0] == '#' ) 126 continue; 127 has_string = 0; 128 for ( i = 0; i < str_len; i++ ) 129 { 130 char c = buf[i]; 131 if ( c != ' ' && c != '\t' && c != '\n' && 132 c != '\v' && c != '\f' && c != '\r' ) 133 { 134 has_string = 1; 135 break; 136 } 137 } 138 } while( !has_string ); 139 140 /* Strip new line and carriage return */ 141 ret = buf + strlen( buf ); 142 if( ret-- > buf && *ret == '\n' ) 143 *ret = '\0'; 144 if( ret-- > buf && *ret == '\r' ) 145 *ret = '\0'; 146 147 return( 0 ); 148} 149 150/** 151 * \brief Splits string delimited by ':'. Ignores '\:'. 152 * 153 * \param buf Input string 154 * \param len Input string length 155 * \param params Out params found 156 * \param params_len Out params array len 157 * 158 * \return Count of strings found. 159 */ 160static int parse_arguments( char *buf, size_t len, char **params, 161 size_t params_len ) 162{ 163 size_t cnt = 0, i; 164 char *cur = buf; 165 char *p = buf, *q; 166 167 params[cnt++] = cur; 168 169 while( *p != '\0' && p < ( buf + len ) ) 170 { 171 if( *p == '\\' ) 172 { 173 p++; 174 p++; 175 continue; 176 } 177 if( *p == ':' ) 178 { 179 if( p + 1 < buf + len ) 180 { 181 cur = p + 1; 182 TEST_HELPER_ASSERT( cnt < params_len ); 183 params[cnt++] = cur; 184 } 185 *p = '\0'; 186 } 187 188 p++; 189 } 190 191 /* Replace newlines, question marks and colons in strings */ 192 for( i = 0; i < cnt; i++ ) 193 { 194 p = params[i]; 195 q = params[i]; 196 197 while( *p != '\0' ) 198 { 199 if( *p == '\\' && *( p + 1 ) == 'n' ) 200 { 201 p += 2; 202 *( q++ ) = '\n'; 203 } 204 else if( *p == '\\' && *( p + 1 ) == ':' ) 205 { 206 p += 2; 207 *( q++ ) = ':'; 208 } 209 else if( *p == '\\' && *( p + 1 ) == '?' ) 210 { 211 p += 2; 212 *( q++ ) = '?'; 213 } 214 else 215 *( q++ ) = *( p++ ); 216 } 217 *q = '\0'; 218 } 219 220 return( cnt ); 221} 222 223/** 224 * \brief Converts parameters into test function consumable parameters. 225 * Example: Input: {"int", "0", "char*", "Hello", 226 * "hex", "abef", "exp", "1"} 227 * Output: { 228 * 0, // Verified int 229 * "Hello", // Verified string 230 * 2, { 0xab, 0xef },// Converted len,hex pair 231 * 9600 // Evaluated expression 232 * } 233 * 234 * 235 * \param cnt Parameter array count. 236 * \param params Out array of found parameters. 237 * \param int_params_store Memory for storing processed integer parameters. 238 * 239 * \return 0 for success else 1 240 */ 241static int convert_params( size_t cnt , char ** params , int32_t * int_params_store ) 242{ 243 char ** cur = params; 244 char ** out = params; 245 int ret = DISPATCH_TEST_SUCCESS; 246 247 while ( cur < params + cnt ) 248 { 249 char * type = *cur++; 250 char * val = *cur++; 251 252 if ( strcmp( type, "char*" ) == 0 ) 253 { 254 if ( verify_string( &val ) == 0 ) 255 { 256 *out++ = val; 257 } 258 else 259 { 260 ret = ( DISPATCH_INVALID_TEST_DATA ); 261 break; 262 } 263 } 264 else if ( strcmp( type, "int" ) == 0 ) 265 { 266 if ( verify_int( val, int_params_store ) == 0 ) 267 { 268 *out++ = (char *) int_params_store++; 269 } 270 else 271 { 272 ret = ( DISPATCH_INVALID_TEST_DATA ); 273 break; 274 } 275 } 276 else if ( strcmp( type, "hex" ) == 0 ) 277 { 278 if ( verify_string( &val ) == 0 ) 279 { 280 *int_params_store = mbedtls_test_unhexify( 281 (unsigned char *) val, val ); 282 *out++ = val; 283 *out++ = (char *)(int_params_store++); 284 } 285 else 286 { 287 ret = ( DISPATCH_INVALID_TEST_DATA ); 288 break; 289 } 290 } 291 else if ( strcmp( type, "exp" ) == 0 ) 292 { 293 int exp_id = strtol( val, NULL, 10 ); 294 if ( get_expression ( exp_id, int_params_store ) == 0 ) 295 { 296 *out++ = (char *)int_params_store++; 297 } 298 else 299 { 300 ret = ( DISPATCH_INVALID_TEST_DATA ); 301 break; 302 } 303 } 304 else 305 { 306 ret = ( DISPATCH_INVALID_TEST_DATA ); 307 break; 308 } 309 } 310 return( ret ); 311} 312 313/** 314 * \brief Tests snprintf implementation with test input. 315 * 316 * \note 317 * At high optimization levels (e.g. gcc -O3), this function may be 318 * inlined in run_test_snprintf. This can trigger a spurious warning about 319 * potential misuse of snprintf from gcc -Wformat-truncation (observed with 320 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc 321 * only. They are still valid for other compilers. Avoid this warning by 322 * forbidding inlining of this function by gcc. 323 * 324 * \param n Buffer test length. 325 * \param ref_buf Expected buffer. 326 * \param ref_ret Expected snprintf return value. 327 * 328 * \return 0 for success else 1 329 */ 330#if defined(__GNUC__) 331__attribute__((__noinline__)) 332#endif 333static int test_snprintf( size_t n, const char *ref_buf, int ref_ret ) 334{ 335 int ret; 336 char buf[10] = "xxxxxxxxx"; 337 const char ref[10] = "xxxxxxxxx"; 338 339 if( n >= sizeof( buf ) ) 340 return( -1 ); 341 ret = mbedtls_snprintf( buf, n, "%s", "123" ); 342 if( ret < 0 || (size_t) ret >= n ) 343 ret = -1; 344 345 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 || 346 ref_ret != ret || 347 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 ) 348 { 349 return( 1 ); 350 } 351 352 return( 0 ); 353} 354 355/** 356 * \brief Tests snprintf implementation. 357 * 358 * \param none 359 * 360 * \return 0 for success else 1 361 */ 362static int run_test_snprintf( void ) 363{ 364 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 || 365 test_snprintf( 1, "", -1 ) != 0 || 366 test_snprintf( 2, "1", -1 ) != 0 || 367 test_snprintf( 3, "12", -1 ) != 0 || 368 test_snprintf( 4, "123", 3 ) != 0 || 369 test_snprintf( 5, "123", 3 ) != 0 ); 370} 371 372 373/** 374 * \brief Desktop implementation of execute_tests(). 375 * Parses command line and executes tests from 376 * supplied or default data file. 377 * 378 * \param argc Command line argument count. 379 * \param argv Argument array. 380 * 381 * \return Program exit status. 382 */ 383int execute_tests( int argc , const char ** argv ) 384{ 385 /* Local Configurations and options */ 386 const char *default_filename = "DATA_FILE"; 387 const char *test_filename = NULL; 388 const char **test_files = NULL; 389 size_t testfile_count = 0; 390 int option_verbose = 0; 391 size_t function_id = 0; 392 393 /* Other Local variables */ 394 int arg_index = 1; 395 const char *next_arg; 396 size_t testfile_index, i, cnt; 397 int ret; 398 unsigned total_errors = 0, total_tests = 0, total_skipped = 0; 399 FILE *file; 400 char buf[5000]; 401 char *params[50]; 402 /* Store for proccessed integer params. */ 403 int32_t int_params[50]; 404 void *pointer; 405#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 406 int stdout_fd = -1; 407#endif /* __unix__ || __APPLE__ __MACH__ */ 408 409#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ 410 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) 411 unsigned char alloc_buf[1000000]; 412 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) ); 413#endif 414 415#if defined(MBEDTLS_TEST_MUTEX_USAGE) 416 mbedtls_test_mutex_usage_init( ); 417#endif 418 419 /* 420 * The C standard doesn't guarantee that all-bits-0 is the representation 421 * of a NULL pointer. We do however use that in our code for initializing 422 * structures, which should work on every modern platform. Let's be sure. 423 */ 424 memset( &pointer, 0, sizeof( void * ) ); 425 if( pointer != NULL ) 426 { 427 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" ); 428 return( 1 ); 429 } 430 431 /* 432 * Make sure we have a snprintf that correctly zero-terminates 433 */ 434 if( run_test_snprintf() != 0 ) 435 { 436 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" ); 437 return( 1 ); 438 } 439 440 while( arg_index < argc ) 441 { 442 next_arg = argv[arg_index]; 443 444 if( strcmp( next_arg, "--verbose" ) == 0 || 445 strcmp( next_arg, "-v" ) == 0 ) 446 { 447 option_verbose = 1; 448 } 449 else if( strcmp(next_arg, "--help" ) == 0 || 450 strcmp(next_arg, "-h" ) == 0 ) 451 { 452 mbedtls_fprintf( stdout, USAGE ); 453 mbedtls_exit( EXIT_SUCCESS ); 454 } 455 else 456 { 457 /* Not an option, therefore treat all further arguments as the file 458 * list. 459 */ 460 test_files = &argv[ arg_index ]; 461 testfile_count = argc - arg_index; 462 } 463 464 arg_index++; 465 } 466 467 /* If no files were specified, assume a default */ 468 if ( test_files == NULL || testfile_count == 0 ) 469 { 470 test_files = &default_filename; 471 testfile_count = 1; 472 } 473 474 /* Initialize the struct that holds information about the last test */ 475 memset( &test_info, 0, sizeof( test_info ) ); 476 477 /* Now begin to execute the tests in the testfiles */ 478 for ( testfile_index = 0; 479 testfile_index < testfile_count; 480 testfile_index++ ) 481 { 482 size_t unmet_dep_count = 0; 483 int unmet_dependencies[20]; 484 int missing_unmet_dependencies = 0; 485 486 test_filename = test_files[ testfile_index ]; 487 488 file = fopen( test_filename, "r" ); 489 if( file == NULL ) 490 { 491 mbedtls_fprintf( stderr, "Failed to open test file: %s\n", 492 test_filename ); 493 return( 1 ); 494 } 495 496 while( !feof( file ) ) 497 { 498 if( unmet_dep_count > 0 ) 499 { 500 mbedtls_fprintf( stderr, 501 "FATAL: Dep count larger than zero at start of loop\n" ); 502 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); 503 } 504 unmet_dep_count = 0; 505 missing_unmet_dependencies = 0; 506 507 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) 508 break; 509 mbedtls_fprintf( stdout, "%s%.66s", 510 test_info.result == TEST_RESULT_FAILED ? "\n" : "", buf ); 511 mbedtls_fprintf( stdout, " " ); 512 for( i = strlen( buf ) + 1; i < 67; i++ ) 513 mbedtls_fprintf( stdout, "." ); 514 mbedtls_fprintf( stdout, " " ); 515 fflush( stdout ); 516 517 total_tests++; 518 519 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 ) 520 break; 521 cnt = parse_arguments( buf, strlen( buf ), params, 522 sizeof( params ) / sizeof( params[0] ) ); 523 524 if( strcmp( params[0], "depends_on" ) == 0 ) 525 { 526 for( i = 1; i < cnt; i++ ) 527 { 528 int dep_id = strtol( params[i], NULL, 10 ); 529 if( dep_check( dep_id ) != DEPENDENCY_SUPPORTED ) 530 { 531 if( unmet_dep_count < 532 ARRAY_LENGTH( unmet_dependencies ) ) 533 { 534 unmet_dependencies[unmet_dep_count] = dep_id; 535 unmet_dep_count++; 536 } 537 else 538 { 539 missing_unmet_dependencies = 1; 540 } 541 } 542 } 543 544 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 ) 545 break; 546 cnt = parse_arguments( buf, strlen( buf ), params, 547 sizeof( params ) / sizeof( params[0] ) ); 548 } 549 550 // If there are no unmet dependencies execute the test 551 if( unmet_dep_count == 0 ) 552 { 553 test_info.result = TEST_RESULT_SUCCESS; 554 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_IDLE; 555 556#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 557 /* Suppress all output from the library unless we're verbose 558 * mode 559 */ 560 if( !option_verbose ) 561 { 562 stdout_fd = redirect_output( stdout, "/dev/null" ); 563 if( stdout_fd == -1 ) 564 { 565 /* Redirection has failed with no stdout so exit */ 566 exit( 1 ); 567 } 568 } 569#endif /* __unix__ || __APPLE__ __MACH__ */ 570 571 function_id = strtoul( params[0], NULL, 10 ); 572 if ( (ret = check_test( function_id )) == DISPATCH_TEST_SUCCESS ) 573 { 574 ret = convert_params( cnt - 1, params + 1, int_params ); 575 if ( DISPATCH_TEST_SUCCESS == ret ) 576 { 577 ret = dispatch_test( function_id, (void **)( params + 1 ) ); 578 } 579 } 580 581#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) 582 if( !option_verbose && restore_output( stdout, stdout_fd ) ) 583 { 584 /* Redirection has failed with no stdout so exit */ 585 exit( 1 ); 586 } 587#endif /* __unix__ || __APPLE__ __MACH__ */ 588 589 } 590 591 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE ) 592 { 593 total_skipped++; 594 mbedtls_fprintf( stdout, "----" ); 595 596 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE ) 597 { 598 mbedtls_fprintf( stdout, "\n Test Suite not enabled" ); 599 } 600 601 if( 1 == option_verbose && unmet_dep_count > 0 ) 602 { 603 mbedtls_fprintf( stdout, "\n Unmet dependencies: " ); 604 for( i = 0; i < unmet_dep_count; i++ ) 605 { 606 mbedtls_fprintf( stdout, "%d ", 607 unmet_dependencies[i] ); 608 } 609 if( missing_unmet_dependencies ) 610 mbedtls_fprintf( stdout, "..." ); 611 } 612 mbedtls_fprintf( stdout, "\n" ); 613 fflush( stdout ); 614 615 unmet_dep_count = 0; 616 missing_unmet_dependencies = 0; 617 } 618 else if( ret == DISPATCH_TEST_SUCCESS ) 619 { 620 if( test_info.result == TEST_RESULT_SUCCESS ) 621 { 622 mbedtls_fprintf( stdout, "PASS\n" ); 623 } 624 else if( test_info.result == TEST_RESULT_SKIPPED ) 625 { 626 mbedtls_fprintf( stdout, "----\n" ); 627 total_skipped++; 628 } 629 else 630 { 631 total_errors++; 632 mbedtls_fprintf( stdout, "FAILED\n" ); 633 mbedtls_fprintf( stdout, " %s\n at line %d, %s\n", 634 test_info.test, test_info.line_no, 635 test_info.filename ); 636 } 637 fflush( stdout ); 638 } 639 else if( ret == DISPATCH_INVALID_TEST_DATA ) 640 { 641 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" ); 642 fclose( file ); 643 mbedtls_exit( 2 ); 644 } 645 else if( ret == DISPATCH_TEST_FN_NOT_FOUND ) 646 { 647 mbedtls_fprintf( stderr, "FAILED: FATAL TEST FUNCTION NOT FUND\n" ); 648 fclose( file ); 649 mbedtls_exit( 2 ); 650 } 651 else 652 total_errors++; 653 } 654 fclose( file ); 655 } 656 657 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n"); 658 if( total_errors == 0 ) 659 mbedtls_fprintf( stdout, "PASSED" ); 660 else 661 mbedtls_fprintf( stdout, "FAILED" ); 662 663 mbedtls_fprintf( stdout, " (%u / %u tests (%u skipped))\n", 664 total_tests - total_errors, total_tests, total_skipped ); 665 666#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ 667 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) 668#if defined(MBEDTLS_MEMORY_DEBUG) 669 mbedtls_memory_buffer_alloc_status(); 670#endif 671 mbedtls_memory_buffer_alloc_free(); 672#endif 673 674 return( total_errors != 0 ); 675} 676