Skip to content

Commit

Permalink
Merge pull request #8158 from tom-cosgrove-arm/rename-assert_compare-…
Browse files Browse the repository at this point in the history
…to-test_assert_compare-2.28

Backport 2.28: Rename test macros ASSERT_COMPARE(), ASSERT_ALLOC() and ASSERT_ALLOC_WEAK()
  • Loading branch information
daverodgman authored Sep 5, 2023
2 parents 5c46332 + a240fe3 commit 06c466d
Show file tree
Hide file tree
Showing 35 changed files with 521 additions and 519 deletions.
80 changes: 41 additions & 39 deletions tests/include/test/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,52 +127,52 @@
* The allocated memory will be filled with zeros.
*
* You must set \p pointer to \c NULL before calling this macro and
* put `mbedtls_free( pointer )` in the test's cleanup code.
* put `mbedtls_free(pointer)` in the test's cleanup code.
*
* If \p length is zero, the resulting \p pointer will be \c NULL.
* If \p item_count is zero, the resulting \p pointer will be \c NULL.
* This is usually what we want in tests since API functions are
* supposed to accept null pointers when a buffer size is zero.
*
* This macro expands to an instruction, not an expression.
* It may jump to the \c exit label.
*
* \param pointer An lvalue where the address of the allocated buffer
* will be stored.
* This expression may be evaluated multiple times.
* \param length Number of elements to allocate.
* This expression may be evaluated multiple times.
* \param pointer An lvalue where the address of the allocated buffer
* will be stored.
* This expression may be evaluated multiple times.
* \param item_count Number of elements to allocate.
* This expression may be evaluated multiple times.
*
*/
#define ASSERT_ALLOC(pointer, length) \
do \
{ \
TEST_ASSERT((pointer) == NULL); \
if ((length) != 0) \
{ \
(pointer) = mbedtls_calloc(sizeof(*(pointer)), \
(length)); \
TEST_ASSERT((pointer) != NULL); \
} \
} \
while (0)
#define TEST_CALLOC(pointer, item_count) \
do { \
TEST_ASSERT((pointer) == NULL); \
if ((item_count) != 0) { \
(pointer) = mbedtls_calloc(sizeof(*(pointer)), \
(item_count)); \
TEST_ASSERT((pointer) != NULL); \
} \
} while (0)

/* For backwards compatibility */
#define ASSERT_ALLOC(pointer, item_count) TEST_CALLOC(pointer, item_count)

/** Allocate memory dynamically. If the allocation fails, skip the test case.
*
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
* This macro behaves like #TEST_CALLOC, except that if the allocation
* fails, it marks the test as skipped rather than failed.
*/
#define ASSERT_ALLOC_WEAK(pointer, length) \
do \
{ \
TEST_ASSERT((pointer) == NULL); \
if ((length) != 0) \
{ \
(pointer) = mbedtls_calloc(sizeof(*(pointer)), \
(length)); \
TEST_ASSUME((pointer) != NULL); \
} \
} \
while (0)
#define TEST_CALLOC_OR_SKIP(pointer, item_count) \
do { \
TEST_ASSERT((pointer) == NULL); \
if ((item_count) != 0) { \
(pointer) = mbedtls_calloc(sizeof(*(pointer)), \
(item_count)); \
TEST_ASSUME((pointer) != NULL); \
} \
} while (0)

/* For backwards compatibility */
#define ASSERT_ALLOC_WEAK(pointer, item_count) TEST_CALLOC_OR_SKIP(pointer, item_count)

/** Compare two buffers and fail the test case if they differ.
*
Expand All @@ -186,14 +186,16 @@
* \param size2 Size of the second buffer in bytes.
* This expression may be evaluated multiple times.
*/
#define ASSERT_COMPARE(p1, size1, p2, size2) \
do \
{ \
#define TEST_MEMORY_COMPARE(p1, size1, p2, size2) \
do { \
TEST_EQUAL((size1), (size2)); \
if ((size1) != 0) \
TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
} \
while (0)
if ((size1) != 0) { \
TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
} \
} while (0)

