naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
8d419f
From ad50f15e51f4f2ffc4ebbfab10a3e1c5739c9ce6 Mon Sep 17 00:00:00 2001
8d419f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d419f
Date: Mon, 7 Mar 2022 15:06:57 +0100
8d419f
Subject: [PATCH] tests: add helper for creating tempfiles with content
8d419f
8d419f
I put it in tests because I think we're most likely to use it in tests.
8d419f
If necessary, it can be moved somewhere else later.
8d419f
8d419f
(cherry picked from commit 367c47c886af7d915e23de8d6aae0a1c135c0350)
8d419f
8d419f
Related: #2082131
8d419f
---
8d419f
 src/shared/tests.c          | 19 +++++++++
8d419f
 src/shared/tests.h          |  2 +
8d419f
 src/test/test-env-file.c    | 77 +++++++++----------------------------
8d419f
 src/test/test-socket-util.c | 19 +++------
8d419f
 4 files changed, 44 insertions(+), 73 deletions(-)
8d419f
8d419f
diff --git a/src/shared/tests.c b/src/shared/tests.c
8d419f
index f5d9536411..307f796fe2 100644
8d419f
--- a/src/shared/tests.c
8d419f
+++ b/src/shared/tests.c
8d419f
@@ -25,6 +25,7 @@
8d419f
 #include "cgroup-util.h"
8d419f
 #include "env-file.h"
8d419f
 #include "env-util.h"
8d419f
+#include "fd-util.h"
8d419f
 #include "fs-util.h"
8d419f
 #include "log.h"
8d419f
 #include "namespace-util.h"
8d419f
@@ -33,6 +34,7 @@
8d419f
 #include "random-util.h"
8d419f
 #include "strv.h"
8d419f
 #include "tests.h"
8d419f
+#include "tmpfile-util.h"
8d419f
 
