Skip to content

Commit 06961fb

Browse files
committed
Merge tag 'move-lib-kunit-v6.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull lib kunit selftest move from Kees Cook: "This is a one-off tree to coordinate the move of selftests out of lib/ and into lib/tests/. A separate tree was used for this to keep the paths sane with all the work in the same place. - move lib/ selftests into lib/tests/ (Kees Cook, Gabriela Bittencourt, Luis Felipe Hernandez, Lukas Bulwahn, Tamir Duberstein) - lib/math: Add int_log test suite (Bruno Sobreira França) - lib/math: Add Kunit test suite for gcd() (Yu-Chun Lin) - lib/tests/kfifo_kunit.c: add tests for the kfifo structure (Diego Vieira) - unicode: refactor selftests into KUnit (Gabriela Bittencourt) - lib/prime_numbers: convert self-test to KUnit (Tamir Duberstein) - printf: convert self-test to KUnit (Tamir Duberstein) - scanf: convert self-test to KUnit (Tamir Duberstein)" * tag 'move-lib-kunit-v6.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: (21 commits) scanf: break kunit into test cases scanf: convert self-test to KUnit scanf: remove redundant debug logs scanf: implicate test line in failure messages printf: implicate test line in failure messages printf: break kunit into test cases printf: convert self-test to KUnit kunit/fortify: Replace "volatile" with OPTIMIZER_HIDE_VAR() kunit/fortify: Expand testing of __compiletime_strlen() kunit/stackinit: Use fill byte different from Clang i386 pattern kunit/overflow: Fix DEFINE_FLEX tests for counted_by selftests: remove reference to prime_numbers.sh MAINTAINERS: adjust entries in FORTIFY_SOURCE and KERNEL HARDENING lib/prime_numbers: convert self-test to KUnit lib/math: Add Kunit test suite for gcd() unicode: kunit: change tests filename and path unicode: kunit: refactor selftest to kunit tests lib/tests/kfifo_kunit.c: add tests for the kfifo structure lib: Move KUnit tests into tests/ subdirectory lib/math: Add int_log test suite ...
2 parents 4f773fc + d62f8c9 commit 06961fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1148
-722
lines changed

Documentation/core-api/printk-formats.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ Do *not* use it from C.
661661
Thanks
662662
======
663663

664-
If you add other %p extensions, please extend <lib/test_printf.c> with
665-
one or more test cases, if at all feasible.
664+
If you add other %p extensions, please extend <lib/tests/printf_kunit.c>
665+
with one or more test cases, if at all feasible.
666666

667667
Thank you for your cooperation and attention.

Documentation/dev-tools/kselftest.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ kselftest. We use kselftests for lib/ as an example.
347347
1. Create the test module
348348

349349
2. Create the test script that will run (load/unload) the module
350-
e.g. ``tools/testing/selftests/lib/printf.sh``
350+
e.g. ``tools/testing/selftests/lib/bitmap.sh``
351351

352352
3. Add line to config file e.g. ``tools/testing/selftests/lib/config``
353353

