chiark / gitweb /
Prep v239: Fix conf-parser.[hc], it got a bit mangled by migration.
[elogind.git] / src / test / test-conf-parser.c
index 30ca7a8e741fe6aba510f8a3babd5a68a44e23e0..1593d3adf0f38f2125e77267d7ac27b42c2e010d 100644 (file)
@@ -1,36 +1,16 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2015 Ronny Chevalier
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
 
 #include "conf-parser.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "fs-util.h"
 #include "log.h"
 #include "macro.h"
 #include "string-util.h"
 #include "strv.h"
 #include "util.h"
 
-/// Additional includes needed by elogind
-#include "fd-util.h"
-#include "fileio.h"
-
+#if 0 /// UNNEEDED by elogind
 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
         _cleanup_free_ char *path = NULL;
 
@@ -45,14 +25,12 @@ static void test_config_parse_log_level_one(const char *rvalue, int expected) {
         assert_se(expected == log_level);
 }
 
-#if 0 /// UNNEEDED by elogind
 static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
         int log_facility = 0;
 
         assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0);
         assert_se(expected == log_facility);
 }
-#endif // 0
 
 static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
         size_t iec_size = 0;
@@ -61,7 +39,6 @@ static void test_config_parse_iec_size_one(const char *rvalue, size_t expected)
         assert_se(expected == iec_size);
 }
 
-#if 0 /// UNNEEDED by elogind
 static void test_config_parse_si_size_one(const char *rvalue, size_t expected) {
         size_t si_size = 0;
 
@@ -91,12 +68,14 @@ static void test_config_parse_strv_one(const char *rvalue, char **expected) {
         assert_se(strv_equal(expected, strv));
 }
 
+#if 0 /// UNNEEDED by elogind
 static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
         mode_t v = 0;
 
         assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
         assert_se(expected == v);
 }
+#endif // 0
 
 static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
         usec_t v = 0;
@@ -112,12 +91,12 @@ static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
         assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
         assert_se(expected == v);
 }
