1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Ronny Chevalier
8 #include "conf-parser.h"
11 //#include "fs-util.h"
14 #include "string-util.h"
18 /// Additional includes needed by elogind
22 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
23 _cleanup_free_ char *path = NULL;
25 assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0);
26 assert_se(streq_ptr(expected, path));
29 static void test_config_parse_log_level_one(const char *rvalue, int expected) {
32 assert_se(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_level, NULL) >= 0);
33 assert_se(expected == log_level);
36 #if 0 /// UNNEEDED by elogind
37 static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
40 assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0);
41 assert_se(expected == log_facility);
45 static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
48 assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL) >= 0);
49 assert_se(expected == iec_size);
52 #if 0 /// UNNEEDED by elogind
53 static void test_config_parse_si_size_one(const char *rvalue, size_t expected) {
56 assert_se(config_parse_si_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_size, NULL) >= 0);
57 assert_se(expected == si_size);
61 static void test_config_parse_int_one(const char *rvalue, int expected) {
64 assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
65 assert_se(expected == v);
68 static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) {
71 assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
72 assert_se(expected == v);
75 static void test_config_parse_strv_one(const char *rvalue, char **expected) {
76 _cleanup_strv_free_ char **strv = NULL;
78 assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0);
79 assert_se(strv_equal(expected, strv));
82 static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
85 assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
86 assert_se(expected == v);
89 static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
92 assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
93 assert_se(expected == v);
96 #if 0 /// UNNEEDED by elogind
97 static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
100 assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
101 assert_se(expected == v);
105 static void test_config_parse_path(void) {
106 test_config_parse_path_one("/path", "/path");
107 test_config_parse_path_one("/path//////////", "/path");
108 test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
109 test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
111 test_config_parse_path_one("not_absolute/path", NULL);
112 test_config_parse_path_one("/path/\xc3\x7f", NULL);
115 static void test_config_parse_log_level(void) {
116 test_config_parse_log_level_one("debug", LOG_DEBUG);
117 test_config_parse_log_level_one("info", LOG_INFO);
119 test_config_parse_log_level_one("garbage", 0);
122 #if 0 /// UNNEEDED by elogind
123 static void test_config_parse_log_facility(void) {
124 test_config_parse_log_facility_one("mail", LOG_MAIL);
125 test_config_parse_log_facility_one("user", LOG_USER);
127 test_config_parse_log_facility_one("garbage", 0);
131 static void test_config_parse_iec_size(void) {
132 test_config_parse_iec_size_one("1024", 1024);
133 test_config_parse_iec_size_one("2K", 2048);
134 test_config_parse_iec_size_one("10M", 10 * 1024 * 1024);
135 test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024);
136 test_config_parse_iec_size_one("0G", 0);
137 test_config_parse_iec_size_one("0", 0);
139 test_config_parse_iec_size_one("-982", 0);
140 test_config_parse_iec_size_one("49874444198739873000000G", 0);
141 test_config_parse_iec_size_one("garbage", 0);
144 #if 0 /// UNNEEDED by elogind
145 static void test_config_parse_si_size(void) {
146 test_config_parse_si_size_one("1024", 1024);
147 test_config_parse_si_size_one("2K", 2000);
148 test_config_parse_si_size_one("10M", 10 * 1000 * 1000);
149 test_config_parse_si_size_one("1G", 1 * 1000 * 1000 * 1000);
150 test_config_parse_si_size_one("0G", 0);
151 test_config_parse_si_size_one("0", 0);
153 test_config_parse_si_size_one("-982", 0);
154 test_config_parse_si_size_one("49874444198739873000000G", 0);
155 test_config_parse_si_size_one("garbage", 0);
159 static void test_config_parse_int(void) {
160 test_config_parse_int_one("1024", 1024);
161 test_config_parse_int_one("-1024", -1024);
162 test_config_parse_int_one("0", 0);
164 test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1);
165 test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1);
166 test_config_parse_int_one("1G", -1);
167 test_config_parse_int_one("garbage", -1);
170 static void test_config_parse_unsigned(void) {
171 test_config_parse_unsigned_one("10241024", 10241024);
172 test_config_parse_unsigned_one("1024", 1024);
173 test_config_parse_unsigned_one("0", 0);
175 test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0);
176 test_config_parse_unsigned_one("1G", 0);
177 test_config_parse_unsigned_one("garbage", 0);
178 test_config_parse_unsigned_one("1000garbage", 0);
181 static void test_config_parse_strv(void) {
182 test_config_parse_strv_one("", STRV_MAKE_EMPTY);
183 test_config_parse_strv_one("foo", STRV_MAKE("foo"));
184 test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo"));
185 test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo"));
186 test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80"));
187 test_config_parse_strv_one("\xc3\x7f", STRV_MAKE_EMPTY);
190 static void test_config_parse_mode(void) {
191 test_config_parse_mode_one("777", 0777);
192 test_config_parse_mode_one("644", 0644);
194 test_config_parse_mode_one("-777", 0);
195 test_config_parse_mode_one("999", 0);
196 test_config_parse_mode_one("garbage", 0);
197 test_config_parse_mode_one("777garbage", 0);
198 test_config_parse_mode_one("777 garbage", 0);
201 static void test_config_parse_sec(void) {
202 test_config_parse_sec_one("1", 1 * USEC_PER_SEC);
203 test_config_parse_sec_one("1s", 1 * USEC_PER_SEC);
204 test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC);
205 test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC);
207 test_config_parse_sec_one("-1", 0);
208 test_config_parse_sec_one("10foo", 0);
209 test_config_parse_sec_one("garbage", 0);
212 #if 0 /// UNNEEDED by elogind
213 static void test_config_parse_nsec(void) {
214 test_config_parse_nsec_one("1", 1);
215 test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC);
216 test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC);
217 test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC);
219 test_config_parse_nsec_one("-1", 0);
220 test_config_parse_nsec_one("10foo", 0);
221 test_config_parse_nsec_one("garbage", 0);
224 static void test_config_parse_iec_uint64(void) {
226 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
227 assert_se(offset == 4 * 1024 * 1024);
229 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
232 static void test_config_parse_join_controllers(void) {
234 _cleanup_(strv_free_freep) char ***c = NULL;
237 /* Test normal operation */
238 r = config_parse_join_controllers(NULL, "example.conf", 11, "Section", 10, "JoinControllers", 0, "cpu,cpuacct net_cls,netprio", &c, NULL);
241 assert_se(strv_length(c[0]) == 2);
242 assert_se(strv_equal(c[0], STRV_MAKE("cpu", "cpuacct")));
243 assert_se(strv_length(c[1]) == 2);
244 assert_se(strv_equal(c[1], STRV_MAKE("net_cls", "netprio")));
245 assert_se(c[2] == NULL);
247 /* Test special case of no mounted controllers */
248 r = config_parse_join_controllers(NULL, "example.conf", 12, "Section", 10, "JoinControllers", 0, "", &c, NULL);
251 assert_se(strv_equal(c[0], STRV_MAKE_EMPTY));
252 assert_se(c[1] == NULL);
254 /* Test merging of overlapping lists */
255 r = config_parse_join_controllers(NULL, "example.conf", 13, "Section", 10, "JoinControllers", 0, "a,b b,c", &c, NULL);
258 assert_se(strv_length(c[0]) == 3);
259 assert_se(strv_contains(c[0], "a"));
260 assert_se(strv_contains(c[0], "b"));
261 assert_se(strv_contains(c[0], "c"));
262 assert_se(c[1] == NULL);
264 /* Test ignoring of bad lines */
266 r = config_parse_join_controllers(NULL, "example.conf", 14, "Section", 10, "JoinControllers", 0, "a,\"b ", &c, NULL);
272 #define x10(x) x x x x x x x x x x
273 #define x100(x) x10(x10(x))
274 #define x1000(x) x10(x100(x))
276 static const char* const config_file[] = {
281 "setting1=1", /* no terminating newline */
283 "\n\n\n\n[Section]\n\n\n"
284 "setting1=1", /* some whitespace, no terminating newline */
290 "setting1=1\n", /* repeated settings */
293 "setting1=1\\\n" /* normal continuation */
298 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
303 "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
304 "\\\\2\n", /* note that C requires one level of escaping, so the
305 * parser gets "…1 BS BS BS NL BS BS 2 NL", which
306 * it translates into "…1 BS BS SP BS BS 2" */
309 "setting1=" /* a line above LINE_MAX length */
314 "setting1=" /* a line above LINE_MAX length, with continuation */
319 "setting1=" /* a line above LINE_MAX length, with continuation */
320 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
324 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
325 x1000(x1000("x") x10("abcde")) "\n",
328 "setting1=" /* many continuation lines, together above the limit */
329 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
332 static void test_config_parse(unsigned i, const char *s) {
333 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
335 _cleanup_fclose_ FILE *f = NULL;
336 _cleanup_free_ char *setting1 = NULL;
338 const ConfigTableItem items[] = {
339 { "Section", "setting1", config_parse_string, 0, &setting1},
343 log_info("== %s[%i] ==", __func__, i);
345 fd = mkostemp_safe(name);
347 assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
349 assert_se(lseek(fd, 0, SEEK_SET) == 0);
350 assert_se(f = fdopen(fd, "r"));
353 int config_parse(const char *unit,
354 const char *filename,
356 const char *sections,
357 ConfigItemLookup lookup,
365 r = config_parse(NULL, name, f,
367 config_item_table_lookup, items,
368 CONFIG_PARSE_WARN, NULL);
373 assert_se(streq(setting1, "1"));
378 assert_se(streq(setting1, "1 2 3"));
383 assert_se(streq(setting1, "1\\\\ \\\\2"));
388 assert_se(streq(setting1, x1000("ABCD")));
393 assert_se(streq(setting1, x1000("ABCD") " foobar"));
397 assert_se(r == -ENOBUFS);
398 assert_se(setting1 == NULL);
403 int main(int argc, char **argv) {
406 log_parse_environment();
409 test_config_parse_path();
410 test_config_parse_log_level();
411 #if 0 /// UNNEEDED by elogind
412 test_config_parse_log_facility();
414 test_config_parse_iec_size();
415 #if 0 /// UNNEEDED by elogind
416 test_config_parse_si_size();
418 test_config_parse_int();
419 test_config_parse_unsigned();
420 test_config_parse_strv();
421 test_config_parse_mode();
422 test_config_parse_sec();
423 #if 0 /// UNNEEDED by elogind
424 test_config_parse_nsec();
425 test_config_parse_iec_uint64();
426 test_config_parse_join_controllers();
429 for (i = 0; i < ELEMENTSOF(config_file); i++)
430 test_config_parse(i, config_file[i]);