]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/test-sd-device.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "device-enumerator-private.h"
4 #include "device-private.h"
5 #include "device-util.h"
6 #include "hashmap.h"
7 #include "string-util.h"
8 #include "tests.h"
9 #include "util.h"
10
11 static void test_sd_device_basic(void) {
12 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
13 sd_device *d;
14
15 log_info("/* %s */", __func__);
16
17 assert_se(sd_device_enumerator_new(&e) >= 0);
18 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
19 FOREACH_DEVICE(e, d) {
20 const char *syspath, *subsystem, *val;
21 dev_t devnum;
22 usec_t usec;
23 int i, r;
24
25 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
26
27 r = sd_device_get_subsystem(d, &subsystem);
28 assert_se(r >= 0 || r == -ENOENT);
29
30 r = sd_device_get_devtype(d, &val);
31 assert_se(r >= 0 || r == -ENOENT);
32
33 r = sd_device_get_devnum(d, &devnum);
34 assert_se((r >= 0 && major(devnum) > 0) || r == -ENOENT);
35
36 r = sd_device_get_ifindex(d, &i);
37 assert_se((r >= 0 && i > 0) || r == -ENOENT);
38
39 r = sd_device_get_driver(d, &val);
40 assert_se(r >= 0 || r == -ENOENT);
41
42 assert_se(sd_device_get_devpath(d, &val) >= 0);
43
44 r = sd_device_get_devname(d, &val);
45 assert_se(r >= 0 || r == -ENOENT);
46
47 assert_se(sd_device_get_sysname(d, &val) >= 0);
48
49 r = sd_device_get_sysnum(d, &val);
50 assert_se(r >= 0 || r == -ENOENT);
51
52 i = sd_device_get_is_initialized(d);
53 assert_se(i >= 0);
54 if (i > 0) {
55 r = sd_device_get_usec_since_initialized(d, &usec);
56 assert_se((r >= 0 && usec > 0) || r == -ENODATA);
57 }
58
59 r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
60 assert_se(r >= 0 || IN_SET(r, -ENOENT, -EINVAL));
61
62 r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val);
63 assert_se(r >= 0 || r == -ENOENT);
64
65 log_debug("subsystem:%s syspath:%s initialized:%s", strna(subsystem), syspath, yes_no(i));
66 }
67 }
68
69 static void test_sd_device_enumerator_filter_subsystem_one(const char *subsystem, Hashmap *h) {
70 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
71 sd_device *d, *t;
72
73 assert_se(sd_device_enumerator_new(&e) >= 0);
74 assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
75
76 FOREACH_DEVICE(e, d) {
77 const char *syspath;
78
79 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
80 assert_se(t = hashmap_remove(h, syspath));
81 assert_se(!sd_device_unref(t));
82
83 log_debug("Removed subsystem:%s syspath:%s", subsystem, syspath);
84 }
85
86 assert_se(hashmap_isempty(h));
87 }
88
89 static void test_sd_device_enumerator_filter_subsystem(void) {
90 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
91 _cleanup_(hashmap_freep) Hashmap *subsystems;
92 sd_device *d;
93 Hashmap *h;
94 char *s;
95
96 log_info("/* %s */", __func__);
97
98 assert_se(subsystems = hashmap_new(&string_hash_ops));
99 assert_se(sd_device_enumerator_new(&e) >= 0);
100
101 FOREACH_DEVICE(e, d) {
102 const char *syspath, *subsystem;
103 int r;
104
105 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
106
107 r = sd_device_get_subsystem(d, &subsystem);
108 assert_se(r >= 0 || r == -ENOENT);
109 if (r < 0)
110 continue;
111
112 h = hashmap_get(subsystems, subsystem);
113 if (!h) {
114 char *str;
115 assert_se(str = strdup(subsystem));
116 assert_se(h = hashmap_new(&string_hash_ops));
117 assert_se(hashmap_put(subsystems, str, h) >= 0);
118 }
119
120 assert_se(hashmap_put(h, syspath, d) >= 0);
121 assert_se(sd_device_ref(d));
122
123 log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
124 }
125
126 while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
127 test_sd_device_enumerator_filter_subsystem_one(s, h);
128 hashmap_free(h);
129 free(s);
130 }
131 }
132
133 int main(int argc, char **argv) {
134 test_setup_logging(LOG_INFO);
135
136 test_sd_device_basic();
137 test_sd_device_enumerator_filter_subsystem();
138
139 return 0;
140 }