Test bpf enum stringification helpers: libbpf_(prog|map|attach)_type_(from|to)_str Signed-off-by: Julia Kartseva <hex@xxxxxx> --- .../selftests/bpf/test_section_names.c | 149 +++++++++++++++++- 1 file changed, 147 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/test_section_names.c b/tools/testing/selftests/bpf/test_section_names.c index 29833aeaf0de..564585a07592 100644 --- a/tools/testing/selftests/bpf/test_section_names.c +++ b/tools/testing/selftests/bpf/test_section_names.c @@ -202,7 +202,137 @@ static int test_attach_type_by_name(const struct sec_name_test *test) return 0; } -static int run_test_case(const struct sec_name_test *test) +static int test_prog_type_from_to_str(void) +{ + enum bpf_prog_type type, actual_type; + const char *str; + int rc; + + for (type = BPF_PROG_TYPE_UNSPEC; type < __MAX_BPF_PROG_TYPE; type++) { + rc = libbpf_prog_type_to_str(type, &str); + if (rc) { + warnx("prog_type_to_str: unexpected rc=%d for type %d", + rc, type); + return rc; + } + + rc = libbpf_prog_type_from_str(str, &actual_type); + if (rc) { + warnx("prog_type_from_str: unexpected rc=%d for str %s", + rc, str); + return rc; + } + + if (actual_type != type) { + warnx("prog: unexpected prog_type for str %s, %d != %d", + str, actual_type, type); + return -EINVAL; + } + } + + rc = libbpf_prog_type_to_str(__MAX_BPF_PROG_TYPE, &str); + if (!rc) { + warnx("prog: unexpected result for __MAX_BPF_PROG_TYPE"); + return -EINVAL; + } + + rc = libbpf_prog_type_from_str("NonExistent", &type); + if (!rc) { + warnx("prog: unexpected result for non existent key"); + return -EINVAL; + } + + return 0; +} + +static int test_map_type_from_to_str(void) +{ + enum bpf_map_type type, actual_type; + const char *str; + int rc; + + for (type = BPF_MAP_TYPE_UNSPEC; type < __MAX_BPF_MAP_TYPE; type++) { + rc = libbpf_map_type_to_str(type, &str); + if (rc) { + warnx("map_type_to_str: unexpected rc=%d for type %d", + rc, type); + return rc; + } + + rc = libbpf_map_type_from_str(str, &actual_type); + if (rc) { + warnx("map_type_from_str: unexpected rc=%d for str %s", + rc, str); + return rc; + } + + if (actual_type != type) { + warnx("map: unexpected map_type for str %s, %d != %d", + str, actual_type, type); + return -EINVAL; + } + } + + rc = libbpf_map_type_to_str(__MAX_BPF_MAP_TYPE, &str); + if (!rc) { + warnx("map: unexpected result for __MAX_BPF_MAP_TYPE"); + return -EINVAL; + } + + rc = libbpf_map_type_from_str("NonExistent", &type); + if (!rc) { + warnx("map: unexpected result for non existent key"); + return -EINVAL; + } + + return 0; +} + +static int test_attach_type_from_to_str(void) +{ + enum bpf_attach_type type, actual_type; + const char *str; + int rc; + + for (type = BPF_CGROUP_INET_INGRESS; type < __MAX_BPF_ATTACH_TYPE; + type++) { + rc = libbpf_attach_type_to_str(type, &str); + if (rc) { + warnx("attach: unexpected rc=%d for type %d", + rc, type); + return rc; + } + + rc = libbpf_attach_type_from_str(str, &actual_type); + if (rc) { + warnx("attach: unexpected rc=%d for str %s", + rc, str); + return rc; + } + + if (actual_type != type) { + warnx("attach: unexpected type for str %s, %d != %d", + str, actual_type, type); + return -EINVAL; + } + } + + rc = libbpf_attach_type_to_str(__MAX_BPF_ATTACH_TYPE, &str); + if (!rc) { + warnx("attach: unexpected result for __MAX_BPF_ATTACH_TYPE"); + return -EINVAL; + } + + rc = libbpf_attach_type_from_str("NonExistent", &type); + if (!rc) { + warnx("attach: unexpected result for non existent key"); + return -EINVAL; + } + + return 0; +} + +static int run_sec_name_test_case(const struct sec_name_test *test) { if (test_prog_type_by_name(test)) return -1; @@ -218,11 +348,26 @@ static int run_tests(void) int i; for (i = 0; i < ARRAY_SIZE(tests); ++i) { - if (run_test_case(&tests[i])) + if (run_sec_name_test_case(&tests[i])) ++fails; else ++passes; } + + if (test_prog_type_from_to_str()) + ++fails; + else + ++passes; + + if (test_map_type_from_to_str()) + ++fails; + else + ++passes; + + if (test_attach_type_from_to_str()) + ++fails; + else + ++passes; printf("Summary: %d PASSED, %d FAILED\n", passes, fails); return fails ? -1 : 0; } -- 2.17.1