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