Import Upstream version 1.2.2
[quagga-debian.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 "filter.h"
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;
49   const u_int8_t data[1024];
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     }
121   ecommunity_free (&etmp);
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
136   ecom = ecommunity_parse ((u_int8_t *)t->data, t->len);
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");
146   ecommunity_unintern (&ecom);
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 }