]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-fileio.c
util-lib: split out env file parsing code into env-file.c
[thirdparty/systemd.git] / src / test / test-fileio.c
index 3476c412910bca6bdcf39b6c51275a3c3f4c5bca..44047f2e5a2f0d7c9ccaf15b9b14aea889f9a593 100644 (file)
@@ -1,9 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2013 Lennart Poettering
-***/
 
 #include <fcntl.h>
 #include <stdio.h>
@@ -11,7 +6,7 @@
 
 #include "alloc-util.h"
 #include "ctype.h"
-#include "def.h"
+#include "env-file.h"
 #include "env-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "process-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tests.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 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"
@@ -68,7 +56,7 @@ static void test_parse_env_file(void) {
         fflush(f);
         fclose(f);
 
-        r = load_env_file(NULL, t, NULL, &a);
+        r = load_env_file(NULL, t, &a);
         assert_se(r >= 0);
 
         STRV_FOREACH(i, a)
@@ -95,7 +83,7 @@ static void test_parse_env_file(void) {
         }
 
         r = parse_env_file(
-                        NULL, t, NULL,
+                        NULL, t,
                        "one", &one,
                        "two", &two,
                        "three", &three,
@@ -105,8 +93,7 @@ static void test_parse_env_file(void) {
                        "seven", &seven,
                        "eight", &eight,
                        "export nine", &nine,
-                       "ten", &ten,
-                       NULL);
+                       "ten", &ten);
 
         assert_se(r >= 0);
 
@@ -132,10 +119,16 @@ 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);
 
-        r = load_env_file(NULL, p, NULL, &b);
+        r = load_env_file(NULL, p, &b);
         assert_se(r >= 0);
 }
 
@@ -143,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"
@@ -173,7 +157,7 @@ static void test_parse_multiline_env_file(void) {
         fflush(f);
         fclose(f);
 
-        r = load_env_file(NULL, t, NULL, &a);
+        r = load_env_file(NULL, t, &a);
         assert_se(r >= 0);
 
         STRV_FOREACH(i, a)
@@ -184,28 +168,28 @@ 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);
 
-        r = load_env_file(NULL, p, NULL, &b);
+        r = load_env_file(NULL, p, &b);
         assert_se(r >= 0);
 }
 
 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"
@@ -262,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"
@@ -301,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);
 
@@ -493,7 +467,7 @@ static void test_load_env_file_pairs(void) {
         f = fdopen(fd, "r");
         assert_se(f);
 
-        r = load_env_file_pairs(f, fn, NULL, &l);
+        r = load_env_file_pairs(f, fn, &l);
         assert_se(r >= 0);
 
         assert_se(strv_length(l) == 14);
@@ -715,9 +689,7 @@ static void test_read_line3(void) {
 }
 
 int main(int argc, char *argv[]) {
-        log_set_max_level(LOG_DEBUG);
-        log_parse_environment();
-        log_open();
+        test_setup_logging(LOG_DEBUG);
 
         test_parse_env_file();
         test_parse_multiline_env_file();