Skip to content

Commit fb3d88a

Browse files
committed
siphash: Convert selftest to KUnit
Convert the siphash self-test to KUnit so it will be included in "all KUnit tests" coverage, and can be run individually still: $ ./tools/testing/kunit/kunit.py run siphash ... [02:58:45] Starting KUnit Kernel (1/1)... [02:58:45] ============================================================ [02:58:45] =================== siphash (1 subtest) ==================== [02:58:45] [PASSED] siphash_test [02:58:45] ===================== [PASSED] siphash ===================== [02:58:45] ============================================================ [02:58:45] Testing complete. Ran 1 tests: passed: 1 [02:58:45] Elapsed time: 21.421s total, 4.306s configuring, 16.947s building, 0.148s running Cc: Vlastimil Babka <[email protected]> Cc: "Steven Rostedt (Google)" <[email protected]> Cc: Yury Norov <[email protected]> Cc: Sander Vanheule <[email protected]> Acked-by: "Jason A. Donenfeld" <[email protected]> Link: https://lore.kernel.org/lkml/CAHmME9r+9MPH6zk3Vn=buEMSbQiWMFryqqzerKarmjYk+tHLJA@mail.gmail.com Tested-by: David Gow <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 62e1cbf commit fb3d88a

File tree

4 files changed

+83
-106
lines changed

4 files changed

+83
-106
lines changed

MAINTAINERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -18864,7 +18864,7 @@ M: Jason A. Donenfeld <[email protected]>
1886418864
S: Maintained
1886518865
F: include/linux/siphash.h
1886618866
F: lib/siphash.c
18867-
F: lib/test_siphash.c
18867+
F: lib/siphash_kunit.c
1886818868

1886918869
SIS 190 ETHERNET DRIVER
1887018870
M: Francois Romieu <[email protected]>

lib/Kconfig.debug

+11-9
Original file line numberDiff line numberDiff line change
@@ -2244,15 +2244,6 @@ config TEST_RHASHTABLE
22442244

22452245
If unsure, say N.
22462246

2247-
config TEST_SIPHASH
2248-
tristate "Perform selftest on siphash functions"
2249-
help
2250-
Enable this option to test the kernel's siphash (<linux/siphash.h>) hash
2251-
functions on boot (or module load).
2252-
2253-
This is intended to help people writing architecture-specific
2254-
optimized versions. If unsure, say N.
2255-
22562247
config TEST_IDA
22572248
tristate "Perform selftest on IDA functions"
22582249

@@ -2585,6 +2576,17 @@ config STRSCPY_KUNIT_TEST
25852576
depends on KUNIT
25862577
default KUNIT_ALL_TESTS
25872578

2579+
config SIPHASH_KUNIT_TEST
2580+
tristate "Perform selftest on siphash functions" if !KUNIT_ALL_TESTS
2581+
depends on KUNIT
2582+
default KUNIT_ALL_TESTS
2583+
help
2584+
Enable this option to test the kernel's siphash (<linux/siphash.h>) hash
2585+
functions on boot (or module load).
2586+
2587+
This is intended to help people writing architecture-specific
2588+
optimized versions. If unsure, say N.
2589+
25882590
config TEST_UDELAY
25892591
tristate "udelay test driver"
25902592
help

lib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ obj-$(CONFIG_TEST_BITOPS) += test_bitops.o
6262
CFLAGS_test_bitops.o += -Werror
6363
obj-$(CONFIG_CPUMASK_KUNIT_TEST) += cpumask_kunit.o
6464
obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
65-
obj-$(CONFIG_TEST_SIPHASH) += test_siphash.o
6665
obj-$(CONFIG_HASH_KUNIT_TEST) += test_hash.o
6766
obj-$(CONFIG_TEST_IDA) += test_ida.o
6867
obj-$(CONFIG_TEST_UBSAN) += test_ubsan.o
@@ -380,6 +379,7 @@ CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable)
380379
obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
381380
obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
382381
obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o
382+
obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o
383383

384384
obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
385385

lib/test_siphash.c renamed to lib/siphash_kunit.c

+70-95
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1515

16+
#include <kunit/test.h>
1617
#include <linux/siphash.h>
1718
#include <linux/kernel.h>
1819
#include <linux/string.h>
@@ -109,114 +110,88 @@ static const u32 test_vectors_hsiphash[64] = {
109110
};
110111
#endif
111112

