]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Introduce fmkostemp_safe and use it in tests
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 23 Oct 2018 08:50:09 +0000 (10:50 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 26 Oct 2018 10:56:51 +0000 (12:56 +0200)
No functional change.

src/basic/fileio.c
src/basic/fileio.h
src/shared/serialize.c
src/test/test-clock.c
src/test/test-conf-parser.c
src/test/test-env-util.c
src/test/test-fileio.c
src/test/test-terminal-util.c

index d2fd4c47b3f2acb8d2b0e01ecb2ecbc25902840d..f14afa5dce440d99cc94e23037d2db5635883547 100644 (file)
@@ -1221,6 +1221,24 @@ int mkostemp_safe(char *pattern) {
         return fd;
 }
 
+int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) {
+        int fd;
+        FILE *f;
+
+        fd = mkostemp_safe(pattern);
+        if (fd < 0)
+                return fd;
+
+        f = fdopen(fd, mode);
+        if (!f) {
+                safe_close(fd);
+                return -errno;
+        }
+
+        *ret_f = f;
+        return 0;
+}
+
 int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
         const char *fn;
         char *t;
index e84710f138199e484e94d1c43f4d733457db8465..102d33d75f24ca42739a128f8c13c547930bd8c6 100644 (file)
@@ -64,6 +64,7 @@ int fflush_sync_and_check(FILE *f);
 
 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
 int mkostemp_safe(char *pattern);
+int fmkostemp_safe(char *pattern, const char *mode, FILE**_f);
 
 int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
 int tempfn_random(const char *p, const char *extra, char **ret);
