1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_probe_read_user_str.skel.h"
5 static const char str1
[] = "mestring";
6 static const char str2
[] = "mestringalittlebigger";
7 static const char str3
[] = "mestringblubblubblubblubblub";
9 static int test_one_str(struct test_probe_read_user_str
*skel
, const char *str
,
12 int err
, duration
= 0;
15 /* Ensure bytes after string are ones */
16 memset(buf
, 1, sizeof(buf
));
17 memcpy(buf
, str
, len
);
19 /* Give prog our userspace pointer */
20 skel
->bss
->user_ptr
= buf
;
22 /* Trigger tracepoint */
25 /* Did helper fail? */
26 if (CHECK(skel
->bss
->ret
< 0, "prog_ret", "prog returned: %ld\n",
30 /* Check that string was copied correctly */
31 err
= memcmp(skel
->bss
->buf
, str
, len
);
32 if (CHECK(err
, "memcmp", "prog copied wrong string"))
35 /* Now check that no extra trailing bytes were copied */
36 memset(buf
, 0, sizeof(buf
));
37 err
= memcmp(skel
->bss
->buf
+ len
, buf
, sizeof(buf
) - len
);
38 if (CHECK(err
, "memcmp", "trailing bytes were not stripped"))
44 void test_probe_read_user_str(void)
46 struct test_probe_read_user_str
*skel
;
47 int err
, duration
= 0;
49 skel
= test_probe_read_user_str__open_and_load();
50 if (CHECK(!skel
, "test_probe_read_user_str__open_and_load",
51 "skeleton open and load failed\n"))
54 /* Give pid to bpf prog so it doesn't read from anyone else */
55 skel
->bss
->pid
= getpid();
57 err
= test_probe_read_user_str__attach(skel
);
58 if (CHECK(err
, "test_probe_read_user_str__attach",
59 "skeleton attach failed: %d\n", err
))
62 if (test_one_str(skel
, str1
, sizeof(str1
)))
64 if (test_one_str(skel
, str2
, sizeof(str2
)))
66 if (test_one_str(skel
, str3
, sizeof(str3
)))
70 test_probe_read_user_str__destroy(skel
);