From: Wen Yang <[email protected]> Add CONFIG_TLOB_KUNIT_TEST (tristate, depends on RV_MON_TLOB && KUNIT, default KUNIT_ALL_TESTS) with a single test suite covering the uprobe line parser: valid bindings are accepted, malformed ones return -EINVAL, and out-of-range thresholds return -ERANGE.
Signed-off-by: Wen Yang <[email protected]> --- kernel/trace/rv/Makefile | 1 + kernel/trace/rv/monitors/tlob/.kunitconfig | 6 ++ kernel/trace/rv/monitors/tlob/Kconfig | 7 ++ kernel/trace/rv/monitors/tlob/tlob_kunit.c | 92 ++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 kernel/trace/rv/monitors/tlob/.kunitconfig create mode 100644 kernel/trace/rv/monitors/tlob/tlob_kunit.c diff --git a/kernel/trace/rv/Makefile b/kernel/trace/rv/Makefile index ae59e97f8682..316d53398345 100644 --- a/kernel/trace/rv/Makefile +++ b/kernel/trace/rv/Makefile @@ -21,6 +21,7 @@ obj-$(CONFIG_RV_MON_STALL) += monitors/stall/stall.o obj-$(CONFIG_RV_MON_DEADLINE) += monitors/deadline/deadline.o obj-$(CONFIG_RV_MON_NOMISS) += monitors/nomiss/nomiss.o obj-$(CONFIG_RV_MON_TLOB) += monitors/tlob/tlob.o +obj-$(CONFIG_TLOB_KUNIT_TEST) += monitors/tlob/tlob_kunit.o # Add new monitors here obj-$(CONFIG_RV_UPROBE) += rv_uprobe.o obj-$(CONFIG_RV_REACTORS) += rv_reactors.o diff --git a/kernel/trace/rv/monitors/tlob/.kunitconfig b/kernel/trace/rv/monitors/tlob/.kunitconfig new file mode 100644 index 000000000000..35d313dfc20d --- /dev/null +++ b/kernel/trace/rv/monitors/tlob/.kunitconfig @@ -0,0 +1,6 @@ +CONFIG_FTRACE=y +CONFIG_KUNIT=y +CONFIG_MODULES=y +CONFIG_RV=y +CONFIG_RV_MON_TLOB=y +CONFIG_TLOB_KUNIT_TEST=y diff --git a/kernel/trace/rv/monitors/tlob/Kconfig b/kernel/trace/rv/monitors/tlob/Kconfig index b29a375de228..7ec3326640c2 100644 --- a/kernel/trace/rv/monitors/tlob/Kconfig +++ b/kernel/trace/rv/monitors/tlob/Kconfig @@ -10,3 +10,10 @@ config RV_MON_TLOB monitor. tlob tracks per-task elapsed wall-clock time across a user-delimited code section and emits error_env_tlob when the elapsed time exceeds a configurable per-invocation budget. + +config TLOB_KUNIT_TEST + tristate "KUnit tests for tlob monitor" if !KUNIT_ALL_TESTS + depends on RV_MON_TLOB && KUNIT + default KUNIT_ALL_TESTS + help + Enable KUnit unit tests for the tlob RV monitor. diff --git a/kernel/trace/rv/monitors/tlob/tlob_kunit.c b/kernel/trace/rv/monitors/tlob/tlob_kunit.c new file mode 100644 index 000000000000..6450d61b26c3 --- /dev/null +++ b/kernel/trace/rv/monitors/tlob/tlob_kunit.c @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the tlob RV monitor. + * + */ +#include <kunit/test.h> + +#include "tlob.h" + +MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); + +static const char * const tlob_parse_valid[] = { + "p /usr/bin/myapp:4768 4848 threshold=5000000", + "p /usr/bin/myapp:0x12a0 0x12f0 threshold=10000000", + "p /opt/my:app/bin:0x100 0x200 threshold=1000000", +}; + +static const char * const tlob_parse_invalid[] = { + /* add: malformed */ + "p :0x100 0x200 threshold=5000", + "p /usr/bin/myapp:0x100 threshold=5000", + "p /usr/bin/myapp:-1 0x200 threshold=5000", + "p /usr/bin/myapp:0x100 0x200", + "p /usr/bin/myapp:0x100 0x100 threshold=5000", + /* remove: malformed */ + "-usr/bin/myapp:0x100", + "-/usr/bin/myapp", + "-/:0x100", + "-/usr/bin/myapp:abc", +}; + +/* threshold_ns < 1000 or > TLOB_MAX_THRESHOLD_NS return -ERANGE, not -EINVAL. */ +static const char * const tlob_parse_out_of_range[] = { + "p /usr/bin/myapp:0x100 0x200 threshold=0", + "p /usr/bin/myapp:0x100 0x200 threshold=999", + "p /usr/bin/myapp:0x100 0x200 threshold=3600000000001", /* TLOB_MAX_THRESHOLD_NS + 1 */ +}; + +/* + * Valid add lines return -ENOENT (kern_path() finds no such file in the test + * environment) rather than 0; a non-(-EINVAL) return confirms the format was + * accepted by the parser. + */ +static void tlob_parse_valid_accepted(struct kunit *test) +{ + char buf[128]; + int i; + + for (i = 0; i < ARRAY_SIZE(tlob_parse_valid); i++) { + strscpy(buf, tlob_parse_valid[i], sizeof(buf)); + KUNIT_EXPECT_NE(test, tlob_create_or_delete_uprobe(buf), -EINVAL); + } +} + +static void tlob_parse_invalid_rejected(struct kunit *test) +{ + char buf[128]; + int i; + + for (i = 0; i < ARRAY_SIZE(tlob_parse_invalid); i++) { + strscpy(buf, tlob_parse_invalid[i], sizeof(buf)); + KUNIT_EXPECT_EQ(test, tlob_create_or_delete_uprobe(buf), -EINVAL); + } +} + +static void tlob_parse_out_of_range_rejected(struct kunit *test) +{ + char buf[128]; + int i; + + for (i = 0; i < ARRAY_SIZE(tlob_parse_out_of_range); i++) { + strscpy(buf, tlob_parse_out_of_range[i], sizeof(buf)); + KUNIT_EXPECT_EQ(test, tlob_create_or_delete_uprobe(buf), -ERANGE); + } +} + +static struct kunit_case tlob_parse_cases[] = { + KUNIT_CASE(tlob_parse_valid_accepted), + KUNIT_CASE(tlob_parse_invalid_rejected), + KUNIT_CASE(tlob_parse_out_of_range_rejected), + {} +}; + +static struct kunit_suite tlob_parse_suite = { + .name = "tlob_parse", + .test_cases = tlob_parse_cases, +}; + +kunit_test_suite(tlob_parse_suite); + +MODULE_DESCRIPTION("KUnit tests for the tlob RV monitor"); +MODULE_LICENSE("GPL"); -- 2.43.0
