summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/test/meson.build1
-rw-r--r--src/test/test-tmpfile-util.c66
-rw-r--r--src/test/test-tmpfiles.c80
3 files changed, 66 insertions, 81 deletions
diff --git a/src/test/meson.build b/src/test/meson.build
index ae4c3f13aa..b57a6bf2f1 100644
--- a/src/test/meson.build
+++ b/src/test/meson.build
@@ -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',
diff --git a/src/test/test-tmpfile-util.c b/src/test/test-tmpfile-util.c
index 415f185a81..455b733422 100644
--- a/src/test/test-tmpfile-util.c
+++ b/src/test/test-tmpfile-util.c
@@ -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
index 0a856200c7..0000000000
--- a/src/test/test-tmpfiles.c
+++ /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);