xref: /linux/tools/testing/selftests/bpf/prog_tests/arg_parsing.c (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
161ddff37SMykola Lysenko // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
261ddff37SMykola Lysenko 
361ddff37SMykola Lysenko #include "test_progs.h"
461ddff37SMykola Lysenko #include "testing_helpers.h"
561ddff37SMykola Lysenko 
init_test_filter_set(struct test_filter_set * set)661ddff37SMykola Lysenko static void init_test_filter_set(struct test_filter_set *set)
761ddff37SMykola Lysenko {
861ddff37SMykola Lysenko 	set->cnt = 0;
961ddff37SMykola Lysenko 	set->tests = NULL;
1061ddff37SMykola Lysenko }
1161ddff37SMykola Lysenko 
free_test_filter_set(struct test_filter_set * set)1261ddff37SMykola Lysenko static void free_test_filter_set(struct test_filter_set *set)
1361ddff37SMykola Lysenko {
1461ddff37SMykola Lysenko 	int i, j;
1561ddff37SMykola Lysenko 
1661ddff37SMykola Lysenko 	for (i = 0; i < set->cnt; i++) {
1761ddff37SMykola Lysenko 		for (j = 0; j < set->tests[i].subtest_cnt; j++)
1861ddff37SMykola Lysenko 			free((void *)set->tests[i].subtests[j]);
1961ddff37SMykola Lysenko 		free(set->tests[i].subtests);
2061ddff37SMykola Lysenko 		free(set->tests[i].name);
2161ddff37SMykola Lysenko 	}
2261ddff37SMykola Lysenko 
2361ddff37SMykola Lysenko 	free(set->tests);
2461ddff37SMykola Lysenko 	init_test_filter_set(set);
2561ddff37SMykola Lysenko }
2661ddff37SMykola Lysenko 
test_parse_test_list(void)2761ddff37SMykola Lysenko static void test_parse_test_list(void)
2861ddff37SMykola Lysenko {
2961ddff37SMykola Lysenko 	struct test_filter_set set;
3061ddff37SMykola Lysenko 
3161ddff37SMykola Lysenko 	init_test_filter_set(&set);
3261ddff37SMykola Lysenko 
3361ddff37SMykola Lysenko 	ASSERT_OK(parse_test_list("arg_parsing", &set, true), "parsing");
3461ddff37SMykola Lysenko 	if (!ASSERT_EQ(set.cnt, 1, "test filters count"))
3561ddff37SMykola Lysenko 		goto error;
3661ddff37SMykola Lysenko 	if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
3761ddff37SMykola Lysenko 		goto error;
3861ddff37SMykola Lysenko 	ASSERT_EQ(set.tests[0].subtest_cnt, 0, "subtest filters count");
3961ddff37SMykola Lysenko 	ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "subtest name");
4061ddff37SMykola Lysenko 	free_test_filter_set(&set);
4161ddff37SMykola Lysenko 
4261ddff37SMykola Lysenko 	ASSERT_OK(parse_test_list("arg_parsing,bpf_cookie", &set, true),
4361ddff37SMykola Lysenko 		  "parsing");
4461ddff37SMykola Lysenko 	if (!ASSERT_EQ(set.cnt, 2, "count of test filters"))
4561ddff37SMykola Lysenko 		goto error;
4661ddff37SMykola Lysenko 	if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
4761ddff37SMykola Lysenko 		goto error;
4861ddff37SMykola Lysenko 	ASSERT_EQ(set.tests[0].subtest_cnt, 0, "subtest filters count");
4961ddff37SMykola Lysenko 	ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
5061ddff37SMykola Lysenko 	ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
5161ddff37SMykola Lysenko 	ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
5261ddff37SMykola Lysenko 	free_test_filter_set(&set);
5361ddff37SMykola Lysenko 
5461ddff37SMykola Lysenko 	ASSERT_OK(parse_test_list("arg_parsing/arg_parsing,bpf_cookie",
5561ddff37SMykola Lysenko 				  &set,
5661ddff37SMykola Lysenko 				  true),
5761ddff37SMykola Lysenko 		  "parsing");
5861ddff37SMykola Lysenko 	if (!ASSERT_EQ(set.cnt, 2, "count of test filters"))
5961ddff37SMykola Lysenko 		goto error;
6061ddff37SMykola Lysenko 	if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
6161ddff37SMykola Lysenko 		goto error;
6261ddff37SMykola Lysenko 	if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
6361ddff37SMykola Lysenko 		goto error;
6461ddff37SMykola Lysenko 	ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
6561ddff37SMykola Lysenko 	ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
6661ddff37SMykola Lysenko 	ASSERT_OK(strcmp("arg_parsing", set.tests[0].subtests[0]),
6761ddff37SMykola Lysenko 		  "subtest name");
6861ddff37SMykola Lysenko 	ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
6961ddff37SMykola Lysenko 	free_test_filter_set(&set);
7061ddff37SMykola Lysenko 
7161ddff37SMykola Lysenko 	ASSERT_OK(parse_test_list("arg_parsing/arg_parsing", &set, true),
7261ddff37SMykola Lysenko 		  "parsing");
7361ddff37SMykola Lysenko 	ASSERT_OK(parse_test_list("bpf_cookie", &set, true), "parsing");
7461ddff37SMykola Lysenko 	ASSERT_OK(parse_test_list("send_signal", &set, true), "parsing");
7561ddff37SMykola Lysenko 	if (!ASSERT_EQ(set.cnt, 3, "count of test filters"))
7661ddff37SMykola Lysenko 		goto error;
7761ddff37SMykola Lysenko 	if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
7861ddff37SMykola Lysenko 		goto error;
7961ddff37SMykola Lysenko 	if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
8061ddff37SMykola Lysenko 		goto error;
8161ddff37SMykola Lysenko 	ASSERT_EQ(set.tests[1].subtest_cnt, 0, "subtest filters count");
8261ddff37SMykola Lysenko 	ASSERT_EQ(set.tests[2].subtest_cnt, 0, "subtest filters count");
8361ddff37SMykola Lysenko 	ASSERT_OK(strcmp("arg_parsing", set.tests[0].name), "test name");
8461ddff37SMykola Lysenko 	ASSERT_OK(strcmp("arg_parsing", set.tests[0].subtests[0]),
8561ddff37SMykola Lysenko 		  "subtest name");
8661ddff37SMykola Lysenko 	ASSERT_OK(strcmp("bpf_cookie", set.tests[1].name), "test name");
8761ddff37SMykola Lysenko 	ASSERT_OK(strcmp("send_signal", set.tests[2].name), "test name");
8861ddff37SMykola Lysenko 	free_test_filter_set(&set);
8961ddff37SMykola Lysenko 
9061ddff37SMykola Lysenko 	ASSERT_OK(parse_test_list("bpf_cookie/trace", &set, false), "parsing");
9161ddff37SMykola Lysenko 	if (!ASSERT_EQ(set.cnt, 1, "count of test filters"))
9261ddff37SMykola Lysenko 		goto error;
9361ddff37SMykola Lysenko 	if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
9461ddff37SMykola Lysenko 		goto error;
9561ddff37SMykola Lysenko 	if (!ASSERT_EQ(set.tests[0].subtest_cnt, 1, "subtest filters count"))
9661ddff37SMykola Lysenko 		goto error;
9761ddff37SMykola Lysenko 	ASSERT_OK(strcmp("*bpf_cookie*", set.tests[0].name), "test name");
9861ddff37SMykola Lysenko 	ASSERT_OK(strcmp("*trace*", set.tests[0].subtests[0]), "subtest name");
990a5c0de8SStephen Veiss 	free_test_filter_set(&set);
1000a5c0de8SStephen Veiss 
1010a5c0de8SStephen Veiss 	ASSERT_OK(parse_test_list("t/subtest1,t/subtest2", &set, true),
1020a5c0de8SStephen Veiss 		  "parsing");
1030a5c0de8SStephen Veiss 	if (!ASSERT_EQ(set.cnt, 1, "count of test filters"))
1040a5c0de8SStephen Veiss 		goto error;
1050a5c0de8SStephen Veiss 	if (!ASSERT_OK_PTR(set.tests, "test filters initialized"))
1060a5c0de8SStephen Veiss 		goto error;
1070a5c0de8SStephen Veiss 	if (!ASSERT_EQ(set.tests[0].subtest_cnt, 2, "subtest filters count"))
1080a5c0de8SStephen Veiss 		goto error;
1090a5c0de8SStephen Veiss 	ASSERT_OK(strcmp("t", set.tests[0].name), "test name");
1100a5c0de8SStephen Veiss 	ASSERT_OK(strcmp("subtest1", set.tests[0].subtests[0]), "subtest name");
1110a5c0de8SStephen Veiss 	ASSERT_OK(strcmp("subtest2", set.tests[0].subtests[1]), "subtest name");
11261ddff37SMykola Lysenko error:
11361ddff37SMykola Lysenko 	free_test_filter_set(&set);
11461ddff37SMykola Lysenko }
11561ddff37SMykola Lysenko 
test_parse_test_list_file(void)116*64276f01SStephen Veiss static void test_parse_test_list_file(void)
117*64276f01SStephen Veiss {
118*64276f01SStephen Veiss 	struct test_filter_set set;
119*64276f01SStephen Veiss 	char tmpfile[80];
120*64276f01SStephen Veiss 	FILE *fp;
121*64276f01SStephen Veiss 	int fd;
122*64276f01SStephen Veiss 
123*64276f01SStephen Veiss 	snprintf(tmpfile, sizeof(tmpfile), "/tmp/bpf_arg_parsing_test.XXXXXX");
124*64276f01SStephen Veiss 	fd = mkstemp(tmpfile);
125*64276f01SStephen Veiss 	if (!ASSERT_GE(fd, 0, "create tmp"))
126*64276f01SStephen Veiss 		return;
127*64276f01SStephen Veiss 
128*64276f01SStephen Veiss 	fp = fdopen(fd, "w");
129*64276f01SStephen Veiss 	if (!ASSERT_NEQ(fp, NULL, "fdopen tmp")) {
130*64276f01SStephen Veiss 		close(fd);
131*64276f01SStephen Veiss 		goto out_remove;
132*64276f01SStephen Veiss 	}
133*64276f01SStephen Veiss 
134*64276f01SStephen Veiss 	fprintf(fp, "# comment\n");
135*64276f01SStephen Veiss 	fprintf(fp, "  test_with_spaces    \n");
136*64276f01SStephen Veiss 	fprintf(fp, "testA/subtest    # comment\n");
137*64276f01SStephen Veiss 	fprintf(fp, "testB#comment with no space\n");
138*64276f01SStephen Veiss 	fprintf(fp, "testB # duplicate\n");
139*64276f01SStephen Veiss 	fprintf(fp, "testA/subtest # subtest duplicate\n");
140*64276f01SStephen Veiss 	fprintf(fp, "testA/subtest2\n");
141*64276f01SStephen Veiss 	fprintf(fp, "testC_no_eof_newline");
142*64276f01SStephen Veiss 	fflush(fp);
143*64276f01SStephen Veiss 
144*64276f01SStephen Veiss 	if (!ASSERT_OK(ferror(fp), "prepare tmp"))
145*64276f01SStephen Veiss 		goto out_fclose;
146*64276f01SStephen Veiss 
147*64276f01SStephen Veiss 	init_test_filter_set(&set);
148*64276f01SStephen Veiss 
149*64276f01SStephen Veiss 	ASSERT_OK(parse_test_list_file(tmpfile, &set, true), "parse file");
150*64276f01SStephen Veiss 
151*64276f01SStephen Veiss 	ASSERT_EQ(set.cnt, 4, "test  count");
152*64276f01SStephen Veiss 	ASSERT_OK(strcmp("test_with_spaces", set.tests[0].name), "test 0 name");
153*64276f01SStephen Veiss 	ASSERT_EQ(set.tests[0].subtest_cnt, 0, "test 0 subtest count");
154*64276f01SStephen Veiss 	ASSERT_OK(strcmp("testA", set.tests[1].name), "test 1 name");
155*64276f01SStephen Veiss 	ASSERT_EQ(set.tests[1].subtest_cnt, 2, "test 1 subtest count");
156*64276f01SStephen Veiss 	ASSERT_OK(strcmp("subtest", set.tests[1].subtests[0]), "test 1 subtest 0");
157*64276f01SStephen Veiss 	ASSERT_OK(strcmp("subtest2", set.tests[1].subtests[1]), "test 1 subtest 1");
158*64276f01SStephen Veiss 	ASSERT_OK(strcmp("testB", set.tests[2].name), "test 2 name");
159*64276f01SStephen Veiss 	ASSERT_OK(strcmp("testC_no_eof_newline", set.tests[3].name), "test 3 name");
160*64276f01SStephen Veiss 
161*64276f01SStephen Veiss 	free_test_filter_set(&set);
162*64276f01SStephen Veiss 
163*64276f01SStephen Veiss out_fclose:
164*64276f01SStephen Veiss 	fclose(fp);
165*64276f01SStephen Veiss out_remove:
166*64276f01SStephen Veiss 	remove(tmpfile);
167*64276f01SStephen Veiss }
168*64276f01SStephen Veiss 
test_arg_parsing(void)16961ddff37SMykola Lysenko void test_arg_parsing(void)
17061ddff37SMykola Lysenko {
17161ddff37SMykola Lysenko 	if (test__start_subtest("test_parse_test_list"))
17261ddff37SMykola Lysenko 		test_parse_test_list();
173*64276f01SStephen Veiss 	if (test__start_subtest("test_parse_test_list_file"))
174*64276f01SStephen Veiss 		test_parse_test_list_file();
17561ddff37SMykola Lysenko }
176