Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "macro.h"
4 : #include "network-generator.h"
5 : #include "string-util.h"
6 :
7 22 : static void test_network_one(const char *ifname, const char *key, const char *value, const char *expected) {
8 22 : _cleanup_(context_clear) Context context = {};
9 22 : _cleanup_free_ char *output = NULL;
10 : Network *network;
11 :
12 22 : printf("# %s=%s\n", key, value);
13 22 : assert_se(parse_cmdline_item(key, value, &context) >= 0);
14 22 : assert_se(network = network_get(&context, ifname));
15 22 : assert_se(network_format(network, &output) >= 0);
16 22 : puts(output);
17 22 : assert_se(streq(output, expected));
18 22 : }
19 :
20 5 : static void test_network_two(const char *ifname,
21 : const char *key1, const char *value1,
22 : const char *key2, const char *value2,
23 : const char *expected) {
24 5 : _cleanup_(context_clear) Context context = {};
25 5 : _cleanup_free_ char *output = NULL;
26 : Network *network;
27 :
28 5 : printf("# %s=%s\n", key1, value1);
29 5 : printf("# %s=%s\n", key2, value2);
30 5 : assert_se(parse_cmdline_item(key1, value1, &context) >= 0);
31 5 : assert_se(parse_cmdline_item(key2, value2, &context) >= 0);
32 5 : assert_se(context_merge_networks(&context) >= 0);
33 5 : assert_se(network = network_get(&context, ifname));
34 5 : assert_se(network_format(network, &output) >= 0);
35 5 : puts(output);
36 5 : assert_se(streq(output, expected));
37 5 : }
38 :
39 1 : static void test_netdev_one(const char *ifname, const char *key, const char *value, const char *expected) {
40 1 : _cleanup_(context_clear) Context context = {};
41 1 : _cleanup_free_ char *output = NULL;
42 : NetDev *netdev;
43 :
44 1 : printf("# %s=%s\n", key, value);
45 1 : assert_se(parse_cmdline_item(key, value, &context) >= 0);
46 1 : assert_se(netdev = netdev_get(&context, ifname));
47 1 : assert_se(netdev_format(netdev, &output) >= 0);
48 1 : puts(output);
49 1 : assert_se(streq(output, expected));
50 1 : }
51 :
52 1 : static void test_link_one(const char *ifname, const char *key, const char *value, const char *expected) {
53 1 : _cleanup_(context_clear) Context context = {};
54 1 : _cleanup_free_ char *output = NULL;
55 : Link *link;
56 :
57 1 : printf("# %s=%s\n", key, value);
58 1 : assert_se(parse_cmdline_item(key, value, &context) >= 0);
59 1 : assert_se(link = link_get(&context, ifname));
60 1 : assert_se(link_format(link, &output) >= 0);
61 1 : puts(output);
62 1 : assert_se(streq(output, expected));
63 1 : }
64 :
65 1 : int main(int argc, char *argv[]) {
66 1 : test_network_one("", "ip", "dhcp6",
67 : "[Match]\n"
68 : "Name=*\n"
69 : "\n[Link]\n"
70 : "\n[Network]\n"
71 : "DHCP=ipv6\n"
72 : "\n[DHCP]\n"
73 : );
74 :
75 1 : test_network_one("eth0", "ip", "eth0:dhcp",
76 : "[Match]\n"
77 : "Name=eth0\n"
78 : "\n[Link]\n"
79 : "\n[Network]\n"
80 : "DHCP=ipv4\n"
81 : "\n[DHCP]\n"
82 : );
83 :
84 1 : test_network_one("eth0", "ip", "eth0:dhcp:1530",
85 : "[Match]\n"
86 : "Name=eth0\n"
87 : "\n[Link]\n"
88 : "MTUBytes=1530\n"
89 : "\n[Network]\n"
90 : "DHCP=ipv4\n"
91 : "\n[DHCP]\n"
92 : );
93 :
94 1 : test_network_one("eth0", "ip", "eth0:dhcp:1530:00:11:22:33:44:55",
95 : "[Match]\n"
96 : "Name=eth0\n"
97 : "\n[Link]\n"
98 : "MACAddress=00:11:22:33:44:55\n"
99 : "MTUBytes=1530\n"
100 : "\n[Network]\n"
101 : "DHCP=ipv4\n"
102 : "\n[DHCP]\n"
103 : );
104 :
105 1 : test_network_one("eth0", "ip", "192.168.0.10::192.168.0.1:255.255.255.0:hogehoge:eth0:on",
106 : "[Match]\n"
107 : "Name=eth0\n"
108 : "\n[Link]\n"
109 : "\n[Network]\n"
110 : "DHCP=yes\n"
111 : "\n[DHCP]\n"
112 : "Hostname=hogehoge\n"
113 : "\n[Address]\n"
114 : "Address=192.168.0.10/24\n"
115 : "\n[Route]\n"
116 : "Gateway=192.168.0.1\n"
117 : );
118 :
119 1 : test_network_one("eth0", "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on",
120 : "[Match]\n"
121 : "Name=eth0\n"
122 : "\n[Link]\n"
123 : "\n[Network]\n"
124 : "DHCP=yes\n"
125 : "\n[DHCP]\n"
126 : "Hostname=hogehoge\n"
127 : "\n[Address]\n"
128 : "Address=192.168.0.10/24\n"
129 : "Peer=192.168.0.2\n"
130 : "\n[Route]\n"
131 : "Gateway=192.168.0.1\n"
132 : );
133 :
134 1 : test_network_one("eth0", "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on:1530",
135 : "[Match]\n"
136 : "Name=eth0\n"
137 : "\n[Link]\n"
138 : "MTUBytes=1530\n"
139 : "\n[Network]\n"
140 : "DHCP=yes\n"
141 : "\n[DHCP]\n"
142 : "Hostname=hogehoge\n"
143 : "\n[Address]\n"
144 : "Address=192.168.0.10/24\n"
145 : "Peer=192.168.0.2\n"
146 : "\n[Route]\n"
147 : "Gateway=192.168.0.1\n"
148 : );
149 :
150 1 : test_network_one("eth0", "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on:1530:00:11:22:33:44:55",
151 : "[Match]\n"
152 : "Name=eth0\n"
153 : "\n[Link]\n"
154 : "MACAddress=00:11:22:33:44:55\n"
155 : "MTUBytes=1530\n"
156 : "\n[Network]\n"
157 : "DHCP=yes\n"
158 : "\n[DHCP]\n"
159 : "Hostname=hogehoge\n"
160 : "\n[Address]\n"
161 : "Address=192.168.0.10/24\n"
162 : "Peer=192.168.0.2\n"
163 : "\n[Route]\n"
164 : "Gateway=192.168.0.1\n"
165 : );
166 :
167 1 : test_network_one("eth0", "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on:10.10.10.10",
168 : "[Match]\n"
169 : "Name=eth0\n"
170 : "\n[Link]\n"
171 : "\n[Network]\n"
172 : "DHCP=yes\n"
173 : "DNS=10.10.10.10\n"
174 : "\n[DHCP]\n"
175 : "Hostname=hogehoge\n"
176 : "\n[Address]\n"
177 : "Address=192.168.0.10/24\n"
178 : "Peer=192.168.0.2\n"
179 : "\n[Route]\n"
180 : "Gateway=192.168.0.1\n"
181 : );
182 :
183 1 : test_network_one("eth0", "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on:10.10.10.10:10.10.10.11",
184 : "[Match]\n"
185 : "Name=eth0\n"
186 : "\n[Link]\n"
187 : "\n[Network]\n"
188 : "DHCP=yes\n"
189 : "DNS=10.10.10.10\n"
190 : "DNS=10.10.10.11\n"
191 : "\n[DHCP]\n"
192 : "Hostname=hogehoge\n"
193 : "\n[Address]\n"
194 : "Address=192.168.0.10/24\n"
195 : "Peer=192.168.0.2\n"
196 : "\n[Route]\n"
197 : "Gateway=192.168.0.1\n"
198 : );
199 :
200 1 : test_network_one("eth0", "ip", "[2001:1234:56:8f63::10]::[2001:1234:56:8f63::1]:64:hogehoge:eth0:on",
201 : "[Match]\n"
202 : "Name=eth0\n"
203 : "\n[Link]\n"
204 : "\n[Network]\n"
205 : "DHCP=yes\n"
206 : "\n[DHCP]\n"
207 : "Hostname=hogehoge\n"
208 : "\n[Address]\n"
209 : "Address=2001:1234:56:8f63::10/64\n"
210 : "\n[Route]\n"
211 : "Gateway=2001:1234:56:8f63::1\n"
212 : );
213 :
214 1 : test_network_one("eth0", "ip", "[2001:1234:56:8f63::10]:[2001:1234:56:8f63::2]:[2001:1234:56:8f63::1]:64:hogehoge:eth0:on",
215 : "[Match]\n"
216 : "Name=eth0\n"
217 : "\n[Link]\n"
218 : "\n[Network]\n"
219 : "DHCP=yes\n"
220 : "\n[DHCP]\n"
221 : "Hostname=hogehoge\n"
222 : "\n[Address]\n"
223 : "Address=2001:1234:56:8f63::10/64\n"
224 : "Peer=2001:1234:56:8f63::2\n"
225 : "\n[Route]\n"
226 : "Gateway=2001:1234:56:8f63::1\n"
227 : );
228 :
229 1 : test_network_one("", "rd.route", "10.1.2.3/16:10.0.2.3",
230 : "[Match]\n"
231 : "Name=*\n"
232 : "\n[Link]\n"
233 : "\n[Network]\n"
234 : "\n[DHCP]\n"
235 : "\n[Route]\n"
236 : "Destination=10.1.2.3/16\n"
237 : "Gateway=10.0.2.3\n"
238 : );
239 :
240 1 : test_network_one("eth0", "rd.route", "10.1.2.3/16:10.0.2.3:eth0",
241 : "[Match]\n"
242 : "Name=eth0\n"
243 : "\n[Link]\n"
244 : "\n[Network]\n"
245 : "\n[DHCP]\n"
246 : "\n[Route]\n"
247 : "Destination=10.1.2.3/16\n"
248 : "Gateway=10.0.2.3\n"
249 : );
250 :
251 1 : test_network_one("", "nameserver", "10.1.2.3",
252 : "[Match]\n"
253 : "Name=*\n"
254 : "\n[Link]\n"
255 : "\n[Network]\n"
256 : "DNS=10.1.2.3\n"
257 : "\n[DHCP]\n"
258 : );
259 :
260 1 : test_network_one("", "rd.peerdns", "0",
261 : "[Match]\n"
262 : "Name=*\n"
263 : "\n[Link]\n"
264 : "\n[Network]\n"
265 : "\n[DHCP]\n"
266 : "UseDNS=no\n"
267 : );
268 :
269 1 : test_network_one("", "rd.peerdns", "1",
270 : "[Match]\n"
271 : "Name=*\n"
272 : "\n[Link]\n"
273 : "\n[Network]\n"
274 : "\n[DHCP]\n"
275 : "UseDNS=yes\n"
276 : );
277 :
278 1 : test_network_one("eth0", "vlan", "vlan99:eth0",
279 : "[Match]\n"
280 : "Name=eth0\n"
281 : "\n[Link]\n"
282 : "\n[Network]\n"
283 : "VLAN=vlan99\n"
284 : "\n[DHCP]\n"
285 : );
286 :
287 1 : test_network_one("eth0", "bridge", "bridge99:eth0,eth1",
288 : "[Match]\n"
289 : "Name=eth0\n"
290 : "\n[Link]\n"
291 : "\n[Network]\n"
292 : "Bridge=bridge99\n"
293 : "\n[DHCP]\n"
294 : );
295 :
296 1 : test_network_one("eth1", "bridge", "bridge99:eth0,eth1",
297 : "[Match]\n"
298 : "Name=eth1\n"
299 : "\n[Link]\n"
300 : "\n[Network]\n"
301 : "Bridge=bridge99\n"
302 : "\n[DHCP]\n"
303 : );
304 :
305 1 : test_network_one("eth0", "bond", "bond99:eth0,eth1",
306 : "[Match]\n"
307 : "Name=eth0\n"
308 : "\n[Link]\n"
309 : "\n[Network]\n"
310 : "Bond=bond99\n"
311 : "\n[DHCP]\n"
312 : );
313 :
314 1 : test_network_one("eth1", "bond", "bond99:eth0,eth1::1530",
315 : "[Match]\n"
316 : "Name=eth1\n"
317 : "\n[Link]\n"
318 : "\n[Network]\n"
319 : "Bond=bond99\n"
320 : "\n[DHCP]\n"
321 : );
322 :
323 1 : test_netdev_one("bond99", "bond", "bond99:eth0,eth1::1530",
324 : "[NetDev]\n"
325 : "Kind=bond\n"
326 : "Name=bond99\n"
327 : "MTUBytes=1530\n"
328 : );
329 :
330 1 : test_link_one("hogehoge", "ifname", "hogehoge:00:11:22:33:44:55",
331 : "[Match]\n"
332 : "MACAddress=00:11:22:33:44:55\n"
333 : "\n[Link]\n"
334 : "Name=hogehoge\n"
335 : );
336 :
337 1 : test_network_two("eth0",
338 : "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on:10.10.10.10:10.10.10.11",
339 : "rd.route", "10.1.2.3/16:10.0.2.3",
340 : "[Match]\n"
341 : "Name=eth0\n"
342 : "\n[Link]\n"
343 : "\n[Network]\n"
344 : "DHCP=yes\n"
345 : "DNS=10.10.10.10\n"
346 : "DNS=10.10.10.11\n"
347 : "\n[DHCP]\n"
348 : "Hostname=hogehoge\n"
349 : "\n[Address]\n"
350 : "Address=192.168.0.10/24\n"
351 : "Peer=192.168.0.2\n"
352 : "\n[Route]\n"
353 : "Destination=10.1.2.3/16\n"
354 : "Gateway=10.0.2.3\n"
355 : "\n[Route]\n"
356 : "Gateway=192.168.0.1\n"
357 : );
358 :
359 1 : test_network_two("eth0",
360 : "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on",
361 : "nameserver", "10.1.2.3",
362 : "[Match]\n"
363 : "Name=eth0\n"
364 : "\n[Link]\n"
365 : "\n[Network]\n"
366 : "DHCP=yes\n"
367 : "DNS=10.1.2.3\n"
368 : "\n[DHCP]\n"
369 : "Hostname=hogehoge\n"
370 : "\n[Address]\n"
371 : "Address=192.168.0.10/24\n"
372 : "Peer=192.168.0.2\n"
373 : "\n[Route]\n"
374 : "Gateway=192.168.0.1\n"
375 : );
376 :
377 1 : test_network_two("eth0",
378 : "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on:10.10.10.10:10.10.10.11",
379 : "nameserver", "10.1.2.3",
380 : "[Match]\n"
381 : "Name=eth0\n"
382 : "\n[Link]\n"
383 : "\n[Network]\n"
384 : "DHCP=yes\n"
385 : "DNS=10.10.10.10\n"
386 : "DNS=10.10.10.11\n"
387 : "DNS=10.1.2.3\n"
388 : "\n[DHCP]\n"
389 : "Hostname=hogehoge\n"
390 : "\n[Address]\n"
391 : "Address=192.168.0.10/24\n"
392 : "Peer=192.168.0.2\n"
393 : "\n[Route]\n"
394 : "Gateway=192.168.0.1\n"
395 : );
396 :
397 1 : test_network_two("eth0",
398 : "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on:10.10.10.10:10.10.10.11",
399 : "rd.peerdns", "1",
400 : "[Match]\n"
401 : "Name=eth0\n"
402 : "\n[Link]\n"
403 : "\n[Network]\n"
404 : "DHCP=yes\n"
405 : "DNS=10.10.10.10\n"
406 : "DNS=10.10.10.11\n"
407 : "\n[DHCP]\n"
408 : "Hostname=hogehoge\n"
409 : "UseDNS=yes\n"
410 : "\n[Address]\n"
411 : "Address=192.168.0.10/24\n"
412 : "Peer=192.168.0.2\n"
413 : "\n[Route]\n"
414 : "Gateway=192.168.0.1\n"
415 : );
416 :
417 1 : test_network_two("eth0",
418 : "ip", "192.168.0.10:192.168.0.2:192.168.0.1:255.255.255.0:hogehoge:eth0:on:10.10.10.10:10.10.10.11",
419 : "bridge", "bridge99:eth0,eth1",
420 : "[Match]\n"
421 : "Name=eth0\n"
422 : "\n[Link]\n"
423 : "\n[Network]\n"
424 : "DHCP=yes\n"
425 : "DNS=10.10.10.10\n"
426 : "DNS=10.10.10.11\n"
427 : "Bridge=bridge99\n"
428 : "\n[DHCP]\n"
429 : "Hostname=hogehoge\n"
430 : "\n[Address]\n"
431 : "Address=192.168.0.10/24\n"
432 : "Peer=192.168.0.2\n"
433 : "\n[Route]\n"
434 : "Gateway=192.168.0.1\n"
435 : );
436 :
437 1 : return 0;
438 : }
|