MAINTAINERS

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4014,10 +4014,10 @@ F: include/vdso/bits.h
40144014
F: lib/bitmap-str.c
40154015
F: lib/bitmap.c
40164016
F: lib/cpumask.c
4017-
F: lib/cpumask_kunit.c
40184017
F: lib/find_bit.c
40194018
F: lib/find_bit_benchmark.c
40204019
F: lib/test_bitmap.c
4020+
F: lib/tests/cpumask_kunit.c
40214021
F: tools/include/linux/bitfield.h
40224022
F: tools/include/linux/bitmap.h
40234023
F: tools/include/linux/bits.h
@@ -9075,9 +9075,9 @@ L: [email protected]
90759075
S: Supported
90769076
T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening
90779077
F: include/linux/fortify-string.h
9078-
F: lib/fortify_kunit.c
9079-
F: lib/memcpy_kunit.c
90809078
F: lib/test_fortify/*
9079+
F: lib/tests/fortify_kunit.c
9080+
F: lib/tests/memcpy_kunit.c
90819081
K: \bunsafe_memcpy\b
90829082
K: \b__NO_FORTIFY\b
90839083

@@ -9762,9 +9762,9 @@ F: include/linux/string.h
97629762
F: include/linux/string_choices.h
97639763
F: include/linux/string_helpers.h
97649764
F: lib/string.c
9765-
F: lib/string_kunit.c
97669765
F: lib/string_helpers.c
9767-
F: lib/string_helpers_kunit.c
9766+
F: lib/tests/string_helpers_kunit.c
9767+
F: lib/tests/string_kunit.c
97689768
F: scripts/coccinelle/api/string_choices.cocci
97699769

97709770
GENERIC UIO DRIVER FOR PCI DEVICES
@@ -12592,7 +12592,7 @@ F: arch/*/configs/hardening.config
1259212592
F: include/linux/overflow.h
1259312593
F: include/linux/randomize_kstack.h
1259412594
F: kernel/configs/hardening.config
12595-
F: lib/usercopy_kunit.c
12595+
F: lib/tests/usercopy_kunit.c
1259612596
F: mm/usercopy.c
1259712597
F: security/Kconfig.hardening
1259812598
K: \b(add|choose)_random_kstack_offset\b
@@ -12990,7 +12990,7 @@ F: Documentation/trace/kprobes.rst
1299012990
F: include/asm-generic/kprobes.h
1299112991
F: include/linux/kprobes.h
1299212992
F: kernel/kprobes.c
12993-
F: lib/test_kprobes.c
12993+
F: lib/tests/test_kprobes.c
1299412994
F: samples/kprobes
1299512995

1299612996
KS0108 LCD CONTROLLER DRIVER
@@ -13320,7 +13320,7 @@ M: Mark Brown <[email protected]>
1332013320
R: Matti Vaittinen <[email protected]>
1332113321
F: include/linux/linear_range.h
1332213322
F: lib/linear_ranges.c
13323-
F: lib/test_linear_ranges.c
13323+
F: lib/tests/test_linear_ranges.c
1332413324

1332513325
LINUX FOR POWER MACINTOSH
1332613326
@@ -13448,7 +13448,7 @@ M: David Gow <[email protected]>
1344813448
1344913449
1345013450
S: Maintained
13451-
F: lib/list-test.c
13451+
F: lib/tests/list-test.c
1345213452

1345313453
LITEX PLATFORM
1345413454
M: Karol Gugala <[email protected]>
@@ -21778,7 +21778,7 @@ M: Jason A. Donenfeld <[email protected]>
2177821778
S: Maintained
2177921779
F: include/linux/siphash.h
2178021780
F: lib/siphash.c
21781-
F: lib/siphash_kunit.c
21781+
F: lib/tests/siphash_kunit.c
2178221782

2178321783
SIS 190 ETHERNET DRIVER
2178421784
M: Francois Romieu <[email protected]>
@@ -25453,8 +25453,8 @@ R: Sergey Senozhatsky <[email protected]>
2545325453
S: Maintained
2545425454
T: git git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git
2545525455
F: Documentation/core-api/printk-formats.rst
25456-
F: lib/test_printf.c
25457-
F: lib/test_scanf.c
25456+
F: lib/tests/printf_kunit.c
25457+
F: lib/tests/scanf_kunit.c
2545825458
F: lib/vsprintf.c
2545925459

2546025460
VT1211 HARDWARE MONITOR DRIVER

fs/unicode/Kconfig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ config UNICODE
1010
be a separate loadable module that gets requested only when a file
1111
system actually use it.
1212

13-
config UNICODE_NORMALIZATION_SELFTEST
13+
config UNICODE_NORMALIZATION_KUNIT_TEST
1414
tristate "Test UTF-8 normalization support"
15-
depends on UNICODE
15+
depends on UNICODE && KUNIT
16+
default KUNIT_ALL_TESTS

fs/unicode/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ifneq ($(CONFIG_UNICODE),)
44
obj-y += unicode.o
55
endif
66
obj-$(CONFIG_UNICODE) += utf8data.o
7-
obj-$(CONFIG_UNICODE_NORMALIZATION_SELFTEST) += utf8-selftest.o
7+
obj-$(CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST) += tests/utf8_kunit.o
88

99
unicode-y := utf8-norm.o utf8-core.o
1010

