From e355fb6fb1c926d0ac109c35704b301e37172c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 26 Mar 2021 13:07:55 +0100 Subject: [PATCH 1/3] basic/log: fix log_trace() log_trace() was always on. It's supposed to be opt-in. --- src/basic/log.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/log.h b/src/basic/log.h index 52fd58d5b4..b3d32abfc8 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -216,7 +216,7 @@ int log_emergency_level(void); #define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__) #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__) -#ifdef LOG_TRACE +#if LOG_TRACE # define log_trace(...) log_debug(__VA_ARGS__) #else # define log_trace(...) do {} while (0) From 93457c0c7a03f28539f248220659cf6c08bd20f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 26 Mar 2021 12:42:22 +0100 Subject: [PATCH 2/3] test-random-util: modernization --- src/test/test-random-util.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/test-random-util.c b/src/test/test-random-util.c index 02a73ecdb2..c6d017ac11 100644 --- a/src/test/test-random-util.c +++ b/src/test/test-random-util.c @@ -7,11 +7,10 @@ static void test_genuine_random_bytes(RandomFlags flags) { uint8_t buf[16] = {}; - unsigned i; log_info("/* %s */", __func__); - for (i = 1; i < sizeof buf; i++) { + for (size_t i = 1; i < sizeof buf; i++) { assert_se(genuine_random_bytes(buf, i, flags) == 0); if (i + 1 < sizeof buf) assert_se(buf[i] == 0); @@ -22,11 +21,10 @@ static void test_genuine_random_bytes(RandomFlags flags) { static void test_pseudo_random_bytes(void) { uint8_t buf[16] = {}; - unsigned i; log_info("/* %s */", __func__); - for (i = 1; i < sizeof buf; i++) { + for (size_t i = 1; i < sizeof buf; i++) { pseudo_random_bytes(buf, i); if (i + 1 < sizeof buf) assert_se(buf[i] == 0); @@ -36,9 +34,11 @@ static void test_pseudo_random_bytes(void) { } static void test_rdrand(void) { - int r, i; + int r; - for (i = 0; i < 10; i++) { + log_info("/* %s */", __func__); + + for (unsigned i = 0; i < 10; i++) { unsigned long x = 0; r = rdrand(&x); From f2a8b8decfa3a5281047ad4542a48435302e2253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 26 Mar 2021 12:42:52 +0100 Subject: [PATCH 3/3] test-random-util: add stochastic test for random_u64_range() --- src/test/test-random-util.c | 52 +++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/test/test-random-util.c b/src/test/test-random-util.c index c6d017ac11..44103efa62 100644 --- a/src/test/test-random-util.c +++ b/src/test/test-random-util.c @@ -1,8 +1,12 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "hexdecoct.h" -#include "random-util.h" #include "log.h" +#include "memory-util.h" +#include "random-util.h" +#include "terminal-util.h" #include "tests.h" static void test_genuine_random_bytes(RandomFlags flags) { @@ -51,6 +55,50 @@ static void test_rdrand(void) { } } +#define TOTAL 100000 + +static void test_random_u64_range_one(unsigned mod) { + log_info("/* %s(%u) */", __func__, mod); + + unsigned max = 0, count[mod]; + zero(count); + + for (unsigned i = 0; i < TOTAL; i++) { + uint64_t x; + + x = random_u64_range(mod); + + log_trace("%05u: %"PRIu64, i, x); + count[x]++; + max = MAX(max, count[x]); + } + + /* Print histogram: vertical axis — value, horizontal axis — count. + * + * The expected value is always TOTAL/mod, because the distribution should be flat. The expected + * variance is TOTAL×p×(1-p), where p==1/mod, and standard deviation the root of the variance. + * Assert that the deviation from the expected value is less than 6 standard deviations. + */ + unsigned scale = 2 * max / (columns() < 20 ? 80 : columns() - 20); + double exp = (double) TOTAL / mod; + + for (size_t i = 0; i < mod; i++) { + double dev = (count[i] - exp) / sqrt(exp * (mod > 1 ? mod - 1 : 1) / mod); + log_debug("%02zu: %5u (%+.3f)%*s", + i, count[i], dev, + count[i] / scale, "x"); + + assert_se(fabs(dev) < 6); /* 6 sigma is excessive, but this check should be enough to + * identify catastrophic failure while minimizing false + * positives. */ + } +} + +static void test_random_u64_range(void) { + for (unsigned mod = 1; mod < 29; mod++) + test_random_u64_range_one(mod); +} + int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); @@ -61,8 +109,8 @@ int main(int argc, char **argv) { test_genuine_random_bytes(RANDOM_ALLOW_INSECURE); test_pseudo_random_bytes(); - test_rdrand(); + test_random_u64_range(); return 0; }