From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH 17/18] test: add random number generator tests
Date: Tue, 15 Sep 2026 12:21:15 -0700 [thread overview]
Message-ID: <20260915192207.3926217-18-stephen@networkplumber.org> (raw)
In-Reply-To: <20260915192207.3926217-1-stephen@networkplumber.org>
Add functional tests for rte_random_bytes() and rte_rand32(). The
rte_random_bytes() cases cover a zero length request, requests smaller
than the internal chunk size, a request large enough to be split into
several chunks, and that two calls do not return the same bytes. Each
case also checks that nothing is written past the requested length.
The rte_rand32() cases check that every bit position is both set and
clear across a number of draws, and that rte_srand() makes the
sequence repeatable.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
app/test/meson.build | 1 +
app/test/test_random.c | 297 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 298 insertions(+)
create mode 100644 app/test/test_random.c
diff --git a/app/test/meson.build b/app/test/meson.build
index 51abeeb732..794e4e5962 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -158,6 +158,7 @@ source_file_deps = {
'test_prefetch.c': [],
'test_ptr_compress.c': ['ptr_compress'],
'test_rand_perf.c': [],
+ 'test_random.c': [],
'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
'test_rcu_qsbr.c': ['rcu', 'hash'],
'test_rcu_qsbr_perf.c': ['rcu', 'hash'],
diff --git a/app/test/test_random.c b/app/test/test_random.c
new file mode 100644
index 0000000000..f0084f5abb
--- /dev/null
+++ b/app/test/test_random.c
@@ -0,0 +1,297 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2026 Stephen Hemminger
+ */
+
+#include <inttypes.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include <rte_common.h>
+#include <rte_random.h>
+
+#include "test.h"
+
+/* Larger than the chunk size rte_random_bytes() splits requests into,
+ * and not a multiple of it, so the loop is exercised.
+ */
+#define BIG_LEN (3 * 256 + 7)
+
+/* Fill pattern, any byte left untouched by the call is visible. */
+#define FILL_BYTE 0xa5
+
+static bool
+all_equal(const uint8_t *buf, size_t len, uint8_t val)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++)
+ if (buf[i] != val)
+ return false;
+
+ return true;
+}
+
+static int
+test_random_bytes_zero(void)
+{
+ uint8_t buf[8];
+
+ memset(buf, FILL_BYTE, sizeof(buf));
+
+ TEST_ASSERT(rte_random_bytes(buf, 0) == 0,
+ "zero length request failed");
+ TEST_ASSERT(all_equal(buf, sizeof(buf), FILL_BYTE),
+ "zero length request wrote to the buffer");
+
+ return TEST_SUCCESS;
+}
+
+static int
+test_random_bytes_small(void)
+{
+ uint8_t buf[32];
+ unsigned int i;
+
+ /* A single byte is worth checking on its own, it is the
+ * smallest request that must still be filled.
+ */
+ for (i = 1; i <= sizeof(buf); i++) {
+ memset(buf, FILL_BYTE, sizeof(buf));
+
+ TEST_ASSERT(rte_random_bytes(buf, i) == 0,
+ "request of %u bytes failed", i);
+ TEST_ASSERT(all_equal(buf + i, sizeof(buf) - i, FILL_BYTE),
+ "request of %u bytes wrote past the end", i);
+ }
+
+ return TEST_SUCCESS;
+}
+
+static int
+test_random_bytes_large(void)
+{
+ uint8_t buf[BIG_LEN + 8];
+
+ memset(buf, FILL_BYTE, sizeof(buf));
+
+ /* Must be split internally and still filled completely. */
+ TEST_ASSERT(rte_random_bytes(buf, BIG_LEN) == 0,
+ "request of %d bytes failed", BIG_LEN);
+ TEST_ASSERT(all_equal(buf + BIG_LEN, sizeof(buf) - BIG_LEN, FILL_BYTE),
+ "request of %d bytes wrote past the end", BIG_LEN);
+ TEST_ASSERT(!all_equal(buf, BIG_LEN, 0),
+ "request of %d bytes returned all zero", BIG_LEN);
+ TEST_ASSERT(!all_equal(buf, BIG_LEN, FILL_BYTE),
+ "request of %d bytes left the buffer untouched", BIG_LEN);
+
+ return TEST_SUCCESS;
+}
+
+static int
+test_random_bytes_distinct(void)
+{
+ uint8_t a[64], b[64];
+
+ TEST_ASSERT(rte_random_bytes(a, sizeof(a)) == 0, "first call failed");
+ TEST_ASSERT(rte_random_bytes(b, sizeof(b)) == 0, "second call failed");
+
+ /* The chance of a false failure here is 2^-512. */
+ TEST_ASSERT(memcmp(a, b, sizeof(a)) != 0,
+ "two calls returned the same bytes");
+
+ return TEST_SUCCESS;
+}
+
+static int
+test_rand(void)
+{
+ unsigned int i;
+ uint64_t any_set = 0;
+ uint64_t always_set = UINT64_MAX;
+
+ /* Every bit position should be both set and clear at some
+ * point, which catches a generator that is stuck or that only
+ * ever fills part of the word.
+ */
+ for (i = 0; i < 100; i++) {
+ uint64_t v = rte_rand();
+
+ any_set |= v;
+ always_set &= v;
+ }
+
+ TEST_ASSERT(any_set == UINT64_MAX,
+ "some bits were never set: %#" PRIx64, any_set);
+ TEST_ASSERT(always_set == 0,
+ "some bits were always set: %#" PRIx64, always_set);
+
+ return TEST_SUCCESS;
+}
+
+static int
+test_rand32(void)
+{
+ unsigned int i;
+ uint32_t any_set = 0;
+ uint32_t always_set = UINT32_MAX;
+
+ /* Every bit position should be both set and clear at some
+ * point, which catches a generator that is stuck or that only
+ * ever fills part of the word.
+ */
+ for (i = 0; i < 100; i++) {
+ uint32_t v = rte_rand32();
+
+ any_set |= v;
+ always_set &= v;
+ }
+
+ TEST_ASSERT(any_set == UINT32_MAX,
+ "some bits were never set: %#x", any_set);
+ TEST_ASSERT(always_set == 0,
+ "some bits were always set: %#x", always_set);
+
+ return TEST_SUCCESS;
+}
+
+/* Bounds worth covering: the power of two case is handled by masking,
+ * everything else goes through the rejection loop.
+ */
+static const uint64_t rand_max_bounds[] = {
+ 2, 3, 17, 64, 1000, 1ULL << 32, (1ULL << 63) + 1, UINT64_MAX,
+};
+
+static int
+test_rand_max(void)
+{
+ unsigned int i, j;
+
+ /* A bound of one leaves only a single legal value. */
+ for (i = 0; i < 100; i++)
+ TEST_ASSERT(rte_rand_max(1) == 0,
+ "upper bound of 1 returned a non-zero value");
+
+ for (i = 0; i < RTE_DIM(rand_max_bounds); i++) {
+ uint64_t bound = rand_max_bounds[i];
+
+ for (j = 0; j < 10000; j++) {
+ uint64_t v = rte_rand_max(bound);
+
+ TEST_ASSERT(v < bound,
+ "value %" PRIu64 " is not less than the upper bound %" PRIu64,
+ v, bound);
+ }
+ }
+
+ return TEST_SUCCESS;
+}
+
+static int
+test_rand_max_spread(void)
+{
+ unsigned int i;
+ bool seen[3] = { false, false, false };
+
+ /* With a small bound every value has to show up quickly. The
+ * chance of missing one in 100 draws is 3 * (2/3)^100.
+ */
+ for (i = 0; i < 100; i++)
+ seen[rte_rand_max(RTE_DIM(seen))] = true;
+
+ for (i = 0; i < RTE_DIM(seen); i++)
+ TEST_ASSERT(seen[i], "value %u was never generated", i);
+
+ return TEST_SUCCESS;
+}
+
+static int
+test_drand(void)
+{
+ unsigned int i;
+ bool low = false, high = false;
+
+ for (i = 0; i < 100; i++) {
+ double v = rte_drand();
+
+ TEST_ASSERT(v >= 0.0 && v < 1.0,
+ "value %f is outside of [0.0, 1.0)", v);
+
+ if (v < 0.5)
+ low = true;
+ else
+ high = true;
+ }
+
+ /* Both halves of the interval have to be used, which catches a
+ * generator stuck on a constant. Missing one has a probability
+ * of 2^-100.
+ */
+ TEST_ASSERT(low, "no value below 0.5 was generated");
+ TEST_ASSERT(high, "no value of 0.5 or above was generated");
+
+ return TEST_SUCCESS;
+}
+
+/* Record a sequence that uses every generator function, so that a
+ * function left out of the shared state would show up here.
+ */
+static void
+record_sequence(uint64_t seed, uint64_t *out, unsigned int n)
+{
+ unsigned int i;
+
+ rte_srand(seed);
+
+ for (i = 0; i < n; i += 4) {
+ out[i] = rte_rand();
+ out[i + 1] = rte_rand32();
+ out[i + 2] = rte_rand_max(1000);
+ out[i + 3] = (uint64_t)(rte_drand() * (1ULL << 53));
+ }
+}
+
+static int
+test_srand_repeatable(void)
+{
+ uint64_t first[16], second[16];
+
+ record_sequence(42, first, RTE_DIM(first));
+ record_sequence(42, second, RTE_DIM(second));
+
+ TEST_ASSERT(memcmp(first, second, sizeof(first)) == 0,
+ "same seed produced a different sequence");
+
+ record_sequence(43, second, RTE_DIM(second));
+
+ TEST_ASSERT(memcmp(first, second, sizeof(first)) != 0,
+ "different seed produced the same sequence");
+
+ return TEST_SUCCESS;
+}
+
+static struct unit_test_suite random_test_suite = {
+ .suite_name = "random autotest",
+ .setup = NULL,
+ .teardown = NULL,
+ .unit_test_cases = {
+ TEST_CASE(test_random_bytes_zero),
+ TEST_CASE(test_random_bytes_small),
+ TEST_CASE(test_random_bytes_large),
+ TEST_CASE(test_random_bytes_distinct),
+ TEST_CASE(test_rand),
+ TEST_CASE(test_rand32),
+ TEST_CASE(test_rand_max),
+ TEST_CASE(test_rand_max_spread),
+ TEST_CASE(test_drand),
+ TEST_CASE(test_srand_repeatable),
+ TEST_CASES_END()
+ }
+};
+
+static int
+test_random(void)
+{
+ return unit_test_suite_runner(&random_test_suite);
+}
+
+REGISTER_FAST_TEST(random_autotest, NOHUGE_OK, ASAN_OK, test_random);
--
2.53.0
next prev parent reply other threads:[~2026-09-15 19:23 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 19:20 [PATCH 00/18] random number generator improvements Stephen Hemminger
2026-09-15 19:20 ` [PATCH 01/18] eal/windows: add getentropy shim Stephen Hemminger
2026-09-15 19:21 ` [PATCH 02/18] random: always use getentropy Stephen Hemminger
2026-09-15 19:21 ` [PATCH 03/18] eal: add 32 bit random number function Stephen Hemminger
2026-09-15 19:21 ` [PATCH 04/18] sched: fix data race in RED random number generation Stephen Hemminger
2026-09-15 19:21 ` [PATCH 05/18] eal/windows: add getrandom shim Stephen Hemminger
2026-09-15 19:21 ` [PATCH 06/18] eal: add function to get random bytes Stephen Hemminger
2026-09-15 19:21 ` [PATCH 07/18] hash: use secure random for Toeplitz hash key Stephen Hemminger
2026-09-15 19:21 ` [PATCH 08/18] pipeline: use secure random for IPsec salt Stephen Hemminger
2026-09-15 19:21 ` [PATCH 09/18] net: use secure random for random MAC address Stephen Hemminger
2026-09-15 19:21 ` [PATCH 10/18] ipsec: use secure random for SAD hash seed Stephen Hemminger
2026-09-15 19:21 ` [PATCH 11/18] net/intel: use secure random for default RSS key Stephen Hemminger
2026-09-15 19:21 ` [PATCH 12/18] random: use all 64 bits of the seed Stephen Hemminger
2026-09-15 19:21 ` [PATCH 13/18] drivers/net: use secure random for RSS key Stephen Hemminger
2026-09-15 19:21 ` [PATCH 14/18] drivers/net: use common random MAC address helper Stephen Hemminger
2026-09-15 19:21 ` [PATCH 15/18] random: remove RDSEED fallback for initial seed Stephen Hemminger
2026-09-16 7:44 ` Bruce Richardson
2026-09-15 19:21 ` [PATCH 16/18] test: allow setting random seed for tests Stephen Hemminger
2026-09-15 19:21 ` Stephen Hemminger [this message]
2026-09-15 19:21 ` [PATCH 18/18] doc: recommend secure random for unpredictable values Stephen Hemminger
2026-09-22 12:32 ` Aaron Conole
2026-10-10 2:27 ` [PATCH v2 0/5] random: fix initial seed entropy Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 1/5] eal/windows: add getentropy shim Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 2/5] random: always use getentropy Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 3/5] random: use all 64 bits of the seed Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 4/5] random: remove RDSEED fallback for initial seed Stephen Hemminger
2026-10-10 2:27 ` [PATCH v2 5/5] test: allow setting random seed for tests Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260915192207.3926217-18-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=dev@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox