1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 #include "skb_load_bytes.skel.h"
5 
6 void test_skb_load_bytes(void)
7 {
8 	struct skb_load_bytes *skel;
9 	int err, prog_fd, test_result;
10 	struct __sk_buff skb = { 0 };
11 
12 	LIBBPF_OPTS(bpf_test_run_opts, tattr,
13 		.data_in = &pkt_v4,
14 		.data_size_in = sizeof(pkt_v4),
15 		.ctx_in = &skb,
16 		.ctx_size_in = sizeof(skb),
17 	);
18 
19 	skel = skb_load_bytes__open_and_load();
20 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
21 		return;
22 
23 	prog_fd = bpf_program__fd(skel->progs.skb_process);
24 	if (!ASSERT_GE(prog_fd, 0, "prog_fd"))
25 		goto out;
26 
27 	skel->bss->load_offset = (uint32_t)(-1);
28 	err = bpf_prog_test_run_opts(prog_fd, &tattr);
29 	if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
30 		goto out;
31 	test_result = skel->bss->test_result;
32 	if (!ASSERT_EQ(test_result, -EFAULT, "offset -1"))
33 		goto out;
34 
35 	skel->bss->load_offset = (uint32_t)10;
36 	err = bpf_prog_test_run_opts(prog_fd, &tattr);
37 	if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
38 		goto out;
39 	test_result = skel->bss->test_result;
40 	if (!ASSERT_EQ(test_result, 0, "offset 10"))
41 		goto out;
42 
43 out:
44 	skb_load_bytes__destroy(skel);
45 }
46