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).
This commit is contained in:
Manuel Pégourié-Gonnard 2018-05-10 11:42:07 +02:00
parent 619b3092c2
commit 5350251977
3 changed files with 60 additions and 94 deletions

View file

@ -519,6 +519,19 @@ static const size_t test_lengths[2] =
375U 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 ) int mbedtls_chacha20_self_test( int verbose )
{ {
unsigned char output[381]; unsigned char output[381];
@ -528,9 +541,7 @@ int mbedtls_chacha20_self_test( int verbose )
for( i = 0U; i < 2U; i++ ) for( i = 0U; i < 2U; i++ )
{ {
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( " ChaCha20 test %u ", i ); mbedtls_printf( " ChaCha20 test %u ", i );
}
result = mbedtls_chacha20_crypt( test_keys[i], result = mbedtls_chacha20_crypt( test_keys[i],
test_nonces[i], test_nonces[i],
@ -538,36 +549,18 @@ int mbedtls_chacha20_self_test( int verbose )
test_lengths[i], test_lengths[i],
test_input[i], test_input[i],
output ); 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] ) ) ASSERT( 0 == memcmp( output, test_output[i], test_lengths[i] ),
{ ( "failed (output)\n" ) );
if ( verbose != 0 )
{
mbedtls_printf( "failed\n" );
}
return( -1 );
}
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
} }
}
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( "\n" ); mbedtls_printf( "\n" );
}
return( 0 ); return( 0 );
} }

View file

@ -452,6 +452,19 @@ static const unsigned char test_mac[1][16] =
} }
}; };
#define ASSERT( cond, args ) \
do \
{ \
if( ! ( cond ) ) \
{ \
if( verbose != 0 ) \
mbedtls_printf args; \
\
return( -1 ); \
} \
} \
while( 0 )
int mbedtls_chachapoly_self_test( int verbose ) int mbedtls_chachapoly_self_test( int verbose )
{ {
mbedtls_chachapoly_context ctx; mbedtls_chachapoly_context ctx;
@ -463,21 +476,12 @@ int mbedtls_chachapoly_self_test( int verbose )
for( i = 0U; i < 1U; i++ ) for( i = 0U; i < 1U; i++ )
{ {
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( " ChaCha20-Poly1305 test %u ", i ); mbedtls_printf( " ChaCha20-Poly1305 test %u ", i );
}
mbedtls_chachapoly_init( &ctx ); mbedtls_chachapoly_init( &ctx );
result = mbedtls_chachapoly_setkey( &ctx, test_key[i] ); result = mbedtls_chachapoly_setkey( &ctx, test_key[i] );
if ( result != 0 ) ASSERT( 0 == result, ( "setkey() error code: %i\n", result ) );
{
if ( verbose != 0 )
{
mbedtls_printf( "setkey() error code: %i\n", result );
}
return( -1 );
}
result = mbedtls_chachapoly_crypt_and_tag( &ctx, result = mbedtls_chachapoly_crypt_and_tag( &ctx,
MBEDTLS_CHACHAPOLY_ENCRYPT, MBEDTLS_CHACHAPOLY_ENCRYPT,
@ -488,45 +492,23 @@ int mbedtls_chachapoly_self_test( int verbose )
test_input[i], test_input[i],
output, output,
mac ); mac );
if ( result != 0 )
{
if ( verbose != 0 )
{
mbedtls_printf( "crypt_and_tag() error code: %i\n", result );
}
return( -1 );
}
if ( memcmp( output, test_output[i], test_input_len[i] ) != 0 ) ASSERT( 0 == result, ( "crypt_and_tag() error code: %i\n", result ) );
{
if ( verbose != 0 )
{
mbedtls_printf( "failure (wrong output)\n" );
}
return( -1 );
}
if ( memcmp( mac, test_mac[i], 16U ) != 0 ) ASSERT( 0 == memcmp( output, test_output[i], test_input_len[i] ),
{ ( "failure (wrong output)\n" ) );
if ( verbose != 0 )
{ ASSERT( 0 == memcmp( mac, test_mac[i], 16U ),
mbedtls_printf( "failure (wrong MAC)\n" ); ( "failure (wrong MAC)\n" ) );
}
return( -1 );
}
mbedtls_chachapoly_free( &ctx ); mbedtls_chachapoly_free( &ctx );
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
} }
}
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( "\n" ); mbedtls_printf( "\n" );
}
return( 0 ); return( 0 );
} }

View file

@ -487,6 +487,19 @@ static const unsigned char test_mac[2][16] =
} }
}; };
#define ASSERT( cond, args ) \
do \
{ \
if( ! ( cond ) ) \
{ \
if( verbose != 0 ) \
mbedtls_printf args; \
\
return( -1 ); \
} \
} \
while( 0 )
int mbedtls_poly1305_self_test( int verbose ) int mbedtls_poly1305_self_test( int verbose )
{ {
unsigned char mac[16]; unsigned char mac[16];
@ -496,44 +509,22 @@ int mbedtls_poly1305_self_test( int verbose )
for( i = 0U; i < 2U; i++ ) for( i = 0U; i < 2U; i++ )
{ {
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( " Poly1305 test %u ", i ); mbedtls_printf( " Poly1305 test %u ", i );
}
result = mbedtls_poly1305_mac( test_keys[i], result = mbedtls_poly1305_mac( test_keys[i],
test_data[i], test_data[i],
test_data_len[i], test_data_len[i],
mac ); mac );
if ( result != 0 ) ASSERT( 0 == result, ( "error code: %i\n", result ) );
{
if ( verbose != 0 )
{
mbedtls_printf( "error code: %i\n", result );
}
return( -1 ); ASSERT( 0 == memcmp( mac, test_mac[i], 16U ), ( "failed (mac)\n" ) );
}
if ( memcmp( mac, test_mac[i], 16U ) != 0 )
{
if ( verbose != 0 )
{
mbedtls_printf( "failed\n" );
}
return( -1 );
}
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
} }
}
if( verbose != 0 ) if( verbose != 0 )
{
mbedtls_printf( "\n" ); mbedtls_printf( "\n" );
}
return( 0 ); return( 0 );
} }