]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-parser.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "conf-parser.h"
8 #include "string-util.h"
10 #include "tmpfile-util.h"
13 static void test_config_parse_path_one(const char *rvalue
, const char *expected
) {
14 _cleanup_free_
char *path
= NULL
;
16 assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &path
, NULL
) >= 0);
17 assert_se(streq_ptr(expected
, path
));
20 static void test_config_parse_log_level_one(const char *rvalue
, int expected
) {
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
);
27 static void test_config_parse_log_facility_one(const char *rvalue
, int expected
) {
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
);
34 static void test_config_parse_iec_size_one(const char *rvalue
, size_t expected
) {
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
);
41 static void test_config_parse_si_size_one(const char *rvalue
, size_t expected
) {
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
);
48 static void test_config_parse_int_one(const char *rvalue
, int expected
) {
51 assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
52 assert_se(expected
== v
);
55 static void test_config_parse_unsigned_one(const char *rvalue
, unsigned expected
) {
58 assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
59 assert_se(expected
== v
);
62 static void test_config_parse_strv_one(const char *rvalue
, char **expected
) {
63 _cleanup_strv_free_
char **strv
= NULL
;
65 assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &strv
, NULL
) >= 0);
66 assert_se(strv_equal(expected
, strv
));
69 static void test_config_parse_mode_one(const char *rvalue
, mode_t expected
) {
72 assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
73 assert_se(expected
== v
);
76 static void test_config_parse_sec_one(const char *rvalue
, usec_t expected
) {
79 assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
80 assert_se(expected
== v
);
83 static void test_config_parse_nsec_one(const char *rvalue
, nsec_t expected
) {
86 assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
87 assert_se(expected
== v
);
90 static 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");
94 test_config_parse_path_one("/path//./////hogehoge///.", "/path/hogehoge");
95 test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
97 test_config_parse_path_one("not_absolute/path", NULL
);
98 test_config_parse_path_one("/path/\xc3\x7f", NULL
);
101 static 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
);
105 test_config_parse_log_level_one("garbage", 0);
108 static 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
);
112 test_config_parse_log_facility_one("garbage", 0);
115 static 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);
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);
128 static 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);
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);
141 static 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);
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);
152 static 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);
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);
163 static 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"));
168 test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80"));
169 test_config_parse_strv_one("\xc3\x7f", STRV_MAKE("\xc3\x7f"));
172 static void test_config_parse_mode(void) {
173 test_config_parse_mode_one("777", 0777);
174 test_config_parse_mode_one("644", 0644);
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);
183 static 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
);
189 test_config_parse_sec_one("-1", 0);
190 test_config_parse_sec_one("10foo", 0);
191 test_config_parse_sec_one("garbage", 0);
194 static 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
);
200 test_config_parse_nsec_one("-1", 0);
201 test_config_parse_nsec_one("10foo", 0);
202 test_config_parse_nsec_one("garbage", 0);
205 static void test_config_parse_iec_uint64(void) {
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);
210 assert_se(config_parse_iec_uint64(NULL
, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset
, NULL
) == 0);
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))
217 static const char* const config_file
[] = {
222 "setting1=1", /* no terminating newline */
224 "\n\n\n\n[Section]\n\n\n"
225 "setting1=1", /* some whitespace, no terminating newline */
231 "setting1=1\n", /* repeated settings */
234 "setting1=1\\\n" /* normal continuation */
239 "#hogehoge\\\n" /* continuation is ignored in comment */
240 "setting1=1\\\n" /* normal continuation */
245 "setting1=1\\\n" /* normal continuation */
246 "#hogehoge\\\n" /* commented out line in continuation is ignored */
251 " #hogehoge\\\n" /* whitespaces before comments */
252 " setting1=1\\\n" /* whitespaces before key */
257 " setting1=1\\\n" /* whitespaces before key */
258 " #hogehoge\\\n" /* commented out line prefixed with whitespaces in continuation */
263 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
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" */
274 "setting1=" /* a line above LINE_MAX length */
279 "setting1=" /* a line above LINE_MAX length, with continuation */
284 "setting1=" /* a line above LINE_MAX length, with continuation */
285 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
289 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
290 x1000(x1000("x") x10("abcde")) "\n",
293 "setting1=" /* many continuation lines, together above the limit */
294 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
297 static void test_config_parse(unsigned i
, const char *s
) {
298 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-conf-parser.XXXXXX";
299 _cleanup_fclose_
FILE *f
= NULL
;
300 _cleanup_free_
char *setting1
= NULL
;
303 const ConfigTableItem items
[] = {
304 { "Section", "setting1", config_parse_string
, 0, &setting1
},
308 log_info("== %s[%i] ==", __func__
, i
);
310 assert_se(fmkostemp_safe(name
, "r+", &f
) == 0);
311 assert_se(fwrite(s
, strlen(s
), 1, f
) == 1);
315 int config_parse(const char *unit,
316 const char *filename,
318 const char *sections,
319 ConfigItemLookup lookup,
327 r
= config_parse(NULL
, name
, f
,
329 config_item_table_lookup
, items
,
330 CONFIG_PARSE_WARN
, NULL
);
335 assert_se(streq(setting1
, "1"));
340 assert_se(streq(setting1
, "1 2 3"));
345 assert_se(streq(setting1
, "1\\\\ \\\\2"));
350 assert_se(streq(setting1
, x1000("ABCD")));
355 assert_se(streq(setting1
, x1000("ABCD") " foobar"));
359 assert_se(r
== -ENOBUFS
);
360 assert_se(setting1
== NULL
);
365 int main(int argc
, char **argv
) {
368 log_parse_environment();
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();
382 test_config_parse_iec_uint64();
384 for (i
= 0; i
< ELEMENTSOF(config_file
); i
++)
385 test_config_parse(i
, config_file
[i
]);