From 98895a96d202e328f5410d8e726d03406320a57f Mon Sep 17 00:00:00 2001 From: Loup Vaillant Date: Wed, 28 Dec 2022 08:50:25 +0100 Subject: [PATCH] Use ASSERT everywhere --- tests/test.c | 50 +++++++++----------------------------------- tests/tis-ci.c | 56 +++++++++++++++++++++++--------------------------- tests/utils.c | 19 +++++++---------- 3 files changed, 43 insertions(+), 82 deletions(-) diff --git a/tests/test.c b/tests/test.c index 53c578c..bce5d50 100644 --- a/tests/test.c +++ b/tests/test.c @@ -115,10 +115,7 @@ static void chacha20(vector_reader *reader) u64 nb_blocks = plain.size / 64 + (plain.size % 64 != 0); u64 new_ctr = crypto_chacha20_ctr(out.buf, plain.buf, plain.size, key.buf, nonce.buf, ctr); - if (new_ctr - ctr != nb_blocks) { - printf("FAILURE: Chacha20 returned counter not correct: "); - exit(1); - } + ASSERT(new_ctr - ctr == nb_blocks); } static void ietf_chacha20(vector_reader *reader) @@ -131,10 +128,7 @@ static void ietf_chacha20(vector_reader *reader) u32 nb_blocks = (u32)(plain.size / 64 + (plain.size % 64 != 0)); u32 new_ctr = crypto_ietf_chacha20_ctr(out.buf, plain.buf, plain.size, key.buf, nonce.buf, ctr); - if (new_ctr - ctr != nb_blocks) { - printf("FAILURE: IETF Chacha20 returned counter not correct: "); - exit(1); - } + ASSERT(new_ctr - ctr == nb_blocks); } static void xchacha20(vector_reader *reader) @@ -147,10 +141,7 @@ static void xchacha20(vector_reader *reader) u64 nb_blocks = plain.size / 64 + (plain.size % 64 != 0); u64 new_ctr = crypto_xchacha20_ctr(out.buf, plain.buf, plain.size, key.buf, nonce.buf, ctr); - if (new_ctr - ctr != nb_blocks) { - printf("FAILURE: XChacha20 returned counter not correct: "); - exit(1); - } + ASSERT(new_ctr - ctr == nb_blocks); } static void hchacha20(vector_reader *reader) @@ -754,18 +745,9 @@ static void edDSA_pk(vector_reader *reader) memcpy(out.buf, public_key, 32); u8 zeroes[32] = {0}; - if (memcmp(seed, zeroes, 32)) { - printf("FAILURE: seed has not been wiped\n"); - exit(1); - } - if (memcmp(secret_key, in.buf, 32)) { - printf("FAILURE: first half of secret key is not the seed\n"); - exit(1); - } - if (memcmp(secret_key + 32, public_key, 32)) { - printf("FAILURE: second half of secret key is not the public key\n"); - exit(1); - } + ASSERT(memcmp(seed , zeroes , 32) == 0); + ASSERT(memcmp(secret_key , in.buf , 32) == 0); + ASSERT(memcmp(secret_key + 32, public_key, 32) == 0); } static void test_edDSA() @@ -864,18 +846,9 @@ static void ed_25519_pk(vector_reader *reader) memcpy(out.buf, public_key, 32); u8 zeroes[32] = {0}; - if (memcmp(seed, zeroes, 32)) { - printf("FAILURE: seed has not been wiped\n"); - exit(1); - } - if (memcmp(secret_key, in.buf, 32)) { - printf("FAILURE: first half of secret key is not the seed\n"); - exit(1); - } - if (memcmp(secret_key + 32, public_key, 32)) { - printf("FAILURE: second half of secret key is not the public key\n"); - exit(1); - } + ASSERT(memcmp(seed , zeroes , 32) == 0); + ASSERT(memcmp(secret_key , in.buf , 32) == 0); + ASSERT(memcmp(secret_key + 32, public_key, 32) == 0); } static void ed_25519_check(vector_reader *reader) @@ -912,10 +885,7 @@ static void elligator_inv(vector_reader *reader) u8 failure = next_input(reader).buf[0]; vector out = next_output(reader); int check = crypto_curve_to_hidden(out.buf, point.buf, tweak); - if ((u8)check != failure) { - printf("Elligator inverse map: failure mismatch\n"); - exit(1); - } + ASSERT((u8)check == failure); } static void test_elligator() diff --git a/tests/tis-ci.c b/tests/tis-ci.c index ce9dc94..053d6c2 100644 --- a/tests/tis-ci.c +++ b/tests/tis-ci.c @@ -239,10 +239,7 @@ static void elligator_inv(vector_reader *reader) u8 failure = next_input(reader).buf[0]; vector out = next_output(reader); int check = crypto_curve_to_hidden(out.buf, point.buf, tweak); - if ((u8)check != failure) { - printf("Elligator inverse map: failure mismatch\n"); - exit(1); - } + ASSERT((u8)check == failure); if (check) { out.buf[0] = 0; } @@ -410,30 +407,29 @@ TEST(elligator_inv) //@ ensures \result == 0; int main(void) { - int status = 0; - status |= v_chacha20 (); - status |= v_ietf_chacha20 (); - status |= v_hchacha20 (); - status |= v_xchacha20 (); - status |= v_poly1305 (); - status |= v_aead_ietf (); - status |= v_blake2b (); - status |= v_sha512 (); - status |= v_hmac_sha512 (); - status |= v_argon2i (); - status |= v_x25519 (); - status |= v_edDSA (); - status |= v_ed_25519 (); - status |= v_ed_25519_check(); - status |= v_elligator_dir (); - status |= v_elligator_inv (); - - status |= p_from_eddsa (); - status |= p_from_ed25519 (); - status |= p_dirty (); - status |= p_x25519_inverse(); - status |= p_verify16 (); - status |= p_verify32 (); - status |= p_verify64 (); - return status; + ASSERT(v_chacha20 () == 0); + ASSERT(v_ietf_chacha20 () == 0); + ASSERT(v_hchacha20 () == 0); + ASSERT(v_xchacha20 () == 0); + ASSERT(v_poly1305 () == 0); + ASSERT(v_aead_ietf () == 0); + ASSERT(v_blake2b () == 0); + ASSERT(v_sha512 () == 0); + ASSERT(v_hmac_sha512 () == 0); + ASSERT(v_argon2i () == 0); + ASSERT(v_x25519 () == 0); + ASSERT(v_edDSA () == 0); + ASSERT(v_ed_25519 () == 0); + ASSERT(v_ed_25519_check() == 0); + ASSERT(v_elligator_dir () == 0); + ASSERT(v_elligator_inv () == 0); + + ASSERT(p_from_eddsa () == 0); + ASSERT(p_from_ed25519 () == 0); + ASSERT(p_dirty () == 0); + ASSERT(p_x25519_inverse() == 0); + ASSERT(p_verify16 () == 0); + ASSERT(p_verify32 () == 0); + ASSERT(p_verify64 () == 0); + return 0; } diff --git a/tests/utils.c b/tests/utils.c index f7235f0..e33ca99 100644 --- a/tests/utils.c +++ b/tests/utils.c @@ -125,10 +125,7 @@ void* alloc(size_t size) return NULL; } void *buf = malloc(size); - if (buf == NULL) { - fprintf(stderr, "Allocation failed: 0x%zx bytes\n", size); - exit(1); - } + ASSERT(buf != NULL); return buf; } @@ -154,10 +151,9 @@ static vector vector_of_string(const char *s) vector next_input(vector_reader *reader) { - if (reader->size == 0 || reader->nb_inputs >= 10) { - fprintf(stderr, "Input reader overload (no more vectors)\n"); - exit(1); - } + ASSERT(reader->size > 0); + ASSERT(reader->nb_inputs < 10); + const char *next = *(reader->next); vector *input = reader->inputs + reader->nb_inputs; reader->next++; @@ -169,10 +165,9 @@ vector next_input(vector_reader *reader) vector next_output(vector_reader *reader) { - if (reader->size == 0 || reader->nb_inputs >= 10) { - fprintf(stderr, "Input reader overload (no more vectors)\n"); - exit(1); - } + ASSERT(reader->size > 0); + ASSERT(reader->nb_inputs < 10); + const char *next = *(reader->next); reader->next++; reader->size--; -- 2.47.3