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