Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
9static 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 "strcasestr",
24 "strnstr",
25 "strncasestr",
26};
27
28void run_too_long_tests(void)
29{
30 struct string_kfuncs_failure2 *skel;
31 struct bpf_program *prog;
32 char test_name[256];
33 int err, i;
34
35 skel = string_kfuncs_failure2__open_and_load();
36 if (!ASSERT_OK_PTR(skel, "string_kfuncs_failure2__open_and_load"))
37 return;
38
39 memset(skel->bss->long_str, 'a', sizeof(skel->bss->long_str));
40
41 for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
42 sprintf(test_name, "test_%s_too_long", test_cases[i]);
43 if (!test__start_subtest(test_name))
44 continue;
45
46 prog = bpf_object__find_program_by_name(skel->obj, test_name);
47 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
48 goto cleanup;
49
50 LIBBPF_OPTS(bpf_test_run_opts, topts);
51 err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
52 if (!ASSERT_OK(err, "bpf_prog_test_run"))
53 goto cleanup;
54
55 ASSERT_EQ(topts.retval, -E2BIG, "reading too long string fails with -E2BIG");
56 }
57
58cleanup:
59 string_kfuncs_failure2__destroy(skel);
60}
61
62void test_string_kfuncs(void)
63{
64 RUN_TESTS(string_kfuncs_success);
65 RUN_TESTS(string_kfuncs_failure1);
66
67 run_too_long_tests();
68}