naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
594167
From 402854a52a0a659fd914279eae17b4e065a9c294 Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Wed, 19 Jan 2022 10:15:50 +0100
594167
Subject: [PATCH] test-env-file: add tests for quoting in env files
594167
594167
(cherry picked from commit e9e982a29d52834375e5fb553cecb45bc905cf91)
594167
594167
Related: #2017035
594167
---
594167
 src/test/test-env-file.c | 68 +++++++++++++++++++++++++++++-----------
594167
 1 file changed, 50 insertions(+), 18 deletions(-)
594167
594167
diff --git a/src/test/test-env-file.c b/src/test/test-env-file.c
594167
index 6cc2455c1f..7b132447bf 100644
594167
--- a/src/test/test-env-file.c
594167
+++ b/src/test/test-env-file.c
594167
@@ -13,11 +13,11 @@
594167
         "a=a\n"                                 \
594167
         "b=b\\\n"                               \
594167
         "c\n"                                   \
594167
-        "d=d\\\n"                               \
594167
-        "e\\\n"                                 \
594167
-        "f\n"                                   \
594167
+        "d= d\\\n"                              \
594167
+        "e  \\\n"                               \
594167
+        "f  \n"                                 \
594167
         "g=g\\ \n"                              \
594167
-        "h=h\n"                                 \
594167
+        "h= ąęół\\ śćńźżµ \n"                   \
594167
         "i=i\\"
594167
 
594167
 #define env_file_2                              \
594167
@@ -26,22 +26,34 @@
594167
 #define env_file_3 \
594167
         "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
594167
         "#--nouser-config                                     \\\n" \
594167
-        "normal=line"
594167
-
594167
-#define env_file_4 \
594167
-       "# Generated\n" \
594167
-       "\n" \
594167
-       "HWMON_MODULES=\"coretemp f71882fg\"\n" \
594167
-       "\n" \
594167
-       "# For compatibility reasons\n" \
594167
-       "\n" \
594167
-       "MODULE_0=coretemp\n" \
594167
-       "MODULE_1=f71882fg"
594167
+        "normal=line                                          \\\n" \
594167
+        ";normal=ignored                                      \\\n" \
594167
+        "normal_ignored                                       \\\n" \
594167
+        "normal ignored                                       \\\n"
594167
+
594167
+#define env_file_4                              \
594167
+        "# Generated\n"                         \
594167
+        "\n"                                    \
594167
+        "HWMON_MODULES=\"coretemp f71882fg\"\n" \
594167
+        "\n"                                    \
594167
+        "# For compatibility reasons\n"         \
594167
+        "\n"                                    \
594167
+        "MODULE_0=coretemp\n"                   \
594167
+        "MODULE_1=f71882fg"
594167
 
594167
 #define env_file_5                              \
594167
-        "a=\n"                                 \
594167
+        "a=\n"                                  \
594167
         "b="
594167
 
594167
+#define env_file_6                              \
594167
+        "a=\\ \\n \\t \\x \\y \\' \n"           \
594167
+        "b= \\$'                  \n"           \
594167
+        "c= ' \\n\\t\\$\\`\\\\\n"               \
594167
+        "'   \n"                                \
594167
+        "d= \" \\n\\t\\$\\`\\\\\n"              \
594167
+        "\"   \n"
594167
+
594167
+
594167
 TEST(load_env_file_1) {
594167
         _cleanup_strv_free_ char **data = NULL;
594167
         int r;
594167
@@ -57,9 +69,9 @@ TEST(load_env_file_1) {
594167
         assert_se(r == 0);
594167
         assert_se(streq(data[0], "a=a"));
594167
         assert_se(streq(data[1], "b=bc"));
594167
-        assert_se(streq(data[2], "d=def"));
594167
+        assert_se(streq(data[2], "d=de  f"));
594167
         assert_se(streq(data[3], "g=g "));
594167
-        assert_se(streq(data[4], "h=h"));
594167
+        assert_se(streq(data[4], "h=ąęół śćńźżµ"));
594167
         assert_se(streq(data[5], "i=i"));
594167
         assert_se(data[6] == NULL);
594167
 }
594167
@@ -133,6 +145,26 @@ TEST(load_env_file_5) {
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
+
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
+        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
+        assert_se(streq(data[3], "d= \\n\\t$`\\\n"));
594167
+        assert_se(data[4] == NULL);
594167
+}
594167
+
594167
 TEST(write_and_load_env_file) {
594167
         const char *v;
594167