]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-conf-parser.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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
233 "[Section]\n"
234 "setting1=1\\\n" /* normal continuation */
235 "2\\\n"
236 "3\n",
237
9adbfeb3
YW
238 "[Section]\n"
239 "#hogehoge\\\n" /* continuation is ignored in comment */
240 "setting1=1\\\n" /* normal continuation */
241 "2\\\n"
242 "3\n",
243
244 "[Section]\n"
245 "setting1=1\\\n" /* normal continuation */
246 "#hogehoge\\\n" /* commented out line in continuation is ignored */
247 "2\\\n"
248 "3\n",
249
ff650ffe
YW
250 "[Section]\n"
251 " #hogehoge\\\n" /* whitespaces before comments */
252 " setting1=1\\\n" /* whitespaces before key */
253 "2\\\n"
254 "3\n",
255
256 "[Section]\n"
257 " setting1=1\\\n" /* whitespaces before key */
258 " #hogehoge\\\n" /* commented out line prefixed with whitespaces in continuation */
259 "2\\\n"
260 "3\n",
261
4f29e0db
FB
262 "[Section]\n"
263 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
264 "2\\\n"
265 "3\\\n",
266
e3f46367
ZJS
267 "[Section]\n"
268 "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
269 "\\\\2\n", /* note that C requires one level of escaping, so the
270 * parser gets "…1 BS BS BS NL BS BS 2 NL", which
271 * it translates into "…1 BS BS SP BS BS 2" */
8f313f4f
ZJS
272
273 "\n[Section]\n\n"
274 "setting1=" /* a line above LINE_MAX length */
275 x1000("ABCD")
276 "\n",
277
278 "[Section]\n"
279 "setting1=" /* a line above LINE_MAX length, with continuation */
280 x1000("ABCD") "\\\n"
281 "foobar",
282
4f29e0db
FB
283 "[Section]\n"
284 "setting1=" /* a line above LINE_MAX length, with continuation */
285 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
286 "foobar\\\n",
287
8f313f4f
ZJS
288 "[Section]\n"
289 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
290 x1000(x1000("x") x10("abcde")) "\n",
291
292 "[Section]\n"
293 "setting1=" /* many continuation lines, together above the limit */
294 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
e3f46367
ZJS
295};
296
297static void test_config_parse(unsigned i, const char *s) {
627d2bac 298 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
e3f46367
ZJS
299 _cleanup_fclose_ FILE *f = NULL;
300 _cleanup_free_ char *setting1 = NULL;
d8351049 301 int r;
e3f46367
ZJS
302
303 const ConfigTableItem items[] = {
304 { "Section", "setting1", config_parse_string, 0, &setting1},
305 {}
306 };
307
308 log_info("== %s[%i] ==", __func__, i);
309
d8351049
ZJS
310 assert_se(fmkostemp_safe(name, "r+", &f) == 0);
311 assert_se(fwrite(s, strlen(s), 1, f) == 1);
312 rewind(f);
e3f46367
ZJS
313
314 /*
315 int config_parse(const char *unit,
316 const char *filename,
317 FILE *f,
318 const char *sections,
319 ConfigItemLookup lookup,
320 const void *table,
321 bool relaxed,
322 bool allow_include,
323 bool warn,
324 void *userdata)
325 */
326
327 r = config_parse(NULL, name, f,
328 "Section\0",
329 config_item_table_lookup, items,
bcde742e 330 CONFIG_PARSE_WARN, NULL);
e3f46367
ZJS
331
332 switch (i) {
333 case 0 ... 3:
8f313f4f 334 assert_se(r == 0);
e3f46367
ZJS
335 assert_se(streq(setting1, "1"));
336 break;
337
ff650ffe 338 case 4 ... 9:
8f313f4f 339 assert_se(r == 0);
e3f46367
ZJS
340 assert_se(streq(setting1, "1 2 3"));
341 break;
342
ff650ffe 343 case 10:
8f313f4f 344 assert_se(r == 0);
e3f46367
ZJS
345 assert_se(streq(setting1, "1\\\\ \\\\2"));
346 break;
8f313f4f 347
ff650ffe 348 case 11:
8f313f4f
ZJS
349 assert_se(r == 0);
350 assert_se(streq(setting1, x1000("ABCD")));
351 break;
352
ff650ffe 353 case 12 ... 13:
8f313f4f
ZJS
354 assert_se(r == 0);
355 assert_se(streq(setting1, x1000("ABCD") " foobar"));
356 break;
357
ff650ffe 358 case 14 ... 15:
8f313f4f
ZJS
359 assert_se(r == -ENOBUFS);
360 assert_se(setting1 == NULL);
361 break;
e3f46367
ZJS
362 }
363}
364
0763adbe 365int main(int argc, char **argv) {
e3f46367
ZJS
366 unsigned i;
367
0763adbe
RC
368 log_parse_environment();
369 log_open();
370
371 test_config_parse_path();
372 test_config_parse_log_level();
373 test_config_parse_log_facility();
374 test_config_parse_iec_size();
375 test_config_parse_si_size();
376 test_config_parse_int();
377 test_config_parse_unsigned();
378 test_config_parse_strv();
379 test_config_parse_mode();
380 test_config_parse_sec();
381 test_config_parse_nsec();
f55211db 382 test_config_parse_iec_uint64();
0763adbe 383
e3f46367
ZJS
384 for (i = 0; i < ELEMENTSOF(config_file); i++)
385 test_config_parse(i, config_file[i]);
386
0763adbe
RC
387 return 0;
388}