]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-device/test-sd-device-monitor.c
test: add tests for sd_device_monitor
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device-monitor.c
CommitLineData
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
18static 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
24 return sd_event_exit(sd_device_monitor_get_event(m), 0);
25}
26
27static int test_loopback(bool subsystem_filter, bool tag_filter, bool use_bpf) {
28 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
29 _cleanup_(sd_device_unrefp) sd_device *loopback = NULL;
30 const char *syspath, *subsystem, *tag;
31 int r;
32
33 log_info("/* %s(subsystem_filter=%s, tag_filter=%s, use_bpf=%s) */", __func__,
34 true_false(subsystem_filter), true_false(tag_filter), true_false(use_bpf));
35
36 assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0);
37 assert_se(sd_device_get_syspath(loopback, &syspath) >= 0);
38 assert_se(device_add_property(loopback, "ACTION", "add") >= 0);
39 assert_se(device_add_property(loopback, "SEQNUM", "10") >= 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, NULL) >= 0);
43
44 assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
45 assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
46 assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath, "loopback-monitor") >= 0);
47
48 if (subsystem_filter) {
49 assert_se(sd_device_get_subsystem(loopback, &subsystem) >= 0);
50 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0);
51 }
52
53 if (tag_filter)
54 FOREACH_DEVICE_TAG(loopback, tag)
55 assert_se(sd_device_monitor_filter_add_match_tag(monitor_client, tag) >= 0);
56
57 if ((subsystem_filter || tag_filter) && use_bpf)
58 assert_se(sd_device_monitor_filter_update(monitor_client) >= 0);
59
60 r = device_monitor_send_device(monitor_server, monitor_client, loopback);
61 if (r < 0)
62 return log_error_errno(r, "Failed to send loopback device: %m");
63
64 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 0);
65
66 return 0;
67}
68
69static void test_subsystem_filter(void) {
70 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
71 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
72 _cleanup_(sd_device_unrefp) sd_device *loopback = NULL;
73 const char *syspath, *subsystem, *p, *s;
74 sd_device *d;
75
76 log_info("/* %s */", __func__);
77
78 assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0);
79 assert_se(sd_device_get_syspath(loopback, &syspath) >= 0);
80 assert_se(sd_device_get_subsystem(loopback, &subsystem) >= 0);
81 assert_se(device_add_property(loopback, "ACTION", "add") >= 0);
82 assert_se(device_add_property(loopback, "SEQNUM", "10") >= 0);
83
84 assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
85 assert_se(sd_device_monitor_start(monitor_server, NULL, NULL, NULL) >= 0);
86
87 assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
88 assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
89 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0);
90 assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath, "subsystem-filter") >= 0);
91
92 assert_se(sd_device_enumerator_new(&e) >= 0);
93 assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, false) >= 0);
94 FOREACH_DEVICE(e, d) {
95 assert_se(sd_device_get_syspath(d, &p) >= 0);
96 assert_se(sd_device_get_subsystem(d, &s) >= 0);
97
98 log_info("Sending device subsystem:%s syspath:%s", s, p);
99 assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
100 }
101
102 log_info("Sending device subsystem:%s syspath:%s", subsystem, syspath);
103 assert_se(device_monitor_send_device(monitor_server, monitor_client, loopback) >= 0);
104 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 0);
105}
106
107int main(int argc, char *argv[]) {
108 int r;
109
110 test_setup_logging(LOG_INFO);
111
112 if (getuid() != 0)
113 return log_tests_skipped("not root");
114
115 r = test_loopback(false, false, false);
116 if (r < 0) {
117 assert_se(r == -EPERM && detect_container() > 0);
118 return log_tests_skipped("Running in container? Skipping remaining tests");
119 }
120
121 assert_se(test_loopback( true, false, false) >= 0);
122 assert_se(test_loopback(false, true, false) >= 0);
123 assert_se(test_loopback( true, true, false) >= 0);
124 assert_se(test_loopback( true, false, true) >= 0);
125 assert_se(test_loopback(false, true, true) >= 0);
126 assert_se(test_loopback( true, true, true) >= 0);
127
128 test_subsystem_filter();
129
130 return 0;
131}