host_test.function 26 KB

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