From 8e8898d40ff25042dbc5db8f8eb7874a381dce6a Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Thu, 30 Jul 2020 14:18:02 +0200 Subject: [PATCH] tests: Replace "TEST_ASSERT(!memcmp ...)" by ASSERT_COMPARE The usage of "!memcmp()" is at least not recommended and better to use the macro dedicated for buffer comparisons. Signed-off-by: Ronald Cron --- tests/suites/helpers.function | 21 +++++++++++++++++ tests/suites/test_suite_aria.function | 28 +++++++++++++---------- tests/suites/test_suite_chacha20.function | 12 +++++----- tests/suites/test_suite_hkdf.function | 8 +++---- tests/suites/test_suite_poly1305.function | 12 ++++++---- 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index 8e7f60d0a..f30f5e664 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -108,6 +108,27 @@ typedef enum } \ } while( 0 ) +/** Compare two buffers and fail the test case if they differ. + * + * This macro expands to an instruction, not an expression. + * It may jump to the \c exit label. + * + * \param p1 Pointer to the start of the first buffer. + * \param size1 Size of the first buffer in bytes. + * This expression may be evaluated multiple times. + * \param p2 Pointer to the start of the second buffer. + * \param size2 Size of the second buffer in bytes. + * This expression may be evaluated multiple times. + */ +#define ASSERT_COMPARE( p1, size1, p2, size2 ) \ + do \ + { \ + TEST_ASSERT( ( size1 ) == ( size2 ) ); \ + if( ( size1 ) != 0 ) \ + TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \ + } \ + while( 0 ) + /** * \brief This macro tests the expression passed to it and skips the * running test if it doesn't evaluate to 'true'. diff --git a/tests/suites/test_suite_aria.function b/tests/suites/test_suite_aria.function index 3cc683bf0..6d6a20335 100644 --- a/tests/suites/test_suite_aria.function +++ b/tests/suites/test_suite_aria.function @@ -226,8 +226,8 @@ void aria_encrypt_ecb( data_t *key_str, data_t *src_str, output + i ) == 0 ); } - TEST_ASSERT( !memcmp( output, - expected_output->x, expected_output->len) ); + ASSERT_COMPARE( output, expected_output->len, + expected_output->x, expected_output->len ); } exit: @@ -256,8 +256,8 @@ void aria_decrypt_ecb( data_t *key_str, data_t *src_str, output + i ) == 0 ); } - TEST_ASSERT( !memcmp( output, - expected_output->x, expected_output->len) ); + ASSERT_COMPARE( output, expected_output->len, + expected_output->x, expected_output->len ); } exit: @@ -282,8 +282,8 @@ void aria_encrypt_cbc( data_t *key_str, data_t *iv_str, output ) == cbc_result ); if( cbc_result == 0 ) { - TEST_ASSERT( !memcmp( output, - expected_output->x, expected_output->len) ); + ASSERT_COMPARE( output, expected_output->len, + expected_output->x, expected_output->len ); } exit: @@ -308,8 +308,8 @@ void aria_decrypt_cbc( data_t *key_str, data_t *iv_str, output ) == cbc_result ); if( cbc_result == 0 ) { - TEST_ASSERT( !memcmp( output, - expected_output->x, expected_output->len) ); + ASSERT_COMPARE( output, expected_output->len, + expected_output->x, expected_output->len ); } exit: @@ -335,7 +335,8 @@ void aria_encrypt_cfb128( data_t *key_str, data_t *iv_str, iv_str->x, src_str->x, output ) == result ); - TEST_ASSERT( !memcmp( output, expected_output->x, expected_output->len) ); + ASSERT_COMPARE( output, expected_output->len, + expected_output->x, expected_output->len ); exit: mbedtls_aria_free( &ctx ); @@ -360,7 +361,8 @@ void aria_decrypt_cfb128( data_t *key_str, data_t *iv_str, iv_str->x, src_str->x, output ) == result ); - TEST_ASSERT( !memcmp( output, expected_output->x, expected_output->len) ); + ASSERT_COMPARE( output, expected_output->len, + expected_output->x, expected_output->len ); exit: mbedtls_aria_free( &ctx ); @@ -385,7 +387,8 @@ void aria_encrypt_ctr( data_t *key_str, data_t *iv_str, iv_str->x, blk, src_str->x, output ) == result ); - TEST_ASSERT( !memcmp( output, expected_output->x, expected_output->len) ); + ASSERT_COMPARE( output, expected_output->len, + expected_output->x, expected_output->len ); exit: mbedtls_aria_free( &ctx ); @@ -410,7 +413,8 @@ void aria_decrypt_ctr( data_t *key_str, data_t *iv_str, iv_str->x, blk, src_str->x, output ) == result ); - TEST_ASSERT( !memcmp( output, expected_output->x, expected_output->len) ); + ASSERT_COMPARE( output, expected_output->len, + expected_output->x, expected_output->len ); exit: mbedtls_aria_free( &ctx ); diff --git a/tests/suites/test_suite_chacha20.function b/tests/suites/test_suite_chacha20.function index 968a9e9b9..67c8de2ec 100644 --- a/tests/suites/test_suite_chacha20.function +++ b/tests/suites/test_suite_chacha20.function @@ -28,8 +28,8 @@ void chacha20_crypt( data_t *key_str, */ TEST_ASSERT( mbedtls_chacha20_crypt( key_str->x, nonce_str->x, counter, src_str->len, src_str->x, output ) == 0 ); - TEST_ASSERT( !memcmp( output, expected_output_str->x, - expected_output_str->len ) ); + ASSERT_COMPARE( output, expected_output_str->len, + expected_output_str->x, expected_output_str->len ); /* * Test the streaming API @@ -43,8 +43,8 @@ void chacha20_crypt( data_t *key_str, memset( output, 0x00, sizeof( output ) ); TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_str->len, src_str->x, output ) == 0 ); - TEST_ASSERT( !memcmp( output, expected_output_str->x, - expected_output_str->len ) ); + ASSERT_COMPARE( output, expected_output_str->len, + expected_output_str->x, expected_output_str->len ); /* * Test the streaming API again, piecewise @@ -59,8 +59,8 @@ void chacha20_crypt( data_t *key_str, TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_str->len - 1, src_str->x + 1, output + 1 ) == 0 ); - TEST_ASSERT( !memcmp( output, expected_output_str->x, - expected_output_str->len ) ); + ASSERT_COMPARE( output, expected_output_str->len, + expected_output_str->x, expected_output_str->len ); mbedtls_chacha20_free( &ctx ); } diff --git a/tests/suites/test_suite_hkdf.function b/tests/suites/test_suite_hkdf.function index eca10afd9..af871e1b3 100644 --- a/tests/suites/test_suite_hkdf.function +++ b/tests/suites/test_suite_hkdf.function @@ -24,7 +24,8 @@ void test_hkdf( int md_alg, data_t *ikm, data_t *salt, data_t *info, info->x, info->len, okm, expected_okm->len ); TEST_ASSERT( ret == 0 ); - TEST_ASSERT( !memcmp( okm, expected_okm->x, expected_okm->len ) ); + ASSERT_COMPARE( okm , expected_okm->len, + expected_okm->x, expected_okm->len ); } /* END_CASE */ @@ -48,12 +49,11 @@ void test_hkdf_extract( int md_alg, char *hex_ikm_string, ikm = unhexify_alloc( hex_ikm_string, &ikm_len ); salt = unhexify_alloc( hex_salt_string, &salt_len ); prk = unhexify_alloc( hex_prk_string, &prk_len ); - TEST_ASSERT( prk_len == output_prk_len ); ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, output_prk ); TEST_ASSERT( ret == 0 ); - TEST_ASSERT( !memcmp( output_prk, prk, prk_len ) ); + ASSERT_COMPARE( output_prk, output_prk_len, prk, prk_len ); exit: mbedtls_free(ikm); @@ -89,7 +89,7 @@ void test_hkdf_expand( int md_alg, char *hex_info_string, ret = mbedtls_hkdf_expand( md, prk, prk_len, info, info_len, output_okm, OKM_LEN ); TEST_ASSERT( ret == 0 ); - TEST_ASSERT( !memcmp( output_okm, okm, okm_len ) ); + ASSERT_COMPARE( output_okm, okm_len, okm, okm_len ); exit: mbedtls_free(info); diff --git a/tests/suites/test_suite_poly1305.function b/tests/suites/test_suite_poly1305.function index 0fc1f6058..4b8995b60 100644 --- a/tests/suites/test_suite_poly1305.function +++ b/tests/suites/test_suite_poly1305.function @@ -22,7 +22,8 @@ void mbedtls_poly1305( data_t *key, data_t *expected_mac, data_t *src_str ) TEST_ASSERT( mbedtls_poly1305_mac( key->x, src_str->x, src_str->len, mac ) == 0 ); - TEST_ASSERT( !memcmp( mac, expected_mac->x, expected_mac->len ) ); + ASSERT_COMPARE( mac, expected_mac->len, + expected_mac->x, expected_mac->len ); /* * Test the streaming API @@ -35,7 +36,8 @@ void mbedtls_poly1305( data_t *key, data_t *expected_mac, data_t *src_str ) TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); - TEST_ASSERT( !memcmp( mac, expected_mac->x, expected_mac->len ) ); + ASSERT_COMPARE( mac, expected_mac->len, + expected_mac->x, expected_mac->len ); /* * Test the streaming API again, piecewise @@ -52,7 +54,8 @@ void mbedtls_poly1305( data_t *key, data_t *expected_mac, data_t *src_str ) TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); - TEST_ASSERT( !memcmp( mac, expected_mac->x, expected_mac->len ) ); + ASSERT_COMPARE( mac, expected_mac->len, + expected_mac->x, expected_mac->len ); } /* @@ -68,7 +71,8 @@ void mbedtls_poly1305( data_t *key, data_t *expected_mac, data_t *src_str ) TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); - TEST_ASSERT( !memcmp( mac, expected_mac->x, expected_mac->len ) ); + ASSERT_COMPARE( mac, expected_mac->len, + expected_mac->x, expected_mac->len ); } mbedtls_poly1305_free( &ctx );