1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
5 #include <test_progs.h>
9 * Utility function uppercasing an entire string.
11 static void uppercase(char *s)
13 for (; *s != '\0'; s++)
18 * Test case to check that all bpf_attach_type variants are covered by
19 * libbpf_bpf_attach_type_str.
21 static void test_libbpf_bpf_attach_type_str(void)
24 const struct btf_type *t;
25 const struct btf_enum *e;
28 btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
29 if (!ASSERT_OK_PTR(btf, "btf_parse"))
32 /* find enum bpf_attach_type and enumerate each value */
33 id = btf__find_by_name_kind(btf, "bpf_attach_type", BTF_KIND_ENUM);
34 if (!ASSERT_GT(id, 0, "bpf_attach_type_id"))
36 t = btf__type_by_id(btf, id);
39 for (i = 0; i < n; e++, i++) {
40 enum bpf_attach_type attach_type = (enum bpf_attach_type)e->val;
41 const char *attach_type_name;
42 const char *attach_type_str;
45 if (attach_type == __MAX_BPF_ATTACH_TYPE)
48 attach_type_name = btf__str_by_offset(btf, e->name_off);
49 attach_type_str = libbpf_bpf_attach_type_str(attach_type);
50 ASSERT_OK_PTR(attach_type_str, attach_type_name);
52 snprintf(buf, sizeof(buf), "BPF_%s", attach_type_str);
55 ASSERT_STREQ(buf, attach_type_name, "exp_str_value");
63 * Test case to check that all bpf_link_type variants are covered by
64 * libbpf_bpf_link_type_str.
66 static void test_libbpf_bpf_link_type_str(void)
69 const struct btf_type *t;
70 const struct btf_enum *e;
73 btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
74 if (!ASSERT_OK_PTR(btf, "btf_parse"))
77 /* find enum bpf_link_type and enumerate each value */
78 id = btf__find_by_name_kind(btf, "bpf_link_type", BTF_KIND_ENUM);
79 if (!ASSERT_GT(id, 0, "bpf_link_type_id"))
81 t = btf__type_by_id(btf, id);
84 for (i = 0; i < n; e++, i++) {
85 enum bpf_link_type link_type = (enum bpf_link_type)e->val;
86 const char *link_type_name;
87 const char *link_type_str;
90 if (link_type == __MAX_BPF_LINK_TYPE)
93 link_type_name = btf__str_by_offset(btf, e->name_off);
94 link_type_str = libbpf_bpf_link_type_str(link_type);
95 ASSERT_OK_PTR(link_type_str, link_type_name);
97 snprintf(buf, sizeof(buf), "BPF_LINK_TYPE_%s", link_type_str);
100 ASSERT_STREQ(buf, link_type_name, "exp_str_value");
108 * Test case to check that all bpf_map_type variants are covered by
109 * libbpf_bpf_map_type_str.
111 static void test_libbpf_bpf_map_type_str(void)
114 const struct btf_type *t;
115 const struct btf_enum *e;
118 btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
119 if (!ASSERT_OK_PTR(btf, "btf_parse"))
122 /* find enum bpf_map_type and enumerate each value */
123 id = btf__find_by_name_kind(btf, "bpf_map_type", BTF_KIND_ENUM);
124 if (!ASSERT_GT(id, 0, "bpf_map_type_id"))
126 t = btf__type_by_id(btf, id);
129 for (i = 0; i < n; e++, i++) {
130 enum bpf_map_type map_type = (enum bpf_map_type)e->val;
131 const char *map_type_name;
132 const char *map_type_str;
135 if (map_type == __MAX_BPF_MAP_TYPE)
138 map_type_name = btf__str_by_offset(btf, e->name_off);
139 map_type_str = libbpf_bpf_map_type_str(map_type);
140 ASSERT_OK_PTR(map_type_str, map_type_name);
142 snprintf(buf, sizeof(buf), "BPF_MAP_TYPE_%s", map_type_str);
145 /* Special case for map_type_name BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED
146 * where it and BPF_MAP_TYPE_CGROUP_STORAGE have the same enum value
147 * (map_type). For this enum value, libbpf_bpf_map_type_str() picks
148 * BPF_MAP_TYPE_CGROUP_STORAGE. The same for
149 * BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE_DEPRECATED and
150 * BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE.
152 if (strcmp(map_type_name, "BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED") == 0)
154 if (strcmp(map_type_name, "BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE_DEPRECATED") == 0)
157 ASSERT_STREQ(buf, map_type_name, "exp_str_value");
165 * Test case to check that all bpf_prog_type variants are covered by
166 * libbpf_bpf_prog_type_str.
168 static void test_libbpf_bpf_prog_type_str(void)
171 const struct btf_type *t;
172 const struct btf_enum *e;
175 btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
176 if (!ASSERT_OK_PTR(btf, "btf_parse"))
179 /* find enum bpf_prog_type and enumerate each value */
180 id = btf__find_by_name_kind(btf, "bpf_prog_type", BTF_KIND_ENUM);
181 if (!ASSERT_GT(id, 0, "bpf_prog_type_id"))
183 t = btf__type_by_id(btf, id);
186 for (i = 0; i < n; e++, i++) {
187 enum bpf_prog_type prog_type = (enum bpf_prog_type)e->val;
188 const char *prog_type_name;
189 const char *prog_type_str;
192 if (prog_type == __MAX_BPF_PROG_TYPE)
195 prog_type_name = btf__str_by_offset(btf, e->name_off);
196 prog_type_str = libbpf_bpf_prog_type_str(prog_type);
197 ASSERT_OK_PTR(prog_type_str, prog_type_name);
199 snprintf(buf, sizeof(buf), "BPF_PROG_TYPE_%s", prog_type_str);
202 ASSERT_STREQ(buf, prog_type_name, "exp_str_value");
210 * Run all libbpf str conversion tests.
212 void test_libbpf_str(void)
214 if (test__start_subtest("bpf_attach_type_str"))
215 test_libbpf_bpf_attach_type_str();
217 if (test__start_subtest("bpf_link_type_str"))
218 test_libbpf_bpf_link_type_str();
220 if (test__start_subtest("bpf_map_type_str"))
221 test_libbpf_bpf_map_type_str();
223 if (test__start_subtest("bpf_prog_type_str"))
224 test_libbpf_bpf_prog_type_str();