1 #line 2 "suites/host_test.function"
4 * \brief Verifies that string is in string parameter format i.e. "<str>"
5 * It also strips enclosing '"' from the input string.
7 * \param str String parameter.
9 * \return 0 if success else 1
11 int verify_string( char **str )
13 if( ( *str )[0] != '"' ||
14 ( *str )[strlen( *str ) - 1] != '"' )
16 mbedtls_fprintf( stderr,
17 "Expected string (with \"\") for parameter and got: %s\n", *str );
22 ( *str )[strlen( *str ) - 1] = '\0';
28 * \brief Verifies that string is an integer. Also gives the converted
31 * \param str Input string.
32 * \param value Pointer to int for output value.
34 * \return 0 if success else 1
36 int verify_int( char *str, int *value )
43 for( i = 0; i < strlen( str ); i++ )
45 if( i == 0 && str[i] == '-' )
51 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
52 str[i - 1] == '0' && ( str[i] == 'x' || str[i] == 'X' ) )
58 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
59 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
60 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
70 *value = strtol( str, NULL, 16 );
72 *value = strtol( str, NULL, 10 );
77 mbedtls_fprintf( stderr,
78 "Expected integer for parameter and got: %s\n", str );
79 return( KEY_VALUE_MAPPING_NOT_FOUND );
84 * \brief Usage string.
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" \
95 " -v | --verbose Display full information about each test\n" \
96 " -h | --help Display this information\n\n", \
102 * \brief Read a line from the passed file pointer.
104 * \param f FILE pointer
105 * \param buf Pointer to memory to hold read line.
106 * \param len Length of the buf.
108 * \return 0 if success else -1
110 int get_line( FILE *f, char *buf, size_t len )
113 int i = 0, str_len = 0, has_string = 0;
115 /* Read until we get a valid line */
118 ret = fgets( buf, len, f );
122 str_len = strlen( buf );
124 /* Skip empty line and comment */
125 if ( str_len == 0 || buf[0] == '#' )
128 for ( i = 0; i < str_len; i++ )
131 if ( c != ' ' && c != '\t' && c != '\n' &&
132 c != '\v' && c != '\f' && c != '\r' )
138 } while( !has_string );
140 /* Strip new line and carriage return */
141 ret = buf + strlen( buf );
142 if( ret-- > buf && *ret == '\n' )
144 if( ret-- > buf && *ret == '\r' )
151 * \brief Splits string delimited by ':'. Ignores '\:'.
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
158 * \return Count of strings found.
160 static int parse_arguments( char *buf, size_t len, char **params,
169 while( *p != '\0' && p < ( buf + len ) )
179 if( p + 1 < buf + len )
182 assert( cnt < params_len );
191 /* Replace newlines, question marks and colons in strings */
192 for( i = 0; i < cnt; i++ )
199 if( *p == '\\' && *( p + 1 ) == 'n' )
204 else if( *p == '\\' && *( p + 1 ) == ':' )
209 else if( *p == '\\' && *( p + 1 ) == '?' )
224 * \brief Converts parameters into test function consumable parameters.
225 * Example: Input: {"int", "0", "char*", "Hello",
226 * "hex", "abef", "exp", "1"}
229 * "Hello", // Verified string
230 * 2, { 0xab, 0xef },// Converted len,hex pair
231 * 9600 // Evaluated expression
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.
239 * \return 0 for success else 1
241 static int convert_params( size_t cnt , char ** params , int * int_params_store )
243 char ** cur = params;
244 char ** out = params;
245 int ret = DISPATCH_TEST_SUCCESS;
247 while ( cur < params + cnt )
249 char * type = *cur++;
252 if ( strcmp( type, "char*" ) == 0 )
254 if ( verify_string( &val ) == 0 )
260 ret = ( DISPATCH_INVALID_TEST_DATA );
264 else if ( strcmp( type, "int" ) == 0 )
266 if ( verify_int( val, int_params_store ) == 0 )
268 *out++ = (char *) int_params_store++;
272 ret = ( DISPATCH_INVALID_TEST_DATA );
276 else if ( strcmp( type, "hex" ) == 0 )
278 if ( verify_string( &val ) == 0 )
280 *int_params_store = unhexify( (unsigned char *) val, val );
282 *out++ = (char *)(int_params_store++);
286 ret = ( DISPATCH_INVALID_TEST_DATA );
290 else if ( strcmp( type, "exp" ) == 0 )
292 int exp_id = strtol( val, NULL, 10 );
293 if ( get_expression ( exp_id, int_params_store ) == 0 )
295 *out++ = (char *)int_params_store++;
299 ret = ( DISPATCH_INVALID_TEST_DATA );
305 ret = ( DISPATCH_INVALID_TEST_DATA );
313 * \brief Tests snprintf implementation with test input.
316 * At high optimization levels (e.g. gcc -O3), this function may be
317 * inlined in run_test_snprintf. This can trigger a spurious warning about
318 * potential misuse of snprintf from gcc -Wformat-truncation (observed with
319 * gcc 7.2). This warning makes tests in run_test_snprintf redundant on gcc
320 * only. They are still valid for other compilers. Avoid this warning by
321 * forbidding inlining of this function by gcc.
323 * \param n Buffer test length.
324 * \param ref_buf Expected buffer.
325 * \param ref_ret Expected snprintf return value.
327 * \return 0 for success else 1
329 #if defined(__GNUC__)
330 __attribute__((__noinline__))
332 static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
335 char buf[10] = "xxxxxxxxx";
336 const char ref[10] = "xxxxxxxxx";
338 if( n >= sizeof( buf ) )
340 ret = mbedtls_snprintf( buf, n, "%s", "123" );
341 if( ret < 0 || (size_t) ret >= n )
344 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
346 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
355 * \brief Tests snprintf implementation.
359 * \return 0 for success else 1
361 static int run_test_snprintf( void )
363 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
364 test_snprintf( 1, "", -1 ) != 0 ||
365 test_snprintf( 2, "1", -1 ) != 0 ||
366 test_snprintf( 3, "12", -1 ) != 0 ||
367 test_snprintf( 4, "123", 3 ) != 0 ||
368 test_snprintf( 5, "123", 3 ) != 0 );
373 * \brief Desktop implementation of execute_tests().
374 * Parses command line and executes tests from
375 * supplied or default data file.
377 * \param argc Command line argument count.
378 * \param argv Argument array.
380 * \return Program exit status.
382 int execute_tests( int argc , const char ** argv )
384 /* Local Configurations and options */
385 const char *default_filename = "DATA_FILE";
386 const char *test_filename = NULL;
387 const char **test_files = NULL;
388 int testfile_count = 0;
389 int option_verbose = 0;
392 /* Other Local variables */
394 const char *next_arg;
395 int testfile_index, ret, i, cnt;
396 int total_errors = 0, total_tests = 0, total_skipped = 0;
400 /* Store for proccessed integer params. */
403 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
405 #endif /* __unix__ || __APPLE__ __MACH__ */
407 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
408 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
409 unsigned char alloc_buf[1000000];
410 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
414 * The C standard doesn't guarantee that all-bits-0 is the representation
415 * of a NULL pointer. We do however use that in our code for initializing
416 * structures, which should work on every modern platform. Let's be sure.
418 memset( &pointer, 0, sizeof( void * ) );
419 if( pointer != NULL )
421 mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
426 * Make sure we have a snprintf that correctly zero-terminates
428 if( run_test_snprintf() != 0 )
430 mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
434 while( arg_index < argc )
436 next_arg = argv[arg_index];
438 if( strcmp( next_arg, "--verbose" ) == 0 ||
439 strcmp( next_arg, "-v" ) == 0 )
443 else if( strcmp(next_arg, "--help" ) == 0 ||
444 strcmp(next_arg, "-h" ) == 0 )
446 mbedtls_fprintf( stdout, USAGE );
447 mbedtls_exit( EXIT_SUCCESS );
451 /* Not an option, therefore treat all further arguments as the file
454 test_files = &argv[ arg_index ];
455 testfile_count = argc - arg_index;
461 /* If no files were specified, assume a default */
462 if ( test_files == NULL || testfile_count == 0 )
464 test_files = &default_filename;
468 /* Initialize the struct that holds information about the last test */
469 memset( &test_info, 0, sizeof( test_info ) );
471 /* Now begin to execute the tests in the testfiles */
472 for ( testfile_index = 0;
473 testfile_index < testfile_count;
476 int unmet_dep_count = 0;
477 char *unmet_dependencies[20];
479 test_filename = test_files[ testfile_index ];
481 file = fopen( test_filename, "r" );
484 mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
489 while( !feof( file ) )
491 if( unmet_dep_count > 0 )
493 mbedtls_fprintf( stderr,
494 "FATAL: Dep count larger than zero at start of loop\n" );
495 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
499 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
501 mbedtls_fprintf( stdout, "%s%.66s", test_info.failed ? "\n" : "", buf );
502 mbedtls_fprintf( stdout, " " );
503 for( i = strlen( buf ) + 1; i < 67; i++ )
504 mbedtls_fprintf( stdout, "." );
505 mbedtls_fprintf( stdout, " " );
510 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
512 cnt = parse_arguments( buf, strlen( buf ), params,
513 sizeof( params ) / sizeof( params[0] ) );
515 if( strcmp( params[0], "depends_on" ) == 0 )
517 for( i = 1; i < cnt; i++ )
519 int dep_id = strtol( params[i], NULL, 10 );
520 if( dep_check( dep_id ) != DEPENDENCY_SUPPORTED )
522 if( 0 == option_verbose )
524 /* Only one count is needed if not verbose */
529 unmet_dependencies[ unmet_dep_count ] = strdup( params[i] );
530 if( unmet_dependencies[ unmet_dep_count ] == NULL )
532 mbedtls_fprintf( stderr, "FATAL: Out of memory\n" );
533 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
539 if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
541 cnt = parse_arguments( buf, strlen( buf ), params,
542 sizeof( params ) / sizeof( params[0] ) );
545 // If there are no unmet dependencies execute the test
546 if( unmet_dep_count == 0 )
548 test_info.failed = 0;
549 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_IDLE;
551 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
552 /* Suppress all output from the library unless we're verbose
555 if( !option_verbose )
557 stdout_fd = redirect_output( &stdout, "/dev/null" );
558 if( stdout_fd == -1 )
560 /* Redirection has failed with no stdout so exit */
564 #endif /* __unix__ || __APPLE__ __MACH__ */
566 function_id = strtol( params[0], NULL, 10 );
567 if ( (ret = check_test( function_id )) == DISPATCH_TEST_SUCCESS )
569 ret = convert_params( cnt - 1, params + 1, int_params );
570 if ( DISPATCH_TEST_SUCCESS == ret )
572 ret = dispatch_test( function_id, (void **)( params + 1 ) );
576 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
577 if( !option_verbose && restore_output( &stdout, stdout_fd ) )
579 /* Redirection has failed with no stdout so exit */
582 #endif /* __unix__ || __APPLE__ __MACH__ */
586 if( unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE )
589 mbedtls_fprintf( stdout, "----" );
591 if( 1 == option_verbose && ret == DISPATCH_UNSUPPORTED_SUITE )
593 mbedtls_fprintf( stdout, "\n Test Suite not enabled" );
596 if( 1 == option_verbose && unmet_dep_count > 0 )
598 mbedtls_fprintf( stdout, "\n Unmet dependencies: " );
599 for( i = 0; i < unmet_dep_count; i++ )
601 mbedtls_fprintf( stdout, "%s ",
602 unmet_dependencies[i] );
603 free( unmet_dependencies[i] );
606 mbedtls_fprintf( stdout, "\n" );
611 else if( ret == DISPATCH_TEST_SUCCESS )
613 if( test_info.failed == 0 )
615 mbedtls_fprintf( stdout, "PASS\n" );
620 mbedtls_fprintf( stdout, "FAILED\n" );
621 mbedtls_fprintf( stdout, " %s\n at line %d, %s\n",
622 test_info.test, test_info.line_no,
623 test_info.filename );
627 else if( ret == DISPATCH_INVALID_TEST_DATA )
629 mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
633 else if( ret == DISPATCH_TEST_FN_NOT_FOUND )
635 mbedtls_fprintf( stderr, "FAILED: FATAL TEST FUNCTION NOT FUND\n" );
644 /* In case we encounter early end of file */
645 for( i = 0; i < unmet_dep_count; i++ )
646 free( unmet_dependencies[i] );
649 mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
650 if( total_errors == 0 )
651 mbedtls_fprintf( stdout, "PASSED" );
653 mbedtls_fprintf( stdout, "FAILED" );
655 mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
656 total_tests - total_errors, total_tests, total_skipped );
658 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
659 !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
660 #if defined(MBEDTLS_MEMORY_DEBUG)
661 mbedtls_memory_buffer_alloc_status();
663 mbedtls_memory_buffer_alloc_free();
666 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
667 if( stdout_fd != -1 )
668 close_output( stdout );
669 #endif /* __unix__ || __APPLE__ __MACH__ */
671 return( total_errors != 0 );