]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-parser.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2015 Ronny Chevalier
6 #include "conf-parser.h"
12 #include "string-util.h"
16 static void test_config_parse_path_one(const char *rvalue
, const char *expected
) {
17 _cleanup_free_
char *path
= NULL
;
19 assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &path
, NULL
) >= 0);
20 assert_se(streq_ptr(expected
, path
));
23 static void test_config_parse_log_level_one(const char *rvalue
, int expected
) {
26 assert_se(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &log_level
, NULL
) >= 0);
27 assert_se(expected
== log_level
);
30 static void test_config_parse_log_facility_one(const char *rvalue
, int expected
) {
33 assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &log_facility
, NULL
) >= 0);
34 assert_se(expected
== log_facility
);
37 static void test_config_parse_iec_size_one(const char *rvalue
, size_t expected
) {
40 assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &iec_size
, NULL
) >= 0);
41 assert_se(expected
== iec_size
);
44 static void test_config_parse_si_size_one(const char *rvalue
, size_t expected
) {
47 assert_se(config_parse_si_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &si_size
, NULL
) >= 0);
48 assert_se(expected
== si_size
);
51 static void test_config_parse_int_one(const char *rvalue
, int expected
) {
54 assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
55 assert_se(expected
== v
);
58 static void test_config_parse_unsigned_one(const char *rvalue
, unsigned expected
) {
61 assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
62 assert_se(expected
== v
);
65 static void test_config_parse_strv_one(const char *rvalue
, char **expected
) {
66 _cleanup_strv_free_
char **strv
= NULL
;
68 assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &strv
, NULL
) >= 0);
69 assert_se(strv_equal(expected
, strv
));
72 static void test_config_parse_mode_one(const char *rvalue
, mode_t expected
) {
75 assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
76 assert_se(expected
== v
);
79 static void test_config_parse_sec_one(const char *rvalue
, usec_t expected
) {
82 assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
83 assert_se(expected
== v
);
86 static void test_config_parse_nsec_one(const char *rvalue
, nsec_t expected
) {
89 assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
90 assert_se(expected
== v
);
93 static void test_config_parse_path(void) {
94 test_config_parse_path_one("/path", "/path");
95 test_config_parse_path_one("/path//////////", "/path");
96 test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
97 test_config_parse_path_one("/path//./////hogehoge///.", "/path/hogehoge");
98 test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
100 test_config_parse_path_one("not_absolute/path", NULL
);
101 test_config_parse_path_one("/path/\xc3\x7f", NULL
);
104 static void test_config_parse_log_level(void) {
105 test_config_parse_log_level_one("debug", LOG_DEBUG
);
106 test_config_parse_log_level_one("info", LOG_INFO
);
108 test_config_parse_log_level_one("garbage", 0);
111 static void test_config_parse_log_facility(void) {
112 test_config_parse_log_facility_one("mail", LOG_MAIL
);
113 test_config_parse_log_facility_one("user", LOG_USER
);
115 test_config_parse_log_facility_one("garbage", 0);
118 static void test_config_parse_iec_size(void) {
119 test_config_parse_iec_size_one("1024", 1024);
120 test_config_parse_iec_size_one("2K", 2048);
121 test_config_parse_iec_size_one("10M", 10 * 1024 * 1024);
122 test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024);
123 test_config_parse_iec_size_one("0G", 0);
124 test_config_parse_iec_size_one("0", 0);
126 test_config_parse_iec_size_one("-982", 0);
127 test_config_parse_iec_size_one("49874444198739873000000G", 0);
128 test_config_parse_iec_size_one("garbage", 0);
131 static void test_config_parse_si_size(void) {
132 test_config_parse_si_size_one("1024", 1024);
133 test_config_parse_si_size_one("2K", 2000);
134 test_config_parse_si_size_one("10M", 10 * 1000 * 1000);
135 test_config_parse_si_size_one("1G", 1 * 1000 * 1000 * 1000);
136 test_config_parse_si_size_one("0G", 0);
137 test_config_parse_si_size_one("0", 0);
139 test_config_parse_si_size_one("-982", 0);
140 test_config_parse_si_size_one("49874444198739873000000G", 0);
141 test_config_parse_si_size_one("garbage", 0);
144 static void test_config_parse_int(void) {
145 test_config_parse_int_one("1024", 1024);
146 test_config_parse_int_one("-1024", -1024);
147 test_config_parse_int_one("0", 0);
149 test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1);
150 test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1);
151 test_config_parse_int_one("1G", -1);
152 test_config_parse_int_one("garbage", -1);
155 static void test_config_parse_unsigned(void) {
156 test_config_parse_unsigned_one("10241024", 10241024);
157 test_config_parse_unsigned_one("1024", 1024);
158 test_config_parse_unsigned_one("0", 0);
160 test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0);
161 test_config_parse_unsigned_one("1G", 0);
162 test_config_parse_unsigned_one("garbage", 0);
163 test_config_parse_unsigned_one("1000garbage", 0);
166 static void test_config_parse_strv(void) {
167 test_config_parse_strv_one("", STRV_MAKE_EMPTY
);
168 test_config_parse_strv_one("foo", STRV_MAKE("foo"));
169 test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo"));
170 test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo"));
171 test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80"));
172 test_config_parse_strv_one("\xc3\x7f", STRV_MAKE("\xc3\x7f"));
175 static void test_config_parse_mode(void) {
176 test_config_parse_mode_one("777", 0777);
177 test_config_parse_mode_one("644", 0644);
179 test_config_parse_mode_one("-777", 0);
180 test_config_parse_mode_one("999", 0);
181 test_config_parse_mode_one("garbage", 0);
182 test_config_parse_mode_one("777garbage", 0);
183 test_config_parse_mode_one("777 garbage", 0);
186 static void test_config_parse_sec(void) {
187 test_config_parse_sec_one("1", 1 * USEC_PER_SEC
);
188 test_config_parse_sec_one("1s", 1 * USEC_PER_SEC
);
189 test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC
);
190 test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC
+ 20 * USEC_PER_SEC
);
192 test_config_parse_sec_one("-1", 0);
193 test_config_parse_sec_one("10foo", 0);
194 test_config_parse_sec_one("garbage", 0);
197 static void test_config_parse_nsec(void) {
198 test_config_parse_nsec_one("1", 1);
199 test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC
);
200 test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC
);
201 test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC
+ 20 * NSEC_PER_SEC
);
203 test_config_parse_nsec_one("-1", 0);
204 test_config_parse_nsec_one("10foo", 0);
205 test_config_parse_nsec_one("garbage", 0);
208 static void test_config_parse_iec_uint64(void) {
210 assert_se(config_parse_iec_uint64(NULL
, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset
, NULL
) == 0);
211 assert_se(offset
== 4 * 1024 * 1024);
213 assert_se(config_parse_iec_uint64(NULL
, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset
, NULL
) == 0);
216 static void test_config_parse_join_controllers(void) {
218 _cleanup_(strv_free_freep
) char ***c
= NULL
;
221 /* Test normal operation */
222 r
= config_parse_join_controllers(NULL
, "example.conf", 11, "Section", 10, "JoinControllers", 0, "cpu,cpuacct net_cls,netprio", &c
, NULL
);
225 assert_se(strv_length(c
[0]) == 2);
226 assert_se(strv_equal(c
[0], STRV_MAKE("cpu", "cpuacct")));
227 assert_se(strv_length(c
[1]) == 2);
228 assert_se(strv_equal(c
[1], STRV_MAKE("net_cls", "netprio")));
229 assert_se(c
[2] == NULL
);
231 /* Test special case of no mounted controllers */
232 r
= config_parse_join_controllers(NULL
, "example.conf", 12, "Section", 10, "JoinControllers", 0, "", &c
, NULL
);
235 assert_se(strv_equal(c
[0], STRV_MAKE_EMPTY
));
236 assert_se(c
[1] == NULL
);
238 /* Test merging of overlapping lists */
239 r
= config_parse_join_controllers(NULL
, "example.conf", 13, "Section", 10, "JoinControllers", 0, "a,b b,c", &c
, NULL
);
242 assert_se(strv_length(c
[0]) == 3);
243 assert_se(strv_contains(c
[0], "a"));
244 assert_se(strv_contains(c
[0], "b"));
245 assert_se(strv_contains(c
[0], "c"));
246 assert_se(c
[1] == NULL
);
248 /* Test ignoring of bad lines */
250 r
= config_parse_join_controllers(NULL
, "example.conf", 14, "Section", 10, "JoinControllers", 0, "a,\"b ", &c
, NULL
);
255 #define x10(x) x x x x x x x x x x
256 #define x100(x) x10(x10(x))
257 #define x1000(x) x10(x100(x))
259 static const char* const config_file
[] = {
264 "setting1=1", /* no terminating newline */
266 "\n\n\n\n[Section]\n\n\n"
267 "setting1=1", /* some whitespace, no terminating newline */
273 "setting1=1\n", /* repeated settings */
276 "setting1=1\\\n" /* normal continuation */
281 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
286 "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
287 "\\\\2\n", /* note that C requires one level of escaping, so the
288 * parser gets "…1 BS BS BS NL BS BS 2 NL", which
289 * it translates into "…1 BS BS SP BS BS 2" */
292 "setting1=" /* a line above LINE_MAX length */
297 "setting1=" /* a line above LINE_MAX length, with continuation */
302 "setting1=" /* a line above LINE_MAX length, with continuation */
303 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
307 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
308 x1000(x1000("x") x10("abcde")) "\n",
311 "setting1=" /* many continuation lines, together above the limit */
312 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
315 static void test_config_parse(unsigned i
, const char *s
) {
316 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-conf-parser.XXXXXX";
318 _cleanup_fclose_
FILE *f
= NULL
;
319 _cleanup_free_
char *setting1
= NULL
;
321 const ConfigTableItem items
[] = {
322 { "Section", "setting1", config_parse_string
, 0, &setting1
},
326 log_info("== %s[%i] ==", __func__
, i
);
328 fd
= mkostemp_safe(name
);
330 assert_se((size_t) write(fd
, s
, strlen(s
)) == strlen(s
));
332 assert_se(lseek(fd
, 0, SEEK_SET
) == 0);
333 assert_se(f
= fdopen(fd
, "r"));
336 int config_parse(const char *unit,
337 const char *filename,
339 const char *sections,
340 ConfigItemLookup lookup,
348 r
= config_parse(NULL
, name
, f
,
350 config_item_table_lookup
, items
,
351 CONFIG_PARSE_WARN
, NULL
);
356 assert_se(streq(setting1
, "1"));
361 assert_se(streq(setting1
, "1 2 3"));
366 assert_se(streq(setting1
, "1\\\\ \\\\2"));
371 assert_se(streq(setting1
, x1000("ABCD")));
376 assert_se(streq(setting1
, x1000("ABCD") " foobar"));
380 assert_se(r
== -ENOBUFS
);
381 assert_se(setting1
== NULL
);
386 int main(int argc
, char **argv
) {
389 log_parse_environment();
392 test_config_parse_path();
393 test_config_parse_log_level();
394 test_config_parse_log_facility();
395 test_config_parse_iec_size();
396 test_config_parse_si_size();
397 test_config_parse_int();
398 test_config_parse_unsigned();
399 test_config_parse_strv();
400 test_config_parse_mode();
401 test_config_parse_sec();
402 test_config_parse_nsec();
403 test_config_parse_iec_uint64();
404 test_config_parse_join_controllers();
406 for (i
= 0; i
< ELEMENTSOF(config_file
); i
++)
407 test_config_parse(i
, config_file
[i
]);