lib/tests: convert test_memcat_p module to KUnit
Subsystems (2)
PATCH v2 1/1 lib/tests: convert test_memcat_p module to KUnit
Both AI review questions are addressed; the changes are described below the --- in the patch.
From 4ebf2dfe3e71066a3af1d6f53b85e3ce3ca3ed4d Mon Sep 17 00:00:00 2001
From: Wang Yibo <[email protected]>
Date: Tue, 13 Oct 2026 22:21:49 +0800
Subject: [PATCH v2] lib/tests: convert test_memcat_p module to KUnit
Move lib/test_memcat_p.c to lib/tests/memcat_p_kunit.c and convert it to use KUnit.
This change switches the ad-hoc test code to standard KUnit test cases. The test data remains the same, but the verification logic is updated to use KUNIT_EXPECT_* macros, so a failure now reports which element mismatched instead of a single pr_err() message. The input arrays and their elements are allocated with the kunit_kmalloc() family, which lets the manual error unwinding paths and the empty module exit hook go away.
Three things are spelled out differently, because a KUnit assertion can abandon the test half way through:
- The array returned by memcat_p() is allocated with kmalloc_array() inside the function under test, so kunit_kfree() does not know about it and does nothing. It is freed with kfree() through a registered test action instead, so that it is released even when a later assertion fails the test.
- The check on the number of merged elements is a KUNIT_ASSERT_EQ():
the element order loop that follows indexes out[] up to
2 * INPUT_MAX - 3 without any bound check, so a shorter than expected array has to stop the test there rather than be walked off the end.
- The counting loop compares against its limit before dereferencing, so that a missing terminator cannot be read past the last element.
CONFIG_TEST_MEMCAT_P keeps its name so that the defconfigs enabling it stay valid; it now depends on KUNIT and defaults to KUNIT_ALL_TESTS, like the other lib KUnit tests. Those defconfigs already select KUNIT and KUNIT_ALL_TESTS, so they keep turning the test on unchanged.
---
v2: address the AI review questions: free the memcat_p() result with
kfree() (through a test action) instead of kunit_kfree(), which silently ignores memory KUnit does not own; and make the element count check fatal, so a short array cannot lead to an out-of-bounds read in the element order loop. While at it, move the limit check in the counting loop before the dereference.
--- lib/Kconfig.debug | 4 +- lib/Makefile | 1 - lib/test_memcat_p.c | 116 ------------------------------------- lib/tests/Makefile | 1 + lib/tests/memcat_p_kunit.c | 91 +++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 118 deletions(-) delete mode 100644 lib/test_memcat_p.c create mode 100644 lib/tests/memcat_p_kunit.c
lib/Kconfig.debug +3 -1
@@ -3364,7 +3364,9 @@ config TEST_DEBUG_VIRTUAL If unsure, say N. config TEST_MEMCAT_P - tristate "Test memcat_p() helper function" + tristate "KUnit test for memcat_p() helper function" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS help Test the memcat_p() helper for correctly merging two pointer arrays together.
lib/Makefile +0 -1
@@ -97,7 +97,6 @@ obj-$(CONFIG_TEST_MAPLE_TREE) += test_maple_tree.o obj-$(CONFIG_TEST_PARMAN) += test_parman.o obj-$(CONFIG_TEST_KMOD) += test_kmod.o obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o -obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o obj-$(CONFIG_TEST_CSD_LOCK) += test_csd_lock.o
lib/test_memcat_p.c deleted +0 -116
116 lines not on this page · raw message
lib/tests/Makefile +1 -0
@@ -40,6 +40,7 @@ CFLAGS_longest_symbol_kunit.o += $(call cc-disable-warning, missing-prototypes) obj-$(CONFIG_LONGEST_SYM_KUNIT_TEST) += longest_symbol_kunit.o obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o +obj-$(CONFIG_TEST_MEMCAT_P) += memcat_p_kunit.o obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare) obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
lib/tests/memcat_p_kunit.c new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.25.1