]>
Commit | Line | Data |
---|---|---|
d62a17ae | 1 | /* |
46f4a4d2 PJ |
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 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
46f4a4d2 | 19 | */ |
5b5bfb8c PJ |
20 | #include <zebra.h> |
21 | ||
22 | #include "vty.h" | |
23 | #include "stream.h" | |
24 | #include "privs.h" | |
25 | #include "memory.h" | |
3f9c7369 | 26 | #include "queue.h" |
039f3a34 | 27 | #include "filter.h" |
5b5bfb8c PJ |
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 */ | |
d62a17ae | 39 | struct test_spec { |
40 | const char *shouldbe; /* the string the path should parse to */ | |
5b5bfb8c PJ |
41 | }; |
42 | ||
43 | ||
44 | /* test segments to parse and validate, and use for other tests */ | |
45 | static struct test_segment { | |
d62a17ae | 46 | const char *name; |
47 | const char *desc; | |
d7c0a89a | 48 | const uint8_t data[1024]; |
d62a17ae | 49 | int len; |
50 | struct test_spec sp; | |
51 | } test_segments[] = {{/* 0 */ | |
52 | "ipaddr", | |
53 | "rt 1.2.3.4:257", | |
54 | {ECOMMUNITY_ENCODE_IP, ECOMMUNITY_ROUTE_TARGET, 0x1, 0x2, | |
55 | 0x3, 0x4, 0x1, 0x1}, | |
56 | 8, | |
57 | {"rt 1.2.3.4:257"}}, | |
58 | {/* 1 */ | |
59 | "ipaddr-so", | |
60 | "soo 1.2.3.4:257", | |
61 | {ECOMMUNITY_ENCODE_IP, ECOMMUNITY_SITE_ORIGIN, 0x1, 0x2, | |
62 | 0x3, 0x4, 0x1, 0x1}, | |
63 | 8, | |
64 | {"soo 1.2.3.4:257"}}, | |
65 | {/* 2 */ | |
66 | "asn", | |
67 | "rt 23456:987654321", | |
68 | {ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN, 0x5b, 0xa0, | |
69 | 0x3a, 0xde, 0x68, 0xb1}, | |
70 | 8, | |
71 | {"soo 23456:987654321"}}, | |
72 | {/* 3 */ | |
73 | "asn4", | |
74 | "rt 168450976:4321", | |
75 | {ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN, 0xa, 0xa, | |
76 | 0x5b, 0xa0, 0x10, 0xe1}, | |
77 | 8, | |
78 | {"soo 168450976:4321"}}, | |
79 | {NULL, NULL, {0}, 0, {NULL}}}; | |
5b5bfb8c PJ |
80 | |
81 | ||
82 | /* validate the given aspath */ | |
d62a17ae | 83 | static int validate(struct ecommunity *ecom, const struct test_spec *sp) |
5b5bfb8c | 84 | { |
d62a17ae | 85 | int fails = 0; |
86 | struct ecommunity *etmp; | |
87 | char *str1, *str2; | |
88 | ||
89 | printf("got:\n %s\n", ecommunity_str(ecom)); | |
90 | str1 = ecommunity_ecom2str(ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0); | |
91 | etmp = ecommunity_str2com(str1, 0, 1); | |
92 | if (etmp) | |
93 | str2 = ecommunity_ecom2str(etmp, | |
94 | ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0); | |
95 | else | |
96 | str2 = NULL; | |
97 | ||
98 | if (strcmp(sp->shouldbe, str1)) { | |
99 | failed++; | |
100 | fails++; | |
101 | printf("shouldbe: %s\n%s\n", str1, sp->shouldbe); | |
102 | } | |
103 | if (!etmp || strcmp(str1, str2)) { | |
104 | failed++; | |
105 | fails++; | |
106 | printf("dogfood: in %s\n" | |
107 | " in->out %s\n", | |
108 | str1, (etmp && str2) ? str2 : "NULL"); | |
109 | } | |
110 | ecommunity_free(&etmp); | |
111 | XFREE(MTYPE_ECOMMUNITY_STR, str1); | |
112 | XFREE(MTYPE_ECOMMUNITY_STR, str2); | |
113 | ||
114 | return fails; | |
5b5bfb8c PJ |
115 | } |
116 | ||
117 | /* basic parsing test */ | |
d62a17ae | 118 | static void parse_test(struct test_segment *t) |
5b5bfb8c | 119 | { |
d62a17ae | 120 | struct ecommunity *ecom; |
121 | ||
122 | printf("%s: %s\n", t->name, t->desc); | |
123 | ||
d7c0a89a | 124 | ecom = ecommunity_parse((uint8_t *)t->data, t->len); |
5b5bfb8c | 125 | |
d62a17ae | 126 | printf("ecom: %s\nvalidating...:\n", ecommunity_str(ecom)); |
5b5bfb8c | 127 | |
d62a17ae | 128 | if (!validate(ecom, &t->sp)) |
129 | printf("OK\n"); | |
130 | else | |
131 | printf("failed\n"); | |
5b5bfb8c | 132 | |
d62a17ae | 133 | printf("\n"); |
134 | ecommunity_unintern(&ecom); | |
5b5bfb8c PJ |
135 | } |
136 | ||
d62a17ae | 137 | |
138 | int main(void) | |
5b5bfb8c | 139 | { |
d62a17ae | 140 | int i = 0; |
141 | ecommunity_init(); | |
142 | while (test_segments[i].name) | |
143 | parse_test(&test_segments[i++]); | |
144 | ||
145 | printf("failures: %d\n", failed); | |
146 | // printf ("aspath count: %ld\n", aspath_count()); | |
147 | return failed; | |
148 | // return (failed + aspath_count()); | |
5b5bfb8c | 149 | } |