Skip to content

Commit

Permalink
For tests, rename TEST_BUFFERS_EQUAL() to TEST_MEMORY_COMPARE()
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Cosgrove <[email protected]>
  • Loading branch information
tom-cosgrove-arm committed Sep 4, 2023
1 parent cd5a7c7 commit ba3b14d
Show file tree
Hide file tree
Showing 26 changed files with 224 additions and 224 deletions.
4 changes: 2 additions & 2 deletions tests/include/test/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
* \param size2 Size of the second buffer in bytes.
* This expression may be evaluated multiple times.
*/
#define TEST_BUFFERS_EQUAL(p1, size1, p2, size2) \
#define TEST_MEMORY_COMPARE(p1, size1, p2, size2) \
do { \
TEST_EQUAL((size1), (size2)); \
if ((size1) != 0) { \
Expand All @@ -195,7 +195,7 @@
} while (0)

/* For backwards compatibility */
#define ASSERT_COMPARE(p1, size1, p2, size2) TEST_BUFFERS_EQUAL(p1, size1, p2, size2)
#define ASSERT_COMPARE(p1, size1, p2, size2) TEST_MEMORY_COMPARE(p1, size1, p2, size2)

/**
* \brief This macro tests the expression passed to it and skips the
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_aes.function
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static int test_ctx_alignment(const data_t *key,
// Decrypt
TEST_ASSERT(mbedtls_aes_crypt_ecb(dec, MBEDTLS_AES_DECRYPT,
ciphertext, output) == 0);
TEST_BUFFERS_EQUAL(plaintext, 16, output, 16);
TEST_MEMORY_COMPARE(plaintext, 16, output, 16);

mbedtls_aes_free(dec);

Expand Down
16 changes: 8 additions & 8 deletions tests/suites/test_suite_aria.function
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void aria_encrypt_ecb(data_t *key_str, data_t *src_str,
output + i) == 0);
}

TEST_BUFFERS_EQUAL(output, expected_output->len,
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);
}

Expand Down Expand Up @@ -252,7 +252,7 @@ void aria_decrypt_ecb(data_t *key_str, data_t *src_str,
output + i) == 0);
}

TEST_BUFFERS_EQUAL(output, expected_output->len,
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);
}

Expand All @@ -277,7 +277,7 @@ void aria_encrypt_cbc(data_t *key_str, data_t *iv_str,
src_str->len, iv_str->x, src_str->x,
output) == cbc_result);
if (cbc_result == 0) {
TEST_BUFFERS_EQUAL(output, expected_output->len,
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);
}

Expand All @@ -302,7 +302,7 @@ void aria_decrypt_cbc(data_t *key_str, data_t *iv_str,
src_str->len, iv_str->x, src_str->x,
output) == cbc_result);
if (cbc_result == 0) {
TEST_BUFFERS_EQUAL(output, expected_output->len,
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);
}

Expand All @@ -329,7 +329,7 @@ void aria_encrypt_cfb128(data_t *key_str, data_t *iv_str,
iv_str->x, src_str->x, output)
== result);

TEST_BUFFERS_EQUAL(output, expected_output->len,
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);

exit:
Expand All @@ -355,7 +355,7 @@ void aria_decrypt_cfb128(data_t *key_str, data_t *iv_str,
iv_str->x, src_str->x, output)
== result);

TEST_BUFFERS_EQUAL(output, expected_output->len,
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);

exit:
Expand All @@ -381,7 +381,7 @@ void aria_encrypt_ctr(data_t *key_str, data_t *iv_str,
iv_str->x, blk, src_str->x, output)
== result);

TEST_BUFFERS_EQUAL(output, expected_output->len,
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);

exit:
Expand All @@ -407,7 +407,7 @@ void aria_decrypt_ctr(data_t *key_str, data_t *iv_str,
iv_str->x, blk, src_str->x, output)
== result);

TEST_BUFFERS_EQUAL(output, expected_output->len,
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);

exit:
Expand Down
12 changes: 6 additions & 6 deletions tests/suites/test_suite_asn1write.function
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int generic_write_finish_step(generic_write_data_t *data,
TEST_EQUAL(ret, data->end - data->p);
TEST_ASSERT(data->p >= data->start);
TEST_ASSERT(data->p <= data->end);
TEST_BUFFERS_EQUAL(data->p, (size_t) (data->end - data->p),
TEST_MEMORY_COMPARE(data->p, (size_t) (data->end - data->p),
expected->x, expected->len);
}
ok = 1;
Expand Down Expand Up @@ -322,7 +322,7 @@ void mbedtls_asn1_write_algorithm_identifier(data_t *oid,
TEST_EQUAL(mbedtls_asn1_get_alg(&p, end_complete,
&alg, &params), 0);
TEST_EQUAL(alg.tag, MBEDTLS_ASN1_OID);
TEST_BUFFERS_EQUAL(alg.p, alg.len, oid->x, oid->len);
TEST_MEMORY_COMPARE(alg.p, alg.len, oid->x, oid->len);
TEST_EQUAL(params.tag, expected_params_tag);
TEST_EQUAL(params.len, expected_params_len);
mbedtls_free(buf_complete);
Expand Down Expand Up @@ -440,7 +440,7 @@ void test_asn1_write_bitstrings(data_t *bitstring, int bits,
mbedtls_asn1_bitstring read = { 0, 0, NULL };
TEST_EQUAL(mbedtls_asn1_get_bitstring(&data.p, data.end,
&read), 0);
TEST_BUFFERS_EQUAL(read.p, read.len,
TEST_MEMORY_COMPARE(read.p, read.len,
masked_bitstring, byte_length);
TEST_EQUAL(read.unused_bits, 8 * byte_length - value_bits);
}
Expand Down Expand Up @@ -545,7 +545,7 @@ void store_named_data_val_found(int old_len, int new_len)
TEST_ASSERT(found == head);

if (new_val != NULL) {
TEST_BUFFERS_EQUAL(found->val.p, found->val.len,
TEST_MEMORY_COMPARE(found->val.p, found->val.len,
new_val, (size_t) new_len);
}
if (new_len == 0) {
Expand Down Expand Up @@ -580,14 +580,14 @@ void store_named_data_val_new(int new_len, int set_new_val)
TEST_ASSERT(found != NULL);
TEST_ASSERT(found == head);
TEST_ASSERT(found->oid.p != oid);
TEST_BUFFERS_EQUAL(found->oid.p, found->oid.len, oid, oid_len);
TEST_MEMORY_COMPARE(found->oid.p, found->oid.len, oid, oid_len);
if (new_len == 0) {
TEST_ASSERT(found->val.p == NULL);
} else if (new_val == NULL) {
TEST_ASSERT(found->val.p != NULL);
} else {
TEST_ASSERT(found->val.p != new_val);
TEST_BUFFERS_EQUAL(found->val.p, found->val.len,
TEST_MEMORY_COMPARE(found->val.p, found->val.len,
new_val, (size_t) new_len);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/suites/test_suite_chacha20.function
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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_BUFFERS_EQUAL(output, expected_output_str->len,
TEST_MEMORY_COMPARE(output, expected_output_str->len,
expected_output_str->x, expected_output_str->len);

/*
Expand All @@ -44,7 +44,7 @@ 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_BUFFERS_EQUAL(output, expected_output_str->len,
TEST_MEMORY_COMPARE(output, expected_output_str->len,
expected_output_str->x, expected_output_str->len);

/*
Expand All @@ -60,7 +60,7 @@ 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_BUFFERS_EQUAL(output, expected_output_str->len,
TEST_MEMORY_COMPARE(output, expected_output_str->len,
expected_output_str->x, expected_output_str->len);

mbedtls_chacha20_free(&ctx);
Expand Down
4 changes: 2 additions & 2 deletions tests/suites/test_suite_cipher.function
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
} else {
TEST_ASSERT(ret == 0);
TEST_BUFFERS_EQUAL(decrypt_buf, outlen, clear->x, clear->len);
TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
}

/* Free this, but keep cipher_plus_tag for deprecated function with PSA */
Expand Down Expand Up @@ -1390,7 +1390,7 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
} else {
/* authentic message: is the plaintext correct? */
TEST_ASSERT(ret == 0);
TEST_BUFFERS_EQUAL(decrypt_buf, outlen, clear->x, clear->len);
TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
}

