]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-conf-parser.c
shared/conf-parser: allow sections to be silently ignored with new -Section syntax
[thirdparty/systemd.git] / src / test / test-conf-parser.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
0763adbe
RC
2
3#include "conf-parser.h"
e3f46367 4#include "fd-util.h"
627d2bac 5#include "fs-util.h"
07630cea 6#include "log.h"
0763adbe 7#include "macro.h"
07630cea 8#include "string-util.h"
0763adbe 9#include "strv.h"
e4de7287 10#include "tmpfile-util.h"
07630cea 11#include "util.h"
0763adbe
RC
12
13static void test_config_parse_path_one(const char *rvalue, const char *expected) {
a12807aa 14 _cleanup_free_ char *path = NULL;
0763adbe
RC
15
16 assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0);
17 assert_se(streq_ptr(expected, path));
0763adbe
RC
18}
19
20static void test_config_parse_log_level_one(const char *rvalue, int expected) {
21 int log_level = 0;
22
23 assert_se(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_level, NULL) >= 0);
24 assert_se(expected == log_level);
25}
26
27static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
28 int log_facility = 0;
29
30 assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0);
31 assert_se(expected == log_facility);
32}
33
34static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
35 size_t iec_size = 0;
36
37 assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL) >= 0);
38 assert_se(expected == iec_size);
39}
40
41static void test_config_parse_si_size_one(const char *rvalue, size_t expected) {
42 size_t si_size = 0;
43
44 assert_se(config_parse_si_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_size, NULL) >= 0);
45 assert_se(expected == si_size);
46}
47
48static void test_config_parse_int_one(const char *rvalue, int expected) {
49 int v = -1;
50
51 assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
52 assert_se(expected == v);
53}
54
55static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) {
56 unsigned v = 0;
57
58 assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
59 assert_se(expected == v);
60}
61
62static void test_config_parse_strv_one(const char *rvalue, char **expected) {
a12807aa 63 _cleanup_strv_free_ char **strv = NULL;
0763adbe
RC
64
65 assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0);
66 assert_se(strv_equal(expected, strv));
0763adbe
RC
67}
68
69static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
70 mode_t v = 0;
71
72 assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
73 assert_se(expected == v);
74}
75
76static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
77 usec_t v = 0;
78
79 assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
80 assert_se(expected == v);
81}
82
83static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
84 nsec_t v = 0;
85
86 assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
87 assert_se(expected == v);
88}
89
90static void test_config_parse_path(void) {
91 test_config_parse_path_one("/path", "/path");
92 test_config_parse_path_one("/path//////////", "/path");
93 test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
cd4f53c5 94 test_config_parse_path_one("/path//./////hogehoge///.", "/path/hogehoge");
d5ade2d6 95 test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
0763adbe
RC
96
97 test_config_parse_path_one("not_absolute/path", NULL);
d5ade2d6 98 test_config_parse_path_one("/path/\xc3\x7f", NULL);
0763adbe
RC
99}
100
101static void test_config_parse_log_level(void) {
102 test_config_parse_log_level_one("debug", LOG_DEBUG);
103 test_config_parse_log_level_one("info", LOG_INFO);
104
105 test_config_parse_log_level_one("garbage", 0);
106}
107
108static void test_config_parse_log_facility(void) {
109 test_config_parse_log_facility_one("mail", LOG_MAIL);
110 test_config_parse_log_facility_one("user", LOG_USER);
111
112 test_config_parse_log_facility_one("garbage", 0);
113}
114
115static void test_config_parse_iec_size(void) {
116 test_config_parse_iec_size_one("1024", 1024);
117 test_config_parse_iec_size_one("2K", 2048);
118 test_config_parse_iec_size_one("10M", 10 * 1024 * 1024);
119 test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024);
120 test_config_parse_iec_size_one("0G", 0);
121 test_config_parse_iec_size_one("0", 0);
122
123 test_config_parse_iec_size_one("-982", 0);
124 test_config_parse_iec_size_one("49874444198739873000000G", 0);
125 test_config_parse_iec_size_one("garbage", 0);
126}
127
128static void test_config_parse_si_size(void) {
129 test_config_parse_si_size_one("1024", 1024);
130 test_config_parse_si_size_one("2K", 2000);
131 test_config_parse_si_size_one("10M", 10 * 1000 * 1000);
132 test_config_parse_si_size_one("1G", 1 * 1000 * 1000 * 1000);
133 test_config_parse_si_size_one("0G", 0);
134 test_config_parse_si_size_one("0", 0);
135
136 test_config_parse_si_size_one("-982", 0);
137 test_config_parse_si_size_one("49874444198739873000000G", 0);
138 test_config_parse_si_size_one("garbage", 0);
139}
140
141static void test_config_parse_int(void) {
142 test_config_parse_int_one("1024", 1024);
143 test_config_parse_int_one("-1024", -1024);
144 test_config_parse_int_one("0", 0);
145
146 test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1);
147 test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1);
148 test_config_parse_int_one("1G", -1);
149 test_config_parse_int_one("garbage", -1);
150}
151
152static void test_config_parse_unsigned(void) {
153 test_config_parse_unsigned_one("10241024", 10241024);
154 test_config_parse_unsigned_one("1024", 1024);
155 test_config_parse_unsigned_one("0", 0);
156
157 test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0);
158 test_config_parse_unsigned_one("1G", 0);
159 test_config_parse_unsigned_one("garbage", 0);
160 test_config_parse_unsigned_one("1000garbage", 0);
161}
162
163static void test_config_parse_strv(void) {
164 test_config_parse_strv_one("", STRV_MAKE_EMPTY);
165 test_config_parse_strv_one("foo", STRV_MAKE("foo"));
166 test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo"));
167 test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo"));
b4958f42 168 test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80"));
f106314c 169 test_config_parse_strv_one("\xc3\x7f", STRV_MAKE("\xc3\x7f"));
0763adbe
RC
170}
171
172static void test_config_parse_mode(void) {
173 test_config_parse_mode_one("777", 0777);
174 test_config_parse_mode_one("644", 0644);
175
176 test_config_parse_mode_one("-777", 0);
177 test_config_parse_mode_one("999", 0);
178 test_config_parse_mode_one("garbage", 0);
179 test_config_parse_mode_one("777garbage", 0);
180 test_config_parse_mode_one("777 garbage", 0);
181}
182
183static void test_config_parse_sec(void) {
184 test_config_parse_sec_one("1", 1 * USEC_PER_SEC);
185 test_config_parse_sec_one("1s", 1 * USEC_PER_SEC);
186 test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC);
187 test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC);
188
189 test_config_parse_sec_one("-1", 0);
190 test_config_parse_sec_one("10foo", 0);
191 test_config_parse_sec_one("garbage", 0);
192}
193
194static void test_config_parse_nsec(void) {
195 test_config_parse_nsec_one("1", 1);
196 test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC);
197 test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC);
198 test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC);
199
200 test_config_parse_nsec_one("-1", 0);
201 test_config_parse_nsec_one("10foo", 0);
202 test_config_parse_nsec_one("garbage", 0);
203}
204
f55211db
RC
205static void test_config_parse_iec_uint64(void) {
206 uint64_t offset = 0;
207 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
208 assert_se(offset == 4 * 1024 * 1024);
209
210 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
211}
212
8f313f4f
ZJS
213#define x10(x) x x x x x x x x x x
214#define x100(x) x10(x10(x))
215#define x1000(x) x10(x100(x))
216
e3f46367
ZJS
217static const char* const config_file[] = {
218 "[Section]\n"
219 "setting1=1\n",
220
221 "[Section]\n"
222 "setting1=1", /* no terminating newline */
223
224 "\n\n\n\n[Section]\n\n\n"
225 "setting1=1", /* some whitespace, no terminating newline */
226
227 "[Section]\n"
228 "[Section]\n"
229 "setting1=1\n"
230 "setting1=2\n"
231 "setting1=1\n", /* repeated settings */
232
3d5d346a
YW
233 "[Section]\n"
234 "[Section]\n"
235 "setting1=1\n"
236 "setting1=2\\\n"
237 " \n" /* empty line breaks continuation */
238 "setting1=1\n", /* repeated settings */
239
e3f46367
ZJS
240 "[Section]\n"
241 "setting1=1\\\n" /* normal continuation */
242 "2\\\n"
243 "3\n",
244
9adbfeb3
YW
245 "[Section]\n"
246 "#hogehoge\\\n" /* continuation is ignored in comment */
247 "setting1=1\\\n" /* normal continuation */
248 "2\\\n"
249 "3\n",
250
251 "[Section]\n"
252 "setting1=1\\\n" /* normal continuation */
253 "#hogehoge\\\n" /* commented out line in continuation is ignored */
254 "2\\\n"
255 "3\n",
256
ff650ffe
YW
257 "[Section]\n"
258 " #hogehoge\\\n" /* whitespaces before comments */
259 " setting1=1\\\n" /* whitespaces before key */
260 "2\\\n"
261 "3\n",
262
263 "[Section]\n"
264 " setting1=1\\\n" /* whitespaces before key */
265 " #hogehoge\\\n" /* commented out line prefixed with whitespaces in continuation */
266 "2\\\n"
267 "3\n",
268
4f29e0db
FB
269 "[Section]\n"
270 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
271 "2\\\n"
272 "3\\\n",
273
e3f46367
ZJS
274 "[Section]\n"
275 "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
276 "\\\\2\n", /* note that C requires one level of escaping, so the
277 * parser gets "…1 BS BS BS NL BS BS 2 NL", which
278 * it translates into "…1 BS BS SP BS BS 2" */
8f313f4f
ZJS
279
280 "\n[Section]\n\n"
281 "setting1=" /* a line above LINE_MAX length */
282 x1000("ABCD")
283 "\n",
284
285 "[Section]\n"
286 "setting1=" /* a line above LINE_MAX length, with continuation */
287 x1000("ABCD") "\\\n"
288 "foobar",
289
4f29e0db
FB
290 "[Section]\n"
291 "setting1=" /* a line above LINE_MAX length, with continuation */
292 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
293 "foobar\\\n",
294
8f313f4f
ZJS
295 "[Section]\n"
296 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
297 x1000(x1000("x") x10("abcde")) "\n",
298
299 "[Section]\n"
300 "setting1=" /* many continuation lines, together above the limit */
301 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
ddeb3f5d
ZJS
302
303 "[Section]\n"
304 "setting1=2\n"
305 "[NoWarnSection]\n"
306 "setting1=3\n"
307 "[WarnSection]\n"
308 "setting1=3\n"
309 "[X-Section]\n"
310 "setting1=3\n",
e3f46367
ZJS
311};
312
313static void test_config_parse(unsigned i, const char *s) {
627d2bac 314 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
e3f46367
ZJS
315 _cleanup_fclose_ FILE *f = NULL;
316 _cleanup_free_ char *setting1 = NULL;
d8351049 317 int r;
e3f46367
ZJS
318
319 const ConfigTableItem items[] = {
320 { "Section", "setting1", config_parse_string, 0, &setting1},
321 {}
322 };
323
324 log_info("== %s[%i] ==", __func__, i);
325
d8351049
ZJS
326 assert_se(fmkostemp_safe(name, "r+", &f) == 0);
327 assert_se(fwrite(s, strlen(s), 1, f) == 1);
328 rewind(f);
e3f46367
ZJS
329
330 /*
331 int config_parse(const char *unit,
332 const char *filename,
333 FILE *f,
334 const char *sections,
335 ConfigItemLookup lookup,
336 const void *table,
ddeb3f5d 337 ConfigParseFlags flags,
e3f46367
ZJS
338 void *userdata)
339 */
340
341 r = config_parse(NULL, name, f,
ddeb3f5d 342 "Section\0-NoWarnSection\0",
e3f46367 343 config_item_table_lookup, items,
bcde742e 344 CONFIG_PARSE_WARN, NULL);
e3f46367
ZJS
345
346 switch (i) {
3d5d346a 347 case 0 ... 4:
8f313f4f 348 assert_se(r == 0);
e3f46367
ZJS
349 assert_se(streq(setting1, "1"));
350 break;
351
3d5d346a 352 case 5 ... 10:
8f313f4f 353 assert_se(r == 0);
e3f46367
ZJS
354 assert_se(streq(setting1, "1 2 3"));
355 break;
356
3d5d346a 357 case 11:
8f313f4f 358 assert_se(r == 0);
e3f46367
ZJS
359 assert_se(streq(setting1, "1\\\\ \\\\2"));
360 break;
8f313f4f 361
3d5d346a 362 case 12:
8f313f4f
ZJS
363 assert_se(r == 0);
364 assert_se(streq(setting1, x1000("ABCD")));
365 break;
366
3d5d346a 367 case 13 ... 14:
8f313f4f
ZJS
368 assert_se(r == 0);
369 assert_se(streq(setting1, x1000("ABCD") " foobar"));
370 break;
371
3d5d346a 372 case 15 ... 16:
8f313f4f
ZJS
373 assert_se(r == -ENOBUFS);
374 assert_se(setting1 == NULL);
375 break;
ddeb3f5d
ZJS
376
377 case 17:
378 assert_se(r == 0);
379 assert_se(streq(setting1, "2"));
380 break;
e3f46367
ZJS
381 }
382}
383
0763adbe 384int main(int argc, char **argv) {
e3f46367
ZJS
385 unsigned i;
386
0763adbe
RC
387 log_parse_environment();
388 log_open();
389
390 test_config_parse_path();
391 test_config_parse_log_level();
392 test_config_parse_log_facility();
393 test_config_parse_iec_size();
394 test_config_parse_si_size();
395 test_config_parse_int();
396 test_config_parse_unsigned();
397 test_config_parse_strv();
398 test_config_parse_mode();
399 test_config_parse_sec();
400 test_config_parse_nsec();
f55211db 401 test_config_parse_iec_uint64();
0763adbe 402
e3f46367
ZJS
403 for (i = 0; i < ELEMENTSOF(config_file); i++)
404 test_config_parse(i, config_file[i]);
405
0763adbe
RC
406 return 0;
407}