2 This file is part of systemd.
4 Copyright 2015 Ronny Chevalier
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "conf-parser.h"
23 #include "string-util.h"
27 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
28 _cleanup_free_ char *path = NULL;
30 assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0);
31 assert_se(streq_ptr(expected, path));
34 static void test_config_parse_log_level_one(const char *rvalue, int expected) {
37 assert_se(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_level, NULL) >= 0);
38 assert_se(expected == log_level);
41 #if 0 /// UNNEEDED by elogind
42 static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
45 assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0);
46 assert_se(expected == log_facility);
50 static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
53 assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL) >= 0);
54 assert_se(expected == iec_size);
57 #if 0 /// UNNEEDED by elogind
58 static void test_config_parse_si_size_one(const char *rvalue, size_t expected) {
61 assert_se(config_parse_si_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_size, NULL) >= 0);
62 assert_se(expected == si_size);
66 static void test_config_parse_int_one(const char *rvalue, int expected) {
69 assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
70 assert_se(expected == v);
73 static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) {
76 assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
77 assert_se(expected == v);
80 static void test_config_parse_strv_one(const char *rvalue, char **expected) {
81 _cleanup_strv_free_ char **strv = NULL;
83 assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0);
84 assert_se(strv_equal(expected, strv));
87 static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
90 assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
91 assert_se(expected == v);
94 static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
97 assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
98 assert_se(expected == v);
101 #if 0 /// UNNEEDED by elogind
102 static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
105 assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
106 assert_se(expected == v);
110 static void test_config_parse_path(void) {
111 test_config_parse_path_one("/path", "/path");
112 test_config_parse_path_one("/path//////////", "/path");
113 test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
114 test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
116 test_config_parse_path_one("not_absolute/path", NULL);
117 test_config_parse_path_one("/path/\xc3\x7f", NULL);
120 static void test_config_parse_log_level(void) {
121 test_config_parse_log_level_one("debug", LOG_DEBUG);
122 test_config_parse_log_level_one("info", LOG_INFO);
124 test_config_parse_log_level_one("garbage", 0);
127 #if 0 /// UNNEEDED by elogind
128 static void test_config_parse_log_facility(void) {
129 test_config_parse_log_facility_one("mail", LOG_MAIL);
130 test_config_parse_log_facility_one("user", LOG_USER);
132 test_config_parse_log_facility_one("garbage", 0);
136 static void test_config_parse_iec_size(void) {
137 test_config_parse_iec_size_one("1024", 1024);
138 test_config_parse_iec_size_one("2K", 2048);
139 test_config_parse_iec_size_one("10M", 10 * 1024 * 1024);
140 test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024);
141 test_config_parse_iec_size_one("0G", 0);
142 test_config_parse_iec_size_one("0", 0);
144 test_config_parse_iec_size_one("-982", 0);
145 test_config_parse_iec_size_one("49874444198739873000000G", 0);
146 test_config_parse_iec_size_one("garbage", 0);
149 #if 0 /// UNNEEDED by elogind
150 static void test_config_parse_si_size(void) {
151 test_config_parse_si_size_one("1024", 1024);
152 test_config_parse_si_size_one("2K", 2000);
153 test_config_parse_si_size_one("10M", 10 * 1000 * 1000);
154 test_config_parse_si_size_one("1G", 1 * 1000 * 1000 * 1000);
155 test_config_parse_si_size_one("0G", 0);
156 test_config_parse_si_size_one("0", 0);
158 test_config_parse_si_size_one("-982", 0);
159 test_config_parse_si_size_one("49874444198739873000000G", 0);
160 test_config_parse_si_size_one("garbage", 0);
164 static void test_config_parse_int(void) {
165 test_config_parse_int_one("1024", 1024);
166 test_config_parse_int_one("-1024", -1024);
167 test_config_parse_int_one("0", 0);
169 test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1);
170 test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1);
171 test_config_parse_int_one("1G", -1);
172 test_config_parse_int_one("garbage", -1);
175 static void test_config_parse_unsigned(void) {
176 test_config_parse_unsigned_one("10241024", 10241024);
177 test_config_parse_unsigned_one("1024", 1024);
178 test_config_parse_unsigned_one("0", 0);
180 test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0);
181 test_config_parse_unsigned_one("1G", 0);
182 test_config_parse_unsigned_one("garbage", 0);
183 test_config_parse_unsigned_one("1000garbage", 0);
186 static void test_config_parse_strv(void) {
187 test_config_parse_strv_one("", STRV_MAKE_EMPTY);
188 test_config_parse_strv_one("foo", STRV_MAKE("foo"));
189 test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo"));
190 test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo"));
191 test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80"));
192 test_config_parse_strv_one("\xc3\x7f", STRV_MAKE_EMPTY);
195 static void test_config_parse_mode(void) {
196 test_config_parse_mode_one("777", 0777);
197 test_config_parse_mode_one("644", 0644);
199 test_config_parse_mode_one("-777", 0);
200 test_config_parse_mode_one("999", 0);
201 test_config_parse_mode_one("garbage", 0);
202 test_config_parse_mode_one("777garbage", 0);
203 test_config_parse_mode_one("777 garbage", 0);
206 static void test_config_parse_sec(void) {
207 test_config_parse_sec_one("1", 1 * USEC_PER_SEC);
208 test_config_parse_sec_one("1s", 1 * USEC_PER_SEC);
209 test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC);
210 test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC);
212 test_config_parse_sec_one("-1", 0);
213 test_config_parse_sec_one("10foo", 0);
214 test_config_parse_sec_one("garbage", 0);
217 #if 0 /// UNNEEDED by elogind
218 static void test_config_parse_nsec(void) {
219 test_config_parse_nsec_one("1", 1);
220 test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC);
221 test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC);
222 test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC);
224 test_config_parse_nsec_one("-1", 0);
225 test_config_parse_nsec_one("10foo", 0);
226 test_config_parse_nsec_one("garbage", 0);
229 static void test_config_parse_iec_uint64(void) {
231 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
232 assert_se(offset == 4 * 1024 * 1024);
234 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
238 #define x10(x) x x x x x x x x x x
239 #define x100(x) x10(x10(x))
240 #define x1000(x) x10(x100(x))
242 static const char* const config_file[] = {
247 "setting1=1", /* no terminating newline */
249 "\n\n\n\n[Section]\n\n\n"
250 "setting1=1", /* some whitespace, no terminating newline */
256 "setting1=1\n", /* repeated settings */
259 "setting1=1\\\n" /* normal continuation */
264 "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
265 "\\\\2\n", /* note that C requires one level of escaping, so the
266 * parser gets "…1 BS BS BS NL BS BS 2 NL", which
267 * it translates into "…1 BS BS SP BS BS 2" */
270 "setting1=" /* a line above LINE_MAX length */
275 "setting1=" /* a line above LINE_MAX length, with continuation */
280 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
281 x1000(x1000("x") x10("abcde")) "\n",
284 "setting1=" /* many continuation lines, together above the limit */
285 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
288 static void test_config_parse(unsigned i, const char *s) {
289 char name[] = "/tmp/test-conf-parser.XXXXXX";
291 _cleanup_fclose_ FILE *f = NULL;
292 _cleanup_free_ char *setting1 = NULL;
294 const ConfigTableItem items[] = {
295 { "Section", "setting1", config_parse_string, 0, &setting1},
299 log_info("== %s[%i] ==", __func__, i);
301 fd = mkostemp_safe(name);
303 assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
305 assert_se(lseek(fd, 0, SEEK_SET) == 0);
306 assert_se(f = fdopen(fd, "r"));
309 int config_parse(const char *unit,
310 const char *filename,
312 const char *sections,
313 ConfigItemLookup lookup,
321 r = config_parse(NULL, name, f,
323 config_item_table_lookup, items,
324 false, false, true, NULL);
329 assert_se(streq(setting1, "1"));
334 assert_se(streq(setting1, "1 2 3"));
339 assert_se(streq(setting1, "1\\\\ \\\\2"));
344 assert_se(streq(setting1, x1000("ABCD")));
349 assert_se(streq(setting1, x1000("ABCD") " foobar"));
353 assert_se(r == -ENOBUFS);
354 assert_se(setting1 == NULL);
359 int main(int argc, char **argv) {
362 log_parse_environment();
365 test_config_parse_path();
366 test_config_parse_log_level();
367 #if 0 /// UNNEEDED by elogind
368 test_config_parse_log_facility();
370 test_config_parse_iec_size();
371 #if 0 /// UNNEEDED by elogind
372 test_config_parse_si_size();
374 test_config_parse_int();
375 test_config_parse_unsigned();
376 test_config_parse_strv();
377 test_config_parse_mode();
378 test_config_parse_sec();
379 #if 0 /// UNNEEDED by elogind
380 test_config_parse_nsec();
381 test_config_parse_iec_uint64();
384 for (i = 0; i < ELEMENTSOF(config_file); i++)
385 test_config_parse(i, config_file[i]);