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"
32 #include "bgpd/bgp_network.h"
35 #include "bgpd/rfapi/rfapi_backend.h"
38 #define OUT_SYMBOL_INFO "\u25ba"
39 #define OUT_SYMBOL_OK "\u2714"
40 #define OUT_SYMBOL_NOK "\u2716"
42 #define TEST_ASSERT(T, C) \
44 if ((T)->state != TEST_SUCCESS || (C)) \
46 (T)->state = TEST_ASSERT_ERROR; \
47 (T)->error = str_printf("assertion failed: %s (%s:%d)", (#C), \
48 __FILE__, __LINE__); \
51 #define TEST_ASSERT_EQ(T, A, B) \
53 if ((T)->state != TEST_SUCCESS || ((A) == (B))) \
55 (T)->state = TEST_ASSERT_ERROR; \
56 (T)->error = str_printf( \
57 "assertion failed: %s[%d] == [%d]%s (%s:%d)", (#A), \
58 (A), (B), (#B), __FILE__, __LINE__); \
61 #define TEST_HANDLER_MAX 5
62 #define TEST_HANDLER(name) _test_handler_##name
63 #define TEST_HANDLER_DECL(name) \
64 static void _test_handler_##name( \
65 struct test *test, struct test_peer_attr *pa, \
66 struct peer *peer, struct peer *group, bool peer_set, \
69 #define TEST_ATTR_HANDLER_DECL(name, attr, pval, gval) \
70 TEST_HANDLER_DECL(name) \
73 TEST_ASSERT_EQ(test, peer->attr, (pval)); \
74 else if (peer_group_active(peer) && group_set) \
75 TEST_ASSERT_EQ(test, peer->attr, (gval)); \
77 TEST_ASSERT_EQ(test, group->attr, (gval)); \
79 TEST_HANDLER_DECL(name)
81 #define TEST_STR_ATTR_HANDLER_DECL(name, attr, pval, gval) \
82 TEST_HANDLER_DECL(name) \
85 TEST_ASSERT(test, peer->attr != NULL); \
86 TEST_ASSERT(test, !strcmp(peer->attr, (pval))); \
87 } else if (peer_group_active(peer) && group_set) { \
88 TEST_ASSERT(test, peer->attr != NULL); \
89 TEST_ASSERT(test, !strcmp(peer->attr, (gval))); \
92 TEST_ASSERT(test, group->attr != NULL); \
93 TEST_ASSERT(test, !strcmp(group->attr, (gval))); \
96 TEST_HANDLER_DECL(name)
98 #define TEST_SU_ATTR_HANDLER_DECL(name, attr, pval, gval) \
99 TEST_HANDLER_DECL(name) \
101 union sockunion su; \
103 str2sockunion(pval, &su); \
104 TEST_ASSERT(test, !sockunion_cmp(peer->attr, &su)); \
105 } else if (peer_group_active(peer) && group_set) { \
106 str2sockunion(gval, &su); \
107 TEST_ASSERT(test, !sockunion_cmp(group->attr, &su)); \
110 str2sockunion(gval, &su); \
111 TEST_ASSERT(test, !sockunion_cmp(group->attr, &su)); \
114 TEST_HANDLER_DECL(name)
116 /* Required variables to link in libbgp */
117 struct zebra_privs_t bgpd_privs
= {0};
118 struct thread_master
*master
;
130 enum test_peer_attr_type
{
132 PEER_AT_AF_FILTER
= 1,
133 PEER_AT_AF_CUSTOM
= 2,
134 PEER_AT_GLOBAL_FLAG
= 3,
135 PEER_AT_GLOBAL_CUSTOM
= 4
139 enum test_state state
;
147 struct peer_group
*group
;
158 const char *peer_address
;
159 const char *peer_interface
;
160 const char *peer_group
;
163 struct test_peer_family
{
168 struct test_peer_attr
{
170 const char *peer_cmd
;
171 const char *group_cmd
;
173 enum test_peer_attr_type type
;
186 bool skip_xfer_cases
;
191 struct test_peer_family families
[AFI_MAX
* SAFI_MAX
];
193 void (*handlers
[TEST_HANDLER_MAX
])(struct test
*test
,
194 struct test_peer_attr
*pa
,
196 struct peer
*group
, bool peer_set
,
200 static struct test_config cfg
= {
203 .peer_address
= "1.1.1.1",
204 .peer_interface
= "IP-TEST",
205 .peer_group
= "PG-TEST",
208 static struct test_peer_family test_default_families
[] = {
209 {.afi
= AFI_IP
, .safi
= SAFI_UNICAST
},
210 {.afi
= AFI_IP
, .safi
= SAFI_MULTICAST
},
211 {.afi
= AFI_IP6
, .safi
= SAFI_UNICAST
},
212 {.afi
= AFI_IP6
, .safi
= SAFI_MULTICAST
},
215 static char *str_vprintf(const char *fmt
, va_list ap
)
224 ret
= vsnprintf(buf
, buf_size
, fmt
, apc
);
227 if (ret
>= 0 && ret
< buf_size
)
235 buf
= XREALLOC(MTYPE_TMP
, buf
, buf_size
);
241 static char *str_printf(const char *fmt
, ...)
247 buf
= str_vprintf(fmt
, ap
);
253 TEST_ATTR_HANDLER_DECL(advertisement_interval
, v_routeadv
, 10, 20);
254 TEST_STR_ATTR_HANDLER_DECL(password
, password
, "FRR-Peer", "FRR-Group");
255 TEST_ATTR_HANDLER_DECL(local_as
, change_local_as
, 1, 2);
256 TEST_ATTR_HANDLER_DECL(timers_1
, keepalive
, 10, 20);
257 TEST_ATTR_HANDLER_DECL(timers_2
, holdtime
, 30, 60);
258 TEST_ATTR_HANDLER_DECL(addpath_types
, addpath_type
[pa
->afi
][pa
->safi
],
259 BGP_ADDPATH_ALL
, BGP_ADDPATH_BEST_PER_AS
);
260 TEST_SU_ATTR_HANDLER_DECL(update_source_su
, update_source
, "255.255.255.1",
262 TEST_STR_ATTR_HANDLER_DECL(update_source_if
, update_if
, "IF-PEER", "IF-GROUP");
264 TEST_ATTR_HANDLER_DECL(allowas_in
, allowas_in
[pa
->afi
][pa
->safi
], 1, 2);
265 TEST_STR_ATTR_HANDLER_DECL(default_originate_route_map
,
266 default_rmap
[pa
->afi
][pa
->safi
].name
, "RM-PEER",
268 TEST_STR_ATTR_HANDLER_DECL(
270 filter
[pa
->afi
][pa
->safi
].dlist
[pa
->u
.filter
.direct
].name
, "DL-PEER",
272 TEST_STR_ATTR_HANDLER_DECL(
273 filter_list
, filter
[pa
->afi
][pa
->safi
].aslist
[pa
->u
.filter
.direct
].name
,
274 "FL-PEER", "FL-GROUP");
275 TEST_ATTR_HANDLER_DECL(maximum_prefix
, pmax
[pa
->afi
][pa
->safi
], 10, 20);
276 TEST_ATTR_HANDLER_DECL(maximum_prefix_threshold
,
277 pmax_threshold
[pa
->afi
][pa
->safi
], 1, 2);
278 TEST_ATTR_HANDLER_DECL(maximum_prefix_restart
, pmax_restart
[pa
->afi
][pa
->safi
],
280 TEST_STR_ATTR_HANDLER_DECL(
281 prefix_list
, filter
[pa
->afi
][pa
->safi
].plist
[pa
->u
.filter
.direct
].name
,
282 "PL-PEER", "PL-GROUP");
283 TEST_STR_ATTR_HANDLER_DECL(
284 route_map
, filter
[pa
->afi
][pa
->safi
].map
[pa
->u
.filter
.direct
].name
,
285 "RM-PEER", "RM-GROUP");
286 TEST_STR_ATTR_HANDLER_DECL(unsuppress_map
, filter
[pa
->afi
][pa
->safi
].usmap
.name
,
287 "UM-PEER", "UM-GROUP");
288 TEST_ATTR_HANDLER_DECL(weight
, weight
[pa
->afi
][pa
->safi
], 100, 200);
290 /* clang-format off */
291 static struct test_peer_attr test_peer_attrs
[] = {
292 /* Peer Attributes */
294 .cmd
= "advertisement-interval",
295 .peer_cmd
= "advertisement-interval 10",
296 .group_cmd
= "advertisement-interval 20",
297 .u
.flag
= PEER_FLAG_ROUTEADV
,
298 .type
= PEER_AT_GLOBAL_FLAG
,
299 .handlers
[0] = TEST_HANDLER(advertisement_interval
),
302 .cmd
= "capability dynamic",
303 .u
.flag
= PEER_FLAG_DYNAMIC_CAPABILITY
,
304 .type
= PEER_AT_GLOBAL_FLAG
,
307 .cmd
= "capability extended-nexthop",
308 .u
.flag
= PEER_FLAG_CAPABILITY_ENHE
,
309 .type
= PEER_AT_GLOBAL_FLAG
,
312 .cmd
= "capability extended-nexthop",
313 .u
.flag
= PEER_FLAG_CAPABILITY_ENHE
,
314 .type
= PEER_AT_GLOBAL_FLAG
,
315 .o
.invert_peer
= true,
316 .o
.use_iface_peer
= true,
319 .cmd
= "description",
320 .peer_cmd
= "description FRR Peer",
321 .group_cmd
= "description FRR Group",
322 .type
= PEER_AT_GLOBAL_CUSTOM
,
325 .cmd
= "disable-connected-check",
326 .u
.flag
= PEER_FLAG_DISABLE_CONNECTED_CHECK
,
327 .type
= PEER_AT_GLOBAL_FLAG
,
330 .cmd
= "dont-capability-negotiate",
331 .u
.flag
= PEER_FLAG_DONT_CAPABILITY
,
332 .type
= PEER_AT_GLOBAL_FLAG
,
335 .cmd
= "enforce-first-as",
336 .u
.flag
= PEER_FLAG_ENFORCE_FIRST_AS
,
337 .type
= PEER_AT_GLOBAL_FLAG
,
341 .peer_cmd
= "local-as 1",
342 .group_cmd
= "local-as 2",
343 .u
.flag
= PEER_FLAG_LOCAL_AS
,
344 .type
= PEER_AT_GLOBAL_FLAG
,
345 .handlers
[0] = TEST_HANDLER(local_as
),
348 .cmd
= "local-as 1 no-prepend",
349 .u
.flag
= PEER_FLAG_LOCAL_AS
| PEER_FLAG_LOCAL_AS_NO_PREPEND
,
350 .type
= PEER_AT_GLOBAL_FLAG
,
353 .cmd
= "local-as 1 no-prepend replace-as",
354 .u
.flag
= PEER_FLAG_LOCAL_AS
| PEER_FLAG_LOCAL_AS_REPLACE_AS
,
355 .type
= PEER_AT_GLOBAL_FLAG
,
358 .cmd
= "override-capability",
359 .u
.flag
= PEER_FLAG_OVERRIDE_CAPABILITY
,
360 .type
= PEER_AT_GLOBAL_FLAG
,
364 .u
.flag
= PEER_FLAG_PASSIVE
,
365 .type
= PEER_AT_GLOBAL_FLAG
,
369 .peer_cmd
= "password FRR-Peer",
370 .group_cmd
= "password FRR-Group",
371 .u
.flag
= PEER_FLAG_PASSWORD
,
372 .type
= PEER_AT_GLOBAL_FLAG
,
373 .handlers
[0] = TEST_HANDLER(password
),
377 .u
.flag
= PEER_FLAG_SHUTDOWN
,
378 .type
= PEER_AT_GLOBAL_FLAG
,
381 .cmd
= "strict-capability-match",
382 .u
.flag
= PEER_FLAG_STRICT_CAP_MATCH
,
383 .type
= PEER_AT_GLOBAL_FLAG
,
387 .peer_cmd
= "timers 10 30",
388 .group_cmd
= "timers 20 60",
389 .u
.flag
= PEER_FLAG_TIMER
,
390 .type
= PEER_AT_GLOBAL_FLAG
,
391 .handlers
[0] = TEST_HANDLER(timers_1
),
392 .handlers
[1] = TEST_HANDLER(timers_2
),
395 .cmd
= "timers connect",
396 .peer_cmd
= "timers connect 10",
397 .group_cmd
= "timers connect 20",
398 .u
.flag
= PEER_FLAG_TIMER_CONNECT
,
399 .type
= PEER_AT_GLOBAL_FLAG
,
402 .cmd
= "update-source",
403 .peer_cmd
= "update-source 255.255.255.1",
404 .group_cmd
= "update-source 255.255.255.2",
405 .u
.flag
= PEER_FLAG_UPDATE_SOURCE
,
406 .type
= PEER_AT_GLOBAL_FLAG
,
407 .handlers
[0] = TEST_HANDLER(update_source_su
),
410 .cmd
= "update-source",
411 .peer_cmd
= "update-source IF-PEER",
412 .group_cmd
= "update-source IF-GROUP",
413 .u
.flag
= PEER_FLAG_UPDATE_SOURCE
,
414 .type
= PEER_AT_GLOBAL_FLAG
,
415 .handlers
[0] = TEST_HANDLER(update_source_if
),
418 /* Address Family Attributes */
421 .peer_cmd
= "addpath-tx-all-paths",
422 .group_cmd
= "addpath-tx-bestpath-per-AS",
423 .type
= PEER_AT_AF_CUSTOM
,
424 .handlers
[0] = TEST_HANDLER(addpath_types
),
428 .peer_cmd
= "allowas-in 1",
429 .group_cmd
= "allowas-in 2",
430 .u
.flag
= PEER_FLAG_ALLOWAS_IN
,
431 .handlers
[0] = TEST_HANDLER(allowas_in
),
434 .cmd
= "allowas-in origin",
435 .u
.flag
= PEER_FLAG_ALLOWAS_IN_ORIGIN
,
438 .cmd
= "as-override",
439 .u
.flag
= PEER_FLAG_AS_OVERRIDE
,
442 .cmd
= "attribute-unchanged as-path",
443 .u
.flag
= PEER_FLAG_AS_PATH_UNCHANGED
,
446 .cmd
= "attribute-unchanged next-hop",
447 .u
.flag
= PEER_FLAG_NEXTHOP_UNCHANGED
,
450 .cmd
= "attribute-unchanged med",
451 .u
.flag
= PEER_FLAG_MED_UNCHANGED
,
454 .cmd
= "attribute-unchanged as-path next-hop",
455 .u
.flag
= PEER_FLAG_AS_PATH_UNCHANGED
456 | PEER_FLAG_NEXTHOP_UNCHANGED
,
459 .cmd
= "attribute-unchanged as-path med",
460 .u
.flag
= PEER_FLAG_AS_PATH_UNCHANGED
461 | PEER_FLAG_MED_UNCHANGED
,
464 .cmd
= "attribute-unchanged as-path next-hop med",
465 .u
.flag
= PEER_FLAG_AS_PATH_UNCHANGED
466 | PEER_FLAG_NEXTHOP_UNCHANGED
467 | PEER_FLAG_MED_UNCHANGED
,
470 .cmd
= "capability orf prefix-list send",
471 .u
.flag
= PEER_FLAG_ORF_PREFIX_SM
,
474 .cmd
= "capability orf prefix-list receive",
475 .u
.flag
= PEER_FLAG_ORF_PREFIX_RM
,
478 .cmd
= "capability orf prefix-list both",
479 .u
.flag
= PEER_FLAG_ORF_PREFIX_SM
| PEER_FLAG_ORF_PREFIX_RM
,
482 .cmd
= "default-originate",
483 .u
.flag
= PEER_FLAG_DEFAULT_ORIGINATE
,
486 .cmd
= "default-originate route-map",
487 .peer_cmd
= "default-originate route-map RM-PEER",
488 .group_cmd
= "default-originate route-map RM-GROUP",
489 .u
.flag
= PEER_FLAG_DEFAULT_ORIGINATE
,
490 .handlers
[0] = TEST_HANDLER(default_originate_route_map
),
493 .cmd
= "distribute-list",
494 .peer_cmd
= "distribute-list DL-PEER in",
495 .group_cmd
= "distribute-list DL-GROUP in",
496 .type
= PEER_AT_AF_FILTER
,
497 .u
.filter
.flag
= PEER_FT_DISTRIBUTE_LIST
,
498 .u
.filter
.direct
= FILTER_IN
,
499 .handlers
[0] = TEST_HANDLER(distribute_list
),
502 .cmd
= "distribute-list",
503 .peer_cmd
= "distribute-list DL-PEER out",
504 .group_cmd
= "distribute-list DL-GROUP out",
505 .type
= PEER_AT_AF_FILTER
,
506 .u
.filter
.flag
= PEER_FT_DISTRIBUTE_LIST
,
507 .u
.filter
.direct
= FILTER_OUT
,
508 .handlers
[0] = TEST_HANDLER(distribute_list
),
511 .cmd
= "filter-list",
512 .peer_cmd
= "filter-list FL-PEER in",
513 .group_cmd
= "filter-list FL-GROUP in",
514 .type
= PEER_AT_AF_FILTER
,
515 .u
.filter
.flag
= PEER_FT_FILTER_LIST
,
516 .u
.filter
.direct
= FILTER_IN
,
517 .handlers
[0] = TEST_HANDLER(filter_list
),
520 .cmd
= "filter-list",
521 .peer_cmd
= "filter-list FL-PEER out",
522 .group_cmd
= "filter-list FL-GROUP out",
523 .type
= PEER_AT_AF_FILTER
,
524 .u
.filter
.flag
= PEER_FT_FILTER_LIST
,
525 .u
.filter
.direct
= FILTER_OUT
,
526 .handlers
[0] = TEST_HANDLER(filter_list
),
529 .cmd
= "maximum-prefix",
530 .peer_cmd
= "maximum-prefix 10",
531 .group_cmd
= "maximum-prefix 20",
532 .u
.flag
= PEER_FLAG_MAX_PREFIX
,
533 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
536 .cmd
= "maximum-prefix",
537 .peer_cmd
= "maximum-prefix 10 restart 100",
538 .group_cmd
= "maximum-prefix 20 restart 200",
539 .u
.flag
= PEER_FLAG_MAX_PREFIX
,
540 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
541 .handlers
[1] = TEST_HANDLER(maximum_prefix_restart
),
544 .cmd
= "maximum-prefix",
545 .peer_cmd
= "maximum-prefix 10 1 restart 100",
546 .group_cmd
= "maximum-prefix 20 2 restart 200",
547 .u
.flag
= PEER_FLAG_MAX_PREFIX
,
548 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
549 .handlers
[1] = TEST_HANDLER(maximum_prefix_threshold
),
550 .handlers
[2] = TEST_HANDLER(maximum_prefix_restart
),
553 .cmd
= "maximum-prefix",
554 .peer_cmd
= "maximum-prefix 10 warning-only",
555 .group_cmd
= "maximum-prefix 20 warning-only",
556 .u
.flag
= PEER_FLAG_MAX_PREFIX
| PEER_FLAG_MAX_PREFIX_WARNING
,
557 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
560 .cmd
= "maximum-prefix",
561 .peer_cmd
= "maximum-prefix 10 1 warning-only",
562 .group_cmd
= "maximum-prefix 20 2 warning-only",
563 .u
.flag
= PEER_FLAG_MAX_PREFIX
| PEER_FLAG_MAX_PREFIX_WARNING
,
564 .handlers
[0] = TEST_HANDLER(maximum_prefix
),
565 .handlers
[1] = TEST_HANDLER(maximum_prefix_threshold
),
568 .cmd
= "next-hop-self",
569 .u
.flag
= PEER_FLAG_NEXTHOP_SELF
,
572 .cmd
= "next-hop-self force",
573 .u
.flag
= PEER_FLAG_FORCE_NEXTHOP_SELF
,
576 .cmd
= "prefix-list",
577 .peer_cmd
= "prefix-list PL-PEER in",
578 .group_cmd
= "prefix-list PL-GROUP in",
579 .type
= PEER_AT_AF_FILTER
,
580 .u
.filter
.flag
= PEER_FT_PREFIX_LIST
,
581 .u
.filter
.direct
= FILTER_IN
,
582 .handlers
[0] = TEST_HANDLER(prefix_list
),
585 .cmd
= "prefix-list",
586 .peer_cmd
= "prefix-list PL-PEER out",
587 .group_cmd
= "prefix-list PL-GROUP out",
588 .type
= PEER_AT_AF_FILTER
,
589 .u
.filter
.flag
= PEER_FT_PREFIX_LIST
,
590 .u
.filter
.direct
= FILTER_OUT
,
591 .handlers
[0] = TEST_HANDLER(prefix_list
),
594 .cmd
= "remove-private-AS",
595 .u
.flag
= PEER_FLAG_REMOVE_PRIVATE_AS
,
598 .cmd
= "remove-private-AS all",
599 .u
.flag
= PEER_FLAG_REMOVE_PRIVATE_AS
600 | PEER_FLAG_REMOVE_PRIVATE_AS_ALL
,
603 .cmd
= "remove-private-AS replace-AS",
604 .u
.flag
= PEER_FLAG_REMOVE_PRIVATE_AS
605 | PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
,
608 .cmd
= "remove-private-AS all replace-AS",
609 .u
.flag
= PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
,
613 .peer_cmd
= "route-map RM-PEER in",
614 .group_cmd
= "route-map RM-GROUP in",
615 .type
= PEER_AT_AF_FILTER
,
616 .u
.filter
.flag
= PEER_FT_ROUTE_MAP
,
617 .u
.filter
.direct
= FILTER_IN
,
618 .handlers
[0] = TEST_HANDLER(route_map
),
622 .peer_cmd
= "route-map RM-PEER out",
623 .group_cmd
= "route-map RM-GROUP out",
624 .type
= PEER_AT_AF_FILTER
,
625 .u
.filter
.flag
= PEER_FT_ROUTE_MAP
,
626 .u
.filter
.direct
= FILTER_OUT
,
627 .handlers
[0] = TEST_HANDLER(route_map
),
630 .cmd
= "route-reflector-client",
631 .u
.flag
= PEER_FLAG_REFLECTOR_CLIENT
,
633 .o
.skip_xfer_cases
= true,
636 .cmd
= "route-server-client",
637 .u
.flag
= PEER_FLAG_RSERVER_CLIENT
,
640 .cmd
= "send-community",
641 .u
.flag
= PEER_FLAG_SEND_COMMUNITY
,
642 .o
.invert_peer
= true,
643 .o
.invert_group
= true,
646 .cmd
= "send-community extended",
647 .u
.flag
= PEER_FLAG_SEND_EXT_COMMUNITY
,
648 .o
.invert_peer
= true,
649 .o
.invert_group
= true,
652 .cmd
= "send-community large",
653 .u
.flag
= PEER_FLAG_SEND_LARGE_COMMUNITY
,
654 .o
.invert_peer
= true,
655 .o
.invert_group
= true,
658 .cmd
= "soft-reconfiguration inbound",
659 .u
.flag
= PEER_FLAG_SOFT_RECONFIG
,
662 .cmd
= "unsuppress-map",
663 .peer_cmd
= "unsuppress-map UM-PEER",
664 .group_cmd
= "unsuppress-map UM-GROUP",
665 .type
= PEER_AT_AF_FILTER
,
666 .u
.filter
.flag
= PEER_FT_UNSUPPRESS_MAP
,
667 .u
.filter
.direct
= 0,
668 .handlers
[0] = TEST_HANDLER(unsuppress_map
),
672 .peer_cmd
= "weight 100",
673 .group_cmd
= "weight 200",
674 .u
.flag
= PEER_FLAG_WEIGHT
,
675 .handlers
[0] = TEST_HANDLER(weight
),
679 /* clang-format on */
681 static const char *str_from_afi(afi_t afi
)
689 return "<unknown AFI>";
693 static const char *str_from_safi(safi_t safi
)
701 return "<unknown SAFI>";
705 static const char *str_from_attr_type(enum test_peer_attr_type at
)
708 case PEER_AT_GLOBAL_FLAG
:
710 case PEER_AT_AF_FLAG
:
712 case PEER_AT_AF_FILTER
:
714 case PEER_AT_GLOBAL_CUSTOM
:
715 case PEER_AT_AF_CUSTOM
:
722 static bool is_attr_type_global(enum test_peer_attr_type at
)
724 return at
== PEER_AT_GLOBAL_FLAG
|| at
== PEER_AT_GLOBAL_CUSTOM
;
727 static void test_log(struct test
*test
, const char *fmt
, ...)
731 /* Skip logging if test instance has previously failed. */
732 if (test
->state
!= TEST_SUCCESS
)
735 /* Store formatted log message. */
737 listnode_add(test
->log
, str_vprintf(fmt
, ap
));
741 static void test_execute(struct test
*test
, const char *fmt
, ...)
748 /* Skip execution if test instance has previously failed. */
749 if (test
->state
!= TEST_SUCCESS
)
752 /* Format command string with variadic arguments. */
754 cmd
= str_vprintf(fmt
, ap
);
757 test
->state
= TEST_INTERNAL_ERROR
;
759 str_printf("could not format command string [%s]", fmt
);
763 /* Tokenize formatted command string. */
764 vline
= cmd_make_strvec(cmd
);
766 test
->state
= TEST_INTERNAL_ERROR
;
767 test
->error
= str_printf(
768 "tokenizing command string [%s] returned empty result",
770 XFREE(MTYPE_TMP
, cmd
);
775 /* Execute command (non-strict). */
776 ret
= cmd_execute_command(vline
, test
->vty
, NULL
, 0);
777 if (ret
!= CMD_SUCCESS
) {
778 test
->state
= TEST_COMMAND_ERROR
;
779 test
->error
= str_printf(
780 "execution of command [%s] has failed with code [%d]",
785 cmd_free_strvec(vline
);
786 XFREE(MTYPE_TMP
, cmd
);
789 static void test_config(struct test
*test
, const char *fmt
, bool invert
,
797 /* Skip execution if test instance has previously failed. */
798 if (test
->state
!= TEST_SUCCESS
)
801 /* Format matcher string with variadic arguments. */
803 matcher
= str_vprintf(fmt
, apc
);
806 test
->state
= TEST_INTERNAL_ERROR
;
808 str_printf("could not format matcher string [%s]", fmt
);
812 /* Fetch BGP configuration into buffer. */
813 bgp_config_write(test
->vty
);
814 config
= buffer_getstr(test
->vty
->obuf
);
815 buffer_reset(test
->vty
->obuf
);
817 /* Match config against matcher. */
818 matched
= !!strstr(config
, matcher
);
819 if (!matched
&& !invert
) {
820 test
->state
= TEST_CONFIG_ERROR
;
821 test
->error
= str_printf("expected config [%s] to be present",
823 } else if (matched
&& invert
) {
824 test
->state
= TEST_CONFIG_ERROR
;
825 test
->error
= str_printf("expected config [%s] to be absent",
829 /* Free memory and return. */
830 XFREE(MTYPE_TMP
, matcher
);
831 XFREE(MTYPE_TMP
, config
);
834 static void test_config_present(struct test
*test
, const char *fmt
, ...)
839 test_config(test
, fmt
, false, ap
);
843 static void test_config_absent(struct test
*test
, const char *fmt
, ...)
848 test_config(test
, fmt
, true, ap
);
852 static void test_initialize(struct test
*test
)
856 /* Skip execution if test instance has previously failed. */
857 if (test
->state
!= TEST_SUCCESS
)
860 /* Log message about (re)-initialization */
861 test_log(test
, "prepare: %sinitialize bgp test environment",
862 test
->bgp
? "re-" : "");
864 /* Attempt gracefully to purge previous BGP configuration. */
865 test_execute(test
, "no router bgp");
866 test
->state
= TEST_SUCCESS
;
868 /* Initialize BGP test environment. */
869 test_execute(test
, "router bgp %d", cfg
.local_asn
);
870 test_execute(test
, "no bgp default ipv4-unicast");
871 test_execute(test
, "neighbor %s peer-group", cfg
.peer_group
);
872 if (test
->o
.use_iface_peer
) {
873 test_execute(test
, "neighbor %s interface", cfg
.peer_interface
);
874 test_execute(test
, "neighbor %s remote-as %d",
876 test
->o
.use_ibgp
? cfg
.local_asn
: cfg
.peer_asn
);
878 test_execute(test
, "neighbor %s remote-as %d", cfg
.peer_address
,
879 test
->o
.use_ibgp
? cfg
.local_asn
: cfg
.peer_asn
);
882 if (test
->state
!= TEST_SUCCESS
)
885 /* Fetch default BGP instance. */
886 test
->bgp
= bgp_get_default();
888 test
->state
= TEST_INTERNAL_ERROR
;
890 str_printf("could not retrieve default bgp instance");
894 /* Fetch peer instance. */
895 if (test
->o
.use_iface_peer
) {
897 peer_lookup_by_conf_if(test
->bgp
, cfg
.peer_interface
);
899 str2sockunion(cfg
.peer_address
, &su
);
900 test
->peer
= peer_lookup(test
->bgp
, &su
);
903 test
->state
= TEST_INTERNAL_ERROR
;
904 test
->error
= str_printf(
905 "could not retrieve instance of bgp peer [%s]",
910 /* Fetch peer-group instance. */
911 test
->group
= peer_group_lookup(test
->bgp
, cfg
.peer_group
);
913 test
->state
= TEST_INTERNAL_ERROR
;
914 test
->error
= str_printf(
915 "could not retrieve instance of bgp peer-group [%s]",
921 static struct test
*test_new(const char *desc
, bool use_ibgp
,
926 test
= XCALLOC(MTYPE_TMP
, sizeof(struct test
));
927 test
->state
= TEST_SUCCESS
;
928 test
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
929 test
->log
= list_new();
930 test
->o
.use_ibgp
= use_ibgp
;
931 test
->o
.use_iface_peer
= use_iface_peer
;
933 test
->vty
= vty_new();
934 test
->vty
->type
= VTY_TERM
;
935 test
->vty
->node
= CONFIG_NODE
;
937 test_initialize(test
);
942 static void test_finish(struct test
*test
)
945 struct listnode
*node
, *nnode
;
947 /* Print test output header. */
948 printf("%s [test] %s\n",
949 (test
->state
== TEST_SUCCESS
) ? OUT_SYMBOL_OK
: OUT_SYMBOL_NOK
,
952 /* Print test log messages. */
953 for (ALL_LIST_ELEMENTS(test
->log
, node
, nnode
, msg
)) {
954 printf("%s %s\n", OUT_SYMBOL_INFO
, msg
);
955 XFREE(MTYPE_TMP
, msg
);
958 /* Print test error message if available. */
959 if (test
->state
!= TEST_SUCCESS
&& test
->error
)
960 printf("%s error: %s\n", OUT_SYMBOL_INFO
, test
->error
);
962 /* Print machine-readable result of test. */
963 printf("%s\n", test
->state
== TEST_SUCCESS
? "OK" : "failed");
965 /* Cleanup allocated memory. */
967 vty_close(test
->vty
);
971 list_delete(&test
->log
);
973 XFREE(MTYPE_TMP
, test
->desc
);
975 XFREE(MTYPE_TMP
, test
->error
);
976 XFREE(MTYPE_TMP
, test
);
979 static void test_peer_flags(struct test
*test
, struct test_peer_attr
*pa
,
980 struct peer
*peer
, bool exp_val
, bool exp_ovrd
)
982 bool exp_inv
, cur_val
, cur_ovrd
, cur_inv
;
984 /* Skip execution if test instance has previously failed. */
985 if (test
->state
!= TEST_SUCCESS
)
988 /* Detect if flag is meant to be inverted. */
989 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
990 exp_inv
= pa
->o
.invert_group
;
992 exp_inv
= pa
->o
.invert_peer
;
994 /* Flip expected value if flag is inverted. */
997 /* Fetch current state of value, override and invert flags. */
998 if (pa
->type
== PEER_AT_GLOBAL_FLAG
) {
999 cur_val
= !!CHECK_FLAG(peer
->flags
, pa
->u
.flag
);
1000 cur_ovrd
= !!CHECK_FLAG(peer
->flags_override
, pa
->u
.flag
);
1001 cur_inv
= !!CHECK_FLAG(peer
->flags_invert
, pa
->u
.flag
);
1002 } else /* if (pa->type == PEER_AT_AF_FLAG) */ {
1003 cur_val
= !!CHECK_FLAG(peer
->af_flags
[pa
->afi
][pa
->safi
],
1005 cur_ovrd
= !!CHECK_FLAG(
1006 peer
->af_flags_override
[pa
->afi
][pa
->safi
], pa
->u
.flag
);
1007 cur_inv
= !!CHECK_FLAG(peer
->af_flags_invert
[pa
->afi
][pa
->safi
],
1011 /* Assert expected flag states. */
1012 TEST_ASSERT_EQ(test
, cur_val
, exp_val
);
1013 TEST_ASSERT_EQ(test
, cur_ovrd
, exp_ovrd
);
1014 TEST_ASSERT_EQ(test
, cur_inv
, exp_inv
);
1017 static void test_af_filter(struct test
*test
, struct test_peer_attr
*pa
,
1018 struct peer
*peer
, bool exp_state
, bool exp_ovrd
)
1021 struct bgp_filter
*filter
;
1023 /* Skip execution if test instance has previously failed. */
1024 if (test
->state
!= TEST_SUCCESS
)
1027 /* Fetch and assert current state of override flag. */
1028 cur_ovrd
= !!CHECK_FLAG(
1029 peer
->filter_override
[pa
->afi
][pa
->safi
][pa
->u
.filter
.direct
],
1032 TEST_ASSERT_EQ(test
, cur_ovrd
, exp_ovrd
);
1034 /* Assert that map/list matches expected state (set/unset). */
1035 filter
= &peer
->filter
[pa
->afi
][pa
->safi
];
1037 switch (pa
->u
.filter
.flag
) {
1038 case PEER_FT_DISTRIBUTE_LIST
:
1039 TEST_ASSERT_EQ(test
,
1040 !!(filter
->dlist
[pa
->u
.filter
.direct
].name
),
1043 case PEER_FT_FILTER_LIST
:
1044 TEST_ASSERT_EQ(test
,
1045 !!(filter
->aslist
[pa
->u
.filter
.direct
].name
),
1048 case PEER_FT_PREFIX_LIST
:
1049 TEST_ASSERT_EQ(test
,
1050 !!(filter
->plist
[pa
->u
.filter
.direct
].name
),
1053 case PEER_FT_ROUTE_MAP
:
1054 TEST_ASSERT_EQ(test
, !!(filter
->map
[pa
->u
.filter
.direct
].name
),
1057 case PEER_FT_UNSUPPRESS_MAP
:
1058 TEST_ASSERT_EQ(test
, !!(filter
->usmap
.name
), exp_state
);
1063 static void test_custom(struct test
*test
, struct test_peer_attr
*pa
,
1064 struct peer
*peer
, struct peer
*group
, bool peer_set
,
1068 char *handler_error
;
1070 for (i
= 0; i
< TEST_HANDLER_MAX
; i
++) {
1071 /* Skip execution if test instance has previously failed. */
1072 if (test
->state
!= TEST_SUCCESS
)
1075 /* Skip further execution if handler is undefined. */
1076 if (!pa
->handlers
[i
])
1079 /* Execute custom handler. */
1080 pa
->handlers
[i
](test
, pa
, peer
, group
, peer_set
, group_set
);
1081 if (test
->state
!= TEST_SUCCESS
) {
1082 test
->state
= TEST_CUSTOM_ERROR
;
1083 handler_error
= test
->error
;
1084 test
->error
= str_printf("custom handler failed: %s",
1086 XFREE(MTYPE_TMP
, handler_error
);
1092 static void test_process(struct test
*test
, struct test_peer_attr
*pa
,
1093 struct peer
*peer
, struct peer
*group
, bool peer_set
,
1097 case PEER_AT_GLOBAL_FLAG
:
1098 case PEER_AT_AF_FLAG
:
1101 peer_set
|| (peer_group_active(peer
) && group_set
),
1103 test_peer_flags(test
, pa
, group
, group_set
, false);
1106 case PEER_AT_AF_FILTER
:
1109 peer_set
|| (peer_group_active(peer
) && group_set
),
1111 test_af_filter(test
, pa
, group
, group_set
, false);
1114 case PEER_AT_GLOBAL_CUSTOM
:
1115 case PEER_AT_AF_CUSTOM
:
1117 * Do nothing here - a custom handler can be executed, but this
1118 * is not required. This will allow defining peer attributes
1119 * which shall not be checked for flag/filter/other internal
1125 test
->state
= TEST_INTERNAL_ERROR
;
1127 str_printf("invalid attribute type: %d", pa
->type
);
1131 /* Attempt to call a custom handler if set for further processing. */
1132 test_custom(test
, pa
, peer
, group
, peer_set
, group_set
);
1135 static void test_peer_attr(struct test
*test
, struct test_peer_attr
*pa
)
1139 const char *ecp
= pa
->o
.invert_peer
? "no " : "";
1140 const char *dcp
= pa
->o
.invert_peer
? "" : "no ";
1141 const char *ecg
= pa
->o
.invert_group
? "no " : "";
1142 const char *dcg
= pa
->o
.invert_group
? "" : "no ";
1143 const char *peer_cmd
= pa
->peer_cmd
?: pa
->cmd
;
1144 const char *group_cmd
= pa
->group_cmd
?: pa
->cmd
;
1145 struct peer
*p
= test
->peer
;
1146 struct peer_group
*g
= test
->group
;
1148 /* Determine type and if test is address-family relevant */
1149 type
= str_from_attr_type(pa
->type
);
1151 test
->state
= TEST_INTERNAL_ERROR
;
1153 str_printf("invalid attribute type: %d", pa
->type
);
1158 * =====================================================================
1159 * Test Case Suite 1: Config persistence after adding peer to group
1161 * Example: If a peer attribute has value [1] and a group attribute has
1162 * value [2], the peer attribute value should be persisted when the peer
1163 * gets added to the peer-group.
1165 * This test suite is meant to test the group2peer functions which can
1166 * be found inside bgpd/bgpd.c, which are related to initial peer-group
1168 * =====================================================================
1171 /* Test Preparation: Switch and activate address-family. */
1172 if (!is_attr_type_global(pa
->type
)) {
1173 test_log(test
, "prepare: switch address-family to [%s]",
1174 get_afi_safi_str(pa
->afi
, pa
->safi
, false));
1175 test_execute(test
, "address-family %s %s",
1176 str_from_afi(pa
->afi
), str_from_safi(pa
->safi
));
1177 test_execute(test
, "neighbor %s activate", g
->name
);
1178 test_execute(test
, "neighbor %s activate", p
->host
);
1181 /* Skip peer-group to peer transfer test cases if requested. */
1182 if (pa
->o
.skip_xfer_cases
&& test
->state
== TEST_SUCCESS
)
1183 test
->state
= TEST_SKIPPING
;
1185 /* Test Case: Set flag on BGP peer. */
1186 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, peer_cmd
,
1188 test_execute(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1189 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1190 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1191 test_process(test
, pa
, p
, g
->conf
, true, false);
1193 /* Test Case: Set flag on BGP peer-group. */
1194 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, group_cmd
,
1196 test_execute(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1197 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1198 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1199 test_process(test
, pa
, p
, g
->conf
, true, true);
1201 /* Test Case: Add BGP peer to peer-group. */
1202 test_log(test
, "case %02d: add peer [%s] to group [%s]", tc
++, p
->host
,
1204 test_execute(test
, "neighbor %s peer-group %s", p
->host
, g
->name
);
1205 test_config_present(test
, "neighbor %s %speer-group %s", p
->host
,
1206 p
->conf_if
? "interface " : "", g
->name
);
1207 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1208 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1209 test_process(test
, pa
, p
, g
->conf
, true, true);
1211 /* Test Case: Unset flag on BGP peer-group. */
1212 test_log(test
, "case %02d: unset %s [%s] on [%s]", tc
++, type
,
1213 group_cmd
, g
->name
);
1214 test_execute(test
, "%sneighbor %s %s", dcg
, g
->name
, group_cmd
);
1215 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1216 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1217 test_process(test
, pa
, p
, g
->conf
, true, false);
1220 * =====================================================================
1221 * Test Case Suite 2: Config inheritance after adding peer to group
1223 * Example: If a peer attribute has not been set and a group attribute
1224 * has a value of [2], the group attribute should be inherited to the
1225 * peer without flagging the newly set value as overridden.
1227 * This test suite is meant to test the group2peer functions which can
1228 * be found inside bgpd/bgpd.c, which are related to initial peer-group
1230 * =====================================================================
1233 /* Test Preparation: Re-initialize test environment. */
1234 test_initialize(test
);
1238 /* Test Preparation: Switch and activate address-family. */
1239 if (!is_attr_type_global(pa
->type
)) {
1240 test_log(test
, "prepare: switch address-family to [%s]",
1241 get_afi_safi_str(pa
->afi
, pa
->safi
, false));
1242 test_execute(test
, "address-family %s %s",
1243 str_from_afi(pa
->afi
), str_from_safi(pa
->safi
));
1244 test_execute(test
, "neighbor %s activate", g
->name
);
1245 test_execute(test
, "neighbor %s activate", p
->host
);
1248 /* Test Case: Set flag on BGP peer-group. */
1249 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, group_cmd
,
1251 test_execute(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1252 test_config_absent(test
, "neighbor %s %s", p
->host
, pa
->cmd
);
1253 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1254 test_process(test
, pa
, p
, g
->conf
, false, true);
1256 /* Test Case: Add BGP peer to peer-group. */
1257 test_log(test
, "case %02d: add peer [%s] to group [%s]", tc
++, p
->host
,
1259 test_execute(test
, "neighbor %s peer-group %s", p
->host
, g
->name
);
1260 test_config_present(test
, "neighbor %s %speer-group %s", p
->host
,
1261 p
->conf_if
? "interface " : "", g
->name
);
1262 test_config_absent(test
, "neighbor %s %s", p
->host
, pa
->cmd
);
1263 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1264 test_process(test
, pa
, p
, g
->conf
, false, true);
1266 /* Stop skipping test cases if previously enabled. */
1267 if (pa
->o
.skip_xfer_cases
&& test
->state
== TEST_SKIPPING
)
1268 test
->state
= TEST_SUCCESS
;
1271 * =====================================================================
1272 * Test Case Suite 3: Miscellaneous flag checks
1274 * This test suite does not focus on initial peer-group inheritance and
1275 * instead executes various different commands to set/unset attributes
1276 * on both peer- and group-level. These checks should always be executed
1278 * =====================================================================
1281 /* Test Preparation: Re-initialize test environment. */
1282 test_initialize(test
);
1286 /* Test Preparation: Switch and activate address-family. */
1287 if (!is_attr_type_global(pa
->type
)) {
1288 test_log(test
, "prepare: switch address-family to [%s]",
1289 get_afi_safi_str(pa
->afi
, pa
->safi
, false));
1290 test_execute(test
, "address-family %s %s",
1291 str_from_afi(pa
->afi
), str_from_safi(pa
->safi
));
1292 test_execute(test
, "neighbor %s activate", g
->name
);
1293 test_execute(test
, "neighbor %s activate", p
->host
);
1296 /* Test Case: Set flag on BGP peer. */
1297 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, peer_cmd
,
1299 test_execute(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1300 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1301 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1302 test_process(test
, pa
, p
, g
->conf
, true, false);
1304 /* Test Case: Add BGP peer to peer-group. */
1305 test_log(test
, "case %02d: add peer [%s] to group [%s]", tc
++, p
->host
,
1307 test_execute(test
, "neighbor %s peer-group %s", p
->host
, g
->name
);
1308 test_config_present(test
, "neighbor %s %speer-group %s", p
->host
,
1309 p
->conf_if
? "interface " : "", g
->name
);
1310 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1311 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1312 test_process(test
, pa
, p
, g
->conf
, true, false);
1314 /* Test Case: Re-add BGP peer to peer-group. */
1315 test_log(test
, "case %02d: re-add peer [%s] to group [%s]", tc
++,
1317 test_execute(test
, "neighbor %s peer-group %s", p
->host
, g
->name
);
1318 test_config_present(test
, "neighbor %s %speer-group %s", p
->host
,
1319 p
->conf_if
? "interface " : "", g
->name
);
1320 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1321 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1322 test_process(test
, pa
, p
, g
->conf
, true, false);
1324 /* Test Case: Set flag on BGP peer-group. */
1325 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, group_cmd
,
1327 test_execute(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1328 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1329 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1330 test_process(test
, pa
, p
, g
->conf
, true, true);
1332 /* Test Case: Unset flag on BGP peer-group. */
1333 test_log(test
, "case %02d: unset %s [%s] on [%s]", tc
++, type
,
1334 group_cmd
, g
->name
);
1335 test_execute(test
, "%sneighbor %s %s", dcg
, g
->name
, group_cmd
);
1336 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1337 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1338 test_process(test
, pa
, p
, g
->conf
, true, false);
1340 /* Test Case: Set flag on BGP peer-group. */
1341 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, group_cmd
,
1343 test_execute(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1344 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1345 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1346 test_process(test
, pa
, p
, g
->conf
, true, true);
1348 /* Test Case: Re-set flag on BGP peer. */
1349 test_log(test
, "case %02d: re-set %s [%s] on [%s]", tc
++, type
,
1351 test_execute(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1352 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1353 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1354 test_process(test
, pa
, p
, g
->conf
, true, true);
1356 /* Test Case: Unset flag on BGP peer. */
1357 test_log(test
, "case %02d: unset %s [%s] on [%s]", tc
++, type
, peer_cmd
,
1359 test_execute(test
, "%sneighbor %s %s", dcp
, p
->host
, peer_cmd
);
1360 test_config_absent(test
, "neighbor %s %s", p
->host
, pa
->cmd
);
1361 test_config_present(test
, "%sneighbor %s %s", ecg
, g
->name
, group_cmd
);
1362 test_process(test
, pa
, p
, g
->conf
, false, true);
1364 /* Test Case: Unset flag on BGP peer-group. */
1365 test_log(test
, "case %02d: unset %s [%s] on [%s]", tc
++, type
,
1366 group_cmd
, g
->name
);
1367 test_execute(test
, "%sneighbor %s %s", dcg
, g
->name
, group_cmd
);
1368 test_config_absent(test
, "neighbor %s %s", p
->host
, pa
->cmd
);
1369 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1370 test_process(test
, pa
, p
, g
->conf
, false, false);
1372 /* Test Case: Set flag on BGP peer. */
1373 test_log(test
, "case %02d: set %s [%s] on [%s]", tc
++, type
, peer_cmd
,
1375 test_execute(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1376 test_config_present(test
, "%sneighbor %s %s", ecp
, p
->host
, peer_cmd
);
1377 test_config_absent(test
, "neighbor %s %s", g
->name
, pa
->cmd
);
1378 test_process(test
, pa
, p
, g
->conf
, true, false);
1381 static void bgp_startup(void)
1384 zlog_aux_init("NONE: ", LOG_DEBUG
);
1385 zprivs_preinit(&bgpd_privs
);
1386 zprivs_init(&bgpd_privs
);
1388 master
= thread_master_create(NULL
);
1389 nb_init(master
, NULL
, 0, false);
1390 bgp_master_init(master
, BGP_SOCKET_SNDBUF_SIZE
, list_new());
1391 bgp_option_set(BGP_OPT_NO_LISTEN
);
1392 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
);
1444 struct list
*pa_list
;
1445 struct test_peer_attr
*pa
, *pac
;
1446 struct listnode
*node
, *nnode
;
1450 pa_list
= list_new();
1452 while (test_peer_attrs
[i
].cmd
) {
1453 pa
= &test_peer_attrs
[i
++];
1455 /* Just copy the peer attribute structure for global flags. */
1456 if (is_attr_type_global(pa
->type
)) {
1457 pac
= XMALLOC(MTYPE_TMP
, sizeof(struct test_peer_attr
));
1458 memcpy(pac
, pa
, sizeof(struct test_peer_attr
));
1459 listnode_add(pa_list
, pac
);
1463 /* Fallback to default families if not specified. */
1464 if (!pa
->families
[0].afi
&& !pa
->families
[0].safi
)
1465 memcpy(&pa
->families
, test_default_families
,
1466 sizeof(test_default_families
));
1468 /* Add peer attribute definition for each address family. */
1470 while (pa
->families
[ii
].afi
&& pa
->families
[ii
].safi
) {
1471 pac
= XMALLOC(MTYPE_TMP
, sizeof(struct test_peer_attr
));
1472 memcpy(pac
, pa
, sizeof(struct test_peer_attr
));
1474 pac
->afi
= pa
->families
[ii
].afi
;
1475 pac
->safi
= pa
->families
[ii
].safi
;
1476 listnode_add(pa_list
, pac
);
1482 for (ALL_LIST_ELEMENTS(pa_list
, node
, nnode
, pa
)) {
1486 /* Build test description string. */
1487 if (pa
->afi
&& pa
->safi
)
1488 desc
= str_printf("peer\\%s-%s\\%s",
1489 str_from_afi(pa
->afi
),
1490 str_from_safi(pa
->safi
), pa
->cmd
);
1492 desc
= str_printf("peer\\%s", pa
->cmd
);
1494 /* Initialize new test instance. */
1495 test
= test_new(desc
, pa
->o
.use_ibgp
, pa
->o
.use_iface_peer
);
1496 XFREE(MTYPE_TMP
, desc
);
1498 /* Execute tests and finish test instance. */
1499 test_peer_attr(test
, pa
);
1502 /* Print empty line as spacer. */
1505 /* Free memory used for peer-attr declaration. */
1506 XFREE(MTYPE_TMP
, pa
);
1509 list_delete(&pa_list
);