]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-device/test-sd-device.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
9380d34c
YW
2
3#include "device-enumerator-private.h"
4#include "device-private.h"
5#include "device-util.h"
68db4a0c 6#include "hashmap.h"
9380d34c
YW
7#include "string-util.h"
8#include "tests.h"
ca78ad1d 9#include "time-util.h"
9380d34c 10
af18705f
YW
11static void test_sd_device_one(sd_device *d) {
12 const char *syspath, *subsystem, *val;
13 dev_t devnum;
14 usec_t usec;
15 int i, r;
9380d34c 16
af18705f 17 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
ecbe9873 18
af18705f
YW
19 r = sd_device_get_subsystem(d, &subsystem);
20 assert_se(r >= 0 || r == -ENOENT);
9380d34c 21
af18705f
YW
22 r = sd_device_get_devtype(d, &val);
23 assert_se(r >= 0 || r == -ENOENT);
9380d34c 24
af18705f
YW
25 r = sd_device_get_devnum(d, &devnum);
26 assert_se((r >= 0 && major(devnum) > 0) || r == -ENOENT);
9380d34c 27
af18705f
YW
28 r = sd_device_get_ifindex(d, &i);
29 assert_se((r >= 0 && i > 0) || r == -ENOENT);
9380d34c 30
af18705f
YW
31 r = sd_device_get_driver(d, &val);
32 assert_se(r >= 0 || r == -ENOENT);
9380d34c 33
af18705f 34 assert_se(sd_device_get_devpath(d, &val) >= 0);
9380d34c 35
af18705f
YW
36 r = sd_device_get_devname(d, &val);
37 assert_se(r >= 0 || r == -ENOENT);
9380d34c 38
af18705f 39 assert_se(sd_device_get_sysname(d, &val) >= 0);
9380d34c 40
af18705f
YW
41 r = sd_device_get_sysnum(d, &val);
42 assert_se(r >= 0 || r == -ENOENT);
9380d34c 43
af18705f
YW
44 i = sd_device_get_is_initialized(d);
45 assert_se(i >= 0);
46 if (i > 0) {
47 r = sd_device_get_usec_since_initialized(d, &usec);
48 assert_se((r >= 0 && usec > 0) || r == -ENODATA);
49 }
9380d34c 50
af18705f
YW
51 r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
52 assert_se(r >= 0 || IN_SET(r, -ENOENT, -EINVAL));
9380d34c 53
af18705f
YW
54 r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val);
55 assert_se(r >= 0 || r == -ENOENT);
9380d34c 56
af18705f
YW
57 log_info("syspath:%s subsystem:%s initialized:%s", syspath, strna(subsystem), yes_no(i));
58}
9380d34c 59
af18705f
YW
60static void test_sd_device_enumerator_devices(void) {
61 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
62 sd_device *d;
9380d34c 63
af18705f
YW
64 log_info("/* %s */", __func__);
65
66 assert_se(sd_device_enumerator_new(&e) >= 0);
67 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
68 FOREACH_DEVICE(e, d)
69 test_sd_device_one(d);
70}
71
72static void test_sd_device_enumerator_subsystems(void) {
73 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
74 sd_device *d;
75
76 log_info("/* %s */", __func__);
77
78 assert_se(sd_device_enumerator_new(&e) >= 0);
79 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
80 FOREACH_SUBSYSTEM(e, d)
81 test_sd_device_one(d);
9380d34c
YW
82}
83
8ee08dc5 84static unsigned test_sd_device_enumerator_filter_subsystem_one(const char *subsystem, Hashmap *h) {
68db4a0c
YW
85 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
86 sd_device *d, *t;
8ee08dc5 87 unsigned n_new_dev = 0;
68db4a0c
YW
88
89 assert_se(sd_device_enumerator_new(&e) >= 0);
90 assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
91
92 FOREACH_DEVICE(e, d) {
93 const char *syspath;
94
95 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
8ee08dc5 96 t = hashmap_remove(h, syspath);
68db4a0c
YW
97 assert_se(!sd_device_unref(t));
98
8ee08dc5
YW
99 if (t)
100 log_debug("Removed subsystem:%s syspath:%s", subsystem, syspath);
101 else {
102 log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath);
103 n_new_dev++;
104 }
68db4a0c
YW
105 }
106
8ee08dc5 107 /* Assume no device is unplugged. */
68db4a0c 108 assert_se(hashmap_isempty(h));
8ee08dc5
YW
109
110 return n_new_dev;
68db4a0c
YW
111}
112
113static void test_sd_device_enumerator_filter_subsystem(void) {
114 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
115 _cleanup_(hashmap_freep) Hashmap *subsystems;
8ee08dc5 116 unsigned n_new_dev = 0;
68db4a0c
YW
117 sd_device *d;
118 Hashmap *h;
119 char *s;
120
ecbe9873
YW
121 log_info("/* %s */", __func__);
122
68db4a0c
YW
123 assert_se(subsystems = hashmap_new(&string_hash_ops));
124 assert_se(sd_device_enumerator_new(&e) >= 0);
125
126 FOREACH_DEVICE(e, d) {
127 const char *syspath, *subsystem;
128 int r;
129
130 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
131
132 r = sd_device_get_subsystem(d, &subsystem);
133 assert_se(r >= 0 || r == -ENOENT);
134 if (r < 0)
135 continue;
136
137 h = hashmap_get(subsystems, subsystem);
138 if (!h) {
139 char *str;
140 assert_se(str = strdup(subsystem));
141 assert_se(h = hashmap_new(&string_hash_ops));
142 assert_se(hashmap_put(subsystems, str, h) >= 0);
143 }
144
b5f24a38 145 assert_se(hashmap_put(h, syspath, d) >= 0);
68db4a0c
YW
146 assert_se(sd_device_ref(d));
147
148 log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
149 }
150
151 while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
8ee08dc5 152 n_new_dev += test_sd_device_enumerator_filter_subsystem_one(s, h);
68db4a0c
YW
153 hashmap_free(h);
154 free(s);
155 }
8ee08dc5
YW
156
157 if (n_new_dev > 0)
158 log_warning("%u new device is found in re-scan", n_new_dev);
159
160 /* Assume that not so many devices are plugged. */
161 assert_se(n_new_dev <= 10);
68db4a0c
YW
162}
163
9380d34c
YW
164int main(int argc, char **argv) {
165 test_setup_logging(LOG_INFO);
166
af18705f
YW
167 test_sd_device_enumerator_devices();
168 test_sd_device_enumerator_subsystems();
68db4a0c
YW
169 test_sd_device_enumerator_filter_subsystem();
170
9380d34c
YW
171 return 0;
172}