blob: dbbf0dd664eabce050ca25f06b67d5b3d28a8747 [file] [log] [blame]
Josh Bailey42ea6852011-07-20 20:44:23 -07001/* $QuaggaId: Format:%an, %ai, %h$ $
2 *
3 * BGP Multipath Unit Test
4 * Copyright (C) 2010 Google Inc.
5 *
6 * This file is part of Quagga
7 *
8 * Quagga is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * Quagga is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Quagga; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#include <zebra.h>
25
26#include "vty.h"
27#include "stream.h"
28#include "privs.h"
29#include "linklist.h"
30#include "memory.h"
31#include "zclient.h"
32
33#include "bgpd/bgpd.h"
Josh Bailey42ea6852011-07-20 20:44:23 -070034#include "bgpd/bgp_table.h"
35#include "bgpd/bgp_route.h"
Josh Bailey96450fa2011-07-20 20:45:12 -070036#include "bgpd/bgp_attr.h"
37#include "bgpd/bgp_mpath.h"
Josh Bailey42ea6852011-07-20 20:44:23 -070038
39#define VT100_RESET "\x1b[0m"
40#define VT100_RED "\x1b[31m"
41#define VT100_GREEN "\x1b[32m"
42#define VT100_YELLOW "\x1b[33m"
43#define OK VT100_GREEN "OK" VT100_RESET
44#define FAILED VT100_RED "failed" VT100_RESET
45
46#define TEST_PASSED 0
47#define TEST_FAILED -1
48
49#define EXPECT_TRUE(expr, res) \
50 if (!(expr)) \
51 { \
52 printf ("Test failure in %s line %u: %s\n", \
53 __FUNCTION__, __LINE__, #expr); \
54 (res) = TEST_FAILED; \
55 }
56
57typedef struct testcase_t__ testcase_t;
58
59typedef int (*test_setup_func)(testcase_t *);
60typedef int (*test_run_func)(testcase_t *);
61typedef int (*test_cleanup_func)(testcase_t *);
62
63struct testcase_t__ {
64 const char *desc;
65 void *test_data;
66 void *verify_data;
67 void *tmp_data;
68 test_setup_func setup;
69 test_run_func run;
70 test_cleanup_func cleanup;
71};
72
73/* need these to link in libbgp */
74struct thread_master *master = NULL;
75struct zclient *zclient;
76struct zebra_privs_t bgpd_privs =
77{
78 .user = NULL,
79 .group = NULL,
80 .vty_group = NULL,
81};
82
83static int tty = 0;
84
85/* Create fake bgp instance */
86static struct bgp *
87bgp_create_fake (as_t *as, const char *name)
88{
89 struct bgp *bgp;
90 afi_t afi;
91 safi_t safi;
92
93 if ( (bgp = XCALLOC (MTYPE_BGP, sizeof (struct bgp))) == NULL)
94 return NULL;
95
96 bgp_lock (bgp);
97 //bgp->peer_self = peer_new (bgp);
98 //bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static announcement");
99
100 bgp->peer = list_new ();
101 //bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
102
103 bgp->group = list_new ();
104 //bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp;
105
106 bgp->rsclient = list_new ();
107 //bgp->rsclient->cmp = (int (*)(void*, void*)) peer_cmp;
108
109 for (afi = AFI_IP; afi < AFI_MAX; afi++)
110 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
111 {
112 bgp->route[afi][safi] = bgp_table_init (afi, safi);
113 bgp->aggregate[afi][safi] = bgp_table_init (afi, safi);
114 bgp->rib[afi][safi] = bgp_table_init (afi, safi);
115 bgp->maxpaths[afi][safi].maxpaths_ebgp = BGP_DEFAULT_MAXPATHS;
116 bgp->maxpaths[afi][safi].maxpaths_ibgp = BGP_DEFAULT_MAXPATHS;
117 }
118
119 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
120 bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
121 bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
122 bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
123 bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
124
125 bgp->as = *as;
126
127 if (name)
128 bgp->name = strdup (name);
129
130 return bgp;
131}
132
133/*=========================================================
134 * Testcase for maximum-paths configuration
135 */
136static int
137setup_bgp_cfg_maximum_paths (testcase_t *t)
138{
139 as_t asn = 1;
140 t->tmp_data = bgp_create_fake (&asn, NULL);
141 if (!t->tmp_data)
142 return -1;
143 return 0;
144}
145
146static int
147run_bgp_cfg_maximum_paths (testcase_t *t)
148{
149 afi_t afi;
150 safi_t safi;
151 struct bgp *bgp;
152 int api_result;
153 int test_result = TEST_PASSED;
154
155 bgp = t->tmp_data;
156 for (afi = AFI_IP; afi < AFI_MAX; afi++)
157 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
158 {
159 /* test bgp_maximum_paths_set */
160 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_EBGP, 10);
161 EXPECT_TRUE (api_result == 0, test_result);
162 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_IBGP, 10);
163 EXPECT_TRUE (api_result == 0, test_result);
164 EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ebgp == 10, test_result);
165 EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ibgp == 10, test_result);
166
167 /* test bgp_maximum_paths_unset */
168 api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_EBGP);
169 EXPECT_TRUE (api_result == 0, test_result);
170 api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_IBGP);
171 EXPECT_TRUE (api_result == 0, test_result);
172 EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ebgp ==
173 BGP_DEFAULT_MAXPATHS), test_result);
174 EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ibgp ==
175 BGP_DEFAULT_MAXPATHS), test_result);
176 }
177
178 return test_result;
179}
180
181static int
182cleanup_bgp_cfg_maximum_paths (testcase_t *t)
183{
184 return bgp_delete ((struct bgp *)t->tmp_data);
185}
186
187testcase_t test_bgp_cfg_maximum_paths = {
188 .desc = "Test bgp maximum-paths config",
189 .setup = setup_bgp_cfg_maximum_paths,
190 .run = run_bgp_cfg_maximum_paths,
191 .cleanup = cleanup_bgp_cfg_maximum_paths,
192};
193
194/*=========================================================
Josh Bailey96450fa2011-07-20 20:45:12 -0700195 * Testcase for bgp_mp_list
196 */
Paul Jakma6d4742b2015-11-25 17:14:37 +0000197
198struct bgp test_mp_bgp;
199
Josh Baileyde8d5df2011-07-20 20:46:01 -0700200struct peer test_mp_list_peer[] = {
Paul Jakma6d4742b2015-11-25 17:14:37 +0000201 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
202 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
203 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
204 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
205 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
Josh Baileyde8d5df2011-07-20 20:46:01 -0700206};
Josh Bailey96450fa2011-07-20 20:45:12 -0700207int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer);
208struct attr test_mp_list_attr[4];
209struct bgp_info test_mp_list_info[] = {
210 { .peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0] },
211 { .peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1] },
212 { .peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1] },
213 { .peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2] },
214 { .peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3] },
215};
216int test_mp_list_info_count =
217 sizeof (test_mp_list_info)/sizeof (struct bgp_info);
218
219static int
220setup_bgp_mp_list (testcase_t *t)
221{
222 test_mp_list_attr[0].nexthop.s_addr = 0x01010101;
223 test_mp_list_attr[1].nexthop.s_addr = 0x02020202;
224 test_mp_list_attr[2].nexthop.s_addr = 0x03030303;
225 test_mp_list_attr[3].nexthop.s_addr = 0x04040404;
226
227 if ((test_mp_list_peer[0].su_remote = sockunion_str2su ("1.1.1.1")) == NULL)
228 return -1;
229 if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL)
230 return -1;
231 if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL)
232 return -1;
233 if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL)
234 return -1;
235 if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL)
236 return -1;
237
238 return 0;
239}
240
241static int
242run_bgp_mp_list (testcase_t *t)
243{
244 struct list mp_list;
245 struct listnode *mp_node;
246 struct bgp_info *info;
247 int i;
248 int test_result = TEST_PASSED;
249 bgp_mp_list_init (&mp_list);
250 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
251
252 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
253 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
254 bgp_mp_list_add (&mp_list, &test_mp_list_info[2]);
255 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
256 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
257
David Lamparterc3138952015-04-21 10:02:23 +0200258 for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count;
Josh Bailey96450fa2011-07-20 20:45:12 -0700259 i++, mp_node = listnextnode(mp_node))
260 {
261 info = listgetdata(mp_node);
262 EXPECT_TRUE (info == &test_mp_list_info[i], test_result);
263 }
264
265 bgp_mp_list_clear (&mp_list);
266 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
267
268 return test_result;
269}
270
271static int
272cleanup_bgp_mp_list (testcase_t *t)
273{
274 int i;
275
276 for (i = 0; i < test_mp_list_peer_count; i++)
277 sockunion_free (test_mp_list_peer[i].su_remote);
278
279 return 0;
280}
281
282testcase_t test_bgp_mp_list = {
283 .desc = "Test bgp_mp_list",
284 .setup = setup_bgp_mp_list,
285 .run = run_bgp_mp_list,
286 .cleanup = cleanup_bgp_mp_list,
287};
288
289/*=========================================================
Josh Baileyde8d5df2011-07-20 20:46:01 -0700290 * Testcase for bgp_info_mpath_update
291 */
292
293struct bgp_node test_rn;
294
295static int
296setup_bgp_info_mpath_update (testcase_t *t)
297{
298 int i;
299 str2prefix ("42.1.1.0/24", &test_rn.p);
300 setup_bgp_mp_list (t);
301 for (i = 0; i < test_mp_list_info_count; i++)
302 bgp_info_add (&test_rn, &test_mp_list_info[i]);
303 return 0;
304}
305
306static int
307run_bgp_info_mpath_update (testcase_t *t)
308{
309 struct bgp_info *new_best, *old_best, *mpath;
310 struct list mp_list;
Paul Jakma6d4742b2015-11-25 17:14:37 +0000311
312 test_mp_bgp.maxpaths[AFI_IP][SAFI_UNICAST].maxpaths_ebgp = 3;
313 test_mp_bgp.maxpaths[AFI_IP][SAFI_UNICAST].maxpaths_ibgp = 3;
314
Josh Baileyde8d5df2011-07-20 20:46:01 -0700315 int test_result = TEST_PASSED;
316 bgp_mp_list_init (&mp_list);
317 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
318 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
319 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
320 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
321 new_best = &test_mp_list_info[3];
322 old_best = NULL;
Paul Jakma6d4742b2015-11-25 17:14:37 +0000323 bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, AFI_IP, SAFI_UNICAST);
Josh Baileyde8d5df2011-07-20 20:46:01 -0700324 bgp_mp_list_clear (&mp_list);
325 EXPECT_TRUE (bgp_info_mpath_count (new_best) == 2, test_result);
326 mpath = bgp_info_mpath_first (new_best);
327 EXPECT_TRUE (mpath == &test_mp_list_info[0], test_result);
328 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
329 mpath = bgp_info_mpath_next (mpath);
330 EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
331 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
332
333 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
334 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
335 new_best = &test_mp_list_info[0];
336 old_best = &test_mp_list_info[3];
Paul Jakma6d4742b2015-11-25 17:14:37 +0000337 bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, AFI_IP, SAFI_UNICAST);
Josh Baileyde8d5df2011-07-20 20:46:01 -0700338 bgp_mp_list_clear (&mp_list);
339 EXPECT_TRUE (bgp_info_mpath_count (new_best) == 1, test_result);
340 mpath = bgp_info_mpath_first (new_best);
341 EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
342 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
343 EXPECT_TRUE (!CHECK_FLAG (test_mp_list_info[0].flags, BGP_INFO_MULTIPATH),
344 test_result);
345
346 return test_result;
347}
348
349static int
350cleanup_bgp_info_mpath_update (testcase_t *t)
351{
352 int i;
353
354 for (i = 0; i < test_mp_list_peer_count; i++)
355 sockunion_free (test_mp_list_peer[i].su_remote);
356
357 return 0;
358}
359
360testcase_t test_bgp_info_mpath_update = {
361 .desc = "Test bgp_info_mpath_update",
362 .setup = setup_bgp_info_mpath_update,
363 .run = run_bgp_info_mpath_update,
364 .cleanup = cleanup_bgp_info_mpath_update,
365};
366
367/*=========================================================
Josh Bailey42ea6852011-07-20 20:44:23 -0700368 * Set up testcase vector
369 */
370testcase_t *all_tests[] = {
371 &test_bgp_cfg_maximum_paths,
Josh Bailey96450fa2011-07-20 20:45:12 -0700372 &test_bgp_mp_list,
Josh Baileyde8d5df2011-07-20 20:46:01 -0700373 &test_bgp_info_mpath_update,
Josh Bailey42ea6852011-07-20 20:44:23 -0700374};
375
376int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *));
377
378/*=========================================================
379 * Test Driver Functions
380 */
381static int
382global_test_init (void)
383{
384 master = thread_master_create ();
Donald Sharp71252932015-09-24 09:25:19 -0400385 zclient = zclient_new (master);
Josh Bailey42ea6852011-07-20 20:44:23 -0700386 bgp_master_init ();
Paul Jakmac9e4f862012-06-14 10:42:39 +0100387 bgp_option_set (BGP_OPT_NO_LISTEN);
388
Josh Bailey42ea6852011-07-20 20:44:23 -0700389 if (fileno (stdout) >= 0)
390 tty = isatty (fileno (stdout));
391 return 0;
392}
393
394static int
395global_test_cleanup (void)
396{
397 zclient_free (zclient);
398 thread_master_free (master);
399 return 0;
400}
401
402static void
403display_result (testcase_t *test, int result)
404{
405 if (tty)
406 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED);
407 else
408 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED");
409}
410
411static int
412setup_test (testcase_t *t)
413{
414 int res = 0;
415 if (t->setup)
416 res = t->setup (t);
417 return res;
418}
419
420static int
421cleanup_test (testcase_t *t)
422{
423 int res = 0;
424 if (t->cleanup)
425 res = t->cleanup (t);
426 return res;
427}
428
429static void
430run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count)
431{
432 int test_index, result;
433 testcase_t *cur_test;
434
435 *pass_count = *fail_count = 0;
436
437 for (test_index = 0; test_index < num_tests; test_index++)
438 {
439 cur_test = tests[test_index];
440 if (!cur_test->desc)
441 {
442 printf ("error: test %d has no description!\n", test_index);
443 continue;
444 }
445 if (!cur_test->run)
446 {
447 printf ("error: test %s has no run function!\n", cur_test->desc);
448 continue;
449 }
450 if (setup_test (cur_test) != 0)
451 {
452 printf ("error: setup failed for test %s\n", cur_test->desc);
453 continue;
454 }
455 result = cur_test->run (cur_test);
456 if (result == TEST_PASSED)
457 *pass_count += 1;
458 else
459 *fail_count += 1;
460 display_result (cur_test, result);
461 if (cleanup_test (cur_test) != 0)
462 {
463 printf ("error: cleanup failed for test %s\n", cur_test->desc);
464 continue;
465 }
466 }
467}
468
469int
470main (void)
471{
472 int pass_count, fail_count;
473 time_t cur_time;
474
475 time (&cur_time);
476 printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
477 if (global_test_init () != 0)
478 {
479 printf("Global init failed. Terminating.\n");
480 exit(1);
481 }
482 run_tests (all_tests, all_tests_count, &pass_count, &fail_count);
483 global_test_cleanup ();
484 printf("Total pass/fail: %d/%d\n", pass_count, fail_count);
485 return fail_count;
486}