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

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