]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-bus-watch-bind: Migrate to new assertion macros
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 7 Nov 2025 11:02:15 +0000 (12:02 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 12 Nov 2025 13:24:14 +0000 (14:24 +0100)
src/libsystemd/sd-bus/test-bus-watch-bind.c

index 158def7f75c9de98fffb2d00f13037e6b4262051..73fe27275501a9e4b04275ae7aa2d99094b74127 100644 (file)
 static int method_foobar(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
         log_info("Got Foobar() call.");
 
-        assert_se(sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0) >= 0);
+        ASSERT_OK(sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0));
         return sd_bus_reply_method_return(m, NULL);
 }
 
 static int method_exit(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
         log_info("Got Exit() call");
 
-        assert_se(sd_bus_reply_method_return(m, NULL) >= 0);
+        ASSERT_OK(sd_bus_reply_method_return(m, NULL));
         /* Simulate D-Bus going away to test the bus_exit_now() path with exit_on_disconnect set */
         bus_enter_closing(sd_bus_message_get_bus(m));
         return 0;
@@ -56,37 +56,37 @@ static void* thread_server(void *p) {
         /* Let's play some games, by slowly creating the socket directory, and renaming it in the middle */
         usleep_safe(100 * USEC_PER_MSEC);
 
-        assert_se(mkdir_parents(path, 0755) >= 0);
+        ASSERT_OK(mkdir_parents(path, 0755));
         usleep_safe(100 * USEC_PER_MSEC);
 
-        assert_se(path_extract_directory(path, &d) >= 0);
-        assert_se(asprintf(&suffixed, "%s.%" PRIx64, d, random_u64()) >= 0);
-        assert_se(rename(d, suffixed) >= 0);
+        ASSERT_OK(path_extract_directory(path, &d));
+        ASSERT_OK(asprintf(&suffixed, "%s.%" PRIx64, d, random_u64()));
+        ASSERT_OK_ERRNO(rename(d, suffixed));
         usleep_safe(100 * USEC_PER_MSEC);
 
-        assert_se(asprintf(&suffixed2, "%s.%" PRIx64, d, random_u64()) >= 0);
-        assert_se(symlink(suffixed2, d) >= 0);
+        ASSERT_OK(asprintf(&suffixed2, "%s.%" PRIx64, d, random_u64()));
+        ASSERT_OK_ERRNO(symlink(suffixed2, d));
         usleep_safe(100 * USEC_PER_MSEC);
 
-        assert_se(path_extract_filename(suffixed, &suffixed_basename) >= 0);
-        assert_se(symlink(suffixed_basename, suffixed2) >= 0);
+        ASSERT_OK(path_extract_filename(suffixed, &suffixed_basename));
+        ASSERT_OK_ERRNO(symlink(suffixed_basename, suffixed2));
         usleep_safe(100 * USEC_PER_MSEC);
 
         socklen_t sa_len;
         r = sockaddr_un_set_path(&u.un, path);
-        assert_se(r >= 0);
+        ASSERT_OK(r);
         sa_len = r;
 
         fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
-        assert_se(fd >= 0);
+        ASSERT_OK_ERRNO(fd);
 
-        assert_se(bind(fd, &u.sa, sa_len) >= 0);
+        ASSERT_OK_ERRNO(bind(fd, &u.sa, sa_len));
         usleep_safe(100 * USEC_PER_MSEC);
 
-        assert_se(listen(fd, SOMAXCONN_DELUXE) >= 0);
+        ASSERT_OK_ERRNO(listen(fd, SOMAXCONN_DELUXE));
         usleep_safe(100 * USEC_PER_MSEC);
 
-        assert_se(touch(path) >= 0);
+        ASSERT_OK(touch(path));
         usleep_safe(100 * USEC_PER_MSEC);
 
         log_debug("Initialized server");
@@ -97,31 +97,31 @@ static void* thread_server(void *p) {
                 sd_id128_t id;
                 int bus_fd, code;
 
-                assert_se(sd_id128_randomize(&id) >= 0);
+                ASSERT_OK(sd_id128_randomize(&id));
 
-                assert_se(sd_event_new(&event) >= 0);
+                ASSERT_OK(sd_event_new(&event));
 
                 bus_fd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
-                assert_se(bus_fd >= 0);
+                ASSERT_OK_ERRNO(bus_fd);
 
                 log_debug("Accepted server connection");
 
-                assert_se(sd_bus_new(&bus) >= 0);
-                assert_se(sd_bus_set_exit_on_disconnect(bus, true) >= 0);
-                assert_se(sd_bus_set_description(bus, "server") >= 0);
-                assert_se(sd_bus_set_fd(bus, bus_fd, bus_fd) >= 0);
-                assert_se(sd_bus_set_server(bus, true, id) >= 0);
-                /* assert_se(sd_bus_set_anonymous(bus, true) >= 0); */
+                ASSERT_OK(sd_bus_new(&bus));
+                ASSERT_OK(sd_bus_set_exit_on_disconnect(bus, true));
+                ASSERT_OK(sd_bus_set_description(bus, "server"));
+                ASSERT_OK(sd_bus_set_fd(bus, bus_fd, bus_fd));
+                ASSERT_OK(sd_bus_set_server(bus, true, id));
+                /* ASSERT_OK(sd_bus_set_anonymous(bus, true)); */
 
-                assert_se(sd_bus_attach_event(bus, event, 0) >= 0);
+                ASSERT_OK(sd_bus_attach_event(bus, event, 0));
 
-                assert_se(sd_bus_add_object_vtable(bus, NULL, "/foo", "foo.TestInterface", vtable, NULL) >= 0);
+                ASSERT_OK(sd_bus_add_object_vtable(bus, NULL, "/foo", "foo.TestInterface", vtable, NULL));
 
-                assert_se(sd_bus_start(bus) >= 0);
+                ASSERT_OK(sd_bus_start(bus));
 
-                assert_se(sd_event_loop(event) >= 0);
+                ASSERT_OK(sd_event_loop(event));
 
-                assert_se(sd_event_get_exit_code(event, &code) >= 0);
+                ASSERT_OK(sd_event_get_exit_code(event, &code));
 
                 if (code > 0)
                         break;
@@ -136,20 +136,18 @@ static void* thread_client1(void *p) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
         const char *path = p, *t;
-        int r;
 
         log_debug("Initializing client1");
 
-        assert_se(sd_bus_new(&bus) >= 0);
-        assert_se(sd_bus_set_description(bus, "client1") >= 0);
+        ASSERT_OK(sd_bus_new(&bus));
+        ASSERT_OK(sd_bus_set_description(bus, "client1"));
 
         t = strjoina("unix:path=", path);
-        assert_se(sd_bus_set_address(bus, t) >= 0);
-        assert_se(sd_bus_set_watch_bind(bus, true) >= 0);
-        assert_se(sd_bus_start(bus) >= 0);
+        ASSERT_OK(sd_bus_set_address(bus, t));
+        ASSERT_OK(sd_bus_set_watch_bind(bus, true));
+        ASSERT_OK(sd_bus_start(bus));
 
-        r = sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Foobar", &error, NULL, NULL);
-        assert_se(r >= 0);
+        ASSERT_OK(sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Foobar", &error, NULL, NULL));
 
         log_debug("Client1 done");
 
@@ -157,8 +155,8 @@ static void* thread_client1(void *p) {
 }
 
 static int client2_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
-        assert_se(sd_bus_message_is_method_error(m, NULL) == 0);
-        assert_se(sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0) >= 0);
+        ASSERT_OK_ZERO(sd_bus_message_is_method_error(m, NULL));
+        ASSERT_OK(sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0));
         return 0;
 }
 
@@ -169,19 +167,19 @@ static void* thread_client2(void *p) {
 
         log_debug("Initializing client2");
 
-        assert_se(sd_event_new(&event) >= 0);
-        assert_se(sd_bus_new(&bus) >= 0);
-        assert_se(sd_bus_set_description(bus, "client2") >= 0);
+        ASSERT_OK(sd_event_new(&event));
+        ASSERT_OK(sd_bus_new(&bus));
+        ASSERT_OK(sd_bus_set_description(bus, "client2"));
 
         t = strjoina("unix:path=", path);
-        assert_se(sd_bus_set_address(bus, t) >= 0);
-        assert_se(sd_bus_set_watch_bind(bus, true) >= 0);
-        assert_se(sd_bus_attach_event(bus, event, 0) >= 0);
-        assert_se(sd_bus_start(bus) >= 0);
+        ASSERT_OK(sd_bus_set_address(bus, t));
+        ASSERT_OK(sd_bus_set_watch_bind(bus, true));
+        ASSERT_OK(sd_bus_attach_event(bus, event, 0));
+        ASSERT_OK(sd_bus_start(bus));
 
-        assert_se(sd_bus_call_method_async(bus, NULL, "foo.bar", "/foo", "foo.TestInterface", "Foobar", client2_callback, NULL, NULL) >= 0);
+        ASSERT_OK(sd_bus_call_method_async(bus, NULL, "foo.bar", "/foo", "foo.TestInterface", "Foobar", client2_callback, NULL, NULL));
 
-        assert_se(sd_event_loop(event) >= 0);
+        ASSERT_OK(sd_event_loop(event));
 
         log_debug("Client2 done");
 
@@ -192,15 +190,15 @@ static void request_exit(const char *path) {
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
         const char *t;
 
-        assert_se(sd_bus_new(&bus) >= 0);
+        ASSERT_OK(sd_bus_new(&bus));
 
         t = strjoina("unix:path=", path);
-        assert_se(sd_bus_set_address(bus, t) >= 0);
-        assert_se(sd_bus_set_watch_bind(bus, true) >= 0);
-        assert_se(sd_bus_set_description(bus, "request-exit") >= 0);
-        assert_se(sd_bus_start(bus) >= 0);
+        ASSERT_OK(sd_bus_set_address(bus, t));
+        ASSERT_OK(sd_bus_set_watch_bind(bus, true));
+        ASSERT_OK(sd_bus_set_description(bus, "request-exit"));
+        ASSERT_OK(sd_bus_start(bus));
 
-        assert_se(sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Exit", NULL, NULL, NULL) >= 0);
+        ASSERT_OK(sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Exit", NULL, NULL, NULL));
 }
 
 int main(int argc, char *argv[]) {
@@ -212,20 +210,20 @@ int main(int argc, char *argv[]) {
 
         /* We use /dev/shm here rather than /tmp, since some weird distros might set up /tmp as some weird fs that
          * doesn't support inotify properly. */
-        assert_se(mkdtemp_malloc("/dev/shm/systemd-watch-bind-XXXXXX", &d) >= 0);
+        ASSERT_OK(mkdtemp_malloc("/dev/shm/systemd-watch-bind-XXXXXX", &d));
 
         path = strjoina(d, "/this/is/a/socket");
 
-        assert_se(pthread_create(&server, NULL, thread_server, path) == 0);
-        assert_se(pthread_create(&client1, NULL, thread_client1, path) == 0);
-        assert_se(pthread_create(&client2, NULL, thread_client2, path) == 0);
+        ASSERT_OK(-pthread_create(&server, NULL, thread_server, path));
+        ASSERT_OK(-pthread_create(&client1, NULL, thread_client1, path));
+        ASSERT_OK(-pthread_create(&client2, NULL, thread_client2, path));
 
-        assert_se(pthread_join(client1, NULL) == 0);
-        assert_se(pthread_join(client2, NULL) == 0);
+        ASSERT_OK(-pthread_join(client1, NULL));
+        ASSERT_OK(-pthread_join(client2, NULL));
 
         request_exit(path);
 
-        assert_se(pthread_join(server, NULL) == 0);
+        ASSERT_OK(-pthread_join(server, NULL));
 
         return 0;
 }