libsodium: updated to 1.0.10
[tomato.git] / release / src / router / libsodium / test / default / randombytes.c
blob78c48dc9711edcfb988d40489970beb479781765
2 #define TEST_NAME "randombytes"
3 #include "cmptest.h"
5 static unsigned char x[65536];
6 static unsigned long long freq[256];
8 static int compat_tests(void)
10 size_t i;
12 memset(x, 0, sizeof x);
13 randombytes(x, sizeof x);
14 for (i = 0; i < 256; ++i) {
15 freq[i] = 0;
17 for (i = 0; i < sizeof x; ++i) {
18 ++freq[255 & (int)x[i]];
20 for (i = 0; i < 256; ++i) {
21 if (!freq[i]) {
22 printf("nacl_tests failed\n");
25 return 0;
28 static int randombytes_tests(void)
30 unsigned int f = 0U;
31 unsigned int i;
32 uint32_t n;
34 #ifdef __EMSCRIPTEN__
35 assert(strcmp(randombytes_implementation_name(), "sysrandom"));
36 #else
37 assert(strcmp(randombytes_implementation_name(), "js"));
38 #endif
39 randombytes(x, 1U);
40 do {
41 n = randombytes_random();
42 f |= ((n >> 24) > 1);
43 f |= ((n >> 16) > 1) << 1;
44 f |= ((n >> 8) > 1) << 2;
45 f |= ((n ) > 1) << 3;
46 f |= (n > 0x7fffffff) << 4;
47 } while (f != 0x1f);
48 randombytes_close();
50 for (i = 0; i < 256; ++i) {
51 freq[i] = 0;
53 for (i = 0; i < 65536; ++i) {
54 ++freq[randombytes_uniform(256)];
56 for (i = 0; i < 256; ++i) {
57 if (!freq[i]) {
58 printf("randombytes_uniform() test failed\n");
61 assert(randombytes_uniform(1U) == 0U);
62 randombytes_close();
63 #ifndef __EMSCRIPTEN__
64 randombytes_set_implementation(&randombytes_salsa20_implementation);
65 assert(strcmp(randombytes_implementation_name(), "salsa20") == 0);
66 #endif
67 randombytes_stir();
68 for (i = 0; i < 256; ++i) {
69 freq[i] = 0;
71 for (i = 0; i < 65536; ++i) {
72 ++freq[randombytes_uniform(256)];
74 for (i = 0; i < 256; ++i) {
75 if (!freq[i]) {
76 printf("randombytes_uniform() test failed\n");
79 memset(x, 0, sizeof x);
80 randombytes_buf(x, sizeof x);
81 for (i = 0; i < 256; ++i) {
82 freq[i] = 0;
84 for (i = 0; i < sizeof x; ++i) {
85 ++freq[255 & (int)x[i]];
87 for (i = 0; i < 256; ++i) {
88 if (!freq[i]) {
89 printf("randombytes_buf() test failed\n");
92 assert(randombytes_uniform(1U) == 0U);
93 randombytes_close();
95 randombytes(x, 1U);
96 randombytes_close();
98 return 0;
101 static uint32_t randombytes_uniform_impl(const uint32_t upper_bound)
103 return upper_bound;
106 static int impl_tests(void)
108 #ifndef __native_client__
109 randombytes_implementation impl = randombytes_sysrandom_implementation;
110 #else
111 randombytes_implementation impl = randombytes_nativeclient_implementation;
112 #endif
113 uint32_t v = randombytes_random();
115 impl.uniform = randombytes_uniform_impl;
116 randombytes_close();
117 randombytes_set_implementation(&impl);
118 assert(randombytes_uniform(v) == v);
119 assert(randombytes_uniform(v) == v);
120 assert(randombytes_uniform(v) == v);
121 assert(randombytes_uniform(v) == v);
122 randombytes_close();
123 impl.close = NULL;
124 randombytes_close();
126 return 0;
129 int main(void)
131 compat_tests();
132 randombytes_tests();
133 #ifndef __EMSCRIPTEN__
134 impl_tests();
135 #endif
136 printf("OK\n");
138 return 0;