]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: use _cleanup_ for temp. files
authorDavid Tardon <dtardon@redhat.com>
Fri, 21 Apr 2023 14:25:31 +0000 (16:25 +0200)
committerDavid Tardon <dtardon@redhat.com>
Fri, 21 Apr 2023 14:44:05 +0000 (16:44 +0200)
src/test/test-acl-util.c
src/test/test-copy.c
src/test/test-fd-util.c
src/test/test-fdset.c
src/test/test-hostname-setup.c
src/test/test-stat-util.c
src/test/test-terminal-util.c

index 2c406893df882a059d6b7300f4c4cbdb777ac2e2..093eaaa01b600810c1b7885aacaca0b5d214cb9c 100644 (file)
@@ -9,13 +9,14 @@
 #include "errno-util.h"
 #include "fd-util.h"
 #include "format-util.h"
+#include "fs-util.h"
 #include "string-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
 #include "user-util.h"
 
 TEST_RET(add_acls_for_user) {
-        char fn[] = "/tmp/test-empty.XXXXXX";
+        _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-empty.XXXXXX";
         _cleanup_close_ int fd = -EBADF;
         char *cmd;
         uid_t uid;
@@ -65,7 +66,6 @@ TEST_RET(add_acls_for_user) {
         cmd = strjoina("getfacl -p ", fn);
         assert_se(system(cmd) == 0);
 
-        (void) unlink(fn);
         return 0;
 }
 
index df08b7ed618734b32630d266a84d0bc80bb2893b..38855fd45e07411e5d7d2af96b177b75b03069e8 100644 (file)
@@ -24,8 +24,8 @@
 
 TEST(copy_file) {
         _cleanup_free_ char *buf = NULL;
-        char fn[] = "/tmp/test-copy_file.XXXXXX";
-        char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
+        _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-copy_file.XXXXXX";
+        _cleanup_(unlink_tempfilep) char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
         size_t sz = 0;
         int fd;
 
@@ -44,9 +44,6 @@ TEST(copy_file) {
         assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
         assert_se(streq(buf, "foo bar bar bar foo\n"));
         assert_se(sz == 20);
-
-        unlink(fn);
-        unlink(fn_copy);
 }
 
 static bool read_file_at_and_streq(int dir_fd, const char *path, const char *expected) {
@@ -108,8 +105,8 @@ TEST(copy_tree_replace_dirs) {
 }
 
 TEST(copy_file_fd) {
-        char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
-        char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
+        _cleanup_(unlink_tempfilep) char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
+        _cleanup_(unlink_tempfilep) char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
         _cleanup_close_ int in_fd = -EBADF, out_fd = -EBADF;
         const char *text = "boohoo\nfoo\n\tbar\n";
         char buf[64] = {};
@@ -126,9 +123,6 @@ TEST(copy_file_fd) {
 
         assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text));
         assert_se(streq(buf, text));
-
-        unlink(in_fn);
-        unlink(out_fn);
 }
 
 TEST(copy_tree) {
@@ -293,8 +287,8 @@ TEST(copy_bytes) {
 }
 
 static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) {
-        char fn2[] = "/tmp/test-copy-file-XXXXXX";
-        char fn3[] = "/tmp/test-copy-file-XXXXXX";
+        _cleanup_(unlink_tempfilep) char fn2[] = "/tmp/test-copy-file-XXXXXX";
+        _cleanup_(unlink_tempfilep) char fn3[] = "/tmp/test-copy-file-XXXXXX";
         _cleanup_close_ int fd = -EBADF, fd2 = -EBADF, fd3 = -EBADF;
         int r;
         struct stat buf, buf2, buf3;
@@ -343,9 +337,6 @@ static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink,
                 assert_se(buf3.st_size == buf2.st_size);
         else
                 assert_se((uint64_t) buf3.st_size == max_bytes);
-
-        unlink(fn2);
-        unlink(fn3);
 }
 
 TEST(copy_bytes_regular_file) {
@@ -392,8 +383,8 @@ TEST(copy_proc) {
 }
 
 TEST_RET(copy_holes) {
-        char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX";
-        char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX";
+        _cleanup_(unlink_tempfilep) char fn[] = "/var/tmp/test-copy-hole-fd-XXXXXX";
+        _cleanup_(unlink_tempfilep) char fn_copy[] = "/var/tmp/test-copy-hole-fd-XXXXXX";
         struct stat stat;
         off_t blksz;
         int r, fd, fd_copy;
@@ -441,9 +432,6 @@ TEST_RET(copy_holes) {
         close(fd);
         close(fd_copy);
 
-        unlink(fn);
-        unlink(fn_copy);
-
         return 0;
 }
 
index c902f83784b321db4e1e63c352942a234556f2fe..20d412f5c6c82292644e47d365a5bd1880db986c 100644 (file)
@@ -30,9 +30,9 @@
 
 TEST(close_many) {
         int fds[3];
-        char name0[] = "/tmp/test-close-many.XXXXXX";
-        char name1[] = "/tmp/test-close-many.XXXXXX";
-        char name2[] = "/tmp/test-close-many.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name0[] = "/tmp/test-close-many.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name1[] = "/tmp/test-close-many.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name2[] = "/tmp/test-close-many.XXXXXX";
 
         fds[0] = mkostemp_safe(name0);
         fds[1] = mkostemp_safe(name1);
@@ -45,22 +45,16 @@ TEST(close_many) {
         assert_se(fcntl(fds[2], F_GETFD) >= 0);
 
         safe_close(fds[2]);
-
-        unlink(name0);
-        unlink(name1);
-        unlink(name2);
 }
 
 TEST(close_nointr) {
-        char name[] = "/tmp/test-test-close_nointr.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-test-close_nointr.XXXXXX";
         int fd;
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
         assert_se(close_nointr(fd) >= 0);
         assert_se(close_nointr(fd) < 0);
-
-        unlink(name);
 }
 
 TEST(same_fd) {
index aa136a633f585a52122e4ae32f86f6948171615c..e2b8f94833079371eeec14dc007fff9eb3b47616 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "fd-util.h"
 #include "fdset.h"
+#include "fs-util.h"
 #include "macro.h"
 #include "tests.h"
 #include "tmpfile-util.h"
 TEST(fdset_new_fill) {
         int fd = -EBADF;
         _cleanup_fdset_free_ FDSet *fdset = NULL;
-        char name[] = "/tmp/test-fdset_new_fill.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_new_fill.XXXXXX";
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
         assert_se(fdset_new_fill(&fdset) >= 0);
         assert_se(fdset_contains(fdset, fd));
-
-        unlink(name);
 }
 
 TEST(fdset_put_dup) {
         _cleanup_close_ int fd = -EBADF;
         int copyfd = -EBADF;
         _cleanup_fdset_free_ FDSet *fdset = NULL;
-        char name[] = "/tmp/test-fdset_put_dup.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_put_dup.XXXXXX";
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
@@ -37,15 +36,13 @@ TEST(fdset_put_dup) {
         assert_se(copyfd >= 0 && copyfd != fd);
         assert_se(fdset_contains(fdset, copyfd));
         assert_se(!fdset_contains(fdset, fd));
-
-        unlink(name);
 }
 
 TEST(fdset_cloexec) {
         int fd = -EBADF;
         _cleanup_fdset_free_ FDSet *fdset = NULL;
         int flags = -1;
-        char name[] = "/tmp/test-fdset_cloexec.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_cloexec.XXXXXX";
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
@@ -63,8 +60,6 @@ TEST(fdset_cloexec) {
         flags = fcntl(fd, F_GETFD);
         assert_se(flags >= 0);
         assert_se(flags & FD_CLOEXEC);
-
-        unlink(name);
 }
 
 TEST(fdset_close_others) {
@@ -72,7 +67,7 @@ TEST(fdset_close_others) {
         int copyfd = -EBADF;
         _cleanup_fdset_free_ FDSet *fdset = NULL;
         int flags = -1;
-        char name[] = "/tmp/test-fdset_close_others.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_close_others.XXXXXX";
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
@@ -87,14 +82,12 @@ TEST(fdset_close_others) {
         assert_se(flags < 0);
         flags = fcntl(copyfd, F_GETFD);
         assert_se(flags >= 0);
-
-        unlink(name);
 }
 
 TEST(fdset_remove) {
         _cleanup_close_ int fd = -EBADF;
         _cleanup_fdset_free_ FDSet *fdset = NULL;
-        char name[] = "/tmp/test-fdset_remove.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_remove.XXXXXX";
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
@@ -106,14 +99,12 @@ TEST(fdset_remove) {
         assert_se(!fdset_contains(fdset, fd));
 
         assert_se(fcntl(fd, F_GETFD) >= 0);
-
-        unlink(name);
 }
 
 TEST(fdset_iterate) {
         int fd = -EBADF;
         _cleanup_fdset_free_ FDSet *fdset = NULL;
-        char name[] = "/tmp/test-fdset_iterate.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_iterate.XXXXXX";
         int c = 0;
         int a;
 
@@ -131,14 +122,12 @@ TEST(fdset_iterate) {
                 assert_se(a == fd);
         }
         assert_se(c == 1);
-
-        unlink(name);
 }
 
 TEST(fdset_isempty) {
         int fd;
         _cleanup_fdset_free_ FDSet *fdset = NULL;
-        char name[] = "/tmp/test-fdset_isempty.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_isempty.XXXXXX";
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
@@ -149,14 +138,12 @@ TEST(fdset_isempty) {
         assert_se(fdset_isempty(fdset));
         assert_se(fdset_put(fdset, fd) >= 0);
         assert_se(!fdset_isempty(fdset));
-
-        unlink(name);
 }
 
 TEST(fdset_steal_first) {
         int fd;
         _cleanup_fdset_free_ FDSet *fdset = NULL;
-        char name[] = "/tmp/test-fdset_steal_first.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fdset_steal_first.XXXXXX";
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
@@ -169,8 +156,6 @@ TEST(fdset_steal_first) {
         assert_se(fdset_steal_first(fdset) == fd);
         assert_se(fdset_steal_first(fdset) < 0);
         assert_se(fdset_put(fdset, fd) >= 0);
-
-        unlink(name);
 }
 
 TEST(fdset_new_array) {
index 241b197f47e3733079121d1dfa69a462710e6731..94e5ece5602aa155d04d0f5a239f14f1c740d162 100644 (file)
@@ -4,13 +4,14 @@
 
 #include "alloc-util.h"
 #include "fileio.h"
+#include "fs-util.h"
 #include "hostname-setup.h"
 #include "string-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
 
 TEST(read_etc_hostname) {
-        char path[] = "/tmp/hostname.XXXXXX";
+        _cleanup_(unlink_tempfilep) char path[] = "/tmp/hostname.XXXXXX";
         char *hostname;
         int fd;
 
@@ -54,8 +55,6 @@ TEST(read_etc_hostname) {
         /* nonexisting file */
         assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT);
         assert_se(hostname == (char*) 0x1234);  /* does not touch argument on error */
-
-        unlink(path);
 }
 
 TEST(hostname_setup) {
index 6b0f2d803371c3a8b50899135e0854216aa17a1f..f79b05a4d1b629075f3576fbfb0201077e148796 100644 (file)
@@ -44,8 +44,8 @@ TEST(null_or_empty_path_with_root) {
 
 TEST(files_same) {
         _cleanup_close_ int fd = -EBADF;
-        char name[] = "/tmp/test-files_same.XXXXXX";
-        char name_alias[] = "/tmp/test-files_same.alias";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-files_same.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name_alias[] = "/tmp/test-files_same.alias";
 
         fd = mkostemp_safe(name);
         assert_se(fd >= 0);
@@ -55,14 +55,11 @@ TEST(files_same) {
         assert_se(files_same(name, name, AT_SYMLINK_NOFOLLOW));
         assert_se(files_same(name, name_alias, 0));
         assert_se(!files_same(name, name_alias, AT_SYMLINK_NOFOLLOW));
-
-        unlink(name);
-        unlink(name_alias);
 }
 
 TEST(is_symlink) {
-        char name[] = "/tmp/test-is_symlink.XXXXXX";
-        char name_link[] = "/tmp/test-is_symlink.link";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-is_symlink.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name_link[] = "/tmp/test-is_symlink.link";
         _cleanup_close_ int fd = -EBADF;
 
         fd = mkostemp_safe(name);
@@ -72,9 +69,6 @@ TEST(is_symlink) {
         assert_se(is_symlink(name) == 0);
         assert_se(is_symlink(name_link) == 1);
         assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
-
-        unlink(name);
-        unlink(name_link);
 }
 
 TEST(path_is_fs_type) {
index 8d5b24e5025fc2c5bb3e188bc26018befec7d912..a2b7101573404f490c0e9eea325e24d876cdbcd7 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
+#include "fs-util.h"
 #include "macro.h"
 #include "path-util.h"
 #include "strv.h"
@@ -40,7 +41,7 @@ TEST(read_one_char) {
         _cleanup_fclose_ FILE *file = NULL;
         char r;
         bool need_nl;
-        char name[] = "/tmp/test-read_one_char.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-read_one_char.XXXXXX";
 
         assert_se(fmkostemp_safe(name, "r+", &file) == 0);
 
@@ -60,8 +61,6 @@ TEST(read_one_char) {
         assert_se(fputs("\n", file) >= 0);
         rewind(file);
         assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
-
-        assert_se(unlink(name) >= 0);
 }
 
 TEST(getttyname_malloc) {