]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-device/test-sd-device.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device.c
CommitLineData
9380d34c
YW
1/* SPDX-License-Identifier: LGPL-2.1+ */
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"
9#include "util.h"
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
68db4a0c
YW
84static void test_sd_device_enumerator_filter_subsystem_one(const char *subsystem, Hashmap *h) {
85 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
86 sd_device *d, *t;
87
88 assert_se(sd_device_enumerator_new(&e) >= 0);
89 assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
90
91 FOREACH_DEVICE(e, d) {
92 const char *syspath;
93
94 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
95 assert_se(t = hashmap_remove(h, syspath));
96 assert_se(!sd_device_unref(t));
97
98 log_debug("Removed subsystem:%s syspath:%s", subsystem, syspath);
99 }
100
101 assert_se(hashmap_isempty(h));
102}
103
104static void test_sd_device_enumerator_filter_subsystem(void) {
105 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
106 _cleanup_(hashmap_freep) Hashmap *subsystems;
107 sd_device *d;
108 Hashmap *h;
109 char *s;
110
ecbe9873
YW
111 log_info("/* %s */", __func__);
112
68db4a0c
YW
113 assert_se(subsystems = hashmap_new(&string_hash_ops));
114 assert_se(sd_device_enumerator_new(&e) >= 0);
115
116 FOREACH_DEVICE(e, d) {
117 const char *syspath, *subsystem;
118 int r;
119
120 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
121
122 r = sd_device_get_subsystem(d, &subsystem);
123 assert_se(r >= 0 || r == -ENOENT);
124 if (r < 0)
125 continue;
126
127 h = hashmap_get(subsystems, subsystem);
128 if (!h) {
129 char *str;
130 assert_se(str = strdup(subsystem));
131 assert_se(h = hashmap_new(&string_hash_ops));
132 assert_se(hashmap_put(subsystems, str, h) >= 0);
133 }
134
b5f24a38 135 assert_se(hashmap_put(h, syspath, d) >= 0);
68db4a0c
YW
136 assert_se(sd_device_ref(d));
137
138 log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
139 }
140
141 while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
142 test_sd_device_enumerator_filter_subsystem_one(s, h);
143 hashmap_free(h);
144 free(s);
145 }
146}
147
9380d34c
YW
148int main(int argc, char **argv) {
149 test_setup_logging(LOG_INFO);
150
af18705f
YW
151 test_sd_device_enumerator_devices();
152 test_sd_device_enumerator_subsystems();
68db4a0c
YW
153 test_sd_device_enumerator_filter_subsystem();
154
9380d34c
YW
155 return 0;
156}