xref: /linux/tools/testing/selftests/bpf/prog_tests/string_kfuncs.c (revision d9104cec3e8fe4b458b74709853231385779001f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2025 Red Hat, Inc.*/
3 #include <test_progs.h>
4 #include "string_kfuncs_success.skel.h"
5 #include "string_kfuncs_failure1.skel.h"
6 #include "string_kfuncs_failure2.skel.h"
7 #include <sys/mman.h>
8 
9 static const char * const test_cases[] = {
10 	"strcmp",
11 	"strchr",
12 	"strchrnul",
13 	"strnchr",
14 	"strrchr",
15 	"strlen",
16 	"strnlen",
17 	"strspn_str",
18 	"strspn_accept",
19 	"strcspn_str",
20 	"strcspn_reject",
21 	"strstr",
22 	"strnstr",
23 };
24 
run_too_long_tests(void)25 void run_too_long_tests(void)
26 {
27 	struct string_kfuncs_failure2 *skel;
28 	struct bpf_program *prog;
29 	char test_name[256];
30 	int err, i;
31 
32 	skel = string_kfuncs_failure2__open_and_load();
33 	if (!ASSERT_OK_PTR(skel, "string_kfuncs_failure2__open_and_load"))
34 		return;
35 
36 	memset(skel->bss->long_str, 'a', sizeof(skel->bss->long_str));
37 
38 	for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
39 		sprintf(test_name, "test_%s_too_long", test_cases[i]);
40 		if (!test__start_subtest(test_name))
41 			continue;
42 
43 		prog = bpf_object__find_program_by_name(skel->obj, test_name);
44 		if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
45 			goto cleanup;
46 
47 		LIBBPF_OPTS(bpf_test_run_opts, topts);
48 		err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
49 		if (!ASSERT_OK(err, "bpf_prog_test_run"))
50 			goto cleanup;
51 
52 		ASSERT_EQ(topts.retval, -E2BIG, "reading too long string fails with -E2BIG");
53 	}
54 
55 cleanup:
56 	string_kfuncs_failure2__destroy(skel);
57 }
58 
test_string_kfuncs(void)59 void test_string_kfuncs(void)
60 {
61 	RUN_TESTS(string_kfuncs_success);
62 	RUN_TESTS(string_kfuncs_failure1);
63 
64 	run_too_long_tests();
65 }
66