summaryrefslogtreecommitdiffstats
path: root/tests/ecommunity_test.c
blob: 87f20f282d870165d40538e75229dcaeb4998934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <zebra.h>

#include "vty.h"
#include "stream.h"
#include "privs.h"
#include "memory.h"

#include "bgpd/bgpd.h"
#include "bgpd/bgp_ecommunity.h"

/* need these to link in libbgp */
struct zebra_privs_t *bgpd_privs = NULL;
struct thread_master *master = NULL;

static int failed = 0;

/* specification for a test - what the results should be */
struct test_spec 
{
  const char *shouldbe; /* the string the path should parse to */
};


/* test segments to parse and validate, and use for other tests */
static struct test_segment {
  const char *name;
  const char *desc;
  const u_int8_t data[1024];
  int len;
  struct test_spec sp;
} test_segments [] = 
{
  { /* 0 */
    "ipaddr",
    "rt 1.2.3.4:257",
    { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_ROUTE_TARGET,
      0x1,0x2,0x3,0x4,	0x1,0x1 },
    8,
    { "rt 1.2.3.4:257" }
  },
  { /* 1 */
    "ipaddr-so",
    "soo 1.2.3.4:257",
    { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_SITE_ORIGIN,
      0x1,0x2,0x3,0x4,	0x1,0x1},
    8,
    { "soo 1.2.3.4:257" }
  },
  { /* 2 */
    "asn",
    "rt 23456:987654321",
    { ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN,
      0x5b,0xa0,	0x3a,0xde,0x68,0xb1 },
    8,
    { "soo 23456:987654321" }
  },
  { /* 3 */
    "asn4",
    "rt 168450976:4321",
    { ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN,
      0xa,0xa,0x5b,0xa0,	0x10,0xe1 },
    8,
    { "soo 168450976:4321" }
  },
  { NULL, NULL, {0}, 0, { NULL } }
};


/* validate the given aspath */
static int
validate (struct ecommunity *ecom, const struct test_spec *sp)
{
  int fails = 0;
  struct ecommunity *etmp;
  char *str1, *str2;
    
  printf ("got:\n  %s\n", ecommunity_str (ecom));
  str1 = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
  etmp = ecommunity_str2com (str1, 0, 1);
  if (etmp)
    str2 = ecommunity_ecom2str (etmp, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
  else
    str2 = NULL;
  
  if (strcmp (sp->shouldbe, str1))
    {
      failed++;
      fails++;
      printf ("shouldbe: %s\n%s\n", str1, sp->shouldbe);
    }
  if (!etmp || strcmp (str1, str2))
    {
      failed++;
      fails++;
      printf ("dogfood: in %s\n"
              "    in->out %s\n",
              str1, 
              (etmp && str2) ? str2 : "NULL");
    }
  ecommunity_free (&etmp);
  XFREE (MTYPE_ECOMMUNITY_STR, str1);
  XFREE (MTYPE_ECOMMUNITY_STR, str2);
  
  return fails;
}

/* basic parsing test */
static void
parse_test (struct test_segment *t)
{
  struct ecommunity *ecom;
  
  printf ("%s: %s\n", t->name, t->desc);

  ecom = ecommunity_parse (t->data, t->len);

  printf ("ecom: %s\nvalidating...:\n", ecommunity_str (ecom));

  if (!validate (ecom, &t->sp))
    printf ("OK\n");
  else
    printf ("failed\n");
  
  printf ("\n");
  ecommunity_unintern (&ecom);
}

     
int
main (void)
{
  int i = 0;
  ecommunity_init();
  while (test_segments[i].name)
    parse_test (&test_segments[i++]);
  
  printf ("failures: %d\n", failed);
  //printf ("aspath count: %ld\n", aspath_count());
  return failed;
  //return (failed + aspath_count());
}