Skip to contentSkip to conversation

lib/random32: convert selftest to KUnit

PATCH Applied Kir Chou 1 participant 1 message Jul 3

Subsystems (4)
lore
Share & tools
lore

Kir Chou

author
#0 lore raw

PATCH v5 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.

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

```
CONFIG_KUNIT=y
CONFIG_PRANDOM_KUNIT_TEST=y
```

---
v5:
- Adds missing MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"). Verified with a kernel build with CONFIG_PRANDOM_KUNIT_TEST.

v4:
- Changes from bool to tristate in lib/Kconfig.debug.
- Spins off the test into its own module under lib/tests/.
- Adds a prototype for prandom_warmup() to avoid a warning.
- Makes prandom_warmup() visible to the test in lib/random32.c.
- Adds the build rule for random32_kunit.o to lib/tests/Makefile.

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

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

---
 lib/Kconfig                |   6 --
 lib/Kconfig.debug          |   9 ++
 lib/random32.c             | 184 ++-----------------------------------
 lib/tests/Makefile         |   1 +
 lib/tests/random32_kunit.c | 182 ++++++++++++++++++++++++++++++++++++
 5 files changed, 202 insertions(+), 180 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..0214a5859 100644
@@ -3354,6 +3354,15 @@ config PRIME_NUMBERS_KUNIT_TEST
 
 	  If unsure, say N
 
+config PRANDOM_KUNIT_TEST
+	tristate "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 +10 -174

205 lines not on this page · raw message

lib/tests/Makefile +1 -0
index 601dba4b7..39d283b26 100644
@@ -49,5 +49,6 @@ obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o
 obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
 obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o
 obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o
+obj-$(CONFIG_PRANDOM_KUNIT_TEST) += random32_kunit.o
 
 obj-$(CONFIG_TEST_RUNTIME_MODULE)		+= module/
lib/tests/random32_kunit.c new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
2.52.0