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 "vmlinux.h"
4#include <bpf/bpf_helpers.h>
5#include "bpf_misc.h"
6#include "errno.h"
7
8char 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
63char _license[] SEC("license") = "GPL";