Skip to content

Commit 0a5c0de

Browse files
sveissanakryiko
authored andcommitted
selftests/bpf: Extract insert_test from parse_test_list
Split the logic to insert new tests into test filter sets out from parse_test_list. Fix the subtest insertion logic to reuse an existing top-level test filter, which prevents the creation of duplicate top-level test filters each with a single subtest. Signed-off-by: Stephen Veiss <sveiss@meta.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20230427225333.3506052-2-sveiss@meta.com
1 parent c39028b commit 0a5c0de

File tree

2 files changed

+108
-65
lines changed

2 files changed

+108
-65
lines changed

tools/testing/selftests/bpf/prog_tests/arg_parsing.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ static void test_parse_test_list(void)
9696
goto error;
9797
ASSERT_OK(strcmp("*bpf_cookie*", set.tests[0].name), "test name");
9898
ASSERT_OK(strcmp("*trace*", set.tests[0].subtests[0]), "subtest name");
99+
free_test_filter_set(&set);
100+
101+
ASSERT_OK(parse_test_list("t/subtest1,t/subtest2", &set, true),
102+
"parsing");
103+
if (!ASSERT_EQ(set.cnt, 1, "count of test filters"))
104+
goto error;
105+
if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
106+
goto error;
107+
if (!ASSERT_EQ(set.tests[0].subtest_cnt, 2, "subtest filters count"))
108+
goto error;
109+
ASSERT_OK(strcmp("t", set.tests[0].name), "test name");
110+
ASSERT_OK(strcmp("subtest1", set.tests[0].subtests[0]), "subtest name");
111+
ASSERT_OK(strcmp("subtest2", set.tests[0].subtests[1]), "subtest name");
99112
error:
100113
free_test_filter_set(&set);
101114
}

tools/testing/selftests/bpf/testing_helpers.c

Lines changed: 95 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -70,92 +70,122 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
7070
return 0;
7171
}
7272

