New test helper macro ASSERT_ALLOC_WEAK

The new macro ASSERT_ALLOC_WEAK does not fail the test case if the
memory allocation fails. This is useful for tests that allocate a
large amount of memory, but that aren't useful on platforms where
allocating such a large amount is not possible.

Ideally this macro should mark the test as skipped. We don't yet have
a facility for that but we're working on it. Once we have a skip
functionality, this macro should be changed to use it.
This commit is contained in:
Gilles Peskine 2019-03-04 17:13:43 +01:00
parent 09c0a2364b
commit 2cd8ecc08b
2 changed files with 21 additions and 7 deletions

View file

@ -158,6 +158,26 @@ typedef enum
} \
while( 0 )
/** Allocate memory dynamically. Exit the test if this fails, but do
* not mark the test as failed.
*
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
* fails, it jumps to the \c exit label without calling test_fail().
*/
#define ASSERT_ALLOC_WEAK( pointer, length ) \
do \
{ \
TEST_ASSERT( ( pointer ) == NULL ); \
if( ( length ) != 0 ) \
{ \
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
( length ) ); \
if( ( pointer ) == NULL ) \
goto exit; \
} \
} \
while( 0 )
/** Compare two buffers and fail the test case if they differ.
*
* This macro expands to an instruction, not an expression.

View file

@ -130,7 +130,7 @@ int get_len_step( const data_t *input, size_t buffer_size,
}
else
{
ASSERT_ALLOC( buf, buffer_size );
ASSERT_ALLOC_WEAK( buf, buffer_size );
if( buffer_size > input->len )
{
memcpy( buf, input->x, input->len );
@ -159,12 +159,6 @@ int get_len_step( const data_t *input, size_t buffer_size,
return( 1 );
exit:
/* It may be impossible to allocate large lengths on embedded platforms.
* Pass in this case (though it would be better to mark the test
* as skipped). */
if( buf == NULL )
return( 1 );
mbedtls_free( buf );
return( 0 );
}