Add test cases to verify the new ".kmod_btfs" section logic in libbpf. The added test cases cover three main scenarios: - Valid module: targeting an existing kernel module BTF to ensure successful loading. - Non-existent module: targeting a fake or missing module, verifying that the process results in an expected failure. - Duplicates and unneeded: providing a mix of repeated and extra module names to ensure the parsing logic remains robust. Signed-off-by: Fuyu Zhao --- .../selftests/bpf/prog_tests/kmod_btfs.c | 52 +++++++++++++++++++ tools/testing/selftests/bpf/progs/kmod_btfs.c | 14 +++++ .../selftests/bpf/progs/kmod_btfs_mix.c | 15 ++++++ .../selftests/bpf/progs/kmod_btfs_nonexist.c | 18 +++++++ 4 files changed, 99 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kmod_btfs.c create mode 100644 tools/testing/selftests/bpf/progs/kmod_btfs.c create mode 100644 tools/testing/selftests/bpf/progs/kmod_btfs_mix.c create mode 100644 tools/testing/selftests/bpf/progs/kmod_btfs_nonexist.c diff --git a/tools/testing/selftests/bpf/prog_tests/kmod_btfs.c b/tools/testing/selftests/bpf/prog_tests/kmod_btfs.c new file mode 100644 index 000000000000..d875fcce6681 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/kmod_btfs.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "kmod_btfs.skel.h" +#include "kmod_btfs_nonexist.skel.h" +#include "kmod_btfs_mix.skel.h" + +static void kmod_btfs_pass(void) +{ + struct kmod_btfs *kmod_btfs_skel; + + kmod_btfs_skel = kmod_btfs__open_and_load(); + if (!ASSERT_OK_PTR(kmod_btfs_skel, "kmod_btfs__open_and_load")) + return; + + kmod_btfs__destroy(kmod_btfs_skel); +} + +static void kmod_btfs_nonexist(void) +{ + struct kmod_btfs_nonexist *kmod_btfs_nonexist_skel; + + kmod_btfs_nonexist_skel = kmod_btfs_nonexist__open_and_load(); + ASSERT_NULL(kmod_btfs_nonexist_skel, "kmod_btfs_nonexist__open_and_load"); +} + +static void kmod_btfs_mix(void) +{ + struct kmod_btfs_mix *kmod_btfs_mix_skel; + + kmod_btfs_mix_skel = kmod_btfs_mix__open_and_load(); + if (!ASSERT_OK_PTR(kmod_btfs_mix_skel, "kmod_btfs_mix__open_and_load")) + return; + + kmod_btfs_mix__destroy(kmod_btfs_mix_skel); +} + +void test_kmod_btfs(void) +{ + if (!env.has_testmod) { + test__skip(); + return; + } + + if (test__start_subtest("kmod_btfs_pass")) + kmod_btfs_pass(); + + if (test__start_subtest("kmod_btfs_nonexist")) + kmod_btfs_nonexist(); + + if (test__start_subtest("kmod_btfs_mix")) + kmod_btfs_mix(); +} diff --git a/tools/testing/selftests/bpf/progs/kmod_btfs.c b/tools/testing/selftests/bpf/progs/kmod_btfs.c new file mode 100644 index 000000000000..8b986bc2e9ff --- /dev/null +++ b/tools/testing/selftests/bpf/progs/kmod_btfs.c @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +SEC("fexit/bpf_testmod_loop_test") +int test_kmod_btfs(void *ctx) +{ + return 0; +} + +DEFINE_KMOD_BTFS(_needed_kmods) = { "bpf_testmod" }; + +char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/kmod_btfs_mix.c b/tools/testing/selftests/bpf/progs/kmod_btfs_mix.c new file mode 100644 index 000000000000..f445cf56ab16 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/kmod_btfs_mix.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +SEC("fexit/bpf_testmod_loop_test") +int test_kmod_btfs_mix(void *ctx) +{ + return 0; +} + +/* mix of duplicated and unneeded modules */ +DEFINE_KMOD_BTFS(_needed_kmods) = { "bpf_testmod", "bpf_testmod", "bpf_test_no_cfi" }; + +char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/kmod_btfs_nonexist.c b/tools/testing/selftests/bpf/progs/kmod_btfs_nonexist.c new file mode 100644 index 000000000000..1638361e5732 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/kmod_btfs_nonexist.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +SEC("fexit/bpf_testmod_loop_test") +int test_kmod_btfs_nonexist(void *ctx) +{ + return 0; +} + +/* + * This should fail to load, because .kmod_btfs does not contain + * the needed module 'bpf_testmod'. + */ +DEFINE_KMOD_BTFS(_needed_kmods) = { "module_nonexist" }; + +char _license[] SEC("license") = "GPL"; -- 2.34.1