1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "device-monitor-private.h"
10 #include "device-private.h"
11 #include "device-util.h"
13 #include "string-util.h"
18 static int monitor_handler(sd_device_monitor
*m
, sd_device
*d
, void *userdata
) {
19 const char *s
, *syspath
= userdata
;
21 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
22 assert_se(streq(s
, syspath
));
24 return sd_event_exit(sd_device_monitor_get_event(m
), 100);
27 static int test_receive_device_fail(void) {
28 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
29 _cleanup_(sd_device_unrefp
) sd_device
*loopback
= NULL
;
33 log_info("/* %s */", __func__
);
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);
39 assert_se(sd_device_get_syspath(loopback
, &syspath
) >= 0);
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);
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);
50 /* Do not use assert_se() here. */
51 r
= device_monitor_send_device(monitor_server
, monitor_client
, loopback
);
53 return log_error_errno(r
, "Failed to send loopback device: %m");
55 assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client
), 0) >= 0);
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
;
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
));
67 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
69 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
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);
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);
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);
78 if (subsystem_filter
) {
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);
85 FOREACH_DEVICE_TAG(device
, tag
)
86 assert_se(sd_device_monitor_filter_add_match_tag(monitor_client
, tag
) >= 0);
88 if ((subsystem_filter
|| tag_filter
) && use_bpf
)
89 assert_se(sd_device_monitor_filter_update(monitor_client
) >= 0);
91 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
92 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 100);
95 static void test_subsystem_filter(sd_device
*device
) {
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
;
98 const char *syspath
, *subsystem
, *p
, *s
;
101 log_info("/* %s */", __func__
);
103 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
104 assert_se(sd_device_get_subsystem(device
, &subsystem
) >= 0);
106 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
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);
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);
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);
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);
122 log_info("Sending device subsystem:%s syspath:%s", s
, p
);
123 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, d
) >= 0);
126 log_info("Sending device subsystem:%s syspath:%s", subsystem
, syspath
);
127 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
128 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 100);
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
;
135 log_device_info(device
, "/* %s */", __func__
);
137 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
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);
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);
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);
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);
154 assert_se(sd_device_monitor_filter_remove(monitor_client
) >= 0);
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);
160 static void test_device_copy_properties(sd_device
*device
) {
161 _cleanup_(sd_device_unrefp
) sd_device
*copy
= NULL
;
163 assert_se(device_shallow_clone(device
, ©
) >= 0);
164 assert_se(device_copy_properties(copy
, device
) >= 0);
166 test_send_receive_one(copy
, false, false, false);
169 int main(int argc
, char *argv
[]) {
170 _cleanup_(sd_device_unrefp
) sd_device
*loopback
= NULL
, *sda
= NULL
;
173 test_setup_logging(LOG_INFO
);
176 return log_tests_skipped("not root");
178 r
= test_receive_device_fail();
180 assert_se(r
== -EPERM
&& detect_container() > 0);
181 return log_tests_skipped("Running in container? Skipping remaining tests");
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);
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);
196 test_subsystem_filter(loopback
);
197 test_sd_device_monitor_filter_remove(loopback
);
198 test_device_copy_properties(loopback
);
200 r
= sd_device_new_from_subsystem_sysname(&sda
, "block", "sda");
202 log_info_errno(r
, "Failed to create sd_device for sda, skipping remaining tests: %m");
206 assert_se(device_add_property(sda
, "ACTION", "change") >= 0);
207 assert_se(device_add_property(sda
, "SEQNUM", "11") >= 0);
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);