xref: /linux/tools/testing/selftests/bpf/progs/string_kfuncs_success.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2025 Red Hat, Inc.*/
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5 #include "bpf_misc.h"
6 #include "errno.h"
7 
8 char str[] = "hello world";
9 
10 #define __test(retval) SEC("syscall") __success __retval(retval)
11 
12 /* Functional tests */
13 __test(0) int test_strcmp_eq(void *ctx) { return bpf_strcmp(str, "hello world"); }
14 __test(1) int test_strcmp_neq(void *ctx) { return bpf_strcmp(str, "hello"); }
15 __test(0) int test_strcasecmp_eq1(void *ctx) { return bpf_strcasecmp(str, "hello world"); }
16 __test(0) int test_strcasecmp_eq2(void *ctx) { return bpf_strcasecmp(str, "HELLO WORLD"); }
17 __test(0) int test_strcasecmp_eq3(void *ctx) { return bpf_strcasecmp(str, "HELLO world"); }
18 __test(1) int test_strcasecmp_neq1(void *ctx) { return bpf_strcasecmp(str, "hello"); }
19 __test(1) int test_strcasecmp_neq2(void *ctx) { return bpf_strcasecmp(str, "HELLO"); }
20 __test(0) int test_strncasecmp_eq1(void *ctx) { return bpf_strncasecmp(str, "hello world", 11); }
21 __test(0) int test_strncasecmp_eq2(void *ctx) { return bpf_strncasecmp(str, "HELLO WORLD", 11); }
22 __test(0) int test_strncasecmp_eq3(void *ctx) { return bpf_strncasecmp(str, "HELLO world", 11); }
23 __test(0) int test_strncasecmp_eq4(void *ctx) { return bpf_strncasecmp(str, "hello", 5); }
24 __test(0) int test_strncasecmp_eq5(void *ctx) { return bpf_strncasecmp(str, "hello world!", 11); }
25 __test(-1) int test_strncasecmp_neq1(void *ctx) { return bpf_strncasecmp(str, "hello!", 6); }
26 __test(1) int test_strncasecmp_neq2(void *ctx) { return bpf_strncasecmp(str, "abc", 3); }
27 __test(1) int test_strchr_found(void *ctx) { return bpf_strchr(str, 'e'); }
28 __test(11) int test_strchr_null(void *ctx) { return bpf_strchr(str, '\0'); }
29 __test(-ENOENT) int test_strchr_notfound(void *ctx) { return bpf_strchr(str, 'x'); }
30 __test(1) int test_strchrnul_found(void *ctx) { return bpf_strchrnul(str, 'e'); }
31 __test(11) int test_strchrnul_notfound(void *ctx) { return bpf_strchrnul(str, 'x'); }
32 __test(1) int test_strnchr_found(void *ctx) { return bpf_strnchr(str, 5, 'e'); }
33 __test(11) int test_strnchr_null(void *ctx) { return bpf_strnchr(str, 12, '\0'); }
34 __test(-ENOENT) int test_strnchr_notfound(void *ctx) { return bpf_strnchr(str, 5, 'w'); }
35 __test(9) int test_strrchr_found(void *ctx) { return bpf_strrchr(str, 'l'); }
36 __test(11) int test_strrchr_null(void *ctx) { return bpf_strrchr(str, '\0'); }
37 __test(-ENOENT) int test_strrchr_notfound(void *ctx) { return bpf_strrchr(str, 'x'); }
38 __test(11) int test_strlen(void *ctx) { return bpf_strlen(str); }
39 __test(11) int test_strnlen(void *ctx) { return bpf_strnlen(str, 12); }
40 __test(5) int test_strspn(void *ctx) { return bpf_strspn(str, "ehlo"); }
41 __test(2) int test_strcspn(void *ctx) { return bpf_strcspn(str, "lo"); }
42 __test(6) int test_strstr_found(void *ctx) { return bpf_strstr(str, "world"); }
43 __test(6) int test_strcasestr_found(void *ctx) { return bpf_strcasestr(str, "woRLD"); }
44 __test(-ENOENT) int test_strstr_notfound(void *ctx) { return bpf_strstr(str, "hi"); }
45 __test(-ENOENT) int test_strcasestr_notfound(void *ctx) { return bpf_strcasestr(str, "hi"); }
46 __test(0) int test_strstr_empty(void *ctx) { return bpf_strstr(str, ""); }
47 __test(0) int test_strcasestr_empty(void *ctx) { return bpf_strcasestr(str, ""); }
48 __test(0) int test_strnstr_found1(void *ctx) { return bpf_strnstr("", "", 0); }
49 __test(0) int test_strnstr_found2(void *ctx) { return bpf_strnstr(str, "hello", 5); }
50 __test(0) int test_strnstr_found3(void *ctx) { return bpf_strnstr(str, "hello", 6); }
51 __test(-ENOENT) int test_strnstr_notfound1(void *ctx) { return bpf_strnstr(str, "hi", 10); }
52 __test(-ENOENT) int test_strnstr_notfound2(void *ctx) { return bpf_strnstr(str, "hello", 4); }
53 __test(-ENOENT) int test_strnstr_notfound3(void *ctx) { return bpf_strnstr("", "a", 0); }
54 __test(0) int test_strnstr_empty(void *ctx) { return bpf_strnstr(str, "", 1); }
55 __test(0) int test_strncasestr_found1(void *ctx) { return bpf_strncasestr("", "", 0); }
56 __test(0) int test_strncasestr_found2(void *ctx) { return bpf_strncasestr(str, "heLLO", 5); }
57 __test(0) int test_strncasestr_found3(void *ctx) { return bpf_strncasestr(str, "heLLO", 6); }
58 __test(-ENOENT) int test_strncasestr_notfound1(void *ctx) { return bpf_strncasestr(str, "hi", 10); }
59 __test(-ENOENT) int test_strncasestr_notfound2(void *ctx) { return bpf_strncasestr(str, "hello", 4); }
60 __test(-ENOENT) int test_strncasestr_notfound3(void *ctx) { return bpf_strncasestr("", "a", 0); }
61 __test(0) int test_strncasestr_empty(void *ctx) { return bpf_strncasestr(str, "", 1); }
62 
63 char _license[] SEC("license") = "GPL";
64