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