]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/test-sd-device.c
Merge pull request #25389 from fbuihuu/update-test-for-opensuse
[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 #include <unistd.h>
6
7 #include "device-enumerator-private.h"
8 #include "device-internal.h"
9 #include "device-private.h"
10 #include "device-util.h"
11 #include "errno-util.h"
12 #include "fd-util.h"
13 #include "hashmap.h"
14 #include "nulstr-util.h"
15 #include "path-util.h"
16 #include "rm-rf.h"
17 #include "stat-util.h"
18 #include "string-util.h"
19 #include "tests.h"
20 #include "time-util.h"
21 #include "tmpfile-util.h"
22 #include "udev-util.h"
23
24 static void test_sd_device_one(sd_device *d) {
25 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
26 const char *syspath, *sysname, *subsystem = NULL, *devname, *val;
27 bool is_block = false;
28 dev_t devnum;
29 usec_t usec;
30 int ifindex, r;
31
32 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
33 assert_se(path_startswith(syspath, "/sys"));
34 assert_se(sd_device_get_sysname(d, &sysname) >= 0);
35
36 log_info("%s(%s)", __func__, syspath);
37
38 assert_se(sd_device_new_from_syspath(&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 assert_se(sd_device_new_from_path(&dev, syspath) >= 0);
44 assert_se(sd_device_get_syspath(dev, &val) >= 0);
45 assert_se(streq(syspath, val));
46 dev = sd_device_unref(dev);
47
48 r = sd_device_get_ifindex(d, &ifindex);
49 if (r >= 0) {
50 assert_se(ifindex > 0);
51
52 r = sd_device_new_from_ifindex(&dev, ifindex);
53 if (r == -ENODEV)
54 log_device_warning_errno(d, r,
55 "Failed to create sd-device object from ifindex %i. "
56 "Maybe running on a non-host network namespace.", ifindex);
57 else {
58 assert_se(r >= 0);
59 assert_se(sd_device_get_syspath(dev, &val) >= 0);
60 assert_se(streq(syspath, val));
61 dev = sd_device_unref(dev);
62 }
63
64 /* This does not require the interface really exists on the network namespace.
65 * Hence, this should always succeed. */
66 assert_se(sd_device_new_from_ifname(&dev, sysname) >= 0);
67 assert_se(sd_device_get_syspath(dev, &val) >= 0);
68 assert_se(streq(syspath, val));
69 dev = sd_device_unref(dev);
70 } else
71 assert_se(r == -ENOENT);
72
73 r = sd_device_get_subsystem(d, &subsystem);
74 if (r < 0)
75 assert_se(r == -ENOENT);
76 else if (!streq(subsystem, "gpio")) { /* Unfortunately, there exist /sys/class/gpio and /sys/bus/gpio.
77 * Hence, sd_device_new_from_subsystem_sysname() and
78 * sd_device_new_from_device_id() may not work as expected. */
79 const char *name, *id;
80
81 if (streq(subsystem, "drivers"))
82 name = strjoina(d->driver_subsystem, ":", sysname);
83 else
84 name = sysname;
85 assert_se(sd_device_new_from_subsystem_sysname(&dev, subsystem, name) >= 0);
86 assert_se(sd_device_get_syspath(dev, &val) >= 0);
87 assert_se(streq(syspath, val));
88 dev = sd_device_unref(dev);
89
90 /* The device ID depends on subsystem. */
91 assert_se(device_get_device_id(d, &id) >= 0);
92 r = sd_device_new_from_device_id(&dev, id);
93 if (r == -ENODEV && ifindex > 0)
94 log_device_warning_errno(d, r,
95 "Failed to create sd-device object from device ID \"%s\". "
96 "Maybe running on a non-host network namespace.", id);
97 else {
98 assert_se(r >= 0);
99 assert_se(sd_device_get_syspath(dev, &val) >= 0);
100 assert_se(streq(syspath, val));
101 dev = sd_device_unref(dev);
102 }
103
104 /* These require udev database, and reading database requires device ID. */
105 r = sd_device_get_is_initialized(d);
106 if (r > 0) {
107 r = sd_device_get_usec_since_initialized(d, &usec);
108 assert_se((r >= 0 && usec > 0) || r == -ENODATA);
109 } else
110 assert(r == 0);
111
112 r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val);
113 assert_se(r >= 0 || r == -ENOENT);
114 }
115
116 is_block = streq_ptr(subsystem, "block");
117
118 r = sd_device_get_devname(d, &devname);
119 if (r >= 0) {
120 r = sd_device_new_from_devname(&dev, devname);
121 if (r >= 0) {
122 assert_se(sd_device_get_syspath(dev, &val) >= 0);
123 assert_se(streq(syspath, val));
124 dev = sd_device_unref(dev);
125 } else
126 assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
127
128 r = sd_device_new_from_path(&dev, devname);
129 if (r >= 0) {
130 assert_se(sd_device_get_syspath(dev, &val) >= 0);
131 assert_se(streq(syspath, val));
132 dev = sd_device_unref(dev);
133
134 _cleanup_close_ int fd = -1;
135 fd = sd_device_open(d, O_CLOEXEC| O_NONBLOCK | (is_block ? O_RDONLY : O_NOCTTY | O_PATH));
136 assert_se(fd >= 0 || ERRNO_IS_PRIVILEGE(fd));
137 } else
138 assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
139 } else
140 assert_se(r == -ENOENT);
141
142 r = sd_device_get_devnum(d, &devnum);
143 if (r >= 0) {
144 _cleanup_free_ char *p = NULL;
145
146 assert_se(major(devnum) > 0);
147
148 assert_se(sd_device_new_from_devnum(&dev, is_block ? 'b' : 'c', devnum) >= 0);
149 assert_se(sd_device_get_syspath(dev, &val) >= 0);
150 assert_se(streq(syspath, val));
151 dev = sd_device_unref(dev);
152
153 assert_se(asprintf(&p, "/dev/%s/%u:%u", is_block ? "block" : "char", major(devnum), minor(devnum)) >= 0);
154 assert_se(sd_device_new_from_devname(&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
159 assert_se(sd_device_new_from_path(&dev, p) >= 0);
160 assert_se(sd_device_get_syspath(dev, &val) >= 0);
161 assert_se(streq(syspath, val));
162 dev = sd_device_unref(dev);
163 } else
164 assert_se(r == -ENOENT);
165
166 assert_se(sd_device_get_devpath(d, &val) >= 0);
167
168 r = sd_device_get_devtype(d, &val);
169 assert_se(r >= 0 || r == -ENOENT);
170
171 r = sd_device_get_driver(d, &val);
172 assert_se(r >= 0 || r == -ENOENT);
173
174 r = sd_device_get_sysnum(d, &val);
175 if (r >= 0) {
176 assert_se(val > sysname);
177 assert_se(val < sysname + strlen(sysname));
178 assert_se(in_charset(val, DIGITS));
179 assert_se(!ascii_isdigit(val[-1]));
180 } else
181 assert_se(r == -ENOENT);
182
183 r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
184 assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
185
186 if (r > 0) {
187 unsigned x;
188
189 assert_se(device_get_sysattr_unsigned(d, "name_assign_type", NULL) >= 0);
190 assert_se(device_get_sysattr_unsigned(d, "name_assign_type", &x) >= 0);
191 }
192 }
193
194 TEST(sd_device_enumerator_devices) {
195 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
196 sd_device *d;
197
198 assert_se(sd_device_enumerator_new(&e) >= 0);
199 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
200 /* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes
201 * disappear during running this test. Let's exclude them here for stability. */
202 assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
203 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
204 /* On CentOS CI, systemd-networkd-tests.py may be running when this test is invoked. The networkd
205 * test creates and removes many network interfaces, and may interfere with this test. */
206 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0);
207 FOREACH_DEVICE(e, d)
208 test_sd_device_one(d);
209 }
210
211 TEST(sd_device_enumerator_subsystems) {
212 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
213 sd_device *d;
214
215 assert_se(sd_device_enumerator_new(&e) >= 0);
216 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
217 FOREACH_SUBSYSTEM(e, d)
218 test_sd_device_one(d);
219 }
220
221 static void test_sd_device_enumerator_filter_subsystem_one(
222 const char *subsystem,
223 Hashmap *h,
224 unsigned *ret_n_new_dev,
225 unsigned *ret_n_removed_dev) {
226
227 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
228 unsigned n_new_dev = 0, n_removed_dev = 0;
229 sd_device *d;
230
231 assert_se(sd_device_enumerator_new(&e) >= 0);
232 assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
233 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
234
235 FOREACH_DEVICE(e, d) {
236 const char *syspath;
237 sd_device *t;
238
239 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
240 t = hashmap_remove(h, syspath);
241
242 if (!t) {
243 log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath);
244 n_new_dev++;
245 }
246
247 assert_se(!sd_device_unref(t));
248 }
249
250 HASHMAP_FOREACH(d, h) {
251 const char *syspath;
252
253 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
254 log_warning("Device removed: subsystem:%s syspath:%s", subsystem, syspath);
255 n_removed_dev++;
256
257 assert_se(!sd_device_unref(d));
258 }
259
260 hashmap_free(h);
261
262 *ret_n_new_dev = n_new_dev;
263 *ret_n_removed_dev = n_removed_dev;
264 }
265
266 static bool test_sd_device_enumerator_filter_subsystem_trial(void) {
267 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
268 _cleanup_(hashmap_freep) Hashmap *subsystems;
269 unsigned n_new_dev = 0, n_removed_dev = 0;
270 sd_device *d;
271 Hashmap *h;
272 char *s;
273
274 assert_se(subsystems = hashmap_new(&string_hash_ops));
275 assert_se(sd_device_enumerator_new(&e) >= 0);
276 /* See comments in TEST(sd_device_enumerator_devices). */
277 assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
278 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
279 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0);
280
281 FOREACH_DEVICE(e, d) {
282 const char *syspath, *subsystem;
283 int r;
284
285 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
286
287 r = sd_device_get_subsystem(d, &subsystem);
288 assert_se(r >= 0 || r == -ENOENT);
289 if (r < 0)
290 continue;
291
292 h = hashmap_get(subsystems, subsystem);
293 if (!h) {
294 char *str;
295 assert_se(str = strdup(subsystem));
296 assert_se(h = hashmap_new(&string_hash_ops));
297 assert_se(hashmap_put(subsystems, str, h) >= 0);
298 }
299
300 assert_se(hashmap_put(h, syspath, d) >= 0);
301 assert_se(sd_device_ref(d));
302
303 log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
304 }
305
306 while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
307 unsigned n, m;
308
309 test_sd_device_enumerator_filter_subsystem_one(s, TAKE_PTR(h), &n, &m);
310 free(s);
311
312 n_new_dev += n;
313 n_removed_dev += m;
314 }
315
316 if (n_new_dev > 0)
317 log_warning("%u new devices are found in re-scan", n_new_dev);
318 if (n_removed_dev > 0)
319 log_warning("%u devices removed in re-scan", n_removed_dev);
320
321 return n_new_dev + n_removed_dev == 0;
322 }
323
324 static bool test_sd_device_enumerator_filter_subsystem_trial_many(void) {
325 for (unsigned i = 0; i < 20; i++) {
326 log_debug("%s(): trial %u", __func__, i);
327 if (test_sd_device_enumerator_filter_subsystem_trial())
328 return true;
329 }
330
331 return false;
332 }
333
334 static int on_inotify(sd_event_source *s, const struct inotify_event *event, void *userdata) {
335 if (test_sd_device_enumerator_filter_subsystem_trial_many())
336 return sd_event_exit(sd_event_source_get_event(s), 0);
337
338 return sd_event_exit(sd_event_source_get_event(s), -EBUSY);
339 }
340
341 TEST(sd_device_enumerator_filter_subsystem) {
342 /* The test test_sd_device_enumerator_filter_subsystem_trial() is quite racy. Let's run the function
343 * several times after the udev queue becomes empty. */
344
345 if (!udev_available() || (access("/run/udev", F_OK) < 0 && errno == ENOENT)) {
346 assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
347 return;
348 }
349
350 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
351 assert_se(sd_event_default(&event) >= 0);
352 assert_se(sd_event_add_inotify(event, NULL, "/run/udev" , IN_DELETE, on_inotify, NULL) >= 0);
353
354 if (udev_queue_is_empty() == 0) {
355 log_debug("udev queue is not empty, waiting for all queued events to be processed.");
356 assert_se(sd_event_loop(event) >= 0);
357 } else
358 assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
359 }
360
361 TEST(sd_device_enumerator_add_match_sysattr) {
362 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
363 sd_device *dev;
364 int ifindex;
365
366 assert_se(sd_device_enumerator_new(&e) >= 0);
367 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
368 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", true) >= 0);
369 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true) >= 0);
370 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "hoge", true) >= 0);
371 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "foo", true) >= 0);
372 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "bar", false) >= 0);
373 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "baz", false) >= 0);
374
375 dev = sd_device_enumerator_get_device_first(e);
376 assert_se(dev);
377 assert_se(sd_device_get_ifindex(dev, &ifindex) >= 0);
378 assert_se(ifindex == 1);
379
380 assert_se(!sd_device_enumerator_get_device_next(e));
381 }
382
383 TEST(sd_device_enumerator_add_match_property) {
384 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
385 sd_device *dev;
386 int ifindex;
387
388 assert_se(sd_device_enumerator_new(&e) >= 0);
389 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
390 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", true) >= 0);
391 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true) >= 0);
392 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", "1*") >= 0);
393 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", "hoge") >= 0);
394 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", NULL) >= 0);
395 assert_se(sd_device_enumerator_add_match_property(e, "AAAAA", "BBBB") >= 0);
396 assert_se(sd_device_enumerator_add_match_property(e, "FOOOO", NULL) >= 0);
397
398 dev = sd_device_enumerator_get_device_first(e);
399 assert_se(dev);
400 assert_se(sd_device_get_ifindex(dev, &ifindex) >= 0);
401 assert_se(ifindex == 1);
402 }
403
404 static void check_parent_match(sd_device_enumerator *e, sd_device *dev) {
405 const char *syspath;
406 bool found = false;
407 sd_device *d;
408
409 assert_se(sd_device_get_syspath(dev, &syspath) >= 0);
410
411 FOREACH_DEVICE(e, d) {
412 const char *s;
413
414 assert_se(sd_device_get_syspath(d, &s) >= 0);
415 if (streq(s, syspath)) {
416 found = true;
417 break;
418 }
419 }
420
421 if (!found) {
422 log_device_debug(dev, "not enumerated, already removed??");
423 /* If the original device not found, then the device should be already removed. */
424 assert_se(access(syspath, F_OK) < 0);
425 assert_se(errno == ENOENT);
426 }
427 }
428
429 TEST(sd_device_enumerator_add_match_parent) {
430 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
431 sd_device *dev;
432 int r;
433
434 assert_se(sd_device_enumerator_new(&e) >= 0);
435 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
436 /* See comments in TEST(sd_device_enumerator_devices). */
437 assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
438 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
439 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0);
440
441 if (!slow_tests_enabled())
442 assert_se(sd_device_enumerator_add_match_subsystem(e, "block", true) >= 0);
443
444 FOREACH_DEVICE(e, dev) {
445 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *p = NULL;
446 const char *syspath;
447 sd_device *parent;
448
449 assert_se(sd_device_get_syspath(dev, &syspath) >= 0);
450
451 r = sd_device_get_parent(dev, &parent);
452 if (r < 0) {
453 assert_se(ERRNO_IS_DEVICE_ABSENT(r));
454 continue;
455 }
456
457 log_debug("> %s", syspath);
458
459 assert_se(sd_device_enumerator_new(&p) >= 0);
460 assert_se(sd_device_enumerator_allow_uninitialized(p) >= 0);
461 assert_se(sd_device_enumerator_add_match_parent(p, parent) >= 0);
462
463 check_parent_match(p, dev);
464
465 /* If the device does not have subsystem, then it is not enumerated. */
466 r = sd_device_get_subsystem(parent, NULL);
467 if (r < 0) {
468 assert_se(r == -ENOENT);
469 continue;
470 }
471 check_parent_match(p, parent);
472 }
473 }
474
475 TEST(sd_device_get_child) {
476 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
477 sd_device *dev;
478 int r;
479
480 assert_se(sd_device_enumerator_new(&e) >= 0);
481 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
482 /* See comments in TEST(sd_device_enumerator_devices). */
483 assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
484 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
485 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0);
486
487 if (!slow_tests_enabled())
488 assert_se(sd_device_enumerator_add_match_subsystem(e, "block", true) >= 0);
489
490 FOREACH_DEVICE(e, dev) {
491 const char *syspath, *parent_syspath, *expected_suffix, *suffix;
492 sd_device *parent, *child;
493 bool found = false;
494
495 assert_se(sd_device_get_syspath(dev, &syspath) >= 0);
496
497 r = sd_device_get_parent(dev, &parent);
498 if (r < 0) {
499 assert_se(ERRNO_IS_DEVICE_ABSENT(r));
500 continue;
501 }
502
503 assert_se(sd_device_get_syspath(parent, &parent_syspath) >= 0);
504 assert_se(expected_suffix = path_startswith(syspath, parent_syspath));
505
506 log_debug("> %s", syspath);
507
508 FOREACH_DEVICE_CHILD_WITH_SUFFIX(parent, child, suffix) {
509 const char *s;
510
511 assert_se(child);
512 assert_se(suffix);
513
514 if (!streq(suffix, expected_suffix))
515 continue;
516
517 assert_se(sd_device_get_syspath(child, &s) >= 0);
518 assert_se(streq(s, syspath));
519 found = true;
520 break;
521 }
522 assert_se(found);
523 }
524 }
525
526 TEST(sd_device_new_from_nulstr) {
527 const char *devlinks =
528 "/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
529 "/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
530 "/dev/disk/by-label/Arch\\x20Linux\0"
531 "/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
532 "/dev/disk/by-partlabel/Arch\\x20Linux\0"
533 "\0";
534
535 _cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL;
536 _cleanup_free_ char *nulstr_copy = NULL;
537 const char *nulstr;
538 size_t len;
539
540 assert_se(sd_device_new_from_syspath(&device, "/sys/class/net/lo") >= 0);
541
542 /* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
543 * test for generating and parsing nulstr. For issue #17772. */
544 NULSTR_FOREACH(devlink, devlinks) {
545 log_device_info(device, "setting devlink: %s", devlink);
546 assert_se(device_add_devlink(device, devlink) >= 0);
547 assert_se(set_contains(device->devlinks, devlink));
548 }
549
550 /* For issue #23799 */
551 assert_se(device_add_tag(device, "tag1", false) >= 0);
552 assert_se(device_add_tag(device, "tag2", false) >= 0);
553 assert_se(device_add_tag(device, "current-tag1", true) >= 0);
554 assert_se(device_add_tag(device, "current-tag2", true) >= 0);
555
556 /* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
557 assert_se(device_add_property_internal(device, "SEQNUM", "1") >= 0);
558 assert_se(device_add_property_internal(device, "ACTION", "change") >= 0);
559
560 assert_se(device_get_properties_nulstr(device, &nulstr, &len) >= 0);
561 assert_se(nulstr_copy = newdup(char, nulstr, len));
562 assert_se(device_new_from_nulstr(&from_nulstr, nulstr_copy, len) >= 0);
563
564 assert_se(sd_device_has_tag(from_nulstr, "tag1") == 1);
565 assert_se(sd_device_has_tag(from_nulstr, "tag2") == 1);
566 assert_se(sd_device_has_tag(from_nulstr, "current-tag1") == 1);
567 assert_se(sd_device_has_tag(from_nulstr, "current-tag2") == 1);
568 assert_se(sd_device_has_current_tag(from_nulstr, "tag1") == 0);
569 assert_se(sd_device_has_current_tag(from_nulstr, "tag2") == 0);
570 assert_se(sd_device_has_current_tag(from_nulstr, "current-tag1") == 1);
571 assert_se(sd_device_has_current_tag(from_nulstr, "current-tag2") == 1);
572
573 NULSTR_FOREACH(devlink, devlinks) {
574 log_device_info(from_nulstr, "checking devlink: %s", devlink);
575 assert_se(set_contains(from_nulstr->devlinks, devlink));
576 }
577 }
578
579 TEST(sd_device_new_from_path) {
580 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
581 _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
582 sd_device *dev;
583 int r;
584
585 assert_se(mkdtemp_malloc("/tmp/test-sd-device.XXXXXXX", &tmpdir) >= 0);
586
587 assert_se(sd_device_enumerator_new(&e) >= 0);
588 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
589 assert_se(sd_device_enumerator_add_match_subsystem(e, "block", true) >= 0);
590 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
591 assert_se(sd_device_enumerator_add_match_property(e, "DEVNAME", "*") >= 0);
592
593 FOREACH_DEVICE(e, dev) {
594 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
595 const char *syspath, *devpath, *sysname, *s;
596 _cleanup_free_ char *path = NULL;
597
598 assert_se(sd_device_get_sysname(dev, &sysname) >= 0);
599
600 log_debug("%s(%s)", __func__, sysname);
601
602 assert_se(sd_device_get_syspath(dev, &syspath) >= 0);
603 assert_se(sd_device_new_from_path(&d, syspath) >= 0);
604 assert_se(sd_device_get_syspath(d, &s) >= 0);
605 assert_se(streq(s, syspath));
606 d = sd_device_unref(d);
607
608 assert_se(sd_device_get_devname(dev, &devpath) >= 0);
609 r = sd_device_new_from_path(&d, devpath);
610 if (r >= 0) {
611 assert_se(sd_device_get_syspath(d, &s) >= 0);
612 assert_se(streq(s, syspath));
613 d = sd_device_unref(d);
614 } else
615 assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
616
617 assert_se(path = path_join(tmpdir, sysname));
618 assert_se(symlink(syspath, path) >= 0);
619 assert_se(sd_device_new_from_path(&d, path) >= 0);
620 assert_se(sd_device_get_syspath(d, &s) >= 0);
621 assert_se(streq(s, syspath));
622 }
623 }
624
625 static void test_devname_from_devnum_one(const char *path) {
626 _cleanup_free_ char *resolved = NULL;
627 struct stat st;
628
629 log_debug("> %s", path);
630
631 if (stat(path, &st) < 0) {
632 assert_se(errno == ENOENT);
633 log_notice("Path %s not found, skipping test", path);
634 return;
635 }
636
637 assert_se(devname_from_devnum(st.st_mode, st.st_rdev, &resolved) >= 0);
638 assert_se(path_equal(path, resolved));
639 resolved = mfree(resolved);
640 assert_se(devname_from_stat_rdev(&st, &resolved) >= 0);
641 assert_se(path_equal(path, resolved));
642 }
643
644 TEST(devname_from_devnum) {
645 test_devname_from_devnum_one("/dev/null");
646 test_devname_from_devnum_one("/dev/zero");
647 test_devname_from_devnum_one("/dev/full");
648 test_devname_from_devnum_one("/dev/random");
649 test_devname_from_devnum_one("/dev/urandom");
650 test_devname_from_devnum_one("/dev/tty");
651
652 if (is_device_node("/run/systemd/inaccessible/blk") > 0) {
653 test_devname_from_devnum_one("/run/systemd/inaccessible/chr");
654 test_devname_from_devnum_one("/run/systemd/inaccessible/blk");
655 }
656 }
657
658 DEFINE_TEST_MAIN(LOG_INFO);