@@ -311,11 +311,11 @@ typedef enum
311311#define TEST_VALID_PARAM( TEST ) \
312312 TEST_ASSERT( ( TEST, 1 ) );
313313
314- #define TEST_HELPER_ASSERT(a) if( !( a ) ) \
314+ #define TEST_HELPER_ASSERT(a) if( !( a ) ) \
315315{ \
316- mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
316+ mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
317317 __FILE__, __LINE__, #a ); \
318- mbedtls_exit( 1 ); \
318+ mbedtls_exit( 1 ); \
319319}
320320
321321#if defined(__GNUC__)
@@ -370,6 +370,38 @@ typedef enum
370370 */
371371#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
372372
373+ /** Allocate memory dynamically and fail the test case if this fails.
374+ *
375+ * You must set \p pointer to \c NULL before calling this macro and
376+ * put `mbedtls_free( pointer )` in the test's cleanup code.
377+ *
378+ * If \p length is zero, the resulting \p pointer will be \c NULL.
379+ * This is usually what we want in tests since API functions are
380+ * supposed to accept null pointers when a buffer size is zero.
381+ *
382+ * This macro expands to an instruction, not an expression.
383+ * It may jump to the \c exit label.
384+ *
385+ * \param pointer An lvalue where the address of the allocated buffer
386+ * will be stored.
387+ * This expression may be evaluated multiple times.
388+ * \param length Number of elements to allocate.
389+ * This expression may be evaluated multiple times.
390+ *
391+ */
392+ #define ASSERT_ALLOC( pointer, length ) \
393+ do \
394+ { \
395+ TEST_ASSERT( ( pointer ) == NULL ); \
396+ if( ( length ) != 0 ) \
397+ { \
398+ ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
399+ ( length ) ); \
400+ TEST_ASSERT( ( pointer ) != NULL ); \
401+ } \
402+ } \
403+ while( 0 )
404+
373405/*
374406 * 32-bit integer manipulation macros (big endian)
375407 */
0 commit comments