]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_prefix2str.c
2 * prefix2str() unit test
3 * Copyright (C) 2019 David Lamparter
5 * Copyright (C) 2019 Cumulus Networks, Inc
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)
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
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
24 #include "lib/prefix.h"
26 #include "tests/helpers/c/prng.h"
28 int main(int argc
, char **argv
)
32 char buf1
[64], buf2
[64], ntopbuf
[64];
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
,
46 prefix2str(&p
, buf2
, sizeof(buf2
));
47 assert(!strcmp(buf1
, buf2
));
48 fprintf(stdout
, "%s\n", buf1
);
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
);
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;
64 for (k
= where
; k
< where
+ num
&& k
< 8; k
++)
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
,
74 prefix2str(&p
, buf2
, sizeof(buf2
));
75 assert(!strcmp(buf1
, buf2
));
76 fprintf(stdout
, "%s\n", buf1
);