]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_prefix2str.c
Merge pull request #8626 from idryzhov/fix-isis-topo1
[mirror_frr.git] / tests / lib / test_prefix2str.c
1 /*
2 * prefix2str() unit test
3 * Copyright (C) 2019 David Lamparter
4 * Portions:
5 * Copyright (C) 2019 Cumulus Networks, Inc
6 * Quentin Young
7 *
8 * This program 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 Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * 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 #include <zebra.h>
23
24 #include "lib/prefix.h"
25
26 #include "tests/helpers/c/prng.h"
27
28 int main(int argc, char **argv)
29 {
30 size_t i, j, k, l;
31 struct in6_addr i6;
32 char buf1[64], buf2[64], ntopbuf[64];
33 struct prng *prng;
34 struct prefix p = {};
35
36 prng = prng_new(0);
37 /* IPv4 */
38 p.family = AF_INET;
39 for (i = 0; i < 1000; i++) {
40 p.u.prefix = prng_rand(prng);
41 p.prefixlen = prng_rand(prng) >> 26;
42 snprintf(buf1, sizeof(buf1), "%s/%d",
43 inet_ntop(AF_INET, &p.u.prefix4, ntopbuf,
44 sizeof(ntopbuf)),
45 p.prefixlen);
46 prefix2str(&p, buf2, sizeof(buf2));
47 assert(!strcmp(buf1, buf2));
48 fprintf(stdout, "%s\n", buf1);
49 }
50
51 /* IPv6 */
52 p.family = AF_INET6;
53 for (i = 0; i < 10000; i++) {
54 uint16_t *i6w = (uint16_t *)&i6;
55 for (j = 0; j < 8; j++)
56 i6w[j] = prng_rand(prng);
57
58 /* clear some words */
59 l = prng_rand(prng) & 7;
60 for (j = 0; j < l; j++) {
61 uint32_t num = __builtin_ctz(prng_rand(prng));
62 uint32_t where = prng_rand(prng) & 7;
63
64 for (k = where; k < where + num && k < 8; k++)
65 i6w[k] = 0;
66 }
67
68 p.prefixlen = prng_rand(prng) >> 24;
69 memcpy(&p.u.prefix, &i6, sizeof(i6));
70 snprintf(buf1, sizeof(buf1), "%s/%d",
71 inet_ntop(AF_INET6, &p.u.prefix6, ntopbuf,
72 sizeof(ntopbuf)),
73 p.prefixlen);
74 prefix2str(&p, buf2, sizeof(buf2));
75 assert(!strcmp(buf1, buf2));
76 fprintf(stdout, "%s\n", buf1);
77 }
78
79 return 0;
80 }