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