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