1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 #include "fexit_test.skel.h"
5 
6 static int fexit_test(struct fexit_test *fexit_skel)
7 {
8 	int err, prog_fd, i;
9 	__u32 duration = 0, retval;
10 	struct bpf_link *link;
11 	__u64 *result;
12 
13 	err = fexit_test__attach(fexit_skel);
14 	if (!ASSERT_OK(err, "fexit_attach"))
15 		return err;
16 
17 	/* Check that already linked program can't be attached again. */
18 	link = bpf_program__attach(fexit_skel->progs.test1);
19 	if (!ASSERT_ERR_PTR(link, "fexit_attach_link"))
20 		return -1;
21 
22 	prog_fd = bpf_program__fd(fexit_skel->progs.test1);
23 	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
24 				NULL, NULL, &retval, &duration);
25 	ASSERT_OK(err, "test_run");
26 	ASSERT_EQ(retval, 0, "test_run");
27 
28 	result = (__u64 *)fexit_skel->bss;
29 	for (i = 0; i < sizeof(*fexit_skel->bss) / sizeof(__u64); i++) {
30 		if (!ASSERT_EQ(result[i], 1, "fexit_result"))
31 			return -1;
32 	}
33 
34 	fexit_test__detach(fexit_skel);
35 
36 	/* zero results for re-attach test */
37 	memset(fexit_skel->bss, 0, sizeof(*fexit_skel->bss));
38 	return 0;
39 }
40 
41 void test_fexit_test(void)
42 {
43 	struct fexit_test *fexit_skel = NULL;
44 	int err;
45 
46 	fexit_skel = fexit_test__open_and_load();
47 	if (!ASSERT_OK_PTR(fexit_skel, "fexit_skel_load"))
48 		goto cleanup;
49 
50 	err = fexit_test(fexit_skel);
51 	if (!ASSERT_OK(err, "fexit_first_attach"))
52 		goto cleanup;
53 
54 	err = fexit_test(fexit_skel);
55 	ASSERT_OK(err, "fexit_second_attach");
56 
57 cleanup:
58 	fexit_test__destroy(fexit_skel);
59 }
60