1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "device-enumerator-private.h"
4 #include "device-private.h"
5 #include "device-util.h"
7 #include "string-util.h"
11 static void test_sd_device_one(sd_device
*d
) {
12 const char *syspath
, *subsystem
, *val
;
17 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
19 r
= sd_device_get_subsystem(d
, &subsystem
);
20 assert_se(r
>= 0 || r
== -ENOENT
);
22 r
= sd_device_get_devtype(d
, &val
);
23 assert_se(r
>= 0 || r
== -ENOENT
);
25 r
= sd_device_get_devnum(d
, &devnum
);
26 assert_se((r
>= 0 && major(devnum
) > 0) || r
== -ENOENT
);
28 r
= sd_device_get_ifindex(d
, &i
);
29 assert_se((r
>= 0 && i
> 0) || r
== -ENOENT
);
31 r
= sd_device_get_driver(d
, &val
);
32 assert_se(r
>= 0 || r
== -ENOENT
);
34 assert_se(sd_device_get_devpath(d
, &val
) >= 0);
36 r
= sd_device_get_devname(d
, &val
);
37 assert_se(r
>= 0 || r
== -ENOENT
);
39 assert_se(sd_device_get_sysname(d
, &val
) >= 0);
41 r
= sd_device_get_sysnum(d
, &val
);
42 assert_se(r
>= 0 || r
== -ENOENT
);
44 i
= sd_device_get_is_initialized(d
);
47 r
= sd_device_get_usec_since_initialized(d
, &usec
);
48 assert_se((r
>= 0 && usec
> 0) || r
== -ENODATA
);
51 r
= sd_device_get_sysattr_value(d
, "name_assign_type", &val
);
52 assert_se(r
>= 0 || IN_SET(r
, -ENOENT
, -EINVAL
));
54 r
= sd_device_get_property_value(d
, "ID_NET_DRIVER", &val
);
55 assert_se(r
>= 0 || r
== -ENOENT
);
57 log_info("syspath:%s subsystem:%s initialized:%s", syspath
, strna(subsystem
), yes_no(i
));
60 static void test_sd_device_enumerator_devices(void) {
61 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
64 log_info("/* %s */", __func__
);
66 assert_se(sd_device_enumerator_new(&e
) >= 0);
67 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
69 test_sd_device_one(d
);
72 static void test_sd_device_enumerator_subsystems(void) {
73 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
76 log_info("/* %s */", __func__
);
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
);
84 static void test_sd_device_enumerator_filter_subsystem_one(const char *subsystem
, Hashmap
*h
) {
85 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
88 assert_se(sd_device_enumerator_new(&e
) >= 0);
89 assert_se(sd_device_enumerator_add_match_subsystem(e
, subsystem
, true) >= 0);
91 FOREACH_DEVICE(e
, d
) {
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
));
98 log_debug("Removed subsystem:%s syspath:%s", subsystem
, syspath
);
101 assert_se(hashmap_isempty(h
));
104 static 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
;
111 log_info("/* %s */", __func__
);
113 assert_se(subsystems
= hashmap_new(&string_hash_ops
));
114 assert_se(sd_device_enumerator_new(&e
) >= 0);
116 FOREACH_DEVICE(e
, d
) {
117 const char *syspath
, *subsystem
;
120 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
122 r
= sd_device_get_subsystem(d
, &subsystem
);
123 assert_se(r
>= 0 || r
== -ENOENT
);
127 h
= hashmap_get(subsystems
, subsystem
);
130 assert_se(str
= strdup(subsystem
));
131 assert_se(h
= hashmap_new(&string_hash_ops
));
132 assert_se(hashmap_put(subsystems
, str
, h
) >= 0);
135 assert_se(hashmap_put(h
, syspath
, d
) >= 0);
136 assert_se(sd_device_ref(d
));
138 log_debug("Added subsystem:%s syspath:%s", subsystem
, syspath
);
141 while ((h
= hashmap_steal_first_key_and_value(subsystems
, (void**) &s
))) {
142 test_sd_device_enumerator_filter_subsystem_one(s
, h
);
148 int main(int argc
, char **argv
) {
149 test_setup_logging(LOG_INFO
);
151 test_sd_device_enumerator_devices();
152 test_sd_device_enumerator_subsystems();
153 test_sd_device_enumerator_filter_subsystem();