Josh Bailey | 42ea685 | 2011-07-20 20:44:23 -0700 | [diff] [blame] | 1 | /* $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 Bailey | 42ea685 | 2011-07-20 20:44:23 -0700 | [diff] [blame] | 34 | #include "bgpd/bgp_table.h" |
| 35 | #include "bgpd/bgp_route.h" |
Josh Bailey | 96450fa | 2011-07-20 20:45:12 -0700 | [diff] [blame^] | 36 | #include "bgpd/bgp_attr.h" |
| 37 | #include "bgpd/bgp_mpath.h" |
Josh Bailey | 42ea685 | 2011-07-20 20:44:23 -0700 | [diff] [blame] | 38 | |
| 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 | |
| 57 | typedef struct testcase_t__ testcase_t; |
| 58 | |
| 59 | typedef int (*test_setup_func)(testcase_t *); |
| 60 | typedef int (*test_run_func)(testcase_t *); |
| 61 | typedef int (*test_cleanup_func)(testcase_t *); |
| 62 | |
| 63 | struct 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 */ |
| 74 | struct thread_master *master = NULL; |
| 75 | struct zclient *zclient; |
| 76 | struct zebra_privs_t bgpd_privs = |
| 77 | { |
| 78 | .user = NULL, |
| 79 | .group = NULL, |
| 80 | .vty_group = NULL, |
| 81 | }; |
| 82 | |
| 83 | static int tty = 0; |
| 84 | |
| 85 | /* Create fake bgp instance */ |
| 86 | static struct bgp * |
| 87 | bgp_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 | */ |
| 136 | static int |
| 137 | setup_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 | |
| 146 | static int |
| 147 | run_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 | |
| 181 | static int |
| 182 | cleanup_bgp_cfg_maximum_paths (testcase_t *t) |
| 183 | { |
| 184 | return bgp_delete ((struct bgp *)t->tmp_data); |
| 185 | } |
| 186 | |
| 187 | testcase_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 Bailey | 96450fa | 2011-07-20 20:45:12 -0700 | [diff] [blame^] | 195 | * Testcase for bgp_mp_list |
| 196 | */ |
| 197 | struct peer test_mp_list_peer[5]; |
| 198 | int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer); |
| 199 | struct attr test_mp_list_attr[4]; |
| 200 | struct bgp_info test_mp_list_info[] = { |
| 201 | { .peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0] }, |
| 202 | { .peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1] }, |
| 203 | { .peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1] }, |
| 204 | { .peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2] }, |
| 205 | { .peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3] }, |
| 206 | }; |
| 207 | int test_mp_list_info_count = |
| 208 | sizeof (test_mp_list_info)/sizeof (struct bgp_info); |
| 209 | |
| 210 | static int |
| 211 | setup_bgp_mp_list (testcase_t *t) |
| 212 | { |
| 213 | test_mp_list_attr[0].nexthop.s_addr = 0x01010101; |
| 214 | test_mp_list_attr[1].nexthop.s_addr = 0x02020202; |
| 215 | test_mp_list_attr[2].nexthop.s_addr = 0x03030303; |
| 216 | test_mp_list_attr[3].nexthop.s_addr = 0x04040404; |
| 217 | |
| 218 | if ((test_mp_list_peer[0].su_remote = sockunion_str2su ("1.1.1.1")) == NULL) |
| 219 | return -1; |
| 220 | if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL) |
| 221 | return -1; |
| 222 | if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL) |
| 223 | return -1; |
| 224 | if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL) |
| 225 | return -1; |
| 226 | if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL) |
| 227 | return -1; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int |
| 233 | run_bgp_mp_list (testcase_t *t) |
| 234 | { |
| 235 | struct list mp_list; |
| 236 | struct listnode *mp_node; |
| 237 | struct bgp_info *info; |
| 238 | int i; |
| 239 | int test_result = TEST_PASSED; |
| 240 | bgp_mp_list_init (&mp_list); |
| 241 | EXPECT_TRUE (listcount(&mp_list) == 0, test_result); |
| 242 | |
| 243 | bgp_mp_list_add (&mp_list, &test_mp_list_info[1]); |
| 244 | bgp_mp_list_add (&mp_list, &test_mp_list_info[4]); |
| 245 | bgp_mp_list_add (&mp_list, &test_mp_list_info[2]); |
| 246 | bgp_mp_list_add (&mp_list, &test_mp_list_info[3]); |
| 247 | bgp_mp_list_add (&mp_list, &test_mp_list_info[0]); |
| 248 | |
| 249 | for (i = 0, mp_node = listhead(&mp_list); i < test_mp_list_info_count; |
| 250 | i++, mp_node = listnextnode(mp_node)) |
| 251 | { |
| 252 | info = listgetdata(mp_node); |
| 253 | EXPECT_TRUE (info == &test_mp_list_info[i], test_result); |
| 254 | } |
| 255 | |
| 256 | bgp_mp_list_clear (&mp_list); |
| 257 | EXPECT_TRUE (listcount(&mp_list) == 0, test_result); |
| 258 | |
| 259 | return test_result; |
| 260 | } |
| 261 | |
| 262 | static int |
| 263 | cleanup_bgp_mp_list (testcase_t *t) |
| 264 | { |
| 265 | int i; |
| 266 | |
| 267 | for (i = 0; i < test_mp_list_peer_count; i++) |
| 268 | sockunion_free (test_mp_list_peer[i].su_remote); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | testcase_t test_bgp_mp_list = { |
| 274 | .desc = "Test bgp_mp_list", |
| 275 | .setup = setup_bgp_mp_list, |
| 276 | .run = run_bgp_mp_list, |
| 277 | .cleanup = cleanup_bgp_mp_list, |
| 278 | }; |
| 279 | |
| 280 | /*========================================================= |
Josh Bailey | 42ea685 | 2011-07-20 20:44:23 -0700 | [diff] [blame] | 281 | * Set up testcase vector |
| 282 | */ |
| 283 | testcase_t *all_tests[] = { |
| 284 | &test_bgp_cfg_maximum_paths, |
Josh Bailey | 96450fa | 2011-07-20 20:45:12 -0700 | [diff] [blame^] | 285 | &test_bgp_mp_list, |
Josh Bailey | 42ea685 | 2011-07-20 20:44:23 -0700 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *)); |
| 289 | |
| 290 | /*========================================================= |
| 291 | * Test Driver Functions |
| 292 | */ |
| 293 | static int |
| 294 | global_test_init (void) |
| 295 | { |
| 296 | master = thread_master_create (); |
| 297 | zclient = zclient_new (); |
| 298 | bgp_master_init (); |
| 299 | |
| 300 | if (fileno (stdout) >= 0) |
| 301 | tty = isatty (fileno (stdout)); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int |
| 306 | global_test_cleanup (void) |
| 307 | { |
| 308 | zclient_free (zclient); |
| 309 | thread_master_free (master); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static void |
| 314 | display_result (testcase_t *test, int result) |
| 315 | { |
| 316 | if (tty) |
| 317 | printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED); |
| 318 | else |
| 319 | printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED"); |
| 320 | } |
| 321 | |
| 322 | static int |
| 323 | setup_test (testcase_t *t) |
| 324 | { |
| 325 | int res = 0; |
| 326 | if (t->setup) |
| 327 | res = t->setup (t); |
| 328 | return res; |
| 329 | } |
| 330 | |
| 331 | static int |
| 332 | cleanup_test (testcase_t *t) |
| 333 | { |
| 334 | int res = 0; |
| 335 | if (t->cleanup) |
| 336 | res = t->cleanup (t); |
| 337 | return res; |
| 338 | } |
| 339 | |
| 340 | static void |
| 341 | run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count) |
| 342 | { |
| 343 | int test_index, result; |
| 344 | testcase_t *cur_test; |
| 345 | |
| 346 | *pass_count = *fail_count = 0; |
| 347 | |
| 348 | for (test_index = 0; test_index < num_tests; test_index++) |
| 349 | { |
| 350 | cur_test = tests[test_index]; |
| 351 | if (!cur_test->desc) |
| 352 | { |
| 353 | printf ("error: test %d has no description!\n", test_index); |
| 354 | continue; |
| 355 | } |
| 356 | if (!cur_test->run) |
| 357 | { |
| 358 | printf ("error: test %s has no run function!\n", cur_test->desc); |
| 359 | continue; |
| 360 | } |
| 361 | if (setup_test (cur_test) != 0) |
| 362 | { |
| 363 | printf ("error: setup failed for test %s\n", cur_test->desc); |
| 364 | continue; |
| 365 | } |
| 366 | result = cur_test->run (cur_test); |
| 367 | if (result == TEST_PASSED) |
| 368 | *pass_count += 1; |
| 369 | else |
| 370 | *fail_count += 1; |
| 371 | display_result (cur_test, result); |
| 372 | if (cleanup_test (cur_test) != 0) |
| 373 | { |
| 374 | printf ("error: cleanup failed for test %s\n", cur_test->desc); |
| 375 | continue; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | int |
| 381 | main (void) |
| 382 | { |
| 383 | int pass_count, fail_count; |
| 384 | time_t cur_time; |
| 385 | |
| 386 | time (&cur_time); |
| 387 | printf("BGP Multipath Tests Run at %s", ctime(&cur_time)); |
| 388 | if (global_test_init () != 0) |
| 389 | { |
| 390 | printf("Global init failed. Terminating.\n"); |
| 391 | exit(1); |
| 392 | } |
| 393 | run_tests (all_tests, all_tests_count, &pass_count, &fail_count); |
| 394 | global_test_cleanup (); |
| 395 | printf("Total pass/fail: %d/%d\n", pass_count, fail_count); |
| 396 | return fail_count; |
| 397 | } |