]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_nexthop_iter.c
2 * Recursive Nexthop Iterator test.
3 * This tests the ALL_NEXTHOPS macro.
5 * Copyright (C) 2012 by Open Source Routing.
6 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
8 * This file is part of Quagga
10 * Quagga is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * Quagga is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "zebra/rib.h"
29 struct thread_master
*master
;
33 str_append(char **buf
, const char *repr
)
37 *buf
= realloc(*buf
, strlen(*buf
) + strlen(repr
) + 1);
39 strncpy((*buf
) + strlen(*buf
), repr
, strlen(repr
) + 1);
49 str_appendf(char **buf
, const char *format
, ...)
56 rv
= vasprintf(&pbuf
, format
, ap
);
60 str_append(buf
, pbuf
);
64 /* This structure contains a nexthop chain
65 * and its expected representation */
68 /* Head of the chain */
70 /* Last nexthop in top chain */
71 struct nexthop
*current_top
;
72 /* Last nexthop in current recursive chain */
73 struct nexthop
*current_recursive
;
74 /* Expected string representation. */
78 static struct nexthop_chain
*
79 nexthop_chain_new(void)
81 struct nexthop_chain
*rv
;
83 rv
= calloc(sizeof(*rv
), 1);
89 nexthop_chain_add_top(struct nexthop_chain
*nc
)
93 nh
= calloc(sizeof(*nh
), 1);
98 nc
->current_top
->next
= nh
;
99 nh
->prev
= nc
->current_top
;
100 nc
->current_top
= nh
;
104 nc
->head
= nc
->current_top
= nh
;
106 nc
->current_recursive
= NULL
;
107 str_appendf(&nc
->repr
, "%p\n", nh
);
111 add_string_representation (char **repr
, struct nexthop
*nh
)
113 struct nexthop
*parent
;
115 /* add indentations first */
116 parent
= nh
->rparent
;
119 str_appendf(repr
, " ");
120 parent
= parent
->rparent
;
122 str_appendf(repr
, "%p\n", nh
);
126 start_recursive_chain (struct nexthop_chain
*nc
, struct nexthop
*nh
)
128 SET_FLAG(nc
->current_top
->flags
, NEXTHOP_FLAG_RECURSIVE
);
129 nc
->current_top
->resolved
= nh
;
130 nh
->rparent
= nc
->current_top
;
131 nc
->current_recursive
= nh
;
134 nexthop_chain_add_recursive(struct nexthop_chain
*nc
)
138 nh
= calloc(sizeof(*nh
), 1);
141 assert(nc
->current_top
);
142 if (nc
->current_recursive
)
144 nc
->current_recursive
->next
= nh
;
145 nh
->prev
= nc
->current_recursive
;
146 nh
->rparent
= nc
->current_recursive
->rparent
;
147 nc
->current_recursive
= nh
;
150 start_recursive_chain (nc
, nh
);
152 add_string_representation (&nc
->repr
, nh
);
156 nexthop_chain_add_recursive_level(struct nexthop_chain
*nc
)
160 nh
= calloc(sizeof(*nh
), 1);
163 assert(nc
->current_top
);
164 if (nc
->current_recursive
)
166 SET_FLAG(nc
->current_recursive
->flags
, NEXTHOP_FLAG_RECURSIVE
);
167 nc
->current_recursive
->resolved
= nh
;
168 nh
->rparent
= nc
->current_recursive
;
169 nc
->current_recursive
= nh
;
172 start_recursive_chain (nc
, nh
);
174 add_string_representation (&nc
->repr
, nh
);
178 nexthop_clear_recursive (struct nexthop
*tcur
)
182 if (CHECK_FLAG(tcur
->flags
, NEXTHOP_FLAG_RECURSIVE
))
183 nexthop_clear_recursive (tcur
->resolved
);
185 nexthop_clear_recursive (tcur
->next
);
189 nexthop_chain_clear(struct nexthop_chain
*nc
)
191 nexthop_clear_recursive (nc
->head
);
192 nc
->head
= nc
->current_top
= nc
->current_recursive
= NULL
;
198 nexthop_chain_free(struct nexthop_chain
*nc
)
202 nexthop_chain_clear(nc
);
206 /* This function builds a string representation of
207 * the nexthop chain using the ALL_NEXTHOPS macro.
208 * It verifies that the ALL_NEXTHOPS macro iterated
209 * correctly over the nexthop chain by comparing the
210 * generated representation with the expected representation.
213 nexthop_chain_verify_iter(struct nexthop_chain
*nc
)
218 for (ALL_NEXTHOPS(nc
->head
, nh
))
219 add_string_representation (&repr
, nh
);
222 printf("===\n%s", repr
);
223 assert((!repr
&& !nc
->repr
) || (repr
&& nc
->repr
&& !strcmp(repr
, nc
->repr
)));
227 /* This test run builds a simple nexthop chain
228 * with some recursive nexthops and verifies that
229 * the iterator works correctly in each stage along
235 struct nexthop_chain
*nc
;
237 nc
= nexthop_chain_new();
238 nexthop_chain_verify_iter(nc
);
240 nexthop_chain_add_top(nc
);
241 nexthop_chain_verify_iter(nc
);
243 nexthop_chain_add_top(nc
);
244 nexthop_chain_verify_iter(nc
);
246 nexthop_chain_add_recursive(nc
);
247 nexthop_chain_verify_iter(nc
);
249 nexthop_chain_add_recursive(nc
);
250 nexthop_chain_verify_iter(nc
);
252 nexthop_chain_add_top(nc
);
253 nexthop_chain_verify_iter(nc
);
255 nexthop_chain_add_top(nc
);
256 nexthop_chain_verify_iter(nc
);
258 nexthop_chain_add_top(nc
);
259 nexthop_chain_verify_iter(nc
);
261 nexthop_chain_add_recursive(nc
);
262 nexthop_chain_verify_iter(nc
);
264 nexthop_chain_add_recursive(nc
);
265 nexthop_chain_verify_iter(nc
);
267 nexthop_chain_add_recursive(nc
);
268 nexthop_chain_verify_iter(nc
);
270 nexthop_chain_add_recursive_level(nc
);
271 nexthop_chain_verify_iter(nc
);
273 nexthop_chain_add_recursive(nc
);
274 nexthop_chain_verify_iter(nc
);
276 nexthop_chain_add_recursive(nc
);
277 nexthop_chain_verify_iter(nc
);
279 nexthop_chain_add_top(nc
);
280 nexthop_chain_verify_iter(nc
);
282 nexthop_chain_free(nc
);
285 /* This test run builds numerous random
286 * nexthop chain configurations and verifies
287 * that the iterator correctly progresses
292 struct nexthop_chain
*nc
;
296 nc
= nexthop_chain_new();
299 for (i
= 0; i
< 1000000; i
++)
301 switch (prng_rand(prng
) % 10)
304 nexthop_chain_clear(nc
);
311 nexthop_chain_add_top(nc
);
317 nexthop_chain_add_recursive(nc
);
321 nexthop_chain_add_recursive_level(nc
);
324 nexthop_chain_verify_iter(nc
);
326 nexthop_chain_free(nc
);
330 int main(int argc
, char **argv
)
332 if (argc
>= 2 && !strcmp("-v", argv
[1]))
335 printf("Simple test passed.\n");
337 printf("PRNG test passed.\n");