Simplify selftest functions using macros
This reduces clutter, making the functions more readable.
Also, it makes lcov see each line as covered. This is not cheating, as the
lines that were previously seen as not covered are not supposed to be reached
anyway (failing branches of the selftests).
Thanks to this and previous test suite enhancements, lcov now sees chacha20.c
and poly1305.c at 100% line coverage, and for chachapoly.c only two lines are
not covered (error returns from lower-level module that should never happen
except perhaps if an alternative implementation returns an unexpected error).
diff --git a/library/chacha20.c b/library/chacha20.c
index d89000d..5a753eb 100644
--- a/library/chacha20.c
+++ b/library/chacha20.c
@@ -519,18 +519,29 @@
375U
};
+#define ASSERT( cond, args ) \
+ do \
+ { \
+ if( ! ( cond ) ) \
+ { \
+ if( verbose != 0 ) \
+ mbedtls_printf args; \
+ \
+ return( -1 ); \
+ } \
+ } \
+ while( 0 )
+
int mbedtls_chacha20_self_test( int verbose )
{
unsigned char output[381];
unsigned i;
int result;
- for ( i = 0U; i < 2U; i++ )
+ for( i = 0U; i < 2U; i++ )
{
- if ( verbose != 0 )
- {
+ if( verbose != 0 )
mbedtls_printf( " ChaCha20 test %u ", i );
- }
result = mbedtls_chacha20_crypt( test_keys[i],
test_nonces[i],
@@ -538,36 +549,18 @@
test_lengths[i],
test_input[i],
output );
- if ( result != 0)
- {
- if ( verbose != 0 )
- {
- mbedtls_printf( "error code: %i\n", result );
- }
- return( -1 );
- }
+ ASSERT( 0 == result, ( "error code: %i\n", result ) );
- if ( 0 != memcmp( output, test_output[i], test_lengths[i] ) )
- {
- if ( verbose != 0 )
- {
- mbedtls_printf( "failed\n" );
- }
+ ASSERT( 0 == memcmp( output, test_output[i], test_lengths[i] ),
+ ( "failed (output)\n" ) );
- return( -1 );
- }
-
- if ( verbose != 0 )
- {
+ if( verbose != 0 )
mbedtls_printf( "passed\n" );
- }
}
if( verbose != 0 )
- {
mbedtls_printf( "\n" );
- }
return( 0 );
}