8d419f
From 4895d281d18e244d8238c1f77e597ce43310531c Mon Sep 17 00:00:00 2001
8d419f
From: Yu Watanabe <watanabe.yu+github@gmail.com>
8d419f
Date: Sun, 17 Apr 2022 07:58:45 +0900
8d419f
Subject: [PATCH] test: add several tests for track item
8d419f
8d419f
(cherry picked from commit 056a18e465bedb1bd35ce0bf78831be168c636cb)
8d419f
8d419f
Related: #2087652
8d419f
---
8d419f
 src/libsystemd/sd-bus/test-bus-track.c | 58 +++++++++++++++++++++++++-
8d419f
 1 file changed, 57 insertions(+), 1 deletion(-)
8d419f
8d419f
diff --git a/src/libsystemd/sd-bus/test-bus-track.c b/src/libsystemd/sd-bus/test-bus-track.c
8d419f
index 238934a880..5604e84f52 100644
8d419f
--- a/src/libsystemd/sd-bus/test-bus-track.c
8d419f
+++ b/src/libsystemd/sd-bus/test-bus-track.c
8d419f
@@ -10,6 +10,7 @@
8d419f
 
8d419f
 static bool track_cb_called_x = false;
8d419f
 static bool track_cb_called_y = false;
8d419f
+static bool track_destroy_called_z = false;
8d419f
 
8d419f
 static int track_cb_x(sd_bus_track *t, void *userdata) {
8d419f
 
8d419f
@@ -39,9 +40,17 @@ static int track_cb_y(sd_bus_track *t, void *userdata) {
8d419f
         return 0;
8d419f
 }
8d419f
 
8d419f
+static int track_cb_z(sd_bus_track *t, void *userdata) {
8d419f
+        assert_not_reached();
8d419f
+}
8d419f
+
8d419f
+static void track_destroy_z(void *userdata) {
8d419f
+        track_destroy_called_z = true;
8d419f
+}
8d419f
+
8d419f
 int main(int argc, char *argv[]) {
8d419f
         _cleanup_(sd_event_unrefp) sd_event *event = NULL;
8d419f
-        _cleanup_(sd_bus_track_unrefp) sd_bus_track *x = NULL, *y = NULL;
8d419f
+        _cleanup_(sd_bus_track_unrefp) sd_bus_track *x = NULL, *y = NULL, *z = NULL;
8d419f
         _cleanup_(sd_bus_unrefp) sd_bus *a = NULL, *b = NULL;
8d419f
         bool use_system_bus = false;
8d419f
         const char *unique;
8d419f
@@ -83,6 +92,53 @@ int main(int argc, char *argv[]) {
8d419f
 
8d419f
         assert_se(sd_bus_track_add_name(y, unique) >= 0);
8d419f
 
8d419f
+        /* Basic tests. */
8d419f
+        assert_se(sd_bus_track_new(a, &z, track_cb_z, NULL) >= 0);
8d419f
+
8d419f
+        /* non-recursive case */
8d419f
+        assert_se(sd_bus_track_set_recursive(z, false) >= 0);
8d419f
+        assert_se(sd_bus_track_get_recursive(z) == 0);
8d419f
+        assert_se(!sd_bus_track_contains(z, unique));
8d419f
+        assert_se(sd_bus_track_count_name(z, unique) == 0);
8d419f
+        assert_se(sd_bus_track_remove_name(z, unique) == 0);
8d419f
+        assert_se(sd_bus_track_add_name(z, unique) >= 0);
8d419f
+        assert_se(sd_bus_track_add_name(z, unique) >= 0);
8d419f
+        assert_se(sd_bus_track_add_name(z, unique) >= 0);
8d419f
+        assert_se(sd_bus_track_set_recursive(z, true) == -EBUSY);
8d419f
+        assert_se(sd_bus_track_contains(z, unique));
8d419f
+        assert_se(sd_bus_track_count_name(z, unique) == 1);
8d419f
+        assert_se(sd_bus_track_remove_name(z, unique) == 1);
8d419f
+        assert_se(!sd_bus_track_contains(z, unique));
8d419f
+        assert_se(sd_bus_track_count_name(z, unique) == 0);
8d419f
+        assert_se(sd_bus_track_remove_name(z, unique) == 0);
8d419f
+
8d419f
+        /* recursive case */
8d419f
+        assert_se(sd_bus_track_set_recursive(z, true) >= 0);
8d419f
+        assert_se(sd_bus_track_get_recursive(z) == 1);
8d419f
+        assert_se(!sd_bus_track_contains(z, unique));
8d419f
+        assert_se(sd_bus_track_count_name(z, unique) == 0);
8d419f
+        assert_se(sd_bus_track_remove_name(z, unique) == 0);
8d419f
+        assert_se(sd_bus_track_add_name(z, unique) >= 0);
8d419f
+        assert_se(sd_bus_track_add_name(z, unique) >= 0);
8d419f
+        assert_se(sd_bus_track_add_name(z, unique) >= 0);
8d419f
+        assert_se(sd_bus_track_set_recursive(z, false) == -EBUSY);
8d419f
+        assert_se(sd_bus_track_contains(z, unique));
8d419f
+        assert_se(sd_bus_track_count_name(z, unique) == 3);
8d419f
+        assert_se(sd_bus_track_remove_name(z, unique) == 1);
8d419f
+        assert_se(sd_bus_track_contains(z, unique));
8d419f
+        assert_se(sd_bus_track_count_name(z, unique) == 2);
8d419f
+        assert_se(sd_bus_track_remove_name(z, unique) == 1);
8d419f
+        assert_se(sd_bus_track_contains(z, unique));
8d419f
+        assert_se(sd_bus_track_count_name(z, unique) == 1);
8d419f
+        assert_se(sd_bus_track_remove_name(z, unique) == 1);
8d419f
+        assert_se(!sd_bus_track_contains(z, unique));
8d419f
+        assert_se(sd_bus_track_count_name(z, unique) == 0);
8d419f
+        assert_se(sd_bus_track_remove_name(z, unique) == 0);
8d419f
+
8d419f
+        assert_se(sd_bus_track_set_destroy_callback(z, track_destroy_z) >= 0);
8d419f
+        z = sd_bus_track_unref(z);
8d419f
+        assert_se(track_destroy_called_z);
8d419f
+
8d419f
         /* Now make b's name disappear */
8d419f
         sd_bus_close(b);
8d419f