Introduce a KUnit test suite to verify that composite compatibility strings are formatted correctly, specifically ensuring that all KHO sub-component compatibility strings are unique and strictly sorted in alphabetical order.
Maintaining alphabetical order in composite compatibility strings is required to guarantee consistent, predictable, and reproducible compatibility string representation across different system configurations. The test suite validates: - KHO_FDT_COMPATIBLE (the root composite compatibility string) - LUO_ABI_COMPATIBLE (the LUO composite compatibility string) - MEMFD_LUO_FH_COMPATIBLE (the memfd file handler composite compatibility string) Signed-off-by: Pasha Tatashin <[email protected]> --- kernel/liveupdate/Kconfig | 15 ++++++ kernel/liveupdate/Makefile | 2 + kernel/liveupdate/liveupdate_test.c | 56 +++++++++++++++++++++++ tools/testing/selftests/liveupdate/config | 1 + 4 files changed, 74 insertions(+) create mode 100644 kernel/liveupdate/liveupdate_test.c diff --git a/kernel/liveupdate/Kconfig b/kernel/liveupdate/Kconfig index c13af38ba23a..617a31dcee73 100644 --- a/kernel/liveupdate/Kconfig +++ b/kernel/liveupdate/Kconfig @@ -86,4 +86,19 @@ config LIVEUPDATE_MEMFD If unsure, say N. +config LIVEUPDATE_KUNIT_TEST + tristate "KUnit tests for LUO and KHO" if !KUNIT_ALL_TESTS + depends on KUNIT + depends on LIVEUPDATE + default KUNIT_ALL_TESTS + help + Enable KUnit tests for LUO and KHO. These tests verify that the + composite KHO, LUO, and memfd compatibility strings remain unique + and sorted alphabetically. + + For more information on KUnit and unit tests in general, please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + endmenu diff --git a/kernel/liveupdate/Makefile b/kernel/liveupdate/Makefile index b481e21a311a..5e0deb85e1b1 100644 --- a/kernel/liveupdate/Makefile +++ b/kernel/liveupdate/Makefile @@ -17,3 +17,5 @@ obj-$(CONFIG_KEXEC_HANDOVER_DEBUG) += kexec_handover_debug.o obj-$(CONFIG_KEXEC_HANDOVER_DEBUGFS) += kexec_handover_debugfs.o obj-$(CONFIG_LIVEUPDATE) += luo.o +obj-$(CONFIG_LIVEUPDATE_KUNIT_TEST) += liveupdate_test.o + diff --git a/kernel/liveupdate/liveupdate_test.c b/kernel/liveupdate/liveupdate_test.c new file mode 100644 index 000000000000..15688d69735e --- /dev/null +++ b/kernel/liveupdate/liveupdate_test.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * KUnit test for Live Update compatibility strings. + */ +#include <kunit/test.h> +#include <linux/string.h> +#include <linux/kho/abi/kexec_handover.h> +#include <linux/kho/abi/luo.h> +#include <linux/kho/abi/memfd.h> + +/* Verify that compatibility sub-components are unique and sorted alphabetically */ +static bool is_alphabetical_unique(const char *compat) +{ + char buf[1024]; + char *string = buf; + char *token; + char *prev = NULL; + char *sub; + + strscpy(buf, compat, sizeof(buf)); + + sub = strchr(string, ';'); + if (!sub) + return true; + + sub++; + + while ((token = strsep(&sub, ";")) != NULL) { + if (prev && strcmp(prev, token) >= 0) + return false; + prev = token; + } + + return true; +} + +static void test_compatibility_alphabetical(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, is_alphabetical_unique(KHO_FDT_COMPATIBLE)); + KUNIT_EXPECT_TRUE(test, is_alphabetical_unique(LUO_ABI_COMPATIBLE)); + KUNIT_EXPECT_TRUE(test, is_alphabetical_unique(MEMFD_LUO_FH_COMPATIBLE)); +} + +static struct kunit_case liveupdate_test_cases[] = { + KUNIT_CASE(test_compatibility_alphabetical), + {} +}; + +static struct kunit_suite liveupdate_test_suite = { + .name = "liveupdate-compatibility", + .test_cases = liveupdate_test_cases, +}; + +kunit_test_suite(liveupdate_test_suite); + +MODULE_LICENSE("GPL"); diff --git a/tools/testing/selftests/liveupdate/config b/tools/testing/selftests/liveupdate/config index 91d03f9a6a39..28c54bb473b8 100644 --- a/tools/testing/selftests/liveupdate/config +++ b/tools/testing/selftests/liveupdate/config @@ -6,6 +6,7 @@ CONFIG_KEXEC_HANDOVER_DEBUGFS=y CONFIG_KEXEC_HANDOVER_DEBUG=y CONFIG_LIVEUPDATE=y CONFIG_LIVEUPDATE_TEST=y +CONFIG_LIVEUPDATE_KUNIT_TEST=y CONFIG_MEMFD_CREATE=y CONFIG_TMPFS=y CONFIG_SHMEM=y -- 2.53.0

