]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/test-sd-device.c
Merge pull request #24264 from DaanDeMeyer/mkosi-dirs
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <ctype.h>
4 #include <fcntl.h>
5
6 #include "device-enumerator-private.h"
7 #include "device-internal.h"
8 #include "device-private.h"
9 #include "device-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "hashmap.h"
13 #include "nulstr-util.h"
14 #include "path-util.h"
15 #include "string-util.h"
16 #include "tests.h"
17 #include "time-util.h"
18
19 static void test_sd_device_one(sd_device *d) {
20 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
21 const char *syspath, *sysname, *subsystem = NULL, *devname, *val;
22 bool is_block = false;
23 dev_t devnum;
24 usec_t usec;
25 int ifindex, r;
26
27 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
28 assert_se(path_startswith(syspath, "/sys"));
29 assert_se(sd_device_get_sysname(d, &sysname) >= 0);
30
31 log_info("%s(%s)", __func__, syspath);
32
33 assert_se(sd_device_new_from_syspath(&dev, syspath) >= 0);
34 assert_se(sd_device_get_syspath(dev, &val) >= 0);
35 assert_se(streq(syspath, val));
36 dev = sd_device_unref(dev);
37
38 assert_se(sd_device_new_from_path(&dev, syspath) >= 0);
39 assert_se(sd_device_get_syspath(dev, &val) >= 0);
40 assert_se(streq(syspath, val));
41 dev = sd_device_unref(dev);
42
43 r = sd_device_get_ifindex(d, &ifindex);
44 if (r >= 0) {
45 assert_se(ifindex > 0);
46
47 r = sd_device_new_from_ifindex(&dev, ifindex);
48 if (r == -ENODEV)
49 log_device_warning_errno(d, r,
50 "Failed to create sd-device object from ifindex %i. "
51 "Maybe running on a non-host network namespace.", ifindex);
52 else {
53 assert_se(r >= 0);
54 assert_se(sd_device_get_syspath(dev, &val) >= 0);
55 assert_se(streq(syspath, val));
56 dev = sd_device_unref(dev);
57 }
58
59 /* This does not require the interface really exists on the network namespace.
60 * Hence, this should always succeed. */
61 assert_se(sd_device_new_from_ifname(&dev, sysname) >= 0);
62 assert_se(sd_device_get_syspath(dev, &val) >= 0);
63 assert_se(streq(syspath, val));
64 dev = sd_device_unref(dev);
65 } else
66 assert_se(r == -ENOENT);
67
68 r = sd_device_get_subsystem(d, &subsystem);
69 if (r < 0)
70 assert_se(r == -ENOENT);
71 else if (!streq(subsystem, "gpio")) { /* Unfortunately, there exist /sys/class/gpio and /sys/bus/gpio.
72 * Hence, sd_device_new_from_subsystem_sysname() and
73 * sd_device_new_from_device_id() may not work as expected. */
74 const char *name, *id;
75
76 if (streq(subsystem, "drivers"))
77 name = strjoina(d->driver_subsystem, ":", sysname);
78 else
79 name = sysname;
80 assert_se(sd_device_new_from_subsystem_sysname(&dev, subsystem, name) >= 0);
81 assert_se(sd_device_get_syspath(dev, &val) >= 0);
82 assert_se(streq(syspath, val));
83 dev = sd_device_unref(dev);
84
85 /* The device ID depends on subsystem. */
86 assert_se(device_get_device_id(d, &id) >= 0);
87 r = sd_device_new_from_device_id(&dev, id);
88 if (r == -ENODEV && ifindex > 0)
89 log_device_warning_errno(d, r,
90 "Failed to create sd-device object from device ID \"%s\". "
91 "Maybe running on a non-host network namespace.", id);
92 else {
93 assert_se(r >= 0);
94 assert_se(sd_device_get_syspath(dev, &val) >= 0);
95 assert_se(streq(syspath, val));
96 dev = sd_device_unref(dev);
97 }
98
99 /* These require udev database, and reading database requires device ID. */
100 r = sd_device_get_is_initialized(d);
101 if (r > 0) {
102 r = sd_device_get_usec_since_initialized(d, &usec);
103 assert_se((r >= 0 && usec > 0) || r == -ENODATA);
104 } else
105 assert(r == 0);
106
107 r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val);
108 assert_se(r >= 0 || r == -ENOENT);
109 }
110
111 is_block = streq_ptr(subsystem, "block");
112
113 r = sd_device_get_devname(d, &devname);
114 if (r >= 0) {
115 r = sd_device_new_from_devname(&dev, devname);
116 if (r >= 0) {
117 assert_se(sd_device_get_syspath(dev, &val) >= 0);
118 assert_se(streq(syspath, val));
119 dev = sd_device_unref(dev);
120 } else
121 assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
122
123 r = sd_device_new_from_path(&dev, devname);
124 if (r >= 0) {
125 assert_se(sd_device_get_syspath(dev, &val) >= 0);
126 assert_se(streq(syspath, val));
127 dev = sd_device_unref(dev);
128
129 _cleanup_close_ int fd = -1;
130 fd = sd_device_open(d, O_CLOEXEC| O_NONBLOCK | (is_block ? O_RDONLY : O_NOCTTY | O_PATH));
131 assert_se(fd >= 0 || ERRNO_IS_PRIVILEGE(fd));
132 } else
133 assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
134 } else
135 assert_se(r == -ENOENT);
136
137 r = sd_device_get_devnum(d, &devnum);
138 if (r >= 0) {
139 _cleanup_free_ char *p = NULL;
140
141 assert_se(major(devnum) > 0);
142
143 assert_se(sd_device_new_from_devnum(&dev, is_block ? 'b' : 'c', devnum) >= 0);
144 assert_se(sd_device_get_syspath(dev, &val) >= 0);
145 assert_se(streq(syspath, val));
146 dev = sd_device_unref(dev);
147
148 assert_se(asprintf(&p, "/dev/%s/%u:%u", is_block ? "block" : "char", major(devnum), minor(devnum)) >= 0);
149 assert_se(sd_device_new_from_devname(&dev, p) >= 0);
150 assert_se(sd_device_get_syspath(dev, &val) >= 0);
151 assert_se(streq(syspath, val));
152 dev = sd_device_unref(dev);
153
154 assert_se(sd_device_new_from_path(&dev, p) >= 0);
155 assert_se(sd_device_get_syspath(dev, &val) >= 0);
156 assert_se(streq(syspath, val));
157 dev = sd_device_unref(dev);
158 } else
159 assert_se(r == -ENOENT);
160
161 assert_se(sd_device_get_devpath(d, &val) >= 0);
162
163 r = sd_device_get_devtype(d, &val);
164 assert_se(r >= 0 || r == -ENOENT);
165
166 r = sd_device_get_driver(d, &val);
167 assert_se(r >= 0 || r == -ENOENT);
168
169 r = sd_device_get_sysnum(d, &val);
170 if (r >= 0) {
171 assert_se(val > sysname);
172 assert_se(val < sysname + strlen(sysname));
173 assert_se(in_charset(val, DIGITS));
174 assert_se(!ascii_isdigit(val[-1]));
175 } else
176 assert_se(r == -ENOENT);
177
178 r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
179 assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
180 }
181
182 TEST(sd_device_enumerator_devices) {
183 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
184 sd_device *d;
185
186 assert_se(sd_device_enumerator_new(&e) >= 0);
187 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
188 /* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes
189 * disappear during running this test. Let's exclude them here for stability. */
190 assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
191 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
192 /* On CentOS CI, systemd-networkd-tests.py may be running when this test is invoked. The networkd
193 * test creates and removes many network interfaces, and may interfere with this test. */
194 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0);
195 FOREACH_DEVICE(e, d)
196 test_sd_device_one(d);
197 }
198
199 TEST(sd_device_enumerator_subsystems) {
200 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
201 sd_device *d;
202
203 assert_se(sd_device_enumerator_new(&e) >= 0);
204 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
205 FOREACH_SUBSYSTEM(e, d)
206 test_sd_device_one(d);
207 }
208
209 static void test_sd_device_enumerator_filter_subsystem_one(
210 const char *subsystem,
211 Hashmap *h,
212 unsigned *ret_n_new_dev,
213 unsigned *ret_n_removed_dev) {
214
215 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
216 unsigned n_new_dev = 0, n_removed_dev = 0;
217 sd_device *d;
218
219 assert_se(sd_device_enumerator_new(&e) >= 0);
220 assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
221 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
222
223 FOREACH_DEVICE(e, d) {
224 const char *syspath;
225 sd_device *t;
226
227 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
228 t = hashmap_remove(h, syspath);
229
230 if (!t) {
231 log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath);
232 n_new_dev++;
233 }
234
235 assert_se(!sd_device_unref(t));
236 }
237
238 HASHMAP_FOREACH(d, h) {
239 const char *syspath;
240
241 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
242 log_warning("Device removed: subsystem:%s syspath:%s", subsystem, syspath);
243 n_removed_dev++;
244
245 assert_se(!sd_device_unref(d));
246 }
247
248 hashmap_free(h);
249
250 *ret_n_new_dev = n_new_dev;
251 *ret_n_removed_dev = n_removed_dev;
252 }
253
254 TEST(sd_device_enumerator_filter_subsystem) {
255 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
256 _cleanup_(hashmap_freep) Hashmap *subsystems;
257 unsigned n_new_dev = 0, n_removed_dev = 0;
258 sd_device *d;
259 Hashmap *h;
260 char *s;
261
262 assert_se(subsystems = hashmap_new(&string_hash_ops));
263 assert_se(sd_device_enumerator_new(&e) >= 0);
264 /* See comments in TEST(sd_device_enumerator_devices). */
265 assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
266 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
267 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0);
268
269 FOREACH_DEVICE(e, d) {
270 const char *syspath, *subsystem;
271 int r;
272
273 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
274
275 r = sd_device_get_subsystem(d, &subsystem);
276 assert_se(r >= 0 || r == -ENOENT);
277 if (r < 0)
278 continue;
279
280 h = hashmap_get(subsystems, subsystem);
281 if (!h) {
282 char *str;
283 assert_se(str = strdup(subsystem));
284 assert_se(h = hashmap_new(&string_hash_ops));
285 assert_se(hashmap_put(subsystems, str, h) >= 0);
286 }
287
288 assert_se(hashmap_put(h, syspath, d) >= 0);
289 assert_se(sd_device_ref(d));
290
291 log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
292 }
293
294 while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
295 unsigned n, m;
296
297 test_sd_device_enumerator_filter_subsystem_one(s, TAKE_PTR(h), &n, &m);
298 free(s);
299
300 n_new_dev += n;
301 n_removed_dev += m;
302 }
303
304 if (n_new_dev > 0)
305 log_warning("%u new devices are found in re-scan", n_new_dev);
306 if (n_removed_dev > 0)
307 log_warning("%u devices removed in re-scan", n_removed_dev);
308
309 /* Assume that not so many devices are plugged or unplugged. */
310 assert_se(n_new_dev + n_removed_dev <= 10);
311 }
312
313 TEST(sd_device_enumerator_add_match_sysattr) {
314 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
315 sd_device *dev;
316 int ifindex;
317
318 assert_se(sd_device_enumerator_new(&e) >= 0);
319 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
320 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", true) >= 0);
321 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true) >= 0);
322 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "hoge", true) >= 0);
323 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "foo", true) >= 0);
324 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "bar", false) >= 0);
325 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "baz", false) >= 0);
326
327 dev = sd_device_enumerator_get_device_first(e);
328 assert_se(dev);
329 assert_se(sd_device_get_ifindex(dev, &ifindex) >= 0);
330 assert_se(ifindex == 1);
331
332 assert_se(!sd_device_enumerator_get_device_next(e));
333 }
334
335 TEST(sd_device_enumerator_add_match_property) {
336 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
337 sd_device *dev;
338 int ifindex;
339
340 assert_se(sd_device_enumerator_new(&e) >= 0);
341 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
342 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", true) >= 0);
343 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true) >= 0);
344 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", "1*") >= 0);
345 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", "hoge") >= 0);
346 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", NULL) >= 0);
347 assert_se(sd_device_enumerator_add_match_property(e, "AAAAA", "BBBB") >= 0);
348 assert_se(sd_device_enumerator_add_match_property(e, "FOOOO", NULL) >= 0);
349
350 dev = sd_device_enumerator_get_device_first(e);
351 assert_se(dev);
352 assert_se(sd_device_get_ifindex(dev, &ifindex) >= 0);
353 assert_se(ifindex == 1);
354 }
355
356 TEST(sd_device_new_from_nulstr) {
357 const char *devlinks =
358 "/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
359 "/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
360 "/dev/disk/by-label/Arch\\x20Linux\0"
361 "/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
362 "/dev/disk/by-partlabel/Arch\\x20Linux\0"
363 "\0";
364
365 _cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL;
366 _cleanup_free_ char *nulstr_copy = NULL;
367 const char *devlink, *nulstr;
368 size_t len;
369
370 assert_se(sd_device_new_from_syspath(&device, "/sys/class/net/lo") >= 0);
371
372 /* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
373 * test for generating and parsing nulstr. For issue #17772. */
374 NULSTR_FOREACH(devlink, devlinks) {
375 log_device_info(device, "setting devlink: %s", devlink);
376 assert_se(device_add_devlink(device, devlink) >= 0);
377 assert_se(set_contains(device->devlinks, devlink));
378 }
379
380 /* For issue #23799 */
381 assert_se(device_add_tag(device, "tag1", false) >= 0);
382 assert_se(device_add_tag(device, "tag2", false) >= 0);
383 assert_se(device_add_tag(device, "current-tag1", true) >= 0);
384 assert_se(device_add_tag(device, "current-tag2", true) >= 0);
385
386 /* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
387 assert_se(device_add_property_internal(device, "SEQNUM", "1") >= 0);
388 assert_se(device_add_property_internal(device, "ACTION", "change") >= 0);
389
390 assert_se(device_get_properties_nulstr(device, &nulstr, &len) >= 0);
391 assert_se(nulstr_copy = newdup(char, nulstr, len));
392 assert_se(device_new_from_nulstr(&from_nulstr, nulstr_copy, len) >= 0);
393
394 assert_se(sd_device_has_tag(from_nulstr, "tag1") == 1);
395 assert_se(sd_device_has_tag(from_nulstr, "tag2") == 1);
396 assert_se(sd_device_has_tag(from_nulstr, "current-tag1") == 1);
397 assert_se(sd_device_has_tag(from_nulstr, "current-tag2") == 1);
398 assert_se(sd_device_has_current_tag(from_nulstr, "tag1") == 0);
399 assert_se(sd_device_has_current_tag(from_nulstr, "tag2") == 0);
400 assert_se(sd_device_has_current_tag(from_nulstr, "current-tag1") == 1);
401 assert_se(sd_device_has_current_tag(from_nulstr, "current-tag2") == 1);
402
403 NULSTR_FOREACH(devlink, devlinks) {
404 log_device_info(from_nulstr, "checking devlink: %s", devlink);
405 assert_se(set_contains(from_nulstr->devlinks, devlink));
406 }
407 }
408
409 DEFINE_TEST_MAIN(LOG_INFO);