]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-random-util: add stochastic test for random_u64_range() 19129/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 26 Mar 2021 11:42:52 +0000 (12:42 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 26 Mar 2021 13:38:44 +0000 (14:38 +0100)
src/test/test-random-util.c

index c6d017ac11352d87fcc17c70887aab6d1546ced2..44103efa62a24a6fd3eb45486b68bf75b491c037 100644 (file)
@@ -1,8 +1,12 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include <math.h>
+
 #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;
 }