xref: /linux/tools/testing/selftests/bpf/progs/string_kfuncs_success.c (revision 260f6f4fda93c8485c8037865c941b42b9cba5d2)
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(1) int test_strchr_found(void *ctx) { return bpf_strchr(str, 'e'); }
16 __test(11) int test_strchr_null(void *ctx) { return bpf_strchr(str, '\0'); }
17 __test(-ENOENT) int test_strchr_notfound(void *ctx) { return bpf_strchr(str, 'x'); }
18 __test(1) int test_strchrnul_found(void *ctx) { return bpf_strchrnul(str, 'e'); }
19 __test(11) int test_strchrnul_notfound(void *ctx) { return bpf_strchrnul(str, 'x'); }
20 __test(1) int test_strnchr_found(void *ctx) { return bpf_strnchr(str, 5, 'e'); }
21 __test(11) int test_strnchr_null(void *ctx) { return bpf_strnchr(str, 12, '\0'); }
22 __test(-ENOENT) int test_strnchr_notfound(void *ctx) { return bpf_strnchr(str, 5, 'w'); }
23 __test(9) int test_strrchr_found(void *ctx) { return bpf_strrchr(str, 'l'); }
24 __test(11) int test_strrchr_null(void *ctx) { return bpf_strrchr(str, '\0'); }
25 __test(-ENOENT) int test_strrchr_notfound(void *ctx) { return bpf_strrchr(str, 'x'); }
26 __test(11) int test_strlen(void *ctx) { return bpf_strlen(str); }
27 __test(11) int test_strnlen(void *ctx) { return bpf_strnlen(str, 12); }
28 __test(5) int test_strspn(void *ctx) { return bpf_strspn(str, "ehlo"); }
29 __test(2) int test_strcspn(void *ctx) { return bpf_strcspn(str, "lo"); }
30 __test(6) int test_strstr_found(void *ctx) { return bpf_strstr(str, "world"); }
31 __test(-ENOENT) int test_strstr_notfound(void *ctx) { return bpf_strstr(str, "hi"); }
32 __test(0) int test_strstr_empty(void *ctx) { return bpf_strstr(str, ""); }
33 __test(0) int test_strnstr_found(void *ctx) { return bpf_strnstr(str, "hello", 6); }
34 __test(-ENOENT) int test_strnstr_notfound(void *ctx) { return bpf_strnstr(str, "hi", 10); }
35 __test(0) int test_strnstr_empty(void *ctx) { return bpf_strnstr(str, "", 1); }
36 
37 char _license[] SEC("license") = "GPL";
38