fs/unicode/tests/.kunitconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_KUNIT=y
2+
CONFIG_UNICODE=y
3+
CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST=y

fs/unicode/utf8-selftest.c renamed to fs/unicode/tests/utf8_kunit.c

Lines changed: 71 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
/*
3-
* Kernel module for testing utf-8 support.
3+
* KUnit tests for utf-8 support.
44
*
55
* Copyright 2017 Collabora Ltd.
66
*/
77

8-
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9-
10-
#include <linux/module.h>
11-
#include <linux/printk.h>
128
#include <linux/unicode.h>
13-
#include <linux/dcache.h>
14-
15-
#include "utf8n.h"
16-
17-
static unsigned int failed_tests;
18-
static unsigned int total_tests;
19-
20-
#define _test(cond, func, line, fmt, ...) do { \
21-
total_tests++; \
22-
if (!cond) { \
23-
failed_tests++; \
24-
pr_err("test %s:%d Failed: %s%s", \
25-
func, line, #cond, (fmt?":":".")); \
26-
if (fmt) \
27-
pr_err(fmt, ##__VA_ARGS__); \
28-
} \
29-
} while (0)
30-
#define test_f(cond, fmt, ...) _test(cond, __func__, __LINE__, fmt, ##__VA_ARGS__)
31-
#define test(cond) _test(cond, __func__, __LINE__, "")
9+
#include <kunit/test.h>
10+
11+
#include "../utf8n.h"
3212

3313
static const struct {
3414
/* UTF-8 strings in this vector _must_ be NULL-terminated. */
@@ -167,78 +147,84 @@ static int utf8cursor(struct utf8cursor *u8c, const struct unicode_map *um,
167147
return utf8ncursor(u8c, um, n, s, (unsigned int)-1);
168148
}
169149

170-
static void check_utf8_nfdi(struct unicode_map *um)
150+
static void check_utf8_nfdi(struct kunit *test)
171151
{
172152
int i;
173153
struct utf8cursor u8c;
154+
struct unicode_map *um = test->priv;
174155

175156
for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) {
176157
int len = strlen(nfdi_test_data[i].str);
177158
int nlen = strlen(nfdi_test_data[i].dec);
178159
int j = 0;
179160
unsigned char c;
161+
int ret;
162+
163+
KUNIT_EXPECT_EQ(test, utf8len(um, UTF8_NFDI, nfdi_test_data[i].str), nlen);
164+
KUNIT_EXPECT_EQ(test, utf8nlen(um, UTF8_NFDI, nfdi_test_data[i].str, len),
165+
nlen);
180166

181-
test((utf8len(um, UTF8_NFDI, nfdi_test_data[i].str) == nlen));
182-
test((utf8nlen(um, UTF8_NFDI, nfdi_test_data[i].str, len) ==
183-
nlen));
184167

185-
if (utf8cursor(&u8c, um, UTF8_NFDI, nfdi_test_data[i].str) < 0)
186-
pr_err("can't create cursor\n");
168+
ret = utf8cursor(&u8c, um, UTF8_NFDI, nfdi_test_data[i].str);
169+
KUNIT_EXPECT_TRUE_MSG(test, ret >= 0, "Can't create cursor\n");
187170

188171
while ((c = utf8byte(&u8c)) > 0) {
189-
test_f((c == nfdi_test_data[i].dec[j]),
190-
"Unexpected byte 0x%x should be 0x%x\n",
191-
c, nfdi_test_data[i].dec[j]);
172+
KUNIT_EXPECT_EQ_MSG(test, c, nfdi_test_data[i].dec[j],
173+
"Unexpected byte 0x%x should be 0x%x\n",
174+
c, nfdi_test_data[i].dec[j]);
192175
j++;
193176
}
194177

195-
test((j == nlen));
178+
KUNIT_EXPECT_EQ(test, j, nlen);
196179
}
197180
}
198181

199-
static void check_utf8_nfdicf(struct unicode_map *um)
182+
static void check_utf8_nfdicf(struct kunit *test)
200183
{
201184
int i;
202185
struct utf8cursor u8c;
186+
struct unicode_map *um = test->priv;
203187

204188
for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) {
205189
int len = strlen(nfdicf_test_data[i].str);
206190
int nlen = strlen(nfdicf_test_data[i].ncf);
207191
int j = 0;
192+
int ret;
208193
unsigned char c;
209194

210-
test((utf8len(um, UTF8_NFDICF, nfdicf_test_data[i].str) ==
211-
nlen));
212-
test((utf8nlen(um, UTF8_NFDICF, nfdicf_test_data[i].str, len) ==
213-
nlen));
195+
KUNIT_EXPECT_EQ(test, utf8len(um, UTF8_NFDICF, nfdicf_test_data[i].str),
196+
nlen);
197+
KUNIT_EXPECT_EQ(test, utf8nlen(um, UTF8_NFDICF, nfdicf_test_data[i].str, len),
198+
nlen);
214199

215-
if (utf8cursor(&u8c, um, UTF8_NFDICF,
216-
nfdicf_test_data[i].str) < 0)
217-
pr_err("can't create cursor\n");
200+
ret = utf8cursor(&u8c, um, UTF8_NFDICF, nfdicf_test_data[i].str);
201+
KUNIT_EXPECT_TRUE_MSG(test, ret >= 0, "Can't create cursor\n");
218202

219203
while ((c = utf8byte(&u8c)) > 0) {
220-
test_f((c == nfdicf_test_data[i].ncf[j]),
221-
"Unexpected byte 0x%x should be 0x%x\n",
222-
c, nfdicf_test_data[i].ncf[j]);
204+
KUNIT_EXPECT_EQ_MSG(test, c, nfdicf_test_data[i].ncf[j],
205+
"Unexpected byte 0x%x should be 0x%x\n",
206+
c, nfdicf_test_data[i].ncf[j]);
223207
j++;
224208
}
225209

226-
test((j == nlen));
210+
KUNIT_EXPECT_EQ(test, j, nlen);
227211
}
228212
}
229213

