summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Müller <deso@posteo.net>2022-05-24 02:04:27 +0300
committerAndrii Nakryiko <andrii@kernel.org>2022-06-03 02:26:36 +0300
commitdea73da2213a722d952840edd598f2f3a40ddc68 (patch)
tree10f8679341ab178b2ef28c45c56bc9dc5f6f35d2
parentba5d1b5802d4a732cb563e81ff30b7f514257a99 (diff)
downloadlinux-dea73da2213a722d952840edd598f2f3a40ddc68.tar.xz
selftests/bpf: Add test for libbpf_bpf_link_type_str
This change adds a test for libbpf_bpf_link_type_str. The test retrieves all variants of the bpf_link_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-12-deso@posteo.net
-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 c88df92868c1..93e9cddaadcf 100644
--- a/tools/testing/selftests/bpf/prog_tests/libbpf_str.c
+++ b/tools/testing/selftests/bpf/prog_tests/libbpf_str.c
@@ -60,6 +60,51 @@ cleanup:
}
/*
+ * Test case to check that all bpf_link_type variants are covered by
+ * libbpf_bpf_link_type_str.
+ */
+static void test_libbpf_bpf_link_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_link_type and enumerate each value */
+ id = btf__find_by_name_kind(btf, "bpf_link_type", BTF_KIND_ENUM);
+ if (!ASSERT_GT(id, 0, "bpf_link_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_link_type link_type = (enum bpf_link_type)e->val;
+ const char *link_type_name;
+ const char *link_type_str;
+ char buf[256];
+
+ if (link_type == MAX_BPF_LINK_TYPE)
+ continue;
+
+ link_type_name = btf__str_by_offset(btf, e->name_off);
+ link_type_str = libbpf_bpf_link_type_str(link_type);
+ ASSERT_OK_PTR(link_type_str, link_type_name);
+
+ snprintf(buf, sizeof(buf), "BPF_LINK_TYPE_%s", link_type_str);
+ uppercase(buf);
+
+ ASSERT_STREQ(buf, link_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.
*/
@@ -151,6 +196,9 @@ void test_libbpf_str(void)
if (test__start_subtest("bpf_attach_type_str"))
test_libbpf_bpf_attach_type_str();
+ if (test__start_subtest("bpf_link_type_str"))
+ test_libbpf_bpf_link_type_str();
+
if (test__start_subtest("bpf_map_type_str"))
test_libbpf_bpf_map_type_str();