]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_nexthop.c
7cf687dffe094811693819abba6532cbad2901a2
[mirror_frr.git] / tests / lib / test_nexthop.c
1 /*
2 * Nexthop module test.
3 *
4 * Copyright (C) 2021 by Volta Networks, Inc.
5 *
6 * This file is part of FRR.
7 *
8 * FRR is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * FRR is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24 #include <nexthop.h>
25
26 static bool verbose;
27
28 static void test_run_first(void)
29 {
30 int ret, i;
31 struct nexthop *nh1, *nh2;
32 struct in_addr addr;
33 struct in6_addr addr6;
34 mpls_label_t labels[MPLS_MAX_LABELS];
35
36 /* Test comparison apis */
37
38 /* ifindex comparisons */
39 nh1 = nexthop_from_ifindex(11, 0);
40 nh2 = nexthop_from_ifindex(12, 0);
41
42 ret = nexthop_cmp_basic(nh1, nh2);
43 assert(ret < 0);
44
45 nexthop_free(nh1);
46 nh1 = nexthop_from_ifindex(12, 0);
47
48 ret = nexthop_cmp_basic(nh1, nh2);
49 assert(ret == 0);
50
51 nexthop_free(nh1);
52 nexthop_free(nh2);
53
54 /* ipv4, vrf */
55 addr.s_addr = 0x04030201;
56 nh1 = nexthop_from_ipv4(&addr, NULL, 0);
57 nh2 = nexthop_from_ipv4(&addr, NULL, 111);
58
59 ret = nexthop_cmp_basic(nh1, nh2);
60 assert(ret != 0);
61
62 nexthop_free(nh2);
63
64 addr.s_addr = 0x04030202;
65 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
66
67 ret = nexthop_cmp_basic(nh1, nh2);
68 assert(ret != 0);
69
70 nexthop_free(nh2);
71
72 addr.s_addr = 0x04030201;
73 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
74
75 ret = nexthop_cmp_basic(nh1, nh2);
76 assert(ret == 0);
77
78 /* Weight */
79 nh2->weight = 20;
80
81 ret = nexthop_cmp_basic(nh1, nh2);
82 assert(ret != 0);
83
84 nexthop_free(nh1);
85 nexthop_free(nh2);
86
87 /* ipv6 */
88 memset(addr6.s6_addr, 0, sizeof(addr6.s6_addr));
89 nh1 = nexthop_from_ipv6(&addr6, 0);
90 nh2 = nexthop_from_ipv6(&addr6, 0);
91
92 ret = nexthop_cmp_basic(nh1, nh2);
93 assert(ret == 0);
94
95 nexthop_free(nh2);
96
97 nh2 = nexthop_from_ipv6(&addr6, 1);
98
99 ret = nexthop_cmp_basic(nh1, nh2);
100 assert(ret != 0);
101
102 nexthop_free(nh2);
103
104 addr6.s6_addr[14] = 1;
105 addr6.s6_addr[15] = 1;
106 nh2 = nexthop_from_ipv6(&addr6, 0);
107
108 ret = nexthop_cmp_basic(nh1, nh2);
109 assert(ret != 0);
110
111 nexthop_free(nh1);
112 nexthop_free(nh2);
113
114 /* Blackhole */
115 nh1 = nexthop_from_blackhole(BLACKHOLE_REJECT, 0);
116 nh2 = nexthop_from_blackhole(BLACKHOLE_REJECT, 0);
117
118 ret = nexthop_cmp_basic(nh1, nh2);
119 assert(ret == 0);
120
121 nexthop_free(nh2);
122
123 nh2 = nexthop_from_blackhole(BLACKHOLE_NULL, 0);
124
125 ret = nexthop_cmp_basic(nh1, nh2);
126 assert(ret != 0);
127
128 /* Labels */
129 addr.s_addr = 0x04030201;
130 nh1 = nexthop_from_ipv4(&addr, NULL, 0);
131 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
132
133 memset(labels, 0, sizeof(labels));
134 labels[0] = 111;
135 labels[1] = 222;
136
137 nexthop_add_labels(nh1, ZEBRA_LSP_STATIC, 2, labels);
138
139 ret = nexthop_cmp_basic(nh1, nh2);
140 assert(ret != 0);
141
142 nexthop_add_labels(nh2, ZEBRA_LSP_STATIC, 2, labels);
143
144 ret = nexthop_cmp_basic(nh1, nh2);
145 assert(ret == 0);
146
147 nexthop_free(nh2);
148
149 /* LSP type isn't included */
150 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
151 nexthop_add_labels(nh2, ZEBRA_LSP_LDP, 2, labels);
152
153 ret = nexthop_cmp_basic(nh1, nh2);
154 assert(ret == 0);
155
156 nexthop_free(nh2);
157
158 labels[2] = 333;
159 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
160 nexthop_add_labels(nh2, ZEBRA_LSP_LDP, 3, labels);
161
162 ret = nexthop_cmp_basic(nh1, nh2);
163 assert(ret != 0);
164
165 nexthop_free(nh1);
166 nexthop_free(nh2);
167
168 nh1 = nexthop_from_ipv4(&addr, NULL, 0);
169 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
170
171 for (i = 0; i < MPLS_MAX_LABELS; i++)
172 labels[i] = 111 * (i + 1);
173
174 nexthop_add_labels(nh1, ZEBRA_LSP_LDP, MPLS_MAX_LABELS, labels);
175 nexthop_add_labels(nh2, ZEBRA_LSP_LDP, MPLS_MAX_LABELS, labels);
176
177 ret = nexthop_cmp_basic(nh1, nh2);
178 assert(ret == 0);
179
180 nexthop_free(nh2);
181
182 /* Test very last label in stack */
183 labels[15] = 999;
184 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
185 nexthop_add_labels(nh2, ZEBRA_LSP_LDP, MPLS_MAX_LABELS, labels);
186
187 ret = nexthop_cmp_basic(nh1, nh2);
188 assert(ret != 0);
189
190 /* End */
191 nexthop_free(nh1);
192 nexthop_free(nh2);
193 }
194
195 int main(int argc, char **argv)
196 {
197 if (argc >= 2 && !strcmp("-v", argv[1]))
198 verbose = true;
199 test_run_first();
200 printf("Simple test passed.\n");
201 }