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