]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_prefix2str.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / lib / test_prefix2str.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * prefix2str() unit test
4 * Copyright (C) 2019 David Lamparter
5 * Portions:
6 * Copyright (C) 2019 Cumulus Networks, Inc
7 * Quentin Young
8 */
9 #include <zebra.h>
10
11 #include "lib/prefix.h"
12
13 #include "tests/helpers/c/prng.h"
14
15 int main(int argc, char **argv)
16 {
17 size_t i, j, k, l;
18 struct in6_addr i6;
19 char buf1[64], buf2[64], ntopbuf[64];
20 struct prng *prng;
21 struct prefix p = {};
22
23 prng = prng_new(0);
24 /* IPv4 */
25 p.family = AF_INET;
26 for (i = 0; i < 1000; i++) {
27 p.u.prefix = prng_rand(prng);
28 p.prefixlen = prng_rand(prng) >> 26;
29 snprintf(buf1, sizeof(buf1), "%s/%d",
30 inet_ntop(AF_INET, &p.u.prefix4, ntopbuf,
31 sizeof(ntopbuf)),
32 p.prefixlen);
33 prefix2str(&p, buf2, sizeof(buf2));
34 assert(!strcmp(buf1, buf2));
35 fprintf(stdout, "%s\n", buf1);
36 }
37
38 /* IPv6 */
39 p.family = AF_INET6;
40 for (i = 0; i < 10000; i++) {
41 uint16_t *i6w = (uint16_t *)&i6;
42 for (j = 0; j < 8; j++)
43 i6w[j] = prng_rand(prng);
44
45 /* clear some words */
46 l = prng_rand(prng) & 7;
47 for (j = 0; j < l; j++) {
48 uint32_t num = __builtin_ctz(prng_rand(prng));
49 uint32_t where = prng_rand(prng) & 7;
50
51 for (k = where; k < where + num && k < 8; k++)
52 i6w[k] = 0;
53 }
54
55 p.prefixlen = prng_rand(prng) >> 24;
56 memcpy(&p.u.prefix, &i6, sizeof(i6));
57 snprintf(buf1, sizeof(buf1), "%s/%d",
58 inet_ntop(AF_INET6, &p.u.prefix6, ntopbuf,
59 sizeof(ntopbuf)),
60 p.prefixlen);
61 prefix2str(&p, buf2, sizeof(buf2));
62 assert(!strcmp(buf1, buf2));
63 fprintf(stdout, "%s\n", buf1);
64 }
65
66 return 0;
67 }