From b436649a25a3359c6389aa48ddc2adc28c60a9d4 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 29 Apr 2021 20:28:54 +0200 Subject: [PATCH] Show values when TEST_EQUAL fails When TEST_EQUAL fails, show the two numerical values in the test log (only with host_test). The values are printed in hexa and signed decimal. The arguments of TEST_EQUAL must now be integers, not pointers or floats. The current implementation requires them to fit in unsigned long long Signed values no larger than long long will work too. The implementation uses unsigned long long rather than uintmax_t to reduce portability concerns. The snprintf function must support "%llx" and "%lld". For this purpose, add room for two lines of text to the mbedtls_test_info structure. This adds 154 bytes of global data. Signed-off-by: Gilles Peskine --- tests/include/test/helpers.h | 23 +++++++++++++++++++++++ tests/include/test/macros.h | 20 +++++++++++++------- tests/src/helpers.c | 25 +++++++++++++++++++++++++ tests/src/psa_exercise_key.c | 8 ++++---- tests/suites/host_test.function | 6 ++++++ 5 files changed, 71 insertions(+), 11 deletions(-) diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h index dfc09cdbb..5d101902b 100644 --- a/tests/include/test/helpers.h +++ b/tests/include/test/helpers.h @@ -72,6 +72,8 @@ typedef struct const char *filename; int line_no; unsigned long step; + char line1[76]; + char line2[76]; #if defined(MBEDTLS_TEST_MUTEX_USAGE) const char *mutex_usage_error; #endif @@ -130,6 +132,27 @@ void mbedtls_test_set_step( unsigned long step ); */ void mbedtls_test_info_reset( void ); +/** + * \brief Record the current test case as a failure if two integers + * have a different value. + * + * This function is usually called via the macro + * #TEST_EQUAL. + * + * \param test Description of the failure or assertion that failed. This + * MUST be a string literal. This normally has the form + * "EXPR1 == EXPR2" where EXPR1 has the value \p value1 + * and EXPR2 has the value \p value2. + * \param line_no Line number where the failure originated. + * \param filename Filename where the failure originated. + * \param value1 The first value to compare. + * \param value2 The second value to compare. + * + * \return \c 1 if the values are equal, otherwise \c 0. + */ +int mbedtls_test_equal( const char *test, int line_no, const char* filename, + unsigned long long value1, unsigned long long value2 ); + /** * \brief This function decodes the hexadecimal representation of * data. diff --git a/tests/include/test/macros.h b/tests/include/test/macros.h index b7b6e8f31..7b4c2f6c9 100644 --- a/tests/include/test/macros.h +++ b/tests/include/test/macros.h @@ -84,15 +84,21 @@ } \ } while( 0 ) -/** Evaluate two expressions and fail the test case if they have different - * values. +/** Evaluate two integer expressions and fail the test case if they have + * different values. * - * \param expr1 An expression to evaluate. - * \param expr2 The expected value of \p expr1. This can be any - * expression, but it is typically a constant. + * The two expressions should have the same signedness, otherwise the + * comparison is not meaningful if the signed value is negative. + * + * \param expr1 An integral-typed expression to evaluate. + * \param expr2 Another integral-typed expression to evaluate. */ -#define TEST_EQUAL( expr1, expr2 ) \ - TEST_ASSERT( ( expr1 ) == ( expr2 ) ) +#define TEST_EQUAL( expr1, expr2 ) \ + do { \ + if( ! mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \ + expr1, expr2 ) ) \ + goto exit; \ + } while( 0 ) /** Allocate memory dynamically and fail the test case if this fails. * The allocated memory will be filled with zeros. diff --git a/tests/src/helpers.c b/tests/src/helpers.c index bf6c1d956..c53e4fc81 100644 --- a/tests/src/helpers.c +++ b/tests/src/helpers.c @@ -113,6 +113,31 @@ void mbedtls_test_info_reset( void ) mbedtls_test_info.test = 0; mbedtls_test_info.line_no = 0; mbedtls_test_info.filename = 0; + memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) ); + memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) ); +} + +int mbedtls_test_equal( const char *test, int line_no, const char* filename, + unsigned long long value1, unsigned long long value2 ) +{ + if( value1 == value2 ) + return( 1 ); + if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ) + { + /* We've already recorded the test as having failed. Don't + * overwrite any previous information about the failure. */ + return( 0 ); + } + mbedtls_test_fail( test, line_no, filename ); + (void) mbedtls_snprintf( mbedtls_test_info.line1, + sizeof( mbedtls_test_info.line1 ), + "lhs = 0x%016llx = %lld", + value1, (long long) value1 ); + (void) mbedtls_snprintf( mbedtls_test_info.line2, + sizeof( mbedtls_test_info.line2 ), + "rhs = 0x%016llx = %lld", + value2, (long long) value2 ); + return( 0 ); } int mbedtls_test_unhexify( unsigned char *obuf, diff --git a/tests/src/psa_exercise_key.c b/tests/src/psa_exercise_key.c index e4e55c9c2..923d2c136 100644 --- a/tests/src/psa_exercise_key.c +++ b/tests/src/psa_exercise_key.c @@ -663,7 +663,7 @@ int mbedtls_test_psa_exported_key_sanity_check( TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); - TEST_EQUAL( p + len, end ); + TEST_EQUAL( len, end - p ); if( ! mbedtls_test_asn1_skip_integer( &p, end, 0, 0, 0 ) ) goto exit; if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ) @@ -684,7 +684,7 @@ int mbedtls_test_psa_exported_key_sanity_check( goto exit; if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) goto exit; - TEST_EQUAL( p, end ); + TEST_EQUAL( p - end, 0 ); TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); } @@ -716,12 +716,12 @@ int mbedtls_test_psa_exported_key_sanity_check( MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); - TEST_EQUAL( p + len, end ); + TEST_EQUAL( len, end - p ); if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ) goto exit; if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) ) goto exit; - TEST_EQUAL( p, end ); + TEST_EQUAL( p - end, 0 ); TEST_ASSERT( exported_length <= diff --git a/tests/suites/host_test.function b/tests/suites/host_test.function index a5fd7179b..17926ebb3 100644 --- a/tests/suites/host_test.function +++ b/tests/suites/host_test.function @@ -778,6 +778,12 @@ int execute_tests( int argc , const char ** argv ) mbedtls_fprintf( stdout, "line %d, %s", mbedtls_test_info.line_no, mbedtls_test_info.filename ); + if( mbedtls_test_info.line1[0] != 0 ) + mbedtls_fprintf( stdout, "\n %s", + mbedtls_test_info.line1 ); + if( mbedtls_test_info.line2[0] != 0 ) + mbedtls_fprintf( stdout, "\n %s", + mbedtls_test_info.line2 ); } fflush( stdout ); }