1215249f6SDave Marchevsky // SPDX-License-Identifier: GPL-2.0
2215249f6SDave Marchevsky /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3215249f6SDave Marchevsky
4215249f6SDave Marchevsky #include <test_progs.h>
5215249f6SDave Marchevsky #include <network_helpers.h>
6215249f6SDave Marchevsky
7215249f6SDave Marchevsky #include "rbtree.skel.h"
8215249f6SDave Marchevsky #include "rbtree_fail.skel.h"
9215249f6SDave Marchevsky #include "rbtree_btf_fail__wrong_node_type.skel.h"
10215249f6SDave Marchevsky #include "rbtree_btf_fail__add_wrong_type.skel.h"
11215249f6SDave Marchevsky
test_rbtree_add_nodes(void)12215249f6SDave Marchevsky static void test_rbtree_add_nodes(void)
13215249f6SDave Marchevsky {
14215249f6SDave Marchevsky LIBBPF_OPTS(bpf_test_run_opts, opts,
15215249f6SDave Marchevsky .data_in = &pkt_v4,
16215249f6SDave Marchevsky .data_size_in = sizeof(pkt_v4),
17215249f6SDave Marchevsky .repeat = 1,
18215249f6SDave Marchevsky );
19215249f6SDave Marchevsky struct rbtree *skel;
20215249f6SDave Marchevsky int ret;
21215249f6SDave Marchevsky
22215249f6SDave Marchevsky skel = rbtree__open_and_load();
23215249f6SDave Marchevsky if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load"))
24215249f6SDave Marchevsky return;
25215249f6SDave Marchevsky
26215249f6SDave Marchevsky ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_nodes), &opts);
27215249f6SDave Marchevsky ASSERT_OK(ret, "rbtree_add_nodes run");
28215249f6SDave Marchevsky ASSERT_OK(opts.retval, "rbtree_add_nodes retval");
29215249f6SDave Marchevsky ASSERT_EQ(skel->data->less_callback_ran, 1, "rbtree_add_nodes less_callback_ran");
30215249f6SDave Marchevsky
31215249f6SDave Marchevsky rbtree__destroy(skel);
32215249f6SDave Marchevsky }
33215249f6SDave Marchevsky
test_rbtree_add_and_remove(void)34215249f6SDave Marchevsky static void test_rbtree_add_and_remove(void)
35215249f6SDave Marchevsky {
36215249f6SDave Marchevsky LIBBPF_OPTS(bpf_test_run_opts, opts,
37215249f6SDave Marchevsky .data_in = &pkt_v4,
38215249f6SDave Marchevsky .data_size_in = sizeof(pkt_v4),
39215249f6SDave Marchevsky .repeat = 1,
40215249f6SDave Marchevsky );
41215249f6SDave Marchevsky struct rbtree *skel;
42215249f6SDave Marchevsky int ret;
43215249f6SDave Marchevsky
44215249f6SDave Marchevsky skel = rbtree__open_and_load();
45215249f6SDave Marchevsky if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load"))
46215249f6SDave Marchevsky return;
47215249f6SDave Marchevsky
48215249f6SDave Marchevsky ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_add_and_remove), &opts);
49215249f6SDave Marchevsky ASSERT_OK(ret, "rbtree_add_and_remove");
50215249f6SDave Marchevsky ASSERT_OK(opts.retval, "rbtree_add_and_remove retval");
51215249f6SDave Marchevsky ASSERT_EQ(skel->data->removed_key, 5, "rbtree_add_and_remove first removed key");
52215249f6SDave Marchevsky
53215249f6SDave Marchevsky rbtree__destroy(skel);
54215249f6SDave Marchevsky }
55215249f6SDave Marchevsky
test_rbtree_first_and_remove(void)56215249f6SDave Marchevsky static void test_rbtree_first_and_remove(void)
57215249f6SDave Marchevsky {
58215249f6SDave Marchevsky LIBBPF_OPTS(bpf_test_run_opts, opts,
59215249f6SDave Marchevsky .data_in = &pkt_v4,
60215249f6SDave Marchevsky .data_size_in = sizeof(pkt_v4),
61215249f6SDave Marchevsky .repeat = 1,
62215249f6SDave Marchevsky );
63215249f6SDave Marchevsky struct rbtree *skel;
64215249f6SDave Marchevsky int ret;
65215249f6SDave Marchevsky
66215249f6SDave Marchevsky skel = rbtree__open_and_load();
67215249f6SDave Marchevsky if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load"))
68215249f6SDave Marchevsky return;
69215249f6SDave Marchevsky
70215249f6SDave Marchevsky ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_first_and_remove), &opts);
71215249f6SDave Marchevsky ASSERT_OK(ret, "rbtree_first_and_remove");
72215249f6SDave Marchevsky ASSERT_OK(opts.retval, "rbtree_first_and_remove retval");
73215249f6SDave Marchevsky ASSERT_EQ(skel->data->first_data[0], 2, "rbtree_first_and_remove first rbtree_first()");
74215249f6SDave Marchevsky ASSERT_EQ(skel->data->removed_key, 1, "rbtree_first_and_remove first removed key");
75215249f6SDave Marchevsky ASSERT_EQ(skel->data->first_data[1], 4, "rbtree_first_and_remove second rbtree_first()");
76215249f6SDave Marchevsky
77215249f6SDave Marchevsky rbtree__destroy(skel);
78215249f6SDave Marchevsky }
79215249f6SDave Marchevsky
test_rbtree_api_release_aliasing(void)80*404ad75aSDave Marchevsky static void test_rbtree_api_release_aliasing(void)
81*404ad75aSDave Marchevsky {
82*404ad75aSDave Marchevsky LIBBPF_OPTS(bpf_test_run_opts, opts,
83*404ad75aSDave Marchevsky .data_in = &pkt_v4,
84*404ad75aSDave Marchevsky .data_size_in = sizeof(pkt_v4),
85*404ad75aSDave Marchevsky .repeat = 1,
86*404ad75aSDave Marchevsky );
87*404ad75aSDave Marchevsky struct rbtree *skel;
88*404ad75aSDave Marchevsky int ret;
89*404ad75aSDave Marchevsky
90*404ad75aSDave Marchevsky skel = rbtree__open_and_load();
91*404ad75aSDave Marchevsky if (!ASSERT_OK_PTR(skel, "rbtree__open_and_load"))
92*404ad75aSDave Marchevsky return;
93*404ad75aSDave Marchevsky
94*404ad75aSDave Marchevsky ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_api_release_aliasing), &opts);
95*404ad75aSDave Marchevsky ASSERT_OK(ret, "rbtree_api_release_aliasing");
96*404ad75aSDave Marchevsky ASSERT_OK(opts.retval, "rbtree_api_release_aliasing retval");
97*404ad75aSDave Marchevsky ASSERT_EQ(skel->data->first_data[0], 42, "rbtree_api_release_aliasing first rbtree_remove()");
98*404ad75aSDave Marchevsky ASSERT_EQ(skel->data->first_data[1], -1, "rbtree_api_release_aliasing second rbtree_remove()");
99*404ad75aSDave Marchevsky
100*404ad75aSDave Marchevsky rbtree__destroy(skel);
101*404ad75aSDave Marchevsky }
102*404ad75aSDave Marchevsky
test_rbtree_success(void)103215249f6SDave Marchevsky void test_rbtree_success(void)
104215249f6SDave Marchevsky {
105215249f6SDave Marchevsky if (test__start_subtest("rbtree_add_nodes"))
106215249f6SDave Marchevsky test_rbtree_add_nodes();
107215249f6SDave Marchevsky if (test__start_subtest("rbtree_add_and_remove"))
108215249f6SDave Marchevsky test_rbtree_add_and_remove();
109215249f6SDave Marchevsky if (test__start_subtest("rbtree_first_and_remove"))
110215249f6SDave Marchevsky test_rbtree_first_and_remove();
111*404ad75aSDave Marchevsky if (test__start_subtest("rbtree_api_release_aliasing"))
112*404ad75aSDave Marchevsky test_rbtree_api_release_aliasing();
113215249f6SDave Marchevsky }
114215249f6SDave Marchevsky
115215249f6SDave Marchevsky #define BTF_FAIL_TEST(suffix) \
116215249f6SDave Marchevsky void test_rbtree_btf_fail__##suffix(void) \
117215249f6SDave Marchevsky { \
118215249f6SDave Marchevsky struct rbtree_btf_fail__##suffix *skel; \
119215249f6SDave Marchevsky \
120215249f6SDave Marchevsky skel = rbtree_btf_fail__##suffix##__open_and_load(); \
121215249f6SDave Marchevsky if (!ASSERT_ERR_PTR(skel, \
122215249f6SDave Marchevsky "rbtree_btf_fail__" #suffix "__open_and_load unexpected success")) \
123215249f6SDave Marchevsky rbtree_btf_fail__##suffix##__destroy(skel); \
124215249f6SDave Marchevsky }
125215249f6SDave Marchevsky
126215249f6SDave Marchevsky #define RUN_BTF_FAIL_TEST(suffix) \
127215249f6SDave Marchevsky if (test__start_subtest("rbtree_btf_fail__" #suffix)) \
128215249f6SDave Marchevsky test_rbtree_btf_fail__##suffix();
129215249f6SDave Marchevsky
130215249f6SDave Marchevsky BTF_FAIL_TEST(wrong_node_type);
131215249f6SDave Marchevsky BTF_FAIL_TEST(add_wrong_type);
132215249f6SDave Marchevsky
test_rbtree_btf_fail(void)133215249f6SDave Marchevsky void test_rbtree_btf_fail(void)
134215249f6SDave Marchevsky {
135215249f6SDave Marchevsky RUN_BTF_FAIL_TEST(wrong_node_type);
136215249f6SDave Marchevsky RUN_BTF_FAIL_TEST(add_wrong_type);
137215249f6SDave Marchevsky }
138215249f6SDave Marchevsky
test_rbtree_fail(void)139215249f6SDave Marchevsky void test_rbtree_fail(void)
140215249f6SDave Marchevsky {
141215249f6SDave Marchevsky RUN_TESTS(rbtree_fail);
142215249f6SDave Marchevsky }
143