Skip to contentSkip to conversation

lib/random32: convert selftest to KUnit

PATCH Superseded Kir Chou 2 participants 3 messages Jun 29 – Jun 30

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Kir Chou

author
#0 lore raw

PATCH v3 1/1 lib/random32: convert selftest to KUnit

This patch converts the existing prandom selftest (lib/random32.c) to use the KUnit framework (lib/tests/random32_kunit.c). Unlike typical KUnit tests, this file is directly #included into lib/random32.c.

The new test:
- Removes the legacy CONFIG_RANDOM32_SELFTEST from lib/random32.c.
- Adds CONFIG_PRANDOM_KUNIT_TEST (defaulting to KUNIT_ALL_TESTS).
- Moves the test logic to lib/tests/random32_kunit.c.

v2:
- Removes the legacy CONFIG_RANDOM32_SELFTEST from lib/Kconfig.
- Add const to arrays in lib/tests/random32_kunit.c.

v3:
- Changes from tristate to bool in lib/Kconfig.debug.

This commit is verified by ./tools/testing/kunit/kunit.py run with the .kunit/.kunitconfig:

```
CONFIG_KUNIT=y
CONFIG_PRANDOM_KUNIT_TEST=y
```
---
 lib/Kconfig                |   6 --
 lib/Kconfig.debug          |   9 ++
 lib/random32.c             | 173 +-----------------------------------
 lib/tests/random32_kunit.c | 174 +++++++++++++++++++++++++++++++++++++
 4 files changed, 185 insertions(+), 177 deletions(-)
 create mode 100644 lib/tests/random32_kunit.c
lib/Kconfig +0 -6
index 2923924be..5f185e9f1 100644
@@ -156,12 +156,6 @@ config AUDIT_COMPAT_GENERIC
 	depends on AUDIT_GENERIC && AUDIT_ARCH_COMPAT_GENERIC && COMPAT
 	default y
 
-config RANDOM32_SELFTEST
-	bool "PRNG perform self test on init"
-	help
-	  This option enables the 32 bit PRNG library functions to perform a
-	  self test on initialization.
-
 #
 # compression support is select'ed if needed
 #
lib/Kconfig.debug +9 -0
index ba36939fd..ef1600600 100644
@@ -3354,6 +3354,15 @@ config PRIME_NUMBERS_KUNIT_TEST
 
 	  If unsure, say N
 
+config PRANDOM_KUNIT_TEST
+	bool "KUnit test for prandom" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  Enable this option to test the prandom functions at runtime.
+
+	  If unsure, say N
+
 endif # RUNTIME_TESTING_MENU
 
 config ARCH_USE_MEMTEST
lib/random32.c +2 -171

177 lines not on this page · raw message

lib/tests/random32_kunit.c new file +174 -0

174 lines not on this page · raw message

Signature · 1 line
2.52.0
#1 lore raw

Hi Kir,

Thanks for the update!

Quoted 10 lines · Kir Chou

This patch converts the existing prandom selftest (lib/random32.c) to use the KUnit framework (lib/tests/random32_kunit.c). Unlike typical KUnit tests, this file is directly #included into lib/random32.c.

The new test:
- Removes the legacy CONFIG_RANDOM32_SELFTEST from lib/random32.c.
- Adds CONFIG_PRANDOM_KUNIT_TEST (defaulting to KUNIT_ALL_TESTS).
- Moves the test logic to lib/tests/random32_kunit.c.

v2:

The changelog belongs under the "---" under your SoB.

Quoted 5 lines

- Removes the legacy CONFIG_RANDOM32_SELFTEST from lib/Kconfig.
- Add const to arrays in lib/tests/random32_kunit.c.

v3:
- Changes from tristate to bool in lib/Kconfig.debug.

That's (IMHO very) unfortunate...

lib/random32.c void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state) · line 131

Quoted 7 lines
lib/random32.c +2 -1
 
-core_initcall(prandom_state_selftest);
+#ifdef CONFIG_PRANDOM_KUNIT_TEST
+#include "tests/random32_kunit.c"
 #endif

I guess that is the reason?

Any reason why the actual test code cannot be spun-off into a separate module, so it can become modular?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author
#2 lore raw

Yes it’s probably doable (done in v4), I didn’t do this to follow the existing design.

Sorry I forgot address the SoB format, I’ll handle if having v5…

Quoted 51 lines · Geert Uytterhoeven

Hi Kir,

Thanks for the update!

Quoted 11 lines · Kir Chou

This patch converts the existing prandom selftest (lib/random32.c) to use the KUnit framework (lib/tests/random32_kunit.c). Unlike typical KUnit tests, this file is directly #included into lib/random32.c.

The new test:
- Removes the legacy CONFIG_RANDOM32_SELFTEST from lib/random32.c.
- Adds CONFIG_PRANDOM_KUNIT_TEST (defaulting to KUNIT_ALL_TESTS).
- Moves the test logic to lib/tests/random32_kunit.c.

v2:

The changelog belongs under the "---" under your SoB.

Quoted 5 lines

- Removes the legacy CONFIG_RANDOM32_SELFTEST from lib/Kconfig.
- Add const to arrays in lib/tests/random32_kunit.c.

v3:
- Changes from tristate to bool in lib/Kconfig.debug.

That's (IMHO very) unfortunate...

Quoted 7 lines
lib/random32.c +2 -1
 
-core_initcall(prandom_state_selftest);
+#ifdef CONFIG_PRANDOM_KUNIT_TEST
+#include "tests/random32_kunit.c"

#endif

I guess that is the reason?

Any reason why the actual test code cannot be spun-off into a separate module, so it can become modular?

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds