From 37580f4f525aa6feadc133c8762a92dc43629b74 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 16 Sep 2014 20:46:43 -0700 Subject: [PATCH] More tests --- .../salsa20/randombytes_salsa20_random.c | 2 +- .../sysrandom/randombytes_sysrandom.c | 2 +- test/default/generichash3.c | 2 +- test/default/randombytes.c | 9 +++++ test/default/sodium_utils.c | 33 ++++++++++++++++++- test/default/sodium_utils.exp | 6 ++++ 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c index 869ae8f1..946747d4 100644 --- a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c @@ -323,7 +323,7 @@ randombytes_salsa20_random_uniform(const uint32_t upper_bound) if (r >= min) { break; } - } + } /* LCOV_EXCL_LINE */ return r % upper_bound; } diff --git a/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c b/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c index 711c711c..2979ef39 100644 --- a/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c +++ b/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c @@ -237,7 +237,7 @@ randombytes_sysrandom_uniform(const uint32_t upper_bound) if (r >= min) { break; } - } + } /* LCOV_EXCL_LINE */ return r % upper_bound; } diff --git a/test/default/generichash3.c b/test/default/generichash3.c index 2e8635c0..02008720 100644 --- a/test/default/generichash3.c +++ b/test/default/generichash3.c @@ -147,7 +147,7 @@ int main(void) assert(crypto_generichash_blake2b_init_salt_personal(&st, k, sizeof k, crypto_generichash_BYTES, NULL, personal) == 0); assert(crypto_generichash_blake2b_init_salt_personal(&st, k, sizeof k, crypto_generichash_BYTES, - personal, NULL) == 0); + salt, NULL) == 0); return 0; } diff --git a/test/default/randombytes.c b/test/default/randombytes.c index ca4181ae..f9f337f3 100644 --- a/test/default/randombytes.c +++ b/test/default/randombytes.c @@ -30,6 +30,10 @@ static int randombytes_tests(void) unsigned int i; assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0); + + randombytes(x, 1U); + randombytes_close(); + for (i = 0; i < 256; ++i) { freq[i] = 0; } @@ -41,6 +45,7 @@ static int randombytes_tests(void) printf("randombytes_uniform() test failed\n"); } } + assert(randombytes_uniform(1U) == 0U); randombytes_close(); randombytes_set_implementation(&randombytes_salsa20_implementation); assert(strcmp(randombytes_implementation_name(), "salsa20") == 0); @@ -69,6 +74,10 @@ static int randombytes_tests(void) printf("randombytes_buf() test failed\n"); } } + assert(randombytes_uniform(1U) == 0U); + randombytes_close(); + + randombytes(x, 1U); randombytes_close(); return 0; diff --git a/test/default/sodium_utils.c b/test/default/sodium_utils.c index 65bdaf18..89274deb 100644 --- a/test/default/sodium_utils.c +++ b/test/default/sodium_utils.c @@ -9,6 +9,7 @@ int main(void) char buf3[33]; unsigned char buf4[4]; const char *hex; + const char *hex_end; size_t bin_len; randombytes_buf(buf1, sizeof buf1); @@ -25,9 +26,39 @@ int main(void) sodium_bin2hex(buf3, 33U, (const unsigned char *)"0123456789ABCDEF", 16U)); hex = "Cafe : 6942"; - sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL); + sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, &hex_end); printf("%lu:%02x%02x%02x%02x\n", (unsigned long)bin_len, buf4[0], buf4[1], buf4[2], buf4[3]); + printf("dt1: %ld\n", (long) (hex_end - hex)); + + hex = "Cafe : 6942"; + sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL); + printf("%lu:%02x%02x%02x%02x\n", (unsigned long)bin_len, buf4[2], buf4[3], + buf4[2], buf4[3]); + + hex = "deadbeef"; + if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) { + printf("sodium_hex2bin() overflow not detected\n"); + } + printf("dt2: %ld\n", (long) (hex_end - hex)); + + hex = "de:ad:be:eff"; + if (sodium_hex2bin(buf1, 4U, hex, 12U, ":", &bin_len, &hex_end) != -1) { + printf("sodium_hex2bin() with an odd input length and a short output buffer\n"); + } + printf("dt3: %ld\n", (long) (hex_end - hex)); + + hex = "de:ad:be:eff"; + if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":", &bin_len, &hex_end) != 0) { + printf("sodium_hex2bin() with an odd input length\n"); + } + printf("dt4: %ld\n", (long) (hex_end - hex)); + + hex = "de:ad:be:eff"; + if (sodium_hex2bin(buf1, sizeof buf1, hex, 13U, ":", &bin_len, &hex_end) != 0) { + printf("sodium_hex2bin() with an odd input length\n"); + } + printf("dt5: %ld\n", (long) (hex_end - hex)); return 0; } diff --git a/test/default/sodium_utils.exp b/test/default/sodium_utils.exp index 2be34ae3..e087a911 100644 --- a/test/default/sodium_utils.exp +++ b/test/default/sodium_utils.exp @@ -5,3 +5,9 @@ 0 30313233343536373839414243444546 4:cafe6942 +dt1: 11 +4:69426942 +dt2: 2 +dt3: 11 +dt4: 11 +dt5: 11