index c76a0d0d9efe06b9780563f864e579e6f18199ed..3a6f8f60ebd0c868dd8273377f40b404833566c7 100644 (file)
@@ -113,7 +113,7 @@ int serialize_strv(FILE *f, const char *key, char **l) {
         int ret = 0, r;
         char **i;
 
-        /* Returns the first error */
+        /* Returns the first error, or positive if anything was serialized, 0 otherwise. */
 
         STRV_FOREACH(i, l) {
                 r = serialize_item_escaped(f, key, *i);
index 50e9b7756f6b227fbc4c9f6f5c8a472a26fefe60..95ac8de91bcca178c6e4e25f7e082f37385e1cf2 100644 (file)
@@ -9,12 +9,12 @@
 #include "clock-util.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "fs-util.h"
 #include "log.h"
 #include "macro.h"
 
 static void test_clock_is_localtime(void) {
         char adjtime[] = "/tmp/test-adjtime.XXXXXX";
-        int fd = -1;
         _cleanup_fclose_ FILE* f = NULL;
 
         static const struct scenario {
@@ -41,17 +41,14 @@ static void test_clock_is_localtime(void) {
         /* without an adjtime file we default to UTC */
         assert_se(clock_is_localtime("/nonexisting/adjtime") == 0);
 
-        fd = mkostemp_safe(adjtime);
-        assert_se(fd >= 0);
+        assert_se(fmkostemp_safe(adjtime, "w", &f) == 0);
         log_info("adjtime test file: %s", adjtime);
-        f = fdopen(fd, "w");
-        assert_se(f);
 
         for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) {
                 log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result);
                 log_info("%s", scenarios[i].contents);
                 rewind(f);
-                ftruncate(fd, 0);
+                ftruncate(fileno(f), 0);
                 assert_se(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
                 assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result);
         }
index ff951d12f4cf55b414d58d32111eef29ec79025f..91f1d9a386712b3ba877e7e8182e65d362bd2d70 100644 (file)
@@ -311,9 +311,9 @@ static const char* const config_file[] = {
 
 static void test_config_parse(unsigned i, const char *s) {
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
-        int fd, r;
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *setting1 = NULL;
+        int r;
 
         const ConfigTableItem items[] = {
                 { "Section", "setting1",  config_parse_string,   0, &setting1},
@@ -322,12 +322,9 @@ static void test_config_parse(unsigned i, const char *s) {
 
         log_info("== %s[%i] ==", __func__, i);
 
-        fd = mkostemp_safe(name);
-        assert_se(fd >= 0);
-        assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
-
-        assert_se(lseek(fd, 0, SEEK_SET) == 0);
-        assert_se(f = fdopen(fd, "r"));
+        assert_se(fmkostemp_safe(name, "r+", &f) == 0);
+        assert_se(fwrite(s, strlen(s), 1, f) == 1);
+        rewind(f);
 
         /*
         int config_parse(const char *unit,
index c988fc6eeea86305c399c6d4625e959e569cd2cf..0ab671b476d7876ceda0768119663682c87414da 100644 (file)
@@ -323,8 +323,7 @@ static void test_serialize_environment(void) {
         _cleanup_strv_free_ char **env = NULL, **env2 = NULL;
         char fn[] = "/tmp/test-env-util.XXXXXXX";
         _cleanup_fclose_ FILE *f = NULL;
-        int fd, r;
-
+        int r;
 
         assert_se(env = strv_new("A=1",
                                  "B=2",
@@ -333,12 +332,8 @@ static void test_serialize_environment(void) {
                                  "FOO%%=a\177b\nc\td e",
                                  NULL));
 
-        fd = mkostemp_safe(fn);
-        assert_se(fd >= 0);
-
-        assert_se(f = fdopen(fd, "r+"));
-
-        assert_se(serialize_strv(f, "env", env) > 0);
+        assert_se(fmkostemp_safe(fn, "r+", &f) == 0);
+        assert_se(serialize_strv(f, "env", env) == 1);
         assert_se(fflush_and_check(f) == 0);
 
         rewind(f);
index aa38a7d29a4e623588463f910c36adf1c2e64198..cabaef6ab85ab4de2e12fd6a118a57f32f5787fe 100644 (file)
@@ -23,24 +23,15 @@ static void test_parse_env_file(void) {
         _cleanup_(unlink_tempfilep) char
                 t[] = "/tmp/test-fileio-in-XXXXXX",
                 p[] = "/tmp/test-fileio-out-XXXXXX";
-        int fd, r;
         FILE *f;
         _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
                         *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
         _cleanup_strv_free_ char **a = NULL, **b = NULL;
         char **i;
         unsigned k;
+        int r;
 
-        fd = mkostemp_safe(p);
-        assert_se(fd >= 0);
-        close(fd);
-
-        fd = mkostemp_safe(t);
-        assert_se(fd >= 0);
-
-        f = fdopen(fd, "w");
-        assert_se(f);
-
+        assert_se(fmkostemp_safe(t, "w", &f) == 0);
         fputs("one=BAR   \n"
               "# comment\n"
               " # comment \n"
@@ -128,6 +119,12 @@ static void test_parse_env_file(void) {
         assert_se(streq(nine, "nineval"));
         assert_se(ten == NULL);
 
+        {
+                /* prepare a temporary file to write the environment to */
+                _cleanup_close_ int fd = mkostemp_safe(p);
+                assert_se(fd >= 0);
+        }
+
         r = write_env_file(p, a);
         assert_se(r >= 0);
 
@@ -139,21 +136,12 @@ static void test_parse_multiline_env_file(void) {
         _cleanup_(unlink_tempfilep) char
                 t[] = "/tmp/test-fileio-in-XXXXXX",
                 p[] = "/tmp/test-fileio-out-XXXXXX";
-        int fd, r;
         FILE *f;
         _cleanup_strv_free_ char **a = NULL, **b = NULL;
         char **i;
+        int r;
 
-        fd = mkostemp_safe(p);
-        assert_se(fd >= 0);
-        close(fd);
-
-        fd = mkostemp_safe(t);
-        assert_se(fd >= 0);
-
-        f = fdopen(fd, "w");
-        assert_se(f);
-
+        assert_se(fmkostemp_safe(t, "w", &f) == 0);
         fputs("one=BAR\\\n"
               "    VAR\\\n"
               "\tGAR\n"
@@ -180,6 +168,11 @@ static void test_parse_multiline_env_file(void) {
         assert_se(streq_ptr(a[2], "tri=bar     var \tgar "));
         assert_se(a[3] == NULL);
 
+        {
+                _cleanup_close_ int fd = mkostemp_safe(p);
+                assert_se(fd >= 0);
+        }
+
         r = write_env_file(p, a);
         assert_se(r >= 0);
 
@@ -189,19 +182,14 @@ static void test_parse_multiline_env_file(void) {
 
 static void test_merge_env_file(void) {
         _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
-        int fd, r;
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_strv_free_ char **a = NULL;
         char **i;
+        int r;
 
-        fd = mkostemp_safe(t);
-        assert_se(fd >= 0);
-
+        assert_se(fmkostemp_safe(t, "w", &f) == 0);
         log_info("/* %s (%s) */", __func__, t);
 
-        f = fdopen(fd, "w");
-        assert_se(f);
-
         r = write_string_stream(f,
                                 "one=1   \n"
                                 "twelve=${one}2\n"
@@ -258,19 +246,14 @@ static void test_merge_env_file(void) {
 
 static void test_merge_env_file_invalid(void) {
         _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
-        int fd, r;
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_strv_free_ char **a = NULL;
         char **i;
+        int r;
 
-        fd = mkostemp_safe(t);
-        assert_se(fd >= 0);
-
+        assert_se(fmkostemp_safe(t, "w", &f) == 0);
         log_info("/* %s (%s) */", __func__, t);
 
-        f = fdopen(fd, "w");
-        assert_se(f);
-
         r = write_string_stream(f,
                                 "unset one   \n"
                                 "unset one=   \n"
@@ -297,16 +280,11 @@ static void test_merge_env_file_invalid(void) {
 
 static void test_executable_is_script(void) {
         _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX";
-        int fd, r;
         _cleanup_fclose_ FILE *f = NULL;
         char *command;
+        int r;
 
-        fd = mkostemp_safe(t);
-        assert_se(fd >= 0);
-
-        f = fdopen(fd, "w");
-        assert_se(f);
-
+        assert_se(fmkostemp_safe(t, "w", &f) == 0);
         fputs("#! /bin/script -a -b \ngoo goo", f);
         fflush(f);
 
index 9f27c7413911450445c9a99d93094e5fefe1d8fe..fbe8133b10a65ed9ec8479614c310e301c3f4cb7 100644 (file)
@@ -32,15 +32,11 @@ static void test_read_one_char(void) {
         char r;
         bool need_nl;
         char name[] = "/tmp/test-read_one_char.XXXXXX";
-        int fd;
 
-        fd = mkostemp_safe(name);
-        assert_se(fd >= 0);
-        file = fdopen(fd, "r+");
-        assert_se(file);
+        assert(fmkostemp_safe(name, "r+", &file) == 0);
+
         assert_se(fputs("c\n", file) >= 0);
         rewind(file);
-
         assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0);
         assert_se(!need_nl);
         assert_se(r == 'c');