8d419f
 char* setup_fake_runtime_dir(void) {
8d419f
         char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
8d419f
@@ -133,6 +135,23 @@ int log_tests_skipped_errno(int r, const char *message) {
8d419f
         return EXIT_TEST_SKIP;
8d419f
 }
8d419f
 
8d419f
+int write_tmpfile(char *pattern, const char *contents) {
8d419f
+        _cleanup_close_ int fd = -1;
8d419f
+
8d419f
+        assert(pattern);
8d419f
+        assert(contents);
8d419f
+
8d419f
+        fd = mkostemp_safe(pattern);
8d419f
+        if (fd < 0)
8d419f
+                return fd;
8d419f
+
8d419f
+        ssize_t l = strlen(contents);
8d419f
+        errno = 0;
8d419f
+        if (write(fd, contents, l) != l)
8d419f
+                return errno_or_else(EIO);
8d419f
+        return 0;
8d419f
+}
8d419f
+
8d419f
 bool have_namespaces(void) {
8d419f
         siginfo_t si = {};
8d419f
         pid_t pid;
8d419f
diff --git a/src/shared/tests.h b/src/shared/tests.h
8d419f
index ef6acd368e..ade527590b 100644
8d419f
--- a/src/shared/tests.h
8d419f
+++ b/src/shared/tests.h
8d419f
@@ -30,6 +30,8 @@ void test_setup_logging(int level);
8d419f
 int log_tests_skipped(const char *message);
8d419f
 int log_tests_skipped_errno(int r, const char *message);
8d419f
 
8d419f
+int write_tmpfile(char *pattern, const char *contents);
8d419f
+
8d419f
 bool have_namespaces(void);
8d419f
 
8d419f
 /* We use the small but non-trivial limit here */
8d419f
diff --git a/src/test/test-env-file.c b/src/test/test-env-file.c
8d419f
index f97206b4d6..886a8e4bc8 100644
8d419f
--- a/src/test/test-env-file.c
8d419f
+++ b/src/test/test-env-file.c
8d419f
@@ -55,18 +55,11 @@
8d419f
 
8d419f
 
8d419f
 TEST(load_env_file_1) {
8d419f
-        _cleanup_strv_free_ char **data = NULL;
8d419f
-        int r;
8d419f
-
8d419f
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
8d419f
-        _cleanup_close_ int fd;
8d419f
-
8d419f
-        fd = mkostemp_safe(name);
8d419f
-        assert_se(fd >= 0);
8d419f
-        assert_se(write(fd, env_file_1, strlen(env_file_1)) == strlen(env_file_1));
8d419f
+        assert_se(write_tmpfile(name, env_file_1) == 0);
8d419f
 
8d419f
-        r = load_env_file(NULL, name, &data);
8d419f
-        assert_se(r == 0);
8d419f
+        _cleanup_strv_free_ char **data = NULL;
8d419f
+        assert_se(load_env_file(NULL, name, &data) == 0);
8d419f
         assert_se(streq(data[0], "a=a"));
8d419f
         assert_se(streq(data[1], "b=bc"));
8d419f
         assert_se(streq(data[2], "d=de  f"));
8d419f
@@ -77,50 +70,30 @@ TEST(load_env_file_1) {
8d419f
 }
8d419f
 
8d419f
 TEST(load_env_file_2) {
8d419f
-        _cleanup_strv_free_ char **data = NULL;
8d419f
-        int r;
8d419f
-
8d419f
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
8d419f
-        _cleanup_close_ int fd;
8d419f
+        assert_se(write_tmpfile(name, env_file_2) == 0);
8d419f
 
8d419f
-        fd = mkostemp_safe(name);
8d419f
-        assert_se(fd >= 0);
8d419f
-        assert_se(write(fd, env_file_2, strlen(env_file_2)) == strlen(env_file_2));
8d419f
-
8d419f
-        r = load_env_file(NULL, name, &data);
8d419f
-        assert_se(r == 0);
8d419f
+        _cleanup_strv_free_ char **data = NULL;
8d419f
+        assert_se(load_env_file(NULL, name, &data) == 0);
8d419f
         assert_se(streq(data[0], "a=a"));
8d419f
         assert_se(data[1] == NULL);
8d419f
 }
8d419f
 
8d419f
 TEST(load_env_file_3) {
8d419f
-        _cleanup_strv_free_ char **data = NULL;
8d419f
-        int r;
8d419f
-
8d419f
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
8d419f
-        _cleanup_close_ int fd;
8d419f
-
8d419f
-        fd = mkostemp_safe(name);
8d419f
-        assert_se(fd >= 0);
8d419f
-        assert_se(write(fd, env_file_3, strlen(env_file_3)) == strlen(env_file_3));
8d419f
+        assert_se(write_tmpfile(name, env_file_3) == 0);
8d419f
 
8d419f
-        r = load_env_file(NULL, name, &data);
8d419f
-        assert_se(r == 0);
8d419f
+        _cleanup_strv_free_ char **data = NULL;
8d419f
+        assert_se(load_env_file(NULL, name, &data) == 0);
8d419f
         assert_se(data == NULL);
8d419f
 }
8d419f
 
8d419f
 TEST(load_env_file_4) {
8d419f
-        _cleanup_strv_free_ char **data = NULL;
8d419f
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
8d419f
-        _cleanup_close_ int fd;
8d419f
-        int r;
8d419f
-
8d419f
-        fd = mkostemp_safe(name);
8d419f
-        assert_se(fd >= 0);
8d419f
-        assert_se(write(fd, env_file_4, strlen(env_file_4)) == strlen(env_file_4));
8d419f
+        assert_se(write_tmpfile(name, env_file_4) == 0);
8d419f
 
8d419f
-        r = load_env_file(NULL, name, &data);
8d419f
-        assert_se(r == 0);
8d419f
+        _cleanup_strv_free_ char **data = NULL;
8d419f
+        assert_se(load_env_file(NULL, name, &data) == 0);
8d419f
         assert_se(streq(data[0], "HWMON_MODULES=coretemp f71882fg"));
8d419f
         assert_se(streq(data[1], "MODULE_0=coretemp"));
8d419f
         assert_se(streq(data[2], "MODULE_1=f71882fg"));
8d419f
@@ -128,36 +101,22 @@ TEST(load_env_file_4) {
8d419f
 }
8d419f
 
8d419f
 TEST(load_env_file_5) {
8d419f
-        _cleanup_strv_free_ char **data = NULL;
8d419f
-        int r;
8d419f
-
8d419f
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
8d419f
-        _cleanup_close_ int fd;
8d419f
-
8d419f
-        fd = mkostemp_safe(name);
8d419f
-        assert_se(fd >= 0);
8d419f
-        assert_se(write(fd, env_file_5, strlen(env_file_5)) == strlen(env_file_5));
8d419f
+        assert_se(write_tmpfile(name, env_file_5) == 0);
8d419f
 
8d419f
-        r = load_env_file(NULL, name, &data);
8d419f
-        assert_se(r == 0);
8d419f
+        _cleanup_strv_free_ char **data = NULL;
8d419f
+        assert_se(load_env_file(NULL, name, &data) == 0);
8d419f
         assert_se(streq(data[0], "a="));
8d419f
         assert_se(streq(data[1], "b="));
8d419f
         assert_se(data[2] == NULL);
8d419f
 }
8d419f
 
8d419f
 TEST(load_env_file_6) {
8d419f
-        _cleanup_strv_free_ char **data = NULL;
8d419f
-        int r;
8d419f
-
8d419f
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
8d419f
-        _cleanup_close_ int fd;
8d419f
+        assert_se(write_tmpfile(name, env_file_6) == 0);
8d419f
 
8d419f
-        fd = mkostemp_safe(name);
8d419f
-        assert_se(fd >= 0);
8d419f
-        assert_se(write(fd, env_file_6, strlen(env_file_6)) == strlen(env_file_6));
8d419f
-
8d419f
-        r = load_env_file(NULL, name, &data);
8d419f
-        assert_se(r == 0);
8d419f
+        _cleanup_strv_free_ char **data = NULL;
8d419f
+        assert_se(load_env_file(NULL, name, &data) == 0);
8d419f
         assert_se(streq(data[0], "a= n t x y '"));
8d419f
         assert_se(streq(data[1], "b=$'"));
8d419f
         assert_se(streq(data[2], "c= \\n\\t\\$\\`\\\\\n"));
8d419f
diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c
8d419f
index 9ee651a5fa..3245516f9a 100644
8d419f
--- a/src/test/test-socket-util.c
8d419f
+++ b/src/test/test-socket-util.c
8d419f
@@ -228,17 +228,12 @@ TEST(passfd_read) {
8d419f
 
8d419f
         if (r == 0) {
8d419f
                 /* Child */
8d419f
-                char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
8d419f
-                _cleanup_close_ int tmpfd = -1;
8d419f
-
8d419f
                 pair[0] = safe_close(pair[0]);
8d419f
 
8d419f
-                tmpfd = mkostemp_safe(tmpfile);
8d419f
-                assert_se(tmpfd >= 0);
8d419f
-                assert_se(write(tmpfd, file_contents, strlen(file_contents)) == (ssize_t) strlen(file_contents));
8d419f
-                tmpfd = safe_close(tmpfd);
8d419f
+                char tmpfile[] = "/tmp/test-socket-util-passfd-read-XXXXXX";
8d419f
+                assert_se(write_tmpfile(tmpfile, file_contents) == 0);
8d419f
 
8d419f
-                tmpfd = open(tmpfile, O_RDONLY);
8d419f
+                _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
8d419f
                 assert_se(tmpfd >= 0);
8d419f
                 assert_se(unlink(tmpfile) == 0);
8d419f
 
8d419f
@@ -277,16 +272,12 @@ TEST(passfd_contents_read) {
8d419f
                 /* Child */
8d419f
                 struct iovec iov = IOVEC_INIT_STRING(wire_contents);
8d419f
                 char tmpfile[] = "/tmp/test-socket-util-passfd-contents-read-XXXXXX";
8d419f
-                _cleanup_close_ int tmpfd = -1;
8d419f
 
8d419f
                 pair[0] = safe_close(pair[0]);
8d419f
 
8d419f
-                tmpfd = mkostemp_safe(tmpfile);
8d419f
-                assert_se(tmpfd >= 0);
8d419f
-                assert_se(write(tmpfd, file_contents, strlen(file_contents)) == (ssize_t) strlen(file_contents));
8d419f
-                tmpfd = safe_close(tmpfd);
8d419f
+                assert_se(write_tmpfile(tmpfile, file_contents) == 0);
8d419f
 
8d419f
-                tmpfd = open(tmpfile, O_RDONLY);
8d419f
+                _cleanup_close_ int tmpfd = open(tmpfile, O_RDONLY);
8d419f
                 assert_se(tmpfd >= 0);
8d419f
                 assert_se(unlink(tmpfile) == 0);
8d419f