From 32ff13ae3dfc07e0d4082a6c4bc26f5c90b13af0 Mon Sep 17 00:00:00 2001 From: SimonB Date: Wed, 17 Feb 2016 23:34:30 +0000 Subject: [PATCH] Refactored test suite template code Restructed test suite helper and main code to support tests suite helper functions, changed C++ comments to C-style, and made the generated source code more navigable. --- tests/scripts/generate_code.pl | 2 +- tests/suites/helpers.function | 41 ++++++++++++++++++++++++++++++++- tests/suites/main_test.function | 40 +------------------------------- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/tests/scripts/generate_code.pl b/tests/scripts/generate_code.pl index ba61b680a..30ee6b01c 100755 --- a/tests/scripts/generate_code.pl +++ b/tests/scripts/generate_code.pl @@ -131,7 +131,7 @@ print TEST_FILE << "END"; /*----------------------------------------------------------------------------*/ -/* Common helper functions */ +/* Common helper code */ $test_common_helpers diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index 8f681dbd4..c18eed895 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -1,3 +1,6 @@ +/*----------------------------------------------------------------------------*/ +/* Headers */ + #if defined(MBEDTLS_PLATFORM_C) #include "mbedtls/platform.h" #else @@ -12,6 +15,10 @@ #define mbedtls_snprintf snprintf #endif +#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) +#include "mbedtls/memory_buffer_alloc.h" +#endif + #ifdef _MSC_VER #include typedef UINT32 uint32_t; @@ -23,6 +30,25 @@ typedef UINT32 uint32_t; #include #include + +/*----------------------------------------------------------------------------*/ +/* Global variables */ + +static int test_errors = 0; + + +/*----------------------------------------------------------------------------*/ +/* Macros */ + +#define TEST_ASSERT( TEST ) \ + do { \ + if( ! (TEST) ) \ + { \ + test_fail( #TEST ); \ + goto exit; \ + } \ + } while( 0 ) + #define assert(a) if( !( a ) ) \ { \ mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \ @@ -53,11 +79,15 @@ typedef UINT32 uint32_t; } #endif + +/*----------------------------------------------------------------------------*/ +/* Helper Functions */ + static int unhexify( unsigned char *obuf, const char *ibuf ) { unsigned char c, c2; int len = strlen( ibuf ) / 2; - assert( strlen( ibuf ) % 2 == 0 ); // must be even number of bytes + assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */ while( *ibuf != 0 ) { @@ -298,3 +328,12 @@ static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len ) return( 0 ); } + +static void test_fail( const char *test ) +{ + test_errors++; + if( test_errors == 1 ) + mbedtls_printf( "FAILED\n" ); + mbedtls_printf( " %s\n", test ); +} + diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function index 2a21441a4..7ec69b45d 100644 --- a/tests/suites/main_test.function +++ b/tests/suites/main_test.function @@ -1,44 +1,6 @@ -#include - -#if defined(MBEDTLS_PLATFORM_C) -#include "mbedtls/platform.h" -#else -#include -#include -#define mbedtls_exit exit -#define mbedtls_free free -#define mbedtls_calloc calloc -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#define mbedtls_snprintf snprintf -#endif - -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) -#include "mbedtls/memory_buffer_alloc.h" -#endif - -static int test_errors = 0; - SUITE_PRE_DEP #define TEST_SUITE_ACTIVE -static void test_fail( const char *test ) -{ - test_errors++; - if( test_errors == 1 ) - mbedtls_printf( "FAILED\n" ); - mbedtls_printf( " %s\n", test ); -} - -#define TEST_ASSERT( TEST ) \ - do { \ - if( ! (TEST) ) \ - { \ - test_fail( #TEST ); \ - goto exit; \ - } \ - } while( 0 ) - int verify_string( char **str ) { if( (*str)[0] != '"' || @@ -190,7 +152,7 @@ int parse_arguments( char *buf, size_t len, char *params[50] ) p++; } - // Replace newlines, question marks and colons in strings + /* Replace newlines, question marks and colons in strings */ for( i = 0; i < cnt; i++ ) { p = params[i];