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