]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-in-addr-util.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / test / test-in-addr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd
4
5 Copyright 2017 Lennart Poettering
6 ***/
7
8 #include <netinet/in.h>
9
10 #include "in-addr-util.h"
11
12 static void test_in_addr_prefix_from_string(const char *p, int family, int ret, const union in_addr_union *u, unsigned char prefixlen) {
13 union in_addr_union q;
14 unsigned char l;
15 int r;
16
17 r = in_addr_prefix_from_string(p, family, &q, &l);
18 assert_se(r == ret);
19
20 if (r >= 0) {
21 int f;
22
23 assert_se(in_addr_equal(family, &q, u));
24 assert_se(l == prefixlen);
25
26 r = in_addr_prefix_from_string_auto(p, &f, &q, &l);
27 assert_se(r >= 0);
28
29 assert_se(f == family);
30 assert_se(in_addr_equal(family, &q, u));
31 assert_se(l == prefixlen);
32 }
33 }
34
35 int main(int argc, char *argv[]) {
36 test_in_addr_prefix_from_string("", AF_INET, -EINVAL, NULL, 0);
37 test_in_addr_prefix_from_string("/", AF_INET, -EINVAL, NULL, 0);
38 test_in_addr_prefix_from_string("/8", AF_INET, -EINVAL, NULL, 0);
39 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);
40 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);
41 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);
42 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);
43 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);
44 test_in_addr_prefix_from_string("1.2.3.4/33", AF_INET, -ERANGE, NULL, 0);
45 test_in_addr_prefix_from_string("1.2.3.4/-1", AF_INET, -ERANGE, NULL, 0);
46 test_in_addr_prefix_from_string("::1", AF_INET, -EINVAL, NULL, 0);
47
48 test_in_addr_prefix_from_string("", AF_INET6, -EINVAL, NULL, 0);
49 test_in_addr_prefix_from_string("/", AF_INET6, -EINVAL, NULL, 0);
50 test_in_addr_prefix_from_string("/8", AF_INET6, -EINVAL, NULL, 0);
51 test_in_addr_prefix_from_string("::1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128);
52 test_in_addr_prefix_from_string("::1/0", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 0);
53 test_in_addr_prefix_from_string("::1/1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 1);
54 test_in_addr_prefix_from_string("::1/2", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 2);
55 test_in_addr_prefix_from_string("::1/32", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 32);
56 test_in_addr_prefix_from_string("::1/33", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 33);
57 test_in_addr_prefix_from_string("::1/64", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 64);
58 test_in_addr_prefix_from_string("::1/128", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128);
59 test_in_addr_prefix_from_string("::1/129", AF_INET6, -ERANGE, NULL, 0);
60 test_in_addr_prefix_from_string("::1/-1", AF_INET6, -ERANGE, NULL, 0);
61
62 return 0;
63 }