]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-in-addr-util.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-in-addr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <netinet/in.h>
4
5 #include "in-addr-util.h"
6
7 static void test_in_addr_prefix_from_string(const char *p, int family, int ret, const union in_addr_union *u, unsigned char prefixlen, bool use_default) {
8 union in_addr_union q;
9 unsigned char l;
10 int r;
11
12 r = in_addr_prefix_from_string_internal(p, use_default, family, &q, &l);
13 assert_se(r == ret);
14
15 if (r >= 0) {
16 int f;
17
18 assert_se(in_addr_equal(family, &q, u));
19 assert_se(l == prefixlen);
20
21 r = in_addr_prefix_from_string_auto_internal(p, use_default, &f, &q, &l);
22 assert_se(r >= 0);
23
24 assert_se(f == family);
25 assert_se(in_addr_equal(family, &q, u));
26 assert_se(l == prefixlen);
27 }
28 }
29
30 int main(int argc, char *argv[]) {
31 test_in_addr_prefix_from_string("", AF_INET, -EINVAL, NULL, 0, false);
32 test_in_addr_prefix_from_string("/", AF_INET, -EINVAL, NULL, 0, false);
33 test_in_addr_prefix_from_string("/8", AF_INET, -EINVAL, NULL, 0, false);
34 test_in_addr_prefix_from_string("1.2.3.4", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, false);
35 test_in_addr_prefix_from_string("1.2.3.4/0", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 0, false);
36 test_in_addr_prefix_from_string("1.2.3.4/1", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 1, false);
37 test_in_addr_prefix_from_string("1.2.3.4/2", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 2, false);
38 test_in_addr_prefix_from_string("1.2.3.4/32", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, false);
39 test_in_addr_prefix_from_string("1.2.3.4/33", AF_INET, -ERANGE, NULL, 0, false);
40 test_in_addr_prefix_from_string("1.2.3.4/-1", AF_INET, -ERANGE, NULL, 0, false);
41 test_in_addr_prefix_from_string("::1", AF_INET, -EINVAL, NULL, 0, false);
42
43 test_in_addr_prefix_from_string("", AF_INET6, -EINVAL, NULL, 0, false);
44 test_in_addr_prefix_from_string("/", AF_INET6, -EINVAL, NULL, 0, false);
45 test_in_addr_prefix_from_string("/8", AF_INET6, -EINVAL, NULL, 0, false);
46 test_in_addr_prefix_from_string("::1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, false);
47 test_in_addr_prefix_from_string("::1/0", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 0, false);
48 test_in_addr_prefix_from_string("::1/1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 1, false);
49 test_in_addr_prefix_from_string("::1/2", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 2, false);
50 test_in_addr_prefix_from_string("::1/32", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 32, false);
51 test_in_addr_prefix_from_string("::1/33", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 33, false);
52 test_in_addr_prefix_from_string("::1/64", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 64, false);
53 test_in_addr_prefix_from_string("::1/128", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, false);
54 test_in_addr_prefix_from_string("::1/129", AF_INET6, -ERANGE, NULL, 0, false);
55 test_in_addr_prefix_from_string("::1/-1", AF_INET6, -ERANGE, NULL, 0, false);
56
57 test_in_addr_prefix_from_string("", AF_INET, -EINVAL, NULL, 0, true);
58 test_in_addr_prefix_from_string("/", AF_INET, -EINVAL, NULL, 0, true);
59 test_in_addr_prefix_from_string("/8", AF_INET, -EINVAL, NULL, 0, true);
60 test_in_addr_prefix_from_string("1.2.3.4", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 8, true);
61 test_in_addr_prefix_from_string("1.2.3.4/0", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 0, true);
62 test_in_addr_prefix_from_string("1.2.3.4/1", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 1, true);
63 test_in_addr_prefix_from_string("1.2.3.4/2", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 2, true);
64 test_in_addr_prefix_from_string("1.2.3.4/32", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, true);
65 test_in_addr_prefix_from_string("1.2.3.4/33", AF_INET, -ERANGE, NULL, 0, true);
66 test_in_addr_prefix_from_string("1.2.3.4/-1", AF_INET, -ERANGE, NULL, 0, true);
67 test_in_addr_prefix_from_string("::1", AF_INET, -EINVAL, NULL, 0, true);
68
69 test_in_addr_prefix_from_string("", AF_INET6, -EINVAL, NULL, 0, true);
70 test_in_addr_prefix_from_string("/", AF_INET6, -EINVAL, NULL, 0, true);
71 test_in_addr_prefix_from_string("/8", AF_INET6, -EINVAL, NULL, 0, true);
72 test_in_addr_prefix_from_string("::1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 0, true);
73 test_in_addr_prefix_from_string("::1/0", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 0, true);
74 test_in_addr_prefix_from_string("::1/1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 1, true);
75 test_in_addr_prefix_from_string("::1/2", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 2, true);
76 test_in_addr_prefix_from_string("::1/32", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 32, true);
77 test_in_addr_prefix_from_string("::1/33", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 33, true);
78 test_in_addr_prefix_from_string("::1/64", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 64, true);
79 test_in_addr_prefix_from_string("::1/128", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, true);
80 test_in_addr_prefix_from_string("::1/129", AF_INET6, -ERANGE, NULL, 0, true);
81 test_in_addr_prefix_from_string("::1/-1", AF_INET6, -ERANGE, NULL, 0, true);
82
83 return 0;
84 }