summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-09-06 03:22:20 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-09-06 03:22:20 +0200
commit684be371508fd505ad02c4a387cd69ce12152931 (patch)
tree94801f47977e6c359a74ae215d6311775d770bce /source
parentc382f3913b5e78c75dba45bbf0ae7300efe012c9 (diff)
downloadkit-684be371508fd505ad02c4a387cd69ce12152931.zip
test
Diffstat (limited to 'source')
-rw-r--r--source/kit/secure_random.c7
-rw-r--r--source/tests/secure_random.test.c12
2 files changed, 6 insertions, 13 deletions
diff --git a/source/kit/secure_random.c b/source/kit/secure_random.c
index 20b3bf8..d51b4ec 100644
--- a/source/kit/secure_random.c
+++ b/source/kit/secure_random.c
@@ -49,17 +49,18 @@ static void secure_random_fallback(ptrdiff_t size, void *data) {
* seed the pseudo random number generator.
*/
+ static int8_t first_run = 1;
static uint64_t n = 0;
static uint64_t time_sec = 0;
static uint64_t time_nsec = 0;
struct timespec t;
- ptrdiff_t i;
timespec_get(&t, TIME_UTC);
kit_mt64_state_t state;
- if (time_sec == 0 && time_nsec == 0) {
+ if (first_run) {
+ first_run = 0;
uint64_t seed[] = { n, get_available_memory(),
(uint64_t) t.tv_sec, (uint64_t) t.tv_nsec };
kit_mt64_init_array(&state, sizeof seed / sizeof *seed, seed);
@@ -79,7 +80,7 @@ static void secure_random_fallback(ptrdiff_t size, void *data) {
time_sec = (uint64_t) t.tv_sec;
time_nsec = (uint64_t) t.tv_nsec;
- for (i = 0; i < size; i++)
+ for (ptrdiff_t i = 0; i < size; i++)
((uint8_t *) data)[i] = (uint8_t) (kit_mt64_generate(&state) >>
56);
diff --git a/source/tests/secure_random.test.c b/source/tests/secure_random.test.c
index 3e8fd9e..0f25b47 100644
--- a/source/tests/secure_random.test.c
+++ b/source/tests/secure_random.test.c
@@ -4,8 +4,6 @@
#define KIT_TEST_FILE secure_random
#include "../kit_test/test.h"
-#include <stdio.h>
-
TEST("secure random") {
int v[20];
memset(v, 0, sizeof v);
@@ -15,16 +13,10 @@ TEST("secure random") {
int repeats = 0;
- printf(" %d \n", v[0]);
- for (int i = 1; i < sizeof v / sizeof *v; i++) {
- printf(" %d", v[i]);
+ for (int i = 1; i < sizeof v / sizeof *v; i++)
for (int j = 0; j < i; j++)
- if (v[i] == v[j]) {
- printf(" - REPEAT");
+ if (v[i] == v[j])
repeats++;
- }
- printf(" \n");
- }
REQUIRE_EQ(repeats, 0);
}