/* For backwards compatibility */
#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
10 changes: 5 additions & 5 deletions tests/src/psa_exercise_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ psa_status_t mbedtls_test_psa_key_agreement_with_self(
key_bits = psa_get_key_bits(&attributes);
public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type);
public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits);
ASSERT_ALLOC(public_key, public_key_length);
TEST_CALLOC(public_key, public_key_length);
PSA_ASSERT(psa_export_public_key(key, public_key, public_key_length,
&public_key_length));

Expand Down Expand Up @@ -547,7 +547,7 @@ psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
key_bits = psa_get_key_bits(&attributes);
public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type);
public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits);
ASSERT_ALLOC(public_key, public_key_length);
TEST_CALLOC(public_key, public_key_length);
PSA_ASSERT(psa_export_public_key(key,
public_key, public_key_length,
&public_key_length));
Expand Down Expand Up @@ -807,7 +807,7 @@ static int exercise_export_key(mbedtls_svc_key_id_t key,
exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
psa_get_key_type(&attributes),
psa_get_key_bits(&attributes));
ASSERT_ALLOC(exported, exported_size);
TEST_CALLOC(exported, exported_size);

if ((usage & PSA_KEY_USAGE_EXPORT) == 0 &&
!PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(&attributes))) {
Expand Down Expand Up @@ -850,7 +850,7 @@ static int exercise_export_public_key(mbedtls_svc_key_id_t key)
exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
psa_get_key_type(&attributes),
psa_get_key_bits(&attributes));
ASSERT_ALLOC(exported, exported_size);
TEST_CALLOC(exported, exported_size);

TEST_EQUAL(psa_export_public_key(key, exported,
exported_size, &exported_length),
Expand All @@ -863,7 +863,7 @@ static int exercise_export_public_key(mbedtls_svc_key_id_t key)
psa_get_key_type(&attributes));
exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type,
psa_get_key_bits(&attributes));
ASSERT_ALLOC(exported, exported_size);
TEST_CALLOC(exported, exported_size);

PSA_ASSERT(psa_export_public_key(key,
exported, exported_size,
Expand Down
6 changes: 3 additions & 3 deletions tests/src/test_helpers/ssl_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,9 @@ int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
}

cert = &(ep->cert);
ASSERT_ALLOC(cert->ca_cert, 1);
ASSERT_ALLOC(cert->cert, 1);
ASSERT_ALLOC(cert->pkey, 1);
TEST_CALLOC(cert->ca_cert, 1);
TEST_CALLOC(cert->cert, 1);
TEST_CALLOC(cert->pkey, 1);

mbedtls_x509_crt_init(cert->ca_cert);
mbedtls_x509_crt_init(cert->cert);
Expand Down
18 changes: 9 additions & 9 deletions 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);
ASSERT_COMPARE(plaintext, 16, output, 16);
TEST_MEMORY_COMPARE(plaintext, 16, output, 16);

mbedtls_aes_free(dec);

Expand Down Expand Up @@ -688,8 +688,8 @@ void aes_ecb_context_alignment(data_t *key)
struct align1 *dec1 = NULL;

/* All peak alignment */
ASSERT_ALLOC(enc0, 1);
ASSERT_ALLOC(dec0, 1);
TEST_CALLOC(enc0, 1);
TEST_CALLOC(dec0, 1);
if (!test_ctx_alignment(key, &enc0->ctx, &dec0->ctx)) {
goto exit;
}
Expand All @@ -699,8 +699,8 @@ void aes_ecb_context_alignment(data_t *key)
dec0 = NULL;

/* Enc aligned, dec not */
ASSERT_ALLOC(enc0, 1);
ASSERT_ALLOC(dec1, 1);
TEST_CALLOC(enc0, 1);
TEST_CALLOC(dec1, 1);
if (!test_ctx_alignment(key, &enc0->ctx, &dec1->ctx)) {
goto exit;
}
Expand All @@ -710,8 +710,8 @@ void aes_ecb_context_alignment(data_t *key)
dec1 = NULL;

