1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <arpa/inet.h>
4 #include <sys/param.h>
5
6 #include "sd-device.h"
7
8 #include "alloc-util.h"
9 #include "dhcp-lease-internal.h"
10 #include "ether-addr-util.h"
11 #include "hostname-setup.h"
12 #include "network-internal.h"
13 #include "networkd-address.h"
14 #include "networkd-manager.h"
15 #include "networkd-route-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "tests.h"
19
test_deserialize_in_addr(void)20 static void test_deserialize_in_addr(void) {
21 _cleanup_free_ struct in_addr *addresses = NULL;
22 _cleanup_free_ struct in6_addr *addresses6 = NULL;
23 union in_addr_union a, b, c, d, e, f;
24 int size;
25 const char *addresses_string = "192.168.0.1 0:0:0:0:0:FFFF:204.152.189.116 192.168.0.2 ::1 192.168.0.3 1:0:0:0:0:0:0:8";
26
27 assert_se(in_addr_from_string(AF_INET, "0:0:0:0:0:FFFF:204.152.189.116", &a) < 0);
28 assert_se(in_addr_from_string(AF_INET6, "192.168.0.1", &d) < 0);
29
30 assert_se(in_addr_from_string(AF_INET, "192.168.0.1", &a) >= 0);
31 assert_se(in_addr_from_string(AF_INET, "192.168.0.2", &b) >= 0);
32 assert_se(in_addr_from_string(AF_INET, "192.168.0.3", &c) >= 0);
33 assert_se(in_addr_from_string(AF_INET6, "0:0:0:0:0:FFFF:204.152.189.116", &d) >= 0);
34 assert_se(in_addr_from_string(AF_INET6, "::1", &e) >= 0);
35 assert_se(in_addr_from_string(AF_INET6, "1:0:0:0:0:0:0:8", &f) >= 0);
36
37 assert_se((size = deserialize_in_addrs(&addresses, addresses_string)) >= 0);
38 assert_se(size == 3);
39 assert_se(in4_addr_equal(&a.in, &addresses[0]));
40 assert_se(in4_addr_equal(&b.in, &addresses[1]));
41 assert_se(in4_addr_equal(&c.in, &addresses[2]));
42
43 assert_se((size = deserialize_in6_addrs(&addresses6, addresses_string)) >= 0);
44 assert_se(size == 3);
45 assert_se(in6_addr_equal(&d.in6, &addresses6[0]));
46 assert_se(in6_addr_equal(&e.in6, &addresses6[1]));
47 assert_se(in6_addr_equal(&f.in6, &addresses6[2]));
48 }
49
test_deserialize_dhcp_routes(void)50 static void test_deserialize_dhcp_routes(void) {
51 size_t size;
52
53 {
54 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
55 assert_se(deserialize_dhcp_routes(&routes, &size, "") >= 0);
56 assert_se(size == 0);
57 }
58
59 {
60 /* no errors */
61 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
62 const char *routes_string = "192.168.0.0/16,192.168.0.1 10.1.2.0/24,10.1.2.1 0.0.0.0/0,10.0.1.1";
63
64 assert_se(deserialize_dhcp_routes(&routes, &size, routes_string) >= 0);
65
66 assert_se(size == 3);
67 assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
68 assert_se(routes[0].gw_addr.s_addr == inet_addr("192.168.0.1"));
69 assert_se(routes[0].dst_prefixlen == 16);
70
71 assert_se(routes[1].dst_addr.s_addr == inet_addr("10.1.2.0"));
72 assert_se(routes[1].gw_addr.s_addr == inet_addr("10.1.2.1"));
73 assert_se(routes[1].dst_prefixlen == 24);
74
75 assert_se(routes[2].dst_addr.s_addr == inet_addr("0.0.0.0"));
76 assert_se(routes[2].gw_addr.s_addr == inet_addr("10.0.1.1"));
77 assert_se(routes[2].dst_prefixlen == 0);
78 }
79
80 {
81 /* error in second word */
82 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
83 const char *routes_string = "192.168.0.0/16,192.168.0.1 10.1.2.0#24,10.1.2.1 0.0.0.0/0,10.0.1.1";
84
85 assert_se(deserialize_dhcp_routes(&routes, &size, routes_string) >= 0);
86
87 assert_se(size == 2);
88 assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
89 assert_se(routes[0].gw_addr.s_addr == inet_addr("192.168.0.1"));
90 assert_se(routes[0].dst_prefixlen == 16);
91
92 assert_se(routes[1].dst_addr.s_addr == inet_addr("0.0.0.0"));
93 assert_se(routes[1].gw_addr.s_addr == inet_addr("10.0.1.1"));
94 assert_se(routes[1].dst_prefixlen == 0);
95 }
96
97 {
98 /* error in every word */
99 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
100 const char *routes_string = "192.168.0.0/55,192.168.0.1 10.1.2.0#24,10.1.2.1 0.0.0.0/0,10.0.1.X";
101
102 assert_se(deserialize_dhcp_routes(&routes, &size, routes_string) >= 0);
103 assert_se(size == 0);
104 }
105 }
106
test_route_tables_one(Manager * manager,const char * name,uint32_t number)107 static void test_route_tables_one(Manager *manager, const char *name, uint32_t number) {
108 _cleanup_free_ char *str = NULL, *expected = NULL, *num_str = NULL;
109 uint32_t t;
110
111 if (!STR_IN_SET(name, "default", "main", "local")) {
112 assert_se(streq(hashmap_get(manager->route_table_names_by_number, UINT32_TO_PTR(number)), name));
113 assert_se(PTR_TO_UINT32(hashmap_get(manager->route_table_numbers_by_name, name)) == number);
114 }
115
116 assert_se(asprintf(&expected, "%s(%" PRIu32 ")", name, number) >= 0);
117 assert_se(manager_get_route_table_to_string(manager, number, &str) >= 0);
118 assert_se(streq(str, expected));
119
120 assert_se(manager_get_route_table_from_string(manager, name, &t) >= 0);
121 assert_se(t == number);
122
123 assert_se(asprintf(&num_str, "%" PRIu32, number) >= 0);
124 assert_se(manager_get_route_table_from_string(manager, num_str, &t) >= 0);
125 assert_se(t == number);
126 }
127
test_route_tables(Manager * manager)128 static void test_route_tables(Manager *manager) {
129 assert_se(config_parse_route_table_names("manager", "filename", 1, "section", 1, "RouteTable", 0, "hoge:123 foo:456 aaa:111", manager, manager) >= 0);
130 assert_se(config_parse_route_table_names("manager", "filename", 1, "section", 1, "RouteTable", 0, "bbb:11111 ccc:22222", manager, manager) >= 0);
131 assert_se(config_parse_route_table_names("manager", "filename", 1, "section", 1, "RouteTable", 0, "ddd:22222", manager, manager) >= 0);
132
133 test_route_tables_one(manager, "hoge", 123);
134 test_route_tables_one(manager, "foo", 456);
135 test_route_tables_one(manager, "aaa", 111);
136 test_route_tables_one(manager, "bbb", 11111);
137 test_route_tables_one(manager, "ccc", 22222);
138
139 assert_se(!hashmap_get(manager->route_table_numbers_by_name, "ddd"));
140
141 test_route_tables_one(manager, "default", 253);
142 test_route_tables_one(manager, "main", 254);
143 test_route_tables_one(manager, "local", 255);
144
145 assert_se(config_parse_route_table_names("manager", "filename", 1, "section", 1, "RouteTable", 0, "", manager, manager) >= 0);
146 assert_se(!manager->route_table_names_by_number);
147 assert_se(!manager->route_table_numbers_by_name);
148
149 /* Invalid pairs */
150 assert_se(config_parse_route_table_names("manager", "filename", 1, "section", 1, "RouteTable", 0, "main:123 default:333 local:999", manager, manager) >= 0);
151 assert_se(config_parse_route_table_names("manager", "filename", 1, "section", 1, "RouteTable", 0, "1234:321 :567 hoge:foo aaa:-888", manager, manager) >= 0);
152 assert_se(!manager->route_table_names_by_number);
153 assert_se(!manager->route_table_numbers_by_name);
154
155 test_route_tables_one(manager, "default", 253);
156 test_route_tables_one(manager, "main", 254);
157 test_route_tables_one(manager, "local", 255);
158 }
159
test_load_config(Manager * manager)160 static int test_load_config(Manager *manager) {
161 int r;
162 /* TODO: should_reload, is false if the config dirs do not exist, so
163 * so we can't do this test here, move it to a test for paths_check_timestamps
164 * directly
165 *
166 * assert_se(network_should_reload(manager) == true);
167 */
168
169 r = manager_load_config(manager);
170 if (r == -EPERM)
171 return r;
172 assert_se(r >= 0);
173
174 assert_se(manager_should_reload(manager) == false);
175
176 return 0;
177 }
178
address_equal(const Address * a1,const Address * a2)179 static bool address_equal(const Address *a1, const Address *a2) {
180 if (a1 == a2)
181 return true;
182
183 if (!a1 || !a2)
184 return false;
185
186 return address_compare_func(a1, a2) == 0;
187 }
188
test_address_equality(void)189 static void test_address_equality(void) {
190 _cleanup_(address_freep) Address *a1 = NULL, *a2 = NULL;
191
192 assert_se(address_new(&a1) >= 0);
193 assert_se(address_new(&a2) >= 0);
194
195 assert_se(address_equal(NULL, NULL));
196 assert_se(!address_equal(a1, NULL));
197 assert_se(!address_equal(NULL, a2));
198 assert_se(address_equal(a1, a2));
199
200 a1->family = AF_INET;
201 assert_se(!address_equal(a1, a2));
202
203 a2->family = AF_INET;
204 assert_se(address_equal(a1, a2));
205
206 assert_se(in_addr_from_string(AF_INET, "192.168.3.9", &a1->in_addr) >= 0);
207 assert_se(!address_equal(a1, a2));
208 assert_se(in_addr_from_string(AF_INET, "192.168.3.9", &a2->in_addr) >= 0);
209 assert_se(address_equal(a1, a2));
210 assert_se(in_addr_from_string(AF_INET, "192.168.3.10", &a1->in_addr_peer) >= 0);
211 assert_se(!address_equal(a1, a2));
212 assert_se(in_addr_from_string(AF_INET, "192.168.3.11", &a2->in_addr_peer) >= 0);
213 assert_se(!address_equal(a1, a2));
214 assert_se(in_addr_from_string(AF_INET, "192.168.3.10", &a2->in_addr_peer) >= 0);
215 assert_se(address_equal(a1, a2));
216 a1->prefixlen = 10;
217 assert_se(!address_equal(a1, a2));
218 a2->prefixlen = 10;
219 assert_se(address_equal(a1, a2));
220
221 a1->family = AF_INET6;
222 assert_se(!address_equal(a1, a2));
223
224 a2->family = AF_INET6;
225 assert_se(in_addr_from_string(AF_INET6, "2001:4ca0:4f01::2", &a1->in_addr) >= 0);
226 assert_se(in_addr_from_string(AF_INET6, "2001:4ca0:4f01::2", &a2->in_addr) >= 0);
227 assert_se(address_equal(a1, a2));
228
229 a2->prefixlen = 8;
230 assert_se(!address_equal(a1, a2));
231
232 a2->prefixlen = 10;
233 assert_se(in_addr_from_string(AF_INET6, "2001:4ca0:4f01::1", &a2->in_addr) >= 0);
234 assert_se(!address_equal(a1, a2));
235 }
236
test_dhcp_hostname_shorten_overlong(void)237 static void test_dhcp_hostname_shorten_overlong(void) {
238 int r;
239
240 {
241 /* simple hostname, no actions, no errors */
242 _cleanup_free_ char *shortened = NULL;
243 r = shorten_overlong("name1", &shortened);
244 assert_se(r == 0);
245 assert_se(streq("name1", shortened));
246 }
247
248 {
249 /* simple fqdn, no actions, no errors */
250 _cleanup_free_ char *shortened = NULL;
251 r = shorten_overlong("name1.example.com", &shortened);
252 assert_se(r == 0);
253 assert_se(streq("name1.example.com", shortened));
254 }
255
256 {
257 /* overlong fqdn, cut to first dot, no errors */
258 _cleanup_free_ char *shortened = NULL;
259 r = shorten_overlong("name1.test-dhcp-this-one-here-is-a-very-very-long-domain.example.com", &shortened);
260 assert_se(r == 1);
261 assert_se(streq("name1", shortened));
262 }
263
264 {
265 /* overlong hostname, cut to HOST_MAX_LEN, no errors */
266 _cleanup_free_ char *shortened = NULL;
267 r = shorten_overlong("test-dhcp-this-one-here-is-a-very-very-long-hostname-without-domainname", &shortened);
268 assert_se(r == 1);
269 assert_se(streq("test-dhcp-this-one-here-is-a-very-very-long-hostname-without-dom", shortened));
270 }
271
272 {
273 /* overlong fqdn, cut to first dot, empty result error */
274 _cleanup_free_ char *shortened = NULL;
275 r = shorten_overlong(".test-dhcp-this-one-here-is-a-very-very-long-hostname.example.com", &shortened);
276 assert_se(r == -EDOM);
277 assert_se(shortened == NULL);
278 }
279
280 }
281
main(void)282 int main(void) {
283 _cleanup_(manager_freep) Manager *manager = NULL;
284 int r;
285
286 test_setup_logging(LOG_INFO);
287
288 test_deserialize_in_addr();
289 test_deserialize_dhcp_routes();
290 test_address_equality();
291 test_dhcp_hostname_shorten_overlong();
292
293 assert_se(manager_new(&manager, /* test_mode = */ true) >= 0);
294 assert_se(manager_setup(manager) >= 0);
295
296 test_route_tables(manager);
297
298 r = test_load_config(manager);
299 if (r == -EPERM)
300 log_debug("Cannot load configuration, ignoring.");
301 else
302 assert_se(r == 0);
303
304 assert_se(manager_enumerate(manager) >= 0);
305 return 0;
306 }
307