ecbff1
From 75fa3b2c5f62dd6d55860aaea979289d53b62a24 Mon Sep 17 00:00:00 2001
ecbff1
From: Jan Synacek <jsynacek@redhat.com>
ecbff1
Date: Thu, 23 Nov 2017 09:58:11 +0100
ecbff1
Subject: [PATCH] test-conf-parser: add tests for config parser
ecbff1
ecbff1
Add some basic tests for config_parse(). Add tests for the new long lines,
ecbff1
including overflow.
ecbff1
ecbff1
(cherry picked from commit e3f46367f577f8bd4b3a62ea0149bdcb112da573)
ecbff1
(cherry picked from commit 8f313f4febb4df13279aaae86c846bbb142a5a39)
ecbff1
ecbff1
Resolves: #1503106
ecbff1
---
ecbff1
 Makefile.am                 |   7 ++
de8967
 src/test/test-conf-parser.c | 155 ++++++++++++++++++++++++++++++++++++
ecbff1
 2 files changed, 162 insertions(+)
ecbff1
 create mode 100644 src/test/test-conf-parser.c
ecbff1
ecbff1
diff --git a/Makefile.am b/Makefile.am
ecbff1
index c4a96e1fd..8c73326fa 100644
ecbff1
--- a/Makefile.am
ecbff1
+++ b/Makefile.am
ecbff1
@@ -1416,6 +1416,7 @@ tests += \
ecbff1
 	test-socket-util \
ecbff1
 	test-fdset \
ecbff1
 	test-conf-files \
ecbff1
+	test-conf-parser \
ecbff1
 	test-capability \
ecbff1
 	test-async \
ecbff1
 	test-ratelimit \
ecbff1
@@ -2041,6 +2042,12 @@ test_conf_files_SOURCES = \
ecbff1
 test_conf_files_LDADD = \
ecbff1
 	libsystemd-shared.la
ecbff1
 
ecbff1
+test_conf_parser_SOURCES = \
ecbff1
+        src/test/test-conf-parser.c
ecbff1
+
ecbff1
+test_conf_parser_LDADD = \
ecbff1
+	libsystemd-shared.la
ecbff1
+
ecbff1
 # ------------------------------------------------------------------------------
ecbff1
 ## .PHONY so it always rebuilds it
ecbff1
 .PHONY: coverage lcov-run lcov-report coverage-sync