mbedtls_free(decrypt_buf);
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_constant_time.function
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void ssl_cf_memcpy_offset(int offset_min, int offset_max, int len)
TEST_CF_PUBLIC(&secret, sizeof(secret));
TEST_CF_PUBLIC(dst, len);

TEST_BUFFERS_EQUAL(dst, len, src + secret, len);
TEST_MEMORY_COMPARE(dst, len, src + secret, len);
}

exit:
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_constant_time_hmac.function
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void ssl_cf_hmac(int hash)
TEST_EQUAL(0, mbedtls_md_hmac_reset(&ref_ctx));

/* Compare */
TEST_BUFFERS_EQUAL(out, out_len, ref_out, out_len);
TEST_MEMORY_COMPARE(out, out_len, ref_out, out_len);
}

mbedtls_free(data);
Expand Down
8 changes: 4 additions & 4 deletions tests/suites/test_suite_ecp.function
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ void ecp_muladd(int id,
&len, actual_result, sizeof(actual_result)));
TEST_ASSERT(len <= MBEDTLS_ECP_MAX_PT_LEN);

TEST_BUFFERS_EQUAL(expected_result->x, expected_result->len,
TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
actual_result, len);

exit:
Expand Down Expand Up @@ -1356,7 +1356,7 @@ void mbedtls_ecp_read_key(int grp_id, data_t *in_key, int expected, int canonica
ret = mbedtls_ecp_write_key(&key, buf, in_key->len);
TEST_ASSERT(ret == 0);

TEST_BUFFERS_EQUAL(in_key->x, in_key->len,
TEST_MEMORY_COMPARE(in_key->x, in_key->len,
buf, in_key->len);
} else {
unsigned char export1[MBEDTLS_ECP_MAX_BYTES];
Expand All @@ -1371,7 +1371,7 @@ void mbedtls_ecp_read_key(int grp_id, data_t *in_key, int expected, int canonica
ret = mbedtls_ecp_write_key(&key2, export2, in_key->len);
TEST_ASSERT(ret == 0);

TEST_BUFFERS_EQUAL(export1, in_key->len,
TEST_MEMORY_COMPARE(export1, in_key->len,
export2, in_key->len);
}
}
Expand Down Expand Up @@ -1448,7 +1448,7 @@ void genkey_mx_known_answer(int bits, data_t *seed, data_t *expected)
* (can be enforced by checking these bits).
* - Other bits must be random (by testing with different RNG outputs,
* we validate that those bits are indeed influenced by the RNG). */
TEST_BUFFERS_EQUAL(expected->x, expected->len,
TEST_MEMORY_COMPARE(expected->x, expected->len,
actual, expected->len);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/suites/test_suite_hkdf.function
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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_BUFFERS_EQUAL(okm, expected_okm->len,
TEST_MEMORY_COMPARE(okm, expected_okm->len,
expected_okm->x, expected_okm->len);
}
/* END_CASE */
Expand Down Expand Up @@ -53,7 +53,7 @@ void test_hkdf_extract(int md_alg, char *hex_ikm_string,
ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, output_prk);
TEST_ASSERT(ret == 0);

TEST_BUFFERS_EQUAL(output_prk, output_prk_len, prk, prk_len);
TEST_MEMORY_COMPARE(output_prk, output_prk_len, prk, prk_len);

exit:
mbedtls_free(ikm);
Expand Down Expand Up @@ -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_BUFFERS_EQUAL(output_okm, okm_len, okm, okm_len);
TEST_MEMORY_COMPARE(output_okm, okm_len, okm, okm_len);

exit:
mbedtls_free(info);
Expand Down
20 changes: 10 additions & 10 deletions tests/suites/test_suite_md.function
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void md_text(int md_type, char *text_src_string, data_t *hash)

TEST_EQUAL(0, mbedtls_md(md_info, src, src_len, output));

TEST_BUFFERS_EQUAL(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
}
/* END_CASE */

Expand All @@ -155,7 +155,7 @@ void md_hex(int md_type, data_t *src_str, data_t *hash)
TEST_EQUAL(0, mbedtls_md(md_info, src_str->x, src_str->len, output));


TEST_BUFFERS_EQUAL(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
}
/* END_CASE */

Expand Down Expand Up @@ -188,14 +188,14 @@ void md_text_multi(int md_type, char *text_src_string,

TEST_EQUAL(0, mbedtls_md_update(&ctx, src + halfway, src_len - halfway));
TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
TEST_BUFFERS_EQUAL(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);

/* Test clone */
memset(output, 0x00, sizeof(output));

TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway));
TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
TEST_BUFFERS_EQUAL(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);

exit:
mbedtls_md_free(&ctx);
Expand Down Expand Up @@ -228,14 +228,14 @@ void md_hex_multi(int md_type, data_t *src_str, data_t *hash)

TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
TEST_BUFFERS_EQUAL(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);

/* Test clone */
memset(output, 0x00, sizeof(output));

TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
TEST_BUFFERS_EQUAL(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);

exit:
mbedtls_md_free(&ctx);
Expand All @@ -258,7 +258,7 @@ void mbedtls_md_hmac(int md_type, int trunc_size,
TEST_EQUAL(0, mbedtls_md_hmac(md_info, key_str->x, key_str->len,
src_str->x, src_str->len, output));

TEST_BUFFERS_EQUAL(output, trunc_size, hash->x, hash->len);
TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len);
}
/* END_CASE */

Expand All @@ -285,7 +285,7 @@ void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));

TEST_BUFFERS_EQUAL(output, trunc_size, hash->x, hash->len);
TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len);

/* Test again, for reset() */
memset(output, 0x00, sizeof(output));
Expand All @@ -295,7 +295,7 @@ void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));

TEST_BUFFERS_EQUAL(output, trunc_size, hash->x, hash->len);
TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len);

exit:
mbedtls_md_free(&ctx);
Expand All @@ -314,6 +314,6 @@ void mbedtls_md_file(int md_type, char *filename,

TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output));

TEST_BUFFERS_EQUAL(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
}
/* END_CASE */
Loading

0 comments on commit ba3b14d

Please sign in to comment.