Commit 6e05abc9 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: Convert test_btf_dump into test_progs test

Convert test_btf_dump into a part of test_progs, instead of
a stand-alone test binary.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191008231009.2991130-3-andriin@fb.com
parent b4099769
...@@ -29,7 +29,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test ...@@ -29,7 +29,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
test_cgroup_storage test_select_reuseport test_section_names \ test_cgroup_storage test_select_reuseport test_section_names \
test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
test_btf_dump test_cgroup_attach xdping test_cgroup_attach xdping
BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c))) BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
TEST_GEN_FILES = $(BPF_OBJ_FILES) TEST_GEN_FILES = $(BPF_OBJ_FILES)
......
#include <stdio.h> // SPDX-License-Identifier: GPL-2.0
#include <stdlib.h> #include <test_progs.h>
#include <string.h> #include <bpf/btf.h>
#include <unistd.h>
#include <errno.h> static int duration = 0;
#include <linux/err.h>
#include <btf.h>
#define CHECK(condition, format...) ({ \
int __ret = !!(condition); \
if (__ret) { \
fprintf(stderr, "%s:%d:FAIL ", __func__, __LINE__); \
fprintf(stderr, format); \
} \
__ret; \
})
void btf_dump_printf(void *ctx, const char *fmt, va_list args) void btf_dump_printf(void *ctx, const char *fmt, va_list args)
{ {
vfprintf(ctx, fmt, args); vfprintf(ctx, fmt, args);
} }
struct btf_dump_test_case { static struct btf_dump_test_case {
const char *name; const char *name;
const char *file;
struct btf_dump_opts opts; struct btf_dump_opts opts;
} btf_dump_test_cases[] = { } btf_dump_test_cases[] = {
{.name = "btf_dump_test_case_syntax", .opts = {}}, {"btf_dump: syntax", "btf_dump_test_case_syntax", {}},
{.name = "btf_dump_test_case_ordering", .opts = {}}, {"btf_dump: ordering", "btf_dump_test_case_ordering", {}},
{.name = "btf_dump_test_case_padding", .opts = {}}, {"btf_dump: padding", "btf_dump_test_case_padding", {}},
{.name = "btf_dump_test_case_packing", .opts = {}}, {"btf_dump: packing", "btf_dump_test_case_packing", {}},
{.name = "btf_dump_test_case_bitfields", .opts = {}}, {"btf_dump: bitfields", "btf_dump_test_case_bitfields", {}},
{.name = "btf_dump_test_case_multidim", .opts = {}}, {"btf_dump: multidim", "btf_dump_test_case_multidim", {}},
{.name = "btf_dump_test_case_namespacing", .opts = {}}, {"btf_dump: namespacing", "btf_dump_test_case_namespacing", {}},
}; };
static int btf_dump_all_types(const struct btf *btf, static int btf_dump_all_types(const struct btf *btf,
...@@ -55,55 +45,51 @@ static int btf_dump_all_types(const struct btf *btf, ...@@ -55,55 +45,51 @@ static int btf_dump_all_types(const struct btf *btf,
return err; return err;
} }
int test_btf_dump_case(int n, struct btf_dump_test_case *test_case) static int test_btf_dump_case(int n, struct btf_dump_test_case *t)
{ {
char test_file[256], out_file[256], diff_cmd[1024]; char test_file[256], out_file[256], diff_cmd[1024];
struct btf *btf = NULL; struct btf *btf = NULL;
int err = 0, fd = -1; int err = 0, fd = -1;
FILE *f = NULL; FILE *f = NULL;
fprintf(stderr, "Test case #%d (%s): ", n, test_case->name); snprintf(test_file, sizeof(test_file), "%s.o", t->file);
snprintf(test_file, sizeof(test_file), "%s.o", test_case->name);
btf = btf__parse_elf(test_file, NULL); btf = btf__parse_elf(test_file, NULL);
if (CHECK(IS_ERR(btf), if (CHECK(IS_ERR(btf), "btf_parse_elf",
"failed to load test BTF: %ld\n", PTR_ERR(btf))) { "failed to load test BTF: %ld\n", PTR_ERR(btf))) {
err = -PTR_ERR(btf); err = -PTR_ERR(btf);
btf = NULL; btf = NULL;
goto done; goto done;
} }
snprintf(out_file, sizeof(out_file), snprintf(out_file, sizeof(out_file), "/tmp/%s.output.XXXXXX", t->file);
"/tmp/%s.output.XXXXXX", test_case->name);
fd = mkstemp(out_file); fd = mkstemp(out_file);
if (CHECK(fd < 0, "failed to create temp output file: %d\n", fd)) { if (CHECK(fd < 0, "create_tmp", "failed to create file: %d\n", fd)) {
err = fd; err = fd;
goto done; goto done;
} }
f = fdopen(fd, "w"); f = fdopen(fd, "w");
if (CHECK(f == NULL, "failed to open temp output file: %s(%d)\n", if (CHECK(f == NULL, "open_tmp", "failed to open file: %s(%d)\n",
strerror(errno), errno)) { strerror(errno), errno)) {
close(fd); close(fd);
goto done; goto done;
} }
test_case->opts.ctx = f; t->opts.ctx = f;
err = btf_dump_all_types(btf, &test_case->opts); err = btf_dump_all_types(btf, &t->opts);
fclose(f); fclose(f);
close(fd); close(fd);
if (CHECK(err, "failure during C dumping: %d\n", err)) { if (CHECK(err, "btf_dump", "failure during C dumping: %d\n", err)) {
goto done; goto done;
} }
snprintf(test_file, sizeof(test_file), "progs/%s.c", test_case->name); snprintf(test_file, sizeof(test_file), "progs/%s.c", t->file);
if (access(test_file, R_OK) == -1) if (access(test_file, R_OK) == -1)
/* /*
* When the test is run with O=, kselftest copies TEST_FILES * When the test is run with O=, kselftest copies TEST_FILES
* without preserving the directory structure. * without preserving the directory structure.
*/ */
snprintf(test_file, sizeof(test_file), "%s.c", snprintf(test_file, sizeof(test_file), "%s.c", t->file);
test_case->name);
/* /*
* Diff test output and expected test output, contained between * Diff test output and expected test output, contained between
* START-EXPECTED-OUTPUT and END-EXPECTED-OUTPUT lines in test case. * START-EXPECTED-OUTPUT and END-EXPECTED-OUTPUT lines in test case.
...@@ -118,33 +104,27 @@ int test_btf_dump_case(int n, struct btf_dump_test_case *test_case) ...@@ -118,33 +104,27 @@ int test_btf_dump_case(int n, struct btf_dump_test_case *test_case)
"out {sub(/^[ \\t]*\\*/, \"\"); print}' '%s' | diff -u - '%s'", "out {sub(/^[ \\t]*\\*/, \"\"); print}' '%s' | diff -u - '%s'",
test_file, out_file); test_file, out_file);
err = system(diff_cmd); err = system(diff_cmd);
if (CHECK(err, if (CHECK(err, "diff",
"differing test output, output=%s, err=%d, diff cmd:\n%s\n", "differing test output, output=%s, err=%d, diff cmd:\n%s\n",
out_file, err, diff_cmd)) out_file, err, diff_cmd))
goto done; goto done;
remove(out_file); remove(out_file);
fprintf(stderr, "OK\n");
done: done:
btf__free(btf); btf__free(btf);
return err; return err;
} }
int main() { void test_btf_dump() {
int test_case_cnt, i, err, failed = 0; int i;
test_case_cnt = sizeof(btf_dump_test_cases) /
sizeof(btf_dump_test_cases[0]);
for (i = 0; i < test_case_cnt; i++) { for (i = 0; i < ARRAY_SIZE(btf_dump_test_cases); i++) {
err = test_btf_dump_case(i, &btf_dump_test_cases[i]); struct btf_dump_test_case *t = &btf_dump_test_cases[i];
if (err)
failed++;
}
fprintf(stderr, "%d tests succeeded, %d tests failed.\n", if (!test__start_subtest(t->name))
test_case_cnt - failed, failed); continue;
return failed; test_btf_dump_case(i, &btf_dump_test_cases[i]);
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment