From: Yu Watanabe Date: Tue, 9 Oct 2018 09:44:41 +0000 (+0900) Subject: test: add test for subsystem filtering of sd_device_enumerator X-Git-Tag: v240~545^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=68db4a0caf2692e0811cbf0f22cadbcbd591af03;p=thirdparty%2Fsystemd.git test: add test for subsystem filtering of sd_device_enumerator --- diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c index fc2d02b4b35..6f4f2e1c6fd 100644 --- a/src/libsystemd/sd-device/test-sd-device.c +++ b/src/libsystemd/sd-device/test-sd-device.c @@ -3,6 +3,7 @@ #include "device-enumerator-private.h" #include "device-private.h" #include "device-util.h" +#include "hashmap.h" #include "string-util.h" #include "tests.h" #include "util.h" @@ -63,9 +64,79 @@ static void test_sd_device_basic(void) { } } +static void test_sd_device_enumerator_filter_subsystem_one(const char *subsystem, Hashmap *h) { + _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; + sd_device *d, *t; + + assert_se(sd_device_enumerator_new(&e) >= 0); + assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0); + + FOREACH_DEVICE(e, d) { + const char *syspath; + + assert_se(sd_device_get_syspath(d, &syspath) >= 0); + assert_se(t = hashmap_remove(h, syspath)); + assert_se(!sd_device_unref(t)); + + log_debug("Removed subsystem:%s syspath:%s", subsystem, syspath); + } + + assert_se(hashmap_isempty(h)); +} + +static void test_sd_device_enumerator_filter_subsystem(void) { + _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; + _cleanup_(hashmap_freep) Hashmap *subsystems; + sd_device *d; + Hashmap *h; + char *s; + + assert_se(subsystems = hashmap_new(&string_hash_ops)); + assert_se(sd_device_enumerator_new(&e) >= 0); + + FOREACH_DEVICE(e, d) { + const char *syspath, *subsystem; + int r; + + assert_se(sd_device_get_syspath(d, &syspath) >= 0); + + r = sd_device_get_subsystem(d, &subsystem); + assert_se(r >= 0 || r == -ENOENT); + if (r < 0) + continue; + + h = hashmap_get(subsystems, subsystem); + if (!h) { + char *str; + assert_se(str = strdup(subsystem)); + assert_se(h = hashmap_new(&string_hash_ops)); + assert_se(hashmap_put(subsystems, str, h) >= 0); + } + + r = hashmap_put(h, syspath, d); + assert_se(r >= 0 || r == -EEXIST); + if (r < 0) { + log_info("Duplicated subsystem:%s syspath:%s", subsystem, syspath); + continue; + } + + assert_se(sd_device_ref(d)); + + log_debug("Added subsystem:%s syspath:%s", subsystem, syspath); + } + + while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) { + test_sd_device_enumerator_filter_subsystem_one(s, h); + hashmap_free(h); + free(s); + } +} + int main(int argc, char **argv) { test_setup_logging(LOG_INFO); test_sd_device_basic(); + test_sd_device_enumerator_filter_subsystem(); + return 0; }