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