112-
static int __init siphash_test_init(void)
113+
#define chk(hash, vector, fmt...) \
114+
KUNIT_EXPECT_EQ_MSG(test, hash, vector, fmt)
115+
116+
static void siphash_test(struct kunit *test)
113117
{
114118
u8 in[64] __aligned(SIPHASH_ALIGNMENT);
115119
u8 in_unaligned[65] __aligned(SIPHASH_ALIGNMENT);
116120
u8 i;
117-
int ret = 0;
118121

119122
for (i = 0; i < 64; ++i) {
120123
in[i] = i;
121124
in_unaligned[i + 1] = i;
122-
if (siphash(in, i, &test_key_siphash) !=
123-
test_vectors_siphash[i]) {
124-
pr_info("siphash self-test aligned %u: FAIL\n", i + 1);
125-
ret = -EINVAL;
126-
}
127-
if (siphash(in_unaligned + 1, i, &test_key_siphash) !=
128-
test_vectors_siphash[i]) {
129-
pr_info("siphash self-test unaligned %u: FAIL\n", i + 1);
130-
ret = -EINVAL;
131-
}
132-
if (hsiphash(in, i, &test_key_hsiphash) !=
133-
test_vectors_hsiphash[i]) {
134-
pr_info("hsiphash self-test aligned %u: FAIL\n", i + 1);
135-
ret = -EINVAL;
136-
}
137-
if (hsiphash(in_unaligned + 1, i, &test_key_hsiphash) !=
138-
test_vectors_hsiphash[i]) {
139-
pr_info("hsiphash self-test unaligned %u: FAIL\n", i + 1);
140-
ret = -EINVAL;
141-
}
142-
}
143-
if (siphash_1u64(0x0706050403020100ULL, &test_key_siphash) !=
144-
test_vectors_siphash[8]) {
145-
pr_info("siphash self-test 1u64: FAIL\n");
146-
ret = -EINVAL;
147-
}
148-
if (siphash_2u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
149-
&test_key_siphash) != test_vectors_siphash[16]) {
150-
pr_info("siphash self-test 2u64: FAIL\n");
151-
ret = -EINVAL;
125+
chk(siphash(in, i, &test_key_siphash),
126+
test_vectors_siphash[i],
127+
"siphash self-test aligned %u: FAIL", i + 1);
128+
chk(siphash(in_unaligned + 1, i, &test_key_siphash),
129+
test_vectors_siphash[i],
130+
"siphash self-test unaligned %u: FAIL", i + 1);
131+
chk(hsiphash(in, i, &test_key_hsiphash),
132+
test_vectors_hsiphash[i],
133+
"hsiphash self-test aligned %u: FAIL", i + 1);
134+
chk(hsiphash(in_unaligned + 1, i, &test_key_hsiphash),
135+
test_vectors_hsiphash[i],
136+
"hsiphash self-test unaligned %u: FAIL", i + 1);
152137
}
153-
if (siphash_3u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
154-
0x1716151413121110ULL, &test_key_siphash) !=
155-
test_vectors_siphash[24]) {
156-
pr_info("siphash self-test 3u64: FAIL\n");
157-
ret = -EINVAL;
158-
}
159-
if (siphash_4u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
138+
chk(siphash_1u64(0x0706050403020100ULL, &test_key_siphash),
139+
test_vectors_siphash[8],
140+
"siphash self-test 1u64: FAIL");
141+
chk(siphash_2u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
142+
&test_key_siphash),
143+
test_vectors_siphash[16],
144+
"siphash self-test 2u64: FAIL");
145+
chk(siphash_3u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
146+
0x1716151413121110ULL, &test_key_siphash),
147+
test_vectors_siphash[24],
148+
"siphash self-test 3u64: FAIL");
149+
chk(siphash_4u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
160150
0x1716151413121110ULL, 0x1f1e1d1c1b1a1918ULL,
161-
&test_key_siphash) != test_vectors_siphash[32]) {
162-
pr_info("siphash self-test 4u64: FAIL\n");
163-
ret = -EINVAL;
164-
}
165-
if (siphash_1u32(0x03020100U, &test_key_siphash) !=
166-
test_vectors_siphash[4]) {
167-
pr_info("siphash self-test 1u32: FAIL\n");
168-
ret = -EINVAL;
169-
}
170-
if (siphash_2u32(0x03020100U, 0x07060504U, &test_key_siphash) !=
171-
test_vectors_siphash[8]) {
172-
pr_info("siphash self-test 2u32: FAIL\n");
173-
ret = -EINVAL;
174-
}
175-
if (siphash_3u32(0x03020100U, 0x07060504U,
176-
0x0b0a0908U, &test_key_siphash) !=
177-
test_vectors_siphash[12]) {
178-
pr_info("siphash self-test 3u32: FAIL\n");
179-
ret = -EINVAL;
180-
}
181-
if (siphash_4u32(0x03020100U, 0x07060504U,
182-
0x0b0a0908U, 0x0f0e0d0cU, &test_key_siphash) !=
183-
test_vectors_siphash[16]) {
184-
pr_info("siphash self-test 4u32: FAIL\n");
185-
ret = -EINVAL;
186-
}
187-
if (hsiphash_1u32(0x03020100U, &test_key_hsiphash) !=
188-
test_vectors_hsiphash[4]) {
189-
pr_info("hsiphash self-test 1u32: FAIL\n");
190-
ret = -EINVAL;
191-
}
192-
if (hsiphash_2u32(0x03020100U, 0x07060504U, &test_key_hsiphash) !=
193-
test_vectors_hsiphash[8]) {
194-
pr_info("hsiphash self-test 2u32: FAIL\n");
195-
ret = -EINVAL;
196-
}
197-
if (hsiphash_3u32(0x03020100U, 0x07060504U,
198-
0x0b0a0908U, &test_key_hsiphash) !=
199-
test_vectors_hsiphash[12]) {
200-
pr_info("hsiphash self-test 3u32: FAIL\n");
201-
ret = -EINVAL;
202-
}
203-
if (hsiphash_4u32(0x03020100U, 0x07060504U,
204-
0x0b0a0908U, 0x0f0e0d0cU, &test_key_hsiphash) !=
205-
test_vectors_hsiphash[16]) {
206-
pr_info("hsiphash self-test 4u32: FAIL\n");
207-
ret = -EINVAL;
208-
}
209-
if (!ret)
210-
pr_info("self-tests: pass\n");
211-
return ret;
151+
&test_key_siphash),
152+
test_vectors_siphash[32],
153+
"siphash self-test 4u64: FAIL");
154+
chk(siphash_1u32(0x03020100U, &test_key_siphash),
155+
test_vectors_siphash[4],
156+
"siphash self-test 1u32: FAIL");
157+
chk(siphash_2u32(0x03020100U, 0x07060504U, &test_key_siphash),
158+
test_vectors_siphash[8],
159+
"siphash self-test 2u32: FAIL");
160+
chk(siphash_3u32(0x03020100U, 0x07060504U,
161+
0x0b0a0908U, &test_key_siphash),
162+
test_vectors_siphash[12],
163+
"siphash self-test 3u32: FAIL");
164+
chk(siphash_4u32(0x03020100U, 0x07060504U,
165+
0x0b0a0908U, 0x0f0e0d0cU, &test_key_siphash),
166+
test_vectors_siphash[16],
167+
"siphash self-test 4u32: FAIL");
168+
chk(hsiphash_1u32(0x03020100U, &test_key_hsiphash),
169+
test_vectors_hsiphash[4],
170+
"hsiphash self-test 1u32: FAIL");
171+
chk(hsiphash_2u32(0x03020100U, 0x07060504U, &test_key_hsiphash),
172+
test_vectors_hsiphash[8],
173+
"hsiphash self-test 2u32: FAIL");
174+
chk(hsiphash_3u32(0x03020100U, 0x07060504U,
175+
0x0b0a0908U, &test_key_hsiphash),
176+
test_vectors_hsiphash[12],
177+
"hsiphash self-test 3u32: FAIL");
178+
chk(hsiphash_4u32(0x03020100U, 0x07060504U,
179+
0x0b0a0908U, 0x0f0e0d0cU, &test_key_hsiphash),
180+
test_vectors_hsiphash[16],
181+
"hsiphash self-test 4u32: FAIL");
212182
}
213183

214-
static void __exit siphash_test_exit(void)
215-
{
216-
}
184+
static struct kunit_case siphash_test_cases[] = {
185+
KUNIT_CASE(siphash_test),
186+
{}
187+
};
188+
189+
static struct kunit_suite siphash_test_suite = {
190+
.name = "siphash",
191+
.test_cases = siphash_test_cases,
192+
};
217193

218-
module_init(siphash_test_init);
219-
module_exit(siphash_test_exit);
194+
kunit_test_suite(siphash_test_suite);
220195

221196
MODULE_AUTHOR("Jason A. Donenfeld <[email protected]>");
222197
MODULE_LICENSE("Dual BSD/GPL");

0 commit comments

Comments
 (0)