diff options
-rw-r--r-- | tests/ecommunity_test.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/tests/ecommunity_test.c b/tests/ecommunity_test.c new file mode 100644 index 00000000..418f659f --- /dev/null +++ b/tests/ecommunity_test.c @@ -0,0 +1,141 @@ +#include <zebra.h> + +#include "vty.h" +#include "stream.h" +#include "privs.h" +#include "memory.h" + +#include "bgpd/bgpd.h" +#include "bgpd/bgp_ecommunity.h" + +/* need these to link in libbgp */ +struct zebra_privs_t *bgpd_privs = NULL; +struct thread_master *master = NULL; + +static int failed = 0; + +/* specification for a test - what the results should be */ +struct test_spec +{ + const char *shouldbe; /* the string the path should parse to */ +}; + + +/* test segments to parse and validate, and use for other tests */ +static struct test_segment { + const char *name; + const char *desc; + const u_char data[1024]; + int len; + struct test_spec sp; +} test_segments [] = +{ + { /* 0 */ + "ipaddr", + "rt 1.2.3.4:257", + { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_ROUTE_TARGET, + 0x1,0x2,0x3,0x4, 0x1,0x1 }, + 8, + { "rt 1.2.3.4:257" } + }, + { /* 1 */ + "ipaddr-so", + "soo 1.2.3.4:257", + { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_SITE_ORIGIN, + 0x1,0x2,0x3,0x4, 0x1,0x1}, + 8, + { "soo 1.2.3.4:257" } + }, + { /* 2 */ + "asn", + "rt 23456:987654321", + { ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN, + 0x5b,0xa0, 0x3a,0xde,0x68,0xb1 }, + 8, + { "soo 23456:987654321" } + }, + { /* 3 */ + "asn4", + "rt 168450976:4321", + { ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN, + 0xa,0xa,0x5b,0xa0, 0x10,0xe1 }, + 8, + { "soo 168450976:4321" } + }, + { NULL, NULL, {0}, 0, { NULL } } +}; + + +/* validate the given aspath */ +static int +validate (struct ecommunity *ecom, const struct test_spec *sp) +{ + int fails = 0; + struct ecommunity *etmp; + char *str1, *str2; + + printf ("got:\n %s\n", ecommunity_str (ecom)); + str1 = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST); + etmp = ecommunity_str2com (str1, 0, 1); + if (etmp) + str2 = ecommunity_ecom2str (etmp, ECOMMUNITY_FORMAT_COMMUNITY_LIST); + else + str2 = NULL; + + if (strcmp (sp->shouldbe, str1)) + { + failed++; + fails++; + printf ("shouldbe: %s\n%s\n", str1, sp->shouldbe); + } + if (!etmp || strcmp (str1, str2)) + { + failed++; + fails++; + printf ("dogfood: in %s\n" + " in->out %s\n", + str1, + (etmp && str2) ? str2 : "NULL"); + } + ecommunity_free (etmp); + XFREE (MTYPE_ECOMMUNITY_STR, str1); + XFREE (MTYPE_ECOMMUNITY_STR, str2); + + return fails; +} + +/* basic parsing test */ +static void +parse_test (struct test_segment *t) +{ + struct ecommunity *ecom; + + printf ("%s: %s\n", t->name, t->desc); + + ecom = ecommunity_parse (t->data, t->len); + + printf ("ecom: %s\nvalidating...:\n", ecommunity_str (ecom)); + + if (!validate (ecom, &t->sp)) + printf ("OK\n"); + else + printf ("failed\n"); + + printf ("\n"); + ecommunity_unintern (ecom); +} + + +int +main (void) +{ + int i = 0; + ecommunity_init(); + while (test_segments[i].name) + parse_test (&test_segments[i++]); + + printf ("failures: %d\n", failed); + //printf ("aspath count: %ld\n", aspath_count()); + return failed; + //return (failed + aspath_count()); +} |