]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_ntop.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / tests / lib / test_ntop.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
874035be
DL
2/*
3 * frr_inet_ntop() unit test
4 * Copyright (C) 2019 David Lamparter
874035be
DL
5 */
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include <assert.h>
12
13#include "tests/helpers/c/prng.h"
14
15/* NB: libfrr is NOT linked for this unit test! */
16
17#define INET_NTOP_NO_OVERRIDE
18#include "lib/ntop.c"
19
20int main(int argc, char **argv)
21{
22 size_t i, j, k, l;
23 struct in_addr i4;
24 struct in6_addr i6, i6check;
25 char buf1[64], buf2[64];
26 const char *rv;
27 struct prng *prng;
28
29 prng = prng_new(0);
30 /* IPv4 */
31 for (i = 0; i < 1000; i++) {
32 i4.s_addr = prng_rand(prng);
33 assert(frr_inet_ntop(AF_INET, &i4, buf1, sizeof(buf1)));
34 assert(inet_ntop(AF_INET, &i4, buf2, sizeof(buf2)));
35 assert(!strcmp(buf1, buf2));
36 }
37
38 /* check size limit */
39 for (i = 0; i < sizeof(buf1); i++) {
40 memset(buf2, 0xcc, sizeof(buf2));
41 rv = frr_inet_ntop(AF_INET, &i4, buf2, i);
42 if (i < strlen(buf1) + 1)
43 assert(!rv);
44 else
45 assert(rv && !strcmp(buf1, buf2));
46 }
47
48 /* IPv6 */
49 for (i = 0; i < 10000; i++) {
50 uint16_t *i6w = (uint16_t *)&i6;
51 for (j = 0; j < 8; j++)
52 i6w[j] = prng_rand(prng);
53
54 /* clear some words */
55 l = prng_rand(prng) & 7;
56 for (j = 0; j < l; j++) {
57 uint32_t num = __builtin_ctz(prng_rand(prng));
58 uint32_t where = prng_rand(prng) & 7;
59
60 for (k = where; k < where + num && k < 8; k++)
61 i6w[k] = 0;
62 }
63
64 assert(frr_inet_ntop(AF_INET6, &i6, buf1, sizeof(buf1)));
65 assert(inet_ntop(AF_INET6, &i6, buf2, sizeof(buf2)));
66 if (strcmp(buf1, buf2))
67 printf("%-40s (FRR) != (SYS) %-40s\n", buf1, buf2);
68
69 assert(inet_pton(AF_INET6, buf1, &i6check));
70 assert(!memcmp(&i6, &i6check, sizeof(i6)));
874035be
DL
71 }
72 return 0;
73}