Fix exit code and add a count of the test suites

Now counts and displays the number of test suites executed, which can vary
depending on build configurations.
All tests are now executed as this is a sample and test program, rather than
exit on first failure.
Exit code now restricted to SUCCESS or FAILURE.
This commit is contained in:
SimonB 2016-03-11 00:40:54 +00:00 committed by Simon Butcher
parent f413b6fffe
commit 5a8afb848a

View File

@ -99,7 +99,8 @@ static int run_test_snprintf( void )
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
int ret = 0, v; int ret = 0, v, suites_tested = 0, suites_failed =0,
exitcode = EXIT_SUCCESS;
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
unsigned char buf[1000000]; unsigned char buf[1000000];
#endif #endif
@ -126,8 +127,11 @@ int main( int argc, char *argv[] )
return( 0 ); return( 0 );
} }
if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 ) if( argc == 2 && ( strcmp( argv[1], "--quiet" ) == 0 ||
strcmp( argv[1], "-q" ) == 0 ) )
{
v = 0; v = 0;
}
else else
{ {
v = 1; v = 1;
@ -142,134 +146,212 @@ int main( int argc, char *argv[] )
#if defined(MBEDTLS_MD2_C) #if defined(MBEDTLS_MD2_C)
if( ( ret = mbedtls_md2_self_test( v ) ) != 0 ) if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_MD4_C) #if defined(MBEDTLS_MD4_C)
if( ( ret = mbedtls_md4_self_test( v ) ) != 0 ) if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_MD5_C) #if defined(MBEDTLS_MD5_C)
if( ( ret = mbedtls_md5_self_test( v ) ) != 0 ) if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_RIPEMD160_C) #if defined(MBEDTLS_RIPEMD160_C)
if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 ) if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_SHA1_C) #if defined(MBEDTLS_SHA1_C)
if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 ) if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_SHA256_C) #if defined(MBEDTLS_SHA256_C)
if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 ) if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_SHA512_C) #if defined(MBEDTLS_SHA512_C)
if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 ) if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_ARC4_C) #if defined(MBEDTLS_ARC4_C)
if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 ) if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_DES_C) #if defined(MBEDTLS_DES_C)
if( ( ret = mbedtls_des_self_test( v ) ) != 0 ) if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_AES_C) #if defined(MBEDTLS_AES_C)
if( ( ret = mbedtls_aes_self_test( v ) ) != 0 ) if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C) #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 ) if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C) #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 ) if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_BASE64_C) #if defined(MBEDTLS_BASE64_C)
if( ( ret = mbedtls_base64_self_test( v ) ) != 0 ) if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_BIGNUM_C) #if defined(MBEDTLS_BIGNUM_C)
if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 ) if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_RSA_C) #if defined(MBEDTLS_RSA_C)
if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 ) if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_X509_USE_C) #if defined(MBEDTLS_X509_USE_C)
if( ( ret = mbedtls_x509_self_test( v ) ) != 0 ) if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_XTEA_C) #if defined(MBEDTLS_XTEA_C)
if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 ) if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_CAMELLIA_C) #if defined(MBEDTLS_CAMELLIA_C)
if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 ) if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_CTR_DRBG_C) #if defined(MBEDTLS_CTR_DRBG_C)
if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 ) if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_HMAC_DRBG_C) #if defined(MBEDTLS_HMAC_DRBG_C)
if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 ) if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_ECP_C) #if defined(MBEDTLS_ECP_C)
if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 ) if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_ECJPAKE_C) #if defined(MBEDTLS_ECJPAKE_C)
if( ( ret = mbedtls_ecjpake_self_test( v ) ) != 0 ) if( ( ret = mbedtls_ecjpake_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_DHM_C) #if defined(MBEDTLS_DHM_C)
if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 ) if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_ENTROPY_C) #if defined(MBEDTLS_ENTROPY_C)
if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 ) if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#if defined(MBEDTLS_PKCS5_C) #if defined(MBEDTLS_PKCS5_C)
if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 ) if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
/* Slow tests last */ /* Slow tests last */
#if defined(MBEDTLS_TIMING_C) #if defined(MBEDTLS_TIMING_C)
if( ( ret = mbedtls_timing_self_test( v ) ) != 0 ) if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
#else #else
@ -285,19 +367,34 @@ int main( int argc, char *argv[] )
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
mbedtls_memory_buffer_alloc_free(); mbedtls_memory_buffer_alloc_free();
if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 ) if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
return( ret ); {
suites_failed++;
}
suites_tested++;
#endif #endif
if( v != 0 ) if( v != 0 )
{ {
mbedtls_printf( " [ All tests passed ]\n\n" ); mbedtls_printf( " Executed %d test suites\n\n", suites_tested);
if( suites_failed > 0)
{
mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
}
else
{
mbedtls_printf( " [ All tests PASS ]\n\n" );
}
#if defined(_WIN32) #if defined(_WIN32)
mbedtls_printf( " Press Enter to exit this program.\n" ); mbedtls_printf( " Press Enter to exit this program.\n" );
fflush( stdout ); getchar(); fflush( stdout ); getchar();
#endif #endif
} }
return( ret ); if( suites_failed > 0)
exitcode = EXIT_FAILURE;
exit(exitcode);
} }