Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : :
3 : : #include <fnmatch.h>
4 : : #include <netinet/in.h>
5 : :
6 : : #include "log.h"
7 : : #include "strv.h"
8 : : #include "in-addr-util.h"
9 : :
10 : 96 : static void test_in_addr_prefix_from_string_one(
11 : : const char *p,
12 : : int family,
13 : : int ret,
14 : : const union in_addr_union *u,
15 : : unsigned char prefixlen,
16 : : int ret_refuse,
17 : : unsigned char prefixlen_refuse,
18 : : int ret_legacy,
19 : : unsigned char prefixlen_legacy) {
20 : :
21 : : union in_addr_union q;
22 : : unsigned char l;
23 : : int f, r;
24 : :
25 : 96 : r = in_addr_prefix_from_string(p, family, &q, &l);
26 [ - + ]: 96 : assert_se(r == ret);
27 : :
28 [ + + ]: 96 : if (r < 0)
29 : 44 : return;
30 : :
31 [ - + ]: 52 : assert_se(in_addr_equal(family, &q, u));
32 [ - + ]: 52 : assert_se(l == prefixlen);
33 : :
34 : 52 : r = in_addr_prefix_from_string_auto(p, &f, &q, &l);
35 [ - + ]: 52 : assert_se(r >= 0);
36 : :
37 [ - + ]: 52 : assert_se(f == family);
38 [ - + ]: 52 : assert_se(in_addr_equal(family, &q, u));
39 [ - + ]: 52 : assert_se(l == prefixlen);
40 : :
41 : 52 : r = in_addr_prefix_from_string_auto_internal(p, PREFIXLEN_REFUSE, &f, &q, &l);
42 [ - + ]: 52 : assert_se(r == ret_refuse);
43 : :
44 [ + + ]: 52 : if (r >= 0) {
45 [ - + ]: 44 : assert_se(f == family);
46 [ - + ]: 44 : assert_se(in_addr_equal(family, &q, u));
47 [ - + ]: 44 : assert_se(l == prefixlen_refuse);
48 : : }
49 : :
50 : 52 : r = in_addr_prefix_from_string_auto_internal(p, PREFIXLEN_LEGACY, &f, &q, &l);
51 [ - + ]: 52 : assert_se(r == ret_legacy);
52 : :
53 [ + - ]: 52 : if (r >= 0) {
54 [ - + ]: 52 : assert_se(f == family);
55 [ - + ]: 52 : assert_se(in_addr_equal(family, &q, u));
56 [ - + ]: 52 : assert_se(l == prefixlen_legacy);
57 : : }
58 : : }
59 : :
60 : 4 : static void test_in_addr_prefix_from_string(void) {
61 : 4 : test_in_addr_prefix_from_string_one("", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0);
62 : 4 : test_in_addr_prefix_from_string_one("/", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0);
63 : 4 : test_in_addr_prefix_from_string_one("/8", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0);
64 : 4 : test_in_addr_prefix_from_string_one("1.2.3.4", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, -ENOANO, 0, 0, 8);
65 : 4 : test_in_addr_prefix_from_string_one("1.2.3.4/0", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 0, 0, 0, 0, 0);
66 : 4 : test_in_addr_prefix_from_string_one("1.2.3.4/1", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 1, 0, 1, 0, 1);
67 : 4 : test_in_addr_prefix_from_string_one("1.2.3.4/2", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 2, 0, 2, 0, 2);
68 : 4 : test_in_addr_prefix_from_string_one("1.2.3.4/32", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, 0, 32, 0, 32);
69 : 4 : test_in_addr_prefix_from_string_one("1.2.3.4/33", AF_INET, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0);
70 : 4 : test_in_addr_prefix_from_string_one("1.2.3.4/-1", AF_INET, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0);
71 : 4 : test_in_addr_prefix_from_string_one("::1", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0);
72 : :
73 : 4 : test_in_addr_prefix_from_string_one("", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0);
74 : 4 : test_in_addr_prefix_from_string_one("/", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0);
75 : 4 : test_in_addr_prefix_from_string_one("/8", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0);
76 : 4 : test_in_addr_prefix_from_string_one("::1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, -ENOANO, 0, 0, 0);
77 : 4 : test_in_addr_prefix_from_string_one("::1/0", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 0, 0, 0, 0, 0);
78 : 4 : test_in_addr_prefix_from_string_one("::1/1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 1, 0, 1, 0, 1);
79 : 4 : test_in_addr_prefix_from_string_one("::1/2", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 2, 0, 2, 0, 2);
80 : 4 : test_in_addr_prefix_from_string_one("::1/32", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 32, 0, 32, 0, 32);
81 : 4 : test_in_addr_prefix_from_string_one("::1/33", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 33, 0, 33, 0, 33);
82 : 4 : test_in_addr_prefix_from_string_one("::1/64", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 64, 0, 64, 0, 64);
83 : 4 : test_in_addr_prefix_from_string_one("::1/128", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, 0, 128, 0, 128);
84 : 4 : test_in_addr_prefix_from_string_one("::1/129", AF_INET6, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0);
85 : 4 : test_in_addr_prefix_from_string_one("::1/-1", AF_INET6, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0);
86 : 4 : }
87 : :
88 : 36 : static void test_in_addr_prefix_to_string_valid(int family, const char *p) {
89 : 36 : _cleanup_free_ char *str = NULL;
90 : : union in_addr_union u;
91 : : unsigned char l;
92 : :
93 [ + - ]: 36 : log_info("/* %s */", p);
94 : :
95 [ - + ]: 36 : assert_se(in_addr_prefix_from_string(p, family, &u, &l) >= 0);
96 [ - + ]: 36 : assert_se(in_addr_prefix_to_string(family, &u, l, &str) >= 0);
97 [ - + ]: 36 : assert_se(streq(str, p));
98 : 36 : }
99 : :
100 : 16 : static void test_in_addr_prefix_to_string_unoptimized(int family, const char *p) {
101 : 16 : _cleanup_free_ char *str1 = NULL, *str2 = NULL;
102 : : union in_addr_union u1, u2;
103 : : unsigned char len1, len2;
104 : :
105 [ + - ]: 16 : log_info("/* %s */", p);
106 : :
107 [ - + ]: 16 : assert_se(in_addr_prefix_from_string(p, family, &u1, &len1) >= 0);
108 [ - + ]: 16 : assert_se(in_addr_prefix_to_string(family, &u1, len1, &str1) >= 0);
109 [ - + ]: 16 : assert_se(in_addr_prefix_from_string(str1, family, &u2, &len2) >= 0);
110 [ - + ]: 16 : assert_se(in_addr_prefix_to_string(family, &u2, len2, &str2) >= 0);
111 : :
112 [ - + ]: 16 : assert_se(streq(str1, str2));
113 [ - + ]: 16 : assert_se(len1 == len2);
114 [ - + ]: 16 : assert_se(in_addr_equal(family, &u1, &u2) > 0);
115 : 16 : }
116 : :
117 : 4 : static void test_in_addr_prefix_to_string(void) {
118 : 4 : test_in_addr_prefix_to_string_valid(AF_INET, "0.0.0.0/32");
119 : 4 : test_in_addr_prefix_to_string_valid(AF_INET, "1.2.3.4/0");
120 : 4 : test_in_addr_prefix_to_string_valid(AF_INET, "1.2.3.4/24");
121 : 4 : test_in_addr_prefix_to_string_valid(AF_INET, "1.2.3.4/32");
122 : 4 : test_in_addr_prefix_to_string_valid(AF_INET, "255.255.255.255/32");
123 : :
124 : 4 : test_in_addr_prefix_to_string_valid(AF_INET6, "::1/128");
125 : 4 : test_in_addr_prefix_to_string_valid(AF_INET6, "fd00:abcd::1/64");
126 : 4 : test_in_addr_prefix_to_string_valid(AF_INET6, "fd00:abcd::1234:1/64");
127 : 4 : test_in_addr_prefix_to_string_valid(AF_INET6, "1111:2222:3333:4444:5555:6666:7777:8888/128");
128 : :
129 : 4 : test_in_addr_prefix_to_string_unoptimized(AF_INET, "0.0.0.0");
130 : 4 : test_in_addr_prefix_to_string_unoptimized(AF_INET, "192.168.0.1");
131 : :
132 : 4 : test_in_addr_prefix_to_string_unoptimized(AF_INET6, "fd00:0000:0000:0000:0000:0000:0000:0001/64");
133 : 4 : test_in_addr_prefix_to_string_unoptimized(AF_INET6, "fd00:1111::0000:2222:3333:4444:0001/64");
134 : 4 : }
135 : :
136 : 4 : static void test_in_addr_random_prefix(void) {
137 : 8 : _cleanup_free_ char *str = NULL;
138 : : union in_addr_union a;
139 : :
140 [ - + ]: 4 : assert_se(in_addr_from_string(AF_INET, "192.168.10.1", &a) >= 0);
141 : :
142 [ - + ]: 4 : assert_se(in_addr_random_prefix(AF_INET, &a, 31, 32) >= 0);
143 [ - + ]: 4 : assert_se(in_addr_to_string(AF_INET, &a, &str) >= 0);
144 [ - + ]: 4 : assert_se(STR_IN_SET(str, "192.168.10.0", "192.168.10.1"));
145 : 4 : str = mfree(str);
146 : :
147 [ - + ]: 4 : assert_se(in_addr_random_prefix(AF_INET, &a, 24, 26) >= 0);
148 [ - + ]: 4 : assert_se(in_addr_to_string(AF_INET, &a, &str) >= 0);
149 [ - + ]: 4 : assert_se(startswith(str, "192.168.10."));
150 : 4 : str = mfree(str);
151 : :
152 [ - + ]: 4 : assert_se(in_addr_random_prefix(AF_INET, &a, 16, 24) >= 0);
153 [ - + ]: 4 : assert_se(in_addr_to_string(AF_INET, &a, &str) >= 0);
154 [ - + ]: 4 : assert_se(fnmatch("192.168.[0-9]*.0", str, 0) == 0);
155 : 4 : str = mfree(str);
156 : :
157 [ - + ]: 4 : assert_se(in_addr_random_prefix(AF_INET, &a, 8, 24) >= 0);
158 [ - + ]: 4 : assert_se(in_addr_to_string(AF_INET, &a, &str) >= 0);
159 [ - + ]: 4 : assert_se(fnmatch("192.[0-9]*.[0-9]*.0", str, 0) == 0);
160 : 4 : str = mfree(str);
161 : :
162 [ - + ]: 4 : assert_se(in_addr_random_prefix(AF_INET, &a, 8, 16) >= 0);
163 [ - + ]: 4 : assert_se(in_addr_to_string(AF_INET, &a, &str) >= 0);
164 [ - + ]: 4 : assert_se(fnmatch("192.[0-9]*.0.0", str, 0) == 0);
165 : 4 : str = mfree(str);
166 : :
167 [ - + ]: 4 : assert_se(in_addr_from_string(AF_INET6, "fd00::1", &a) >= 0);
168 : :
169 [ - + ]: 4 : assert_se(in_addr_random_prefix(AF_INET6, &a, 16, 64) >= 0);
170 [ - + ]: 4 : assert_se(in_addr_to_string(AF_INET6, &a, &str) >= 0);
171 [ - + ]: 4 : assert_se(startswith(str, "fd00:"));
172 : 4 : str = mfree(str);
173 : :
174 [ - + ]: 4 : assert_se(in_addr_random_prefix(AF_INET6, &a, 8, 16) >= 0);
175 [ - + ]: 4 : assert_se(in_addr_to_string(AF_INET6, &a, &str) >= 0);
176 [ - + ]: 4 : assert_se(fnmatch("fd??::", str, 0) == 0);
177 : 4 : str = mfree(str);
178 : 4 : }
179 : :
180 : 4 : int main(int argc, char *argv[]) {
181 : 4 : test_in_addr_prefix_from_string();
182 : 4 : test_in_addr_random_prefix();
183 : 4 : test_in_addr_prefix_to_string();
184 : :
185 : 4 : return 0;
186 : : }
|