]>
Commit | Line | Data |
---|---|---|
42ea6851 JB |
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" | |
3f9c7369 | 32 | #include "queue.h" |
42ea6851 JB |
33 | |
34 | #include "bgpd/bgpd.h" | |
42ea6851 JB |
35 | #include "bgpd/bgp_table.h" |
36 | #include "bgpd/bgp_route.h" | |
96450faf JB |
37 | #include "bgpd/bgp_attr.h" |
38 | #include "bgpd/bgp_mpath.h" | |
42ea6851 JB |
39 | |
40 | #define VT100_RESET "\x1b[0m" | |
41 | #define VT100_RED "\x1b[31m" | |
42 | #define VT100_GREEN "\x1b[32m" | |
43 | #define VT100_YELLOW "\x1b[33m" | |
44 | #define OK VT100_GREEN "OK" VT100_RESET | |
45 | #define FAILED VT100_RED "failed" VT100_RESET | |
46 | ||
47 | #define TEST_PASSED 0 | |
48 | #define TEST_FAILED -1 | |
49 | ||
50 | #define EXPECT_TRUE(expr, res) \ | |
51 | if (!(expr)) \ | |
52 | { \ | |
53 | printf ("Test failure in %s line %u: %s\n", \ | |
54 | __FUNCTION__, __LINE__, #expr); \ | |
55 | (res) = TEST_FAILED; \ | |
56 | } | |
57 | ||
58 | typedef struct testcase_t__ testcase_t; | |
59 | ||
60 | typedef int (*test_setup_func)(testcase_t *); | |
61 | typedef int (*test_run_func)(testcase_t *); | |
62 | typedef int (*test_cleanup_func)(testcase_t *); | |
63 | ||
64 | struct testcase_t__ { | |
65 | const char *desc; | |
66 | void *test_data; | |
67 | void *verify_data; | |
68 | void *tmp_data; | |
69 | test_setup_func setup; | |
70 | test_run_func run; | |
71 | test_cleanup_func cleanup; | |
72 | }; | |
73 | ||
74 | /* need these to link in libbgp */ | |
75 | struct thread_master *master = NULL; | |
76 | struct zclient *zclient; | |
77 | struct zebra_privs_t bgpd_privs = | |
78 | { | |
79 | .user = NULL, | |
80 | .group = NULL, | |
81 | .vty_group = NULL, | |
82 | }; | |
83 | ||
84 | static int tty = 0; | |
85 | ||
86 | /* Create fake bgp instance */ | |
87 | static struct bgp * | |
88 | bgp_create_fake (as_t *as, const char *name) | |
89 | { | |
90 | struct bgp *bgp; | |
91 | afi_t afi; | |
92 | safi_t safi; | |
93 | ||
94 | if ( (bgp = XCALLOC (MTYPE_BGP, sizeof (struct bgp))) == NULL) | |
95 | return NULL; | |
96 | ||
97 | bgp_lock (bgp); | |
98 | //bgp->peer_self = peer_new (bgp); | |
99 | //bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static announcement"); | |
100 | ||
101 | bgp->peer = list_new (); | |
102 | //bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp; | |
103 | ||
104 | bgp->group = list_new (); | |
105 | //bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp; | |
106 | ||
42ea6851 JB |
107 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
108 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
109 | { | |
110 | bgp->route[afi][safi] = bgp_table_init (afi, safi); | |
111 | bgp->aggregate[afi][safi] = bgp_table_init (afi, safi); | |
112 | bgp->rib[afi][safi] = bgp_table_init (afi, safi); | |
d5b77cc2 DS |
113 | bgp->maxpaths[afi][safi].maxpaths_ebgp = MULTIPATH_NUM; |
114 | bgp->maxpaths[afi][safi].maxpaths_ibgp = MULTIPATH_NUM; | |
42ea6851 JB |
115 | } |
116 | ||
117 | bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF; | |
118 | bgp->default_holdtime = BGP_DEFAULT_HOLDTIME; | |
119 | bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE; | |
120 | bgp->restart_time = BGP_DEFAULT_RESTART_TIME; | |
121 | bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME; | |
122 | ||
123 | bgp->as = *as; | |
124 | ||
125 | if (name) | |
126 | bgp->name = strdup (name); | |
127 | ||
128 | return bgp; | |
129 | } | |
130 | ||
131 | /*========================================================= | |
132 | * Testcase for maximum-paths configuration | |
133 | */ | |
134 | static int | |
135 | setup_bgp_cfg_maximum_paths (testcase_t *t) | |
136 | { | |
137 | as_t asn = 1; | |
138 | t->tmp_data = bgp_create_fake (&asn, NULL); | |
139 | if (!t->tmp_data) | |
140 | return -1; | |
141 | return 0; | |
142 | } | |
143 | ||
144 | static int | |
145 | run_bgp_cfg_maximum_paths (testcase_t *t) | |
146 | { | |
147 | afi_t afi; | |
148 | safi_t safi; | |
149 | struct bgp *bgp; | |
150 | int api_result; | |
151 | int test_result = TEST_PASSED; | |
152 | ||
153 | bgp = t->tmp_data; | |
154 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
155 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
156 | { | |
157 | /* test bgp_maximum_paths_set */ | |
5e242b0d | 158 | api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_EBGP, 10, 0); |
42ea6851 | 159 | EXPECT_TRUE (api_result == 0, test_result); |
5e242b0d | 160 | api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_IBGP, 10, 0); |
42ea6851 JB |
161 | EXPECT_TRUE (api_result == 0, test_result); |
162 | EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ebgp == 10, test_result); | |
163 | EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ibgp == 10, test_result); | |
164 | ||
165 | /* test bgp_maximum_paths_unset */ | |
166 | api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_EBGP); | |
167 | EXPECT_TRUE (api_result == 0, test_result); | |
168 | api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_IBGP); | |
169 | EXPECT_TRUE (api_result == 0, test_result); | |
170 | EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ebgp == | |
d5b77cc2 | 171 | MULTIPATH_NUM), test_result); |
42ea6851 | 172 | EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ibgp == |
d5b77cc2 | 173 | MULTIPATH_NUM), test_result); |
42ea6851 JB |
174 | } |
175 | ||
176 | return test_result; | |
177 | } | |
178 | ||
179 | static int | |
180 | cleanup_bgp_cfg_maximum_paths (testcase_t *t) | |
181 | { | |
182 | return bgp_delete ((struct bgp *)t->tmp_data); | |
183 | } | |
184 | ||
185 | testcase_t test_bgp_cfg_maximum_paths = { | |
186 | .desc = "Test bgp maximum-paths config", | |
187 | .setup = setup_bgp_cfg_maximum_paths, | |
188 | .run = run_bgp_cfg_maximum_paths, | |
189 | .cleanup = cleanup_bgp_cfg_maximum_paths, | |
190 | }; | |
191 | ||
96450faf JB |
192 | /*========================================================= |
193 | * Testcase for bgp_mp_list | |
194 | */ | |
de8d5dff JB |
195 | struct peer test_mp_list_peer[] = { |
196 | { .local_as = 1, .as = 2 }, | |
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 | }; | |
96450faf JB |
202 | int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer); |
203 | struct attr test_mp_list_attr[4]; | |
204 | struct bgp_info test_mp_list_info[] = { | |
205 | { .peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0] }, | |
206 | { .peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1] }, | |
207 | { .peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1] }, | |
208 | { .peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2] }, | |
209 | { .peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3] }, | |
210 | }; | |
211 | int test_mp_list_info_count = | |
212 | sizeof (test_mp_list_info)/sizeof (struct bgp_info); | |
213 | ||
214 | static int | |
215 | setup_bgp_mp_list (testcase_t *t) | |
216 | { | |
217 | test_mp_list_attr[0].nexthop.s_addr = 0x01010101; | |
218 | test_mp_list_attr[1].nexthop.s_addr = 0x02020202; | |
219 | test_mp_list_attr[2].nexthop.s_addr = 0x03030303; | |
220 | test_mp_list_attr[3].nexthop.s_addr = 0x04040404; | |
221 | ||
222 | if ((test_mp_list_peer[0].su_remote = sockunion_str2su ("1.1.1.1")) == NULL) | |
223 | return -1; | |
224 | if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL) | |
225 | return -1; | |
226 | if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL) | |
227 | return -1; | |
228 | if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL) | |
229 | return -1; | |
230 | if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL) | |
231 | return -1; | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
236 | static int | |
237 | run_bgp_mp_list (testcase_t *t) | |
238 | { | |
239 | struct list mp_list; | |
240 | struct listnode *mp_node; | |
241 | struct bgp_info *info; | |
242 | int i; | |
243 | int test_result = TEST_PASSED; | |
244 | bgp_mp_list_init (&mp_list); | |
245 | EXPECT_TRUE (listcount(&mp_list) == 0, test_result); | |
246 | ||
247 | bgp_mp_list_add (&mp_list, &test_mp_list_info[1]); | |
248 | bgp_mp_list_add (&mp_list, &test_mp_list_info[4]); | |
249 | bgp_mp_list_add (&mp_list, &test_mp_list_info[2]); | |
250 | bgp_mp_list_add (&mp_list, &test_mp_list_info[3]); | |
251 | bgp_mp_list_add (&mp_list, &test_mp_list_info[0]); | |
252 | ||
9fc3f9b3 | 253 | for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count; |
96450faf JB |
254 | i++, mp_node = listnextnode(mp_node)) |
255 | { | |
256 | info = listgetdata(mp_node); | |
257 | EXPECT_TRUE (info == &test_mp_list_info[i], test_result); | |
258 | } | |
259 | ||
260 | bgp_mp_list_clear (&mp_list); | |
261 | EXPECT_TRUE (listcount(&mp_list) == 0, test_result); | |
262 | ||
263 | return test_result; | |
264 | } | |
265 | ||
266 | static int | |
267 | cleanup_bgp_mp_list (testcase_t *t) | |
268 | { | |
269 | int i; | |
270 | ||
271 | for (i = 0; i < test_mp_list_peer_count; i++) | |
272 | sockunion_free (test_mp_list_peer[i].su_remote); | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
277 | testcase_t test_bgp_mp_list = { | |
278 | .desc = "Test bgp_mp_list", | |
279 | .setup = setup_bgp_mp_list, | |
280 | .run = run_bgp_mp_list, | |
281 | .cleanup = cleanup_bgp_mp_list, | |
282 | }; | |
283 | ||
de8d5dff JB |
284 | /*========================================================= |
285 | * Testcase for bgp_info_mpath_update | |
286 | */ | |
287 | ||
288 | struct bgp_node test_rn; | |
289 | ||
290 | static int | |
291 | setup_bgp_info_mpath_update (testcase_t *t) | |
292 | { | |
293 | int i; | |
294 | str2prefix ("42.1.1.0/24", &test_rn.p); | |
295 | setup_bgp_mp_list (t); | |
296 | for (i = 0; i < test_mp_list_info_count; i++) | |
297 | bgp_info_add (&test_rn, &test_mp_list_info[i]); | |
298 | return 0; | |
299 | } | |
300 | ||
301 | static int | |
302 | run_bgp_info_mpath_update (testcase_t *t) | |
303 | { | |
304 | struct bgp_info *new_best, *old_best, *mpath; | |
305 | struct list mp_list; | |
306 | struct bgp_maxpaths_cfg mp_cfg = { 3, 3 }; | |
307 | int test_result = TEST_PASSED; | |
308 | bgp_mp_list_init (&mp_list); | |
309 | bgp_mp_list_add (&mp_list, &test_mp_list_info[4]); | |
310 | bgp_mp_list_add (&mp_list, &test_mp_list_info[3]); | |
311 | bgp_mp_list_add (&mp_list, &test_mp_list_info[0]); | |
312 | bgp_mp_list_add (&mp_list, &test_mp_list_info[1]); | |
313 | new_best = &test_mp_list_info[3]; | |
314 | old_best = NULL; | |
315 | bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, &mp_cfg); | |
316 | bgp_mp_list_clear (&mp_list); | |
317 | EXPECT_TRUE (bgp_info_mpath_count (new_best) == 2, test_result); | |
318 | mpath = bgp_info_mpath_first (new_best); | |
319 | EXPECT_TRUE (mpath == &test_mp_list_info[0], test_result); | |
320 | EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result); | |
321 | mpath = bgp_info_mpath_next (mpath); | |
322 | EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result); | |
323 | EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result); | |
324 | ||
325 | bgp_mp_list_add (&mp_list, &test_mp_list_info[0]); | |
326 | bgp_mp_list_add (&mp_list, &test_mp_list_info[1]); | |
327 | new_best = &test_mp_list_info[0]; | |
328 | old_best = &test_mp_list_info[3]; | |
329 | bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, &mp_cfg); | |
330 | bgp_mp_list_clear (&mp_list); | |
331 | EXPECT_TRUE (bgp_info_mpath_count (new_best) == 1, test_result); | |
332 | mpath = bgp_info_mpath_first (new_best); | |
333 | EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result); | |
334 | EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result); | |
335 | EXPECT_TRUE (!CHECK_FLAG (test_mp_list_info[0].flags, BGP_INFO_MULTIPATH), | |
336 | test_result); | |
337 | ||
338 | return test_result; | |
339 | } | |
340 | ||
341 | static int | |
342 | cleanup_bgp_info_mpath_update (testcase_t *t) | |
343 | { | |
344 | int i; | |
345 | ||
346 | for (i = 0; i < test_mp_list_peer_count; i++) | |
347 | sockunion_free (test_mp_list_peer[i].su_remote); | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | testcase_t test_bgp_info_mpath_update = { | |
353 | .desc = "Test bgp_info_mpath_update", | |
354 | .setup = setup_bgp_info_mpath_update, | |
355 | .run = run_bgp_info_mpath_update, | |
356 | .cleanup = cleanup_bgp_info_mpath_update, | |
357 | }; | |
358 | ||
42ea6851 JB |
359 | /*========================================================= |
360 | * Set up testcase vector | |
361 | */ | |
362 | testcase_t *all_tests[] = { | |
363 | &test_bgp_cfg_maximum_paths, | |
96450faf | 364 | &test_bgp_mp_list, |
de8d5dff | 365 | &test_bgp_info_mpath_update, |
42ea6851 JB |
366 | }; |
367 | ||
368 | int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *)); | |
369 | ||
370 | /*========================================================= | |
371 | * Test Driver Functions | |
372 | */ | |
373 | static int | |
374 | global_test_init (void) | |
375 | { | |
376 | master = thread_master_create (); | |
4140ca4d | 377 | zclient = zclient_new(master); |
42ea6851 | 378 | bgp_master_init (); |
ebf00458 | 379 | vrf_init (); |
c9e4f862 PJ |
380 | bgp_option_set (BGP_OPT_NO_LISTEN); |
381 | ||
42ea6851 JB |
382 | if (fileno (stdout) >= 0) |
383 | tty = isatty (fileno (stdout)); | |
384 | return 0; | |
385 | } | |
386 | ||
387 | static int | |
388 | global_test_cleanup (void) | |
389 | { | |
005b6bc0 LB |
390 | if (zclient != NULL) |
391 | zclient_free (zclient); | |
42ea6851 JB |
392 | thread_master_free (master); |
393 | return 0; | |
394 | } | |
395 | ||
396 | static void | |
397 | display_result (testcase_t *test, int result) | |
398 | { | |
399 | if (tty) | |
400 | printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED); | |
401 | else | |
402 | printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED"); | |
403 | } | |
404 | ||
405 | static int | |
406 | setup_test (testcase_t *t) | |
407 | { | |
408 | int res = 0; | |
409 | if (t->setup) | |
410 | res = t->setup (t); | |
411 | return res; | |
412 | } | |
413 | ||
414 | static int | |
415 | cleanup_test (testcase_t *t) | |
416 | { | |
417 | int res = 0; | |
418 | if (t->cleanup) | |
419 | res = t->cleanup (t); | |
420 | return res; | |
421 | } | |
422 | ||
423 | static void | |
424 | run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count) | |
425 | { | |
426 | int test_index, result; | |
427 | testcase_t *cur_test; | |
428 | ||
429 | *pass_count = *fail_count = 0; | |
430 | ||
431 | for (test_index = 0; test_index < num_tests; test_index++) | |
432 | { | |
433 | cur_test = tests[test_index]; | |
434 | if (!cur_test->desc) | |
435 | { | |
436 | printf ("error: test %d has no description!\n", test_index); | |
437 | continue; | |
438 | } | |
439 | if (!cur_test->run) | |
440 | { | |
441 | printf ("error: test %s has no run function!\n", cur_test->desc); | |
442 | continue; | |
443 | } | |
444 | if (setup_test (cur_test) != 0) | |
445 | { | |
446 | printf ("error: setup failed for test %s\n", cur_test->desc); | |
447 | continue; | |
448 | } | |
449 | result = cur_test->run (cur_test); | |
450 | if (result == TEST_PASSED) | |
451 | *pass_count += 1; | |
452 | else | |
453 | *fail_count += 1; | |
454 | display_result (cur_test, result); | |
455 | if (cleanup_test (cur_test) != 0) | |
456 | { | |
457 | printf ("error: cleanup failed for test %s\n", cur_test->desc); | |
458 | continue; | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
463 | int | |
464 | main (void) | |
465 | { | |
466 | int pass_count, fail_count; | |
467 | time_t cur_time; | |
468 | ||
469 | time (&cur_time); | |
470 | printf("BGP Multipath Tests Run at %s", ctime(&cur_time)); | |
471 | if (global_test_init () != 0) | |
472 | { | |
473 | printf("Global init failed. Terminating.\n"); | |
474 | exit(1); | |
475 | } | |
476 | run_tests (all_tests, all_tests_count, &pass_count, &fail_count); | |
477 | global_test_cleanup (); | |
478 | printf("Total pass/fail: %d/%d\n", pass_count, fail_count); | |
479 | return fail_count; | |
480 | } |