230-
static void check_utf8_comparisons(struct unicode_map *table)
214+
static void check_utf8_comparisons(struct kunit *test)
231215
{
232216
int i;
217+
struct unicode_map *um = test->priv;
233218

234219
for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) {
235220
const struct qstr s1 = {.name = nfdi_test_data[i].str,
236221
.len = sizeof(nfdi_test_data[i].str)};
237222
const struct qstr s2 = {.name = nfdi_test_data[i].dec,
238223
.len = sizeof(nfdi_test_data[i].dec)};
239224

240-
test_f(!utf8_strncmp(table, &s1, &s2),
241-
"%s %s comparison mismatch\n", s1.name, s2.name);
225+
/* strncmp returns 0 when strings are equal */
226+
KUNIT_EXPECT_TRUE_MSG(test, utf8_strncmp(um, &s1, &s2) == 0,
227+
"%s %s comparison mismatch\n", s1.name, s2.name);
242228
}
243229

244230
for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) {
@@ -247,62 +233,65 @@ static void check_utf8_comparisons(struct unicode_map *table)
247233
const struct qstr s2 = {.name = nfdicf_test_data[i].ncf,
248234
.len = sizeof(nfdicf_test_data[i].ncf)};
249235

250-
test_f(!utf8_strncasecmp(table, &s1, &s2),
251-
"%s %s comparison mismatch\n", s1.name, s2.name);
236+
/* strncasecmp returns 0 when strings are equal */
237+
KUNIT_EXPECT_TRUE_MSG(test, utf8_strncasecmp(um, &s1, &s2) == 0,
238+
"%s %s comparison mismatch\n", s1.name, s2.name);
252239
}
253240
}
254241

