From 9d7dfb74d137c8f940a2e8c21ba66178462e9620 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 20 Dec 2017 20:23:46 +0100 Subject: [PATCH] selftest: refactor to separate the list of tests from the logic No behavior change. --- programs/test/selftest.c | 275 +++++++++++++++++++-------------------- 1 file changed, 133 insertions(+), 142 deletions(-) diff --git a/programs/test/selftest.c b/programs/test/selftest.c index 82e9581f0..399f0d721 100644 --- a/programs/test/selftest.c +++ b/programs/test/selftest.c @@ -96,9 +96,120 @@ static int run_test_snprintf( void ) test_snprintf( 5, "123", 3 ) != 0 ); } +#if defined(MBEDTLS_SELF_TEST) +#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) +int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose ) +{ + if( verbose != 0 ) + { +#if defined(MBEDTLS_MEMORY_DEBUG) + mbedtls_memory_buffer_alloc_status( ); +#endif + } + mbedtls_memory_buffer_alloc_free( ); + return( mbedtls_memory_buffer_alloc_self_test( verbose ) ); +} +#endif + +typedef struct +{ + const char *name; + int ( *function )( int ); +} selftest_t; + +const selftest_t selftests[] = +{ +#if defined(MBEDTLS_MD2_C) + {"md2", mbedtls_md2_self_test}, +#endif +#if defined(MBEDTLS_MD4_C) + {"md4", mbedtls_md4_self_test}, +#endif +#if defined(MBEDTLS_MD5_C) + {"md5", mbedtls_md5_self_test}, +#endif +#if defined(MBEDTLS_RIPEMD160_C) + {"ripemd160", mbedtls_ripemd160_self_test}, +#endif +#if defined(MBEDTLS_SHA1_C) + {"sha1", mbedtls_sha1_self_test}, +#endif +#if defined(MBEDTLS_SHA256_C) + {"sha256", mbedtls_sha256_self_test}, +#endif +#if defined(MBEDTLS_SHA512_C) + {"sha512", mbedtls_sha512_self_test}, +#endif +#if defined(MBEDTLS_ARC4_C) + {"arc4", mbedtls_arc4_self_test}, +#endif +#if defined(MBEDTLS_DES_C) + {"des", mbedtls_des_self_test}, +#endif +#if defined(MBEDTLS_AES_C) + {"aes", mbedtls_aes_self_test}, +#endif +#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C) + {"gcm", mbedtls_gcm_self_test}, +#endif +#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C) + {"ccm", mbedtls_ccm_self_test}, +#endif +#if defined(MBEDTLS_BASE64_C) + {"base64", mbedtls_base64_self_test}, +#endif +#if defined(MBEDTLS_BIGNUM_C) + {"mpi", mbedtls_mpi_self_test}, +#endif +#if defined(MBEDTLS_RSA_C) + {"rsa", mbedtls_rsa_self_test}, +#endif +#if defined(MBEDTLS_X509_USE_C) + {"x509", mbedtls_x509_self_test}, +#endif +#if defined(MBEDTLS_XTEA_C) + {"xtea", mbedtls_xtea_self_test}, +#endif +#if defined(MBEDTLS_CAMELLIA_C) + {"camellia", mbedtls_camellia_self_test}, +#endif +#if defined(MBEDTLS_CTR_DRBG_C) + {"ctr_drbg", mbedtls_ctr_drbg_self_test}, +#endif +#if defined(MBEDTLS_HMAC_DRBG_C) + {"hmac_drbg", mbedtls_hmac_drbg_self_test}, +#endif +#if defined(MBEDTLS_ECP_C) + {"ecp", mbedtls_ecp_self_test}, +#endif +#if defined(MBEDTLS_DHM_C) + {"dhm", mbedtls_dhm_self_test}, +#endif +#if defined(MBEDTLS_ENTROPY_C) + {"entropy", mbedtls_entropy_self_test}, +#endif +#if defined(MBEDTLS_PKCS5_C) + {"pkcs5", mbedtls_pkcs5_self_test}, +#endif +/* Slower test after the faster ones */ +#if defined(MBEDTLS_TIMING_C) + {"timing", mbedtls_timing_self_test}, +#endif +/* Heap test comes last */ +#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) + {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test}, +#endif + {NULL, NULL} +}; +#endif /* MBEDTLS_SELF_TEST */ + int main( int argc, char *argv[] ) { - int ret = 0, v; +#if defined(MBEDTLS_SELF_TEST) + const selftest_t *test; +#endif /* MBEDTLS_SELF_TEST */ + int v; + int suites_tested = 0, suites_failed = 0; #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) unsigned char buf[1000000]; #endif @@ -139,132 +250,14 @@ int main( int argc, char *argv[] ) mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) ); #endif -#if defined(MBEDTLS_MD2_C) - if( ( ret = mbedtls_md2_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_MD4_C) - if( ( ret = mbedtls_md4_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_MD5_C) - if( ( ret = mbedtls_md5_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_RIPEMD160_C) - if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_SHA1_C) - if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_SHA256_C) - if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_SHA512_C) - if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_ARC4_C) - if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_DES_C) - if( ( ret = mbedtls_des_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_AES_C) - if( ( ret = mbedtls_aes_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C) - if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C) - if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_BASE64_C) - if( ( ret = mbedtls_base64_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_BIGNUM_C) - if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_RSA_C) - if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_X509_USE_C) - if( ( ret = mbedtls_x509_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_XTEA_C) - if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_CAMELLIA_C) - if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_CTR_DRBG_C) - if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_HMAC_DRBG_C) - if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_ECP_C) - if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_DHM_C) - if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_ENTROPY_C) - if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 ) - return( ret ); -#endif - -#if defined(MBEDTLS_PKCS5_C) - if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 ) - return( ret ); -#endif - -/* Slow tests last */ - -#if defined(MBEDTLS_TIMING_C) - if( ( ret = mbedtls_timing_self_test( v ) ) != 0 ) - return( ret ); -#endif + for( test = selftests; test->name != NULL; test++ ) + { + if( test->function( v ) != 0 ) + { + suites_failed++; + } + suites_tested++; + } #else mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" ); @@ -272,26 +265,24 @@ int main( int argc, char *argv[] ) if( v != 0 ) { -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG) - mbedtls_memory_buffer_alloc_status(); -#endif - } + mbedtls_printf( " Executed %d test suites\n\n", suites_tested ); -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) - mbedtls_memory_buffer_alloc_free(); - - if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 ) - return( ret ); -#endif - - if( v != 0 ) - { - mbedtls_printf( " [ All tests passed ]\n\n" ); + if( suites_failed > 0) + { + mbedtls_printf( " [ %d tests FAILED ]\n\n", suites_failed ); + } + else + { + mbedtls_printf( " [ All tests passed ]\n\n" ); + } #if defined(_WIN32) mbedtls_printf( " Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif } - return( ret ); + if( suites_failed > 0) + return( EXIT_FAILURE ); + + return( EXIT_SUCCESS ); }