From: Yu Watanabe Date: Thu, 1 Aug 2024 04:14:47 +0000 (+0900) Subject: test: modernize test-sd-device-monitor.c X-Git-Tag: v257-rc1~772^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0403221a2a572548e040384707f58ec83cf9f679;p=thirdparty%2Fsystemd.git test: modernize test-sd-device-monitor.c --- diff --git a/src/libsystemd/sd-device/test-sd-device-monitor.c b/src/libsystemd/sd-device/test-sd-device-monitor.c index 3dbf9871dc3..613e17a68ac 100644 --- a/src/libsystemd/sd-device/test-sd-device-monitor.c +++ b/src/libsystemd/sd-device/test-sd-device-monitor.c @@ -17,39 +17,104 @@ #include "tests.h" #include "virt.h" +static void prepare_loopback(sd_device **ret) { + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; + + ASSERT_OK(sd_device_new_from_syspath(&dev, "/sys/class/net/lo")); + ASSERT_OK(device_add_property(dev, "ACTION", "add")); + ASSERT_OK(device_add_property(dev, "SEQNUM", "10")); + ASSERT_OK(device_add_tag(dev, "TEST_SD_DEVICE_MONITOR", true)); + + *ret = TAKE_PTR(dev); +} + +static int prepare_sda(sd_device **ret) { + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; + int r; + + r = sd_device_new_from_subsystem_sysname(&dev, "block", "sda"); + if (r < 0) + return r; + + ASSERT_OK(device_add_property(dev, "ACTION", "change")); + ASSERT_OK(device_add_property(dev, "SEQNUM", "11")); + + *ret = TAKE_PTR(dev); + return 0; +} + static int monitor_handler(sd_device_monitor *m, sd_device *d, void *userdata) { const char *s, *syspath = userdata; - assert_se(sd_device_get_syspath(d, &s) >= 0); - assert_se(streq(s, syspath)); + ASSERT_OK(sd_device_get_syspath(d, &s)); + ASSERT_TRUE(streq(s, syspath)); return sd_event_exit(sd_device_monitor_get_event(m), 100); } -static void test_receive_device_fail(void) { +static void prepare_monitor(sd_device_monitor **ret_server, sd_device_monitor **ret_client) { + _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; + + ASSERT_OK(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1)); + ASSERT_OK(sd_device_monitor_set_description(monitor_server, "sender")); + ASSERT_OK(sd_device_monitor_start(monitor_server, NULL, NULL)); + + ASSERT_OK(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1)); + ASSERT_OK(sd_device_monitor_set_description(monitor_client, "client")); + ASSERT_OK(device_monitor_allow_unicast_sender(monitor_client, monitor_server)); + + *ret_server = TAKE_PTR(monitor_server); + *ret_client = TAKE_PTR(monitor_client); +} + +static void send_by_enumerator( + sd_device_monitor *monitor_server, + sd_device_monitor *monitor_client, + sd_device_enumerator *e, + size_t n, + const char *syspath_filter) { + + size_t i = 0; + + FOREACH_DEVICE(e, d) { + const char *p, *s; + + ASSERT_OK(sd_device_get_syspath(d, &p)); + ASSERT_OK(sd_device_get_subsystem(d, &s)); + + if (syspath_filter && path_startswith(p, syspath_filter)) + continue; + + ASSERT_OK(device_add_property(d, "ACTION", "add")); + ASSERT_OK(device_add_property(d, "SEQNUM", "10")); + + log_device_debug(d, "Sending device subsystem:%s syspath:%s", s, p); + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, d)); + + /* The sysattr and parent filters are not implemented in BPF yet. So, sending multiple + * devices may fills up buffer and device_monitor_send_device() may return EAGAIN. Let's + * send only a few devices here, which should be filtered out by the receiver. */ + if (n != SIZE_MAX && ++i >= n) + break; + } +} + +TEST(refuse_invalid_device) { _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; _cleanup_(sd_device_unrefp) sd_device *loopback = NULL; const char *syspath; - log_info("/* %s */", __func__); - /* Try to send device with invalid action and without seqnum. */ - assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); - assert_se(device_add_property(loopback, "ACTION", "hoge") >= 0); + ASSERT_OK(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo")); + ASSERT_OK(device_add_property(loopback, "ACTION", "hoge")); - assert_se(sd_device_get_syspath(loopback, &syspath) >= 0); + ASSERT_OK(sd_device_get_syspath(loopback, &syspath)); - assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_server, "sender") >= 0); - assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); + prepare_monitor(&monitor_server, &monitor_client); - assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_client, "receiver") >= 0); - assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); - assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); - - assert_se(device_monitor_send_device(monitor_server, monitor_client, loopback) >= 0); - assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client), 0) >= 0); + ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath)); + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, loopback)); + ASSERT_OK(sd_event_run(sd_device_monitor_get_event(monitor_client), 0)); } static void test_send_receive_one(sd_device *device, bool subsystem_filter, bool tag_filter, bool use_bpf) { @@ -59,243 +124,185 @@ static void test_send_receive_one(sd_device *device, bool subsystem_filter, bool log_device_info(device, "/* %s(subsystem_filter=%s, tag_filter=%s, use_bpf=%s) */", __func__, true_false(subsystem_filter), true_false(tag_filter), true_false(use_bpf)); - assert_se(sd_device_get_syspath(device, &syspath) >= 0); - - assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_server, "sender") >= 0); - assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); + ASSERT_OK(sd_device_get_syspath(device, &syspath)); - assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_client, "receiver") >= 0); - assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); - assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); + prepare_monitor(&monitor_server, &monitor_client); if (subsystem_filter) { - assert_se(sd_device_get_subsystem(device, &subsystem) >= 0); + ASSERT_OK(sd_device_get_subsystem(device, &subsystem)); (void) sd_device_get_devtype(device, &devtype); - assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, devtype) >= 0); + ASSERT_OK(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, devtype)); } if (tag_filter) FOREACH_DEVICE_TAG(device, tag) - assert_se(sd_device_monitor_filter_add_match_tag(monitor_client, tag) >= 0); + ASSERT_OK(sd_device_monitor_filter_add_match_tag(monitor_client, tag)); if ((subsystem_filter || tag_filter) && use_bpf) - assert_se(sd_device_monitor_filter_update(monitor_client) >= 0); + ASSERT_OK(sd_device_monitor_filter_update(monitor_client)); - assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); - assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); + ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath)); + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, device)); + ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100); } -static void test_subsystem_filter(sd_device *device) { - _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; - _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; - const char *syspath, *subsystem; +TEST(sd_device_monitor_send_receive) { + _cleanup_(sd_device_unrefp) sd_device *loopback = NULL, *sda = NULL; + int r; - log_device_info(device, "/* %s */", __func__); + prepare_loopback(&loopback); + test_send_receive_one(loopback, false, false, false); + test_send_receive_one(loopback, true, false, false); + test_send_receive_one(loopback, false, true, false); + test_send_receive_one(loopback, true, true, false); + test_send_receive_one(loopback, true, false, true); + test_send_receive_one(loopback, false, true, true); + test_send_receive_one(loopback, true, true, true); - assert_se(sd_device_get_syspath(device, &syspath) >= 0); - assert_se(sd_device_get_subsystem(device, &subsystem) >= 0); + r = prepare_sda(&sda); + if (r < 0) + return (void) log_tests_skipped_errno(r, "Failed to create sd_device for sda"); - assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_server, "sender") >= 0); - assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); + test_send_receive_one(sda, false, false, false); + test_send_receive_one(sda, true, false, false); + test_send_receive_one(sda, false, true, false); + test_send_receive_one(sda, true, true, false); + test_send_receive_one(sda, true, false, true); + test_send_receive_one(sda, false, true, true); + test_send_receive_one(sda, true, true, true); +} - assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_client, "receiver") >= 0); - assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); - assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0); - assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); +TEST(sd_device_monitor_filter_add_match_subsystem_devtype) { + _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; + _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + const char *syspath, *subsystem; - assert_se(sd_device_enumerator_new(&e) >= 0); - assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, false) >= 0); - FOREACH_DEVICE(e, d) { - const char *p, *s; + prepare_loopback(&device); - assert_se(sd_device_get_syspath(d, &p) >= 0); - assert_se(sd_device_get_subsystem(d, &s) >= 0); + ASSERT_OK(sd_device_get_syspath(device, &syspath)); + ASSERT_OK(sd_device_get_subsystem(device, &subsystem)); - assert_se(device_add_property(d, "ACTION", "add") >= 0); - assert_se(device_add_property(d, "SEQNUM", "10") >= 0); + prepare_monitor(&monitor_server, &monitor_client); - log_device_debug(d, "Sending device subsystem:%s syspath:%s", s, p); - assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0); - } + ASSERT_OK(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL)); + ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath)); + + ASSERT_OK(sd_device_enumerator_new(&e)); + ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, subsystem, false)); + send_by_enumerator(monitor_server, monitor_client, e, SIZE_MAX, NULL); log_device_info(device, "Sending device subsystem:%s syspath:%s", subsystem, syspath); - assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); - assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, device)); + ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100); } -static void test_tag_filter(sd_device *device) { +TEST(sd_device_monitor_filter_add_match_tag) { _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; const char *syspath; - log_device_info(device, "/* %s */", __func__); + prepare_loopback(&device); - assert_se(sd_device_get_syspath(device, &syspath) >= 0); + ASSERT_OK(sd_device_get_syspath(device, &syspath)); - assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_server, "sender") >= 0); - assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); + prepare_monitor(&monitor_server, &monitor_client); - assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_client, "receiver") >= 0); - assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); - assert_se(sd_device_monitor_filter_add_match_tag(monitor_client, "TEST_SD_DEVICE_MONITOR") >= 0); - assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); - - assert_se(sd_device_enumerator_new(&e) >= 0); - FOREACH_DEVICE(e, d) { - const char *p; + ASSERT_OK(sd_device_monitor_filter_add_match_tag(monitor_client, "TEST_SD_DEVICE_MONITOR")); + ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath)); - assert_se(sd_device_get_syspath(d, &p) >= 0); - - assert_se(device_add_property(d, "ACTION", "add") >= 0); - assert_se(device_add_property(d, "SEQNUM", "10") >= 0); - - log_device_debug(d, "Sending device syspath:%s", p); - assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0); - } + ASSERT_OK(sd_device_enumerator_new(&e)); + send_by_enumerator(monitor_server, monitor_client, e, SIZE_MAX, NULL); log_device_info(device, "Sending device syspath:%s", syspath); - assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); - assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); - + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, device)); + ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100); } -static void test_sysattr_filter(sd_device *device, const char *sysattr) { +TEST(sd_device_monitor_filter_add_match_sysattr) { _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + static const char *sysattr = "ifindex"; const char *syspath, *sysattr_value; - log_device_info(device, "/* %s(%s) */", __func__, sysattr); + prepare_loopback(&device); - assert_se(sd_device_get_syspath(device, &syspath) >= 0); - assert_se(sd_device_get_sysattr_value(device, sysattr, &sysattr_value) >= 0); + ASSERT_OK(sd_device_get_syspath(device, &syspath)); + ASSERT_OK(sd_device_get_sysattr_value(device, sysattr, &sysattr_value)); - assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_server, "sender") >= 0); - assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); - - assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_client, "receiver") >= 0); - assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); - assert_se(sd_device_monitor_filter_add_match_sysattr(monitor_client, sysattr, sysattr_value, true) >= 0); - assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); - - assert_se(sd_device_enumerator_new(&e) >= 0); - assert_se(sd_device_enumerator_add_match_sysattr(e, sysattr, sysattr_value, false) >= 0); - FOREACH_DEVICE(e, d) { - const char *p; + prepare_monitor(&monitor_server, &monitor_client); - assert_se(sd_device_get_syspath(d, &p) >= 0); + ASSERT_OK(sd_device_monitor_filter_add_match_sysattr(monitor_client, sysattr, sysattr_value, true)); + ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath)); - assert_se(device_add_property(d, "ACTION", "add") >= 0); - assert_se(device_add_property(d, "SEQNUM", "10") >= 0); - - log_device_debug(d, "Sending device syspath:%s", p); - assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0); - - /* The sysattr filter is not implemented in BPF yet. So, sending multiple devices may fills up - * buffer and device_monitor_send_device() may return EAGAIN. Let's send one device here, - * which should be filtered out by the receiver. */ - break; - } + ASSERT_OK(sd_device_enumerator_new(&e)); + ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, sysattr, sysattr_value, false)); + send_by_enumerator(monitor_server, monitor_client, e, 5, NULL); log_device_info(device, "Sending device syspath:%s", syspath); - assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); - assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); - + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, device)); + ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100); } -static void test_parent_filter(sd_device *device) { +TEST(sd_device_monitor_add_match_parent) { _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; const char *syspath, *parent_syspath; sd_device *parent; int r; - log_device_info(device, "/* %s */", __func__); - - assert_se(sd_device_get_syspath(device, &syspath) >= 0); - r = sd_device_get_parent(device, &parent); + r = prepare_sda(&device); if (r < 0) - return (void) log_device_info(device, "Device does not have parent, skipping."); - assert_se(sd_device_get_syspath(parent, &parent_syspath) >= 0); - - assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_server, "sender") >= 0); - assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); + return (void) log_tests_skipped_errno(r, "Failed to create sd_device for sda"); - assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_client, "receiver") >= 0); - assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); - assert_se(sd_device_monitor_filter_add_match_parent(monitor_client, parent, true) >= 0); - assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); + ASSERT_OK(sd_device_get_syspath(device, &syspath)); - assert_se(sd_device_enumerator_new(&e) >= 0); - FOREACH_DEVICE(e, d) { - const char *p; + r = sd_device_get_parent(device, &parent); + if (r < 0) + return (void) log_tests_skipped("sda does not have parent"); - assert_se(sd_device_get_syspath(d, &p) >= 0); - if (path_startswith(p, parent_syspath)) - continue; + ASSERT_OK(sd_device_get_syspath(parent, &parent_syspath)); - assert_se(device_add_property(d, "ACTION", "add") >= 0); - assert_se(device_add_property(d, "SEQNUM", "10") >= 0); + prepare_monitor(&monitor_server, &monitor_client); - log_device_debug(d, "Sending device syspath:%s", p); - assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0); + ASSERT_OK(sd_device_monitor_filter_add_match_parent(monitor_client, parent, true)); + ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath)); - /* The parent filter is not implemented in BPF yet. So, sending multiple devices may fills up - * buffer and device_monitor_send_device() may return EAGAIN. Let's send one device here, - * which should be filtered out by the receiver. */ - break; - } + ASSERT_OK(sd_device_enumerator_new(&e)); + send_by_enumerator(monitor_server, monitor_client, e, 5, parent_syspath); log_device_info(device, "Sending device syspath:%s", syspath); - assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); - assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); - + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, device)); + ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100); } -static void test_sd_device_monitor_filter_remove(sd_device *device) { +TEST(sd_device_monitor_filter_remove) { _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; const char *syspath; - log_device_info(device, "/* %s */", __func__); - - assert_se(sd_device_get_syspath(device, &syspath) >= 0); + prepare_loopback(&device); - assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_server, "sender") >= 0); - assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0); + ASSERT_OK(sd_device_get_syspath(device, &syspath)); - assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0); - assert_se(sd_device_monitor_set_description(monitor_client, "receiver") >= 0); - assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0); - assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0); + prepare_monitor(&monitor_server, &monitor_client); - assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, "hoge", NULL) >= 0); - assert_se(sd_device_monitor_filter_update(monitor_client) >= 0); + ASSERT_OK(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, "hoge", NULL)); + ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath)); - assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); - assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client), 0) >= 0); + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, device)); + ASSERT_OK(sd_event_run(sd_device_monitor_get_event(monitor_client), 0)); - assert_se(sd_device_monitor_filter_remove(monitor_client) >= 0); + ASSERT_OK(sd_device_monitor_filter_remove(monitor_client)); - assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0); - assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100); + ASSERT_OK(device_monitor_send_device(monitor_server, monitor_client, device)); + ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100); } -int main(int argc, char *argv[]) { - _cleanup_(sd_device_unrefp) sd_device *loopback = NULL, *sda = NULL; - int r; - - test_setup_logging(LOG_INFO); - +static int intro(void) { if (getuid() != 0) return log_tests_skipped("not root"); @@ -305,44 +312,10 @@ int main(int argc, char *argv[]) { if (path_is_read_only_fs("/sys") > 0) return log_tests_skipped("Running in container"); - test_receive_device_fail(); - - assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); - assert_se(device_add_property(loopback, "ACTION", "add") >= 0); - assert_se(device_add_property(loopback, "SEQNUM", "10") >= 0); - assert_se(device_add_tag(loopback, "TEST_SD_DEVICE_MONITOR", true) >= 0); - - test_send_receive_one(loopback, false, false, false); - test_send_receive_one(loopback, true, false, false); - test_send_receive_one(loopback, false, true, false); - test_send_receive_one(loopback, true, true, false); - test_send_receive_one(loopback, true, false, true); - test_send_receive_one(loopback, false, true, true); - test_send_receive_one(loopback, true, true, true); - - test_subsystem_filter(loopback); - test_tag_filter(loopback); - test_sysattr_filter(loopback, "ifindex"); - test_sd_device_monitor_filter_remove(loopback); + if (access("/sys/class/net/lo", F_OK) < 0) + return log_tests_skipped_errno(errno, "Loopback network interface 'lo' does not exist"); - r = sd_device_new_from_subsystem_sysname(&sda, "block", "sda"); - if (r < 0) { - log_info_errno(r, "Failed to create sd_device for sda, skipping remaining tests: %m"); - return 0; - } - - assert_se(device_add_property(sda, "ACTION", "change") >= 0); - assert_se(device_add_property(sda, "SEQNUM", "11") >= 0); - - test_send_receive_one(sda, false, false, false); - test_send_receive_one(sda, true, false, false); - test_send_receive_one(sda, false, true, false); - test_send_receive_one(sda, true, true, false); - test_send_receive_one(sda, true, false, true); - test_send_receive_one(sda, false, true, true); - test_send_receive_one(sda, true, true, true); - - test_parent_filter(sda); - - return 0; + return EXIT_SUCCESS; } + +DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);