]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_ntop.c
2 * frr_inet_ntop() unit test
3 * Copyright (C) 2019 David Lamparter
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "tests/helpers/c/prng.h"
28 /* NB: libfrr is NOT linked for this unit test! */
30 #define INET_NTOP_NO_OVERRIDE
33 int main(int argc
, char **argv
)
37 struct in6_addr i6
, i6check
;
38 char buf1
[64], buf2
[64];
44 for (i
= 0; i
< 1000; i
++) {
45 i4
.s_addr
= prng_rand(prng
);
46 assert(frr_inet_ntop(AF_INET
, &i4
, buf1
, sizeof(buf1
)));
47 assert(inet_ntop(AF_INET
, &i4
, buf2
, sizeof(buf2
)));
48 assert(!strcmp(buf1
, buf2
));
51 /* check size limit */
52 for (i
= 0; i
< sizeof(buf1
); i
++) {
53 memset(buf2
, 0xcc, sizeof(buf2
));
54 rv
= frr_inet_ntop(AF_INET
, &i4
, buf2
, i
);
55 if (i
< strlen(buf1
) + 1)
58 assert(rv
&& !strcmp(buf1
, buf2
));
62 for (i
= 0; i
< 10000; i
++) {
63 uint16_t *i6w
= (uint16_t *)&i6
;
64 for (j
= 0; j
< 8; j
++)
65 i6w
[j
] = prng_rand(prng
);
67 /* clear some words */
68 l
= prng_rand(prng
) & 7;
69 for (j
= 0; j
< l
; j
++) {
70 uint32_t num
= __builtin_ctz(prng_rand(prng
));
71 uint32_t where
= prng_rand(prng
) & 7;
73 for (k
= where
; k
< where
+ num
&& k
< 8; k
++)
77 assert(frr_inet_ntop(AF_INET6
, &i6
, buf1
, sizeof(buf1
)));
78 assert(inet_ntop(AF_INET6
, &i6
, buf2
, sizeof(buf2
)));
79 if (strcmp(buf1
, buf2
))
80 printf("%-40s (FRR) != (SYS) %-40s\n", buf1
, buf2
);
82 assert(inet_pton(AF_INET6
, buf1
, &i6check
));
83 assert(!memcmp(&i6
, &i6check
, sizeof(i6
)));