]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
d62a17ae | 2 | /* |
46f4a4d2 | 3 | * Copyright (C) 2007 Sun Microsystems, Inc. |
46f4a4d2 | 4 | */ |
5b5bfb8c PJ |
5 | #include <zebra.h> |
6 | ||
7 | #include "vty.h" | |
8 | #include "stream.h" | |
9 | #include "privs.h" | |
10 | #include "memory.h" | |
3f9c7369 | 11 | #include "queue.h" |
039f3a34 | 12 | #include "filter.h" |
5b5bfb8c PJ |
13 | |
14 | #include "bgpd/bgpd.h" | |
15 | #include "bgpd/bgp_ecommunity.h" | |
16 | ||
17 | /* need these to link in libbgp */ | |
fcf6dce7 | 18 | struct zebra_privs_t bgpd_privs = {}; |
5b5bfb8c PJ |
19 | struct thread_master *master = NULL; |
20 | ||
21 | static int failed = 0; | |
22 | ||
23 | /* specification for a test - what the results should be */ | |
d62a17ae | 24 | struct test_spec { |
25 | const char *shouldbe; /* the string the path should parse to */ | |
5b5bfb8c PJ |
26 | }; |
27 | ||
28 | ||
29 | /* test segments to parse and validate, and use for other tests */ | |
30 | static struct test_segment { | |
d62a17ae | 31 | const char *name; |
32 | const char *desc; | |
d7c0a89a | 33 | const uint8_t data[1024]; |
d62a17ae | 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}}}; | |
5b5bfb8c PJ |
65 | |
66 | ||
67 | /* validate the given aspath */ | |
d62a17ae | 68 | static int validate(struct ecommunity *ecom, const struct test_spec *sp) |
5b5bfb8c | 69 | { |
d62a17ae | 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; | |
5b5bfb8c PJ |
100 | } |
101 | ||
102 | /* basic parsing test */ | |
d62a17ae | 103 | static void parse_test(struct test_segment *t) |
5b5bfb8c | 104 | { |
d62a17ae | 105 | struct ecommunity *ecom; |
106 | ||
107 | printf("%s: %s\n", t->name, t->desc); | |
108 | ||
27aa23a4 | 109 | ecom = ecommunity_parse((uint8_t *)t->data, t->len, 0); |
5b5bfb8c | 110 | |
d62a17ae | 111 | printf("ecom: %s\nvalidating...:\n", ecommunity_str(ecom)); |
5b5bfb8c | 112 | |
d62a17ae | 113 | if (!validate(ecom, &t->sp)) |
114 | printf("OK\n"); | |
115 | else | |
116 | printf("failed\n"); | |
5b5bfb8c | 117 | |
d62a17ae | 118 | printf("\n"); |
119 | ecommunity_unintern(&ecom); | |
5b5bfb8c PJ |
120 | } |
121 | ||
d62a17ae | 122 | |
123 | int main(void) | |
5b5bfb8c | 124 | { |
d62a17ae | 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()); | |
5b5bfb8c | 134 | } |