tests: merge test-tmpfiles.c into test-tmpfile-util.c

The former was added in 65b3903ff5,
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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-03-09 21:35:53 +09:00
committed by Yu Watanabe
parent ba0e70673c
commit b2d3e2a0dc
3 changed files with 66 additions and 81 deletions
-1
View 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',
+66
View 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);
-80
View File
@@ -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);