]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-parser.c
aa6e4f1a953e91b1bef5fbe9606648d4a5914ba7
[thirdparty/systemd.git] / src / test / test-conf-parser.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2015 Ronny Chevalier
6 ***/
7
8 #include "conf-parser.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "fs-util.h"
12 #include "log.h"
13 #include "macro.h"
14 #include "string-util.h"
15 #include "strv.h"
16 #include "util.h"
17
18 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
19 _cleanup_free_ char *path = NULL;
20
21 assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0);
22 assert_se(streq_ptr(expected, path));
23 }
24
25 static void test_config_parse_log_level_one(const char *rvalue, int expected) {
26 int log_level = 0;
27
28 assert_se(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_level, NULL) >= 0);
29 assert_se(expected == log_level);
30 }
31
32 static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
33 int log_facility = 0;
34
35 assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0);
36 assert_se(expected == log_facility);
37 }
38
39 static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
40 size_t iec_size = 0;
41
42 assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL) >= 0);
43 assert_se(expected == iec_size);
44 }
45
46 static void test_config_parse_si_size_one(const char *rvalue, size_t expected) {
47 size_t si_size = 0;
48
49 assert_se(config_parse_si_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_size, NULL) >= 0);
50 assert_se(expected == si_size);
51 }
52
53 static void test_config_parse_int_one(const char *rvalue, int expected) {
54 int v = -1;
55
56 assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
57 assert_se(expected == v);
58 }
59
60 static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) {
61 unsigned v = 0;
62
63 assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
64 assert_se(expected == v);
65 }
66
67 static void test_config_parse_strv_one(const char *rvalue, char **expected) {
68 _cleanup_strv_free_ char **strv = NULL;
69
70 assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0);
71 assert_se(strv_equal(expected, strv));
72 }
73
74 static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
75 mode_t v = 0;
76
77 assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
78 assert_se(expected == v);
79 }
80
81 static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
82 usec_t v = 0;
83
84 assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
85 assert_se(expected == v);
86 }
87
88 static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
89 nsec_t v = 0;
90
91 assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
92 assert_se(expected == v);
93 }
94
95 static void test_config_parse_path(void) {
96 test_config_parse_path_one("/path", "/path");
97 test_config_parse_path_one("/path//////////", "/path");
98 test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
99 test_config_parse_path_one("/path//./////hogehoge///.", "/path/hogehoge");
100 test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
101
102 test_config_parse_path_one("not_absolute/path", NULL);
103 test_config_parse_path_one("/path/\xc3\x7f", NULL);
104 }
105
106 static void test_config_parse_log_level(void) {
107 test_config_parse_log_level_one("debug", LOG_DEBUG);
108 test_config_parse_log_level_one("info", LOG_INFO);
109
110 test_config_parse_log_level_one("garbage", 0);
111 }
112
113 static void test_config_parse_log_facility(void) {
114 test_config_parse_log_facility_one("mail", LOG_MAIL);
115 test_config_parse_log_facility_one("user", LOG_USER);
116
117 test_config_parse_log_facility_one("garbage", 0);
118 }
119
120 static void test_config_parse_iec_size(void) {
121 test_config_parse_iec_size_one("1024", 1024);
122 test_config_parse_iec_size_one("2K", 2048);
123 test_config_parse_iec_size_one("10M", 10 * 1024 * 1024);
124 test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024);
125 test_config_parse_iec_size_one("0G", 0);
126 test_config_parse_iec_size_one("0", 0);
127
128 test_config_parse_iec_size_one("-982", 0);
129 test_config_parse_iec_size_one("49874444198739873000000G", 0);
130 test_config_parse_iec_size_one("garbage", 0);
131 }
132
133 static void test_config_parse_si_size(void) {
134 test_config_parse_si_size_one("1024", 1024);
135 test_config_parse_si_size_one("2K", 2000);
136 test_config_parse_si_size_one("10M", 10 * 1000 * 1000);
137 test_config_parse_si_size_one("1G", 1 * 1000 * 1000 * 1000);
138 test_config_parse_si_size_one("0G", 0);
139 test_config_parse_si_size_one("0", 0);
140
141 test_config_parse_si_size_one("-982", 0);
142 test_config_parse_si_size_one("49874444198739873000000G", 0);
143 test_config_parse_si_size_one("garbage", 0);
144 }
145
146 static void test_config_parse_int(void) {
147 test_config_parse_int_one("1024", 1024);
148 test_config_parse_int_one("-1024", -1024);
149 test_config_parse_int_one("0", 0);
150
151 test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1);
152 test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1);
153 test_config_parse_int_one("1G", -1);
154 test_config_parse_int_one("garbage", -1);
155 }
156
157 static void test_config_parse_unsigned(void) {
158 test_config_parse_unsigned_one("10241024", 10241024);
159 test_config_parse_unsigned_one("1024", 1024);
160 test_config_parse_unsigned_one("0", 0);
161
162 test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0);
163 test_config_parse_unsigned_one("1G", 0);
164 test_config_parse_unsigned_one("garbage", 0);
165 test_config_parse_unsigned_one("1000garbage", 0);
166 }
167
168 static void test_config_parse_strv(void) {
169 test_config_parse_strv_one("", STRV_MAKE_EMPTY);
170 test_config_parse_strv_one("foo", STRV_MAKE("foo"));
171 test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo"));
172 test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo"));
173 test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80"));
174 test_config_parse_strv_one("\xc3\x7f", STRV_MAKE("\xc3\x7f"));
175 }
176
177 static void test_config_parse_mode(void) {
178 test_config_parse_mode_one("777", 0777);
179 test_config_parse_mode_one("644", 0644);
180
181 test_config_parse_mode_one("-777", 0);
182 test_config_parse_mode_one("999", 0);
183 test_config_parse_mode_one("garbage", 0);
184 test_config_parse_mode_one("777garbage", 0);
185 test_config_parse_mode_one("777 garbage", 0);
186 }
187
188 static void test_config_parse_sec(void) {
189 test_config_parse_sec_one("1", 1 * USEC_PER_SEC);
190 test_config_parse_sec_one("1s", 1 * USEC_PER_SEC);
191 test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC);
192 test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC);
193
194 test_config_parse_sec_one("-1", 0);
195 test_config_parse_sec_one("10foo", 0);
196 test_config_parse_sec_one("garbage", 0);
197 }
198
199 static void test_config_parse_nsec(void) {
200 test_config_parse_nsec_one("1", 1);
201 test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC);
202 test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC);
203 test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC);
204
205 test_config_parse_nsec_one("-1", 0);
206 test_config_parse_nsec_one("10foo", 0);
207 test_config_parse_nsec_one("garbage", 0);
208 }
209
210 static void test_config_parse_iec_uint64(void) {
211 uint64_t offset = 0;
212 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
213 assert_se(offset == 4 * 1024 * 1024);
214
215 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
216 }
217
218 static void test_config_parse_join_controllers(void) {
219 int r;
220 _cleanup_(strv_free_freep) char ***c = NULL;
221 char ***c2;
222
223 /* Test normal operation */
224 r = config_parse_join_controllers(NULL, "example.conf", 11, "Section", 10, "JoinControllers", 0, "cpu,cpuacct net_cls,netprio", &c, NULL);
225 assert_se(r == 0);
226 assert_se(c);
227 assert_se(strv_length(c[0]) == 2);
228 assert_se(strv_equal(c[0], STRV_MAKE("cpu", "cpuacct")));
229 assert_se(strv_length(c[1]) == 2);
230 assert_se(strv_equal(c[1], STRV_MAKE("net_cls", "netprio")));
231 assert_se(c[2] == NULL);
232
233 /* Test special case of no mounted controllers */
234 r = config_parse_join_controllers(NULL, "example.conf", 12, "Section", 10, "JoinControllers", 0, "", &c, NULL);
235 assert_se(r == 0);
236 assert_se(c);
237 assert_se(strv_equal(c[0], STRV_MAKE_EMPTY));
238 assert_se(c[1] == NULL);
239
240 /* Test merging of overlapping lists */
241 r = config_parse_join_controllers(NULL, "example.conf", 13, "Section", 10, "JoinControllers", 0, "a,b b,c", &c, NULL);
242 assert_se(r == 0);
243 assert_se(c);
244 assert_se(strv_length(c[0]) == 3);
245 assert_se(strv_contains(c[0], "a"));
246 assert_se(strv_contains(c[0], "b"));
247 assert_se(strv_contains(c[0], "c"));
248 assert_se(c[1] == NULL);
249
250 /* Test ignoring of bad lines */
251 c2 = c;
252 r = config_parse_join_controllers(NULL, "example.conf", 14, "Section", 10, "JoinControllers", 0, "a,\"b ", &c, NULL);
253 assert_se(r < 0);
254 assert_se(c == c2);
255 }
256
257 #define x10(x) x x x x x x x x x x
258 #define x100(x) x10(x10(x))
259 #define x1000(x) x10(x100(x))
260
261 static const char* const config_file[] = {
262 "[Section]\n"
263 "setting1=1\n",
264
265 "[Section]\n"
266 "setting1=1", /* no terminating newline */
267
268 "\n\n\n\n[Section]\n\n\n"
269 "setting1=1", /* some whitespace, no terminating newline */
270
271 "[Section]\n"
272 "[Section]\n"
273 "setting1=1\n"
274 "setting1=2\n"
275 "setting1=1\n", /* repeated settings */
276
277 "[Section]\n"
278 "setting1=1\\\n" /* normal continuation */
279 "2\\\n"
280 "3\n",
281
282 "[Section]\n"
283 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
284 "2\\\n"
285 "3\\\n",
286
287 "[Section]\n"
288 "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
289 "\\\\2\n", /* note that C requires one level of escaping, so the
290 * parser gets "…1 BS BS BS NL BS BS 2 NL", which
291 * it translates into "…1 BS BS SP BS BS 2" */
292
293 "\n[Section]\n\n"
294 "setting1=" /* a line above LINE_MAX length */
295 x1000("ABCD")
296 "\n",
297
298 "[Section]\n"
299 "setting1=" /* a line above LINE_MAX length, with continuation */
300 x1000("ABCD") "\\\n"
301 "foobar",
302
303 "[Section]\n"
304 "setting1=" /* a line above LINE_MAX length, with continuation */
305 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
306 "foobar\\\n",
307
308 "[Section]\n"
309 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
310 x1000(x1000("x") x10("abcde")) "\n",
311
312 "[Section]\n"
313 "setting1=" /* many continuation lines, together above the limit */
314 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
315 };
316
317 static void test_config_parse(unsigned i, const char *s) {
318 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
319 int fd, r;
320 _cleanup_fclose_ FILE *f = NULL;
321 _cleanup_free_ char *setting1 = NULL;
322
323 const ConfigTableItem items[] = {
324 { "Section", "setting1", config_parse_string, 0, &setting1},
325 {}
326 };
327
328 log_info("== %s[%i] ==", __func__, i);
329
330 fd = mkostemp_safe(name);
331 assert_se(fd >= 0);
332 assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
333
334 assert_se(lseek(fd, 0, SEEK_SET) == 0);
335 assert_se(f = fdopen(fd, "r"));
336
337 /*
338 int config_parse(const char *unit,
339 const char *filename,
340 FILE *f,
341 const char *sections,
342 ConfigItemLookup lookup,
343 const void *table,
344 bool relaxed,
345 bool allow_include,
346 bool warn,
347 void *userdata)
348 */
349
350 r = config_parse(NULL, name, f,
351 "Section\0",
352 config_item_table_lookup, items,
353 CONFIG_PARSE_WARN, NULL);
354
355 switch (i) {
356 case 0 ... 3:
357 assert_se(r == 0);
358 assert_se(streq(setting1, "1"));
359 break;
360
361 case 4 ... 5:
362 assert_se(r == 0);
363 assert_se(streq(setting1, "1 2 3"));
364 break;
365
366 case 6:
367 assert_se(r == 0);
368 assert_se(streq(setting1, "1\\\\ \\\\2"));
369 break;
370
371 case 7:
372 assert_se(r == 0);
373 assert_se(streq(setting1, x1000("ABCD")));
374 break;
375
376 case 8 ... 9:
377 assert_se(r == 0);
378 assert_se(streq(setting1, x1000("ABCD") " foobar"));
379 break;
380
381 case 10 ... 11:
382 assert_se(r == -ENOBUFS);
383 assert_se(setting1 == NULL);
384 break;
385 }
386 }
387
388 int main(int argc, char **argv) {
389 unsigned i;
390
391 log_parse_environment();
392 log_open();
393
394 test_config_parse_path();
395 test_config_parse_log_level();
396 test_config_parse_log_facility();
397 test_config_parse_iec_size();
398 test_config_parse_si_size();
399 test_config_parse_int();
400 test_config_parse_unsigned();
401 test_config_parse_strv();
402 test_config_parse_mode();
403 test_config_parse_sec();
404 test_config_parse_nsec();
405 test_config_parse_iec_uint64();
406 test_config_parse_join_controllers();
407
408 for (i = 0; i < ELEMENTSOF(config_file); i++)
409 test_config_parse(i, config_file[i]);
410
411 return 0;
412 }