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_basic(void) {
12 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
15 log_info("/* %s */", __func__
);
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
;
25 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
27 r
= sd_device_get_subsystem(d
, &subsystem
);
28 assert_se(r
>= 0 || r
== -ENOENT
);
30 r
= sd_device_get_devtype(d
, &val
);
31 assert_se(r
>= 0 || r
== -ENOENT
);
33 r
= sd_device_get_devnum(d
, &devnum
);
34 assert_se((r
>= 0 && major(devnum
) > 0) || r
== -ENOENT
);
36 r
= sd_device_get_ifindex(d
, &i
);
37 assert_se((r
>= 0 && i
> 0) || r
== -ENOENT
);
39 r
= sd_device_get_driver(d
, &val
);
40 assert_se(r
>= 0 || r
== -ENOENT
);
42 assert_se(sd_device_get_devpath(d
, &val
) >= 0);
44 r
= sd_device_get_devname(d
, &val
);
45 assert_se(r
>= 0 || r
== -ENOENT
);
47 assert_se(sd_device_get_sysname(d
, &val
) >= 0);
49 r
= sd_device_get_sysnum(d
, &val
);
50 assert_se(r
>= 0 || r
== -ENOENT
);
52 i
= sd_device_get_is_initialized(d
);
55 r
= sd_device_get_usec_since_initialized(d
, &usec
);
56 assert_se((r
>= 0 && usec
> 0) || r
== -ENODATA
);
59 r
= sd_device_get_sysattr_value(d
, "name_assign_type", &val
);
60 assert_se(r
>= 0 || IN_SET(r
, -ENOENT
, -EINVAL
));
62 r
= sd_device_get_property_value(d
, "ID_NET_DRIVER", &val
);
63 assert_se(r
>= 0 || r
== -ENOENT
);
65 log_debug("subsystem:%s syspath:%s initialized:%s", strna(subsystem
), syspath
, yes_no(i
));
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
;
73 assert_se(sd_device_enumerator_new(&e
) >= 0);
74 assert_se(sd_device_enumerator_add_match_subsystem(e
, subsystem
, true) >= 0);
76 FOREACH_DEVICE(e
, d
) {
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
));
83 log_debug("Removed subsystem:%s syspath:%s", subsystem
, syspath
);
86 assert_se(hashmap_isempty(h
));
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
;
96 log_info("/* %s */", __func__
);
98 assert_se(subsystems
= hashmap_new(&string_hash_ops
));
99 assert_se(sd_device_enumerator_new(&e
) >= 0);
101 FOREACH_DEVICE(e
, d
) {
102 const char *syspath
, *subsystem
;
105 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
107 r
= sd_device_get_subsystem(d
, &subsystem
);
108 assert_se(r
>= 0 || r
== -ENOENT
);
112 h
= hashmap_get(subsystems
, subsystem
);
115 assert_se(str
= strdup(subsystem
));
116 assert_se(h
= hashmap_new(&string_hash_ops
));
117 assert_se(hashmap_put(subsystems
, str
, h
) >= 0);
120 assert_se(hashmap_put(h
, syspath
, d
) >= 0);
121 assert_se(sd_device_ref(d
));
123 log_debug("Added subsystem:%s syspath:%s", subsystem
, syspath
);
126 while ((h
= hashmap_steal_first_key_and_value(subsystems
, (void**) &s
))) {
127 test_sd_device_enumerator_filter_subsystem_one(s
, h
);
133 int main(int argc
, char **argv
) {
134 test_setup_logging(LOG_INFO
);
136 test_sd_device_basic();
137 test_sd_device_enumerator_filter_subsystem();