Improve test failure reporting

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-11-27 21:30:58 +01:00
parent 290f01b3f5
commit 2336555444

View file

@ -26,10 +26,10 @@ void pkcs7_parse( char *pkcs7_file, int res_expect )
mbedtls_pkcs7_init( &pkcs7 );
res = mbedtls_pk_load_file( pkcs7_file, &pkcs7_buf, &buflen );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
res = mbedtls_pkcs7_parse_der( &pkcs7, pkcs7_buf, buflen );
TEST_ASSERT( res == res_expect );
TEST_EQUAL( res, res_expect );
exit:
mbedtls_free( pkcs7_buf );
@ -60,16 +60,16 @@ void pkcs7_verify( char *pkcs7_file, char *crt, char *filetobesigned, int do_has
mbedtls_x509_crt_init( &x509 );
res = mbedtls_x509_crt_parse_file( &x509, crt );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
res = mbedtls_pk_load_file( pkcs7_file, &pkcs7_buf, &buflen );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
res = mbedtls_pkcs7_parse_der( &pkcs7, pkcs7_buf, buflen );
TEST_ASSERT( res == MBEDTLS_PKCS7_SIGNED_DATA );
TEST_EQUAL( res, MBEDTLS_PKCS7_SIGNED_DATA );
res = stat( filetobesigned, &st );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
file = fopen( filetobesigned, "rb" );
TEST_ASSERT( file != NULL );
@ -79,18 +79,18 @@ void pkcs7_verify( char *pkcs7_file, char *crt, char *filetobesigned, int do_has
TEST_ASSERT( data != NULL );
buflen = fread( (void *)data , sizeof( unsigned char ), datalen, file );
TEST_ASSERT( buflen == datalen );
TEST_EQUAL( buflen, datalen );
fclose( file );
if( do_hash_alg )
{
res = mbedtls_oid_get_md_alg( &pkcs7.signed_data.digest_alg_identifiers, &md_alg );
TEST_ASSERT( res == 0 );
TEST_ASSERT( md_alg == (mbedtls_md_type_t) do_hash_alg );
TEST_EQUAL( res, 0 );
TEST_EQUAL( md_alg, (mbedtls_md_type_t) do_hash_alg );
md_info = mbedtls_md_info_from_type( md_alg );
res = mbedtls_md( md_info, data, datalen, hash );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
res = mbedtls_pkcs7_signed_hash_verify( &pkcs7, &x509, hash, sizeof(hash) );
}
@ -98,7 +98,7 @@ void pkcs7_verify( char *pkcs7_file, char *crt, char *filetobesigned, int do_has
{
res = mbedtls_pkcs7_signed_data_verify( &pkcs7, &x509, data, datalen );
}
TEST_ASSERT( res == res_expect );
TEST_EQUAL( res, res_expect );
exit:
mbedtls_x509_crt_free( &x509 );
@ -134,21 +134,21 @@ void pkcs7_verify_multiple_signers( char *pkcs7_file, char *crt1, char *crt2, ch
mbedtls_x509_crt_init( &x509_2 );
res = mbedtls_pk_load_file( pkcs7_file, &pkcs7_buf, &buflen );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
res = mbedtls_pkcs7_parse_der( &pkcs7, pkcs7_buf, buflen );
TEST_ASSERT( res == MBEDTLS_PKCS7_SIGNED_DATA );
TEST_EQUAL( res, MBEDTLS_PKCS7_SIGNED_DATA );
TEST_ASSERT( pkcs7.signed_data.no_of_signers == 2 );
TEST_EQUAL( pkcs7.signed_data.no_of_signers, 2 );
res = mbedtls_x509_crt_parse_file( &x509_1, crt1 );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
res = mbedtls_x509_crt_parse_file( &x509_2, crt2 );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
res = stat( filetobesigned, &st );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
file = fopen( filetobesigned, "rb" );
TEST_ASSERT( file != NULL );
@ -156,32 +156,32 @@ void pkcs7_verify_multiple_signers( char *pkcs7_file, char *crt1, char *crt2, ch
datalen = st.st_size;
ASSERT_ALLOC( data, datalen );
buflen = fread( ( void * )data , sizeof( unsigned char ), datalen, file );
TEST_ASSERT( buflen == datalen );
TEST_EQUAL( buflen, datalen );
fclose( file );
if( do_hash_alg )
{
res = mbedtls_oid_get_md_alg( &pkcs7.signed_data.digest_alg_identifiers, &md_alg );
TEST_ASSERT( res == 0 );
TEST_ASSERT( md_alg == MBEDTLS_MD_SHA256 );
TEST_EQUAL( res, 0 );
TEST_EQUAL( md_alg, MBEDTLS_MD_SHA256 );
md_info = mbedtls_md_info_from_type( md_alg );
res = mbedtls_md( md_info, data, datalen, hash );
TEST_ASSERT( res == 0 );
TEST_EQUAL( res, 0 );
res = mbedtls_pkcs7_signed_hash_verify( &pkcs7, &x509_1, hash, sizeof(hash) );
TEST_ASSERT( res == res_expect );
TEST_EQUAL( res, res_expect );
}
else
{
res = mbedtls_pkcs7_signed_data_verify( &pkcs7, &x509_1, data, datalen );
TEST_ASSERT( res == res_expect );
TEST_EQUAL( res, res_expect );
}
res = mbedtls_pkcs7_signed_data_verify( &pkcs7, &x509_2, data, datalen );
TEST_ASSERT( res == res_expect );
TEST_EQUAL( res, res_expect );
exit:
mbedtls_x509_crt_free( &x509_1 );