Skip to contentSkip to conversation

scanf: convert self-test to KUnit

PATCH Superseded Tamir Duberstein 2 participants 6 messages Feb 10, 2025

Subsystems (2)

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v5 cover scanf: convert self-test to KUnit

This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.

I tested this using:

$ tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 scanf

---
Changes in v5:
- Remove extraneous trailing newlines from failure messages.
- Replace pr_debug with kunit_printk.
- Use static char arrays instead of kmalloc.
- Drop KUnit boilerplate from CONFIG_SCANF_KUNIT_TEST help text.
- Drop arch changes.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Bake test into various macros, greatly reducing diff noise.
- Revert control flow changes.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Reduce diff noise in lib/Makefile. (Petr Mladek)
- Split scanf_test into a few test cases. New output:
: =================== scanf (10 subtests) ====================
: [PASSED] numbers_simple
: ====================== numbers_list =======================
: [PASSED] delim=" "
: [PASSED] delim=":"
: [PASSED] delim=","
: [PASSED] delim="-"
: [PASSED] delim="/"
: ================== [PASSED] numbers_list ===================
: ============ numbers_list_field_width_typemax =============
: [PASSED] delim=" "
: [PASSED] delim=":"
: [PASSED] delim=","
: [PASSED] delim="-"
: [PASSED] delim="/"
: ======== [PASSED] numbers_list_field_width_typemax =========
: =========== numbers_list_field_width_val_width ============
: [PASSED] delim=" "
: [PASSED] delim=":"
: [PASSED] delim=","
: [PASSED] delim="-"
: [PASSED] delim="/"
: ======= [PASSED] numbers_list_field_width_val_width ========
: [PASSED] numbers_slice
: [PASSED] numbers_prefix_overflow
: [PASSED] test_simple_strtoull
: [PASSED] test_simple_strtoll
: [PASSED] test_simple_strtoul
: [PASSED] test_simple_strtol
: ====================== [PASSED] scanf ======================
: ============================================================
: Testing complete. Ran 22 tests: passed: 22
: Elapsed time: 5.517s total, 0.001s configuring, 5.440s building, 0.067s running
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Rename lib/{test_scanf.c => scanf_kunit.c}. (Andy Shevchenko)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Tamir Duberstein (2):
scanf: convert self-test to KUnit
scanf: break kunit into test cases

 MAINTAINERS                          |   2 +-
 lib/Kconfig.debug                    |  12 +-
 lib/Makefile                         |   2 +-
 lib/{test_scanf.c => scanf_kunit.c}  | 278 ++++++++++++++++-------------------
 tools/testing/selftests/lib/Makefile |   2 +-
 tools/testing/selftests/lib/config   |   1 -
 tools/testing/selftests/lib/scanf.sh |   4 -
 7 files changed, 138 insertions(+), 163 deletions(-)

---
base-commit: a64dcfb451e254085a7daee5fe51bf22959d52d3
change-id: 20250131-scanf-kunit-convert-f70dc33bb34c

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
#1 lore raw

PATCH v5 1/2 scanf: convert self-test to KUnit

Convert the scanf() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't refactor the tests into proper parameterized tests - it's all one big test case.

---
 MAINTAINERS                          |   2 +-
 lib/Kconfig.debug                    |  12 +-
 lib/Makefile                         |   2 +-
 lib/{test_scanf.c => scanf_kunit.c}  | 228 ++++++++++++++++-------------------
 tools/testing/selftests/lib/Makefile |   2 +-
 tools/testing/selftests/lib/config   |   1 -
 tools/testing/selftests/lib/scanf.sh |   4 -
 7 files changed, 118 insertions(+), 133 deletions(-)
MAINTAINERS +1 -1
index 25c86f47353d..ab1d90c66c02 100644
@@ -25411,8 +25411,8 @@ R:	Sergey Senozhatsky <[email protected]>
 S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git
 F:	Documentation/core-api/printk-formats.rst
+F:	lib/scanf_kunit.c
 F:	lib/test_printf.c
-F:	lib/test_scanf.c
 F:	lib/vsprintf.c
 
 VT1211 HARDWARE MONITOR DRIVER
lib/Kconfig.debug +9 -3
index 1af972a92d06..d35b60203856 100644
@@ -2427,6 +2427,15 @@ config ASYNC_RAID6_TEST
 config TEST_HEXDUMP
 	tristate "Test functions located in the hexdump module at runtime"
 
