summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/fsession_test.c
blob: a299aeb8cc2e475da415564b955c29219748c89d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 ChinaTelecom */
#include <test_progs.h>
#include "fsession_test.skel.h"

static int check_result(struct fsession_test *skel)
{
	LIBBPF_OPTS(bpf_test_run_opts, topts);
	int err, prog_fd;

	/* Trigger test function calls */
	prog_fd = bpf_program__fd(skel->progs.test1);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		return err;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		return topts.retval;

	for (int i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) {
		if (!ASSERT_EQ(((__u64 *)skel->bss)[i], 1, "test_result"))
			return -EINVAL;
	}

	return 0;
}

static void test_fsession_basic(void)
{
	struct fsession_test *skel = NULL;
	int err;

	skel = fsession_test__open();
	if (!ASSERT_OK_PTR(skel, "fsession_test__open"))
		return;

	err = fsession_test__load(skel);
	if (err == -EOPNOTSUPP) {
		test__skip();
		goto cleanup;
	}
	if (!ASSERT_OK(err, "fsession_test__load"))
		goto cleanup;

	err = fsession_test__attach(skel);
	if (!ASSERT_OK(err, "fsession_attach"))
		goto cleanup;

	check_result(skel);
cleanup:
	fsession_test__destroy(skel);
}

static void test_fsession_reattach(void)
{
	struct fsession_test *skel = NULL;
	int err;

	skel = fsession_test__open();
	if (!ASSERT_OK_PTR(skel, "fsession_test__open"))
		return;

	err = fsession_test__load(skel);
	if (err == -EOPNOTSUPP) {
		test__skip();
		goto cleanup;
	}
	if (!ASSERT_OK(err, "fsession_test__load"))
		goto cleanup;

	/* first attach */
	err = fsession_test__attach(skel);
	if (!ASSERT_OK(err, "fsession_first_attach"))
		goto cleanup;

	if (check_result(skel))
		goto cleanup;

	/* detach */
	fsession_test__detach(skel);

	/* reset counters */
	memset(skel->bss, 0, sizeof(*skel->bss));

	/* second attach */
	err = fsession_test__attach(skel);
	if (!ASSERT_OK(err, "fsession_second_attach"))
		goto cleanup;

	if (check_result(skel))
		goto cleanup;

cleanup:
	fsession_test__destroy(skel);
}

static void test_fsession_cookie(void)
{
	struct fsession_test *skel = NULL;
	int err;

	skel = fsession_test__open();
	if (!ASSERT_OK_PTR(skel, "fsession_test__open"))
		goto cleanup;

	/*
	 * The test_fsession_basic() will test the session cookie with
	 * bpf_get_func_ip() case, so we need only check
	 * the cookie without bpf_get_func_ip() case here
	 */
	bpf_program__set_autoload(skel->progs.test6, false);

	err = fsession_test__load(skel);
	if (err == -EOPNOTSUPP) {
		test__skip();
		goto cleanup;
	}
	if (!ASSERT_OK(err, "fsession_test__load"))
		goto cleanup;

	err = fsession_test__attach(skel);
	if (!ASSERT_OK(err, "fsession_attach"))
		goto cleanup;

	skel->bss->test6_entry_result = 1;
	skel->bss->test6_exit_result = 1;

	check_result(skel);
cleanup:
	fsession_test__destroy(skel);
}

void test_fsession_test(void)
{
	if (test__start_subtest("fsession_test"))
		test_fsession_basic();
	if (test__start_subtest("fsession_reattach"))
		test_fsession_reattach();
	if (test__start_subtest("fsession_cookie"))
		test_fsession_cookie();
}