New test helper macros TEST_LE_U, TEST_LE_S

Test assertions for integer comparisons that display the compared values on
failure. Similar to TEST_EQUAL.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-04-13 23:59:52 +02:00
parent 3ff25443c8
commit d1465429a2
3 changed files with 114 additions and 0 deletions

View file

@ -154,6 +154,48 @@ void mbedtls_test_info_reset( void );
int mbedtls_test_equal( const char *test, int line_no, const char* filename,
unsigned long long value1, unsigned long long value2 );
/**
* \brief Record the current test case as a failure based
* on comparing two unsigned integers.
*
* This function is usually called via the macro
* #TEST_LE_U.
*
* \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 \p value1 <= \p value2, otherwise \c 0.
*/
int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
unsigned long long value1, unsigned long long value2 );
/**
* \brief Record the current test case as a failure based
* on comparing two signed integers.
*
* This function is usually called via the macro
* #TEST_LE_S.
*
* \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 \p value1 <= \p value2, otherwise \c 0.
*/
int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
long long value1, long long value2 );
/**
* \brief This function decodes the hexadecimal representation of
* data.

View file

@ -89,6 +89,32 @@
goto exit; \
} while( 0 )
/** Evaluate two unsigned integer expressions and fail the test case
* if they are not in increasing order (left <= right).
*
* \param expr1 An integral-typed expression to evaluate.
* \param expr2 Another integral-typed expression to evaluate.
*/
#define TEST_LE_U( expr1, expr2 ) \
do { \
if( ! mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
expr1, expr2 ) ) \
goto exit; \
} while( 0 )
/** Evaluate two signed integer expressions and fail the test case
* if they are not in increasing order (left <= right).
*
* \param expr1 An integral-typed expression to evaluate.
* \param expr2 Another integral-typed expression to evaluate.
*/
#define TEST_LE_S( expr1, expr2 ) \
do { \
if( ! mbedtls_test_le_s( #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

@ -122,6 +122,52 @@ int mbedtls_test_equal( const char *test, int line_no, const char* filename,
return( 0 );
}
int mbedtls_test_le_u( 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 = %llu",
value1, value1 );
(void) mbedtls_snprintf( mbedtls_test_info.line2,
sizeof( mbedtls_test_info.line2 ),
"rhs = 0x%016llx = %llu",
value2, value2 );
return( 0 );
}
int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
long long value1, 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",
(unsigned long long) value1, value1 );
(void) mbedtls_snprintf( mbedtls_test_info.line2,
sizeof( mbedtls_test_info.line2 ),
"rhs = 0x%016llx = %lld",
(unsigned long long) value2, value2 );
return( 0 );
}
int mbedtls_test_unhexify( unsigned char *obuf,
size_t obufmax,
const char *ibuf,