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