2017-05-09 18:20:21 +02:00
|
|
|
#line 2 "suites/helpers.function"
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Headers */
|
|
|
|
|
2020-06-09 13:52:23 +02:00
|
|
|
#include <test/macros.h>
|
2020-06-03 10:11:18 +02:00
|
|
|
#include <test/helpers.h>
|
2020-06-09 16:57:42 +02:00
|
|
|
#include <test/random.h>
|
2020-06-09 13:52:23 +02:00
|
|
|
|
2016-05-17 14:35:51 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2014-06-06 14:48:09 +02:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_fprintf fprintf
|
2016-09-30 14:11:29 +02:00
|
|
|
#define mbedtls_snprintf snprintf
|
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
|
|
|
#define mbedtls_exit exit
|
2016-04-27 14:35:37 +02:00
|
|
|
#define mbedtls_time time
|
|
|
|
#define mbedtls_time_t time_t
|
2016-04-18 19:18:48 +02:00
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
2014-06-06 14:48:09 +02:00
|
|
|
#endif
|
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
|
|
|
#endif
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS)
|
|
|
|
#include "mbedtls/platform_util.h"
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-13 17:57:25 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <basetsd.h>
|
2018-06-22 12:34:33 +02:00
|
|
|
typedef UINT8 uint8_t;
|
|
|
|
typedef INT32 int32_t;
|
2011-03-13 17:57:25 +01:00
|
|
|
typedef UINT32 uint32_t;
|
2015-11-14 14:09:01 +01:00
|
|
|
#define strncasecmp _strnicmp
|
|
|
|
#define strcasecmp _stricmp
|
2011-03-13 17:57:25 +01:00
|
|
|
#else
|
2015-06-22 19:21:23 +02:00
|
|
|
#include <stdint.h>
|
2011-03-13 17:57:25 +01:00
|
|
|
#endif
|
|
|
|
|
2013-08-16 13:31:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-10-05 11:57:49 +02:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
#include <unistd.h>
|
2017-12-05 13:08:15 +01:00
|
|
|
#include <strings.h>
|
2016-10-05 11:57:49 +02:00
|
|
|
#endif
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
/* Type for Hex parameters */
|
2018-06-29 12:05:32 +02:00
|
|
|
typedef struct data_tag
|
2017-06-09 05:32:58 +02:00
|
|
|
{
|
|
|
|
uint8_t * x;
|
|
|
|
uint32_t len;
|
2018-06-29 12:05:32 +02:00
|
|
|
} data_t;
|
2017-06-09 05:32:58 +02:00
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-06-29 11:02:54 +02:00
|
|
|
/* Status and error constants */
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2018-06-29 11:02:54 +02:00
|
|
|
#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
|
|
|
|
#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
|
|
|
|
#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
|
2016-04-18 00:24:50 +02:00
|
|
|
|
2018-06-29 11:02:54 +02:00
|
|
|
#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
|
|
|
|
#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
|
|
|
|
#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
|
|
|
|
#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
|
|
|
|
Only int, string, binary data
|
|
|
|
and integer expressions are
|
|
|
|
allowed */
|
|
|
|
#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
|
|
|
|
build */
|
2016-02-18 00:34:30 +01:00
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
|
|
|
|
PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
|
|
|
|
* is pending */
|
|
|
|
PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
|
|
|
|
* failure function has been made */
|
|
|
|
} paramfail_test_state_t;
|
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Macros */
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
/**
|
|
|
|
* \brief This macro tests the expression passed to it as a test step or
|
|
|
|
* individual test in a test case.
|
2018-09-27 13:51:25 +02:00
|
|
|
*
|
2019-01-31 14:20:20 +01:00
|
|
|
* It allows a library function to return a value and return an error
|
|
|
|
* code that can be tested.
|
2018-09-27 13:51:25 +02:00
|
|
|
*
|
2019-01-31 14:20:20 +01:00
|
|
|
* When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
|
|
|
|
* callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
* This macro is not suitable for negative parameter validation tests,
|
|
|
|
* as it assumes the test step will not create an error.
|
|
|
|
*
|
2019-02-11 13:05:54 +01:00
|
|
|
* Failing the test means:
|
|
|
|
* - Mark this test case as failed.
|
|
|
|
* - Print a message identifying the failure.
|
|
|
|
* - Jump to the \c exit label.
|
2018-09-27 13:51:25 +02:00
|
|
|
*
|
2019-02-11 13:05:54 +01:00
|
|
|
* This macro expands to an instruction, not an expression.
|
|
|
|
* It may jump to the \c exit label.
|
|
|
|
*
|
2019-01-31 14:20:20 +01:00
|
|
|
* \param TEST The test expression to be tested.
|
2018-09-27 13:51:25 +02:00
|
|
|
*/
|
2019-01-31 14:20:20 +01:00
|
|
|
#define TEST_ASSERT( TEST ) \
|
|
|
|
do { \
|
|
|
|
if( ! (TEST) ) \
|
|
|
|
{ \
|
|
|
|
test_fail( #TEST, __LINE__, __FILE__ ); \
|
|
|
|
goto exit; \
|
|
|
|
} \
|
2016-02-18 00:34:30 +01:00
|
|
|
} while( 0 )
|
|
|
|
|
2018-12-17 23:26:52 +01:00
|
|
|
/** Evaluate two expressions and fail the test case if they have different
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
* \param expr1 An expression to evaluate.
|
|
|
|
* \param expr2 The expected value of \p expr1. This can be any
|
|
|
|
* expression, but it is typically a constant.
|
|
|
|
*/
|
|
|
|
#define TEST_EQUAL( expr1, expr2 ) \
|
|
|
|
TEST_ASSERT( ( expr1 ) == ( expr2 ) )
|
|
|
|
|
2018-09-27 13:52:16 +02:00
|
|
|
/** Allocate memory dynamically and fail the test case if this fails.
|
|
|
|
*
|
|
|
|
* You must set \p pointer to \c NULL before calling this macro and
|
|
|
|
* put `mbedtls_free( pointer )` in the test's cleanup code.
|
|
|
|
*
|
2018-11-30 18:51:45 +01:00
|
|
|
* If \p length is zero, the resulting \p pointer will be \c NULL.
|
2018-09-27 13:52:16 +02:00
|
|
|
* This is usually what we want in tests since API functions are
|
|
|
|
* supposed to accept null pointers when a buffer size is zero.
|
|
|
|
*
|
|
|
|
* This macro expands to an instruction, not an expression.
|
|
|
|
* It may jump to the \c exit label.
|
|
|
|
*
|
|
|
|
* \param pointer An lvalue where the address of the allocated buffer
|
|
|
|
* will be stored.
|
|
|
|
* This expression may be evaluated multiple times.
|
2018-11-30 18:51:45 +01:00
|
|
|
* \param length Number of elements to allocate.
|
2018-09-27 13:52:16 +02:00
|
|
|
* This expression may be evaluated multiple times.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-30 18:51:45 +01:00
|
|
|
#define ASSERT_ALLOC( pointer, length ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
TEST_ASSERT( ( pointer ) == NULL ); \
|
|
|
|
if( ( length ) != 0 ) \
|
|
|
|
{ \
|
|
|
|
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
|
|
|
|
( length ) ); \
|
|
|
|
TEST_ASSERT( ( pointer ) != NULL ); \
|
|
|
|
} \
|
|
|
|
} \
|
2018-09-27 13:52:16 +02:00
|
|
|
while( 0 )
|
|
|
|
|
2020-01-21 16:20:04 +01:00
|
|
|
/** Allocate memory dynamically. If the allocation fails, skip the test case.
|
2019-03-04 17:13:43 +01:00
|
|
|
*
|
|
|
|
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
|
2020-01-21 16:20:04 +01:00
|
|
|
* fails, it marks the test as skipped rather than failed.
|
2019-03-04 17:13:43 +01:00
|
|
|
*/
|
|
|
|
#define ASSERT_ALLOC_WEAK( pointer, length ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
TEST_ASSERT( ( pointer ) == NULL ); \
|
|
|
|
if( ( length ) != 0 ) \
|
|
|
|
{ \
|
|
|
|
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
|
|
|
|
( length ) ); \
|
2020-01-21 16:20:04 +01:00
|
|
|
TEST_ASSUME( ( pointer ) != NULL ); \
|
2019-03-04 17:13:43 +01:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while( 0 )
|
|
|
|
|
2018-09-27 13:56:31 +02:00
|
|
|
/** Compare two buffers and fail the test case if they differ.
|
|
|
|
*
|
|
|
|
* This macro expands to an instruction, not an expression.
|
|
|
|
* It may jump to the \c exit label.
|
|
|
|
*
|
|
|
|
* \param p1 Pointer to the start of the first buffer.
|
|
|
|
* \param size1 Size of the first buffer in bytes.
|
|
|
|
* This expression may be evaluated multiple times.
|
|
|
|
* \param p2 Pointer to the start of the second buffer.
|
|
|
|
* \param size2 Size of the second buffer in bytes.
|
|
|
|
* This expression may be evaluated multiple times.
|
|
|
|
*/
|
|
|
|
#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
TEST_ASSERT( ( size1 ) == ( size2 ) ); \
|
|
|
|
if( ( size1 ) != 0 ) \
|
|
|
|
TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
|
|
|
|
} \
|
|
|
|
while( 0 )
|
|
|
|
|
2019-07-05 14:31:30 +02:00
|
|
|
/**
|
|
|
|
* \brief This macro tests the expression passed to it and skips the
|
|
|
|
* running test if it doesn't evaluate to 'true'.
|
|
|
|
*
|
|
|
|
* \param TEST The test expression to be tested.
|
|
|
|
*/
|
|
|
|
#define TEST_ASSUME( TEST ) \
|
|
|
|
do { \
|
|
|
|
if( ! (TEST) ) \
|
|
|
|
{ \
|
|
|
|
test_skip( #TEST, __LINE__, __FILE__ ); \
|
|
|
|
goto exit; \
|
|
|
|
} \
|
|
|
|
} while( 0 )
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
|
|
|
|
/**
|
|
|
|
* \brief This macro tests the statement passed to it as a test step or
|
|
|
|
* individual test in a test case. The macro assumes the test will fail
|
|
|
|
* and will generate an error.
|
|
|
|
*
|
|
|
|
* It allows a library function to return a value and tests the return
|
|
|
|
* code on return to confirm the given error code was returned.
|
|
|
|
*
|
|
|
|
* When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
|
|
|
|
* callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
|
|
|
|
* expected failure, and the test will pass.
|
|
|
|
*
|
|
|
|
* This macro is intended for negative parameter validation tests,
|
|
|
|
* where the failing function may return an error value or call
|
|
|
|
* MBEDTLS_PARAM_FAILED() to indicate the error.
|
|
|
|
*
|
|
|
|
* \param PARAM_ERROR_VALUE The expected error code.
|
|
|
|
*
|
|
|
|
* \param TEST The test expression to be tested.
|
|
|
|
*/
|
|
|
|
#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
|
|
|
|
do { \
|
|
|
|
test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
|
|
|
|
if( (TEST) != (PARAM_ERR_VALUE) || \
|
|
|
|
test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
|
|
|
|
{ \
|
|
|
|
test_fail( #TEST, __LINE__, __FILE__ ); \
|
|
|
|
goto exit; \
|
|
|
|
} \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief This macro tests the statement passed to it as a test step or
|
|
|
|
* individual test in a test case. The macro assumes the test will fail
|
|
|
|
* and will generate an error.
|
|
|
|
*
|
|
|
|
* It assumes the library function under test cannot return a value and
|
|
|
|
* assumes errors can only be indicated byt calls to
|
|
|
|
* MBEDTLS_PARAM_FAILED().
|
|
|
|
*
|
|
|
|
* When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
|
|
|
|
* callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
|
|
|
|
* expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
|
|
|
|
* can be made.
|
|
|
|
*
|
|
|
|
* This macro is intended for negative parameter validation tests,
|
|
|
|
* where the failing function can only return an error by calling
|
|
|
|
* MBEDTLS_PARAM_FAILED() to indicate the error.
|
|
|
|
*
|
|
|
|
* \param TEST The test expression to be tested.
|
|
|
|
*/
|
|
|
|
#define TEST_INVALID_PARAM( TEST ) \
|
|
|
|
do { \
|
|
|
|
memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
|
|
|
|
if( setjmp( param_fail_jmp ) == 0 ) \
|
|
|
|
{ \
|
|
|
|
TEST; \
|
|
|
|
test_fail( #TEST, __LINE__, __FILE__ ); \
|
|
|
|
goto exit; \
|
|
|
|
} \
|
|
|
|
memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
|
|
|
|
} while( 0 )
|
|
|
|
#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief This macro tests the statement passed to it as a test step or
|
|
|
|
* individual test in a test case. The macro assumes the test will not fail.
|
|
|
|
*
|
|
|
|
* It assumes the library function under test cannot return a value and
|
|
|
|
* assumes errors can only be indicated by calls to
|
|
|
|
* MBEDTLS_PARAM_FAILED().
|
|
|
|
*
|
|
|
|
* When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
|
|
|
|
* callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
|
|
|
|
* expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
|
|
|
|
* can be made.
|
|
|
|
*
|
|
|
|
* This macro is intended to test that functions returning void
|
|
|
|
* accept all of the parameter values they're supposed to accept - eg
|
|
|
|
* that they don't call MBEDTLS_PARAM_FAILED() when a parameter
|
|
|
|
* that's allowed to be NULL happens to be NULL.
|
|
|
|
*
|
|
|
|
* Note: for functions that return something other that void,
|
|
|
|
* checking that they accept all the parameters they're supposed to
|
|
|
|
* accept is best done by using TEST_ASSERT() and checking the return
|
|
|
|
* value as well.
|
|
|
|
*
|
|
|
|
* Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
|
|
|
|
* disabled, as it makes sense to check that the functions accept all
|
|
|
|
* legal values even if this option is disabled - only in that case,
|
|
|
|
* the test is more about whether the function segfaults than about
|
|
|
|
* whether it invokes MBEDTLS_PARAM_FAILED().
|
|
|
|
*
|
|
|
|
* \param TEST The test expression to be tested.
|
|
|
|
*/
|
|
|
|
#define TEST_VALID_PARAM( TEST ) \
|
|
|
|
TEST_ASSERT( ( TEST, 1 ) );
|
|
|
|
|
2018-09-27 13:52:16 +02:00
|
|
|
/** Allocate memory dynamically and fail the test case if this fails.
|
|
|
|
*
|
|
|
|
* You must set \p pointer to \c NULL before calling this macro and
|
|
|
|
* put `mbedtls_free( pointer )` in the test's cleanup code.
|
|
|
|
*
|
2018-11-30 18:51:45 +01:00
|
|
|
* If \p length is zero, the resulting \p pointer will be \c NULL.
|
2018-09-27 13:52:16 +02:00
|
|
|
* This is usually what we want in tests since API functions are
|
|
|
|
* supposed to accept null pointers when a buffer size is zero.
|
|
|
|
*
|
|
|
|
* This macro expands to an instruction, not an expression.
|
|
|
|
* It may jump to the \c exit label.
|
|
|
|
*
|
|
|
|
* \param pointer An lvalue where the address of the allocated buffer
|
|
|
|
* will be stored.
|
|
|
|
* This expression may be evaluated multiple times.
|
2018-11-30 18:51:45 +01:00
|
|
|
* \param length Number of elements to allocate.
|
2018-09-27 13:52:16 +02:00
|
|
|
* This expression may be evaluated multiple times.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-30 18:51:45 +01:00
|
|
|
#define ASSERT_ALLOC( pointer, length ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
TEST_ASSERT( ( pointer ) == NULL ); \
|
|
|
|
if( ( length ) != 0 ) \
|
|
|
|
{ \
|
|
|
|
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
|
|
|
|
( length ) ); \
|
|
|
|
TEST_ASSERT( ( pointer ) != NULL ); \
|
|
|
|
} \
|
|
|
|
} \
|
2018-09-27 13:52:16 +02:00
|
|
|
while( 0 )
|
|
|
|
|
2016-04-18 00:24:50 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Global variables */
|
|
|
|
|
2019-07-05 14:31:30 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TEST_RESULT_SUCCESS = 0,
|
|
|
|
TEST_RESULT_FAILED,
|
|
|
|
TEST_RESULT_SKIPPED
|
|
|
|
} test_result_t;
|
|
|
|
|
2019-09-16 15:12:51 +02:00
|
|
|
typedef struct
|
2017-10-01 17:42:29 +02:00
|
|
|
{
|
2019-01-31 14:20:20 +01:00
|
|
|
paramfail_test_state_t paramfail_test_state;
|
2019-07-05 14:31:30 +02:00
|
|
|
test_result_t result;
|
2017-10-01 17:42:29 +02:00
|
|
|
const char *test;
|
|
|
|
const char *filename;
|
|
|
|
int line_no;
|
2019-03-01 14:26:30 +01:00
|
|
|
unsigned long step;
|
2017-10-01 17:42:29 +02:00
|
|
|
}
|
2019-09-16 15:12:51 +02:00
|
|
|
test_info_t;
|
|
|
|
static test_info_t test_info;
|
2016-04-18 00:24:50 +02:00
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS)
|
|
|
|
jmp_buf param_fail_jmp;
|
|
|
|
jmp_buf jmp_tmp;
|
|
|
|
#endif
|
|
|
|
|
2017-07-24 13:27:09 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper flags for complex dependencies */
|
|
|
|
|
|
|
|
/* Indicates whether we expect mbedtls_entropy_init
|
|
|
|
* to initialize some strong entropy source. */
|
|
|
|
#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
|
|
|
|
( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
|
|
|
|
( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
|
|
|
|
defined(MBEDTLS_HAVEGE_C) || \
|
|
|
|
defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
|
|
|
|
defined(ENTROPY_NV_SEED) ) )
|
2017-09-07 09:09:33 +02:00
|
|
|
#define ENTROPY_HAVE_STRONG
|
2017-07-24 13:27:09 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-02-18 00:34:30 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
/* Helper Functions */
|
2019-01-31 14:20:20 +01:00
|
|
|
|
2019-03-01 14:26:30 +01:00
|
|
|
/** Set the test step number for failure reports.
|
|
|
|
*
|
|
|
|
* Call this function to display "step NNN" in addition to the line number
|
|
|
|
* and file name if a test fails. Typically the "step number" is the index
|
|
|
|
* of a for loop but it can be whatever you want.
|
|
|
|
*
|
|
|
|
* \param step The step number to report.
|
|
|
|
*/
|
|
|
|
void test_set_step( unsigned long step )
|
|
|
|
{
|
|
|
|
test_info.step = step;
|
|
|
|
}
|
|
|
|
|
2018-10-03 17:17:41 +02:00
|
|
|
void test_fail( const char *test, int line_no, const char* filename )
|
2019-01-31 14:20:20 +01:00
|
|
|
{
|
2019-07-05 14:31:30 +02:00
|
|
|
test_info.result = TEST_RESULT_FAILED;
|
|
|
|
test_info.test = test;
|
|
|
|
test_info.line_no = line_no;
|
|
|
|
test_info.filename = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_skip( const char *test, int line_no, const char* filename )
|
|
|
|
{
|
|
|
|
test_info.result = TEST_RESULT_SKIPPED;
|
2019-01-31 14:20:20 +01:00
|
|
|
test_info.test = test;
|
|
|
|
test_info.line_no = line_no;
|
|
|
|
test_info.filename = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CHECK_PARAMS)
|
|
|
|
void mbedtls_param_failed( const char *failure_condition,
|
|
|
|
const char *file,
|
|
|
|
int line )
|
|
|
|
{
|
|
|
|
/* If we are testing the callback function... */
|
|
|
|
if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
|
|
|
|
{
|
|
|
|
test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ...else we treat this as an error */
|
|
|
|
|
|
|
|
/* Record the location of the failure, but not as a failure yet, in case
|
|
|
|
* it was part of the test */
|
|
|
|
test_fail( failure_condition, line, file );
|
2019-07-05 14:31:30 +02:00
|
|
|
test_info.result = TEST_RESULT_SUCCESS;
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
longjmp( param_fail_jmp, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-05 11:57:49 +02:00
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
static int redirect_output( FILE** out_stream, const char* path )
|
|
|
|
{
|
|
|
|
int stdout_fd = dup( fileno( *out_stream ) );
|
|
|
|
|
|
|
|
if( stdout_fd == -1 )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush( *out_stream );
|
|
|
|
fclose( *out_stream );
|
|
|
|
*out_stream = fopen( path, "w" );
|
|
|
|
|
|
|
|
if( *out_stream == NULL )
|
|
|
|
{
|
2019-01-31 14:20:20 +01:00
|
|
|
close( stdout_fd );
|
2016-10-05 11:57:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int restore_output( FILE** out_stream, int old_fd )
|
|
|
|
{
|
|
|
|
fflush( *out_stream );
|
|
|
|
fclose( *out_stream );
|
|
|
|
|
|
|
|
*out_stream = fdopen( old_fd, "w" );
|
|
|
|
if( *out_stream == NULL )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-13 00:07:30 +02:00
|
|
|
|
2016-10-13 12:26:29 +02:00
|
|
|
static void close_output( FILE* out_stream )
|
2016-10-13 00:07:30 +02:00
|
|
|
{
|
2016-10-13 12:26:29 +02:00
|
|
|
fclose( out_stream );
|
2016-10-13 00:07:30 +02:00
|
|
|
}
|
2016-10-05 11:57:49 +02:00
|
|
|
#endif /* __unix__ || __APPLE__ __MACH__ */
|