1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3 #include "test_progs.h"
4 #include "testing_helpers.h"
5
init_test_filter_set(struct test_filter_set * set)6 static void init_test_filter_set(struct test_filter_set *set)
7 {
8 set->cnt = 0;
9 set->tests = NULL;
10 }
11
free_test_filter_set(struct test_filter_set * set)12 static void free_test_filter_set(struct test_filter_set *set)
13 {
14 int i, j;
15
16 for (i = 0; i < set->cnt; i++) {
17 for (j = 0; j < set->tests[i].subtest_cnt; j++)
18 free((void *)set->tests[i].subtests[j]);
19 free(set->tests[i].subtests);
20 free(set->tests[i].name);
21 }
22
23 free(set->tests);
24 init_test_filter_set(set);
25 }
26
test_parse_test_list(void)27 static void test_parse_test_list(void)
28 {
29 struct test_filter_set set;
30
31 init_test_filter_set(&set);
32
33 ASSERT_OK(parse_test_list("arg_parsing", &set, true), "parsing");
34 if (!ASSERT_EQ(set.cnt, 1, "test filters count"))
35 goto error;
36 if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
37 goto error;
38 ASSERT_EQ(set.tests[0].subtest_cnt, 0, "subtest filters count");
39 ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "subtest name");
40 free_test_filter_set(&set);
41
42 ASSERT_OK(parse_test_list("arg_parsing,bpf_cookie", &set, true),
43 "parsing");
44 if (!ASSERT_EQ(set.cnt, 2, "count of test filters"))
45 goto error;
46 if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
47 goto error;
48 ASSERT_EQ(set.tests[0].subtest_cnt, 0, "subtest filters count");
49 ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
50 ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
51 ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
52 free_test_filter_set(&set);
53
54 ASSERT_OK(parse_test_list("arg_parsing/arg_parsing,bpf_cookie",
55 &set,
56 true),
57 "parsing");
58 if (!ASSERT_EQ(set.cnt, 2, "count of test filters"))
59 goto error;
60 if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
61 goto error;
62 if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
63 goto error;
64 ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
65 ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
66 ASSERT_OK(strcmp("arg_parsing", set.tests[0].subtests[0]),
67 "subtest name");
68 ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
69 free_test_filter_set(&set);
70
71 ASSERT_OK(parse_test_list("arg_parsing/arg_parsing", &set, true),
72 "parsing");
73 ASSERT_OK(parse_test_list("bpf_cookie", &set, true), "parsing");
74 ASSERT_OK(parse_test_list("send_signal", &set, true), "parsing");
75 if (!ASSERT_EQ(set.cnt, 3, "count of test filters"))
76 goto error;
77 if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
78 goto error;
79 if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
80 goto error;
81 ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
82 ASSERT_EQ(set.tests[2].subtest_cnt, 0, "subtest filters count");
83 ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
84 ASSERT_OK(strcmp("arg_parsing", set.tests[0].subtests[0]),
85 "subtest name");
86 ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
87 ASSERT_OK(strcmp("send_signal", set.tests[2].name), "test name");
88 free_test_filter_set(&set);
89
90 ASSERT_OK(parse_test_list("bpf_cookie/trace", &set, false), "parsing");
91 if (!ASSERT_EQ(set.cnt, 1, "count of test filters"))
92 goto error;
93 if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
94 goto error;
95 if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
96 goto error;
97 ASSERT_OK(strcmp("*bpf_cookie*", set.tests[0].name), "test name");
98 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");
112 error:
113 free_test_filter_set(&set);
114 }
115
test_parse_test_list_file(void)116 static void test_parse_test_list_file(void)
117 {
118 struct test_filter_set set;
119 char tmpfile[80];
120 FILE *fp;
121 int fd;
122
123 snprintf(tmpfile, sizeof(tmpfile), "/tmp/bpf_arg_parsing_test.XXXXXX");
124 fd = mkstemp(tmpfile);
125 if (!ASSERT_GE(fd, 0, "create tmp"))
126 return;
127
128 fp = fdopen(fd, "w");
129 if (!ASSERT_NEQ(fp, NULL, "fdopen tmp")) {
130 close(fd);
131 goto out_remove;
132 }
133
134 fprintf(fp, "# comment\n");
135 fprintf(fp, " test_with_spaces \n");
136 fprintf(fp, "testA/subtest # comment\n");
137 fprintf(fp, "testB#comment with no space\n");
138 fprintf(fp, "testB # duplicate\n");
139 fprintf(fp, "testA/subtest # subtest duplicate\n");
140 fprintf(fp, "testA/subtest2\n");
141 fprintf(fp, "testC_no_eof_newline");
142 fflush(fp);
143
144 if (!ASSERT_OK(ferror(fp), "prepare tmp"))
145 goto out_fclose;
146
147 init_test_filter_set(&set);
148
149 ASSERT_OK(parse_test_list_file(tmpfile, &set, true), "parse file");
150
151 ASSERT_EQ(set.cnt, 4, "test count");
152 ASSERT_OK(strcmp("test_with_spaces", set.tests[0].name), "test 0 name");
153 ASSERT_EQ(set.tests[0].subtest_cnt, 0, "test 0 subtest count");
154 ASSERT_OK(strcmp("testA", set.tests[1].name), "test 1 name");
155 ASSERT_EQ(set.tests[1].subtest_cnt, 2, "test 1 subtest count");
156 ASSERT_OK(strcmp("subtest", set.tests[1].subtests[0]), "test 1 subtest 0");
157 ASSERT_OK(strcmp("subtest2", set.tests[1].subtests[1]), "test 1 subtest 1");
158 ASSERT_OK(strcmp("testB", set.tests[2].name), "test 2 name");
159 ASSERT_OK(strcmp("testC_no_eof_newline", set.tests[3].name), "test 3 name");
160
161 free_test_filter_set(&set);
162
163 out_fclose:
164 fclose(fp);
165 out_remove:
166 remove(tmpfile);
167 }
168
test_arg_parsing(void)169 void test_arg_parsing(void)
170 {
171 if (test__start_subtest("test_parse_test_list"))
172 test_parse_test_list();
173 if (test__start_subtest("test_parse_test_list_file"))
174 test_parse_test_list_file();
175 }
176