Skip to content

Commit b802a69

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 bc13105 commit b802a69

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
@@ -166,6 +166,26 @@ typedef enum
166166
} \
167167
while( 0 )
168168

169+
/** Allocate memory dynamically. Exit the test if this fails, but do
170+
* not mark the test as failed.
171+
*
172+
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
173+
* fails, it jumps to the \c exit label without calling test_fail().
174+
*/
175+
#define ASSERT_ALLOC_WEAK( pointer, length ) \
176+
do \
177+
{ \
178+
TEST_ASSERT( ( pointer ) == NULL ); \
179+
if( ( length ) != 0 ) \
180+
{ \
181+
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
182+
( length ) ); \
183+
if( ( pointer ) == NULL ) \
184+
goto exit; \
185+
} \
186+
} \
187+
while( 0 )
188+
169189
/** Compare two buffers and fail the test case if they differ.
170190
*
171191
* 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)