dcavalca / rpms / systemd

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