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