]>
Commit | Line | Data |
---|---|---|
aaa5b4ce YW |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <stdbool.h> | |
4 | #include <unistd.h> | |
5 | ||
6 | #include "sd-device.h" | |
7 | #include "sd-event.h" | |
8 | ||
9 | #include "device-monitor-private.h" | |
10 | #include "device-private.h" | |
11 | #include "device-util.h" | |
12 | #include "macro.h" | |
13 | #include "string-util.h" | |
14 | #include "tests.h" | |
15 | #include "util.h" | |
16 | #include "virt.h" | |
17 | ||
18 | static int monitor_handler(sd_device_monitor *m, sd_device *d, void *userdata) { | |
19 | const char *s, *syspath = userdata; | |
20 | ||
21 | assert_se(sd_device_get_syspath(d, &s) >= 0); | |
22 | assert_se(streq(s, syspath)); | |
23 | ||
aa6c83e6 | 24 | return sd_event_exit(sd_device_monitor_get_event(m), 100); |
aaa5b4ce YW |
25 | } |
26 | ||
4fe0caad | 27 | static int test_receive_device_fail(void) { |
aaa5b4ce | 28 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; |
4fe0caad YW |
29 | _cleanup_(sd_device_unrefp) sd_device *loopback = NULL; |
30 | const char *syspath; | |
aaa5b4ce YW |
31 | int r; |
32 | ||
4fe0caad YW |
33 | log_info("/* %s */", __func__); |
34 | ||
35 | /* Try to send device with invalid action and without seqnum. */ | |
36 | assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); | |
37 | assert_se(device_add_property(loopback, "ACTION", "hoge") >= 0); | |
38 | ||
39 | assert_se(sd_device_get_syspath(loopback, &syspath) >= 0); | |
40 | ||
41 | assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); | |
42 | assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); | |
43 | assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0); | |
44 | ||
45 | assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); | |
46 | assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); | |
47 | assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); | |
48 | assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0); | |
49 | ||
50 | /* Do not use assert_se() here. */ | |
51 | r = device_monitor_send_device(monitor_server, monitor_client, loopback); | |
52 | if (r < 0) | |
53 | return log_error_errno(r, "Failed to send loopback device: %m"); | |
54 | ||
55 | assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client), 0) >= 0); | |
56 | ||
57 | return 0; | |
58 | } | |
59 | ||
60 | static void test_send_receive_one(sd_device *device, bool subsystem_filter, bool tag_filter, bool use_bpf) { | |
61 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; | |
62 | const char *syspath, *subsystem, *tag, *devtype = NULL; | |
63 | ||
a5a59a24 YW |
64 | log_device_info(device, "/* %s(subsystem_filter=%s, tag_filter=%s, use_bpf=%s) */", __func__, |
65 | true_false(subsystem_filter), true_false(tag_filter), true_false(use_bpf)); | |
aaa5b4ce | 66 | |
a5a59a24 | 67 | assert_se(sd_device_get_syspath(device, &syspath) >= 0); |
aaa5b4ce YW |
68 | |
69 | assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); | |
deb2b734 YW |
70 | assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); |
71 | assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0); | |
aaa5b4ce YW |
72 | |
73 | assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); | |
74 | assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); | |
deb2b734 YW |
75 | assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); |
76 | assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0); | |
aaa5b4ce YW |
77 | |
78 | if (subsystem_filter) { | |
a5a59a24 YW |
79 | assert_se(sd_device_get_subsystem(device, &subsystem) >= 0); |
80 | (void) sd_device_get_devtype(device, &devtype); | |
81 | assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, devtype) >= 0); | |
aaa5b4ce YW |
82 | } |
83 | ||
84 | if (tag_filter) | |
a5a59a24 | 85 | FOREACH_DEVICE_TAG(device, tag) |
aaa5b4ce YW |
86 | assert_se(sd_device_monitor_filter_add_match_tag(monitor_client, tag) >= 0); |
87 | ||
88 | if ((subsystem_filter || tag_filter) && use_bpf) | |
89 | assert_se(sd_device_monitor_filter_update(monitor_client) >= 0); | |
90 | ||
4fe0caad | 91 | assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); |
aa6c83e6 | 92 | assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); |
aaa5b4ce YW |
93 | } |
94 | ||
a5a59a24 | 95 | static void test_subsystem_filter(sd_device *device) { |
aaa5b4ce YW |
96 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; |
97 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
aaa5b4ce YW |
98 | const char *syspath, *subsystem, *p, *s; |
99 | sd_device *d; | |
100 | ||
101 | log_info("/* %s */", __func__); | |
102 | ||
a5a59a24 YW |
103 | assert_se(sd_device_get_syspath(device, &syspath) >= 0); |
104 | assert_se(sd_device_get_subsystem(device, &subsystem) >= 0); | |
aaa5b4ce YW |
105 | |
106 | assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); | |
deb2b734 YW |
107 | assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); |
108 | assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0); | |
aaa5b4ce YW |
109 | |
110 | assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); | |
111 | assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); | |
112 | assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0); | |
deb2b734 YW |
113 | assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); |
114 | assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0); | |
aaa5b4ce YW |
115 | |
116 | assert_se(sd_device_enumerator_new(&e) >= 0); | |
117 | assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, false) >= 0); | |
118 | FOREACH_DEVICE(e, d) { | |
119 | assert_se(sd_device_get_syspath(d, &p) >= 0); | |
120 | assert_se(sd_device_get_subsystem(d, &s) >= 0); | |
121 | ||
122 | log_info("Sending device subsystem:%s syspath:%s", s, p); | |
123 | assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0); | |
124 | } | |
125 | ||
126 | log_info("Sending device subsystem:%s syspath:%s", subsystem, syspath); | |
a5a59a24 | 127 | assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); |
aa6c83e6 YW |
128 | assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); |
129 | } | |
130 | ||
131 | static void test_sd_device_monitor_filter_remove(sd_device *device) { | |
132 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; | |
133 | const char *syspath; | |
134 | ||
135 | log_device_info(device, "/* %s */", __func__); | |
136 | ||
137 | assert_se(sd_device_get_syspath(device, &syspath) >= 0); | |
138 | ||
139 | assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); | |
140 | assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); | |
141 | assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0); | |
142 | ||
143 | assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); | |
144 | assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); | |
145 | assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); | |
146 | assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0); | |
147 | ||
148 | assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, "hoge", NULL) >= 0); | |
149 | assert_se(sd_device_monitor_filter_update(monitor_client) >= 0); | |
150 | ||
151 | assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); | |
152 | assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client), 0) >= 0); | |
153 | ||
154 | assert_se(sd_device_monitor_filter_remove(monitor_client) >= 0); | |
155 | ||
156 | assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); | |
157 | assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); | |
aaa5b4ce YW |
158 | } |
159 | ||
9ffb9096 YW |
160 | static void test_device_copy_properties(sd_device *device) { |
161 | _cleanup_(sd_device_unrefp) sd_device *copy = NULL; | |
162 | ||
163 | assert_se(device_shallow_clone(device, ©) >= 0); | |
164 | assert_se(device_copy_properties(copy, device) >= 0); | |
165 | ||
166 | test_send_receive_one(copy, false, false, false); | |
167 | } | |
168 | ||
aaa5b4ce | 169 | int main(int argc, char *argv[]) { |
a5a59a24 | 170 | _cleanup_(sd_device_unrefp) sd_device *loopback = NULL, *sda = NULL; |
aaa5b4ce YW |
171 | int r; |
172 | ||
173 | test_setup_logging(LOG_INFO); | |
174 | ||
175 | if (getuid() != 0) | |
176 | return log_tests_skipped("not root"); | |
177 | ||
4fe0caad | 178 | r = test_receive_device_fail(); |
aaa5b4ce YW |
179 | if (r < 0) { |
180 | assert_se(r == -EPERM && detect_container() > 0); | |
181 | return log_tests_skipped("Running in container? Skipping remaining tests"); | |
182 | } | |
183 | ||
4fe0caad YW |
184 | assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); |
185 | assert_se(device_add_property(loopback, "ACTION", "add") >= 0); | |
186 | assert_se(device_add_property(loopback, "SEQNUM", "10") >= 0); | |
187 | ||
188 | test_send_receive_one(loopback, false, false, false); | |
189 | test_send_receive_one(loopback, true, false, false); | |
190 | test_send_receive_one(loopback, false, true, false); | |
191 | test_send_receive_one(loopback, true, true, false); | |
192 | test_send_receive_one(loopback, true, false, true); | |
193 | test_send_receive_one(loopback, false, true, true); | |
194 | test_send_receive_one(loopback, true, true, true); | |
a5a59a24 YW |
195 | |
196 | test_subsystem_filter(loopback); | |
aa6c83e6 | 197 | test_sd_device_monitor_filter_remove(loopback); |
9ffb9096 | 198 | test_device_copy_properties(loopback); |
a5a59a24 YW |
199 | |
200 | r = sd_device_new_from_subsystem_sysname(&sda, "block", "sda"); | |
201 | if (r < 0) { | |
202 | log_info_errno(r, "Failed to create sd_device for sda, skipping remaining tests: %m"); | |
203 | return 0; | |
204 | } | |
205 | ||
206 | assert_se(device_add_property(sda, "ACTION", "change") >= 0); | |
207 | assert_se(device_add_property(sda, "SEQNUM", "11") >= 0); | |
aaa5b4ce | 208 | |
4fe0caad YW |
209 | test_send_receive_one(sda, false, false, false); |
210 | test_send_receive_one(sda, true, false, false); | |
211 | test_send_receive_one(sda, false, true, false); | |
212 | test_send_receive_one(sda, true, true, false); | |
213 | test_send_receive_one(sda, true, false, true); | |
214 | test_send_receive_one(sda, false, true, true); | |
215 | test_send_receive_one(sda, true, true, true); | |
aaa5b4ce YW |
216 | |
217 | return 0; | |
218 | } |