]>
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_RO 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 nexthop_chain_add_recursive(struct nexthop_chain
*nc
)
115 nh
= calloc(sizeof(*nh
), 1);
118 assert(nc
->current_top
);
119 if (nc
->current_recursive
)
121 nc
->current_recursive
->next
= nh
;
122 nh
->prev
= nc
->current_recursive
;
123 nc
->current_recursive
= nh
;
127 SET_FLAG(nc
->current_top
->flags
, NEXTHOP_FLAG_RECURSIVE
);
128 nc
->current_top
->resolved
= nh
;
129 nc
->current_recursive
= nh
;
131 str_appendf(&nc
->repr
, " %p\n", nh
);
135 nexthop_chain_clear(struct nexthop_chain
*nc
)
137 struct nexthop
*tcur
, *tnext
;
139 for (tcur
= nc
->head
; tcur
; tcur
= tnext
)
142 if (CHECK_FLAG(tcur
->flags
, NEXTHOP_FLAG_RECURSIVE
))
144 struct nexthop
*rcur
, *rnext
;
145 for (rcur
= tcur
->resolved
; rcur
; rcur
= rnext
)
153 nc
->head
= nc
->current_top
= nc
->current_recursive
= NULL
;
159 nexthop_chain_free(struct nexthop_chain
*nc
)
163 nexthop_chain_clear(nc
);
167 /* This function builds a string representation of
168 * the nexthop chain using the ALL_NEXTHOPS_RO macro.
169 * It verifies that the ALL_NEXTHOPS_RO macro iterated
170 * correctly over the nexthop chain by comparing the
171 * generated representation with the expected representation.
174 nexthop_chain_verify_iter(struct nexthop_chain
*nc
)
176 struct nexthop
*nh
, *tnh
;
180 for (ALL_NEXTHOPS_RO(nc
->head
, nh
, tnh
, recursing
))
183 str_appendf(&repr
, " %p\n", nh
);
185 str_appendf(&repr
, "%p\n", nh
);
189 printf("===\n%s", repr
);
190 assert((!repr
&& !nc
->repr
) || (repr
&& nc
->repr
&& !strcmp(repr
, nc
->repr
)));
194 /* This test run builds a simple nexthop chain
195 * with some recursive nexthops and verifies that
196 * the iterator works correctly in each stage along
202 struct nexthop_chain
*nc
;
204 nc
= nexthop_chain_new();
205 nexthop_chain_verify_iter(nc
);
207 nexthop_chain_add_top(nc
);
208 nexthop_chain_verify_iter(nc
);
210 nexthop_chain_add_top(nc
);
211 nexthop_chain_verify_iter(nc
);
213 nexthop_chain_add_recursive(nc
);
214 nexthop_chain_verify_iter(nc
);
216 nexthop_chain_add_recursive(nc
);
217 nexthop_chain_verify_iter(nc
);
219 nexthop_chain_add_top(nc
);
220 nexthop_chain_verify_iter(nc
);
222 nexthop_chain_add_top(nc
);
223 nexthop_chain_verify_iter(nc
);
225 nexthop_chain_add_top(nc
);
226 nexthop_chain_verify_iter(nc
);
228 nexthop_chain_add_recursive(nc
);
229 nexthop_chain_verify_iter(nc
);
231 nexthop_chain_add_recursive(nc
);
232 nexthop_chain_verify_iter(nc
);
234 nexthop_chain_add_recursive(nc
);
235 nexthop_chain_verify_iter(nc
);
237 nexthop_chain_free(nc
);
240 /* This test run builds numerous random
241 * nexthop chain configurations and verifies
242 * that the iterator correctly progresses
247 struct nexthop_chain
*nc
;
251 nc
= nexthop_chain_new();
254 for (i
= 0; i
< 1000000; i
++)
256 switch (prng_rand(prng
) % 10)
259 nexthop_chain_clear(nc
);
266 nexthop_chain_add_top(nc
);
273 nexthop_chain_add_recursive(nc
);
276 nexthop_chain_verify_iter(nc
);
278 nexthop_chain_free(nc
);
282 int main(int argc
, char **argv
)
284 if (argc
>= 2 && !strcmp("-v", argv
[1]))
287 printf("Simple test passed.\n");
289 printf("PRNG test passed.\n");