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