1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 18:15:18 -07:00

More tests for randombytes

This commit is contained in:
Frank Denis 2014-09-14 12:43:53 -07:00
parent c35aee3b94
commit 0ec5a25c88
3 changed files with 64 additions and 6 deletions

View File

@ -35,6 +35,7 @@ EXTRA_DIST = \
onetimeauth7.exp \
pwhash.exp \
pwhash_scrypt_ll.exp \
randombytes.exp \
scalarmult.exp \
scalarmult2.exp \
scalarmult5.exp \
@ -92,6 +93,7 @@ DISTCLEANFILES = \
onetimeauth7.res \
pwhash.res \
pwhash_scrypt_ll.res \
randombytes.res \
scalarmult.res \
scalarmult2.res \
scalarmult5.res \
@ -283,7 +285,7 @@ pwhash_LDADD = $(TESTS_LDADD)
pwhash_scrypt_ll_SOURCE = cmptest.h pwhash_scrypt_ll.c
pwhash_scrypt_ll_LDADD = $(TESTS_LDADD)
randombytes_SOURCE = randombytes.c
randombytes_SOURCE = cmptest.h randombytes.c
randombytes_LDADD = $(TESTS_LDADD)
scalarmult_SOURCE = cmptest.h scalarmult.c

View File

@ -1,12 +1,15 @@
#include "randombytes.h"
#define TEST_NAME "randombytes"
#include "cmptest.h"
unsigned char x[65536];
unsigned long long freq[256];
static int nacl_tests(void)
static int compat_tests(void)
{
unsigned long long i;
size_t i;
memset(x, 0, sizeof x);
randombytes(x, sizeof x);
for (i = 0; i < 256; ++i) {
freq[i] = 0;
@ -16,14 +19,66 @@ static int nacl_tests(void)
}
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
return 111;
printf("nacl_tests failed\n");
}
}
return 0;
}
static int randombytes_tests(void)
{
unsigned int i;
assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0);
for (i = 0; i < 256; ++i) {
freq[i] = 0;
}
for (i = 0; i < 65536; ++i) {
++freq[randombytes_uniform(256)];
}
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
printf("randombytes_uniform() test failed\n");
}
}
randombytes_close();
randombytes_set_implementation(&randombytes_salsa20_implementation);
assert(strcmp(randombytes_implementation_name(), "salsa20") == 0);
randombytes_stir();
for (i = 0; i < 256; ++i) {
freq[i] = 0;
}
for (i = 0; i < 65536; ++i) {
++freq[randombytes_uniform(256)];
}
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
printf("randombytes_uniform() test failed\n");
}
}
memset(x, 0, sizeof x);
randombytes_buf(x, sizeof x);
for (i = 0; i < 256; ++i) {
freq[i] = 0;
}
for (i = 0; i < sizeof x; ++i) {
++freq[255 & (int)x[i]];
}
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
printf("randombytes_buf() test failed\n");
}
}
randombytes_close();
return 0;
}
int main(void)
{
nacl_tests();
compat_tests();
randombytes_tests();
printf("OK\n");
return 0;
}

View File

@ -0,0 +1 @@
OK