1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "conf-parser.h"
13 #include "time-util.h"
14 #include "tmpfile-util.h"
16 static void test_config_parse_path_one(const char *rvalue
, const char *expected
) {
17 _cleanup_free_
char *path
= NULL
;
19 ASSERT_OK(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &path
, NULL
));
20 ASSERT_STREQ(expected
, path
);
23 static void test_config_parse_log_level_one(const char *rvalue
, int expected
) {
26 ASSERT_OK(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &log_level
, NULL
));
27 ASSERT_EQ(expected
, log_level
);
30 static void test_config_parse_log_facility_one(const char *rvalue
, int expected
) {
33 ASSERT_OK(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &log_facility
, NULL
));
34 ASSERT_EQ(expected
, log_facility
);
37 static void test_config_parse_iec_size_one(const char *rvalue
, size_t expected
) {
40 ASSERT_OK(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &iec_size
, NULL
));
41 ASSERT_EQ(expected
, iec_size
);
44 static void test_config_parse_si_uint64_one(const char *rvalue
, uint64_t expected
) {
45 uint64_t si_uint64
= 0;
47 ASSERT_OK(config_parse_si_uint64("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &si_uint64
, NULL
));
48 ASSERT_EQ(expected
, si_uint64
);
51 static void test_config_parse_int_one(const char *rvalue
, int expected
) {
54 ASSERT_OK(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
));
55 ASSERT_EQ(expected
, v
);
58 static void test_config_parse_unsigned_one(const char *rvalue
, unsigned expected
) {
61 ASSERT_OK(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
));
62 ASSERT_EQ(expected
, v
);
65 static void test_config_parse_strv_one(const char *rvalue
, bool filter_duplicates
, char **expected
) {
66 _cleanup_strv_free_
char **strv
= NULL
;
68 ASSERT_OK(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", filter_duplicates
, rvalue
, &strv
, NULL
));
69 ASSERT_TRUE(strv_equal(expected
, strv
));
72 static void test_config_parse_mode_one(const char *rvalue
, mode_t expected
) {
75 ASSERT_OK(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
));
76 ASSERT_EQ(expected
, v
);
79 static void test_config_parse_sec_one(const char *rvalue
, usec_t expected
) {
82 ASSERT_OK(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
));
83 ASSERT_EQ(expected
, v
);
86 static void test_config_parse_nsec_one(const char *rvalue
, nsec_t expected
) {
89 ASSERT_OK(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue
, &v
, NULL
));
90 ASSERT_EQ(expected
, v
);
93 static void test_config_parse_iec_uint64_one(const char *rvalue
, uint64_t expected
) {
96 ASSERT_OK(config_parse_iec_uint64("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue
, &v
, NULL
));
97 ASSERT_EQ(expected
, v
);
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");
107 test_config_parse_path_one("not_absolute/path", NULL
);
108 test_config_parse_path_one("/path/\xc3\x7f", NULL
);
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
);
115 test_config_parse_log_level_one("garbage", 0);
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
);
122 test_config_parse_log_facility_one("garbage", 0);
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);
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);
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);
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);
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);
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);
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);
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);
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"));
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"));
189 TEST(config_parse_mode
) {
190 test_config_parse_mode_one("777", 0777);
191 test_config_parse_mode_one("644", 0644);
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);
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
);
206 test_config_parse_sec_one("-1", 0);
207 test_config_parse_sec_one("10foo", 0);
208 test_config_parse_sec_one("garbage", 0);
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
);
217 test_config_parse_nsec_one("-1", 0);
218 test_config_parse_nsec_one("10foo", 0);
219 test_config_parse_nsec_one("garbage", 0);
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));
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))
231 static const char* const config_file
[] = {
236 "setting1=1", /* no terminating newline */
238 "\n\n\n\n[Section]\n\n\n"
239 "setting1=1", /* some whitespace, no terminating newline */
245 "setting1= 1\n", /* repeated settings */
251 " \n" /* empty line breaks continuation */
252 "setting1=1\n", /* repeated settings */
255 "setting1=1\\\n" /* normal continuation */
260 "#hogehoge\\\n" /* continuation is ignored in comment */
261 "setting1=1\\\n" /* normal continuation */
266 "setting1=1\\\n" /* normal continuation */
267 "#hogehoge\\\n" /* commented out line in continuation is ignored */
272 " #hogehoge\\\n" /* whitespaces before comments */
273 " setting1=1\\\n" /* whitespaces before key */
278 " setting1=1\\\n" /* whitespaces before key */
279 " #hogehoge\\\n" /* commented out line prefixed with whitespaces in continuation */
284 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
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" */
295 "setting1=" /* a line above LINE_MAX length */
300 "setting1=" /* a line above LINE_MAX length, with continuation */
305 "setting1=" /* a line above LINE_MAX length, with continuation */
306 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
310 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
311 x1000(x1000("x") x10("abcde")) "\n",
314 "setting1=" /* many continuation lines, together above the limit */
315 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
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
;
333 const ConfigTableItem items
[] = {
334 { "Section", "setting1", config_parse_string
, 0, &setting1
},
338 log_info("== %s[%u] ==", __func__
, i
);
340 assert_se(fmkostemp_safe(name
, "r+", &f
) == 0);
341 assert_se(fwrite(s
, strlen(s
), 1, f
) == 1);
345 int config_parse(const char *unit,
346 const char *filename,
348 const char *sections,
349 ConfigItemLookup lookup,
351 ConfigParseFlags flags,
353 struct stat *ret_stat);
356 r
= config_parse(NULL
, name
, f
,
359 config_item_table_lookup
, items
,
367 ASSERT_STREQ(setting1
, "1");
372 ASSERT_STREQ(setting1
, "1 2 3");
377 ASSERT_STREQ(setting1
, "1\\\\ \\\\2");
382 ASSERT_STREQ(setting1
, x1000("ABCD"));
387 ASSERT_STREQ(setting1
, x1000("ABCD") " foobar");
391 assert_se(r
== -ENOBUFS
);
392 ASSERT_NULL(setting1
);
397 ASSERT_STREQ(setting1
, "2");
403 for (unsigned i
= 0; i
< ELEMENTSOF(config_file
); i
++)
404 test_config_parse_one(i
, config_file
[i
]);
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
;
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));
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);
433 _cleanup_free_
char *A
= NULL
, *B
= NULL
, *C
= NULL
, *D
= NULL
, *E
= NULL
, *F
= NULL
;
434 _cleanup_strv_free_
char **dropins
= NULL
;
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
},
446 r
= config_parse_standard_file_with_dropins_full(
447 root
, "kernel/install.conf",
448 /* sections= */ NULL
,
449 config_item_table_lookup
, items
,
451 /* userdata= */ NULL
,
452 /* ret_stats_by_path= */ NULL
,
453 /* ret_dropin_files= */ &dropins
);
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
);
468 assert_se(strv_length(dropins
) == 4);
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));
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);
485 r
= config_parse_standard_file_with_dropins_full(
486 root
, "kernel/install2.conf",
487 /* sections= */ NULL
,
488 config_item_table_lookup
, items
,
490 /* userdata= */ NULL
,
491 /* ret_stats_by_path= */ NULL
,
492 /* ret_dropin_files= */ NULL
);
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
);
502 DEFINE_TEST_MAIN(LOG_INFO
);