summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/rbtree.c
blob: 156fa95c42f61842856ced0649b56330e8ff43bd (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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */

#include <test_progs.h>
#include <network_helpers.h>

#include "rbtree.skel.h"
#include "rbtree_fail.skel.h"
#include "rbtree_btf_fail__wrong_node_type.skel.h"
#include "rbtree_btf_fail__add_wrong_type.skel.h"

static void test_rbtree_add_nodes(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		    .data_in = &pkt_v4,
		    .data_size_in = sizeof(pkt_v4),
		    .repeat = 1,
	);
	struct rbtree *skel;
	int ret;

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

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_nodes), &opts);
	ASSERT_OK(ret, "rbtree_add_nodes run");
	ASSERT_OK(opts.retval, "rbtree_add_nodes retval");
	ASSERT_EQ(skel->data->less_callback_ran, 1, "rbtree_add_nodes less_callback_ran");

	rbtree__destroy(skel);
}

static void test_rbtree_add_and_remove(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		    .data_in = &pkt_v4,
		    .data_size_in = sizeof(pkt_v4),
		    .repeat = 1,
	);
	struct rbtree *skel;
	int ret;

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

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_and_remove), &opts);
	ASSERT_OK(ret, "rbtree_add_and_remove");
	ASSERT_OK(opts.retval, "rbtree_add_and_remove retval");
	ASSERT_EQ(skel->data->removed_key, 5, "rbtree_add_and_remove first removed key");

	rbtree__destroy(skel);
}

static void test_rbtree_first_and_remove(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		    .data_in = &pkt_v4,
		    .data_size_in = sizeof(pkt_v4),
		    .repeat = 1,
	);
	struct rbtree *skel;
	int ret;

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

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_first_and_remove), &opts);
	ASSERT_OK(ret, "rbtree_first_and_remove");
	ASSERT_OK(opts.retval, "rbtree_first_and_remove retval");
	ASSERT_EQ(skel->data->first_data[0], 2, "rbtree_first_and_remove first rbtree_first()");
	ASSERT_EQ(skel->data->removed_key, 1, "rbtree_first_and_remove first removed key");
	ASSERT_EQ(skel->data->first_data[1], 4, "rbtree_first_and_remove second rbtree_first()");

	rbtree__destroy(skel);
}

void test_rbtree_success(void)
{
	if (test__start_subtest("rbtree_add_nodes"))
		test_rbtree_add_nodes();
	if (test__start_subtest("rbtree_add_and_remove"))
		test_rbtree_add_and_remove();
	if (test__start_subtest("rbtree_first_and_remove"))
		test_rbtree_first_and_remove();
}

#define BTF_FAIL_TEST(suffix)									\
void test_rbtree_btf_fail__##suffix(void)							\
{												\
	struct rbtree_btf_fail__##suffix *skel;							\
												\
	skel = rbtree_btf_fail__##suffix##__open_and_load();					\
	if (!ASSERT_ERR_PTR(skel,								\
			    "rbtree_btf_fail__" #suffix "__open_and_load unexpected success"))	\
		rbtree_btf_fail__##suffix##__destroy(skel);					\
}

#define RUN_BTF_FAIL_TEST(suffix)				\
	if (test__start_subtest("rbtree_btf_fail__" #suffix))	\
		test_rbtree_btf_fail__##suffix();

BTF_FAIL_TEST(wrong_node_type);
BTF_FAIL_TEST(add_wrong_type);

void test_rbtree_btf_fail(void)
{
	RUN_BTF_FAIL_TEST(wrong_node_type);
	RUN_BTF_FAIL_TEST(add_wrong_type);
}

void test_rbtree_fail(void)
{
	RUN_TESTS(rbtree_fail);
}