1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "in-addr-prefix-util.h"
4 #include "tests.h"
5
test_config_parse_in_addr_prefixes_one(int family,const union in_addr_union * addr,uint8_t prefixlen,Set ** prefixes)6 static void test_config_parse_in_addr_prefixes_one(int family, const union in_addr_union *addr, uint8_t prefixlen, Set **prefixes) {
7 _cleanup_free_ char *str = NULL;
8
9 assert_se(in_addr_prefix_to_string(family, addr, prefixlen, &str) >= 0);
10 assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, str, prefixes, NULL) >= 0);
11 }
12
test_config_parse_in_addr_prefixes(Set ** ret)13 static void test_config_parse_in_addr_prefixes(Set **ret) {
14 _cleanup_set_free_ Set *prefixes = NULL;
15
16 log_info("/* %s() */", __func__);
17
18 for (uint32_t i = 0; i < 256; i++) {
19 /* ipv4 link-local address */
20 test_config_parse_in_addr_prefixes_one(AF_INET, &(union in_addr_union) {
21 .in.s_addr = htobe32((UINT32_C(169) << 24) |
22 (UINT32_C(254) << 16) |
23 (i << 8)),
24 }, 24, &prefixes);
25
26 /* ipv6 multicast address */
27 test_config_parse_in_addr_prefixes_one(AF_INET6, &(union in_addr_union) {
28 .in6.s6_addr[0] = 0xff,
29 .in6.s6_addr[1] = i,
30 }, 16, &prefixes);
31
32 for (uint32_t j = 0; j < 256; j++) {
33 test_config_parse_in_addr_prefixes_one(AF_INET, &(union in_addr_union) {
34 .in.s_addr = htobe32((UINT32_C(169) << 24) |
35 (UINT32_C(254) << 16) |
36 (i << 8) | j),
37 }, 32, &prefixes);
38
39 test_config_parse_in_addr_prefixes_one(AF_INET6, &(union in_addr_union) {
40 .in6.s6_addr[0] = 0xff,
41 .in6.s6_addr[1] = i,
42 .in6.s6_addr[2] = j,
43 }, 24, &prefixes);
44 }
45 }
46
47 *ret = TAKE_PTR(prefixes);
48 }
49
test_in_addr_prefixes_reduce(Set * prefixes)50 static void test_in_addr_prefixes_reduce(Set *prefixes) {
51 log_info("/* %s() */", __func__);
52
53 assert_se(set_size(prefixes) == 2 * 256 * 257);
54 assert_se(!in_addr_prefixes_is_any(prefixes));
55
56 assert_se(in_addr_prefixes_reduce(prefixes) >= 0);
57 assert_se(set_size(prefixes) == 2 * 256);
58 assert_se(!in_addr_prefixes_is_any(prefixes));
59
60 assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, "link-local", &prefixes, NULL) == 0);
61 assert_se(set_size(prefixes) == 2 * 256 + 2);
62 assert_se(!in_addr_prefixes_is_any(prefixes));
63
64 assert_se(in_addr_prefixes_reduce(prefixes) >= 0);
65 assert_se(set_size(prefixes) == 256 + 2);
66 assert_se(!in_addr_prefixes_is_any(prefixes));
67
68 assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, "multicast", &prefixes, NULL) == 0);
69 assert_se(set_size(prefixes) == 256 + 4);
70 assert_se(!in_addr_prefixes_is_any(prefixes));
71
72 assert_se(in_addr_prefixes_reduce(prefixes) >= 0);
73 assert_se(set_size(prefixes) == 4);
74 assert_se(!in_addr_prefixes_is_any(prefixes));
75
76 assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, "any", &prefixes, NULL) == 0);
77 assert_se(set_size(prefixes) == 6);
78 assert_se(in_addr_prefixes_is_any(prefixes));
79
80 assert_se(in_addr_prefixes_reduce(prefixes) >= 0);
81 assert_se(set_size(prefixes) == 2);
82 assert_se(in_addr_prefixes_is_any(prefixes));
83 }
84
TEST(in_addr_prefixes)85 TEST(in_addr_prefixes) {
86 _cleanup_set_free_ Set *prefixes = NULL;
87
88 test_config_parse_in_addr_prefixes(&prefixes);
89 test_in_addr_prefixes_reduce(prefixes);
90 }
91
92 DEFINE_TEST_MAIN(LOG_DEBUG);
93