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