9 #define CHECK(condition, format...) ({ \
10 int __ret = !!(condition); \
12 fprintf(stderr, "%s:%d:FAIL ", __func__, __LINE__); \
13 fprintf(stderr, format); \
18 void btf_dump_printf(void *ctx
, const char *fmt
, va_list args
)
20 vfprintf(ctx
, fmt
, args
);
23 struct btf_dump_test_case
{
25 struct btf_dump_opts opts
;
26 } btf_dump_test_cases
[] = {
27 {.name
= "btf_dump_test_case_syntax", .opts
= {}},
28 {.name
= "btf_dump_test_case_ordering", .opts
= {}},
29 {.name
= "btf_dump_test_case_padding", .opts
= {}},
30 {.name
= "btf_dump_test_case_packing", .opts
= {}},
31 {.name
= "btf_dump_test_case_bitfields", .opts
= {}},
32 {.name
= "btf_dump_test_case_multidim", .opts
= {}},
33 {.name
= "btf_dump_test_case_namespacing", .opts
= {}},
36 static int btf_dump_all_types(const struct btf
*btf
,
37 const struct btf_dump_opts
*opts
)
39 size_t type_cnt
= btf__get_nr_types(btf
);
43 d
= btf_dump__new(btf
, NULL
, opts
, btf_dump_printf
);
47 for (id
= 1; id
<= type_cnt
; id
++) {
48 err
= btf_dump__dump_type(d
, id
);
58 int test_btf_dump_case(int n
, struct btf_dump_test_case
*test_case
)
60 char test_file
[256], out_file
[256], diff_cmd
[1024];
61 struct btf
*btf
= NULL
;
65 fprintf(stderr
, "Test case #%d (%s): ", n
, test_case
->name
);
67 snprintf(test_file
, sizeof(test_file
), "%s.o", test_case
->name
);
69 btf
= btf__parse_elf(test_file
, NULL
);
70 if (CHECK(IS_ERR(btf
),
71 "failed to load test BTF: %ld\n", PTR_ERR(btf
))) {
77 snprintf(out_file
, sizeof(out_file
),
78 "/tmp/%s.output.XXXXXX", test_case
->name
);
79 fd
= mkstemp(out_file
);
80 if (CHECK(fd
< 0, "failed to create temp output file: %d\n", fd
)) {
85 if (CHECK(f
== NULL
, "failed to open temp output file: %s(%d)\n",
86 strerror(errno
), errno
)) {
91 test_case
->opts
.ctx
= f
;
92 err
= btf_dump_all_types(btf
, &test_case
->opts
);
95 if (CHECK(err
, "failure during C dumping: %d\n", err
)) {
99 snprintf(test_file
, sizeof(test_file
), "progs/%s.c", test_case
->name
);
101 * Diff test output and expected test output, contained between
102 * START-EXPECTED-OUTPUT and END-EXPECTED-OUTPUT lines in test case.
103 * For expected output lines, everything before '*' is stripped out.
104 * Also lines containing comment start and comment end markers are
107 snprintf(diff_cmd
, sizeof(diff_cmd
),
108 "awk '/START-EXPECTED-OUTPUT/{out=1;next} "
109 "/END-EXPECTED-OUTPUT/{out=0} "
110 "/\\/\\*|\\*\\//{next} " /* ignore comment start/end lines */
111 "out {sub(/^[ \\t]*\\*/, \"\"); print}' '%s' | diff -u - '%s'",
112 test_file
, out_file
);
113 err
= system(diff_cmd
);
115 "differing test output, output=%s, err=%d, diff cmd:\n%s\n",
116 out_file
, err
, diff_cmd
))
120 fprintf(stderr
, "OK\n");
128 int test_case_cnt
, i
, err
, failed
= 0;
130 test_case_cnt
= sizeof(btf_dump_test_cases
) /
131 sizeof(btf_dump_test_cases
[0]);
133 for (i
= 0; i
< test_case_cnt
; i
++) {
134 err
= test_btf_dump_case(i
, &btf_dump_test_cases
[i
]);
139 fprintf(stderr
, "%d tests succeeded, %d tests failed.\n",
140 test_case_cnt
- failed
, failed
);