]> git.proxmox.com Git - mirror_frr.git/blob - tests/ecommunity_test.c
9166af61421fbceb07ffe2fd80d76aa683c11c85
[mirror_frr.git] / tests / ecommunity_test.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 {
42 const char *shouldbe; /* the string the path should parse to */
43 };
44
45
46 /* test segments to parse and validate, and use for other tests */
47 static struct test_segment {
48 const char *name;
49 const char *desc;
50 const u_int8_t data[1024];
51 int len;
52 struct test_spec sp;
53 } test_segments [] =
54 {
55 { /* 0 */
56 "ipaddr",
57 "rt 1.2.3.4:257",
58 { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_ROUTE_TARGET,
59 0x1,0x2,0x3,0x4, 0x1,0x1 },
60 8,
61 { "rt 1.2.3.4:257" }
62 },
63 { /* 1 */
64 "ipaddr-so",
65 "soo 1.2.3.4:257",
66 { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_SITE_ORIGIN,
67 0x1,0x2,0x3,0x4, 0x1,0x1},
68 8,
69 { "soo 1.2.3.4:257" }
70 },
71 { /* 2 */
72 "asn",
73 "rt 23456:987654321",
74 { ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN,
75 0x5b,0xa0, 0x3a,0xde,0x68,0xb1 },
76 8,
77 { "soo 23456:987654321" }
78 },
79 { /* 3 */
80 "asn4",
81 "rt 168450976:4321",
82 { ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN,
83 0xa,0xa,0x5b,0xa0, 0x10,0xe1 },
84 8,
85 { "soo 168450976:4321" }
86 },
87 { NULL, NULL, {0}, 0, { NULL } }
88 };
89
90
91 /* validate the given aspath */
92 static int
93 validate (struct ecommunity *ecom, const struct test_spec *sp)
94 {
95 int fails = 0;
96 struct ecommunity *etmp;
97 char *str1, *str2;
98
99 printf ("got:\n %s\n", ecommunity_str (ecom));
100 str1 = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
101 etmp = ecommunity_str2com (str1, 0, 1);
102 if (etmp)
103 str2 = ecommunity_ecom2str (etmp, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
104 else
105 str2 = NULL;
106
107 if (strcmp (sp->shouldbe, str1))
108 {
109 failed++;
110 fails++;
111 printf ("shouldbe: %s\n%s\n", str1, sp->shouldbe);
112 }
113 if (!etmp || strcmp (str1, str2))
114 {
115 failed++;
116 fails++;
117 printf ("dogfood: in %s\n"
118 " in->out %s\n",
119 str1,
120 (etmp && str2) ? str2 : "NULL");
121 }
122 ecommunity_free (&etmp);
123 XFREE (MTYPE_ECOMMUNITY_STR, str1);
124 XFREE (MTYPE_ECOMMUNITY_STR, str2);
125
126 return fails;
127 }
128
129 /* basic parsing test */
130 static void
131 parse_test (struct test_segment *t)
132 {
133 struct ecommunity *ecom;
134
135 printf ("%s: %s\n", t->name, t->desc);
136
137 ecom = ecommunity_parse ((u_int8_t *)t->data, t->len);
138
139 printf ("ecom: %s\nvalidating...:\n", ecommunity_str (ecom));
140
141 if (!validate (ecom, &t->sp))
142 printf ("OK\n");
143 else
144 printf ("failed\n");
145
146 printf ("\n");
147 ecommunity_unintern (&ecom);
148 }
149
150
151 int
152 main (void)
153 {
154 int i = 0;
155 ecommunity_init();
156 while (test_segments[i].name)
157 parse_test (&test_segments[i++]);
158
159 printf ("failures: %d\n", failed);
160 //printf ("aspath count: %ld\n", aspath_count());
161 return failed;
162 //return (failed + aspath_count());
163 }