]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: use new ASSERT_OK_OR macro
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 28 Jun 2025 18:12:32 +0000 (20:12 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 1 Jul 2025 15:51:49 +0000 (17:51 +0200)
src/test/test-audit-util.c
src/test/test-cgroup-util.c
src/test/test-execute.c
src/test/test-process-util.c

index af2813d6170afdbaf267f1fe03213845a9e36f5e..61521f33978a5b69607292962bf6c03afdcca615 100644 (file)
@@ -13,18 +13,14 @@ TEST(audit_loginuid_from_pid) {
         ASSERT_OK(pidref_set_pid(&pid1, 1));
 
         uid_t uid;
-        r = audit_loginuid_from_pid(&self, &uid);
-        if (r != -ENODATA)
-                ASSERT_OK(r);
+        ASSERT_OK_OR(r = audit_loginuid_from_pid(&self, &uid), -ENODATA);
         if (r >= 0)
                 log_info("self audit login uid: " UID_FMT, uid);
 
         ASSERT_ERROR(audit_loginuid_from_pid(&pid1, &uid), ENODATA);
 
         uint32_t sessionid;
-        r = audit_session_from_pid(&self, &sessionid);
-        if (r != -ENODATA)
-                ASSERT_OK(r);
+        ASSERT_OK_OR(r = audit_session_from_pid(&self, &sessionid), -ENODATA);
         if (r >= 0)
                 log_info("self audit session id: %" PRIu32, sessionid);
 
index 0512532d0bf00b887e68b9bccf15e1ddc9cba168..8debc21527137f40197e2a4a40c3a53b54517dda 100644 (file)
@@ -232,28 +232,21 @@ TEST(proc, .sd_booted = true) {
                         continue;
 
                 r = cg_pid_get_path_shifted(pid.pid, NULL, &path_shifted);
-                if (r != -ESRCH)
-                        ASSERT_OK(r);
+                ASSERT_OK_OR(r, -ESRCH);
                 r = cg_pidref_get_unit(&pid, &unit);
-                if (r != -ESRCH)
-                        ASSERT_OK(r);
+                ASSERT_OK_OR(r, -ESRCH);
                 r = cg_pid_get_slice(pid.pid, &slice);
-                if (r != -ESRCH)
-                        ASSERT_OK(r);
+                ASSERT_OK_OR(r, -ESRCH);
 
                 /* Not all processes belong to a specific user or a machine */
                 r = cg_pidref_get_owner_uid(&pid, &uid);
-                if (!IN_SET(r, -ESRCH, -ENXIO))
-                        ASSERT_OK(r);
+                ASSERT_OK_OR(r, -ESRCH, -ENXIO);
                 r = cg_pidref_get_session(&pid, &session);
-                if (!IN_SET(r, -ESRCH, -ENXIO))
-                        ASSERT_OK(r);
+                ASSERT_OK_OR(r, -ESRCH, -ENXIO);
                 r = cg_pid_get_user_unit(pid.pid, &user_unit);
-                if (!IN_SET(r, -ESRCH, -ENXIO))
-                        ASSERT_OK(r);
+                ASSERT_OK_OR(r, -ESRCH, -ENXIO);
                 r = cg_pid_get_machine_name(pid.pid, &machine);
-                if (!IN_SET(r, -ESRCH, -ENOENT))
-                        ASSERT_OK(r);
+                ASSERT_OK_OR(r, -ESRCH, -ENOENT);
 
                 log_debug(PID_FMT": %s, %s, "UID_FMT", %s, %s, %s, %s, %s",
                           pid.pid,
index 621bbd4990033c30dec768bf67067dcccf3a3ad5..23c1fe2eedbabc35669bfc4815c5930e883a21b3 100644 (file)
@@ -223,9 +223,7 @@ static void start_parent_slices(Unit *unit) {
         slice = UNIT_GET_SLICE(unit);
         if (slice) {
                 start_parent_slices(slice);
-                int r = unit_start(slice, NULL);
-                if (r != -EALREADY)
-                        ASSERT_OK(r);
+                ASSERT_OK_OR(unit_start(slice, NULL), -EALREADY);
         }
 }
 
@@ -1472,7 +1470,6 @@ static int prepare_ns(const char *process_name) {
         ASSERT_OK(r);
         if (r == 0) {
                 _cleanup_free_ char *unit_dir = NULL, *build_dir = NULL, *build_dir_mount = NULL;
-                int ret;
 
                 const char *coverage = getenv("COVERAGE_BUILD_DIR");
                 if (!coverage)
@@ -1494,9 +1491,7 @@ static int prepare_ns(const char *process_name) {
                 ASSERT_OK(copy_directory_at(AT_FDCWD, unit_dir, AT_FDCWD, PRIVATE_UNIT_DIR, COPY_MERGE_EMPTY));
 
                 /* Mount tmpfs on the following directories to make not StateDirectory= or friends disturb the host. */
-                ret = get_build_exec_dir(&build_dir);
-                if (ret != -ENOEXEC)
-                        ASSERT_OK(ret);
+                ASSERT_OK_OR(get_build_exec_dir(&build_dir), -ENOEXEC);
 
                 if (build_dir) {
                         /* Account for a build directory being in one of the soon-to-be-tmpfs directories. If we
@@ -1512,11 +1507,11 @@ static int prepare_ns(const char *process_name) {
                         ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, NULL));
 
                 if (build_dir_mount) {
-                        ret = RET_NERRNO(access(build_dir, F_OK));
-                        if (ret != -ENOENT)
-                                ASSERT_OK(ret);
+                        int k;
 
-                        if (ret == -ENOENT) {
+                        ASSERT_OK_OR(k = RET_NERRNO(access(build_dir, F_OK)), -ENOENT);
+
+                        if (k == -ENOENT) {
                                 /* The build directory got overmounted by tmpfs, so let's use the "backup" bind mount to
                                  * bring it back. */
                                 ASSERT_OK(mkdir_p(build_dir, 0755));
index 376b20b04848083148ac25148b9fb616997175f8..c0095a749c9f756861b18b7e1536225403df48cd 100644 (file)
@@ -86,9 +86,7 @@ static void test_pid_get_comm_one(pid_t pid) {
 
         ASSERT_TRUE(pid_is_kernel_thread(pid) == 0 || pid != 1);
 
-        r = get_process_exe(pid, &f);
-        if (r != -EACCES)
-                ASSERT_OK(r);
+        ASSERT_OK_OR(get_process_exe(pid, &f), -EACCES);
         log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
 
         ASSERT_OK_ZERO(pid_get_uid(pid, &u));
@@ -97,9 +95,7 @@ static void test_pid_get_comm_one(pid_t pid) {
         ASSERT_OK_ZERO(get_process_gid(pid, &g));
         log_info("PID"PID_FMT" GID: "GID_FMT, pid, g);
 
-        r = get_process_environ(pid, &env);
-        if (r != -EACCES)
-                ASSERT_OK(r);
+        ASSERT_OK_OR(get_process_environ(pid, &env), -EACCES);
         log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno);
 
         if (!detect_container() && pid == 1)
@@ -166,9 +162,7 @@ TEST(pid_get_cmdline) {
 
         for (;;) {
                 pid_t pid;
-
-                r = proc_dir_read(d, &pid);
-                ASSERT_OK(r);
+                ASSERT_OK(r = proc_dir_read(d, &pid));
 
                 if (r == 0) /* EOF */
                         break;