+config SCANF_KUNIT_TEST
+	tristate "KUnit test scanf() family of functions at runtime" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  Enable this option to test the scanf functions at boot.
+
+	  If unsure, say N.
+
 config STRING_KUNIT_TEST
 	tristate "KUnit test string functions at runtime" if !KUNIT_ALL_TESTS
 	depends on KUNIT
@@ -2443,9 +2452,6 @@ config TEST_KSTRTOX
 config TEST_PRINTF
 	tristate "Test printf() family of functions at runtime"
 
-config TEST_SCANF
-	tristate "Test scanf() family of functions at runtime"
-
 config TEST_BITMAP
 	tristate "Test bitmap_*() family of functions at runtime"
 	help
lib/Makefile +1 -1
index d5cfc7afbbb8..6bdf2287878d 100644
@@ -85,7 +85,7 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
 obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o
 obj-$(CONFIG_TEST_PRINTF) += test_printf.o
-obj-$(CONFIG_TEST_SCANF) += test_scanf.o
+obj-$(CONFIG_SCANF_KUNIT_TEST) += scanf_kunit.o
 
 obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
 ifeq ($(CONFIG_CC_IS_CLANG)$(CONFIG_KASAN),yy)
lib/scanf_kunit.c renamed from lib/test_scanf.c +106 -122

567 lines not on this page · raw message

tools/testing/selftests/lib/Makefile +1 -1
index c52fe3ad8e98..4afda556151f 100644
@@ -4,5 +4,5 @@
 # No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
 all:
 
-TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh scanf.sh
+TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh
 include ../lib.mk
tools/testing/selftests/lib/config +0 -1
index dc15aba8d0a3..80b55a504868 100644
@@ -1,5 +1,4 @@
 CONFIG_TEST_PRINTF=m
-CONFIG_TEST_SCANF=m
 CONFIG_TEST_BITMAP=m
 CONFIG_PRIME_NUMBERS=m
 CONFIG_TEST_BITOPS=m
tools/testing/selftests/lib/scanf.sh deleted +0 -4
deleted file mode 100755
index b59b8ba561c3..000000000000
@@ -1,4 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-# Tests the scanf infrastructure using test_scanf kernel module.
-$(dirname $0)/../kselftest/module.sh "scanf" test_scanf
Signature · 1 line
2.48.1
#2 lore raw
Quoted 40 lines · Tamir Duberstein

Convert the scanf() self-test to a KUnit test.

In the interest of keeping the patch reasonably-sized this doesn't refactor the tests into proper parameterized tests - it's all one big test case.

---
 MAINTAINERS                          |   2 +-
 lib/Kconfig.debug                    |  12 +-
 lib/Makefile                         |   2 +-
 lib/{test_scanf.c => scanf_kunit.c}  | 228 ++++++++++++++++-------------------
 tools/testing/selftests/lib/Makefile |   2 +-
 tools/testing/selftests/lib/config   |   1 -
 tools/testing/selftests/lib/scanf.sh |   4 -
 7 files changed, 118 insertions(+), 133 deletions(-)
MAINTAINERS +1 -1
index 25c86f47353d..ab1d90c66c02 100644
@@ -25411,8 +25411,8 @@ R:      Sergey Senozhatsky <[email protected]>
 S:     Maintained
 T:     git git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git
 F:     Documentation/core-api/printk-formats.rst
+F:     lib/scanf_kunit.c
 F:     lib/test_printf.c
-F:     lib/test_scanf.c
 F:     lib/vsprintf.c
 
 VT1211 HARDWARE MONITOR DRIVER
lib/Kconfig.debug +0 -0
index 1af972a92d06..d35b60203856 100644
@@ -2427,6 +2427,15 @@ config ASYNC_RAID6_TEST
 config TEST_HEXDUMP

lib/Kconfig.debug config ASYNC_RAID6_TEST · line 2437

Quoted 10 lines
lib/Kconfig.debug +8 -0
        tristate "Test functions located in the hexdump module at runtime"
 
+config SCANF_KUNIT_TEST
+       tristate "KUnit test scanf() family of functions at runtime" if !KUNIT_ALL_TESTS
+       depends on KUNIT
+       default KUNIT_ALL_TESTS
+       help
+         Enable this option to test the scanf functions at boot.
+
+         If unsure, say N.