-#endif // 0
 
 static void test_config_parse_path(void) {
         test_config_parse_path_one("/path", "/path");
         test_config_parse_path_one("/path//////////", "/path");
         test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
+        test_config_parse_path_one("/path//./////hogehoge///.", "/path/hogehoge");
         test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
 
         test_config_parse_path_one("not_absolute/path", NULL);
@@ -131,14 +110,12 @@ static void test_config_parse_log_level(void) {
         test_config_parse_log_level_one("garbage", 0);
 }
 
-#if 0 /// UNNEEDED by elogind
 static void test_config_parse_log_facility(void) {
         test_config_parse_log_facility_one("mail", LOG_MAIL);
         test_config_parse_log_facility_one("user", LOG_USER);
 
         test_config_parse_log_facility_one("garbage", 0);
 }
-#endif // 0
 
 static void test_config_parse_iec_size(void) {
         test_config_parse_iec_size_one("1024", 1024);
@@ -153,7 +130,6 @@ static void test_config_parse_iec_size(void) {
         test_config_parse_iec_size_one("garbage", 0);
 }
 
-#if 0 /// UNNEEDED by elogind
 static void test_config_parse_si_size(void) {
         test_config_parse_si_size_one("1024", 1024);
         test_config_parse_si_size_one("2K", 2000);
@@ -196,9 +172,10 @@ static void test_config_parse_strv(void) {
         test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo"));
         test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo"));
         test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80"));
-        test_config_parse_strv_one("\xc3\x7f", STRV_MAKE_EMPTY);
+        test_config_parse_strv_one("\xc3\x7f", STRV_MAKE("\xc3\x7f"));
 }
 
+#if 0 /// UNNEEDED by elogind
 static void test_config_parse_mode(void) {
         test_config_parse_mode_one("777", 0777);
         test_config_parse_mode_one("644", 0644);
@@ -209,6 +186,7 @@ static void test_config_parse_mode(void) {
         test_config_parse_mode_one("777garbage", 0);
         test_config_parse_mode_one("777 garbage", 0);
 }
+#endif // 0
 
 static void test_config_parse_sec(void) {
         test_config_parse_sec_one("1", 1 * USEC_PER_SEC);
@@ -240,7 +218,6 @@ static void test_config_parse_iec_uint64(void) {
 
         assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
 }
-#endif // 0
 
 static void test_config_parse_join_controllers(void) {
         int r;
@@ -280,6 +257,7 @@ static void test_config_parse_join_controllers(void) {
         assert_se(r < 0);
         assert_se(c == c2);
 }
+#endif // 0
 
 #define x10(x) x x x x x x x x x x
 #define x100(x) x10(x10(x))
@@ -306,6 +284,11 @@ static const char* const config_file[] = {
         "2\\\n"
         "3\n",
 
+        "[Section]\n"
+        "setting1=1\\\n"     /* continuation with extra trailing backslash at the end */
+        "2\\\n"
+        "3\\\n",
+
         "[Section]\n"
         "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
         "\\\\2\n",           /* note that C requires one level of escaping, so the
@@ -322,6 +305,11 @@ static const char* const config_file[] = {
         x1000("ABCD") "\\\n"
         "foobar",
 
+        "[Section]\n"
+        "setting1="          /* a line above LINE_MAX length, with continuation */
+        x1000("ABCD") "\\\n" /* and an extra trailing backslash */
+        "foobar\\\n",
+
         "[Section]\n"
         "setting1="          /* a line above the allowed limit: 9 + 1050000 + 1 */
         x1000(x1000("x") x10("abcde")) "\n",
@@ -332,7 +320,7 @@ static const char* const config_file[] = {
 };
 
 static void test_config_parse(unsigned i, const char *s) {
-        char name[] = "/tmp/test-conf-parser.XXXXXX";
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
         int fd, r;
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *setting1 = NULL;
@@ -375,27 +363,27 @@ static void test_config_parse(unsigned i, const char *s) {
                 assert_se(streq(setting1, "1"));
                 break;
 
-        case 4:
+        case 4 ... 5:
                 assert_se(r == 0);
                 assert_se(streq(setting1, "1 2 3"));
                 break;
 
-        case 5:
+        case 6:
                 assert_se(r == 0);
                 assert_se(streq(setting1, "1\\\\ \\\\2"));
                 break;
 
-        case 6:
+        case 7:
                 assert_se(r == 0);
                 assert_se(streq(setting1, x1000("ABCD")));
                 break;
 
-        case 7:
+        case 8 ... 9:
                 assert_se(r == 0);
                 assert_se(streq(setting1, x1000("ABCD") " foobar"));
                 break;
 
-        case 8 ... 9:
+        case 10 ... 11:
                 assert_se(r == -ENOBUFS);
                 assert_se(setting1 == NULL);
                 break;
@@ -408,25 +396,25 @@ int main(int argc, char **argv) {
         log_parse_environment();
         log_open();
 
+#if 0 /// UNNEEDED by elogind
         test_config_parse_path();
         test_config_parse_log_level();
-#if 0 /// UNNEEDED by elogind
         test_config_parse_log_facility();
-#endif // 0
         test_config_parse_iec_size();
-#if 0 /// UNNEEDED by elogind
         test_config_parse_si_size();
 #endif // 0
         test_config_parse_int();
         test_config_parse_unsigned();
         test_config_parse_strv();
+#if 0 /// UNNEEDED by elogind
         test_config_parse_mode();
+#endif // 0
         test_config_parse_sec();
 #if 0 /// UNNEEDED by elogind
         test_config_parse_nsec();
         test_config_parse_iec_uint64();
-#endif // 0
         test_config_parse_join_controllers();
+#endif // 0
 
         for (i = 0; i < ELEMENTSOF(config_file); i++)
                 test_config_parse(i, config_file[i]);