]>
Commit | Line | Data |
---|---|---|
0856e78d YW |
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | ||
3 | #include "in-addr-prefix-util.h" | |
4 | #include "tests.h" | |
5 | ||
61af1813 ZJS |
6 | static void test_in_addr_prefix_to_string_one(int f, const char *addr, unsigned prefixlen) { |
7 | union in_addr_union ua; | |
61af1813 | 8 | assert_se(in_addr_from_string(f, addr, &ua) >= 0); |
c71384a9 ZJS |
9 | |
10 | const char *r = IN_ADDR_PREFIX_TO_STRING(f, &ua, prefixlen); | |
11 | assert_se(r); | |
61af1813 ZJS |
12 | printf("%s: %s/%u == %s\n", __func__, addr, prefixlen, r); |
13 | assert_se(startswith(r, addr)); | |
c71384a9 | 14 | |
c79e88b3 IK |
15 | ASSERT_STREQ(r, IN_ADDR_PREFIX_TO_STRING(f, &ua, prefixlen)); |
16 | ASSERT_STREQ(IN_ADDR_PREFIX_TO_STRING(f, &ua, prefixlen), r); | |
61af1813 ZJS |
17 | } |
18 | ||
19 | TEST(in_addr_to_string_prefix) { | |
20 | test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 0); | |
21 | test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 1); | |
22 | test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 31); | |
23 | test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 32); | |
24 | test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 256); | |
25 | test_in_addr_prefix_to_string_one(AF_INET, "10.11.12.13", UINT_MAX); | |
26 | test_in_addr_prefix_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0); | |
27 | test_in_addr_prefix_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", UINT_MAX); | |
28 | test_in_addr_prefix_to_string_one(AF_INET6, "::1", 11); | |
29 | test_in_addr_prefix_to_string_one(AF_INET6, "fe80::", 33); | |
30 | } | |
31 | ||
0856e78d | 32 | static void test_config_parse_in_addr_prefixes_one(int family, const union in_addr_union *addr, uint8_t prefixlen, Set **prefixes) { |
c71384a9 ZJS |
33 | const char *str = IN_ADDR_PREFIX_TO_STRING(family, addr, prefixlen); |
34 | assert_se(str); | |
0856e78d | 35 | |
0856e78d | 36 | assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, str, prefixes, NULL) >= 0); |
c71384a9 | 37 | |
c79e88b3 IK |
38 | ASSERT_STREQ(str, IN_ADDR_PREFIX_TO_STRING(family, addr, prefixlen)); |
39 | ASSERT_STREQ(IN_ADDR_PREFIX_TO_STRING(family, addr, prefixlen), str); | |
0856e78d YW |
40 | } |
41 | ||
42 | static void test_config_parse_in_addr_prefixes(Set **ret) { | |
43 | _cleanup_set_free_ Set *prefixes = NULL; | |
44 | ||
45 | log_info("/* %s() */", __func__); | |
46 | ||
47 | for (uint32_t i = 0; i < 256; i++) { | |
48 | /* ipv4 link-local address */ | |
49 | test_config_parse_in_addr_prefixes_one(AF_INET, &(union in_addr_union) { | |
50 | .in.s_addr = htobe32((UINT32_C(169) << 24) | | |
51 | (UINT32_C(254) << 16) | | |
52 | (i << 8)), | |
53 | }, 24, &prefixes); | |
54 | ||
55 | /* ipv6 multicast address */ | |
56 | test_config_parse_in_addr_prefixes_one(AF_INET6, &(union in_addr_union) { | |
57 | .in6.s6_addr[0] = 0xff, | |
58 | .in6.s6_addr[1] = i, | |
59 | }, 16, &prefixes); | |
60 | ||
61 | for (uint32_t j = 0; j < 256; j++) { | |
62 | test_config_parse_in_addr_prefixes_one(AF_INET, &(union in_addr_union) { | |
63 | .in.s_addr = htobe32((UINT32_C(169) << 24) | | |
64 | (UINT32_C(254) << 16) | | |
65 | (i << 8) | j), | |
66 | }, 32, &prefixes); | |
67 | ||
68 | test_config_parse_in_addr_prefixes_one(AF_INET6, &(union in_addr_union) { | |
69 | .in6.s6_addr[0] = 0xff, | |
70 | .in6.s6_addr[1] = i, | |
71 | .in6.s6_addr[2] = j, | |
72 | }, 24, &prefixes); | |
73 | } | |
74 | } | |
75 | ||
76 | *ret = TAKE_PTR(prefixes); | |
77 | } | |
78 | ||
79 | static void test_in_addr_prefixes_reduce(Set *prefixes) { | |
80 | log_info("/* %s() */", __func__); | |
81 | ||
82 | assert_se(set_size(prefixes) == 2 * 256 * 257); | |
83 | assert_se(!in_addr_prefixes_is_any(prefixes)); | |
84 | ||
85 | assert_se(in_addr_prefixes_reduce(prefixes) >= 0); | |
86 | assert_se(set_size(prefixes) == 2 * 256); | |
87 | assert_se(!in_addr_prefixes_is_any(prefixes)); | |
88 | ||
89 | assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, "link-local", &prefixes, NULL) == 0); | |
90 | assert_se(set_size(prefixes) == 2 * 256 + 2); | |
91 | assert_se(!in_addr_prefixes_is_any(prefixes)); | |
92 | ||
93 | assert_se(in_addr_prefixes_reduce(prefixes) >= 0); | |
94 | assert_se(set_size(prefixes) == 256 + 2); | |
95 | assert_se(!in_addr_prefixes_is_any(prefixes)); | |
96 | ||
97 | assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, "multicast", &prefixes, NULL) == 0); | |
98 | assert_se(set_size(prefixes) == 256 + 4); | |
99 | assert_se(!in_addr_prefixes_is_any(prefixes)); | |
100 | ||
101 | assert_se(in_addr_prefixes_reduce(prefixes) >= 0); | |
102 | assert_se(set_size(prefixes) == 4); | |
103 | assert_se(!in_addr_prefixes_is_any(prefixes)); | |
104 | ||
105 | assert_se(config_parse_in_addr_prefixes("unit", "filename", 1, "Service", 1, "IPAddressAllow", 0, "any", &prefixes, NULL) == 0); | |
106 | assert_se(set_size(prefixes) == 6); | |
107 | assert_se(in_addr_prefixes_is_any(prefixes)); | |
108 | ||
109 | assert_se(in_addr_prefixes_reduce(prefixes) >= 0); | |
110 | assert_se(set_size(prefixes) == 2); | |
111 | assert_se(in_addr_prefixes_is_any(prefixes)); | |
112 | } | |
113 | ||
4f7452a8 | 114 | TEST(in_addr_prefixes) { |
0856e78d YW |
115 | _cleanup_set_free_ Set *prefixes = NULL; |
116 | ||
0856e78d YW |
117 | test_config_parse_in_addr_prefixes(&prefixes); |
118 | test_in_addr_prefixes_reduce(prefixes); | |
0856e78d | 119 | } |
4f7452a8 JJ |
120 | |
121 | DEFINE_TEST_MAIN(LOG_DEBUG); |