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" |
| 34 | #include "bgpd/bgp_mpath.h" |
| 35 | #include "bgpd/bgp_table.h" |
| 36 | #include "bgpd/bgp_route.h" |
| 37 | |
| 38 | #define VT100_RESET "\x1b[0m" |
| 39 | #define VT100_RED "\x1b[31m" |
| 40 | #define VT100_GREEN "\x1b[32m" |
| 41 | #define VT100_YELLOW "\x1b[33m" |
| 42 | #define OK VT100_GREEN "OK" VT100_RESET |
| 43 | #define FAILED VT100_RED "failed" VT100_RESET |
| 44 | |
| 45 | #define TEST_PASSED 0 |
| 46 | #define TEST_FAILED -1 |
| 47 | |
| 48 | #define EXPECT_TRUE(expr, res) \ |
| 49 | if (!(expr)) \ |
| 50 | { \ |
| 51 | printf ("Test failure in %s line %u: %s\n", \ |
| 52 | __FUNCTION__, __LINE__, #expr); \ |
| 53 | (res) = TEST_FAILED; \ |
| 54 | } |
| 55 | |
| 56 | typedef struct testcase_t__ testcase_t; |
| 57 | |
| 58 | typedef int (*test_setup_func)(testcase_t *); |
| 59 | typedef int (*test_run_func)(testcase_t *); |
| 60 | typedef int (*test_cleanup_func)(testcase_t *); |
| 61 | |
| 62 | struct testcase_t__ { |
| 63 | const char *desc; |
| 64 | void *test_data; |
| 65 | void *verify_data; |
| 66 | void *tmp_data; |
| 67 | test_setup_func setup; |
| 68 | test_run_func run; |
| 69 | test_cleanup_func cleanup; |
| 70 | }; |
| 71 | |
| 72 | /* need these to link in libbgp */ |
| 73 | struct thread_master *master = NULL; |
| 74 | struct zclient *zclient; |
| 75 | struct zebra_privs_t bgpd_privs = |
| 76 | { |
| 77 | .user = NULL, |
| 78 | .group = NULL, |
| 79 | .vty_group = NULL, |
| 80 | }; |
| 81 | |
| 82 | static int tty = 0; |
| 83 | |
| 84 | /* Create fake bgp instance */ |
| 85 | static struct bgp * |
| 86 | bgp_create_fake (as_t *as, const char *name) |
| 87 | { |
| 88 | struct bgp *bgp; |
| 89 | afi_t afi; |
| 90 | safi_t safi; |
| 91 | |
| 92 | if ( (bgp = XCALLOC (MTYPE_BGP, sizeof (struct bgp))) == NULL) |
| 93 | return NULL; |
| 94 | |
| 95 | bgp_lock (bgp); |
| 96 | //bgp->peer_self = peer_new (bgp); |
| 97 | //bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static announcement"); |
| 98 | |
| 99 | bgp->peer = list_new (); |
| 100 | //bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp; |
| 101 | |
| 102 | bgp->group = list_new (); |
| 103 | //bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp; |
| 104 | |
| 105 | bgp->rsclient = list_new (); |
| 106 | //bgp->rsclient->cmp = (int (*)(void*, void*)) peer_cmp; |
| 107 | |
| 108 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 109 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 110 | { |
| 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 = BGP_DEFAULT_MAXPATHS; |
| 115 | bgp->maxpaths[afi][safi].maxpaths_ibgp = BGP_DEFAULT_MAXPATHS; |
| 116 | } |
| 117 | |
| 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; |
| 123 | |
| 124 | bgp->as = *as; |
| 125 | |
| 126 | if (name) |
| 127 | bgp->name = strdup (name); |
| 128 | |
| 129 | return bgp; |
| 130 | } |
| 131 | |
| 132 | /*========================================================= |
| 133 | * Testcase for maximum-paths configuration |
| 134 | */ |
| 135 | static int |
| 136 | setup_bgp_cfg_maximum_paths (testcase_t *t) |
| 137 | { |
| 138 | as_t asn = 1; |
| 139 | t->tmp_data = bgp_create_fake (&asn, NULL); |
| 140 | if (!t->tmp_data) |
| 141 | return -1; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int |
| 146 | run_bgp_cfg_maximum_paths (testcase_t *t) |
| 147 | { |
| 148 | afi_t afi; |
| 149 | safi_t safi; |
| 150 | struct bgp *bgp; |
| 151 | int api_result; |
| 152 | int test_result = TEST_PASSED; |
| 153 | |
| 154 | bgp = t->tmp_data; |
| 155 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 156 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 157 | { |
| 158 | /* test bgp_maximum_paths_set */ |
| 159 | api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_EBGP, 10); |
| 160 | EXPECT_TRUE (api_result == 0, test_result); |
| 161 | api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_IBGP, 10); |
| 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); |
| 165 | |
| 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 | BGP_DEFAULT_MAXPATHS), test_result); |
| 173 | EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ibgp == |
| 174 | BGP_DEFAULT_MAXPATHS), test_result); |
| 175 | } |
| 176 | |
| 177 | return test_result; |
| 178 | } |
| 179 | |
| 180 | static int |
| 181 | cleanup_bgp_cfg_maximum_paths (testcase_t *t) |
| 182 | { |
| 183 | return bgp_delete ((struct bgp *)t->tmp_data); |
| 184 | } |
| 185 | |
| 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, |
| 191 | }; |
| 192 | |
| 193 | /*========================================================= |
| 194 | * Set up testcase vector |
| 195 | */ |
| 196 | testcase_t *all_tests[] = { |
| 197 | &test_bgp_cfg_maximum_paths, |
| 198 | }; |
| 199 | |
| 200 | int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *)); |
| 201 | |
| 202 | /*========================================================= |
| 203 | * Test Driver Functions |
| 204 | */ |
| 205 | static int |
| 206 | global_test_init (void) |
| 207 | { |
| 208 | master = thread_master_create (); |
| 209 | zclient = zclient_new (); |
| 210 | bgp_master_init (); |
| 211 | |
| 212 | if (fileno (stdout) >= 0) |
| 213 | tty = isatty (fileno (stdout)); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int |
| 218 | global_test_cleanup (void) |
| 219 | { |
| 220 | zclient_free (zclient); |
| 221 | thread_master_free (master); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | display_result (testcase_t *test, int result) |
| 227 | { |
| 228 | if (tty) |
| 229 | printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED); |
| 230 | else |
| 231 | printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED"); |
| 232 | } |
| 233 | |
| 234 | static int |
| 235 | setup_test (testcase_t *t) |
| 236 | { |
| 237 | int res = 0; |
| 238 | if (t->setup) |
| 239 | res = t->setup (t); |
| 240 | return res; |
| 241 | } |
| 242 | |
| 243 | static int |
| 244 | cleanup_test (testcase_t *t) |
| 245 | { |
| 246 | int res = 0; |
| 247 | if (t->cleanup) |
| 248 | res = t->cleanup (t); |
| 249 | return res; |
| 250 | } |
| 251 | |
| 252 | static void |
| 253 | run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count) |
| 254 | { |
| 255 | int test_index, result; |
| 256 | testcase_t *cur_test; |
| 257 | |
| 258 | *pass_count = *fail_count = 0; |
| 259 | |
| 260 | for (test_index = 0; test_index < num_tests; test_index++) |
| 261 | { |
| 262 | cur_test = tests[test_index]; |
| 263 | if (!cur_test->desc) |
| 264 | { |
| 265 | printf ("error: test %d has no description!\n", test_index); |
| 266 | continue; |
| 267 | } |
| 268 | if (!cur_test->run) |
| 269 | { |
| 270 | printf ("error: test %s has no run function!\n", cur_test->desc); |
| 271 | continue; |
| 272 | } |
| 273 | if (setup_test (cur_test) != 0) |
| 274 | { |
| 275 | printf ("error: setup failed for test %s\n", cur_test->desc); |
| 276 | continue; |
| 277 | } |
| 278 | result = cur_test->run (cur_test); |
| 279 | if (result == TEST_PASSED) |
| 280 | *pass_count += 1; |
| 281 | else |
| 282 | *fail_count += 1; |
| 283 | display_result (cur_test, result); |
| 284 | if (cleanup_test (cur_test) != 0) |
| 285 | { |
| 286 | printf ("error: cleanup failed for test %s\n", cur_test->desc); |
| 287 | continue; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | int |
| 293 | main (void) |
| 294 | { |
| 295 | int pass_count, fail_count; |
| 296 | time_t cur_time; |
| 297 | |
| 298 | time (&cur_time); |
| 299 | printf("BGP Multipath Tests Run at %s", ctime(&cur_time)); |
| 300 | if (global_test_init () != 0) |
| 301 | { |
| 302 | printf("Global init failed. Terminating.\n"); |
| 303 | exit(1); |
| 304 | } |
| 305 | run_tests (all_tests, all_tests_count, &pass_count, &fail_count); |
| 306 | global_test_cleanup (); |
| 307 | printf("Total pass/fail: %d/%d\n", pass_count, fail_count); |
| 308 | return fail_count; |
| 309 | } |