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