Ugh, I meant to change this "at boot" to "at runtime".

Petr, if you are willing to take this through printf and apply this tiny edit, I'd really appreciate it.

#3 lore raw

PATCH v5 2/2 scanf: break kunit into test cases

In reply to Tamir Duberstein

Use suite_init and move some tests into scanf_test_cases. This gives us nicer output in the event of a failure.

---
 lib/scanf_kunit.c | 98 +++++++++++++++++++++++++------------------------------
 1 file changed, 44 insertions(+), 54 deletions(-)
lib/scanf_kunit.c +44 -54
index dfd29b103053..cbbbcc1c88d2 100644
@@ -4,19 +4,14 @@
  */
 
 #include <kunit/test.h>
-#include <linux/bitops.h>
-#include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/overflow.h>
-#include <linux/printk.h>
 #include <linux/prandom.h>
-#include <linux/slab.h>
-#include <linux/string.h>
+#include <linux/sprintf.h>
 
 #define BUF_SIZE 1024
 
-static char *test_buffer;
-static char *fmt_buffer;
+static char test_buffer[BUF_SIZE];
+static char fmt_buffer[BUF_SIZE];
 static struct rnd_state rnd_state;
 
 typedef void (*check_fn)(struct kunit *test, const void *check_data, const char *string,
@@ -428,8 +423,11 @@ static void numbers_list_hh(struct kunit *test, const char *delim)
 	numbers_list_8(signed char,	   "0x%hhx", delim, "hhi", check_char);
 }
 
