LCOV - code coverage report
Current view: top level - tests/testcases/bgpd - test_ecommunities.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 38 76.3 %
Date: 2015-11-19 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.10