New macro ASSERT_ALLOC to allocate memory in tests

The new macro ASSERT_ALLOC allocates memory with mbedtls_calloc and
fails the test if the allocation fails. It outputs a null pointer if
the requested size is 0. It is meant to replace existing calls to
mbedtls_calloc.
This commit is contained in:
Gilles Peskine 2018-09-27 13:52:16 +02:00 committed by Janos Follath
parent 5f0ccd5a3c
commit 28405300ee

View file

@ -230,13 +230,44 @@ typedef enum
#define TEST_VALID_PARAM( TEST ) \
TEST_ASSERT( ( TEST, 1 ) );
#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
{ \
mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
__FILE__, __LINE__, #a ); \
mbedtls_exit( 1 ); \
mbedtls_exit( 1 ); \
}
/** Allocate memory dynamically and fail the test case if this fails.
*
* You must set \p pointer to \c NULL before calling this macro and
* put `mbedtls_free( pointer )` in the test's cleanup code.
*
* If \p size is zero, the resulting \p pointer will be \c NULL.
* This is usually what we want in tests since API functions are
* supposed to accept null pointers when a buffer size is zero.
*
* This macro expands to an instruction, not an expression.
* It may jump to the \c exit label.
*
* \param pointer An lvalue where the address of the allocated buffer
* will be stored.
* This expression may be evaluated multiple times.
* \param size Buffer size to allocate in bytes.
* This expression may be evaluated multiple times.
*
*/
#define ASSERT_ALLOC( pointer, size ) \
do \
{ \
TEST_ASSERT( ( pointer ) == NULL ); \
if( ( size ) != 0 ) \
{ \
( pointer ) = mbedtls_calloc( 1, ( size ) ); \
TEST_ASSERT( ( pointer ) != NULL ); \
} \
} \
while( 0 )
/*
* 32-bit integer manipulation macros (big endian)
*/