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