xref: /linux/tools/testing/selftests/bpf/progs/string_kfuncs_success.c (revision d9104cec3e8fe4b458b74709853231385779001f)
1*e8763fb6SViktor Malik // SPDX-License-Identifier: GPL-2.0
2*e8763fb6SViktor Malik /* Copyright (C) 2025 Red Hat, Inc.*/
3*e8763fb6SViktor Malik #include "vmlinux.h"
4*e8763fb6SViktor Malik #include <bpf/bpf_helpers.h>
5*e8763fb6SViktor Malik #include "bpf_misc.h"
6*e8763fb6SViktor Malik #include "errno.h"
7*e8763fb6SViktor Malik 
8*e8763fb6SViktor Malik char str[] = "hello world";
9*e8763fb6SViktor Malik 
10*e8763fb6SViktor Malik #define __test(retval) SEC("syscall") __success __retval(retval)
11*e8763fb6SViktor Malik 
12*e8763fb6SViktor Malik /* Functional tests */
test_strcmp_eq(void * ctx)13*e8763fb6SViktor Malik __test(0) int test_strcmp_eq(void *ctx) { return bpf_strcmp(str, "hello world"); }
test_strcmp_neq(void * ctx)14*e8763fb6SViktor Malik __test(1) int test_strcmp_neq(void *ctx) { return bpf_strcmp(str, "hello"); }
test_strchr_found(void * ctx)15*e8763fb6SViktor Malik __test(1) int test_strchr_found(void *ctx) { return bpf_strchr(str, 'e'); }
test_strchr_null(void * ctx)16*e8763fb6SViktor Malik __test(11) int test_strchr_null(void *ctx) { return bpf_strchr(str, '\0'); }
test_strchr_notfound(void * ctx)17*e8763fb6SViktor Malik __test(-ENOENT) int test_strchr_notfound(void *ctx) { return bpf_strchr(str, 'x'); }
test_strchrnul_found(void * ctx)18*e8763fb6SViktor Malik __test(1) int test_strchrnul_found(void *ctx) { return bpf_strchrnul(str, 'e'); }
test_strchrnul_notfound(void * ctx)19*e8763fb6SViktor Malik __test(11) int test_strchrnul_notfound(void *ctx) { return bpf_strchrnul(str, 'x'); }
test_strnchr_found(void * ctx)20*e8763fb6SViktor Malik __test(1) int test_strnchr_found(void *ctx) { return bpf_strnchr(str, 5, 'e'); }
test_strnchr_null(void * ctx)21*e8763fb6SViktor Malik __test(11) int test_strnchr_null(void *ctx) { return bpf_strnchr(str, 12, '\0'); }
test_strnchr_notfound(void * ctx)22*e8763fb6SViktor Malik __test(-ENOENT) int test_strnchr_notfound(void *ctx) { return bpf_strnchr(str, 5, 'w'); }
test_strrchr_found(void * ctx)23*e8763fb6SViktor Malik __test(9) int test_strrchr_found(void *ctx) { return bpf_strrchr(str, 'l'); }
test_strrchr_null(void * ctx)24*e8763fb6SViktor Malik __test(11) int test_strrchr_null(void *ctx) { return bpf_strrchr(str, '\0'); }
test_strrchr_notfound(void * ctx)25*e8763fb6SViktor Malik __test(-ENOENT) int test_strrchr_notfound(void *ctx) { return bpf_strrchr(str, 'x'); }
test_strlen(void * ctx)26*e8763fb6SViktor Malik __test(11) int test_strlen(void *ctx) { return bpf_strlen(str); }
test_strnlen(void * ctx)27*e8763fb6SViktor Malik __test(11) int test_strnlen(void *ctx) { return bpf_strnlen(str, 12); }
test_strspn(void * ctx)28*e8763fb6SViktor Malik __test(5) int test_strspn(void *ctx) { return bpf_strspn(str, "ehlo"); }
test_strcspn(void * ctx)29*e8763fb6SViktor Malik __test(2) int test_strcspn(void *ctx) { return bpf_strcspn(str, "lo"); }
test_strstr_found(void * ctx)30*e8763fb6SViktor Malik __test(6) int test_strstr_found(void *ctx) { return bpf_strstr(str, "world"); }
test_strstr_notfound(void * ctx)31*e8763fb6SViktor Malik __test(-ENOENT) int test_strstr_notfound(void *ctx) { return bpf_strstr(str, "hi"); }
test_strstr_empty(void * ctx)32*e8763fb6SViktor Malik __test(0) int test_strstr_empty(void *ctx) { return bpf_strstr(str, ""); }
test_strnstr_found(void * ctx)33*e8763fb6SViktor Malik __test(0) int test_strnstr_found(void *ctx) { return bpf_strnstr(str, "hello", 6); }
test_strnstr_notfound(void * ctx)34*e8763fb6SViktor Malik __test(-ENOENT) int test_strnstr_notfound(void *ctx) { return bpf_strnstr(str, "hi", 10); }
test_strnstr_empty(void * ctx)35*e8763fb6SViktor Malik __test(0) int test_strnstr_empty(void *ctx) { return bpf_strnstr(str, "", 1); }
36*e8763fb6SViktor Malik 
37*e8763fb6SViktor Malik char _license[] SEC("license") = "GPL";
38