-static void numbers_list(struct kunit *test, const char *delim)
+static void numbers_list(struct kunit *test)
 {
+	const char * const *param = test->param_value;
+	const char *delim = *param;
+
 	numbers_list_ll(test, delim);
 	numbers_list_l(test, delim);
 	numbers_list_d(test, delim);
@@ -500,8 +498,11 @@ static void numbers_list_field_width_hh(struct kunit *test, const char *delim)
  * List of numbers separated by delim. Each field width specifier is the
  * maximum possible digits for the given type and base.
  */
-static void numbers_list_field_width_typemax(struct kunit *test, const char *delim)
+static void numbers_list_field_width_typemax(struct kunit *test)
 {
+	const char * const *param = test->param_value;
+	const char *delim = *param;
+
 	numbers_list_field_width_ll(test, delim);
 	numbers_list_field_width_l(test, delim);
 	numbers_list_field_width_d(test, delim);
@@ -563,8 +564,11 @@ static void numbers_list_field_width_val_hh(struct kunit *test, const char *deli
  * List of numbers separated by delim. Each field width specifier is the
  * exact length of the corresponding value digits in the string being scanned.
  */
-static void numbers_list_field_width_val_width(struct kunit *test, const char *delim)
+static void numbers_list_field_width_val_width(struct kunit *test)
 {
+	const char * const *param = test->param_value;
+	const char *delim = *param;
+
 	numbers_list_field_width_val_ll(test, delim);
 	numbers_list_field_width_val_l(test, delim);
 	numbers_list_field_width_val_d(test, delim);
@@ -580,7 +584,12 @@ static void numbers_list_field_width_val_width(struct kunit *test, const char *d
  */
 static void numbers_slice(struct kunit *test)
 {
-	numbers_list_field_width_val_width(test, "");
+	const char *delim = "";
+
+	KUNIT_ASSERT_PTR_EQ(test, test->param_value, NULL);
+	test->param_value = &delim;
+
+	numbers_list_field_width_val_width(test);
 }
 
 #define test_number_prefix(T, str, scan_fmt, expect0, expect1, n_args, fn)	\
@@ -732,62 +741,43 @@ static const char * const number_delimiters[] = {
 	" ", ":", ",", "-", "/",
 };
 
-static void test_numbers(struct kunit *test)
+static void number_delimiter_param_desc(const char * const *param,
+					   char *desc)
 {
-	int i;
+	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "\"%s\"", *param);
+}
 
-	/* String containing only one number. */
-	numbers_simple(test);
+KUNIT_ARRAY_PARAM(number_delimiters, number_delimiters, number_delimiter_param_desc);
 
+static struct kunit_case scanf_test_cases[] = {
+	KUNIT_CASE(numbers_simple),
 	/* String with multiple numbers separated by delimiter. */
-	for (i = 0; i < ARRAY_SIZE(number_delimiters); i++) {
-		numbers_list(test, number_delimiters[i]);
-
-		/* Field width may be longer than actual field digits. */
-		numbers_list_field_width_typemax(test, number_delimiters[i]);
-
-		/* Each field width exactly length of actual field digits. */
-		numbers_list_field_width_val_width(test, number_delimiters[i]);
-	}
-
+	KUNIT_CASE_PARAM(numbers_list, number_delimiters_gen_params),
+	/* Field width may be longer than actual field digits. */
+	KUNIT_CASE_PARAM(numbers_list_field_width_typemax, number_delimiters_gen_params),
+	/* Each field width exactly length of actual field digits. */
+	KUNIT_CASE_PARAM(numbers_list_field_width_val_width, number_delimiters_gen_params),
 	/* Slice continuous sequence of digits using field widths. */
-	numbers_slice(test);
+	KUNIT_CASE(numbers_slice),
+	KUNIT_CASE(numbers_prefix_overflow),
 
-	numbers_prefix_overflow(test);
-}
+	KUNIT_CASE(test_simple_strtoull),
+	KUNIT_CASE(test_simple_strtoll),
+	KUNIT_CASE(test_simple_strtoul),
+	KUNIT_CASE(test_simple_strtol),
+	{}
+};
 
-static void scanf_test(struct kunit *test)
+static int scanf_suite_init(struct kunit_suite *suite)
 {
-	test_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
-	if (!test_buffer)
-		return;
-
-	fmt_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
-	if (!fmt_buffer) {
-		kfree(test_buffer);
-		return;
-	}
-
 	prandom_seed_state(&rnd_state, 3141592653589793238ULL);
 
-	test_numbers(test);
-
-	test_simple_strtoull(test);
-	test_simple_strtoll(test);
-	test_simple_strtoul(test);
-	test_simple_strtol(test);
-
-	kfree(fmt_buffer);
-	kfree(test_buffer);
+	return 0;
 }
 
-static struct kunit_case scanf_test_cases[] = {
-	KUNIT_CASE(scanf_test),
-	{}
-};
-
 static struct kunit_suite scanf_test_suite = {
 	.name = "scanf",
+	.suite_init = scanf_suite_init,
 	.test_cases = scanf_test_cases,
 };
 
Signature · 1 line
2.48.1
#4 lore raw

Re: [PATCH v5 2/2] scanf: break kunit into test cases

Hi Tamir,

Quoted 5 lines · Tamir Duberstein

Use suite_init and move some tests into scanf_test_cases. This gives us nicer output in the event of a failure.

Thanks for your patch!

Quoted 12 lines
lib/scanf_kunit.c +0 -4
@@ -4,19 +4,14 @@
  */
 
 #include <kunit/test.h>
-#include <linux/bitops.h>
-#include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/overflow.h>
-#include <linux/printk.h>
 #include <linux/prandom.h>

lib/scanf_kunit.c line 14

Quoted 10 lines
lib/scanf_kunit.c +3 -4
-#include <linux/slab.h>
-#include <linux/string.h>
+#include <linux/sprintf.h>
 
 #define BUF_SIZE 1024
 
-static char *test_buffer;
-static char *fmt_buffer;
+static char test_buffer[BUF_SIZE];
+static char fmt_buffer[BUF_SIZE];

Why? This wastes 2 KiB of memory when the test is not running.

lib/scanf_kunit.c line 17

Quoted 3 lines
lib/scanf_kunit.c +0 -0
 static struct rnd_state rnd_state;
 
 typedef void (*check_fn)(struct kunit *test, const void *check_data, const char *string,

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
#5 lore raw

Re: [PATCH v5 2/2] scanf: break kunit into test cases

Quoted 9 lines · Geert Uytterhoeven

lib/scanf_kunit.c line 14

Quoted 7 lines

[...]

lib/scanf_kunit.c +2 -2
-static char *test_buffer;
-static char *fmt_buffer;
+static char test_buffer[BUF_SIZE];
+static char fmt_buffer[BUF_SIZE];

Why? This wastes 2 KiB of memory when the test is not running.

It's a fair point. I guess I'll respin v6 after all and go back to kmalloc.

Thanks for reviewing!