Skip to contentSkip to conversation

lib/tests: add KUnit tests for the UCS-2 string helpers

PATCH New Amit Patole 1 participant 1 message Nov 6

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

PATCH 1/1 lib/tests: add KUnit tests for the UCS-2 string helpers

lib/ucs2_string.c is used for EFI variable names (efivarfs, efi-pstore), Apple device properties, the EFI stub's load options and Hyper-V interface aliases, but has no unit tests.

Add a KUnit suite that covers:

- ucs2_strnlen(), ucs2_strlen() and ucs2_strsize(), including a byte limit that is not a whole number of characters;
- ucs2_strscpy(): exact fit, truncation with -E2BIG and NUL termination, and the zero-sized and oversized buffer checks;
- ucs2_strncmp(): ordering, the length limit, prefixes and unsigned comparison of characters above 0x7fff;
- ucs2_utf8size() and ucs2_as_utf8() for the one, two and three byte UTF-8 boundaries (U+007F/U+0080, U+07FF/U+0800, U+FFFF);
- truncation in ucs2_as_utf8(): a multi-byte character is never split, and the terminator is written only when there is room;
- ucs2_as_utf8_l() on a source that is not NUL terminated, which is why it was split out of ucs2_as_utf8().

An AI coding assistant (Claude Opus 5.5) was used to pick the untested helpers and draft the tests. They were run with "tools/testing/kunit/kunit.py run" on UML (19/19 pass), built with W=1 without warnings, and checked against deliberately broken versions of ucs2_strscpy() and ucs2_as_utf8_l(), which the suite catches.

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

CONFIG_UCS2_STRING_KUNIT_TEST=y: 19/19 pass.

---
 lib/Kconfig.debug             |  14 ++
 lib/tests/Makefile            |   1 +
 lib/tests/ucs2_string_kunit.c | 288 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 303 insertions(+)
lib/Kconfig.debug +14 -0
index 2d22638ec13a..6e5d1c617f2e 100644
@@ -3461,6 +3461,20 @@ config RATELIMIT_KUNIT_TEST
 
 	  If unsure, say N.
 
+config UCS2_STRING_KUNIT_TEST
+	tristate "KUnit test for UCS-2 string helpers" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	select UCS2_STRING
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables the KUnit test suite for the UCS-2 string
+	  helpers in lib/ucs2_string.c, which are used for EFI variable
+	  names and other firmware strings. The tests cover length,
+	  copy and compare helpers and the UCS-2 to UTF-8 conversion,
+	  including truncation at the destination buffer size.
+
+	  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..cf5486008d75 100644
@@ -54,6 +54,7 @@ CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable)
 obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
 obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o
 obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o
+obj-$(CONFIG_UCS2_STRING_KUNIT_TEST) += ucs2_string_kunit.o
 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
lib/tests/ucs2_string_kunit.c new file +288 -0

288 lines not on this page · raw message

---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261105-kunit-ucs2-string-58ea0aa11314

Best regards,

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