]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add test for subsystem filtering of sd_device_enumerator
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 9 Oct 2018 09:44:41 +0000 (18:44 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 13 Oct 2018 12:45:50 +0000 (21:45 +0900)
src/libsystemd/sd-device/test-sd-device.c

index fc2d02b4b35fa8dbe7492896775e9e4b49f03929..6f4f2e1c6fd52c66942bdd118a89a9893bfe6229 100644 (file)
@@ -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;
 }