Line data Source code
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 "time-util.h"
10 :
11 1116 : static 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;
16 :
17 1116 : assert_se(sd_device_get_syspath(d, &syspath) >= 0);
18 :
19 1116 : r = sd_device_get_subsystem(d, &subsystem);
20 1116 : assert_se(r >= 0 || r == -ENOENT);
21 :
22 1116 : r = sd_device_get_devtype(d, &val);
23 1116 : assert_se(r >= 0 || r == -ENOENT);
24 :
25 1116 : r = sd_device_get_devnum(d, &devnum);
26 1116 : assert_se((r >= 0 && major(devnum) > 0) || r == -ENOENT);
27 :
28 1116 : r = sd_device_get_ifindex(d, &i);
29 1116 : assert_se((r >= 0 && i > 0) || r == -ENOENT);
30 :
31 1116 : r = sd_device_get_driver(d, &val);
32 1116 : assert_se(r >= 0 || r == -ENOENT);
33 :
34 1116 : assert_se(sd_device_get_devpath(d, &val) >= 0);
35 :
36 1116 : r = sd_device_get_devname(d, &val);
37 1116 : assert_se(r >= 0 || r == -ENOENT);
38 :
39 1116 : assert_se(sd_device_get_sysname(d, &val) >= 0);
40 :
41 1116 : r = sd_device_get_sysnum(d, &val);
42 1116 : assert_se(r >= 0 || r == -ENOENT);
43 :
44 1116 : i = sd_device_get_is_initialized(d);
45 1116 : assert_se(i >= 0);
46 1116 : if (i > 0) {
47 373 : r = sd_device_get_usec_since_initialized(d, &usec);
48 373 : assert_se((r >= 0 && usec > 0) || r == -ENODATA);
49 : }
50 :
51 1116 : r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
52 1116 : assert_se(r >= 0 || IN_SET(r, -ENOENT, -EINVAL));
53 :
54 1116 : r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val);
55 1116 : assert_se(r >= 0 || r == -ENOENT);
56 :
57 1116 : log_info("syspath:%s subsystem:%s initialized:%s", syspath, strna(subsystem), yes_no(i));
58 1116 : }
59 :
60 1 : static void test_sd_device_enumerator_devices(void) {
61 1 : _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
62 : sd_device *d;
63 :
64 1 : log_info("/* %s */", __func__);
65 :
66 1 : assert_se(sd_device_enumerator_new(&e) >= 0);
67 1 : assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
68 731 : FOREACH_DEVICE(e, d)
69 730 : test_sd_device_one(d);
70 1 : }
71 :
72 1 : static void test_sd_device_enumerator_subsystems(void) {
73 1 : _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
74 : sd_device *d;
75 :
76 1 : log_info("/* %s */", __func__);
77 :
78 1 : assert_se(sd_device_enumerator_new(&e) >= 0);
79 1 : assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
80 387 : FOREACH_SUBSYSTEM(e, d)
81 386 : test_sd_device_one(d);
82 1 : }
83 :
84 71 : static void test_sd_device_enumerator_filter_subsystem_one(const char *subsystem, Hashmap *h) {
85 71 : _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
86 : sd_device *d, *t;
87 :
88 71 : assert_se(sd_device_enumerator_new(&e) >= 0);
89 71 : assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
90 :
91 801 : FOREACH_DEVICE(e, d) {
92 : const char *syspath;
93 :
94 730 : assert_se(sd_device_get_syspath(d, &syspath) >= 0);
95 730 : assert_se(t = hashmap_remove(h, syspath));
96 730 : assert_se(!sd_device_unref(t));
97 :
98 730 : log_debug("Removed subsystem:%s syspath:%s", subsystem, syspath);
99 : }
100 :
101 71 : assert_se(hashmap_isempty(h));
102 71 : }
103 :
104 1 : static void test_sd_device_enumerator_filter_subsystem(void) {
105 1 : _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
106 1 : _cleanup_(hashmap_freep) Hashmap *subsystems;
107 : sd_device *d;
108 : Hashmap *h;
109 : char *s;
110 :
111 1 : log_info("/* %s */", __func__);
112 :
113 1 : assert_se(subsystems = hashmap_new(&string_hash_ops));
114 1 : assert_se(sd_device_enumerator_new(&e) >= 0);
115 :
116 731 : FOREACH_DEVICE(e, d) {
117 : const char *syspath, *subsystem;
118 : int r;
119 :
120 730 : assert_se(sd_device_get_syspath(d, &syspath) >= 0);
121 :
122 730 : r = sd_device_get_subsystem(d, &subsystem);
123 730 : assert_se(r >= 0 || r == -ENOENT);
124 730 : if (r < 0)
125 0 : continue;
126 :
127 730 : h = hashmap_get(subsystems, subsystem);
128 730 : if (!h) {
129 : char *str;
130 71 : assert_se(str = strdup(subsystem));
131 71 : assert_se(h = hashmap_new(&string_hash_ops));
132 71 : assert_se(hashmap_put(subsystems, str, h) >= 0);
133 : }
134 :
135 730 : assert_se(hashmap_put(h, syspath, d) >= 0);
136 730 : assert_se(sd_device_ref(d));
137 :
138 730 : log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
139 : }
140 :
141 72 : while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
142 71 : test_sd_device_enumerator_filter_subsystem_one(s, h);
143 71 : hashmap_free(h);
144 71 : free(s);
145 : }
146 1 : }
147 :
148 1 : int main(int argc, char **argv) {
149 1 : test_setup_logging(LOG_INFO);
150 :
151 1 : test_sd_device_enumerator_devices();
152 1 : test_sd_device_enumerator_subsystems();
153 1 : test_sd_device_enumerator_filter_subsystem();
154 :
155 1 : return 0;
156 : }
|