]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-parser.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / test / test-conf-parser.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2015 Ronny Chevalier
4 ***/
5
6 #include "conf-parser.h"
7 #include "fd-util.h"
8 #include "fileio.h"
9 #include "fs-util.h"
10 #include "log.h"
11 #include "macro.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "util.h"
15
16 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
17 _cleanup_free_ char *path = NULL;
18
19 assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0);
20 assert_se(streq_ptr(expected, path));
21 }
22
23 static void test_config_parse_log_level_one(const char *rvalue, int expected) {
24 int log_level = 0;
25
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);
28 }
29
30 static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
31 int log_facility = 0;
32
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);
35 }
36
37 static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
38 size_t iec_size = 0;
39
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);
42 }
43
44 static void test_config_parse_si_size_one(const char *rvalue, size_t expected) {
45 size_t si_size = 0;
46
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);
49 }
50
51 static void test_config_parse_int_one(const char *rvalue, int expected) {
52 int v = -1;
53
54 assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
55 assert_se(expected == v);
56 }
57
58 static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) {
59 unsigned v = 0;
60
61 assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
62 assert_se(expected == v);
63 }
64
65 static void test_config_parse_strv_one(const char *rvalue, char **expected) {
66 _cleanup_strv_free_ char **strv = NULL;
67
68 assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0);
69 assert_se(strv_equal(expected, strv));
70 }
71
72 static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
73 mode_t v = 0;
74
75 assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
76 assert_se(expected == v);
77 }
78
79 static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
80 usec_t v = 0;
81
82 assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
83 assert_se(expected == v);
84 }
85
86 static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
87 nsec_t v = 0;
88
89 assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
90 assert_se(expected == v);
91 }
92
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");
99
100 test_config_parse_path_one("not_absolute/path", NULL);
101 test_config_parse_path_one("/path/\xc3\x7f", NULL);
102 }
103
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);
107
108 test_config_parse_log_level_one("garbage", 0);
109 }
110
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);
114
115 test_config_parse_log_facility_one("garbage", 0);
116 }
117
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);
125
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);
129 }
130
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);
138
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);
142 }
143
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);
148
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);
153 }
154
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);
159
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);
164 }
165
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"));
173 }
174
175 static void test_config_parse_mode(void) {
176 test_config_parse_mode_one("777", 0777);
177 test_config_parse_mode_one("644", 0644);
178
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);
184 }
185
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);
191
192 test_config_parse_sec_one("-1", 0);
193 test_config_parse_sec_one("10foo", 0);
194 test_config_parse_sec_one("garbage", 0);
195 }
196
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);
202
203 test_config_parse_nsec_one("-1", 0);
204 test_config_parse_nsec_one("10foo", 0);
205 test_config_parse_nsec_one("garbage", 0);
206 }
207
208 static void test_config_parse_iec_uint64(void) {
209 uint64_t offset = 0;
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);
212
213 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
214 }
215
216 static void test_config_parse_join_controllers(void) {
217 int r;
218 _cleanup_(strv_free_freep) char ***c = NULL;
219 char ***c2;
220
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);
223 assert_se(r == 0);
224 assert_se(c);
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);
230
231 /* Test special case of no mounted controllers */
232 r = config_parse_join_controllers(NULL, "example.conf", 12, "Section", 10, "JoinControllers", 0, "", &c, NULL);
233 assert_se(r == 0);
234 assert_se(c);
235 assert_se(strv_equal(c[0], STRV_MAKE_EMPTY));
236 assert_se(c[1] == NULL);
237
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);
240 assert_se(r == 0);
241 assert_se(c);
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);
247
248 /* Test ignoring of bad lines */
249 c2 = c;
250 r = config_parse_join_controllers(NULL, "example.conf", 14, "Section", 10, "JoinControllers", 0, "a,\"b ", &c, NULL);
251 assert_se(r < 0);
252 assert_se(c == c2);
253 }
254
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))
258
259 static const char* const config_file[] = {
260 "[Section]\n"
261 "setting1=1\n",
262
263 "[Section]\n"
264 "setting1=1", /* no terminating newline */
265
266 "\n\n\n\n[Section]\n\n\n"
267 "setting1=1", /* some whitespace, no terminating newline */
268
269 "[Section]\n"
270 "[Section]\n"
271 "setting1=1\n"
272 "setting1=2\n"
273 "setting1=1\n", /* repeated settings */
274
275 "[Section]\n"
276 "setting1=1\\\n" /* normal continuation */
277 "2\\\n"
278 "3\n",
279
280 "[Section]\n"
281 "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
282 "2\\\n"
283 "3\\\n",
284
285 "[Section]\n"
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" */
290
291 "\n[Section]\n\n"
292 "setting1=" /* a line above LINE_MAX length */
293 x1000("ABCD")
294 "\n",
295
296 "[Section]\n"
297 "setting1=" /* a line above LINE_MAX length, with continuation */
298 x1000("ABCD") "\\\n"
299 "foobar",
300
301 "[Section]\n"
302 "setting1=" /* a line above LINE_MAX length, with continuation */
303 x1000("ABCD") "\\\n" /* and an extra trailing backslash */
304 "foobar\\\n",
305
306 "[Section]\n"
307 "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */
308 x1000(x1000("x") x10("abcde")) "\n",
309
310 "[Section]\n"
311 "setting1=" /* many continuation lines, together above the limit */
312 x1000(x1000("x") x10("abcde") "\\\n") "xxx",
313 };
314
315 static void test_config_parse(unsigned i, const char *s) {
316 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
317 int fd, r;
318 _cleanup_fclose_ FILE *f = NULL;
319 _cleanup_free_ char *setting1 = NULL;
320
321 const ConfigTableItem items[] = {
322 { "Section", "setting1", config_parse_string, 0, &setting1},
323 {}
324 };
325
326 log_info("== %s[%i] ==", __func__, i);
327
328 fd = mkostemp_safe(name);
329 assert_se(fd >= 0);
330 assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
331
332 assert_se(lseek(fd, 0, SEEK_SET) == 0);
333 assert_se(f = fdopen(fd, "r"));
334
335 /*
336 int config_parse(const char *unit,
337 const char *filename,
338 FILE *f,
339 const char *sections,
340 ConfigItemLookup lookup,
341 const void *table,
342 bool relaxed,
343 bool allow_include,
344 bool warn,
345 void *userdata)
346 */
347
348 r = config_parse(NULL, name, f,
349 "Section\0",
350 config_item_table_lookup, items,
351 CONFIG_PARSE_WARN, NULL);
352
353 switch (i) {
354 case 0 ... 3:
355 assert_se(r == 0);
356 assert_se(streq(setting1, "1"));
357 break;
358
359 case 4 ... 5:
360 assert_se(r == 0);
361 assert_se(streq(setting1, "1 2 3"));
362 break;
363
364 case 6:
365 assert_se(r == 0);
366 assert_se(streq(setting1, "1\\\\ \\\\2"));
367 break;
368
369 case 7:
370 assert_se(r == 0);
371 assert_se(streq(setting1, x1000("ABCD")));
372 break;
373
374 case 8 ... 9:
375 assert_se(r == 0);
376 assert_se(streq(setting1, x1000("ABCD") " foobar"));
377 break;
378
379 case 10 ... 11:
380 assert_se(r == -ENOBUFS);
381 assert_se(setting1 == NULL);
382 break;
383 }
384 }
385
386 int main(int argc, char **argv) {
387 unsigned i;
388
389 log_parse_environment();
390 log_open();
391
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();
405
406 for (i = 0; i < ELEMENTSOF(config_file); i++)
407 test_config_parse(i, config_file[i]);
408
409 return 0;
410 }