]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/test-network.c
3ffc4b95ab2b9811a4727362d5edb8bd9705e759
[thirdparty/systemd.git] / src / network / test-network.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 ***/
7
8 #include <sys/param.h>
9
10 #include "alloc-util.h"
11 #include "dhcp-lease-internal.h"
12 #include "hostname-util.h"
13 #include "network-internal.h"
14 #include "networkd-manager.h"
15 #include "string-util.h"
16 #include "udev-util.h"
17
18 static void test_deserialize_in_addr(void) {
19 _cleanup_free_ struct in_addr *addresses = NULL;
20 _cleanup_free_ struct in6_addr *addresses6 = NULL;
21 struct in_addr a, b, c;
22 struct in6_addr d, e, f;
23 int size;
24 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";
25
26 assert_se(inet_pton(AF_INET, "0:0:0:0:0:FFFF:204.152.189.116", &a) == 0);
27 assert_se(inet_pton(AF_INET6, "192.168.0.1", &d) == 0);
28
29 assert_se(inet_pton(AF_INET, "192.168.0.1", &a) == 1);
30 assert_se(inet_pton(AF_INET, "192.168.0.2", &b) == 1);
31 assert_se(inet_pton(AF_INET, "192.168.0.3", &c) == 1);
32 assert_se(inet_pton(AF_INET6, "0:0:0:0:0:FFFF:204.152.189.116", &d) == 1);
33 assert_se(inet_pton(AF_INET6, "::1", &e) == 1);
34 assert_se(inet_pton(AF_INET6, "1:0:0:0:0:0:0:8", &f) == 1);
35
36 assert_se((size = deserialize_in_addrs(&addresses, addresses_string)) >= 0);
37 assert_se(size == 3);
38 assert_se(!memcmp(&a, &addresses[0], sizeof(struct in_addr)));
39 assert_se(!memcmp(&b, &addresses[1], sizeof(struct in_addr)));
40 assert_se(!memcmp(&c, &addresses[2], sizeof(struct in_addr)));
41
42 assert_se((size = deserialize_in6_addrs(&addresses6, addresses_string)) >= 0);
43 assert_se(size == 3);
44 assert_se(!memcmp(&d, &addresses6[0], sizeof(struct in6_addr)));
45 assert_se(!memcmp(&e, &addresses6[1], sizeof(struct in6_addr)));
46 assert_se(!memcmp(&f, &addresses6[2], sizeof(struct in6_addr)));
47 }
48
49 static void test_deserialize_dhcp_routes(void) {
50 size_t size, allocated;
51
52 {
53 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
54 assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, "") >= 0);
55 assert_se(size == 0);
56 }
57
58 {
59 /* no errors */
60 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
61 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";
62
63 assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
64
65 assert_se(size == 3);
66 assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
67 assert_se(routes[0].gw_addr.s_addr == inet_addr("192.168.0.1"));
68 assert_se(routes[0].dst_prefixlen == 16);
69
70 assert_se(routes[1].dst_addr.s_addr == inet_addr("10.1.2.0"));
71 assert_se(routes[1].gw_addr.s_addr == inet_addr("10.1.2.1"));
72 assert_se(routes[1].dst_prefixlen == 24);
73
74 assert_se(routes[2].dst_addr.s_addr == inet_addr("0.0.0.0"));
75 assert_se(routes[2].gw_addr.s_addr == inet_addr("10.0.1.1"));
76 assert_se(routes[2].dst_prefixlen == 0);
77 }
78
79 {
80 /* error in second word */
81 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
82 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";
83
84 assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
85
86 assert_se(size == 2);
87 assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
88 assert_se(routes[0].gw_addr.s_addr == inet_addr("192.168.0.1"));
89 assert_se(routes[0].dst_prefixlen == 16);
90
91 assert_se(routes[1].dst_addr.s_addr == inet_addr("0.0.0.0"));
92 assert_se(routes[1].gw_addr.s_addr == inet_addr("10.0.1.1"));
93 assert_se(routes[1].dst_prefixlen == 0);
94 }
95
96 {
97 /* error in every word */
98 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
99 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";
100
101 assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
102 assert_se(size == 0);
103 }
104 }
105
106 static int test_load_config(Manager *manager) {
107 int r;
108 /* TODO: should_reload, is false if the config dirs do not exist, so
109 * so we can't do this test here, move it to a test for paths_check_timestamps
110 * directly
111 *
112 * assert_se(network_should_reload(manager) == true);
113 */
114
115 r = manager_load_config(manager);
116 if (r == -EPERM)
117 return r;
118 assert_se(r >= 0);
119
120 assert_se(manager_should_reload(manager) == false);
121
122 return 0;
123 }
124
125 static void test_network_get(Manager *manager, struct udev_device *loopback) {
126 Network *network;
127 const struct ether_addr mac = {};
128
129 /* let's assume that the test machine does not have a .network file
130 that applies to the loopback device... */
131 assert_se(network_get(manager, loopback, "lo", &mac, &network) == -ENOENT);
132 assert_se(!network);
133 }
134
135 static void test_address_equality(void) {
136 _cleanup_(address_freep) Address *a1 = NULL, *a2 = NULL;
137
138 assert_se(address_new(&a1) >= 0);
139 assert_se(address_new(&a2) >= 0);
140
141 assert_se(address_equal(NULL, NULL));
142 assert_se(!address_equal(a1, NULL));
143 assert_se(!address_equal(NULL, a2));
144 assert_se(address_equal(a1, a2));
145
146 a1->family = AF_INET;
147 assert_se(!address_equal(a1, a2));
148
149 a2->family = AF_INET;
150 assert_se(address_equal(a1, a2));
151
152 assert_se(inet_pton(AF_INET, "192.168.3.9", &a1->in_addr.in));
153 assert_se(!address_equal(a1, a2));
154 assert_se(inet_pton(AF_INET, "192.168.3.9", &a2->in_addr.in));
155 assert_se(address_equal(a1, a2));
156 assert_se(inet_pton(AF_INET, "192.168.3.10", &a1->in_addr_peer.in));
157 assert_se(address_equal(a1, a2));
158 assert_se(inet_pton(AF_INET, "192.168.3.11", &a2->in_addr_peer.in));
159 assert_se(address_equal(a1, a2));
160 a1->prefixlen = 10;
161 assert_se(!address_equal(a1, a2));
162 a2->prefixlen = 10;
163 assert_se(address_equal(a1, a2));
164
165 a1->family = AF_INET6;
166 assert_se(!address_equal(a1, a2));
167
168 a2->family = AF_INET6;
169 assert_se(inet_pton(AF_INET6, "2001:4ca0:4f01::2", &a1->in_addr.in6));
170 assert_se(inet_pton(AF_INET6, "2001:4ca0:4f01::2", &a2->in_addr.in6));
171 assert_se(address_equal(a1, a2));
172
173 a2->prefixlen = 8;
174 assert_se(address_equal(a1, a2));
175
176 assert_se(inet_pton(AF_INET6, "2001:4ca0:4f01::1", &a2->in_addr.in6));
177 assert_se(!address_equal(a1, a2));
178 }
179
180 static void test_dhcp_hostname_shorten_overlong(void) {
181 int r;
182
183 {
184 /* simple hostname, no actions, no errors */
185 _cleanup_free_ char *shortened = NULL;
186 r = shorten_overlong("name1", &shortened);
187 assert_se(r == 0);
188 assert_se(streq("name1", shortened));
189 }
190
191 {
192 /* simple fqdn, no actions, no errors */
193 _cleanup_free_ char *shortened = NULL;
194 r = shorten_overlong("name1.example.com", &shortened);
195 assert_se(r == 0);
196 assert_se(streq("name1.example.com", shortened));
197 }
198
199 {
200 /* overlong fqdn, cut to first dot, no errors */
201 _cleanup_free_ char *shortened = NULL;
202 r = shorten_overlong("name1.test-dhcp-this-one-here-is-a-very-very-long-domain.example.com", &shortened);
203 assert_se(r == 1);
204 assert_se(streq("name1", shortened));
205 }
206
207 {
208 /* overlong hostname, cut to HOST_MAX_LEN, no errors */
209 _cleanup_free_ char *shortened = NULL;
210 r = shorten_overlong("test-dhcp-this-one-here-is-a-very-very-long-hostname-without-domainname", &shortened);
211 assert_se(r == 1);
212 assert_se(streq("test-dhcp-this-one-here-is-a-very-very-long-hostname-without-dom", shortened));
213 }
214
215 {
216 /* overlong fqdn, cut to first dot, empty result error */
217 _cleanup_free_ char *shortened = NULL;
218 r = shorten_overlong(".test-dhcp-this-one-here-is-a-very-very-long-hostname.example.com", &shortened);
219 assert_se(r == -EDOM);
220 assert_se(shortened == NULL);
221 }
222
223 }
224
225 int main(void) {
226 _cleanup_(manager_freep) Manager *manager = NULL;
227 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
228 _cleanup_(udev_unrefp) struct udev *udev = NULL;
229 _cleanup_(udev_device_unrefp) struct udev_device *loopback = NULL;
230 int r;
231
232 test_deserialize_in_addr();
233 test_deserialize_dhcp_routes();
234 test_address_equality();
235 test_dhcp_hostname_shorten_overlong();
236
237 assert_se(sd_event_default(&event) >= 0);
238
239 assert_se(manager_new(&manager, event) >= 0);
240
241 r = test_load_config(manager);
242 if (r == -EPERM)
243 return EXIT_TEST_SKIP;
244
245 udev = udev_new();
246 assert_se(udev);
247
248 loopback = udev_device_new_from_syspath(udev, "/sys/class/net/lo");
249 assert_se(loopback);
250 assert_se(udev_device_get_ifindex(loopback) == 1);
251
252 test_network_get(manager, loopback);
253
254 assert_se(manager_rtnl_enumerate_links(manager) >= 0);
255 }