/* Dec aligned, enc not */
ASSERT_ALLOC(enc1, 1);
ASSERT_ALLOC(dec0, 1);
TEST_CALLOC(enc1, 1);
TEST_CALLOC(dec0, 1);
if (!test_ctx_alignment(key, &enc1->ctx, &dec0->ctx)) {
goto exit;
}
Expand All @@ -721,8 +721,8 @@ void aes_ecb_context_alignment(data_t *key)
dec0 = NULL;

/* Both shifted */
ASSERT_ALLOC(enc1, 1);
ASSERT_ALLOC(dec1, 1);
TEST_CALLOC(enc1, 1);
TEST_CALLOC(dec1, 1);
if (!test_ctx_alignment(key, &enc1->ctx, &dec1->ctx)) {
goto exit;
}
Expand Down
32 changes: 16 additions & 16 deletions tests/suites/test_suite_aria.function
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ void aria_encrypt_ecb(data_t *key_str, data_t *src_str,
output + i) == 0);
}

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

exit:
Expand All @@ -252,8 +252,8 @@ void aria_decrypt_ecb(data_t *key_str, data_t *src_str,
output + i) == 0);
}

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

exit:
Expand All @@ -277,8 +277,8 @@ 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) {
ASSERT_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);
}

exit:
Expand All @@ -302,8 +302,8 @@ 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) {
ASSERT_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);
TEST_MEMORY_COMPARE(output, expected_output->len,
expected_output->x, expected_output->len);
}

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

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

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

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

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

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

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

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

exit:
mbedtls_aria_free(&ctx);
Expand Down
14 changes: 7 additions & 7 deletions tests/suites/test_suite_asn1parse.function
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ int get_len_step(const data_t *input, size_t buffer_size,
/* Allocate a new buffer of exactly the length to parse each time.
* This gives memory sanitizers a chance to catch buffer overreads. */
if (buffer_size == 0) {
ASSERT_ALLOC(buf, 1);
TEST_CALLOC(buf, 1);
end = buf + 1;
p = end;
} else {
ASSERT_ALLOC_WEAK(buf, buffer_size);
TEST_CALLOC_OR_SKIP(buf, buffer_size);
if (buffer_size > input->len) {
memcpy(buf, input->x, input->len);
memset(buf + input->len, 'A', buffer_size - input->len);
Expand Down Expand Up @@ -247,7 +247,7 @@ void parse_prefixes(const data_t *input,
mbedtls_test_set_step(buffer_size);
/* Allocate a new buffer of exactly the length to parse each time.
* This gives memory sanitizers a chance to catch buffer overreads. */
ASSERT_ALLOC(buf, buffer_size);
TEST_CALLOC(buf, buffer_size);
memcpy(buf, input->x, buffer_size);
p = buf;
ret = nested_parse(&p, buf + buffer_size);
Expand Down Expand Up @@ -506,7 +506,7 @@ void get_mpi_too_large()

mbedtls_mpi_init(&actual_mpi);

ASSERT_ALLOC(buf, size);
TEST_CALLOC(buf, size);
buf[0] = 0x02; /* tag: INTEGER */
buf[1] = 0x84; /* 4-octet length */
buf[2] = (too_many_octets >> 24) & 0xff;
Expand Down Expand Up @@ -729,10 +729,10 @@ void free_named_data(int with_oid, int with_val, int with_next)
{ { 0x06, 0, NULL }, { 0, 0, NULL }, NULL, 0 };

if (with_oid) {
ASSERT_ALLOC(head.oid.p, 1);
TEST_CALLOC(head.oid.p, 1);
}
if (with_val) {
ASSERT_ALLOC(head.val.p, 1);
TEST_CALLOC(head.val.p, 1);
}
if (with_next) {
head.next = &next;
Expand All @@ -758,7 +758,7 @@ void free_named_data_list(int length)

for (i = 0; i < length; i++) {
mbedtls_asn1_named_data *new = NULL;
ASSERT_ALLOC(new, 1);
TEST_CALLOC(new, 1);
new->next = head;
head = new;
}
Expand Down
Loading

0 comments on commit 06c466d

Please sign in to comment.