richardphibel / rpms / systemd

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