]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-parser.c
e2f9f28792e21b5408167bec091f841aa169b843
[thirdparty/systemd.git] / src / test / test-conf-parser.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "conf-parser.h"
6 #include "fd-util.h"
7 #include "fileio.h"
8 #include "log.h"
9 #include "mkdir.h"
10 #include "rm-rf.h"
11 #include "strv.h"
12 #include "tests.h"
13 #include "time-util.h"
14 #include "tmpfile-util.h"
15
16 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
17 _cleanup_free_ char *path = NULL;
18
19 ASSERT_OK(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL));
20 ASSERT_STREQ(expected, path);
21 }
22
23 static void test_config_parse_log_level_one(const char *rvalue, int expected) {
24 int log_level = 0;
25
26 ASSERT_OK(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_level, NULL));
27 ASSERT_EQ(expected, log_level);
28 }
29
30 static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
31 int log_facility = 0;
32
33 ASSERT_OK(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL));
34 ASSERT_EQ(expected, log_facility);
35 }
36
37 static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
38 size_t iec_size = 0;
39
40 ASSERT_OK(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL));
41 ASSERT_EQ(expected, iec_size);
42 }
43
44 static void test_config_parse_si_uint64_one(const char *rvalue, uint64_t expected) {
45 uint64_t si_uint64 = 0;
46
47 ASSERT_OK(config_parse_si_uint64("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_uint64, NULL));
48 ASSERT_EQ(expected, si_uint64);
49 }
50
51 static void test_config_parse_int_one(const char *rvalue, int expected) {
52 int v = -1;
53
54 ASSERT_OK(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL));
55 ASSERT_EQ(expected, v);
56 }
57
58 static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) {
59 unsigned v = 0;
60
61 ASSERT_OK(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL));
62 ASSERT_EQ(expected, v);
63 }
64
65 static void test_config_parse_strv_one(const char *rvalue, bool filter_duplicates, char **expected) {
66 _cleanup_strv_free_ char **strv = NULL;
67
68 ASSERT_OK(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", filter_duplicates, rvalue, &strv, NULL));
69 ASSERT_TRUE(strv_equal(expected, strv));
70 }
71
72 static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
73 mode_t v = 0;
74
75 ASSERT_OK(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL));
76 ASSERT_EQ(expected, v);
77 }
78
79 static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
80 usec_t v = 0;
81
82 ASSERT_OK(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL));
83 ASSERT_EQ(expected, v);
84 }
85
86 static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
87 nsec_t v = 0;
88
89 ASSERT_OK(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL));
90 ASSERT_EQ(expected, v);
91 }
92
93 static void test_config_parse_iec_uint64_one(const char *rvalue, uint64_t expected) {
94 uint64_t v = 0;
95
96 ASSERT_OK(config_parse_iec_uint64("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL));
97 ASSERT_EQ(expected, v);
98 }
99
100 TEST(config_parse_path) {
101 test_config_parse_path_one("/path", "/path");
102 test_config_parse_path_one("/path//////////", "/path");
103 test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
104 test_config_parse_path_one("/path//./////hogehoge///.", "/path/hogehoge");
105 test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
106
107 test_config_parse_path_one("not_absolute/path", NULL);
108 test_config_parse_path_one("/path/\xc3\x7f", NULL);
109 }
110
111 TEST(config_parse_log_level) {
112 test_config_parse_log_level_one("debug", LOG_DEBUG);
113 test_config_parse_log_level_one("info", LOG_INFO);
114
115 test_config_parse_log_level_one("garbage", 0);
116 }
117
118 TEST(config_parse_log_facility) {
119 test_config_parse_log_facility_one("mail", LOG_MAIL);
120 test_config_parse_log_facility_one("user", LOG_USER);
121
122 test_config_parse_log_facility_one("garbage", 0);
123 }
124
125 TEST(config_parse_iec_size) {
126 test_config_parse_iec_size_one("1024", 1024);
127 test_config_parse_iec_size_one("2K", 2048);
128 test_config_parse_iec_size_one("10M", 10 * 1024 * 1024);
129 test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024);
130 test_config_parse_iec_size_one("0G", 0);
131 test_config_parse_iec_size_one("0", 0);
132
133 test_config_parse_iec_size_one("-982", 0);
134 test_config_parse_iec_size_one("49874444198739873000000G", 0);
135 test_config_parse_iec_size_one("garbage", 0);
136 }
137
138 TEST(config_parse_si_uint64) {
139 test_config_parse_si_uint64_one("1024", 1024);
140 test_config_parse_si_uint64_one("2K", 2000);
141 test_config_parse_si_uint64_one("10M", 10 * 1000 * 1000);
142 test_config_parse_si_uint64_one("1G", 1 * 1000 * 1000 * 1000);
143 test_config_parse_si_uint64_one("0G", 0);
144 test_config_parse_si_uint64_one("0", 0);
145
146 test_config_parse_si_uint64_one("-982", 0);
147 test_config_parse_si_uint64_one("49874444198739873000000G", 0);
148 test_config_parse_si_uint64_one("garbage", 0);
149 }
150
151 TEST(config_parse_int) {
152 test_config_parse_int_one("1024", 1024);
153 test_config_parse_int_one("-1024", -1024);
154 test_config_parse_int_one("0", 0);
155
156 test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1);
157 test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1);
158 test_config_parse_int_one("1G", -1);
159 test_config_parse_int_one("garbage", -1);
160 }
161
162 TEST(config_parse_unsigned) {
163 test_config_parse_unsigned_one("10241024", 10241024);
164 test_config_parse_unsigned_one("1024", 1024);
165 test_config_parse_unsigned_one("0", 0);
166
167 test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0);
168 test_config_parse_unsigned_one("1G", 0);
169 test_config_parse_unsigned_one("garbage", 0);
170 test_config_parse_unsigned_one("1000garbage", 0);
171 }
172
173 TEST(config_parse_strv) {
174 test_config_parse_strv_one("", false, STRV_EMPTY);
175 test_config_parse_strv_one("foo", false, STRV_MAKE("foo"));
176 test_config_parse_strv_one("foo bar foo", false, STRV_MAKE("foo", "bar", "foo"));
177 test_config_parse_strv_one("\"foo bar\" foo", false, STRV_MAKE("foo bar", "foo"));
178 test_config_parse_strv_one("\xc3\x80", false, STRV_MAKE("\xc3\x80"));
179 test_config_parse_strv_one("\xc3\x7f", false, STRV_MAKE("\xc3\x7f"));
180
181 test_config_parse_strv_one("", true, STRV_EMPTY);
182 test_config_parse_strv_one("foo", true, STRV_MAKE("foo"));
183 test_config_parse_strv_one("foo bar foo", true, STRV_MAKE("foo", "bar"));
184 test_config_parse_strv_one("\"foo bar\" foo", true, STRV_MAKE("foo bar", "foo"));
185 test_config_parse_strv_one("\xc3\x80", true, STRV_MAKE("\xc3\x80"));
186 test_config_parse_strv_one("\xc3\x7f", true, STRV_MAKE("\xc3\x7f"));
187 }
188
189 TEST(config_parse_mode) {
190 test_config_parse_mode_one("777", 0777);
191 test_config_parse_mode_one("644", 0644);
192
193 test_config_parse_mode_one("-777", 0);
194 test_config_parse_mode_one("999", 0);
195 test_config_parse_mode_one("garbage", 0);
196 test_config_parse_mode_one("777garbage", 0);
197 test_config_parse_mode_one("777 garbage", 0);
198 }
199
200 TEST(config_parse_sec) {
201 test_config_parse_sec_one("1", 1 * USEC_PER_SEC);
202 test_config_parse_sec_one("1s", 1 * USEC_PER_SEC);
203 test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC);
204 test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC);
205
206 test_config_parse_sec_one("-1", 0);
207 test_config_parse_sec_one("10foo", 0);
208 test_config_parse_sec_one("garbage", 0);
209 }
210
211 TEST(config_parse_nsec) {
212 test_config_parse_nsec_one("1", 1);
213 test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC);
214 test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC);
215 test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC);
216
217 test_config_parse_nsec_one("-1", 0);
218 test_config_parse_nsec_one("10foo", 0);
219 test_config_parse_nsec_one("garbage", 0);
220 }
221
222 TEST(config_parse_iec_uint64) {
223 test_config_parse_iec_uint64_one("4M", UINT64_C(4 * 1024 * 1024));
224 test_config_parse_iec_uint64_one("4.5M", UINT64_C((4 * 1024 + 512) * 1024));
225 }
226
227 #define x10(x) x x x x x x x x x x
228 #define x100(x) x10(x10(x))
229 #define x1000(x) x10(x100(x))
230
231 static const char* const config_file[] = {
232 "[Section]\n"
233 "setting1=1\n",
234
235 "[Section]\n"
236 "setting1=1", /* no terminating newline */
237
238 "\n\n\n\n[Section]\n\n\n"
239 "setting1=1", /* some whitespace, no terminating newline */
240
241 "[Section]\n"
242 "[Section]\n"
243 "setting1=1\n"
244 "setting1= 2 \t\n"
245 "setting1= 1\n", /* repeated settings */
246
247 "[Section]\n"
248 "[Section]\n"
249 "setting1=1\n"
250 "setting1=2\\\n"
251 " \n" /* empty line breaks continuation */
252 "setting1=1\n", /* repeated settings */
253
254 "[Section]\n"
255 "setting1=1\\\n" /* normal continuation */
256 "2\\\n"
257 "3\n",
258
259 "[Section]\n"
260 "#hogehoge\\\n" /* continuation is ignored in comment */
261 "setting1=1\\\n" /* normal continuation */
262 "2\\\n"
263 "3\n",
264
265 "[Section]\n"
266 "setting1=1\\\n" /* normal continuation */
267 "#hogehoge\\\n" /* commented out line in continuation is ignored */
268 "2\\\n"
269 "3\n",
270
271 "[Section]\n"
272 " #hogehoge\\\n" /* whitespaces before comments */
273 " setting1=1\\\n" /* whitespaces before key */
274 "2\\\n"
275 "3\n",
276
277 "[Section]\n"
278 " setting1=1\\\n" /* whitespaces before key */
279 " #hogehoge\\\n" /* commented out line prefixed with whitespaces in continuation */
280 "2\\\n"
281 "3\n",
282
283 "[Section]\n"
284 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
285 "2\\\n"
286 "3\\\n",
287
288 "[Section]\n"
289 "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
290 "\\\\2\n", /* note that C requires one level of escaping, so the
291 * parser gets "…1 BS BS BS NL BS BS 2 NL", which
292 * it translates into "…1 BS BS SP BS BS 2" */
293
294 "\n[Section]\n\n"
295 "setting1=" /* a line above LINE_MAX length */
296 x1000("ABCD")
297 "\n",
298
299 "[Section]\n"
300 "setting1=" /* a line above LINE_MAX length, with continuation */
301 x1000("ABCD") "\\\n"
302 "foobar",
303
304 "[Section]\n"
305 "setting1=" /* a line above LINE_MAX length, with continuation */
306 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
307 "foobar\\\n",
308
309 "[Section]\n"
310 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
311 x1000(x1000("x") x10("abcde")) "\n",
312
313 "[Section]\n"
314 "setting1=" /* many continuation lines, together above the limit */
315 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
316
317 "[Section]\n"
318 "setting1=2\n"
319 "[NoWarnSection]\n"
320 "setting1=3\n"
321 "[WarnSection]\n"
322 "setting1=3\n"
323 "[X-Section]\n"
324 "setting1=3\n",
325 };
326
327 static void test_config_parse_one(unsigned i, const char *s) {
328 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
329 _cleanup_fclose_ FILE *f = NULL;
330 _cleanup_free_ char *setting1 = NULL;
331 int r;
332
333 const ConfigTableItem items[] = {
334 { "Section", "setting1", config_parse_string, 0, &setting1},
335 {}
336 };
337
338 log_info("== %s[%u] ==", __func__, i);
339
340 assert_se(fmkostemp_safe(name, "r+", &f) == 0);
341 assert_se(fwrite(s, strlen(s), 1, f) == 1);
342 rewind(f);
343
344 /*
345 int config_parse(const char *unit,
346 const char *filename,
347 FILE *f,
348 const char *sections,
349 ConfigItemLookup lookup,
350 const void *table,
351 ConfigParseFlags flags,
352 void *userdata,
353 struct stat *ret_stat);
354 */
355
356 r = config_parse(NULL, name, f,
357 "Section\0"
358 "-NoWarnSection\0",
359 config_item_table_lookup, items,
360 CONFIG_PARSE_WARN,
361 NULL,
362 NULL);
363
364 switch (i) {
365 case 0 ... 4:
366 assert_se(r == 1);
367 ASSERT_STREQ(setting1, "1");
368 break;
369
370 case 5 ... 10:
371 assert_se(r == 1);
372 ASSERT_STREQ(setting1, "1 2 3");
373 break;
374
375 case 11:
376 assert_se(r == 1);
377 ASSERT_STREQ(setting1, "1\\\\ \\\\2");
378 break;
379
380 case 12:
381 assert_se(r == 1);
382 ASSERT_STREQ(setting1, x1000("ABCD"));
383 break;
384
385 case 13 ... 14:
386 assert_se(r == 1);
387 ASSERT_STREQ(setting1, x1000("ABCD") " foobar");
388 break;
389
390 case 15 ... 16:
391 assert_se(r == -ENOBUFS);
392 ASSERT_NULL(setting1);
393 break;
394
395 case 17:
396 assert_se(r == 1);
397 ASSERT_STREQ(setting1, "2");
398 break;
399 }
400 }
401
402 TEST(config_parse) {
403 for (unsigned i = 0; i < ELEMENTSOF(config_file); i++)
404 test_config_parse_one(i, config_file[i]);
405 }
406
407 TEST(config_parse_standard_file_with_dropins_full) {
408 _cleanup_(rm_rf_physical_and_freep) char *root = NULL;
409 _cleanup_close_ int rfd = -EBADF;
410 int r;
411
412 ASSERT_OK(rfd = mkdtemp_open("/tmp/test-config-parse-XXXXXX", 0, &root));
413 assert_se(mkdir_p_root(root, "/etc/kernel/install.conf.d", UID_INVALID, GID_INVALID, 0755));
414 assert_se(mkdir_p_root(root, "/run/kernel/install.conf.d", UID_INVALID, GID_INVALID, 0755));
415 assert_se(mkdir_p_root(root, "/usr/lib/kernel/install.conf.d", UID_INVALID, GID_INVALID, 0755));
416 assert_se(mkdir_p_root(root, "/usr/local/lib/kernel/install.conf.d", UID_INVALID, GID_INVALID, 0755));
417
418 assert_se(write_string_file_at(rfd, "usr/lib/kernel/install.conf", /* this one is ignored */
419 "A=!!!", WRITE_STRING_FILE_CREATE) == 0);
420 assert_se(write_string_file_at(rfd, "usr/local/lib/kernel/install.conf",
421 "A=aaa", WRITE_STRING_FILE_CREATE) == 0);
422 assert_se(write_string_file_at(rfd, "usr/local/lib/kernel/install.conf.d/drop1.conf",
423 "B=bbb", WRITE_STRING_FILE_CREATE) == 0);
424 assert_se(write_string_file_at(rfd, "usr/local/lib/kernel/install.conf.d/drop2.conf",
425 "C=c1", WRITE_STRING_FILE_CREATE) == 0);
426 assert_se(write_string_file_at(rfd, "usr/lib/kernel/install.conf.d/drop2.conf", /* this one is ignored */
427 "C=c2", WRITE_STRING_FILE_CREATE) == 0);
428 assert_se(write_string_file_at(rfd, "run/kernel/install.conf.d/drop3.conf",
429 "D=ddd", WRITE_STRING_FILE_CREATE) == 0);
430 assert_se(write_string_file_at(rfd, "etc/kernel/install.conf.d/drop4.conf",
431 "E=eee", WRITE_STRING_FILE_CREATE) == 0);
432
433 _cleanup_free_ char *A = NULL, *B = NULL, *C = NULL, *D = NULL, *E = NULL, *F = NULL;
434 _cleanup_strv_free_ char **dropins = NULL;
435
436 const ConfigTableItem items[] = {
437 { NULL, "A", config_parse_string, 0, &A},
438 { NULL, "B", config_parse_string, 0, &B},
439 { NULL, "C", config_parse_string, 0, &C},
440 { NULL, "D", config_parse_string, 0, &D},
441 { NULL, "E", config_parse_string, 0, &E},
442 { NULL, "F", config_parse_string, 0, &F},
443 {}
444 };
445
446 r = config_parse_standard_file_with_dropins_full(
447 root, "kernel/install.conf",
448 /* sections= */ NULL,
449 config_item_table_lookup, items,
450 CONFIG_PARSE_WARN,
451 /* userdata= */ NULL,
452 /* ret_stats_by_path= */ NULL,
453 /* ret_dropin_files= */ &dropins);
454 assert_se(r >= 0);
455 ASSERT_STREQ(A, "aaa");
456 ASSERT_STREQ(B, "bbb");
457 ASSERT_STREQ(C, "c1");
458 ASSERT_STREQ(D, "ddd");
459 ASSERT_STREQ(E, "eee");
460 ASSERT_STREQ(F, NULL);
461
462 A = mfree(A);
463 B = mfree(B);
464 C = mfree(C);
465 D = mfree(D);
466 E = mfree(E);
467
468 assert_se(strv_length(dropins) == 4);
469
470 /* Make sure that we follow symlinks */
471 assert_se(mkdir_p_root(root, "/etc/kernel/install2.conf.d", UID_INVALID, GID_INVALID, 0755));
472 assert_se(mkdir_p_root(root, "/run/kernel/install2.conf.d", UID_INVALID, GID_INVALID, 0755));
473 assert_se(mkdir_p_root(root, "/usr/lib/kernel/install2.conf.d", UID_INVALID, GID_INVALID, 0755));
474 assert_se(mkdir_p_root(root, "/usr/local/lib/kernel/install2.conf.d", UID_INVALID, GID_INVALID, 0755));
475
476 /* (Those symlinks are only useful relative to <root>. */
477 assert_se(symlinkat("/usr/lib/kernel/install.conf", rfd, "usr/lib/kernel/install2.conf") == 0);
478 assert_se(symlinkat("/usr/local/lib/kernel/install.conf", rfd, "usr/local/lib/kernel/install2.conf") == 0);
479 assert_se(symlinkat("/usr/local/lib/kernel/install.conf.d/drop1.conf", rfd, "usr/local/lib/kernel/install2.conf.d/drop1.conf") == 0);
480 assert_se(symlinkat("/usr/local/lib/kernel/install.conf.d/drop2.conf", rfd, "usr/local/lib/kernel/install2.conf.d/drop2.conf") == 0);
481 assert_se(symlinkat("/usr/lib/kernel/install.conf.d/drop2.conf", rfd, "usr/lib/kernel/install2.conf.d/drop2.conf") == 0);
482 assert_se(symlinkat("/run/kernel/install.conf.d/drop3.conf", rfd, "run/kernel/install2.conf.d/drop3.conf") == 0);
483 assert_se(symlinkat("/etc/kernel/install.conf.d/drop4.conf", rfd, "etc/kernel/install2.conf.d/drop4.conf") == 0);
484
485 r = config_parse_standard_file_with_dropins_full(
486 root, "kernel/install2.conf",
487 /* sections= */ NULL,
488 config_item_table_lookup, items,
489 CONFIG_PARSE_WARN,
490 /* userdata= */ NULL,
491 /* ret_stats_by_path= */ NULL,
492 /* ret_dropin_files= */ NULL);
493 assert_se(r >= 0);
494 ASSERT_STREQ(A, "aaa");
495 ASSERT_STREQ(B, "bbb");
496 ASSERT_STREQ(C, "c1");
497 ASSERT_STREQ(D, "ddd");
498 ASSERT_STREQ(E, "eee");
499 ASSERT_STREQ(F, NULL);
500 }
501
502 DEFINE_TEST_MAIN(LOG_INFO);