bgpd: tighten bounds checking in RR ORF msg reader
[jleu-quagga.git] / tests / ecommunity_test.c
blob418f659f74ffeb910f8047ddc0e205b840e99dfa
1 #include <zebra.h>
3 #include "vty.h"
4 #include "stream.h"
5 #include "privs.h"
6 #include "memory.h"
8 #include "bgpd/bgpd.h"
9 #include "bgpd/bgp_ecommunity.h"
11 /* need these to link in libbgp */
12 struct zebra_privs_t *bgpd_privs = NULL;
13 struct thread_master *master = NULL;
15 static int failed = 0;
17 /* specification for a test - what the results should be */
18 struct test_spec
20 const char *shouldbe; /* the string the path should parse to */
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_char data[1024];
29 int len;
30 struct test_spec sp;
31 } test_segments [] =
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 },
39 { "rt 1.2.3.4:257" }
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},
47 { "soo 1.2.3.4:257" }
49 { /* 2 */
50 "asn",
51 "rt 23456:987654321",
52 { ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN,
53 0x5b,0xa0, 0x3a,0xde,0x68,0xb1 },
55 { "soo 23456:987654321" }
57 { /* 3 */
58 "asn4",
59 "rt 168450976:4321",
60 { ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN,
61 0xa,0xa,0x5b,0xa0, 0x10,0xe1 },
63 { "soo 168450976:4321" }
65 { NULL, NULL, {0}, 0, { NULL } }
69 /* validate the given aspath */
70 static int
71 validate (struct ecommunity *ecom, const struct test_spec *sp)
73 int fails = 0;
74 struct ecommunity *etmp;
75 char *str1, *str2;
77 printf ("got:\n %s\n", ecommunity_str (ecom));
78 str1 = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
79 etmp = ecommunity_str2com (str1, 0, 1);
80 if (etmp)
81 str2 = ecommunity_ecom2str (etmp, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
82 else
83 str2 = NULL;
85 if (strcmp (sp->shouldbe, str1))
87 failed++;
88 fails++;
89 printf ("shouldbe: %s\n%s\n", str1, sp->shouldbe);
91 if (!etmp || strcmp (str1, str2))
93 failed++;
94 fails++;
95 printf ("dogfood: in %s\n"
96 " in->out %s\n",
97 str1,
98 (etmp && str2) ? str2 : "NULL");
100 ecommunity_free (etmp);
101 XFREE (MTYPE_ECOMMUNITY_STR, str1);
102 XFREE (MTYPE_ECOMMUNITY_STR, str2);
104 return fails;
107 /* basic parsing test */
108 static void
109 parse_test (struct test_segment *t)
111 struct ecommunity *ecom;
113 printf ("%s: %s\n", t->name, t->desc);
115 ecom = ecommunity_parse (t->data, t->len);
117 printf ("ecom: %s\nvalidating...:\n", ecommunity_str (ecom));
119 if (!validate (ecom, &t->sp))
120 printf ("OK\n");
121 else
122 printf ("failed\n");
124 printf ("\n");
125 ecommunity_unintern (ecom);
130 main (void)
132 int i = 0;
133 ecommunity_init();
134 while (test_segments[i].name)
135 parse_test (&test_segments[i++]);
137 printf ("failures: %d\n", failed);
138 //printf ("aspath count: %ld\n", aspath_count());
139 return failed;
140 //return (failed + aspath_count());