]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_nexthop.c
Merge pull request #12780 from opensourcerouting/spdx-license-id
[mirror_frr.git] / tests / lib / test_nexthop.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Nexthop module test.
4 *
5 * Copyright (C) 2021 by Volta Networks, Inc.
6 */
7
8 #include <zebra.h>
9 #include <nexthop.h>
10
11 static bool verbose;
12
13 static void test_run_first(void)
14 {
15 int ret, i;
16 struct nexthop *nh1, *nh2;
17 struct in_addr addr;
18 struct in6_addr addr6;
19 mpls_label_t labels[MPLS_MAX_LABELS];
20
21 /* Test comparison apis */
22
23 /* ifindex comparisons */
24 nh1 = nexthop_from_ifindex(11, 0);
25 nh2 = nexthop_from_ifindex(12, 0);
26
27 ret = nexthop_cmp_basic(nh1, nh2);
28 assert(ret < 0);
29
30 nexthop_free(nh1);
31 nh1 = nexthop_from_ifindex(12, 0);
32
33 ret = nexthop_cmp_basic(nh1, nh2);
34 assert(ret == 0);
35
36 nexthop_free(nh1);
37 nexthop_free(nh2);
38
39 /* ipv4, vrf */
40 addr.s_addr = 0x04030201;
41 nh1 = nexthop_from_ipv4(&addr, NULL, 0);
42 nh2 = nexthop_from_ipv4(&addr, NULL, 111);
43
44 ret = nexthop_cmp_basic(nh1, nh2);
45 assert(ret != 0);
46
47 nexthop_free(nh2);
48
49 addr.s_addr = 0x04030202;
50 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
51
52 ret = nexthop_cmp_basic(nh1, nh2);
53 assert(ret != 0);
54
55 nexthop_free(nh2);
56
57 addr.s_addr = 0x04030201;
58 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
59
60 ret = nexthop_cmp_basic(nh1, nh2);
61 assert(ret == 0);
62
63 /* Weight */
64 nh2->weight = 20;
65
66 ret = nexthop_cmp_basic(nh1, nh2);
67 assert(ret != 0);
68
69 nexthop_free(nh1);
70 nexthop_free(nh2);
71
72 /* ipv6 */
73 memset(addr6.s6_addr, 0, sizeof(addr6.s6_addr));
74 nh1 = nexthop_from_ipv6(&addr6, 0);
75 nh2 = nexthop_from_ipv6(&addr6, 0);
76
77 ret = nexthop_cmp_basic(nh1, nh2);
78 assert(ret == 0);
79
80 nexthop_free(nh2);
81
82 nh2 = nexthop_from_ipv6(&addr6, 1);
83
84 ret = nexthop_cmp_basic(nh1, nh2);
85 assert(ret != 0);
86
87 nexthop_free(nh2);
88
89 addr6.s6_addr[14] = 1;
90 addr6.s6_addr[15] = 1;
91 nh2 = nexthop_from_ipv6(&addr6, 0);
92
93 ret = nexthop_cmp_basic(nh1, nh2);
94 assert(ret != 0);
95
96 nexthop_free(nh1);
97 nexthop_free(nh2);
98
99 /* Blackhole */
100 nh1 = nexthop_from_blackhole(BLACKHOLE_REJECT, 0);
101 nh2 = nexthop_from_blackhole(BLACKHOLE_REJECT, 0);
102
103 ret = nexthop_cmp_basic(nh1, nh2);
104 assert(ret == 0);
105
106 nexthop_free(nh2);
107
108 nh2 = nexthop_from_blackhole(BLACKHOLE_NULL, 0);
109
110 ret = nexthop_cmp_basic(nh1, nh2);
111 assert(ret != 0);
112
113 /* Labels */
114 addr.s_addr = 0x04030201;
115 nh1 = nexthop_from_ipv4(&addr, NULL, 0);
116 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
117
118 memset(labels, 0, sizeof(labels));
119 labels[0] = 111;
120 labels[1] = 222;
121
122 nexthop_add_labels(nh1, ZEBRA_LSP_STATIC, 2, labels);
123
124 ret = nexthop_cmp_basic(nh1, nh2);
125 assert(ret != 0);
126
127 nexthop_add_labels(nh2, ZEBRA_LSP_STATIC, 2, labels);
128
129 ret = nexthop_cmp_basic(nh1, nh2);
130 assert(ret == 0);
131
132 nexthop_free(nh2);
133
134 /* LSP type isn't included */
135 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
136 nexthop_add_labels(nh2, ZEBRA_LSP_LDP, 2, labels);
137
138 ret = nexthop_cmp_basic(nh1, nh2);
139 assert(ret == 0);
140
141 nexthop_free(nh2);
142
143 labels[2] = 333;
144 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
145 nexthop_add_labels(nh2, ZEBRA_LSP_LDP, 3, labels);
146
147 ret = nexthop_cmp_basic(nh1, nh2);
148 assert(ret != 0);
149
150 nexthop_free(nh1);
151 nexthop_free(nh2);
152
153 nh1 = nexthop_from_ipv4(&addr, NULL, 0);
154 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
155
156 for (i = 0; i < MPLS_MAX_LABELS; i++)
157 labels[i] = 111 * (i + 1);
158
159 nexthop_add_labels(nh1, ZEBRA_LSP_LDP, MPLS_MAX_LABELS, labels);
160 nexthop_add_labels(nh2, ZEBRA_LSP_LDP, MPLS_MAX_LABELS, labels);
161
162 ret = nexthop_cmp_basic(nh1, nh2);
163 assert(ret == 0);
164
165 nexthop_free(nh2);
166
167 /* Test very last label in stack */
168 labels[15] = 999;
169 nh2 = nexthop_from_ipv4(&addr, NULL, 0);
170 nexthop_add_labels(nh2, ZEBRA_LSP_LDP, MPLS_MAX_LABELS, labels);
171
172 ret = nexthop_cmp_basic(nh1, nh2);
173 assert(ret != 0);
174
175 /* End */
176 nexthop_free(nh1);
177 nexthop_free(nh2);
178 }
179
180 int main(int argc, char **argv)
181 {
182 if (argc >= 2 && !strcmp("-v", argv[1]))
183 verbose = true;
184 test_run_first();
185 printf("Simple test passed.\n");
186 }