Paul Jakma | 5b5bfb8 | 2007-10-18 16:33:08 +0000 | [diff] [blame] | 1 | #include <zebra.h> |
| 2 | |
| 3 | #include "vty.h" |
| 4 | #include "stream.h" |
| 5 | #include "privs.h" |
| 6 | #include "memory.h" |
| 7 | |
| 8 | #include "bgpd/bgpd.h" |
| 9 | #include "bgpd/bgp_ecommunity.h" |
| 10 | |
| 11 | /* need these to link in libbgp */ |
| 12 | struct zebra_privs_t *bgpd_privs = NULL; |
| 13 | struct thread_master *master = NULL; |
| 14 | |
| 15 | static int failed = 0; |
| 16 | |
| 17 | /* specification for a test - what the results should be */ |
| 18 | struct test_spec |
| 19 | { |
| 20 | const char *shouldbe; /* the string the path should parse to */ |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | /* test segments to parse and validate, and use for other tests */ |
| 25 | static struct test_segment { |
| 26 | const char *name; |
| 27 | const char *desc; |
| 28 | const u_char data[1024]; |
| 29 | int len; |
| 30 | struct test_spec sp; |
| 31 | } test_segments [] = |
| 32 | { |
| 33 | { /* 0 */ |
| 34 | "ipaddr", |
| 35 | "rt 1.2.3.4:257", |
| 36 | { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_ROUTE_TARGET, |
| 37 | 0x1,0x2,0x3,0x4, 0x1,0x1 }, |
| 38 | 8, |
| 39 | { "rt 1.2.3.4:257" } |
| 40 | }, |
| 41 | { /* 1 */ |
| 42 | "ipaddr-so", |
| 43 | "soo 1.2.3.4:257", |
| 44 | { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_SITE_ORIGIN, |
| 45 | 0x1,0x2,0x3,0x4, 0x1,0x1}, |
| 46 | 8, |
| 47 | { "soo 1.2.3.4:257" } |
| 48 | }, |
| 49 | { /* 2 */ |
| 50 | "asn", |
| 51 | "rt 23456:987654321", |
| 52 | { ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN, |
| 53 | 0x5b,0xa0, 0x3a,0xde,0x68,0xb1 }, |
| 54 | 8, |
| 55 | { "soo 23456:987654321" } |
| 56 | }, |
| 57 | { /* 3 */ |
| 58 | "asn4", |
| 59 | "rt 168450976:4321", |
| 60 | { ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN, |
| 61 | 0xa,0xa,0x5b,0xa0, 0x10,0xe1 }, |
| 62 | 8, |
| 63 | { "soo 168450976:4321" } |
| 64 | }, |
| 65 | { NULL, NULL, {0}, 0, { NULL } } |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | /* validate the given aspath */ |
| 70 | static int |
| 71 | validate (struct ecommunity *ecom, const struct test_spec *sp) |
| 72 | { |
| 73 | int fails = 0; |
| 74 | struct ecommunity *etmp; |
| 75 | char *str1, *str2; |
| 76 | |
| 77 | printf ("got:\n %s\n", ecommunity_str (ecom)); |
| 78 | str1 = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST); |
| 79 | etmp = ecommunity_str2com (str1, 0, 1); |
| 80 | if (etmp) |
| 81 | str2 = ecommunity_ecom2str (etmp, ECOMMUNITY_FORMAT_COMMUNITY_LIST); |
| 82 | else |
| 83 | str2 = NULL; |
| 84 | |
| 85 | if (strcmp (sp->shouldbe, str1)) |
| 86 | { |
| 87 | failed++; |
| 88 | fails++; |
| 89 | printf ("shouldbe: %s\n%s\n", str1, sp->shouldbe); |
| 90 | } |
| 91 | if (!etmp || strcmp (str1, str2)) |
| 92 | { |
| 93 | failed++; |
| 94 | fails++; |
| 95 | printf ("dogfood: in %s\n" |
| 96 | " in->out %s\n", |
| 97 | str1, |
| 98 | (etmp && str2) ? str2 : "NULL"); |
| 99 | } |
| 100 | ecommunity_free (etmp); |
| 101 | XFREE (MTYPE_ECOMMUNITY_STR, str1); |
| 102 | XFREE (MTYPE_ECOMMUNITY_STR, str2); |
| 103 | |
| 104 | return fails; |
| 105 | } |
| 106 | |
| 107 | /* basic parsing test */ |
| 108 | static void |
| 109 | parse_test (struct test_segment *t) |
| 110 | { |
| 111 | struct ecommunity *ecom; |
| 112 | |
| 113 | printf ("%s: %s\n", t->name, t->desc); |
| 114 | |
| 115 | ecom = ecommunity_parse (t->data, t->len); |
| 116 | |
| 117 | printf ("ecom: %s\nvalidating...:\n", ecommunity_str (ecom)); |
| 118 | |
| 119 | if (!validate (ecom, &t->sp)) |
| 120 | printf ("OK\n"); |
| 121 | else |
| 122 | printf ("failed\n"); |
| 123 | |
| 124 | printf ("\n"); |
| 125 | ecommunity_unintern (ecom); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | int |
| 130 | main (void) |
| 131 | { |
| 132 | int i = 0; |
| 133 | ecommunity_init(); |
| 134 | while (test_segments[i].name) |
| 135 | parse_test (&test_segments[i++]); |
| 136 | |
| 137 | printf ("failures: %d\n", failed); |
| 138 | //printf ("aspath count: %ld\n", aspath_count()); |
| 139 | return failed; |
| 140 | //return (failed + aspath_count()); |
| 141 | } |