73-
int parse_test_list(const char *s,
74-
struct test_filter_set *set,
75-
bool is_glob_pattern)
73+
static int do_insert_test(struct test_filter_set *set,
74+
char *test_str,
75+
char *subtest_str)
7676
{
77-
char *input, *state = NULL, *next;
78-
struct test_filter *tmp, *tests = NULL;
79-
int i, j, cnt = 0;
77+
struct test_filter *tmp, *test;
78+
char **ctmp;
79+
int i;
8080

81-
input = strdup(s);
82-
if (!input)
81+
for (i = 0; i < set->cnt; i++) {
82+
test = &set->tests[i];
83+
84+
if (strcmp(test_str, test->name) == 0) {
85+
free(test_str);
86+
goto subtest;
87+
}
88+
}
89+
90+
tmp = realloc(set->tests, sizeof(*test) * (set->cnt + 1));
91+
if (!tmp)
8392
return -ENOMEM;
8493

85-
while ((next = strtok_r(state ? NULL : input, ",", &state))) {
86-
char *subtest_str = strchr(next, '/');
87-
char *pattern = NULL;
88-
int glob_chars = 0;
94+
set->tests = tmp;
95+
test = &set->tests[set->cnt];
8996

90-
tmp = realloc(tests, sizeof(*tests) * (cnt + 1));
91-
if (!tmp)
92-
goto err;
93-
tests = tmp;
97+
test->name = test_str;
98+
test->subtests = NULL;
99+
test->subtest_cnt = 0;
94100

95-
tests[cnt].subtest_cnt = 0;
96-
tests[cnt].subtests = NULL;
101+
set->cnt++;
97102

98-
if (is_glob_pattern) {
99-
pattern = "%s";
100-
} else {
101-
pattern = "*%s*";
102-
glob_chars = 2;
103-
}
103+
subtest:
104+
if (!subtest_str)
105+
return 0;
104106

105-
if (subtest_str) {
106-
char **tmp_subtests = NULL;
107-
int subtest_cnt = tests[cnt].subtest_cnt;
108-
109-
*subtest_str = '\0';
110-
subtest_str += 1;
111-
tmp_subtests = realloc(tests[cnt].subtests,
112-
sizeof(*tmp_subtests) *
113-
(subtest_cnt + 1));
114-
if (!tmp_subtests)
115-
goto err;
116-
tests[cnt].subtests = tmp_subtests;
117-
118-
tests[cnt].subtests[subtest_cnt] =
119-
malloc(strlen(subtest_str) + glob_chars + 1);
120-
if (!tests[cnt].subtests[subtest_cnt])
121-
goto err;
122-
sprintf(tests[cnt].subtests[subtest_cnt],
123-
pattern,
124-
subtest_str);
125-
126-
tests[cnt].subtest_cnt++;
107+
for (i = 0; i < test->subtest_cnt; i++) {
108+
if (strcmp(subtest_str, test->subtests[i]) == 0) {
109+
free(subtest_str);
110+
return 0;
127111
}
112+
}
128113

129-
tests[cnt].name = malloc(strlen(next) + glob_chars + 1);
130-
if (!tests[cnt].name)
131-
goto err;
132-
sprintf(tests[cnt].name, pattern, next);
114+
ctmp = realloc(test->subtests,
115+
sizeof(*test->subtests) * (test->subtest_cnt + 1));
116+
if (!ctmp)
117+
return -ENOMEM;
118+
119+
test->subtests = ctmp;
120+
test->subtests[test->subtest_cnt] = subtest_str;
133121

134-
cnt++;
122+
test->subtest_cnt++;
123+
124+
return 0;
125+
}
126+
127+
static int insert_test(struct test_filter_set *set,
128+
char *test_spec,
129+
bool is_glob_pattern)
130+
{
131+
char *pattern, *subtest_str, *ext_test_str, *ext_subtest_str = NULL;
132+
int glob_chars = 0;
133+
134+
if (is_glob_pattern) {
135+
pattern = "%s";
136+
} else {
137+
pattern = "*%s*";
138+
glob_chars = 2;
135139
}
136140

137-
tmp = realloc(set->tests, sizeof(*tests) * (cnt + set->cnt));
138-
if (!tmp)
141+
subtest_str = strchr(test_spec, '/');
142+
if (subtest_str) {
143+
*subtest_str = '\0';
144+
subtest_str += 1;
145+
}
146+
147+
ext_test_str = malloc(strlen(test_spec) + glob_chars + 1);
148+
if (!ext_test_str)
139149
goto err;
140150

141-
memcpy(tmp + set->cnt, tests, sizeof(*tests) * cnt);
142-
set->tests = tmp;
143-
set->cnt += cnt;
151+
sprintf(ext_test_str, pattern, test_spec);
144152

145-
free(tests);
146-
free(input);
147-
return 0;
153+
if (subtest_str) {
154+
ext_subtest_str = malloc(strlen(subtest_str) + glob_chars + 1);
155+
if (!ext_subtest_str)
156+
goto err;
157+
158+
sprintf(ext_subtest_str, pattern, subtest_str);
159+
}
160+
161+
return do_insert_test(set, ext_test_str, ext_subtest_str);
148162

149163
err:
150-
for (i = 0; i < cnt; i++) {
151-
for (j = 0; j < tests[i].subtest_cnt; j++)
152-
free(tests[i].subtests[j]);
164+
free(ext_test_str);
165+
free(ext_subtest_str);
166+
167+
return -ENOMEM;
168+
}
153169

154-
free(tests[i].name);
170+
int parse_test_list(const char *s,
171+
struct test_filter_set *set,
172+
bool is_glob_pattern)
173+
{
174+
char *input, *state = NULL, *test_spec;
175+
int err = 0;
176+
177+
input = strdup(s);
178+
if (!input)
179+
return -ENOMEM;
180+
181+
while ((test_spec = strtok_r(state ? NULL : input, ",", &state))) {
182+
err = insert_test(set, test_spec, is_glob_pattern);
183+
if (err)
184+
break;
155185
}
156-
free(tests);
186+
157187
free(input);
158-
return -ENOMEM;
188+
return err;
159189
}
160190

161191
__u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)

0 commit comments

Comments
 (0)