]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_ntop.c
Merge pull request #8978 from kssoman/ospf_new
[mirror_frr.git] / tests / lib / test_ntop.c
CommitLineData
874035be
DL
1/*
2 * frr_inet_ntop() unit test
3 * Copyright (C) 2019 David Lamparter
4 *
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)
8 * any later version.
9 *
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
13 * more details.
14 *
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
18 */
19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <assert.h>
25
26#include "tests/helpers/c/prng.h"
27
28/* NB: libfrr is NOT linked for this unit test! */
29
30#define INET_NTOP_NO_OVERRIDE
31#include "lib/ntop.c"
32
33int main(int argc, char **argv)
34{
35 size_t i, j, k, l;
36 struct in_addr i4;
37 struct in6_addr i6, i6check;
38 char buf1[64], buf2[64];
39 const char *rv;
40 struct prng *prng;
41
42 prng = prng_new(0);
43 /* IPv4 */
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));
49 }
50
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)
56 assert(!rv);
57 else
58 assert(rv && !strcmp(buf1, buf2));
59 }
60
61 /* IPv6 */
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);
66
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;
72
73 for (k = where; k < where + num && k < 8; k++)
74 i6w[k] = 0;
75 }
76
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);
81
82 assert(inet_pton(AF_INET6, buf1, &i6check));
83 assert(!memcmp(&i6, &i6check, sizeof(i6)));
874035be
DL
84 }
85 return 0;
86}