From 5605591cc183c5d31e0e8dd17ffea9c1432118fd Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 1 Mar 2019 14:26:30 +0100 Subject: [PATCH] Report step number when a test case fails Allow test code to declare a "step number". Report the current step number when a test fails. --- tests/suites/helpers.function | 14 ++++++++++++++ tests/suites/host_test.function | 13 ++++++++++--- tests/suites/target_test.function | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index 1a524a677..d45fd4ea7 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -393,6 +393,7 @@ static struct const char *test; const char *filename; int line_no; + unsigned long step; } test_info; @@ -423,6 +424,19 @@ jmp_buf jmp_tmp; /*----------------------------------------------------------------------------*/ /* Helper Functions */ +/** Set the test step number for failure reports. + * + * Call this function to display "step NNN" in addition to the line number + * and file name if a test fails. Typically the "step number" is the index + * of a for loop but it can be whatever you want. + * + * \param step The step number to report. + */ +void test_set_step( unsigned long step ) +{ + test_info.step = step; +} + void test_fail( const char *test, int line_no, const char* filename ) { test_info.result = TEST_RESULT_FAILED; diff --git a/tests/suites/host_test.function b/tests/suites/host_test.function index 0f98d23aa..24d9b9747 100644 --- a/tests/suites/host_test.function +++ b/tests/suites/host_test.function @@ -548,6 +548,7 @@ int execute_tests( int argc , const char ** argv ) { test_info.result = TEST_RESULT_SUCCESS; test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_IDLE; + test_info.step = (unsigned long)( -1 ); #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) /* Suppress all output from the library unless we're verbose @@ -624,9 +625,15 @@ int execute_tests( int argc , const char ** argv ) { total_errors++; mbedtls_fprintf( stdout, "FAILED\n" ); - mbedtls_fprintf( stdout, " %s\n at line %d, %s\n", - test_info.test, test_info.line_no, - test_info.filename ); + mbedtls_fprintf( stdout, " %s\n at ", + test_info.test ); + if( test_info.step != (unsigned long)( -1 ) ) + { + mbedtls_fprintf( stdout, "step %lu, ", + test_info.step ); + } + mbedtls_fprintf( stdout, "line %d, %s", + test_info.line_no, test_info.filename ); } fflush( stdout ); } diff --git a/tests/suites/target_test.function b/tests/suites/target_test.function index 91f719873..937e8dd72 100644 --- a/tests/suites/target_test.function +++ b/tests/suites/target_test.function @@ -375,6 +375,7 @@ int execute_tests( int args, const char ** argv ) { ret = 0; test_info.result = TEST_RESULT_SUCCESS; + test_info.step = (unsigned long)( -1 ); data_len = 0; data = receive_data( &data_len );