Merge pull request #5091 from gilles-peskine-arm/test_equal_verbose

Show values when TEST_EQUAL fails
This commit is contained in:
Gilles Peskine 2021-10-22 17:25:03 +02:00 committed by GitHub
commit f7946a6210
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 11 deletions

View file

@ -73,6 +73,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
@ -131,6 +133,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.

View file

@ -73,15 +73,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.

View file

@ -95,6 +95,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,

View file

@ -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 <=

View file

@ -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 );
}