Move helper testing functions to tests/src/helpers.c
Moves the functions `test_fail`, `test_set_step`, `test_skip` and the struct
`test_info` from `tests/suites/helpers.function` to `tests/src/helpers.*`.
This is done to open these functions up to the API where they can be used by
other functions in the 'src' test infrastructure module.
As the functions are now contained within the src folder of the testing
infrastructure, the `mbedtls_` prefix has been added to the functions.
Signed-off-by: Chris Jones <christopher.jones@arm.com>
diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h
index 2c7b179..8d31048 100644
--- a/tests/include/test/helpers.h
+++ b/tests/include/test/helpers.h
@@ -49,9 +49,41 @@
#include <stddef.h>
#include <stdint.h>
+typedef enum
+{
+ TEST_RESULT_SUCCESS = 0,
+ TEST_RESULT_FAILED,
+ TEST_RESULT_SKIPPED
+} test_result_t;
+
+typedef struct
+{
+ test_result_t result;
+ const char *test;
+ const char *filename;
+ int line_no;
+ unsigned long step;
+}
+test_info_t;
+extern test_info_t test_info;
+
int mbedtls_test_platform_setup( void );
void mbedtls_test_platform_teardown( void );
+void mbedtls_test_fail( const char *test, int line_no, const char* filename );
+
+/** 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 mbedtls_test_set_step( unsigned long step );
+
+void mbedtls_test_skip( const char *test, int line_no, const char* filename );
+
/**
* \brief This function decodes the hexadecimal representation of
* data.
diff --git a/tests/src/helpers.c b/tests/src/helpers.c
index a18f1d4..a2f9c3f 100644
--- a/tests/src/helpers.c
+++ b/tests/src/helpers.c
@@ -44,6 +44,8 @@
static mbedtls_platform_context platform_ctx;
#endif
+test_info_t test_info;
+
/*----------------------------------------------------------------------------*/
/* Helper Functions */
@@ -77,6 +79,33 @@
return( 0 );
}
+void mbedtls_test_fail( const char *test, int line_no, const char* filename )
+{
+ if( test_info.result == TEST_RESULT_FAILED )
+ {
+ /* We've already recorded the test as having failed. Don't
+ * overwrite any previous information about the failure. */
+ return;
+ }
+ test_info.result = TEST_RESULT_FAILED;
+ test_info.test = test;
+ test_info.line_no = line_no;
+ test_info.filename = filename;
+}
+
+void mbedtls_test_set_step( unsigned long step )
+{
+ test_info.step = step;
+}
+
+void mbedtls_test_skip( const char *test, int line_no, const char* filename )
+{
+ test_info.result = TEST_RESULT_SKIPPED;
+ test_info.test = test;
+ test_info.line_no = line_no;
+ test_info.filename = filename;
+}
+
int mbedtls_test_unhexify( unsigned char *obuf,
size_t obufmax,
const char *ibuf,
diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function
index 1dc6721..9762d41 100644
--- a/tests/suites/helpers.function
+++ b/tests/suites/helpers.function
@@ -108,7 +108,7 @@
do { \
if( ! (TEST) ) \
{ \
- test_fail( #TEST, __LINE__, __FILE__ ); \
+ mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
goto exit; \
} \
} while( 0 )
@@ -201,13 +201,13 @@
*
* \param TEST The test expression to be tested.
*/
-#define TEST_ASSUME( TEST ) \
- do { \
- if( ! (TEST) ) \
- { \
- test_skip( #TEST, __LINE__, __FILE__ ); \
- goto exit; \
- } \
+#define TEST_ASSUME( TEST ) \
+ do { \
+ if( ! (TEST) ) \
+ { \
+ mbedtls_test_skip( #TEST, __LINE__, __FILE__ ); \
+ goto exit; \
+ } \
} while( 0 )
#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
@@ -237,7 +237,7 @@
if( ( ( TEST ) != ( PARAM_ERR_VALUE ) ) || \
( mbedtls_test_param_failed_check_expected_call( ) != 0 ) ) \
{ \
- test_fail( #TEST, __LINE__, __FILE__ ); \
+ mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
goto exit; \
} \
mbedtls_test_param_failed_check_expected_call( ); \
@@ -270,7 +270,7 @@
if( setjmp( mbedtls_test_param_failed_get_state_buf( ) ) == 0 ) \
{ \
TEST; \
- test_fail( #TEST, __LINE__, __FILE__ ); \
+ mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
goto exit; \
} \
mbedtls_test_param_failed_reset_state( ); \
@@ -346,24 +346,6 @@
/*----------------------------------------------------------------------------*/
/* Global variables */
-typedef enum
-{
- TEST_RESULT_SUCCESS = 0,
- TEST_RESULT_FAILED,
- TEST_RESULT_SKIPPED
-} test_result_t;
-
-typedef struct
-{
- test_result_t result;
- const char *test;
- const char *filename;
- int line_no;
- unsigned long step;
-}
-test_info_t;
-static test_info_t test_info;
-
#if defined(MBEDTLS_CHECK_PARAMS)
jmp_buf jmp_tmp;
#endif
@@ -386,41 +368,6 @@
/*----------------------------------------------------------------------------*/
/* Helper Functions */
-/** 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;
-}
-
-void test_fail( const char *test, int line_no, const char* filename )
-{
- if( test_info.result == TEST_RESULT_FAILED )
- {
- /* We've already recorded the test as having failed. Don't
- * overwrite any previous information about the failure. */
- return;
- }
- 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;
- test_info.test = test;
- test_info.line_no = line_no;
- test_info.filename = filename;
-}
-
#if defined(MBEDTLS_PSA_CRYPTO_C)
/** Check that no PSA Crypto key slots are in use.
*
@@ -435,7 +382,7 @@
return 0;
else
{
- test_fail( msg, line_no, filename );
+ mbedtls_test_fail( msg, line_no, filename );
return 1;
}
}
diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function
index 97026c6..57395ae 100644
--- a/tests/suites/main_test.function
+++ b/tests/suites/main_test.function
@@ -179,9 +179,9 @@
{
/* Unexpected parameter validation error */
mbedtls_test_param_failed_get_location_record( &location_record );
- test_fail( location_record.failure_condition,
- location_record.line,
- location_record.file );
+ mbedtls_test_fail( location_record.failure_condition,
+ location_record.line,
+ location_record.file );
}
mbedtls_test_param_failed_reset_state( );
diff --git a/tests/suites/test_suite_asn1parse.function b/tests/suites/test_suite_asn1parse.function
index 990f343..51cb3ca 100644
--- a/tests/suites/test_suite_asn1parse.function
+++ b/tests/suites/test_suite_asn1parse.function
@@ -130,7 +130,7 @@
size_t parsed_length;
int ret;
- test_set_step( buffer_size );
+ mbedtls_test_set_step( buffer_size );
/* Allocate a new buffer of exactly the length to parse each time.
* This gives memory sanitizers a chance to catch buffer overreads. */
if( buffer_size == 0 )
@@ -198,7 +198,7 @@
TEST_ASSERT( content > state->input_start );
offset = content - state->input_start;
- test_set_step( offset );
+ mbedtls_test_set_step( offset );
if( *rest == 0 )
return( RET_TRAVERSE_STOP );
@@ -252,7 +252,7 @@
*/
for( buffer_size = 1; buffer_size <= input->len + 1; buffer_size++ )
{
- test_set_step( buffer_size );
+ mbedtls_test_set_step( buffer_size );
/* Allocate a new buffer of exactly the length to parse each time.
* This gives memory sanitizers a chance to catch buffer overreads. */
ASSERT_ALLOC( buf, buffer_size );
diff --git a/tests/suites/test_suite_asn1write.function b/tests/suites/test_suite_asn1write.function
index 21465c7..8824739 100644
--- a/tests/suites/test_suite_asn1write.function
+++ b/tests/suites/test_suite_asn1write.function
@@ -15,7 +15,7 @@
int generic_write_start_step( generic_write_data_t *data )
{
- test_set_step( data->size );
+ mbedtls_test_set_step( data->size );
ASSERT_ALLOC( data->output, data->size == 0 ? 1 : data->size );
data->end = data->output + data->size;
data->p = data->end;
diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function
index d486dd1..a45d7e0 100644
--- a/tests/suites/test_suite_psa_crypto.function
+++ b/tests/suites/test_suite_psa_crypto.function
@@ -537,7 +537,8 @@
hash_alg = KNOWN_SUPPORTED_HASH_ALG;
alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
#else
- test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
+ mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
+ __LINE__, __FILE__ );
return( 1 );
#endif
}
@@ -997,7 +998,7 @@
mbedtls_snprintf( message, sizeof( message ),
"No sanity check for public key type=0x%08lx",
(unsigned long) type );
- test_fail( message, __LINE__, __FILE__ );
+ mbedtls_test_fail( message, __LINE__, __FILE__ );
(void) p;
(void) end;
return( 0 );
@@ -1111,8 +1112,8 @@
* asymmetric, also check \p psa_export_public_key.
*
* If the key fails the tests, this function calls the test framework's
- * `test_fail` function and returns false. Otherwise this function returns
- * true. Therefore it should be used as follows:
+ * `mbedtls_test_fail` function and returns false. Otherwise this function
+ * returns true. Therefore it should be used as follows:
* ```
* if( ! exercise_key( ... ) ) goto exit;
* ```
@@ -1158,7 +1159,7 @@
mbedtls_snprintf( message, sizeof( message ),
"No code to exercise alg=0x%08lx",
(unsigned long) alg );
- test_fail( message, __LINE__, __FILE__ );
+ mbedtls_test_fail( message, __LINE__, __FILE__ );
ok = 0;
}
@@ -2672,7 +2673,7 @@
/* Compare with corrupted value */
for( i = 0; i < output_length; i++ )
{
- test_set_step( i );
+ mbedtls_test_set_step( i );
output[i] ^= 1;
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
output, output_length ),
@@ -3147,7 +3148,7 @@
( output_size >= expected_mac->len ? PSA_SUCCESS :
PSA_ERROR_BUFFER_TOO_SMALL );
- test_set_step( output_size );
+ mbedtls_test_set_step( output_size );
ASSERT_ALLOC( actual_mac, output_size );
/* Calculate the MAC. */
@@ -3233,7 +3234,7 @@
/* Test changing one byte. */
for( size_t i = 0; i < expected_mac->len; i++ )
{
- test_set_step( i );
+ mbedtls_test_set_step( i );
perturbed_mac[i] ^= 1;
PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
PSA_ASSERT( psa_mac_update( &operation,
diff --git a/tests/suites/test_suite_psa_crypto_se_driver_hal.function b/tests/suites/test_suite_psa_crypto_se_driver_hal.function
index d623221..59bbb6e 100644
--- a/tests/suites/test_suite_psa_crypto_se_driver_hal.function
+++ b/tests/suites/test_suite_psa_crypto_se_driver_hal.function
@@ -45,7 +45,7 @@
do { \
if( ! (TEST) ) \
{ \
- test_fail( #TEST, __LINE__, __FILE__ ); \
+ mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
return( PSA_ERROR_DETECTED_BY_DRIVER ); \
} \
} while( 0 )
@@ -61,7 +61,7 @@
do { \
if( ! (TEST) ) \
{ \
- test_fail( #TEST, __LINE__, __FILE__ ); \
+ mbedtls_test_fail( #TEST, __LINE__, __FILE__ ); \
status = PSA_ERROR_DETECTED_BY_DRIVER; \
goto exit; \
} \
@@ -72,10 +72,10 @@
* Run the code \p expr. If this returns \p expected_status,
* do nothing. If this returns #PSA_ERROR_DETECTED_BY_DRIVER,
* jump directly to the `exit` label. If this returns any other
- * status, call test_fail() then jump to `exit`.
+ * status, call mbedtls_test_fail() then jump to `exit`.
*
* The special case for #PSA_ERROR_DETECTED_BY_DRIVER is because in this
- * case, the test driver code is expected to have called test_fail()
+ * case, the test driver code is expected to have called mbedtls_test_fail()
* already, so we make sure not to overwrite the failure information.
*/
#define PSA_ASSERT_VIA_DRIVER( expr, expected_status ) \
@@ -85,7 +85,7 @@
goto exit; \
if( PSA_ASSERT_VIA_DRIVER_status != ( expected_status ) ) \
{ \
- test_fail( #expr, __LINE__, __FILE__ ); \
+ mbedtls_test_fail( #expr, __LINE__, __FILE__ ); \
goto exit; \
} \
} while( 0 )
diff --git a/tests/suites/test_suite_ssl.function b/tests/suites/test_suite_ssl.function
index f377ffa..568e66b 100644
--- a/tests/suites/test_suite_ssl.function
+++ b/tests/suites/test_suite_ssl.function
@@ -3609,7 +3609,7 @@
*/
for( i = block_size; i < buflen; i++ )
{
- test_set_step( i );
+ mbedtls_test_set_step( i );
/* Restore correct pre-encryption record */
rec = rec_save;
@@ -3646,7 +3646,7 @@
*/
for( i = padlen; i <= pad_max_len; i++ )
{
- test_set_step( i );
+ mbedtls_test_set_step( i );
/* Restore correct pre-encryption record */
rec = rec_save;
@@ -4466,7 +4466,7 @@
*/
for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
{
- test_set_step( max_in_len * 10000 );
+ mbedtls_test_set_step( max_in_len * 10000 );
/* Use allocated in buffer to catch overreads */
ASSERT_ALLOC( data, max_in_len );
@@ -4474,7 +4474,7 @@
min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
{
- test_set_step( max_in_len * 10000 + in_len );
+ mbedtls_test_set_step( max_in_len * 10000 + in_len );
/* Set up dummy data and add_data */
rec_num++;
@@ -4531,7 +4531,7 @@
for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
{
- test_set_step( (int) secret );
+ mbedtls_test_set_step( (int) secret );
TEST_CF_SECRET( &secret, sizeof( secret ) );
mbedtls_ssl_cf_memcpy_offset( dst, src, secret,