1 /* $QuaggaId: Format:%an, %ai, %h$ $
3 * BGP Multipath Unit Test
4 * Copyright (C) 2010 Google Inc.
6 * This file is part of Quagga
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
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.
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
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mpath.h"
41 #define VT100_RESET "\x1b[0m"
42 #define VT100_RED "\x1b[31m"
43 #define VT100_GREEN "\x1b[32m"
44 #define VT100_YELLOW "\x1b[33m"
45 #define OK VT100_GREEN "OK" VT100_RESET
46 #define FAILED VT100_RED "failed" VT100_RESET
49 #define TEST_FAILED -1
51 #define EXPECT_TRUE(expr, res) \
54 printf ("Test failure in %s line %u: %s\n", \
55 __FUNCTION__, __LINE__, #expr); \
56 (res) = TEST_FAILED; \
59 typedef struct testcase_t__ testcase_t
;
61 typedef int (*test_setup_func
)(testcase_t
*);
62 typedef int (*test_run_func
)(testcase_t
*);
63 typedef int (*test_cleanup_func
)(testcase_t
*);
70 test_setup_func setup
;
72 test_cleanup_func cleanup
;
75 /* need these to link in libbgp */
76 struct thread_master
*master
= NULL
;
77 struct zclient
*zclient
;
78 struct zebra_privs_t bgpd_privs
=
87 /* Create fake bgp instance */
89 bgp_create_fake (as_t
*as
, const char *name
)
95 if ( (bgp
= XCALLOC (MTYPE_BGP
, sizeof (struct bgp
))) == NULL
)
99 //bgp->peer_self = peer_new (bgp);
100 //bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static announcement");
102 bgp
->peer
= list_new ();
103 //bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
105 bgp
->group
= list_new ();
106 //bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp;
108 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
109 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
111 bgp
->route
[afi
][safi
] = bgp_table_init (afi
, safi
);
112 bgp
->aggregate
[afi
][safi
] = bgp_table_init (afi
, safi
);
113 bgp
->rib
[afi
][safi
] = bgp_table_init (afi
, safi
);
114 bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
= MULTIPATH_NUM
;
115 bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
= MULTIPATH_NUM
;
118 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
119 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
120 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
121 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
122 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
127 bgp
->name
= strdup (name
);
132 /*=========================================================
133 * Testcase for maximum-paths configuration
136 setup_bgp_cfg_maximum_paths (testcase_t
*t
)
139 t
->tmp_data
= bgp_create_fake (&asn
, NULL
);
146 run_bgp_cfg_maximum_paths (testcase_t
*t
)
152 int test_result
= TEST_PASSED
;
155 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
156 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
158 /* test bgp_maximum_paths_set */
159 api_result
= bgp_maximum_paths_set (bgp
, afi
, safi
, BGP_PEER_EBGP
, 10, 0);
160 EXPECT_TRUE (api_result
== 0, test_result
);
161 api_result
= bgp_maximum_paths_set (bgp
, afi
, safi
, BGP_PEER_IBGP
, 10, 0);
162 EXPECT_TRUE (api_result
== 0, test_result
);
163 EXPECT_TRUE (bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
== 10, test_result
);
164 EXPECT_TRUE (bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
== 10, test_result
);
166 /* test bgp_maximum_paths_unset */
167 api_result
= bgp_maximum_paths_unset (bgp
, afi
, safi
, BGP_PEER_EBGP
);
168 EXPECT_TRUE (api_result
== 0, test_result
);
169 api_result
= bgp_maximum_paths_unset (bgp
, afi
, safi
, BGP_PEER_IBGP
);
170 EXPECT_TRUE (api_result
== 0, test_result
);
171 EXPECT_TRUE ((bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
==
172 MULTIPATH_NUM
), test_result
);
173 EXPECT_TRUE ((bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
==
174 MULTIPATH_NUM
), test_result
);
181 cleanup_bgp_cfg_maximum_paths (testcase_t
*t
)
183 return bgp_delete ((struct bgp
*)t
->tmp_data
);
186 testcase_t test_bgp_cfg_maximum_paths
= {
187 .desc
= "Test bgp maximum-paths config",
188 .setup
= setup_bgp_cfg_maximum_paths
,
189 .run
= run_bgp_cfg_maximum_paths
,
190 .cleanup
= cleanup_bgp_cfg_maximum_paths
,
193 /*=========================================================
194 * Testcase for bgp_mp_list
196 struct peer test_mp_list_peer
[] = {
197 { .local_as
= 1, .as
= 2 },
198 { .local_as
= 1, .as
= 2 },
199 { .local_as
= 1, .as
= 2 },
200 { .local_as
= 1, .as
= 2 },
201 { .local_as
= 1, .as
= 2 },
203 int test_mp_list_peer_count
= sizeof (test_mp_list_peer
)/ sizeof (struct peer
);
204 struct attr test_mp_list_attr
[4];
205 struct bgp_info test_mp_list_info
[] = {
206 { .peer
= &test_mp_list_peer
[0], .attr
= &test_mp_list_attr
[0] },
207 { .peer
= &test_mp_list_peer
[1], .attr
= &test_mp_list_attr
[1] },
208 { .peer
= &test_mp_list_peer
[2], .attr
= &test_mp_list_attr
[1] },
209 { .peer
= &test_mp_list_peer
[3], .attr
= &test_mp_list_attr
[2] },
210 { .peer
= &test_mp_list_peer
[4], .attr
= &test_mp_list_attr
[3] },
212 int test_mp_list_info_count
=
213 sizeof (test_mp_list_info
)/sizeof (struct bgp_info
);
216 setup_bgp_mp_list (testcase_t
*t
)
218 test_mp_list_attr
[0].nexthop
.s_addr
= 0x01010101;
219 test_mp_list_attr
[1].nexthop
.s_addr
= 0x02020202;
220 test_mp_list_attr
[2].nexthop
.s_addr
= 0x03030303;
221 test_mp_list_attr
[3].nexthop
.s_addr
= 0x04040404;
223 if ((test_mp_list_peer
[0].su_remote
= sockunion_str2su ("1.1.1.1")) == NULL
)
225 if ((test_mp_list_peer
[1].su_remote
= sockunion_str2su ("2.2.2.2")) == NULL
)
227 if ((test_mp_list_peer
[2].su_remote
= sockunion_str2su ("3.3.3.3")) == NULL
)
229 if ((test_mp_list_peer
[3].su_remote
= sockunion_str2su ("4.4.4.4")) == NULL
)
231 if ((test_mp_list_peer
[4].su_remote
= sockunion_str2su ("5.5.5.5")) == NULL
)
238 run_bgp_mp_list (testcase_t
*t
)
241 struct listnode
*mp_node
;
242 struct bgp_info
*info
;
244 int test_result
= TEST_PASSED
;
245 bgp_mp_list_init (&mp_list
);
246 EXPECT_TRUE (listcount(&mp_list
) == 0, test_result
);
248 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[1]);
249 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[4]);
250 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[2]);
251 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[3]);
252 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[0]);
254 for (i
= 0, mp_node
= mp_list
.head
; i
< test_mp_list_info_count
;
255 i
++, mp_node
= listnextnode(mp_node
))
257 info
= listgetdata(mp_node
);
258 EXPECT_TRUE (info
== &test_mp_list_info
[i
], test_result
);
261 bgp_mp_list_clear (&mp_list
);
262 EXPECT_TRUE (listcount(&mp_list
) == 0, test_result
);
268 cleanup_bgp_mp_list (testcase_t
*t
)
272 for (i
= 0; i
< test_mp_list_peer_count
; i
++)
273 sockunion_free (test_mp_list_peer
[i
].su_remote
);
278 testcase_t test_bgp_mp_list
= {
279 .desc
= "Test bgp_mp_list",
280 .setup
= setup_bgp_mp_list
,
281 .run
= run_bgp_mp_list
,
282 .cleanup
= cleanup_bgp_mp_list
,
285 /*=========================================================
286 * Testcase for bgp_info_mpath_update
289 struct bgp_node test_rn
;
292 setup_bgp_info_mpath_update (testcase_t
*t
)
295 str2prefix ("42.1.1.0/24", &test_rn
.p
);
296 setup_bgp_mp_list (t
);
297 for (i
= 0; i
< test_mp_list_info_count
; i
++)
298 bgp_info_add (&test_rn
, &test_mp_list_info
[i
]);
303 run_bgp_info_mpath_update (testcase_t
*t
)
305 struct bgp_info
*new_best
, *old_best
, *mpath
;
307 struct bgp_maxpaths_cfg mp_cfg
= { 3, 3 };
308 int test_result
= TEST_PASSED
;
309 bgp_mp_list_init (&mp_list
);
310 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[4]);
311 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[3]);
312 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[0]);
313 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[1]);
314 new_best
= &test_mp_list_info
[3];
316 bgp_info_mpath_update (&test_rn
, new_best
, old_best
, &mp_list
, &mp_cfg
);
317 bgp_mp_list_clear (&mp_list
);
318 EXPECT_TRUE (bgp_info_mpath_count (new_best
) == 2, test_result
);
319 mpath
= bgp_info_mpath_first (new_best
);
320 EXPECT_TRUE (mpath
== &test_mp_list_info
[0], test_result
);
321 EXPECT_TRUE (CHECK_FLAG (mpath
->flags
, BGP_INFO_MULTIPATH
), test_result
);
322 mpath
= bgp_info_mpath_next (mpath
);
323 EXPECT_TRUE (mpath
== &test_mp_list_info
[1], test_result
);
324 EXPECT_TRUE (CHECK_FLAG (mpath
->flags
, BGP_INFO_MULTIPATH
), test_result
);
326 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[0]);
327 bgp_mp_list_add (&mp_list
, &test_mp_list_info
[1]);
328 new_best
= &test_mp_list_info
[0];
329 old_best
= &test_mp_list_info
[3];
330 bgp_info_mpath_update (&test_rn
, new_best
, old_best
, &mp_list
, &mp_cfg
);
331 bgp_mp_list_clear (&mp_list
);
332 EXPECT_TRUE (bgp_info_mpath_count (new_best
) == 1, test_result
);
333 mpath
= bgp_info_mpath_first (new_best
);
334 EXPECT_TRUE (mpath
== &test_mp_list_info
[1], test_result
);
335 EXPECT_TRUE (CHECK_FLAG (mpath
->flags
, BGP_INFO_MULTIPATH
), test_result
);
336 EXPECT_TRUE (!CHECK_FLAG (test_mp_list_info
[0].flags
, BGP_INFO_MULTIPATH
),
343 cleanup_bgp_info_mpath_update (testcase_t
*t
)
347 for (i
= 0; i
< test_mp_list_peer_count
; i
++)
348 sockunion_free (test_mp_list_peer
[i
].su_remote
);
353 testcase_t test_bgp_info_mpath_update
= {
354 .desc
= "Test bgp_info_mpath_update",
355 .setup
= setup_bgp_info_mpath_update
,
356 .run
= run_bgp_info_mpath_update
,
357 .cleanup
= cleanup_bgp_info_mpath_update
,
360 /*=========================================================
361 * Set up testcase vector
363 testcase_t
*all_tests
[] = {
364 &test_bgp_cfg_maximum_paths
,
366 &test_bgp_info_mpath_update
,
369 int all_tests_count
= (sizeof(all_tests
)/sizeof(testcase_t
*));
371 /*=========================================================
372 * Test Driver Functions
375 global_test_init (void)
377 master
= thread_master_create ();
378 zclient
= zclient_new(master
);
381 bgp_option_set (BGP_OPT_NO_LISTEN
);
383 if (fileno (stdout
) >= 0)
384 tty
= isatty (fileno (stdout
));
389 global_test_cleanup (void)
392 zclient_free (zclient
);
393 thread_master_free (master
);
398 display_result (testcase_t
*test
, int result
)
401 printf ("%s: %s\n", test
->desc
, result
== TEST_PASSED
? OK
: FAILED
);
403 printf ("%s: %s\n", test
->desc
, result
== TEST_PASSED
? "OK" : "FAILED");
407 setup_test (testcase_t
*t
)
416 cleanup_test (testcase_t
*t
)
420 res
= t
->cleanup (t
);
425 run_tests (testcase_t
*tests
[], int num_tests
, int *pass_count
, int *fail_count
)
427 int test_index
, result
;
428 testcase_t
*cur_test
;
430 *pass_count
= *fail_count
= 0;
432 for (test_index
= 0; test_index
< num_tests
; test_index
++)
434 cur_test
= tests
[test_index
];
437 printf ("error: test %d has no description!\n", test_index
);
442 printf ("error: test %s has no run function!\n", cur_test
->desc
);
445 if (setup_test (cur_test
) != 0)
447 printf ("error: setup failed for test %s\n", cur_test
->desc
);
450 result
= cur_test
->run (cur_test
);
451 if (result
== TEST_PASSED
)
455 display_result (cur_test
, result
);
456 if (cleanup_test (cur_test
) != 0)
458 printf ("error: cleanup failed for test %s\n", cur_test
->desc
);
467 int pass_count
, fail_count
;
471 printf("BGP Multipath Tests Run at %s", ctime(&cur_time
));
472 if (global_test_init () != 0)
474 printf("Global init failed. Terminating.\n");
477 run_tests (all_tests
, all_tests_count
, &pass_count
, &fail_count
);
478 global_test_cleanup ();
479 printf("Total pass/fail: %d/%d\n", pass_count
, fail_count
);