Paul Jakma | 010ebbb | 2014-09-16 11:53:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Sun Microsystems, Inc. |
| 3 | * |
| 4 | * This file is part of Quagga. |
| 5 | * |
| 6 | * Quagga is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2, or (at your option) any |
| 9 | * later version. |
| 10 | * |
| 11 | * Quagga is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with Quagga; see the file COPYING. If not, write to the Free |
| 18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
Paul Jakma | 5b5bfb8 | 2007-10-18 16:33:08 +0000 | [diff] [blame] | 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "vty.h" |
| 24 | #include "stream.h" |
| 25 | #include "privs.h" |
| 26 | #include "memory.h" |
Donald Sharp | 0490729 | 2016-01-07 10:03:01 -0500 | [diff] [blame^] | 27 | #include "filter.h" |
Paul Jakma | 5b5bfb8 | 2007-10-18 16:33:08 +0000 | [diff] [blame] | 28 | |
| 29 | #include "bgpd/bgpd.h" |
| 30 | #include "bgpd/bgp_ecommunity.h" |
| 31 | |
| 32 | /* need these to link in libbgp */ |
| 33 | struct zebra_privs_t *bgpd_privs = NULL; |
| 34 | struct thread_master *master = NULL; |
| 35 | |
| 36 | static int failed = 0; |
| 37 | |
| 38 | /* specification for a test - what the results should be */ |
| 39 | struct test_spec |
| 40 | { |
| 41 | const char *shouldbe; /* the string the path should parse to */ |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | /* test segments to parse and validate, and use for other tests */ |
| 46 | static struct test_segment { |
| 47 | const char *name; |
| 48 | const char *desc; |
Paul Jakma | 1dba254 | 2012-05-01 16:20:33 +0100 | [diff] [blame] | 49 | const u_int8_t data[1024]; |
Paul Jakma | 5b5bfb8 | 2007-10-18 16:33:08 +0000 | [diff] [blame] | 50 | int len; |
| 51 | struct test_spec sp; |
| 52 | } test_segments [] = |
| 53 | { |
| 54 | { /* 0 */ |
| 55 | "ipaddr", |
| 56 | "rt 1.2.3.4:257", |
| 57 | { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_ROUTE_TARGET, |
| 58 | 0x1,0x2,0x3,0x4, 0x1,0x1 }, |
| 59 | 8, |
| 60 | { "rt 1.2.3.4:257" } |
| 61 | }, |
| 62 | { /* 1 */ |
| 63 | "ipaddr-so", |
| 64 | "soo 1.2.3.4:257", |
| 65 | { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_SITE_ORIGIN, |
| 66 | 0x1,0x2,0x3,0x4, 0x1,0x1}, |
| 67 | 8, |
| 68 | { "soo 1.2.3.4:257" } |
| 69 | }, |
| 70 | { /* 2 */ |
| 71 | "asn", |
| 72 | "rt 23456:987654321", |
| 73 | { ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN, |
| 74 | 0x5b,0xa0, 0x3a,0xde,0x68,0xb1 }, |
| 75 | 8, |
| 76 | { "soo 23456:987654321" } |
| 77 | }, |
| 78 | { /* 3 */ |
| 79 | "asn4", |
| 80 | "rt 168450976:4321", |
| 81 | { ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN, |
| 82 | 0xa,0xa,0x5b,0xa0, 0x10,0xe1 }, |
| 83 | 8, |
| 84 | { "soo 168450976:4321" } |
| 85 | }, |
| 86 | { NULL, NULL, {0}, 0, { NULL } } |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | /* validate the given aspath */ |
| 91 | static int |
| 92 | validate (struct ecommunity *ecom, const struct test_spec *sp) |
| 93 | { |
| 94 | int fails = 0; |
| 95 | struct ecommunity *etmp; |
| 96 | char *str1, *str2; |
| 97 | |
| 98 | printf ("got:\n %s\n", ecommunity_str (ecom)); |
| 99 | str1 = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST); |
| 100 | etmp = ecommunity_str2com (str1, 0, 1); |
| 101 | if (etmp) |
| 102 | str2 = ecommunity_ecom2str (etmp, ECOMMUNITY_FORMAT_COMMUNITY_LIST); |
| 103 | else |
| 104 | str2 = NULL; |
| 105 | |
| 106 | if (strcmp (sp->shouldbe, str1)) |
| 107 | { |
| 108 | failed++; |
| 109 | fails++; |
| 110 | printf ("shouldbe: %s\n%s\n", str1, sp->shouldbe); |
| 111 | } |
| 112 | if (!etmp || strcmp (str1, str2)) |
| 113 | { |
| 114 | failed++; |
| 115 | fails++; |
| 116 | printf ("dogfood: in %s\n" |
| 117 | " in->out %s\n", |
| 118 | str1, |
| 119 | (etmp && str2) ? str2 : "NULL"); |
| 120 | } |
Paul Jakma | 1dba254 | 2012-05-01 16:20:33 +0100 | [diff] [blame] | 121 | ecommunity_free (&etmp); |
Paul Jakma | 5b5bfb8 | 2007-10-18 16:33:08 +0000 | [diff] [blame] | 122 | XFREE (MTYPE_ECOMMUNITY_STR, str1); |
| 123 | XFREE (MTYPE_ECOMMUNITY_STR, str2); |
| 124 | |
| 125 | return fails; |
| 126 | } |
| 127 | |
| 128 | /* basic parsing test */ |
| 129 | static void |
| 130 | parse_test (struct test_segment *t) |
| 131 | { |
| 132 | struct ecommunity *ecom; |
| 133 | |
| 134 | printf ("%s: %s\n", t->name, t->desc); |
| 135 | |
David Lamparter | c313895 | 2015-04-21 10:02:23 +0200 | [diff] [blame] | 136 | ecom = ecommunity_parse ((u_int8_t *)t->data, t->len); |
Paul Jakma | 5b5bfb8 | 2007-10-18 16:33:08 +0000 | [diff] [blame] | 137 | |
| 138 | printf ("ecom: %s\nvalidating...:\n", ecommunity_str (ecom)); |
| 139 | |
| 140 | if (!validate (ecom, &t->sp)) |
| 141 | printf ("OK\n"); |
| 142 | else |
| 143 | printf ("failed\n"); |
| 144 | |
| 145 | printf ("\n"); |
Paul Jakma | 1dba254 | 2012-05-01 16:20:33 +0100 | [diff] [blame] | 146 | ecommunity_unintern (&ecom); |
Paul Jakma | 5b5bfb8 | 2007-10-18 16:33:08 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | int |
| 151 | main (void) |
| 152 | { |
| 153 | int i = 0; |
| 154 | ecommunity_init(); |
| 155 | while (test_segments[i].name) |
| 156 | parse_test (&test_segments[i++]); |
| 157 | |
| 158 | printf ("failures: %d\n", failed); |
| 159 | //printf ("aspath count: %ld\n", aspath_count()); |
| 160 | return failed; |
| 161 | //return (failed + aspath_count()); |
| 162 | } |