]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: merge test-tmpfiles.c into test-tmpfile-util.c
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 9 Mar 2023 10:04:20 +0000 (11:04 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 9 Mar 2023 12:35:53 +0000 (21:35 +0900)
The former was added in 65b3903ff576488eaabb51d3c4fbf9c73d867d7c,
but the name is confusing: the test has nothing to do with systemd-tmpfiles.
It had one function that mostly tested functions from tmpfile-util.c, so
just move it into the latter.

src/test/meson.build
src/test/test-tmpfile-util.c
src/test/test-tmpfiles.c [deleted file]

index ae4c3f13aa89872d2be7bcde8b45d9ffee764f86..b57a6bf2f17bbee02160154927961de027a5f809 100644 (file)
@@ -157,7 +157,6 @@ simple_tests += files(
         'test-sysctl-util.c',
         'test-terminal-util.c',
         'test-tmpfile-util.c',
-        'test-tmpfiles.c',
         'test-tpm2.c',
         'test-udev-util.c',
         'test-uid-alloc-range.c',
index 415f185a817b5dde5f8ed242d59d344cd3440ebd..455b733422af8e5a20bdecdeebb6181f095be6d0 100644 (file)
@@ -2,8 +2,13 @@
 
 #include "alloc-util.h"
 #include "errno-util.h"
+#include "fd-util.h"
+#include "fileio.h"
+#include "format-util.h"
+#include "fs-util.h"
 #include "log.h"
 #include "path-util.h"
+#include "process-util.h"
 #include "string-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
@@ -237,4 +242,65 @@ TEST(tempfn_random_child) {
         test_tempfn_random_child_one(p, "hoge", q, 0);
 }
 
+TEST(link_tmpfile) {
+        _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL;
+        _cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
+        const char *p = saved_argv[1] ?: "/tmp";
+        char *pattern;
+
+        pattern = strjoina(p, "/systemd-test-XXXXXX");
+
+        fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
+        assert_se(fd >= 0);
+
+        assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd) > 0);
+        (void) system(cmd);
+        assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
+        log_debug("link1: %s", ans);
+        assert_se(endswith(ans, " (deleted)"));
+
+        fd2 = mkostemp_safe(pattern);
+        assert_se(fd2 >= 0);
+        assert_se(unlink(pattern) == 0);
+
+        assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
+        (void) system(cmd2);
+        assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
+        log_debug("link2: %s", ans2);
+        assert_se(endswith(ans2, " (deleted)"));
+
+        pattern = strjoina(p, "/tmpfiles-test");
+        assert_se(tempfn_random(pattern, NULL, &d) >= 0);
+
+        fd = safe_close(fd);
+        fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
+        assert_se(fd >= 0);
+        assert_se(write(fd, "foobar\n", 7) == 7);
+
+        assert_se(touch(d) >= 0);
+        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) == -EEXIST);
+        assert_se(unlink(d) >= 0);
+        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) >= 0);
+
+        assert_se(read_one_line_file(d, &line) >= 0);
+        assert_se(streq(line, "foobar"));
+
+        fd = safe_close(fd);
+        tmp = mfree(tmp);
+
+        fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
+        assert_se(fd >= 0);
+
+        assert_se(write(fd, "waumiau\n", 8) == 8);
+
+        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) == -EEXIST);
+        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ true) >= 0);
+
+        line = mfree(line);
+        assert_se(read_one_line_file(d, &line) >= 0);
+        assert_se(streq(line, "waumiau"));
+
+        assert_se(unlink(d) >= 0);
+}
+
 DEFINE_TEST_MAIN(LOG_DEBUG);
diff --git a/src/test/test-tmpfiles.c b/src/test/test-tmpfiles.c
deleted file mode 100644 (file)
index 0a85620..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include "alloc-util.h"
-#include "fd-util.h"
-#include "fileio.h"
-#include "format-util.h"
-#include "fs-util.h"
-#include "log.h"
-#include "process-util.h"
-#include "string-util.h"
-#include "tests.h"
-#include "tmpfile-util.h"
-
-TEST(tmpfiles) {
-        _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL;
-        _cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
-        const char *p = saved_argv[1] ?: "/tmp";
-        char *pattern;
-
-        pattern = strjoina(p, "/systemd-test-XXXXXX");
-
-        fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
-        assert_se(fd >= 0);
-
-        assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd) > 0);
-        (void) system(cmd);
-        assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
-        log_debug("link1: %s", ans);
-        assert_se(endswith(ans, " (deleted)"));
-
-        fd2 = mkostemp_safe(pattern);
-        assert_se(fd2 >= 0);
-        assert_se(unlink(pattern) == 0);
-
-        assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
-        (void) system(cmd2);
-        assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
-        log_debug("link2: %s", ans2);
-        assert_se(endswith(ans2, " (deleted)"));
-
-        pattern = strjoina(p, "/tmpfiles-test");
-        assert_se(tempfn_random(pattern, NULL, &d) >= 0);
-
-        fd = safe_close(fd);
-        fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
-        assert_se(fd >= 0);
-        assert_se(write(fd, "foobar\n", 7) == 7);
-
-        assert_se(touch(d) >= 0);
-        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) == -EEXIST);
-        assert_se(unlink(d) >= 0);
-        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) >= 0);
-
-        assert_se(read_one_line_file(d, &line) >= 0);
-        assert_se(streq(line, "foobar"));
-
-        fd = safe_close(fd);
-        tmp = mfree(tmp);
-
-        fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
-        assert_se(fd >= 0);
-
-        assert_se(write(fd, "waumiau\n", 8) == 8);
-
-        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ false) == -EEXIST);
-        assert_se(link_tmpfile(fd, tmp, d, /* replace= */ true) >= 0);
-
-        line = mfree(line);
-        assert_se(read_one_line_file(d, &line) >= 0);
-        assert_se(streq(line, "waumiau"));
-
-        assert_se(unlink(d) >= 0);
-}
-
-DEFINE_TEST_MAIN(LOG_DEBUG);