diff --git a/programs/psa/crypto_examples.c b/programs/psa/crypto_examples.c index 97beb0eb4b..8160789ae5 100644 --- a/programs/psa/crypto_examples.c +++ b/programs/psa/crypto_examples.c @@ -319,18 +319,6 @@ static void cipher_examples( void ) printf( "\tsuccess!\r\n" ); } -#if defined(MBEDTLS_CHECK_PARAMS) -#include "mbedtls/platform_util.h" -void mbedtls_param_failed( const char *failure_condition, - const char *file, - int line ) -{ - printf( "%s:%i: Input param failed - %s\n", - file, line, failure_condition ); - exit( EXIT_FAILURE ); -} -#endif - int main( void ) { ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); diff --git a/programs/psa/key_ladder_demo.c b/programs/psa/key_ladder_demo.c index b633f75781..17843e1b74 100644 --- a/programs/psa/key_ladder_demo.c +++ b/programs/psa/key_ladder_demo.c @@ -605,18 +605,6 @@ static void usage( void ) printf( " and the same sequence of labels.\n" ); } -#if defined(MBEDTLS_CHECK_PARAMS) -#include "mbedtls/platform_util.h" -void mbedtls_param_failed( const char *failure_condition, - const char *file, - int line ) -{ - printf( "%s:%i: Input param failed - %s\n", - file, line, failure_condition ); - exit( EXIT_FAILURE ); -} -#endif - int main( int argc, char *argv[] ) { const char *key_file_name = "master.key"; diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h index 0c516355a8..79a63fbd3a 100644 --- a/tests/include/test/helpers.h +++ b/tests/include/test/helpers.h @@ -103,4 +103,83 @@ unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen ); int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len ); +#if defined(MBEDTLS_CHECK_PARAMS) + +typedef struct +{ + const char *failure_condition; + const char *file; + int line; +} +mbedtls_test_param_failed_location_record_t; + +/** + * \brief Get the location record of the last call to + * mbedtls_test_param_failed(). + * + * \note The call expectation is set up and active until the next call to + * mbedtls_test_param_failed_check_expected_call() or + * mbedtls_param_failed() that cancels it. + */ +void mbedtls_test_param_failed_get_location_record( + mbedtls_test_param_failed_location_record_t *location_record ); + +/** + * \brief State that a call to mbedtls_param_failed() is expected. + * + * \note The call expectation is set up and active until the next call to + * mbedtls_test_param_failed_check_expected_call() or + * mbedtls_param_failed that cancel it. + */ +void mbedtls_test_param_failed_expect_call( void ); + +/** + * \brief Check whether mbedtls_param_failed() has been called as expected. + * + * \note Check whether mbedtls_param_failed() has been called between the + * last call to mbedtls_test_param_failed_expect_call() and the call + * to this function. + * + * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(), + * mbedtls_param_failed() has been called. + * \c -1 Otherwise. + */ +int mbedtls_test_param_failed_check_expected_call( void ); + +/** + * \brief Get a pointer to the object of type jmp_buf holding the execution + * state information used by mbedtls_param_failed() to do a long jump. + * + * \note If a call to mbedtls_param_failed() is not expected in the sense + * that there is no call to mbedtls_test_param_failed_expect_call() + * preceding it, then mbedtls_param_failed() will try to restore the + * execution to the state stored in the jmp_buf object whose address + * is returned by the present function. + * + * \note The returned pointer is of type void* as its type is opaque, + * implementation dependent (jmp_buf is an array type not the type of + * one element of an array). + * + * \return Address of the object of type jmp_buf holding the execution state + * information used by mbedtls_param_failed() to do a long jump. + */ +void* mbedtls_test_param_failed_get_state_buf( void ); + +/** + * \brief Reset the execution state used by mbedtls_param_failed() to do a + * long jump. + * + * \note If a call to mbedtls_param_failed() is not expected in the sense + * that there is no call to mbedtls_test_param_failed_expect_call() + * preceding it, then mbedtls_param_failed() will try to restore the + * execution state that this function reset. + * + * \note It is recommended to reset the execution state when the state + * is not relevant anymore. That way an unexpected call to + * mbedtls_param_failed() will not trigger a long jump with + * undefined behavior but rather a long jump that will rather fault. + */ +void mbedtls_test_param_failed_reset_state( void ); +#endif /* MBEDTLS_CHECK_PARAMS */ + #endif /* TEST_HELPERS_H */ diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 01f5910ffd..60e5302664 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -1183,9 +1183,7 @@ component_test_check_params_functionality () { scripts/config.py full # includes CHECK_PARAMS # Make MBEDTLS_PARAM_FAILED call mbedtls_param_failed(). scripts/config.py unset MBEDTLS_CHECK_PARAMS_ASSERT - # Only build and run tests. Do not build sample programs, because - # they don't have a mbedtls_param_failed() function. - make CC=gcc CFLAGS='-Werror -O1' lib test + make CC=gcc CFLAGS='-Werror -O1' all test } component_test_check_params_without_platform () { diff --git a/tests/src/helpers.c b/tests/src/helpers.c index b9abf19aa3..a963da974a 100644 --- a/tests/src/helpers.c +++ b/tests/src/helpers.c @@ -21,10 +21,34 @@ #include #include +#if defined(MBEDTLS_CHECK_PARAMS) +#include +#endif + +/*----------------------------------------------------------------------------*/ +/* Static global variables */ + +#if defined(MBEDTLS_CHECK_PARAMS) +typedef struct +{ + uint8_t expected_call; + uint8_t expected_call_happened; + + jmp_buf state; + + mbedtls_test_param_failed_location_record_t location_record; +} +param_failed_ctx_t; +static param_failed_ctx_t param_failed_ctx; +#endif + #if defined(MBEDTLS_PLATFORM_C) static mbedtls_platform_context platform_ctx; #endif +/*----------------------------------------------------------------------------*/ +/* Helper Functions */ + int mbedtls_test_platform_setup( void ) { int ret = 0; @@ -161,3 +185,64 @@ int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, } return ret; } + +#if defined(MBEDTLS_CHECK_PARAMS) +void mbedtls_test_param_failed_get_location_record( + mbedtls_test_param_failed_location_record_t *location_record ) +{ + *location_record = param_failed_ctx.location_record; +} + +void mbedtls_test_param_failed_expect_call( void ) +{ + param_failed_ctx.expected_call_happened = 0; + param_failed_ctx.expected_call = 1; +} + +int mbedtls_test_param_failed_check_expected_call( void ) +{ + param_failed_ctx.expected_call = 0; + + if( param_failed_ctx.expected_call_happened != 0 ) + return( 0 ); + + return( -1 ); +} + +void* mbedtls_test_param_failed_get_state_buf( void ) +{ + return ¶m_failed_ctx.state[0]; +} + +void mbedtls_test_param_failed_reset_state( void ) +{ + memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) ); +} + +void mbedtls_param_failed( const char *failure_condition, + const char *file, + int line ) +{ + /* Record the location of the failure */ + param_failed_ctx.location_record.failure_condition = failure_condition; + param_failed_ctx.location_record.file = file; + param_failed_ctx.location_record.line = line; + + /* If we are testing the callback function... */ + if( param_failed_ctx.expected_call != 0 ) + { + param_failed_ctx.expected_call = 0; + param_failed_ctx.expected_call_happened = 1; + } + else + { + /* ...else try a long jump. If the execution state has not been set-up + * or reset then the long jump buffer is all zero's and the call will + * with high probability fault, emphasizing there is something to look + * at. + */ + + longjmp( param_failed_ctx.state, 1 ); + } +} +#endif /* MBEDTLS_CHECK_PARAMS */ diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index 3180a27e6a..a3bfae3f07 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -361,26 +361,6 @@ test_info_t; static test_info_t test_info; #if defined(MBEDTLS_CHECK_PARAMS) -typedef struct -{ - const char *failure_condition; - const char *file; - int line; -} -mbedtls_test_param_failed_location_record_t; - -typedef struct -{ - uint8_t expected_call; - uint8_t expected_call_happened; - - jmp_buf state; - - mbedtls_test_param_failed_location_record_t location_record; -} -param_failed_ctx_t; -static param_failed_ctx_t param_failed_ctx; - jmp_buf jmp_tmp; #endif @@ -431,124 +411,6 @@ void test_skip( const char *test, int line_no, const char* filename ) test_info.filename = filename; } -#if defined(MBEDTLS_CHECK_PARAMS) -/** - * \brief Get the location record of the last call to - * mbedtls_test_param_failed(). - * - * \note The call expectation is set up and active until the next call to - * mbedtls_test_param_failed_check_expected_call() or - * mbedtls_param_failed() that cancels it. - */ -void mbedtls_test_param_failed_get_location_record( - mbedtls_test_param_failed_location_record_t *location_record ) -{ - *location_record = param_failed_ctx.location_record; -} - -/** - * \brief State that a call to mbedtls_param_failed() is expected. - * - * \note The call expectation is set up and active until the next call to - * mbedtls_test_param_failed_check_expected_call() or - * mbedtls_param_failed that cancel it. - */ -void mbedtls_test_param_failed_expect_call( void ) -{ - param_failed_ctx.expected_call_happened = 0; - param_failed_ctx.expected_call = 1; -} - -/** - * \brief Check whether mbedtls_param_failed() has been called as expected. - * - * \note Check whether mbedtls_param_failed() has been called between the - * last call to mbedtls_test_param_failed_expect_call() and the call - * to this function. - * - * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(), - * mbedtls_param_failed() has been called. - * \c -1 Otherwise. - */ -int mbedtls_test_param_failed_check_expected_call( void ) -{ - param_failed_ctx.expected_call = 0; - - if( param_failed_ctx.expected_call_happened != 0 ) - return( 0 ); - - return( -1 ); -} - -/** - * \brief Get a pointer to the object of type jmp_buf holding the execution - * state information used by mbedtls_param_failed() to do a long jump. - * - * \note If a call to mbedtls_param_failed() is not expected in the sense - * that there is no call to mbedtls_test_param_failed_expect_call() - * preceding it, then mbedtls_param_failed() will try to restore the - * execution to the state stored in the jmp_buf object whose address - * is returned by the present function. - * - * \note The returned pointer is of type void* as its type is opaque, - * implementation dependent (jmp_buf is an array type not the type of - * one element of an array). - * - * \return Address of the object of type jmp_buf holding the execution state - * information used by mbedtls_param_failed() to do a long jump. - */ -void* mbedtls_test_param_failed_get_state_buf( void ) -{ - return ¶m_failed_ctx.state[0]; -} - -/** - * \brief Reset the execution state used by mbedtls_param_failed() to do a - * long jump. - * - * \note If a call to mbedtls_param_failed() is not expected in the sense - * that there is no call to mbedtls_test_param_failed_expect_call() - * preceding it, then mbedtls_param_failed() will try to restore the - * execution state that this function reset. - * - * \note It is recommended to reset the execution state when the state - * is not relevant anymore. That way an unexpected call to - * mbedtls_param_failed() will not trigger a long jump with - * undefined behavior but rather a long jump that will rather fault. - */ -void mbedtls_test_param_failed_reset_state( void ) -{ - memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) ); -} - -void mbedtls_param_failed( const char *failure_condition, - const char *file, - int line ) -{ - /* Record the location of the failure */ - param_failed_ctx.location_record.failure_condition = failure_condition; - param_failed_ctx.location_record.file = file; - param_failed_ctx.location_record.line = line; - - /* If we are testing the callback function... */ - if( param_failed_ctx.expected_call != 0 ) - { - param_failed_ctx.expected_call = 0; - param_failed_ctx.expected_call_happened = 1; - } - else - { - /* ...else try a long jump. If the execution state has not been set-up - * or reset then the long jump buffer is all zero's and the call will - * with high probability fault, emphasizing there is something to look - * at. - */ - - longjmp( param_failed_ctx.state, 1 ); - } -} -#endif - #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) static int redirect_output( FILE** out_stream, const char* path ) {