From 2cd8ecc08b9e73196daa12e22feb90fd446b3407 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 4 Mar 2019 17:13:43 +0100 Subject: [PATCH] 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. --- tests/suites/helpers.function | 20 ++++++++++++++++++++ tests/suites/test_suite_asn1parse.function | 8 +------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index d45fd4ea7d..00320bca37 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -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. diff --git a/tests/suites/test_suite_asn1parse.function b/tests/suites/test_suite_asn1parse.function index 9e9f509499..f5ecd5515b 100644 --- a/tests/suites/test_suite_asn1parse.function +++ b/tests/suites/test_suite_asn1parse.function @@ -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 ); }