1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
15 typedef int32_t (*rte_rib6_test)(void);
17 static int32_t test_create_invalid(void);
18 static int32_t test_multiple_create(void);
19 static int32_t test_free_null(void);
20 static int32_t test_insert_invalid(void);
21 static int32_t test_get_fn(void);
22 static int32_t test_basic(void);
23 static int32_t test_tree_traversal(void);
26 #define MAX_RULES (1 << 22)
29 * Check that rte_rib6_create fails gracefully for incorrect user input
33 test_create_invalid(void)
35 struct rte_rib6 *rib = NULL;
36 struct rte_rib6_conf config;
38 config.max_nodes = MAX_RULES;
41 /* rte_rib6_create: rib name == NULL */
42 rib = rte_rib6_create(NULL, SOCKET_ID_ANY, &config);
43 RTE_TEST_ASSERT(rib == NULL,
44 "Call succeeded with invalid parameters\n");
46 /* rte_rib6_create: config == NULL */
47 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, NULL);
48 RTE_TEST_ASSERT(rib == NULL,
49 "Call succeeded with invalid parameters\n");
51 /* socket_id < -1 is invalid */
52 rib = rte_rib6_create(__func__, -2, &config);
53 RTE_TEST_ASSERT(rib == NULL,
54 "Call succeeded with invalid parameters\n");
56 /* rte_rib6_create: max_nodes = 0 */
58 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
59 RTE_TEST_ASSERT(rib == NULL,
60 "Call succeeded with invalid parameters\n");
61 config.max_nodes = MAX_RULES;
67 * Create rib table then delete rib table 10 times
68 * Use a slightly different rules size each time
71 test_multiple_create(void)
73 struct rte_rib6 *rib = NULL;
74 struct rte_rib6_conf config;
79 for (i = 0; i < 100; i++) {
80 config.max_nodes = MAX_RULES - i;
81 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
82 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
85 /* Can not test free so return success */
90 * Call rte_rib6_free for NULL pointer user input. Note: free has no return and
91 * therefore it is impossible to check for failure but this test is added to
92 * increase function coverage metrics and to validate that freeing null does
98 struct rte_rib6 *rib = NULL;
99 struct rte_rib6_conf config;
101 config.max_nodes = MAX_RULES;
104 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
105 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
113 * Check that rte_rib6_insert fails gracefully
114 * for incorrect user input arguments
117 test_insert_invalid(void)
119 struct rte_rib6 *rib = NULL;
120 struct rte_rib6_node *node, *node1;
121 struct rte_rib6_conf config;
122 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE] = {0};
125 config.max_nodes = MAX_RULES;
128 /* rte_rib6_insert: rib == NULL */
129 node = rte_rib6_insert(NULL, ip, depth);
130 RTE_TEST_ASSERT(node == NULL,
131 "Call succeeded with invalid parameters\n");
133 /*Create valid rib to use in rest of test. */
134 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
135 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
137 /* rte_rib6_insert: depth > MAX_DEPTH */
138 node = rte_rib6_insert(rib, ip, MAX_DEPTH + 1);
139 RTE_TEST_ASSERT(node == NULL,
140 "Call succeeded with invalid parameters\n");
142 /* insert the same ip/depth twice*/
143 node = rte_rib6_insert(rib, ip, depth);
144 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
145 node1 = rte_rib6_insert(rib, ip, depth);
146 RTE_TEST_ASSERT(node1 == NULL,
147 "Call succeeded with invalid parameters\n");
155 * Call rte_rib6_node access functions with incorrect input.
156 * After call rte_rib6_node access functions with correct args
157 * and check the return values for correctness
162 struct rte_rib6 *rib = NULL;
163 struct rte_rib6_node *node;
164 struct rte_rib6_conf config;
166 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE] = {192, 0, 2, 0, 0, 0, 0, 0,
167 0, 0, 0, 0, 0, 0, 0, 0};
168 uint8_t ip_ret[RTE_RIB6_IPV6_ADDR_SIZE];
169 uint64_t nh_set = 10;
175 config.max_nodes = MAX_RULES;
178 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
179 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
181 node = rte_rib6_insert(rib, ip, depth);
182 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
184 /* test rte_rib6_get_ip() with incorrect args */
185 ret = rte_rib6_get_ip(NULL, ip_ret);
186 RTE_TEST_ASSERT(ret < 0,
187 "Call succeeded with invalid parameters\n");
188 ret = rte_rib6_get_ip(node, NULL);
189 RTE_TEST_ASSERT(ret < 0,
190 "Call succeeded with invalid parameters\n");
192 /* test rte_rib6_get_depth() with incorrect args */
193 ret = rte_rib6_get_depth(NULL, &depth_ret);
194 RTE_TEST_ASSERT(ret < 0,
195 "Call succeeded with invalid parameters\n");
196 ret = rte_rib6_get_depth(node, NULL);
197 RTE_TEST_ASSERT(ret < 0,
198 "Call succeeded with invalid parameters\n");
200 /* test rte_rib6_set_nh() with incorrect args */
201 ret = rte_rib6_set_nh(NULL, nh_set);
202 RTE_TEST_ASSERT(ret < 0,
203 "Call succeeded with invalid parameters\n");
205 /* test rte_rib6_get_nh() with incorrect args */
206 ret = rte_rib6_get_nh(NULL, &nh_ret);
207 RTE_TEST_ASSERT(ret < 0,
208 "Call succeeded with invalid parameters\n");
209 ret = rte_rib6_get_nh(node, NULL);
210 RTE_TEST_ASSERT(ret < 0,
211 "Call succeeded with invalid parameters\n");
213 /* test rte_rib6_get_ext() with incorrect args */
214 ext = rte_rib6_get_ext(NULL);
215 RTE_TEST_ASSERT(ext == NULL,
216 "Call succeeded with invalid parameters\n");
218 /* check the return values */
219 ret = rte_rib6_get_ip(node, ip_ret);
220 RTE_TEST_ASSERT((ret == 0) && (rte_rib6_is_equal(ip_ret, ip)),
221 "Failed to get proper node ip\n");
222 ret = rte_rib6_get_depth(node, &depth_ret);
223 RTE_TEST_ASSERT((ret == 0) && (depth_ret == depth),
224 "Failed to get proper node depth\n");
225 ret = rte_rib6_set_nh(node, nh_set);
226 RTE_TEST_ASSERT(ret == 0,
227 "Failed to set rte_rib_node nexthop\n");
228 ret = rte_rib6_get_nh(node, &nh_ret);
229 RTE_TEST_ASSERT((ret == 0) && (nh_ret == nh_set),
230 "Failed to get proper nexthop\n");
238 * Call insert, lookup/lookup_exact and delete for a single rule
243 struct rte_rib6 *rib = NULL;
244 struct rte_rib6_node *node;
245 struct rte_rib6_conf config;
247 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE] = {192, 0, 2, 0, 0, 0, 0, 0,
248 0, 0, 0, 0, 0, 0, 0, 0};
249 uint64_t next_hop_add = 10;
250 uint64_t next_hop_return;
254 config.max_nodes = MAX_RULES;
257 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
258 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
260 node = rte_rib6_insert(rib, ip, depth);
261 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
263 status = rte_rib6_set_nh(node, next_hop_add);
264 RTE_TEST_ASSERT(status == 0,
265 "Failed to set rte_rib_node field\n");
267 node = rte_rib6_lookup(rib, ip);
268 RTE_TEST_ASSERT(node != NULL, "Failed to lookup\n");
270 status = rte_rib6_get_nh(node, &next_hop_return);
271 RTE_TEST_ASSERT((status == 0) && (next_hop_add == next_hop_return),
272 "Failed to get proper nexthop\n");
274 node = rte_rib6_lookup_exact(rib, ip, depth);
275 RTE_TEST_ASSERT(node != NULL,
276 "Failed to lookup\n");
278 status = rte_rib6_get_nh(node, &next_hop_return);
279 RTE_TEST_ASSERT((status == 0) && (next_hop_add == next_hop_return),
280 "Failed to get proper nexthop\n");
282 rte_rib6_remove(rib, ip, depth);
284 node = rte_rib6_lookup(rib, ip);
285 RTE_TEST_ASSERT(node == NULL,
286 "Lookup returns non existent rule\n");
287 node = rte_rib6_lookup_exact(rib, ip, depth);
288 RTE_TEST_ASSERT(node == NULL,
289 "Lookup returns non existent rule\n");
297 test_tree_traversal(void)
299 struct rte_rib6 *rib = NULL;
300 struct rte_rib6_node *node;
301 struct rte_rib6_conf config;
303 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE] = {10, 0, 2, 130, 0, 0, 0, 0,
304 0, 0, 0, 0, 0, 0, 0, 0};
305 uint8_t ip1[RTE_RIB6_IPV6_ADDR_SIZE] = {10, 0, 2, 0, 0, 0, 0, 0,
306 0, 0, 0, 0, 0, 0, 0, 0};
307 uint8_t ip2[RTE_RIB6_IPV6_ADDR_SIZE] = {10, 0, 2, 130, 0, 0, 0, 0,
308 0, 0, 0, 0, 0, 0, 0, 80};
311 config.max_nodes = MAX_RULES;
314 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
315 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
317 node = rte_rib6_insert(rib, ip1, depth);
318 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
319 node = rte_rib6_insert(rib, ip2, depth);
320 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
323 node = rte_rib6_get_nxt(rib, ip, 32, node, RTE_RIB6_GET_NXT_ALL);
324 RTE_TEST_ASSERT(node != NULL, "Failed to get rib_node\n");
331 static struct unit_test_suite rib6_tests = {
332 .suite_name = "rib6 autotest",
336 TEST_CASE(test_create_invalid),
337 TEST_CASE(test_free_null),
338 TEST_CASE(test_insert_invalid),
339 TEST_CASE(test_get_fn),
340 TEST_CASE(test_basic),
341 TEST_CASE(test_tree_traversal),
346 static struct unit_test_suite rib6_slow_tests = {
347 .suite_name = "rib6 slow autotest",
351 TEST_CASE(test_multiple_create),
362 return unit_test_suite_runner(&rib6_tests);
368 return unit_test_suite_runner(&rib6_slow_tests);
371 REGISTER_TEST_COMMAND(rib6_autotest, test_rib6);
372 REGISTER_TEST_COMMAND(rib6_slow_autotest, test_slow_rib6);