ecbff1
diff --git a/src/test/test-conf-parser.c b/src/test/test-conf-parser.c
ecbff1
new file mode 100644
ecbff1
index 000000000..4052d0095
ecbff1
--- /dev/null
ecbff1
+++ b/src/test/test-conf-parser.c
ecbff1
@@ -0,0 +1,155 @@
ecbff1
+/***
ecbff1
+  This file is part of systemd.
ecbff1
+
ecbff1
+  Copyright 2015 Ronny Chevalier
ecbff1
+
ecbff1
+  systemd is free software; you can redistribute it and/or modify it
ecbff1
+  under the terms of the GNU Lesser General Public License as published by
ecbff1
+  the Free Software Foundation; either version 2.1 of the License, or
ecbff1
+  (at your option) any later version.
ecbff1
+
ecbff1
+  systemd is distributed in the hope that it will be useful, but
ecbff1
+  WITHOUT ANY WARRANTY; without even the implied warranty of
ecbff1
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ecbff1
+  Lesser General Public License for more details.
ecbff1
+
ecbff1
+  You should have received a copy of the GNU Lesser General Public License
ecbff1
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
ecbff1
+***/
ecbff1
+
ecbff1
+#include "conf-parser.h"
ecbff1
+#include "fileio.h"
ecbff1
+#include "log.h"
ecbff1
+#include "macro.h"
ecbff1
+#include "strv.h"
ecbff1
+#include "util.h"
ecbff1
+
ecbff1
+#define x10(x) x x x x x x x x x x
ecbff1
+#define x100(x) x10(x10(x))
ecbff1
+#define x1000(x) x10(x100(x))
ecbff1
+
ecbff1
+static const char* const config_file[] = {
ecbff1
+        "[Section]\n"
ecbff1
+        "setting1=1\n",
ecbff1
+
ecbff1
+        "[Section]\n"
ecbff1
+        "setting1=1",        /* no terminating newline */
ecbff1
+
ecbff1
+        "\n\n\n\n[Section]\n\n\n"
ecbff1
+        "setting1=1",        /* some whitespace, no terminating newline */
ecbff1
+
ecbff1
+        "[Section]\n"
ecbff1
+        "[Section]\n"
ecbff1
+        "setting1=1\n"
ecbff1
+        "setting1=2\n"
ecbff1
+        "setting1=1\n",      /* repeated settings */
ecbff1
+
ecbff1
+        "[Section]\n"
ecbff1
+        "setting1=1\\\n"     /* normal continuation */
ecbff1
+        "2\\\n"
ecbff1
+        "3\n",
ecbff1
+
ecbff1
+        "[Section]\n"
ecbff1
+        "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
ecbff1
+        "\\\\2\n",           /* note that C requires one level of escaping, so the
ecbff1
+                              * parser gets "…1 BS BS BS NL BS BS 2 NL", which
ecbff1
+                              * it translates into "…1 BS BS SP BS BS 2" */
ecbff1
+
ecbff1
+        "\n[Section]\n\n"
ecbff1
+        "setting1="          /* a line above LINE_MAX length */
ecbff1
+        x1000("ABCD")
ecbff1
+        "\n",
ecbff1
+
ecbff1
+        "[Section]\n"
ecbff1
+        "setting1="          /* a line above LINE_MAX length, with continuation */
ecbff1
+        x1000("ABCD") "\\\n"
ecbff1
+        "foobar",
ecbff1
+
ecbff1
+        "[Section]\n"
ecbff1
+        "setting1="          /* a line above the allowed limit: 9 + 1050000 + 1 */
ecbff1
+        x1000(x1000("x") x10("abcde")) "\n",
ecbff1
+
ecbff1
+        "[Section]\n"
ecbff1
+        "setting1="          /* many continuation lines, together above the limit */
ecbff1
+        x1000(x1000("x") x10("abcde") "\\\n") "xxx",
ecbff1
+};
ecbff1
+
ecbff1
+static void test_config_parse(unsigned i, const char *s) {
ecbff1
+        char name[] = "/tmp/test-conf-parser.XXXXXX";
ecbff1
+        int fd, r;
ecbff1
+        _cleanup_fclose_ FILE *f = NULL;
ecbff1
+        _cleanup_free_ char *setting1 = NULL;
ecbff1
+
ecbff1
+        const ConfigTableItem items[] = {
ecbff1
+                { "Section", "setting1",  config_parse_string,   0, &setting1},
ecbff1
+                {}
ecbff1
+        };
ecbff1
+
ecbff1
+        log_info("== %s[%i] ==", __func__, i);
ecbff1
+
ecbff1
+        fd = mkostemp_safe(name, O_CLOEXEC);
ecbff1
+        assert_se(fd >= 0);
ecbff1
+        assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
ecbff1
+
ecbff1
+        assert_se(lseek(fd, 0, SEEK_SET) == 0);
ecbff1
+        assert_se(f = fdopen(fd, "r"));
ecbff1
+
ecbff1
+        /*
ecbff1
+        int config_parse(const char *unit,
ecbff1
+                         const char *filename,
ecbff1
+                         FILE *f,
ecbff1
+                         const char *sections,
ecbff1
+                         ConfigItemLookup lookup,
ecbff1
+                         const void *table,
ecbff1
+                         bool relaxed,
ecbff1
+                         bool allow_include,
ecbff1
+                         bool warn,
ecbff1
+                         void *userdata)
ecbff1
+        */
ecbff1
+
ecbff1
+        r = config_parse(NULL, name, f,
ecbff1
+                         "Section\0",
ecbff1
+                         config_item_table_lookup, items,
ecbff1
+                         false, false, true, NULL);
ecbff1
+
ecbff1
+        switch (i) {
ecbff1
+        case 0 ... 3:
ecbff1
+                assert_se(r == 0);
ecbff1
+                assert_se(streq(setting1, "1"));
ecbff1
+                break;
ecbff1
+
ecbff1
+        case 4:
ecbff1
+                assert_se(r == 0);
ecbff1
+                assert_se(streq(setting1, "1 2 3"));
ecbff1
+                break;
ecbff1
+
ecbff1
+        case 5:
ecbff1
+                assert_se(r == 0);
ecbff1
+                assert_se(streq(setting1, "1\\\\ \\\\2"));
ecbff1
+                break;
ecbff1
+
ecbff1
+        case 6:
ecbff1
+                assert_se(r == 0);
ecbff1
+                assert_se(streq(setting1, x1000("ABCD")));
ecbff1
+                break;
ecbff1
+
ecbff1
+        case 7:
ecbff1
+                assert_se(r == 0);
ecbff1
+                assert_se(streq(setting1, x1000("ABCD") " foobar"));
ecbff1
+                break;
ecbff1
+
ecbff1
+        case 8 ... 9:
ecbff1
+                assert_se(r == -ENOBUFS);
ecbff1
+                assert_se(setting1 == NULL);
ecbff1
+                break;
ecbff1
+        }
ecbff1
+}
ecbff1
+
ecbff1
+int main(int argc, char **argv) {
ecbff1
+        unsigned i;
ecbff1
+
ecbff1
+        for (i = 0; i < ELEMENTSOF(config_file); i++)
ecbff1
+                test_config_parse(i, config_file[i]);
ecbff1
+
ecbff1
+        return 0;
ecbff1
+}