2 * BGP Peer Attribute Unit Tests
3 * Copyright (C) 2018 Pascal Mathis
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bgpd/bgpd.h"
24 #include "bgpd/bgp_attr.h"
25 #include "bgpd/bgp_regex.h"
26 #include "bgpd/bgp_clist.h"
27 #include "bgpd/bgp_dump.h"
28 #include "bgpd/bgp_filter.h"
29 #include "bgpd/bgp_route.h"
30 #include "bgpd/bgp_vty.h"
31 #include "bgpd/bgp_zebra.h"
34 #include "bgpd/rfapi/rfapi_backend.h"
37 #define OUT_SYMBOL_INFO "\u25ba"
38 #define OUT_SYMBOL_OK "\u2714"
39 #define OUT_SYMBOL_NOK "\u2716"
41 #define TEST_ASSERT(T, C) \
43 if ((T)->state != TEST_SUCCESS || (C)) \
45 (T)->state = TEST_ASSERT_ERROR; \
46 (T)->error = str_printf("assertion failed: %s (%s:%d)", (#C), \
47 __FILE__, __LINE__); \
50 #define TEST_ASSERT_EQ(T, A, B) \
52 if ((T)->state != TEST_SUCCESS || ((A) == (B))) \
54 (T)->state = TEST_ASSERT_ERROR; \
55 (T)->error = str_printf( \
56 "assertion failed: %s[%d] == [%d]%s (%s:%d)", (#A), \
57 (A), (B), (#B), __FILE__, __LINE__); \
60 #define TEST_HANDLER_MAX 5
61 #define TEST_HANDLER(name) _test_handler_##name
62 #define TEST_HANDLER_DECL(name) \
63 static void _test_handler_##name( \
64 struct test *test, struct test_peer_attr *pa, \
65 struct peer *peer, struct peer *group, bool peer_set, \
68 #define TEST_ATTR_HANDLER_DECL(name, attr, pval, gval) \
69 TEST_HANDLER_DECL(name) \
72 TEST_ASSERT_EQ(test, peer->attr, (pval)); \
73 else if (peer_group_active(peer) && group_set) \
74 TEST_ASSERT_EQ(test, peer->attr, (gval)); \
76 TEST_ASSERT_EQ(test, group->attr, (gval)); \
78 TEST_HANDLER_DECL(name)
80 #define TEST_STR_ATTR_HANDLER_DECL(name, attr, pval, gval) \
81 TEST_HANDLER_DECL(name) \
84 TEST_ASSERT(test, peer->attr != NULL); \
85 TEST_ASSERT(test, !strcmp(peer->attr, (pval))); \
86 } else if (peer_group_active(peer) && group_set) { \
87 TEST_ASSERT(test, peer->attr != NULL); \
88 TEST_ASSERT(test, !strcmp(peer->attr, (gval))); \
91 TEST_ASSERT(test, group->attr != NULL); \
92 TEST_ASSERT(test, !strcmp(group->attr, (gval))); \
95 TEST_HANDLER_DECL(name)
97 #define TEST_SU_ATTR_HANDLER_DECL(name, attr, pval, gval) \
98 TEST_HANDLER_DECL(name) \
100 union sockunion su; \
102 str2sockunion(pval, &su); \
103 TEST_ASSERT(test, !sockunion_cmp(peer->attr, &su)); \
104 } else if (peer_group_active(peer) && group_set) { \
105 str2sockunion(gval, &su); \
106 TEST_ASSERT(test, !sockunion_cmp(group->attr, &su)); \
109 str2sockunion(gval, &su); \
110 TEST_ASSERT(test, !sockunion_cmp(group->attr, &su)); \
113 TEST_HANDLER_DECL(name)
115 /* Required variables to link in libbgp */
116 struct zebra_privs_t bgpd_privs
= {0};
117 struct thread_master
*master
;
129 enum test_peer_attr_type
{
131 PEER_AT_AF_FILTER
= 1,
132 PEER_AT_AF_CUSTOM
= 2,
133 PEER_AT_GLOBAL_FLAG
= 3,
134 PEER_AT_GLOBAL_CUSTOM
= 4
138 enum test_state state
;
146 struct peer_group
*group
;
157 const char *peer_address
;
158 const char *peer_interface
;
159 const char *peer_group
;
162 struct test_peer_family
{
167 struct test_peer_attr
{
169 const char *peer_cmd
;
170 const char *group_cmd
;
172 enum test_peer_attr_type type
;
185 bool skip_xfer_cases
;
190 struct test_peer_family families
[AFI_MAX
* SAFI_MAX
];
192 void (*handlers
[TEST_HANDLER_MAX
])(struct test
*test
,
193 struct test_peer_attr
*pa
,
195 struct peer
*group
, bool peer_set
,
199 static struct test_config cfg
= {
202 .peer_address
= "1.1.1.1",
203 .peer_interface
= "IP-TEST",
204 .peer_group
= "PG-TEST",
207 static struct test_peer_family test_default_families
[] = {
208 {.afi
= AFI_IP
, .safi
= SAFI_UNICAST
},
209 {.afi
= AFI_IP
, .safi
= SAFI_MULTICAST
},
210 {.afi
= AFI_IP6
, .safi
= SAFI_UNICAST
},
211 {.afi
= AFI_IP6
, .safi
= SAFI_MULTICAST
},
214 static char *str_vprintf(const char *fmt
, va_list ap
)
223 ret
= vsnprintf(buf
, buf_size
, fmt
, apc
);
226 if (ret
>= 0 && ret
< buf_size
)
234 buf
= XREALLOC(MTYPE_TMP
, buf
, buf_size
);
240 static char *str_printf(const char *fmt
, ...)
246 buf
= str_vprintf(fmt
, ap
);
252 TEST_ATTR_HANDLER_DECL(advertisement_interval
, v_routeadv
, 10, 20);
253 TEST_STR_ATTR_HANDLER_DECL(password
, password
, "FRR-Peer", "FRR-Group");
254 TEST_ATTR_HANDLER_DECL(local_as
, change_local_as
, 1, 2);
255 TEST_ATTR_HANDLER_DECL(timers_1
, keepalive
, 10, 20);
256 TEST_ATTR_HANDLER_DECL(timers_2
, holdtime
, 30, 60);
257 TEST_ATTR_HANDLER_DECL(addpath_types
, addpath_type
[pa
->afi
][pa
->safi
],
258 BGP_ADDPATH_ALL
, BGP_ADDPATH_BEST_PER_AS
);
259 TEST_SU_ATTR_HANDLER_DECL(update_source_su
, update_source
, "255.255.255.1",
261 TEST_STR_ATTR_HANDLER_DECL(update_source_if
, update_if
, "IF-PEER", "IF-GROUP");
263 TEST_ATTR_HANDLER_DECL(allowas_in
, allowas_in
[pa
->afi
][pa
->safi
], 1, 2);
264 TEST_STR_ATTR_HANDLER_DECL(default_originate_route_map
,
265 default_rmap
[pa
->afi
][pa
->safi
].name
, "RM-PEER",
267 TEST_STR_ATTR_HANDLER_DECL(
269 filter
[pa
->afi
][pa
->safi
].dlist
[pa
->u
.filter
.direct
].name
, "DL-PEER",
271 TEST_STR_ATTR_HANDLER_DECL(
272 filter_list
, filter
[pa
->afi
][pa
->safi
].aslist
[pa
->u
.filter
.direct
].name
,
273 "FL-PEER", "FL-GROUP");
274 TEST_ATTR_HANDLER_DECL(maximum_prefix
, pmax
[pa
->afi
][pa
->safi
], 10, 20);
275 TEST_ATTR_HANDLER_DECL(maximum_prefix_threshold
,
276 pmax_threshold
[pa
->afi
][pa
->safi
], 1, 2);
277 TEST_ATTR_HANDLER_DECL(maximum_prefix_restart
, pmax_restart
[pa
->afi
][pa
->safi
],
279 TEST_STR_ATTR_HANDLER_DECL(
280 prefix_list
, filter
[pa
->afi
][pa
->safi
].plist
[pa
->u
.filter
.direct
].name
,
281 "PL-PEER", "PL-GROUP");
282 TEST_STR_ATTR_HANDLER_DECL(
283 route_map
, filter
[pa
->afi
][pa
->safi
].map
[pa
->u
.filter
.direct
].name
,
284 "RM-PEER", "RM-GROUP");
285 TEST_STR_ATTR_HANDLER_DECL(unsuppress_map
, filter
[pa
->afi
][pa
->safi
].usmap
.name
,
286 "UM-PEER", "UM-GROUP");
287 TEST_ATTR_HANDLER_DECL(weight
, weight
[pa
->afi
][pa
->safi
], 100, 200);
289 /* clang-format off */
290 static struct test_peer_attr test_peer_attrs
[] = {
291 /* Peer Attributes */
293 .cmd
= "advertisement-interval",
294 .peer_cmd
= "advertisement-interval 10",
295 .group_cmd
= "advertisement-interval 20",
296 .u
.flag
= PEER_FLAG_ROUTEADV
,
297 .type
= PEER_AT_GLOBAL_FLAG
,
298 .handlers
[0] = TEST_HANDLER(advertisement_interval
),
301 .cmd
= "capability dynamic",
302 .u
.flag
= PEER_FLAG_DYNAMIC_CAPABILITY
,
303 .type
= PEER_AT_GLOBAL_FLAG
,
306 .cmd
= "capability extended-nexthop",
307 .u
.flag
= PEER_FLAG_CAPABILITY_ENHE
,
308 .type
= PEER_AT_GLOBAL_FLAG
,
311 .cmd
= "capability extended-nexthop",
312 .u
.flag
= PEER_FLAG_CAPABILITY_ENHE
,
313 .type
= PEER_AT_GLOBAL_FLAG
,
314 .o
.invert_peer
= true,
315 .o
.use_iface_peer
= true,
318 .cmd
= "description",
319 .peer_cmd
= "description FRR Peer",
320 .group_cmd
= "description FRR Group",
321 .type
= PEER_AT_GLOBAL_CUSTOM
,
324 .cmd
= "disable-connected-check",
325 .u
.flag
= PEER_FLAG_DISABLE_CONNECTED_CHECK
,
326 .type
= PEER_AT_GLOBAL_FLAG
,
329 .cmd
= "dont-capability-negotiate",
330 .u
.flag
= PEER_FLAG_DONT_CAPABILITY
,
331 .type
= PEER_AT_GLOBAL_FLAG
,
334 .cmd
= "enforce-first-as",
335 .u
.flag
= PEER_FLAG_ENFORCE_FIRST_AS
,
336 .type
= PEER_AT_GLOBAL_FLAG
,
340 .peer_cmd
= "local-as 1",
341 .group_cmd
= "local-as 2",
342 .u
.flag
= PEER_FLAG_LOCAL_AS
,
343 .type
= PEER_AT_GLOBAL_FLAG
,
344 .handlers
[0] = TEST_HANDLER(local_as
),
347 .cmd
= "local-as 1 no-prepend",
348 .u
.flag
= PEER_FLAG_LOCAL_AS
| PEER_FLAG_LOCAL_AS_NO_PREPEND
,
349 .type
= PEER_AT_GLOBAL_FLAG
,
352 .cmd
= "local-as 1 no-prepend replace-as",
353 .u
.flag
= PEER_FLAG_LOCAL_AS
| PEER_FLAG_LOCAL_AS_REPLACE_AS
,
354 .type
= PEER_AT_GLOBAL_FLAG
,
357 .cmd
= "override-capability",
358 .u
.flag
= PEER_FLAG_OVERRIDE_CAPABILITY
,
359 .type
= PEER_AT_GLOBAL_FLAG
,
363 .u
.flag
= PEER_FLAG_PASSIVE
,
364 .type
= PEER_AT_GLOBAL_FLAG
,
368 .peer_cmd
= "password FRR-Peer",
369 .group_cmd
= "password FRR-Group",
370 .u
.flag
= PEER_FLAG_PASSWORD
,
371 .type
= PEER_AT_GLOBAL_FLAG
,
372 .handlers
[0] = TEST_HANDLER(password
),
376 .u
.flag
= PEER_FLAG_SHUTDOWN
,
377 .type
= PEER_AT_GLOBAL_FLAG
,
380 .cmd
= "strict-capability-match",
381 .u
.flag
= PEER_FLAG_STRICT_CAP_MATCH
,
382 .type
= PEER_AT_GLOBAL_FLAG
,
386 .peer_cmd
= "timers 10 30",
387 .group_cmd
= "timers 20 60",
388 .u
.flag
= PEER_FLAG_TIMER
,
389 .type
= PEER_AT_GLOBAL_FLAG
,
390 .handlers
[0] = TEST_HANDLER(timers_1
),
391 .handlers
[1] = TEST_HANDLER(timers_2
),
394 .cmd
= "timers connect",
395 .peer_cmd
= "timers connect 10",
396 .group_cmd
= "timers connect 20",
397 .u
.flag
= PEER_FLAG_TIMER_CONNECT
,
398 .type
= PEER_AT_GLOBAL_FLAG
,
401 .cmd
= "update-source",
402 .peer_cmd
= "update-source 255.255.255.1",
403 .group_cmd
= "update-source 255.255.255.2",
404 .u
.flag
= PEER_FLAG_UPDATE_SOURCE
,
405 .type
= PEER_AT_GLOBAL_FLAG
,
406 .handlers
[0] = TEST_HANDLER(update_source_su
),
409 .cmd
= "update-source",
410 .peer_cmd
= "update-source IF-PEER",
411 .group_cmd
= "update-source IF-GROUP",
412 .u
.flag
= PEER_FLAG_UPDATE_SOURCE
,
413 .type
= PEER_AT_GLOBAL_FLAG
,
414 .handlers
[0] = TEST_HANDLER(update_source_if
),
417 /* Address Family Attributes */
420 .peer_cmd
= "addpath-tx-all-paths",
421 .group_cmd
= "addpath-tx-bestpath-per-AS",
422 .type
= PEER_AT_AF_CUSTOM
,
423 .handlers
[0] = TEST_HANDLER(addpath_types
),
427 .peer_cmd
= "allowas-in 1",
428 .group_cmd
= "allowas-in 2",
429 .u
.flag
= PEER_FLAG_ALLOWAS_IN
,
430 .handlers
[0] = TEST_HANDLER(allowas_in
),
433 .cmd
= "allowas-in origin",
434 .u
.flag
= PEER_FLAG_ALLOWAS_IN_ORIGIN
,
437 .cmd
= "as-override",
438 .u
.flag
= PEER_FLAG_AS_OVERRIDE
,
441 .cmd
= "attribute-unchanged as-path",
442 .u
.flag
= PEER_FLAG_AS_PATH_UNCHANGED
,
445 .cmd
= "attribute-unchanged next-hop",
446 .u
.flag
= PEER_FLAG_NEXTHOP_UNCHANGED
,
449 .cmd
= "attribute-unchanged med",
450 .u
.flag
= PEER_FLAG_MED_UNCHANGED
,
453 .cmd
= "attribute-unchanged as-path next-hop",
454 .u
.flag
= PEER_FLAG_AS_PATH_UNCHANGED
455 | PEER_FLAG_NEXTHOP_UNCHANGED
,
458 .cmd
= "attribute-unchanged as-path med",
459 .u
.flag
= PEER_FLAG_AS_PATH_UNCHANGED
460 | PEER_FLAG_MED_UNCHANGED
,
463 .cmd
= "attribute-unchanged as-path next-hop med",
464 .u
.flag
= PEER_FLAG_AS_PATH_UNCHANGED
465 | PEER_FLAG_NEXTHOP_UNCHANGED
466 | PEER_FLAG_MED_UNCHANGED
,
469 .cmd
= "capability orf prefix-list send",
470 .u
.flag
= PEER_FLAG_ORF_PREFIX_SM
,
473 .cmd
= "capability orf prefix-list receive",
474 .u
.flag
= PEER_FLAG_ORF_PREFIX_RM
,
477 .cmd
= "capability orf prefix-list both",
478 .u
.flag
= PEER_FLAG_ORF_PREFIX_SM
| PEER_FLAG_ORF_PREFIX_RM
,
481 .cmd
= "default-originate",
482 .u
.flag
= PEER_FLAG_DEFAULT_ORIGINATE
,
485 .cmd
= "default-originate route-map",
486 .peer_cmd
= "default-originate route-map RM-PEER",
487 .group_cmd
= "default-originate route-map RM-GROUP",
488 .u
.flag
= PEER_FLAG_DEFAULT_ORIGINATE
,
489 .handlers
[0] = TEST_HANDLER(default_originate_route_map
),
492 .cmd
= "distribute-list",
493 .peer_cmd
= "distribute-list DL-PEER in",
494 .group_cmd
= "distribute-list DL-GROUP in",
495 .type
= PEER_AT_AF_FILTER
,
496 .u
.filter
.flag
= PEER_FT_DISTRIBUTE_LIST
,
497 .u
.filter
.direct
= FILTER_IN
,
498 .handlers
[0] = TEST_HANDLER(distribute_list
),
501 .cmd
= "distribute-list",
502 .peer_cmd
= "distribute-list DL-PEER out",
503 .group_cmd
= "distribute-list DL-GROUP out",
504 .type
= PEER_AT_AF_FILTER
,
505 .u
.filter
.flag
= PEER_FT_DISTRIBUTE_LIST
,
506 .u
.filter
.direct
= FILTER_OUT
,
507 .handlers
[0] = TEST_HANDLER(distribute_list
),
510 .cmd
= "filter-list",
511 .peer_cmd
= "filter-list FL-PEER in",
512 .group_cmd
= "filter-list FL-GROUP in",
513 .type
= PEER_AT_AF_FILTER
,
514 .u
.filter
.flag
= PEER_FT_FILTER_LIST
,
515 .u
.filter
.direct
= FILTER_IN
,
516 .handlers
[0] = TEST_HANDLER(filter_list
),
519 .cmd
= "filter-list",
520 .peer_cmd
= "filter-list FL-PEER out",
521 .group_cmd
= "filter-list FL-GROUP out",
522 .type
= PEER_AT_AF_FILTER
,
523 .u
.filter
.flag
= PEER_FT_FILTER_LIST
,
524 .u
.filter
.direct
= FILTER_OUT
,
525 .handlers
[0] = TEST_HANDLER(filter_list
),
528 .cmd
= "maximum-prefix",
529 .peer_cmd
= "maximum-prefix 10",
530 .group_cmd
= "maximum-prefix 20",
531 .u
.flag
= PEER_FLAG_MAX_PREFIX
,
532 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
535 .cmd
= "maximum-prefix",
536 .peer_cmd
= "maximum-prefix 10 restart 100",
537 .group_cmd
= "maximum-prefix 20 restart 200",
538 .u
.flag
= PEER_FLAG_MAX_PREFIX
,
539 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
540 .handlers
[1] = TEST_HANDLER(maximum_prefix_restart
),
543 .cmd
= "maximum-prefix",
544 .peer_cmd
= "maximum-prefix 10 1 restart 100",
545 .group_cmd
= "maximum-prefix 20 2 restart 200",
546 .u
.flag
= PEER_FLAG_MAX_PREFIX
,
547 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
548 .handlers
[1] = TEST_HANDLER(maximum_prefix_threshold
),
549 .handlers
[2] = TEST_HANDLER(maximum_prefix_restart
),
552 .cmd
= "maximum-prefix",
553 .peer_cmd
= "maximum-prefix 10 warning-only",
554 .group_cmd
= "maximum-prefix 20 warning-only",
555 .u
.flag
= PEER_FLAG_MAX_PREFIX
| PEER_FLAG_MAX_PREFIX_WARNING
,
556 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
559 .cmd
= "maximum-prefix",
560 .peer_cmd
= "maximum-prefix 10 1 warning-only",
561 .group_cmd
= "maximum-prefix 20 2 warning-only",
562 .u
.flag
= PEER_FLAG_MAX_PREFIX
| PEER_FLAG_MAX_PREFIX_WARNING
,
563 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
564 .handlers
[1] = TEST_HANDLER(maximum_prefix_threshold
),
567 .cmd
= "next-hop-self",
568 .u
.flag
= PEER_FLAG_NEXTHOP_SELF
,
571 .cmd
= "next-hop-self force",
572 .u
.flag
= PEER_FLAG_FORCE_NEXTHOP_SELF
,
575 .cmd
= "prefix-list",
576 .peer_cmd
= "prefix-list PL-PEER in",
577 .group_cmd
= "prefix-list PL-GROUP in",
578 .type
= PEER_AT_AF_FILTER
,
579 .u
.filter
.flag
= PEER_FT_PREFIX_LIST
,
580 .u
.filter
.direct
= FILTER_IN
,
581 .handlers
[0] = TEST_HANDLER(prefix_list
),
584 .cmd
= "prefix-list",
585 .peer_cmd
= "prefix-list PL-PEER out",
586 .group_cmd
= "prefix-list PL-GROUP out",
587 .type
= PEER_AT_AF_FILTER
,
588 .u
.filter
.flag
= PEER_FT_PREFIX_LIST
,
589 .u
.filter
.direct
= FILTER_OUT
,
590 .handlers
[0] = TEST_HANDLER(prefix_list
),
593 .cmd
= "remove-private-AS",
594 .u
.flag
= PEER_FLAG_REMOVE_PRIVATE_AS
,
597 .cmd
= "remove-private-AS all",
598 .u
.flag
= PEER_FLAG_REMOVE_PRIVATE_AS
599 | PEER_FLAG_REMOVE_PRIVATE_AS_ALL
,
602 .cmd
= "remove-private-AS replace-AS",
603 .u
.flag
= PEER_FLAG_REMOVE_PRIVATE_AS
604 | PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
,
607 .cmd
= "remove-private-AS all replace-AS",
608 .u
.flag
= PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
,
612 .peer_cmd
= "route-map RM-PEER in",
613 .group_cmd
= "route-map RM-GROUP in",
614 .type
= PEER_AT_AF_FILTER
,
615 .u
.filter
.flag
= PEER_FT_ROUTE_MAP
,
616 .u
.filter
.direct
= FILTER_IN
,
617 .handlers
[0] = TEST_HANDLER(route_map
),
621 .peer_cmd
= "route-map RM-PEER out",
622 .group_cmd
= "route-map RM-GROUP out",
623 .type
= PEER_AT_AF_FILTER
,
624 .u
.filter
.flag
= PEER_FT_ROUTE_MAP
,
625 .u
.filter
.direct
= FILTER_OUT
,
626 .handlers
[0] = TEST_HANDLER(route_map
),
629 .cmd
= "route-reflector-client",
630 .u
.flag
= PEER_FLAG_REFLECTOR_CLIENT
,
632 .o
.skip_xfer_cases
= true,
635 .cmd
= "route-server-client",
636 .u
.flag
= PEER_FLAG_RSERVER_CLIENT
,
639 .cmd
= "send-community",
640 .u
.flag
= PEER_FLAG_SEND_COMMUNITY
,
641 .o
.invert_peer
= true,
642 .o
.invert_group
= true,
645 .cmd
= "send-community extended",
646 .u
.flag
= PEER_FLAG_SEND_EXT_COMMUNITY
,
647 .o
.invert_peer
= true,
648 .o
.invert_group
= true,
651 .cmd
= "send-community large",
652 .u
.flag
= PEER_FLAG_SEND_LARGE_COMMUNITY
,
653 .o
.invert_peer
= true,
654 .o
.invert_group
= true,
657 .cmd
= "soft-reconfiguration inbound",
658 .u
.flag
= PEER_FLAG_SOFT_RECONFIG
,
661 .cmd
= "unsuppress-map",
662 .peer_cmd
= "unsuppress-map UM-PEER",
663 .group_cmd
= "unsuppress-map UM-GROUP",
664 .type
= PEER_AT_AF_FILTER
,
665 .u
.filter
.flag
= PEER_FT_UNSUPPRESS_MAP
,
666 .u
.filter
.direct
= 0,
667 .handlers
[0] = TEST_HANDLER(unsuppress_map
),
671 .peer_cmd
= "weight 100",
672 .group_cmd
= "weight 200",
673 .u
.flag
= PEER_FLAG_WEIGHT
,
674 .handlers
[0] = TEST_HANDLER(weight
),
678 /* clang-format on */
680 static const char *str_from_afi(afi_t afi
)
688 return "<unknown AFI>";
692 static const char *str_from_safi(safi_t safi
)
700 return "<unknown SAFI>";
704 static const char *str_from_attr_type(enum test_peer_attr_type at
)
707 case PEER_AT_GLOBAL_FLAG
:
709 case PEER_AT_AF_FLAG
:
711 case PEER_AT_AF_FILTER
:
713 case PEER_AT_GLOBAL_CUSTOM
:
714 case PEER_AT_AF_CUSTOM
:
721 static bool is_attr_type_global(enum test_peer_attr_type at
)
723 return at
== PEER_AT_GLOBAL_FLAG
|| at
== PEER_AT_GLOBAL_CUSTOM
;
726 static void test_log(struct test
*test
, const char *fmt
, ...)
730 /* Skip logging if test instance has previously failed. */
731 if (test
->state
!= TEST_SUCCESS
)
734 /* Store formatted log message. */
736 listnode_add(test
->log
, str_vprintf(fmt
, ap
));
740 static void test_execute(struct test
*test
, const char *fmt
, ...)
747 /* Skip execution if test instance has previously failed. */
748 if (test
->state
!= TEST_SUCCESS
)
751 /* Format command string with variadic arguments. */
753 cmd
= str_vprintf(fmt
, ap
);
756 test
->state
= TEST_INTERNAL_ERROR
;
758 str_printf("could not format command string [%s]", fmt
);
762 /* Tokenize formatted command string. */
763 vline
= cmd_make_strvec(cmd
);
765 test
->state
= TEST_INTERNAL_ERROR
;
766 test
->error
= str_printf(
767 "tokenizing command string [%s] returned empty result",
769 XFREE(MTYPE_TMP
, cmd
);
774 /* Execute command (non-strict). */
775 ret
= cmd_execute_command(vline
, test
->vty
, NULL
, 0);
776 if (ret
!= CMD_SUCCESS
) {
777 test
->state
= TEST_COMMAND_ERROR
;
778 test
->error
= str_printf(
779 "execution of command [%s] has failed with code [%d]",
784 cmd_free_strvec(vline
);
785 XFREE(MTYPE_TMP
, cmd
);
788 static void test_config(struct test
*test
, const char *fmt
, bool invert
,
796 /* Skip execution if test instance has previously failed. */
797 if (test
->state
!= TEST_SUCCESS
)
800 /* Format matcher string with variadic arguments. */
802 matcher
= str_vprintf(fmt
, apc
);
805 test
->state
= TEST_INTERNAL_ERROR
;
807 str_printf("could not format matcher string [%s]", fmt
);
811 /* Fetch BGP configuration into buffer. */
812 bgp_config_write(test
->vty
);
813 config
= buffer_getstr(test
->vty
->obuf
);
814 buffer_reset(test
->vty
->obuf
);
816 /* Match config against matcher. */
817 matched
= !!strstr(config
, matcher
);
818 if (!matched
&& !invert
) {
819 test
->state
= TEST_CONFIG_ERROR
;
820 test
->error
= str_printf("expected config [%s] to be present",
822 } else if (matched
&& invert
) {
823 test
->state
= TEST_CONFIG_ERROR
;
824 test
->error
= str_printf("expected config [%s] to be absent",
828 /* Free memory and return. */
829 XFREE(MTYPE_TMP
, matcher
);
830 XFREE(MTYPE_TMP
, config
);
833 static void test_config_present(struct test
*test
, const char *fmt
, ...)
838 test_config(test
, fmt
, false, ap
);
842 static void test_config_absent(struct test
*test
, const char *fmt
, ...)
847 test_config(test
, fmt
, true, ap
);
851 static void test_initialize(struct test
*test
)
855 /* Skip execution if test instance has previously failed. */
856 if (test
->state
!= TEST_SUCCESS
)
859 /* Log message about (re)-initialization */
860 test_log(test
, "prepare: %sinitialize bgp test environment",
861 test
->bgp
? "re-" : "");
863 /* Attempt gracefully to purge previous BGP configuration. */
864 test_execute(test
, "no router bgp");
865 test
->state
= TEST_SUCCESS
;
867 /* Initialize BGP test environment. */
868 test_execute(test
, "router bgp %d", cfg
.local_asn
);
869 test_execute(test
, "no bgp default ipv4-unicast");
870 test_execute(test
, "neighbor %s peer-group", cfg
.peer_group
);
871 if (test
->o
.use_iface_peer
) {
872 test_execute(test
, "neighbor %s interface", cfg
.peer_interface
);
873 test_execute(test
, "neighbor %s remote-as %d",
875 test
->o
.use_ibgp
? cfg
.local_asn
: cfg
.peer_asn
);
877 test_execute(test
, "neighbor %s remote-as %d", cfg
.peer_address
,
878 test
->o
.use_ibgp
? cfg
.local_asn
: cfg
.peer_asn
);
881 if (test
->state
!= TEST_SUCCESS
)
884 /* Fetch default BGP instance. */
885 test
->bgp
= bgp_get_default();
887 test
->state
= TEST_INTERNAL_ERROR
;
889 str_printf("could not retrieve default bgp instance");
893 /* Fetch peer instance. */
894 if (test
->o
.use_iface_peer
) {
896 peer_lookup_by_conf_if(test
->bgp
, cfg
.peer_interface
);
898 str2sockunion(cfg
.peer_address
, &su
);
899 test
->peer
= peer_lookup(test
->bgp
, &su
);
902 test
->state
= TEST_INTERNAL_ERROR
;
903 test
->error
= str_printf(
904 "could not retrieve instance of bgp peer [%s]",
909 /* Fetch peer-group instance. */
910 test
->group
= peer_group_lookup(test
->bgp
, cfg
.peer_group
);
912 test
->state
= TEST_INTERNAL_ERROR
;
913 test
->error
= str_printf(
914 "could not retrieve instance of bgp peer-group [%s]",
920 static struct test
*test_new(const char *desc
, bool use_ibgp
,
925 test
= XCALLOC(MTYPE_TMP
, sizeof(struct test
));
926 test
->state
= TEST_SUCCESS
;
927 test
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
928 test
->log
= list_new();
929 test
->o
.use_ibgp
= use_ibgp
;
930 test
->o
.use_iface_peer
= use_iface_peer
;
932 test
->vty
= vty_new();
933 test
->vty
->type
= VTY_TERM
;
934 test
->vty
->node
= CONFIG_NODE
;
936 test_initialize(test
);
941 static void test_finish(struct test
*test
)
944 struct listnode
*node
, *nnode
;
946 /* Print test output header. */
947 printf("%s [test] %s\n",
948 (test
->state
== TEST_SUCCESS
) ? OUT_SYMBOL_OK
: OUT_SYMBOL_NOK
,
951 /* Print test log messages. */
952 for (ALL_LIST_ELEMENTS(test
->log
, node
, nnode
, msg
)) {
953 printf("%s %s\n", OUT_SYMBOL_INFO
, msg
);
954 XFREE(MTYPE_TMP
, msg
);
957 /* Print test error message if available. */
958 if (test
->state
!= TEST_SUCCESS
&& test
->error
)
959 printf("%s error: %s\n", OUT_SYMBOL_INFO
, test
->error
);
961 /* Print machine-readable result of test. */
962 printf("%s\n", test
->state
== TEST_SUCCESS
? "OK" : "failed");
964 /* Cleanup allocated memory. */
966 vty_close(test
->vty
);
970 list_delete(&test
->log
);
972 XFREE(MTYPE_TMP
, test
->desc
);
974 XFREE(MTYPE_TMP
, test
->error
);
975 XFREE(MTYPE_TMP
, test
);
978 static void test_peer_flags(struct test
*test
, struct test_peer_attr
*pa
,
979 struct peer
*peer
, bool exp_val
, bool exp_ovrd
)
981 bool exp_inv
, cur_val
, cur_ovrd
, cur_inv
;
983 /* Skip execution if test instance has previously failed. */
984 if (test
->state
!= TEST_SUCCESS
)
987 /* Detect if flag is meant to be inverted. */
988 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
989 exp_inv
= pa
->o
.invert_group
;
991 exp_inv
= pa
->o
.invert_peer
;
993 /* Flip expected value if flag is inverted. */
996 /* Fetch current state of value, override and invert flags. */
997 if (pa
->type
== PEER_AT_GLOBAL_FLAG
) {
998 cur_val
= !!CHECK_FLAG(peer
->flags
, pa
->u
.flag
);
999 cur_ovrd
= !!CHECK_FLAG(peer
->flags_override
, pa
->u
.flag
);
1000 cur_inv
= !!CHECK_FLAG(peer
->flags_invert
, pa
->u
.flag
);
1001 } else /* if (pa->type == PEER_AT_AF_FLAG) */ {
1002 cur_val
= !!CHECK_FLAG(peer
->af_flags
[pa
->afi
][pa
->safi
],
1004 cur_ovrd
= !!CHECK_FLAG(
1005 peer
->af_flags_override
[pa
->afi
][pa
->safi
], pa
->u
.flag
);
1006 cur_inv
= !!CHECK_FLAG(peer
->af_flags_invert
[pa
->afi
][pa
->safi
],
1010 /* Assert expected flag states. */
1011 TEST_ASSERT_EQ(test
, cur_val
, exp_val
);
1012 TEST_ASSERT_EQ(test
, cur_ovrd
, exp_ovrd
);
1013 TEST_ASSERT_EQ(test
, cur_inv
, exp_inv
);
1016 static void test_af_filter(struct test
*test
, struct test_peer_attr
*pa
,
1017 struct peer
*peer
, bool exp_state
, bool exp_ovrd
)
1020 struct bgp_filter
*filter
;
1022 /* Skip execution if test instance has previously failed. */
1023 if (test
->state
!= TEST_SUCCESS
)
1026 /* Fetch and assert current state of override flag. */
1027 cur_ovrd
= !!CHECK_FLAG(
1028 peer
->filter_override
[pa
->afi
][pa
->safi
][pa
->u
.filter
.direct
],
1031 TEST_ASSERT_EQ(test
, cur_ovrd
, exp_ovrd
);
1033 /* Assert that map/list matches expected state (set/unset). */
1034 filter
= &peer
->filter
[pa
->afi
][pa
->safi
];
1036 switch (pa
->u
.filter
.flag
) {
1037 case PEER_FT_DISTRIBUTE_LIST
:
1038 TEST_ASSERT_EQ(test
,
1039 !!(filter
->dlist
[pa
->u
.filter
.direct
].name
),
1042 case PEER_FT_FILTER_LIST
:
1043 TEST_ASSERT_EQ(test
,
1044 !!(filter
->aslist
[pa
->u
.filter
.direct
].name
),
1047 case PEER_FT_PREFIX_LIST
:
1048 TEST_ASSERT_EQ(test
,
1049 !!(filter
->plist
[pa
->u
.filter
.direct
].name
),
1052 case PEER_FT_ROUTE_MAP
:
1053 TEST_ASSERT_EQ(test
, !!(filter
->map
[pa
->u
.filter
.direct
].name
),
1056 case PEER_FT_UNSUPPRESS_MAP
:
1057 TEST_ASSERT_EQ(test
, !!(filter
->usmap
.name
), exp_state
);
1062 static void test_custom(struct test
*test
, struct test_peer_attr
*pa
,
1063 struct peer
*peer
, struct peer
*group
, bool peer_set
,
1067 char *handler_error
;
1069 for (i
= 0; i
< TEST_HANDLER_MAX
; i
++) {
1070 /* Skip execution if test instance has previously failed. */
1071 if (test
->state
!= TEST_SUCCESS
)
1074 /* Skip further execution if handler is undefined. */
1075 if (!pa
->handlers
[i
])
1078 /* Execute custom handler. */
1079 pa
->handlers
[i
](test
, pa
, peer
, group
, peer_set
, group_set
);
1080 if (test
->state
!= TEST_SUCCESS
) {
1081 test
->state
= TEST_CUSTOM_ERROR
;
1082 handler_error
= test
->error
;
1083 test
->error
= str_printf("custom handler failed: %s",
1085 XFREE(MTYPE_TMP
, handler_error
);
1091 static void test_process(struct test
*test
, struct test_peer_attr
*pa
,
1092 struct peer
*peer
, struct peer
*group
, bool peer_set
,
1096 case PEER_AT_GLOBAL_FLAG
:
1097 case PEER_AT_AF_FLAG
:
1100 peer_set
|| (peer_group_active(peer
) && group_set
),
1102 test_peer_flags(test
, pa
, group
, group_set
, false);
1105 case PEER_AT_AF_FILTER
:
1108 peer_set
|| (peer_group_active(peer
) && group_set
),
1110 test_af_filter(test
, pa
, group
, group_set
, false);
1113 case PEER_AT_GLOBAL_CUSTOM
:
1114 case PEER_AT_AF_CUSTOM
:
1116 * Do nothing here - a custom handler can be executed, but this
1117 * is not required. This will allow defining peer attributes
1118 * which shall not be checked for flag/filter/other internal
1124 test
->state
= TEST_INTERNAL_ERROR
;
1126 str_printf("invalid attribute type: %d", pa
->type
);
1130 /* Attempt to call a custom handler if set for further processing. */
1131 test_custom(test
, pa
, peer
, group
, peer_set
, group_set
);
1134 static void test_peer_attr(struct test
*test
, struct test_peer_attr
*pa
)
1138 const char *ecp
= pa
->o
.invert_peer
? "no " : "";
1139 const char *dcp
= pa
->o
.invert_peer
? "" : "no ";
1140 const char *ecg
= pa
->o
.invert_group
? "no " : "";
1141 const char *dcg
= pa
->o
.invert_group
? "" : "no ";
1142 const char *peer_cmd
= pa
->peer_cmd
?: pa
->cmd
;
1143 const char *group_cmd
= pa
->group_cmd
?: pa
->cmd
;
1144 struct peer
*p
= test
->peer
;
1145 struct peer_group
*g
= test
->group
;
1147 /* Determine type and if test is address-family relevant */
1148 type
= str_from_attr_type(pa
->type
);
1150 test
->state
= TEST_INTERNAL_ERROR
;
1152 str_printf("invalid attribute type: %d", pa
->type
);
1157 * =====================================================================
1158 * Test Case Suite 1: Config persistence after adding peer to group
1160 * Example: If a peer attribute has value [1] and a group attribute has
1161 * value [2], the peer attribute value should be persisted when the peer
1162 * gets added to the peer-group.
1164 * This test suite is meant to test the group2peer functions which can
1165 * be found inside bgpd/bgpd.c, which are related to initial peer-group
1167 * =====================================================================
1170 /* Test Preparation: Switch and activate address-family. */
1171 if (!is_attr_type_global(pa
->type
)) {
1172 test_log(test
, "prepare: switch address-family to [%s]",
1173 afi_safi_print(pa
->afi
, pa
->safi
));
1174 test_execute(test
, "address-family %s %s",
1175 str_from_afi(pa
->afi
), str_from_safi(pa
->safi
));
1176 test_execute(test
, "neighbor %s activate", g
->name
);
1177 test_execute(test
, "neighbor %s activate", p
->host
);
1180 /* Skip peer-group to peer transfer test cases if requested. */
1181 if (pa
->o
.skip_xfer_cases
&& test
->state
== TEST_SUCCESS
)
1182 test
->state
= TEST_SKIPPING
;
1184 /* Test Case: Set flag on BGP peer. */
1185 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, peer_cmd
,
1187 test_execute(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1188 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1189 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1190 test_process(test
, pa
, p
, g
->conf
, true, false);
1192 /* Test Case: Set flag on BGP peer-group. */
1193 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, group_cmd
,
1195 test_execute(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1196 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1197 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1198 test_process(test
, pa
, p
, g
->conf
, true, true);
1200 /* Test Case: Add BGP peer to peer-group. */
1201 test_log(test
, "case %02d: add peer [%s] to group [%s]", tc
++, p
->host
,
1203 test_execute(test
, "neighbor %s peer-group %s", p
->host
, g
->name
);
1204 test_config_present(test
, "neighbor %s %speer-group %s", p
->host
,
1205 p
->conf_if
? "interface " : "", g
->name
);
1206 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1207 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1208 test_process(test
, pa
, p
, g
->conf
, true, true);
1210 /* Test Case: Unset flag on BGP peer-group. */
1211 test_log(test
, "case %02d: unset %s [%s] on [%s]", tc
++, type
,
1212 group_cmd
, g
->name
);
1213 test_execute(test
, "%sneighbor %s %s", dcg
, g
->name
, group_cmd
);
1214 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1215 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1216 test_process(test
, pa
, p
, g
->conf
, true, false);
1219 * =====================================================================
1220 * Test Case Suite 2: Config inheritance after adding peer to group
1222 * Example: If a peer attribute has not been set and a group attribute
1223 * has a value of [2], the group attribute should be inherited to the
1224 * peer without flagging the newly set value as overridden.
1226 * This test suite is meant to test the group2peer functions which can
1227 * be found inside bgpd/bgpd.c, which are related to initial peer-group
1229 * =====================================================================
1232 /* Test Preparation: Re-initialize test environment. */
1233 test_initialize(test
);
1237 /* Test Preparation: Switch and activate address-family. */
1238 if (!is_attr_type_global(pa
->type
)) {
1239 test_log(test
, "prepare: switch address-family to [%s]",
1240 afi_safi_print(pa
->afi
, pa
->safi
));
1241 test_execute(test
, "address-family %s %s",
1242 str_from_afi(pa
->afi
), str_from_safi(pa
->safi
));
1243 test_execute(test
, "neighbor %s activate", g
->name
);
1244 test_execute(test
, "neighbor %s activate", p
->host
);
1247 /* Test Case: Set flag on BGP peer-group. */
1248 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, group_cmd
,
1250 test_execute(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1251 test_config_absent(test
, "neighbor %s %s", p
->host
, pa
->cmd
);
1252 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1253 test_process(test
, pa
, p
, g
->conf
, false, true);
1255 /* Test Case: Add BGP peer to peer-group. */
1256 test_log(test
, "case %02d: add peer [%s] to group [%s]", tc
++, p
->host
,
1258 test_execute(test
, "neighbor %s peer-group %s", p
->host
, g
->name
);
1259 test_config_present(test
, "neighbor %s %speer-group %s", p
->host
,
1260 p
->conf_if
? "interface " : "", g
->name
);
1261 test_config_absent(test
, "neighbor %s %s", p
->host
, pa
->cmd
);
1262 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1263 test_process(test
, pa
, p
, g
->conf
, false, true);
1265 /* Stop skipping test cases if previously enabled. */
1266 if (pa
->o
.skip_xfer_cases
&& test
->state
== TEST_SKIPPING
)
1267 test
->state
= TEST_SUCCESS
;
1270 * =====================================================================
1271 * Test Case Suite 3: Miscellaneous flag checks
1273 * This test suite does not focus on initial peer-group inheritance and
1274 * instead executes various different commands to set/unset attributes
1275 * on both peer- and group-level. These checks should always be executed
1277 * =====================================================================
1280 /* Test Preparation: Re-initialize test environment. */
1281 test_initialize(test
);
1285 /* Test Preparation: Switch and activate address-family. */
1286 if (!is_attr_type_global(pa
->type
)) {
1287 test_log(test
, "prepare: switch address-family to [%s]",
1288 afi_safi_print(pa
->afi
, pa
->safi
));
1289 test_execute(test
, "address-family %s %s",
1290 str_from_afi(pa
->afi
), str_from_safi(pa
->safi
));
1291 test_execute(test
, "neighbor %s activate", g
->name
);
1292 test_execute(test
, "neighbor %s activate", p
->host
);
1295 /* Test Case: Set flag on BGP peer. */
1296 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, peer_cmd
,
1298 test_execute(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1299 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1300 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1301 test_process(test
, pa
, p
, g
->conf
, true, false);
1303 /* Test Case: Add BGP peer to peer-group. */
1304 test_log(test
, "case %02d: add peer [%s] to group [%s]", tc
++, p
->host
,
1306 test_execute(test
, "neighbor %s peer-group %s", p
->host
, g
->name
);
1307 test_config_present(test
, "neighbor %s %speer-group %s", p
->host
,
1308 p
->conf_if
? "interface " : "", g
->name
);
1309 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1310 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1311 test_process(test
, pa
, p
, g
->conf
, true, false);
1313 /* Test Case: Re-add BGP peer to peer-group. */
1314 test_log(test
, "case %02d: re-add peer [%s] to group [%s]", tc
++,
1316 test_execute(test
, "neighbor %s peer-group %s", p
->host
, g
->name
);
1317 test_config_present(test
, "neighbor %s %speer-group %s", p
->host
,
1318 p
->conf_if
? "interface " : "", g
->name
);
1319 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1320 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1321 test_process(test
, pa
, p
, g
->conf
, true, false);
1323 /* Test Case: Set flag on BGP peer-group. */
1324 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, group_cmd
,
1326 test_execute(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1327 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1328 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1329 test_process(test
, pa
, p
, g
->conf
, true, true);
1331 /* Test Case: Unset flag on BGP peer-group. */
1332 test_log(test
, "case %02d: unset %s [%s] on [%s]", tc
++, type
,
1333 group_cmd
, g
->name
);
1334 test_execute(test
, "%sneighbor %s %s", dcg
, g
->name
, group_cmd
);
1335 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1336 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1337 test_process(test
, pa
, p
, g
->conf
, true, false);
1339 /* Test Case: Set flag on BGP peer-group. */
1340 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, group_cmd
,
1342 test_execute(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1343 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1344 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1345 test_process(test
, pa
, p
, g
->conf
, true, true);
1347 /* Test Case: Re-set flag on BGP peer. */
1348 test_log(test
, "case %02d: re-set %s [%s] on [%s]", tc
++, type
,
1350 test_execute(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1351 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1352 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1353 test_process(test
, pa
, p
, g
->conf
, true, true);
1355 /* Test Case: Unset flag on BGP peer. */
1356 test_log(test
, "case %02d: unset %s [%s] on [%s]", tc
++, type
, peer_cmd
,
1358 test_execute(test
, "%sneighbor %s %s", dcp
, p
->host
, peer_cmd
);
1359 test_config_absent(test
, "neighbor %s %s", p
->host
, pa
->cmd
);
1360 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1361 test_process(test
, pa
, p
, g
->conf
, false, true);
1363 /* Test Case: Unset flag on BGP peer-group. */
1364 test_log(test
, "case %02d: unset %s [%s] on [%s]", tc
++, type
,
1365 group_cmd
, g
->name
);
1366 test_execute(test
, "%sneighbor %s %s", dcg
, g
->name
, group_cmd
);
1367 test_config_absent(test
, "neighbor %s %s", p
->host
, pa
->cmd
);
1368 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1369 test_process(test
, pa
, p
, g
->conf
, false, false);
1371 /* Test Case: Set flag on BGP peer. */
1372 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, peer_cmd
,
1374 test_execute(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1375 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1376 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1377 test_process(test
, pa
, p
, g
->conf
, true, false);
1380 static void bgp_startup(void)
1383 openzlog("testbgpd", "NONE", 0, LOG_CONS
| LOG_NDELAY
| LOG_PID
,
1385 zprivs_preinit(&bgpd_privs
);
1386 zprivs_init(&bgpd_privs
);
1388 master
= thread_master_create(NULL
);
1390 nb_init(master
, NULL
, 0);
1391 bgp_master_init(master
);
1392 bgp_option_set(BGP_OPT_NO_LISTEN
);
1393 vrf_init(NULL
, NULL
, NULL
, NULL
, NULL
);
1398 static void bgp_shutdown(void)
1401 struct listnode
*node
, *nnode
;
1405 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
1409 bgp_route_map_terminate();
1411 bgp_pthreads_finish();
1412 access_list_add_hook(NULL
);
1413 access_list_delete_hook(NULL
);
1414 access_list_reset();
1415 as_list_add_hook(NULL
);
1416 as_list_delete_hook(NULL
);
1418 prefix_list_add_hook(NULL
);
1419 prefix_list_delete_hook(NULL
);
1420 prefix_list_reset();
1421 community_list_terminate(bgp_clist
);
1423 #ifdef ENABLE_BGP_VNC
1424 vnc_zebra_destroy();
1426 bgp_zebra_destroy();
1428 bf_free(bm
->rd_idspace
);
1429 list_delete(&bm
->bgp
);
1430 memset(bm
, 0, sizeof(*bm
));
1436 zprivs_terminate(&bgpd_privs
);
1437 thread_master_free(master
);
1445 struct list
*pa_list
;
1446 struct test_peer_attr
*pa
, *pac
;
1447 struct listnode
*node
, *nnode
;
1451 pa_list
= list_new();
1453 while (test_peer_attrs
[i
].cmd
) {
1454 pa
= &test_peer_attrs
[i
++];
1456 /* Just copy the peer attribute structure for global flags. */
1457 if (is_attr_type_global(pa
->type
)) {
1458 pac
= XMALLOC(MTYPE_TMP
, sizeof(struct test_peer_attr
));
1459 memcpy(pac
, pa
, sizeof(struct test_peer_attr
));
1460 listnode_add(pa_list
, pac
);
1464 /* Fallback to default families if not specified. */
1465 if (!pa
->families
[0].afi
&& !pa
->families
[0].safi
)
1466 memcpy(&pa
->families
, test_default_families
,
1467 sizeof(test_default_families
));
1469 /* Add peer attribute definition for each address family. */
1471 while (pa
->families
[ii
].afi
&& pa
->families
[ii
].safi
) {
1472 pac
= XMALLOC(MTYPE_TMP
, sizeof(struct test_peer_attr
));
1473 memcpy(pac
, pa
, sizeof(struct test_peer_attr
));
1475 pac
->afi
= pa
->families
[ii
].afi
;
1476 pac
->safi
= pa
->families
[ii
].safi
;
1477 listnode_add(pa_list
, pac
);
1483 for (ALL_LIST_ELEMENTS(pa_list
, node
, nnode
, pa
)) {
1487 /* Build test description string. */
1488 if (pa
->afi
&& pa
->safi
)
1489 desc
= str_printf("peer\\%s-%s\\%s",
1490 str_from_afi(pa
->afi
),
1491 str_from_safi(pa
->safi
), pa
->cmd
);
1493 desc
= str_printf("peer\\%s", pa
->cmd
);
1495 /* Initialize new test instance. */
1496 test
= test_new(desc
, pa
->o
.use_ibgp
, pa
->o
.use_iface_peer
);
1497 XFREE(MTYPE_TMP
, desc
);
1499 /* Execute tests and finish test instance. */
1500 test_peer_attr(test
, pa
);
1503 /* Print empty line as spacer. */
1506 /* Free memory used for peer-attr declaration. */
1507 XFREE(MTYPE_TMP
, pa
);
1510 list_delete(&pa_list
);