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