summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/libbpf_str.c
diff options
context:
space:
mode:
authorDaniel Müller <deso@posteo.net>2022-05-23 23:04:24 +0000
committerAndrii Nakryiko <andrii@kernel.org>2022-06-02 16:26:28 -0700
commit0b27b3d9fdf88c132b095a30ee2c61cd6e56e6cc (patch)
tree2b7824110c83f617466e47fc9cb56249fc60a347 /tools/testing/selftests/bpf/prog_tests/libbpf_str.c
parentccde5760bac1b83cfb2bfed97748b71829cbe5d8 (diff)
downloadlwn-0b27b3d9fdf88c132b095a30ee2c61cd6e56e6cc.tar.gz
lwn-0b27b3d9fdf88c132b095a30ee2c61cd6e56e6cc.zip
selftests/bpf: Add test for libbpf_bpf_attach_type_str
This change adds a test for libbpf_bpf_attach_type_str. The test retrieves all variants of the bpf_attach_type enumeration using BTF and makes sure that the function under test works as expected for them. Signed-off-by: Daniel Müller <deso@posteo.net> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Quentin Monnet <quentin@isovalent.com> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20220523230428.3077108-9-deso@posteo.net
Diffstat (limited to 'tools/testing/selftests/bpf/prog_tests/libbpf_str.c')
-rw-r--r--tools/testing/selftests/bpf/prog_tests/libbpf_str.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_str.c b/tools/testing/selftests/bpf/prog_tests/libbpf_str.c
index f5185a46ee00..c88df92868c1 100644
--- a/tools/testing/selftests/bpf/prog_tests/libbpf_str.c
+++ b/tools/testing/selftests/bpf/prog_tests/libbpf_str.c
@@ -15,6 +15,51 @@ static void uppercase(char *s)
}
/*
+ * Test case to check that all bpf_attach_type variants are covered by
+ * libbpf_bpf_attach_type_str.
+ */
+static void test_libbpf_bpf_attach_type_str(void)
+{
+ struct btf *btf;
+ const struct btf_type *t;
+ const struct btf_enum *e;
+ int i, n, id;
+
+ btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
+ if (!ASSERT_OK_PTR(btf, "btf_parse"))
+ return;
+
+ /* find enum bpf_attach_type and enumerate each value */
+ id = btf__find_by_name_kind(btf, "bpf_attach_type", BTF_KIND_ENUM);
+ if (!ASSERT_GT(id, 0, "bpf_attach_type_id"))
+ goto cleanup;
+ t = btf__type_by_id(btf, id);
+ e = btf_enum(t);
+ n = btf_vlen(t);
+ for (i = 0; i < n; e++, i++) {
+ enum bpf_attach_type attach_type = (enum bpf_attach_type)e->val;
+ const char *attach_type_name;
+ const char *attach_type_str;
+ char buf[256];
+
+ if (attach_type == __MAX_BPF_ATTACH_TYPE)
+ continue;
+
+ attach_type_name = btf__str_by_offset(btf, e->name_off);
+ attach_type_str = libbpf_bpf_attach_type_str(attach_type);
+ ASSERT_OK_PTR(attach_type_str, attach_type_name);
+
+ snprintf(buf, sizeof(buf), "BPF_%s", attach_type_str);
+ uppercase(buf);
+
+ ASSERT_STREQ(buf, attach_type_name, "exp_str_value");
+ }
+
+cleanup:
+ btf__free(btf);
+}
+
+/*
* Test case to check that all bpf_map_type variants are covered by
* libbpf_bpf_map_type_str.
*/
@@ -103,6 +148,9 @@ cleanup:
*/
void test_libbpf_str(void)
{
+ if (test__start_subtest("bpf_attach_type_str"))
+ test_libbpf_bpf_attach_type_str();
+
if (test__start_subtest("bpf_map_type_str"))
test_libbpf_bpf_map_type_str();