2  * Copyright (C) 2007 Sun Microsystems, Inc.
 
   4  * This file is part of Quagga.
 
   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
 
  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.
 
  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
 
  29 #include "bgpd/bgpd.h"
 
  30 #include "bgpd/bgp_ecommunity.h"
 
  32 /* need these to link in libbgp */
 
  33 struct zebra_privs_t *bgpd_privs = NULL;
 
  34 struct thread_master *master = NULL;
 
  36 static int failed = 0;
 
  38 /* specification for a test - what the results should be */
 
  41   const char *shouldbe; /* the string the path should parse to */
 
  45 /* test segments to parse and validate, and use for other tests */
 
  46 static struct test_segment {
 
  49   const u_int8_t data[1024];
 
  57     { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_ROUTE_TARGET,
 
  58       0x1,0x2,0x3,0x4,  0x1,0x1 },
 
  65     { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_SITE_ORIGIN,
 
  66       0x1,0x2,0x3,0x4,  0x1,0x1},
 
  73     { ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN,
 
  74       0x5b,0xa0,        0x3a,0xde,0x68,0xb1 },
 
  76     { "soo 23456:987654321" }
 
  81     { ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN,
 
  82       0xa,0xa,0x5b,0xa0,        0x10,0xe1 },
 
  84     { "soo 168450976:4321" }
 
  86   { NULL, NULL, {0}, 0, { NULL } }
 
  90 /* validate the given aspath */
 
  92 validate (struct ecommunity *ecom, const struct test_spec *sp)
 
  95   struct ecommunity *etmp;
 
  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);
 
 102     str2 = ecommunity_ecom2str (etmp, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
 
 106   if (strcmp (sp->shouldbe, str1))
 
 110       printf ("shouldbe: %s\n%s\n", str1, sp->shouldbe);
 
 112   if (!etmp || strcmp (str1, str2))
 
 116       printf ("dogfood: in %s\n"
 
 119               (etmp && str2) ? str2 : "NULL");
 
 121   ecommunity_free (&etmp);
 
 122   XFREE (MTYPE_ECOMMUNITY_STR, str1);
 
 123   XFREE (MTYPE_ECOMMUNITY_STR, str2);
 
 128 /* basic parsing test */
 
 130 parse_test (struct test_segment *t)
 
 132   struct ecommunity *ecom;
 
 134   printf ("%s: %s\n", t->name, t->desc);
 
 136   ecom = ecommunity_parse ((u_int8_t *)t->data, t->len);
 
 138   printf ("ecom: %s\nvalidating...:\n", ecommunity_str (ecom));
 
 140   if (!validate (ecom, &t->sp))
 
 146   ecommunity_unintern (&ecom);
 
 155   while (test_segments[i].name)
 
 156     parse_test (&test_segments[i++]);
 
 158   printf ("failures: %d\n", failed);
 
 159   //printf ("aspath count: %ld\n", aspath_count());
 
 161   //return (failed + aspath_count());