Skip to content

Commit

Permalink
For tests, rename TEST_CALLOC_OR_FAIL() to just TEST_CALLOC()
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 20e27de commit 30ceb23
Show file tree
Hide file tree
Showing 27 changed files with 150 additions and 150 deletions.
6 changes: 3 additions & 3 deletions tests/include/test/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
* This expression may be evaluated multiple times.
*
*/
#define TEST_CALLOC_OR_FAIL(pointer, length) \
#define TEST_CALLOC(pointer, length) \
do { \
TEST_ASSERT((pointer) == NULL); \
if ((length) != 0) { \
Expand All @@ -154,11 +154,11 @@
} while (0)

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

/** Allocate memory dynamically. If the allocation fails, skip the test case.
*
* This macro behaves like #TEST_CALLOC_OR_FAIL, 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 TEST_CALLOC_OR_SKIP(pointer, length) \
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);
TEST_CALLOC_OR_FAIL(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);
TEST_CALLOC_OR_FAIL(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));
TEST_CALLOC_OR_FAIL(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));
TEST_CALLOC_OR_FAIL(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));
TEST_CALLOC_OR_FAIL(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);
TEST_CALLOC_OR_FAIL(cert->ca_cert, 1);
TEST_CALLOC_OR_FAIL(cert->cert, 1);
TEST_CALLOC_OR_FAIL(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
16 changes: 8 additions & 8 deletions tests/suites/test_suite_aes.function
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,8 @@ void aes_ecb_context_alignment(data_t *key)
struct align1 *dec1 = NULL;

/* All peak alignment */
TEST_CALLOC_OR_FAIL(enc0, 1);
TEST_CALLOC_OR_FAIL(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 */
TEST_CALLOC_OR_FAIL(enc0, 1);
TEST_CALLOC_OR_FAIL(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 */
TEST_CALLOC_OR_FAIL(enc1, 1);
TEST_CALLOC_OR_FAIL(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 */
TEST_CALLOC_OR_FAIL(enc1, 1);
TEST_CALLOC_OR_FAIL(dec1, 1);
TEST_CALLOC(enc1, 1);
TEST_CALLOC(dec1, 1);
if (!test_ctx_alignment(key, &enc1->ctx, &dec1->ctx)) {
goto exit;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/suites/test_suite_asn1parse.function
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ 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) {
TEST_CALLOC_OR_FAIL(buf, 1);
TEST_CALLOC(buf, 1);
end = buf + 1;
p = end;
} else {
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. */
TEST_CALLOC_OR_FAIL(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);

TEST_CALLOC_OR_FAIL(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) {
TEST_CALLOC_OR_FAIL(head.oid.p, 1);
TEST_CALLOC(head.oid.p, 1);
}
if (with_val) {
TEST_CALLOC_OR_FAIL(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;
TEST_CALLOC_OR_FAIL(new, 1);
TEST_CALLOC(new, 1);
new->next = head;
head = new;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/suites/test_suite_asn1write.function
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int generic_write_start_step(generic_write_data_t *data)
mbedtls_test_set_step(data->size);
mbedtls_free(data->output);
data->output = NULL;
TEST_CALLOC_OR_FAIL(data->output, data->size == 0 ? 1 : data->size);
TEST_CALLOC(data->output, data->size == 0 ? 1 : data->size);
data->end = data->output + data->size;
data->p = data->end;
data->start = data->end - data->size;
Expand Down Expand Up @@ -296,7 +296,7 @@ void mbedtls_asn1_write_algorithm_identifier(data_t *oid,
size_t len_complete = data_len + par_len;
unsigned char expected_params_tag;
size_t expected_params_len;
TEST_CALLOC_OR_FAIL(buf_complete, len_complete);
TEST_CALLOC(buf_complete, len_complete);
unsigned char *end_complete = buf_complete + len_complete;
memcpy(buf_complete, data.p, data_len);
if (par_len == 0) {
Expand Down Expand Up @@ -404,7 +404,7 @@ void test_asn1_write_bitstrings(data_t *bitstring, int bits,
TEST_ASSERT(bitstring->len >= byte_length);

#if defined(MBEDTLS_ASN1_PARSE_C)
TEST_CALLOC_OR_FAIL(masked_bitstring, byte_length);
TEST_CALLOC(masked_bitstring, byte_length);
if (byte_length != 0) {
memcpy(masked_bitstring, bitstring->x, byte_length);
if (bits % 8 != 0) {
Expand Down Expand Up @@ -477,7 +477,7 @@ void store_named_data_find(data_t *oid0, data_t *oid1,
}
pointers[ARRAY_LENGTH(nd)] = NULL;
for (i = 0; i < ARRAY_LENGTH(nd); i++) {
TEST_CALLOC_OR_FAIL(nd[i].oid.p, oid[i]->len);
TEST_CALLOC(nd[i].oid.p, oid[i]->len);
memcpy(nd[i].oid.p, oid[i]->x, oid[i]->len);
nd[i].oid.len = oid[i]->len;
nd[i].next = pointers[i+1];
Expand Down Expand Up @@ -529,7 +529,7 @@ void store_named_data_val_found(int old_len, int new_len)
unsigned char *new_val = (unsigned char *) "new value";

if (old_len != 0) {
TEST_CALLOC_OR_FAIL(nd.val.p, (size_t) old_len);
TEST_CALLOC(nd.val.p, (size_t) old_len);
old_val = nd.val.p;
nd.val.len = old_len;
memset(old_val, 'x', old_len);
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_bignum.function
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ void mpi_random_many(int min, data_t *bound_bytes, int iterations)
full_stats = 0;
stats_len = n_bits;
}
TEST_CALLOC_OR_FAIL(stats, stats_len);
TEST_CALLOC(stats, stats_len);

for (i = 0; i < (size_t) iterations; i++) {
mbedtls_test_set_step(i);
Expand Down
18 changes: 9 additions & 9 deletions tests/suites/test_suite_cipher.function
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
* (we need the tag appended to the ciphertext)
*/
cipher_plus_tag_len = cipher->len + tag->len;
TEST_CALLOC_OR_FAIL(cipher_plus_tag, cipher_plus_tag_len);
TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len);
memcpy(cipher_plus_tag, cipher->x, cipher->len);
memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);

Expand All @@ -1247,7 +1247,7 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
* Try decrypting to a buffer that's 1B too small
*/
if (decrypt_buf_len != 0) {
TEST_CALLOC_OR_FAIL(decrypt_buf, decrypt_buf_len - 1);
TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1);

outlen = 0;
ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
Expand All @@ -1262,7 +1262,7 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
/*
* Authenticate and decrypt, and check result
*/
TEST_CALLOC_OR_FAIL(decrypt_buf, decrypt_buf_len);
TEST_CALLOC(decrypt_buf, decrypt_buf_len);

outlen = 0;
ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
Expand Down Expand Up @@ -1306,7 +1306,7 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
/*
* Try encrypting with an output buffer that's 1B too small
*/
TEST_CALLOC_OR_FAIL(encrypt_buf, encrypt_buf_len - 1);
TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1);

outlen = 0;
ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
Expand All @@ -1320,7 +1320,7 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
/*
* Encrypt and check the result
*/
TEST_CALLOC_OR_FAIL(encrypt_buf, encrypt_buf_len);
TEST_CALLOC(encrypt_buf, encrypt_buf_len);

outlen = 0;
ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
Expand Down Expand Up @@ -1374,7 +1374,7 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
* Authenticate and decrypt, and check result
*/

TEST_CALLOC_OR_FAIL(decrypt_buf, cipher->len);
TEST_CALLOC(decrypt_buf, cipher->len);
outlen = 0;
ret = mbedtls_cipher_auth_decrypt(&ctx, iv->x, iv->len, ad->x, ad->len,
tmp_cipher, cipher->len, decrypt_buf, &outlen,
Expand Down Expand Up @@ -1411,14 +1411,14 @@ void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
/* prepare buffers for encryption */
#if defined(MBEDTLS_USE_PSA_CRYPTO)
if (use_psa) {
TEST_CALLOC_OR_FAIL(cipher_plus_tag, cipher->len + tag->len);
TEST_CALLOC(cipher_plus_tag, cipher->len + tag->len);
tmp_cipher = cipher_plus_tag;
tmp_tag = cipher_plus_tag + cipher->len;
} else
#endif /* MBEDTLS_USE_PSA_CRYPTO */
{
TEST_CALLOC_OR_FAIL(encrypt_buf, cipher->len);
TEST_CALLOC_OR_FAIL(tag_buf, tag->len);
TEST_CALLOC(encrypt_buf, cipher->len);
TEST_CALLOC(tag_buf, tag->len);
tmp_cipher = encrypt_buf;
tmp_tag = tag_buf;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/suites/test_suite_constant_time.function
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void ssl_cf_memcpy_offset(int offset_min, int offset_max, int len)
size_t src_len = offset_max + len;
size_t secret;

TEST_CALLOC_OR_FAIL(dst, len);
TEST_CALLOC_OR_FAIL(src, src_len);
TEST_CALLOC(dst, len);
TEST_CALLOC(src, src_len);

/* Fill src in a way that we can detect if we copied the right bytes */
mbedtls_test_rnd_std_rand(NULL, src, src_len);
Expand Down
4 changes: 2 additions & 2 deletions tests/suites/test_suite_constant_time_hmac.function
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void ssl_cf_hmac(int hash)
block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;

/* Use allocated out buffer to catch overwrites */
TEST_CALLOC_OR_FAIL(out, out_len);
TEST_CALLOC(out, out_len);

/* Set up contexts with the given hash and a dummy key */
TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1));
Expand All @@ -54,7 +54,7 @@ void ssl_cf_hmac(int hash)
mbedtls_test_set_step(max_in_len * 10000);

/* Use allocated in buffer to catch overreads */
TEST_CALLOC_OR_FAIL(data, max_in_len);
TEST_CALLOC(data, max_in_len);

min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
for (in_len = min_in_len; in_len <= max_in_len; in_len++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_ecp.function
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ void genkey_mx_known_answer(int bits, data_t *seed, data_t *expected)
rnd_info.fallback_f_rng = NULL;
rnd_info.fallback_p_rng = NULL;

TEST_CALLOC_OR_FAIL(actual, expected->len);
TEST_CALLOC(actual, expected->len);

ret = mbedtls_ecp_gen_privkey_mx(bits, &d,
mbedtls_test_rnd_buffer_rand, &rnd_info);
Expand Down
8 changes: 4 additions & 4 deletions tests/suites/test_suite_mps.function
Original file line number Diff line number Diff line change
Expand Up @@ -844,15 +844,15 @@ void mbedtls_mps_reader_random_usage(int num_out_chunks,
mbedtls_mps_reader rd;

if (acc_size > 0) {
TEST_CALLOC_OR_FAIL(acc, acc_size);
TEST_CALLOC(acc, acc_size);
}

/* This probably needs to be changed because we want
* our tests to be deterministic. */
// srand( time( NULL ) );

TEST_CALLOC_OR_FAIL(outgoing, num_out_chunks * max_chunk_size);
TEST_CALLOC_OR_FAIL(incoming, num_out_chunks * max_chunk_size);
TEST_CALLOC(outgoing, num_out_chunks * max_chunk_size);
TEST_CALLOC(incoming, num_out_chunks * max_chunk_size);

mbedtls_mps_reader_init(&rd, acc, acc_size);

Expand Down Expand Up @@ -884,7 +884,7 @@ void mbedtls_mps_reader_random_usage(int num_out_chunks,
}

tmp_size = (rand() % max_chunk_size) + 1;
TEST_CALLOC_OR_FAIL(tmp, tmp_size);
TEST_CALLOC(tmp, tmp_size);

TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL, tmp, tmp_size) == 0);
ret = mbedtls_mps_reader_feed(&rd, tmp, tmp_size);
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_pk.function
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ void pk_sign_verify(int type, int parameter, int sign_ret, int verify_ret)
#endif

hash_len = mbedtls_md_get_size(mbedtls_md_info_from_type(md));
TEST_CALLOC_OR_FAIL(hash, hash_len);
TEST_CALLOC(hash, hash_len);

mbedtls_pk_init(&pk);
USE_PSA_INIT();
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_pkcs12.function
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void pkcs12_derive_key(int md_type, int key_size_arg,

salt_len = salt_arg->len;

TEST_CALLOC_OR_FAIL(output_data, key_size);
TEST_CALLOC(output_data, key_size);

int ret = mbedtls_pkcs12_derivation(output_data,
key_size,
Expand Down
Loading

0 comments on commit 30ceb23

Please sign in to comment.