Skip to contentSkip to conversation

lib/tests: add KUnit tests for xxHash

PATCH New Amit Patole 1 participant 1 message Nov 6

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/tests: add KUnit tests for xxHash

lib/xxhash.c provides xxh32() and xxh64(), used by zstd (frame checksums and long-distance matching), btrfs and erofs, where the values end up on disk. The crypto self-tests cover xxh64() through the "xxhash64" shash, but nothing tests xxh32() or the library interface directly.

Add a KUnit suite with known answers from the reference xxHash implementation. The input is generated the same way as in xxHash's own sanity check, so the lengths 1, 14 and 222 reproduce the values published there. It covers:

- xxh32() with two seeds and xxh64() with three, for lengths around the 4, 8, 16 and 32 byte boundaries of the algorithms;
- the xxh64 streaming interface, split into two updates at every possible point and fed one byte at a time, and repeated xxh64_digest() calls;
- xxh64_update() rejecting a NULL input with -EINVAL;
- input at every offset from 1 to 7 bytes, which must not change the result.

An AI coding assistant (Claude Opus 5.5) was used to draft the tests, and the expected values were generated with the reference implementation (python-xxhash 3.7.0) after checking it against the published sanity values. The tests were run with "tools/testing/kunit/kunit.py run" on UML (146/146 pass), built with W=1 without warnings, and checked against off-by-one changes to the length checks in xxh32(), xxh64() and xxh64_update(), which the suite catches.

---
Tested on UML with a kunitconfig of CONFIG_KUNIT=y and

CONFIG_XXHASH_KUNIT_TEST=y: 146/146 pass.

---
 lib/Kconfig.debug        |  14 +++
 lib/tests/Makefile       |   1 +
 lib/tests/xxhash_kunit.c | 260 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 275 insertions(+)
lib/Kconfig.debug +14 -0
index 2d22638ec13a..d88fab338d4b 100644
@@ -3461,6 +3461,20 @@ config RATELIMIT_KUNIT_TEST
 
 	  If unsure, say N.
 
+config XXHASH_KUNIT_TEST
+	tristate "KUnit test for xxHash" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	select XXHASH
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables the KUnit test suite for lib/xxhash.c, the
+	  xxh32() and xxh64() hashes used by zstd, btrfs and erofs. The
+	  tests check known answers from the reference xxHash
+	  implementation, the xxh64 streaming interface and unaligned
+	  input.
+
+	  If unsure, say N.
+
 config UUID_KUNIT_TEST
 	tristate "KUnit test for UUID" if !KUNIT_ALL_TESTS
 	depends on KUNIT
lib/tests/Makefile +1 -0
index df0537e1b91f..8828efce4412 100644
@@ -58,5 +58,6 @@ 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_UUID_KUNIT_TEST) += uuid_kunit.o
+obj-$(CONFIG_XXHASH_KUNIT_TEST) += xxhash_kunit.o
 
 obj-$(CONFIG_TEST_RUNTIME_MODULE)		+= module/
lib/tests/xxhash_kunit.c new file +260 -0

260 lines not on this page · raw message

---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261106-kunit-xxhash-25a4cca743ac

Best regards,

Signature · 1 line
Amit Patole <[email protected]>