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