1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2007 Sun Microsystems, Inc.
14 #include "bgpd/bgpd.h"
15 #include "bgpd/bgp_ecommunity.h"
17 /* need these to link in libbgp */
18 struct zebra_privs_t bgpd_privs
= {};
19 struct thread_master
*master
= NULL
;
21 static int failed
= 0;
23 /* specification for a test - what the results should be */
25 const char *shouldbe
; /* the string the path should parse to */
29 /* test segments to parse and validate, and use for other tests */
30 static struct test_segment
{
33 const uint8_t data
[1024];
36 } test_segments
[] = {{/* 0 */
39 {ECOMMUNITY_ENCODE_IP
, ECOMMUNITY_ROUTE_TARGET
, 0x1, 0x2,
46 {ECOMMUNITY_ENCODE_IP
, ECOMMUNITY_SITE_ORIGIN
, 0x1, 0x2,
53 {ECOMMUNITY_ENCODE_AS
, ECOMMUNITY_SITE_ORIGIN
, 0x5b, 0xa0,
54 0x3a, 0xde, 0x68, 0xb1},
56 {"soo 23456:987654321"}},
60 {ECOMMUNITY_ENCODE_AS4
, ECOMMUNITY_SITE_ORIGIN
, 0xa, 0xa,
61 0x5b, 0xa0, 0x10, 0xe1},
63 {"soo 168450976:4321"}},
64 {NULL
, NULL
, {0}, 0, {NULL
}}};
67 /* validate the given aspath */
68 static int validate(struct ecommunity
*ecom
, const struct test_spec
*sp
)
71 struct ecommunity
*etmp
;
74 printf("got:\n %s\n", ecommunity_str(ecom
));
75 str1
= ecommunity_ecom2str(ecom
, ECOMMUNITY_FORMAT_COMMUNITY_LIST
, 0);
76 etmp
= ecommunity_str2com(str1
, 0, 1);
78 str2
= ecommunity_ecom2str(etmp
,
79 ECOMMUNITY_FORMAT_COMMUNITY_LIST
, 0);
83 if (strcmp(sp
->shouldbe
, str1
)) {
86 printf("shouldbe: %s\n%s\n", str1
, sp
->shouldbe
);
88 if (!etmp
|| strcmp(str1
, str2
)) {
91 printf("dogfood: in %s\n"
93 str1
, (etmp
&& str2
) ? str2
: "NULL");
95 ecommunity_free(&etmp
);
96 XFREE(MTYPE_ECOMMUNITY_STR
, str1
);
97 XFREE(MTYPE_ECOMMUNITY_STR
, str2
);
102 /* basic parsing test */
103 static void parse_test(struct test_segment
*t
)
105 struct ecommunity
*ecom
;
107 printf("%s: %s\n", t
->name
, t
->desc
);
109 ecom
= ecommunity_parse((uint8_t *)t
->data
, t
->len
, 0);
111 printf("ecom: %s\nvalidating...:\n", ecommunity_str(ecom
));
113 if (!validate(ecom
, &t
->sp
))
119 ecommunity_unintern(&ecom
);
127 while (test_segments
[i
].name
)
128 parse_test(&test_segments
[i
++]);
130 printf("failures: %d\n", failed
);
131 // printf ("aspath count: %ld\n", aspath_count());
133 // return (failed + aspath_count());