1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Facebook */
3 
4 #include <test_progs.h>
5 #include "dynptr_fail.skel.h"
6 #include "dynptr_success.skel.h"
7 
8 static const char * const success_tests[] = {
9 	"test_read_write",
10 	"test_data_slice",
11 	"test_ringbuf",
12 };
13 
14 static void verify_success(const char *prog_name)
15 {
16 	struct dynptr_success *skel;
17 	struct bpf_program *prog;
18 	struct bpf_link *link;
19 
20 	skel = dynptr_success__open();
21 	if (!ASSERT_OK_PTR(skel, "dynptr_success__open"))
22 		return;
23 
24 	skel->bss->pid = getpid();
25 
26 	dynptr_success__load(skel);
27 	if (!ASSERT_OK_PTR(skel, "dynptr_success__load"))
28 		goto cleanup;
29 
30 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
31 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
32 		goto cleanup;
33 
34 	link = bpf_program__attach(prog);
35 	if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
36 		goto cleanup;
37 
38 	usleep(1);
39 
40 	ASSERT_EQ(skel->bss->err, 0, "err");
41 
42 	bpf_link__destroy(link);
43 
44 cleanup:
45 	dynptr_success__destroy(skel);
46 }
47 
48 void test_dynptr(void)
49 {
50 	int i;
51 
52 	for (i = 0; i < ARRAY_SIZE(success_tests); i++) {
53 		if (!test__start_subtest(success_tests[i]))
54 			continue;
55 
56 		verify_success(success_tests[i]);
57 	}
58 
59 	RUN_TESTS(dynptr_fail);
60 }
61