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 "strcasecmp", 12 "strchr", 13 "strchrnul", 14 "strnchr", 15 "strrchr", 16 "strlen", 17 "strnlen", 18 "strspn_str", 19 "strspn_accept", 20 "strcspn_str", 21 "strcspn_reject", 22 "strstr", 23 "strnstr", 24 }; 25 26 void run_too_long_tests(void) 27 { 28 struct string_kfuncs_failure2 *skel; 29 struct bpf_program *prog; 30 char test_name[256]; 31 int err, i; 32 33 skel = string_kfuncs_failure2__open_and_load(); 34 if (!ASSERT_OK_PTR(skel, "string_kfuncs_failure2__open_and_load")) 35 return; 36 37 memset(skel->bss->long_str, 'a', sizeof(skel->bss->long_str)); 38 39 for (i = 0; i < ARRAY_SIZE(test_cases); i++) { 40 sprintf(test_name, "test_%s_too_long", test_cases[i]); 41 if (!test__start_subtest(test_name)) 42 continue; 43 44 prog = bpf_object__find_program_by_name(skel->obj, test_name); 45 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) 46 goto cleanup; 47 48 LIBBPF_OPTS(bpf_test_run_opts, topts); 49 err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts); 50 if (!ASSERT_OK(err, "bpf_prog_test_run")) 51 goto cleanup; 52 53 ASSERT_EQ(topts.retval, -E2BIG, "reading too long string fails with -E2BIG"); 54 } 55 56 cleanup: 57 string_kfuncs_failure2__destroy(skel); 58 } 59 60 void test_string_kfuncs(void) 61 { 62 RUN_TESTS(string_kfuncs_success); 63 RUN_TESTS(string_kfuncs_failure1); 64 65 run_too_long_tests(); 66 } 67