Skip to content

Commit 2cd8ecc

Browse files
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.
1 parent 09c0a23 commit 2cd8ecc

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

tests/suites/helpers.function

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ typedef enum
158158
} \
159159
while( 0 )
160160

161+
/** Allocate memory dynamically. Exit the test if this fails, but do
162+
* not mark the test as failed.
163+
*
164+
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
165+
* fails, it jumps to the \c exit label without calling test_fail().
166+
*/
167+
#define ASSERT_ALLOC_WEAK( pointer, length ) \
168+
do \
169+
{ \
170+
TEST_ASSERT( ( pointer ) == NULL ); \
171+
if( ( length ) != 0 ) \
172+
{ \
173+
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
174+
( length ) ); \
175+
if( ( pointer ) == NULL ) \
176+
goto exit; \
177+
} \
178+
} \
179+
while( 0 )
180+
161181
/** Compare two buffers and fail the test case if they differ.
162182
*
163183
* This macro expands to an instruction, not an expression.

tests/suites/test_suite_asn1parse.function

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int get_len_step( const data_t *input, size_t buffer_size,
130130
}
131131
else
132132
{
133-
ASSERT_ALLOC( buf, buffer_size );
133+
ASSERT_ALLOC_WEAK( buf, buffer_size );
134134
if( buffer_size > input->len )
135135
{
136136
memcpy( buf, input->x, input->len );
@@ -159,12 +159,6 @@ int get_len_step( const data_t *input, size_t buffer_size,
159159
return( 1 );
160160

161161
exit:
162-
/* It may be impossible to allocate large lengths on embedded platforms.
163-
* Pass in this case (though it would be better to mark the test
164-
* as skipped). */
165-
if( buf == NULL )
166-
return( 1 );
167-
168162
mbedtls_free( buf );
169163
return( 0 );
170164
}

0 commit comments

Comments
 (0)