255-
static void check_supported_versions(struct unicode_map *um)
242+
static void check_supported_versions(struct kunit *test)
256243
{
244+
struct unicode_map *um = test->priv;
257245
/* Unicode 7.0.0 should be supported. */
258-
test(utf8version_is_supported(um, UNICODE_AGE(7, 0, 0)));
246+
KUNIT_EXPECT_TRUE(test, utf8version_is_supported(um, UNICODE_AGE(7, 0, 0)));
259247

260248
/* Unicode 9.0.0 should be supported. */
261-
test(utf8version_is_supported(um, UNICODE_AGE(9, 0, 0)));
249+
KUNIT_EXPECT_TRUE(test, utf8version_is_supported(um, UNICODE_AGE(9, 0, 0)));
262250

263251
/* Unicode 1x.0.0 (the latest version) should be supported. */
264-
test(utf8version_is_supported(um, UTF8_LATEST));
252+
KUNIT_EXPECT_TRUE(test, utf8version_is_supported(um, UTF8_LATEST));
265253

266254
/* Next versions don't exist. */
267-
test(!utf8version_is_supported(um, UNICODE_AGE(13, 0, 0)));
268-
test(!utf8version_is_supported(um, UNICODE_AGE(0, 0, 0)));
269-
test(!utf8version_is_supported(um, UNICODE_AGE(-1, -1, -1)));
255+
KUNIT_EXPECT_FALSE(test, utf8version_is_supported(um, UNICODE_AGE(13, 0, 0)));
256+
KUNIT_EXPECT_FALSE(test, utf8version_is_supported(um, UNICODE_AGE(0, 0, 0)));
257+
KUNIT_EXPECT_FALSE(test, utf8version_is_supported(um, UNICODE_AGE(-1, -1, -1)));
270258
}
271259

272-
static int __init init_test_ucd(void)
260+
static struct kunit_case unicode_normalization_test_cases[] = {
261+
KUNIT_CASE(check_supported_versions),
262+
KUNIT_CASE(check_utf8_comparisons),
263+
KUNIT_CASE(check_utf8_nfdicf),
264+
KUNIT_CASE(check_utf8_nfdi),
265+
{}
266+
};
267+
268+
static int init_test_ucd(struct kunit *test)
273269
{
274-
struct unicode_map *um;
270+
struct unicode_map *um = utf8_load(UTF8_LATEST);
275271

276-
failed_tests = 0;
277-
total_tests = 0;
272+
test->priv = um;
278273

279-
um = utf8_load(UTF8_LATEST);
280-
if (IS_ERR(um)) {
281-
pr_err("%s: Unable to load utf8 table.\n", __func__);
282-
return PTR_ERR(um);
283-
}
274+
KUNIT_EXPECT_EQ_MSG(test, IS_ERR(um), 0,
275+
"%s: Unable to load utf8 table.\n", __func__);
284276

285-
check_supported_versions(um);
286-
check_utf8_nfdi(um);
287-
check_utf8_nfdicf(um);
288-
check_utf8_comparisons(um);
289-
290-
if (!failed_tests)
291-
pr_info("All %u tests passed\n", total_tests);
292-
else
293-
pr_err("%u out of %u tests failed\n", failed_tests,
294-
total_tests);
295-
utf8_unload(um);
296277
return 0;
297278
}
298279

299-
static void __exit exit_test_ucd(void)
280+
static void exit_test_ucd(struct kunit *test)
300281
{
282+
utf8_unload(test->priv);
301283
}
302284

303-
module_init(init_test_ucd);
304-
module_exit(exit_test_ucd);
285+
static struct kunit_suite unicode_normalization_test_suite = {
286+
.name = "unicode_normalization",
287+
.test_cases = unicode_normalization_test_cases,
288+
.init = init_test_ucd,
289+
.exit = exit_test_ucd,
290+
};
291+
292+
kunit_test_suite(unicode_normalization_test_suite);
293+
305294

306295
MODULE_AUTHOR("Gabriel Krisman Bertazi <[email protected]>");
307-
MODULE_DESCRIPTION("Kernel module for testing utf-8 support");
296+
MODULE_DESCRIPTION("KUnit tests for utf-8 support.");
308297
MODULE_LICENSE("GPL");

fs/unicode/utf8-norm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ int utf8byte(struct utf8cursor *u8c)
586586
}
587587
}
588588

589-
#ifdef CONFIG_UNICODE_NORMALIZATION_SELFTEST_MODULE
589+
#if IS_MODULE(CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST)
590590
EXPORT_SYMBOL_GPL(utf8version_is_supported);
591591
EXPORT_SYMBOL_GPL(utf8nlen);
592592
EXPORT_SYMBOL_GPL(utf8ncursor);

0 commit comments

Comments
 (0)