libsodium/test/default/randombytes.c

140 lines
3.1 KiB
C
Raw Normal View History

2014-09-14 15:43:53 -04:00
#define TEST_NAME "randombytes"
#include "cmptest.h"
2013-01-20 17:03:05 -05:00
2015-11-23 10:07:13 -05:00
static unsigned char x[65536];
static unsigned long long freq[256];
2013-01-20 17:03:05 -05:00
2014-09-14 15:43:53 -04:00
static int compat_tests(void)
2013-01-20 17:03:05 -05:00
{
2014-09-14 15:43:53 -04:00
size_t i;
2013-01-20 17:03:05 -05:00
2014-09-14 15:43:53 -04:00
memset(x, 0, sizeof x);
2014-09-14 14:32:55 -04:00
randombytes(x, sizeof x);
for (i = 0; i < 256; ++i) {
freq[i] = 0;
}
2014-09-14 14:32:55 -04:00
for (i = 0; i < sizeof x; ++i) {
++freq[255 & (int)x[i]];
}
2014-09-14 14:32:55 -04:00
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
2014-09-14 15:43:53 -04:00
printf("nacl_tests failed\n");
}
}
return 0;
}
static int randombytes_tests(void)
{
2015-01-18 04:34:31 -05:00
unsigned int f = 0U;
2014-09-14 15:43:53 -04:00
unsigned int i;
2015-01-18 04:34:31 -05:00
uint32_t n;
2014-09-14 15:43:53 -04:00
#ifdef __EMSCRIPTEN__
assert(strcmp(randombytes_implementation_name(), "sysrandom"));
#else
assert(strcmp(randombytes_implementation_name(), "js"));
#endif
2014-09-16 23:46:43 -04:00
randombytes(x, 1U);
2015-01-18 04:34:31 -05:00
do {
n = randombytes_random();
f |= ((n >> 24) > 1);
f |= ((n >> 16) > 1) << 1;
f |= ((n >> 8) > 1) << 2;
f |= ((n ) > 1) << 3;
f |= (n > 0x7fffffff) << 4;
} while (f != 0x1f);
2014-09-16 23:46:43 -04:00
randombytes_close();
2014-09-14 15:43:53 -04:00
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");
}
}
2014-09-16 23:46:43 -04:00
assert(randombytes_uniform(1U) == 0U);
2014-09-14 15:43:53 -04:00
randombytes_close();
#ifndef __EMSCRIPTEN__
2014-09-14 15:43:53 -04:00
randombytes_set_implementation(&randombytes_salsa20_implementation);
assert(strcmp(randombytes_implementation_name(), "salsa20") == 0);
#endif
2014-09-14 15:43:53 -04:00
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");
}
}
2014-09-16 23:46:43 -04:00
assert(randombytes_uniform(1U) == 0U);
randombytes_close();
randombytes(x, 1U);
2014-09-14 15:43:53 -04:00
randombytes_close();
return 0;
2014-09-14 14:32:55 -04:00
}
static uint32_t randombytes_uniform_impl(const uint32_t upper_bound)
{
return upper_bound;
}
static int impl_tests(void)
{
#ifndef __native_client__
randombytes_implementation impl = randombytes_sysrandom_implementation;
#else
randombytes_implementation impl = randombytes_nativeclient_implementation;
#endif
uint32_t v = randombytes_random();
impl.uniform = randombytes_uniform_impl;
randombytes_close();
randombytes_set_implementation(&impl);
assert(randombytes_uniform(v) == v);
assert(randombytes_uniform(v) == v);
assert(randombytes_uniform(v) == v);
assert(randombytes_uniform(v) == v);
randombytes_close();
impl.close = NULL;
randombytes_close();
return 0;
}
2014-09-14 14:32:55 -04:00
int main(void)
{
2014-09-14 15:43:53 -04:00
compat_tests();
randombytes_tests();
#ifndef __EMSCRIPTEN__
impl_tests();
#endif
2014-09-14 15:43:53 -04:00
printf("OK\n");
2014-09-14 14:32:55 -04:00
return 0;
2013-01-20 17:03:05 -05:00
}