chiark / gitweb /
test-conf-parser: add tests for the new long lines, including overflow handling
[elogind.git] / src / test / test-conf-parser.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2015 Ronny Chevalier
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "conf-parser.h"
21 #include "log.h"
22 #include "macro.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "util.h"
26
27 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
28         _cleanup_free_ char *path = NULL;
29
30         assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0);
31         assert_se(streq_ptr(expected, path));
32 }
33
34 static void test_config_parse_log_level_one(const char *rvalue, int expected) {
35         int log_level = 0;
36
37         assert_se(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_level, NULL) >= 0);
38         assert_se(expected == log_level);
39 }
40
41 #if 0 /// UNNEEDED by elogind
42 static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
43         int log_facility = 0;
44
45         assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0);
46         assert_se(expected == log_facility);
47 }
48 #endif // 0
49
50 static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
51         size_t iec_size = 0;
52
53         assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL) >= 0);
54         assert_se(expected == iec_size);
55 }
56
57 #if 0 /// UNNEEDED by elogind
58 static void test_config_parse_si_size_one(const char *rvalue, size_t expected) {
59         size_t si_size = 0;
60
61         assert_se(config_parse_si_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_size, NULL) >= 0);
62         assert_se(expected == si_size);
63 }
64 #endif // 0
65
66 static void test_config_parse_int_one(const char *rvalue, int expected) {
67         int v = -1;
68
69         assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
70         assert_se(expected == v);
71 }
72
73 static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) {
74         unsigned v = 0;
75
76         assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
77         assert_se(expected == v);
78 }
79
80 static void test_config_parse_strv_one(const char *rvalue, char **expected) {
81         _cleanup_strv_free_ char **strv = NULL;
82
83         assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0);
84         assert_se(strv_equal(expected, strv));
85 }
86
87 static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
88         mode_t v = 0;
89
90         assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
91         assert_se(expected == v);
92 }
93
94 static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
95         usec_t v = 0;
96
97         assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
98         assert_se(expected == v);
99 }
100
101 #if 0 /// UNNEEDED by elogind
102 static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
103         nsec_t v = 0;
104
105         assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
106         assert_se(expected == v);
107 }
108 #endif // 0
109
110 static void test_config_parse_path(void) {
111         test_config_parse_path_one("/path", "/path");
112         test_config_parse_path_one("/path//////////", "/path");
113         test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
114         test_config_parse_path_one("/path/\xc3\x80", "/path/\xc3\x80");
115
116         test_config_parse_path_one("not_absolute/path", NULL);
117         test_config_parse_path_one("/path/\xc3\x7f", NULL);
118 }
119
120 static void test_config_parse_log_level(void) {
121         test_config_parse_log_level_one("debug", LOG_DEBUG);
122         test_config_parse_log_level_one("info", LOG_INFO);
123
124         test_config_parse_log_level_one("garbage", 0);
125 }
126
127 #if 0 /// UNNEEDED by elogind
128 static void test_config_parse_log_facility(void) {
129         test_config_parse_log_facility_one("mail", LOG_MAIL);
130         test_config_parse_log_facility_one("user", LOG_USER);
131
132         test_config_parse_log_facility_one("garbage", 0);
133 }
134 #endif // 0
135
136 static void test_config_parse_iec_size(void) {
137         test_config_parse_iec_size_one("1024", 1024);
138         test_config_parse_iec_size_one("2K", 2048);
139         test_config_parse_iec_size_one("10M", 10 * 1024 * 1024);
140         test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024);
141         test_config_parse_iec_size_one("0G", 0);
142         test_config_parse_iec_size_one("0", 0);
143
144         test_config_parse_iec_size_one("-982", 0);
145         test_config_parse_iec_size_one("49874444198739873000000G", 0);
146         test_config_parse_iec_size_one("garbage", 0);
147 }
148
149 #if 0 /// UNNEEDED by elogind
150 static void test_config_parse_si_size(void) {
151         test_config_parse_si_size_one("1024", 1024);
152         test_config_parse_si_size_one("2K", 2000);
153         test_config_parse_si_size_one("10M", 10 * 1000 * 1000);
154         test_config_parse_si_size_one("1G", 1 * 1000 * 1000 * 1000);
155         test_config_parse_si_size_one("0G", 0);
156         test_config_parse_si_size_one("0", 0);
157
158         test_config_parse_si_size_one("-982", 0);
159         test_config_parse_si_size_one("49874444198739873000000G", 0);
160         test_config_parse_si_size_one("garbage", 0);
161 }
162 #endif // 0
163
164 static void test_config_parse_int(void) {
165         test_config_parse_int_one("1024", 1024);
166         test_config_parse_int_one("-1024", -1024);
167         test_config_parse_int_one("0", 0);
168
169         test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1);
170         test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1);
171         test_config_parse_int_one("1G", -1);
172         test_config_parse_int_one("garbage", -1);
173 }
174
175 static void test_config_parse_unsigned(void) {
176         test_config_parse_unsigned_one("10241024", 10241024);
177         test_config_parse_unsigned_one("1024", 1024);
178         test_config_parse_unsigned_one("0", 0);
179
180         test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0);
181         test_config_parse_unsigned_one("1G", 0);
182         test_config_parse_unsigned_one("garbage", 0);
183         test_config_parse_unsigned_one("1000garbage", 0);
184 }
185
186 static void test_config_parse_strv(void) {
187         test_config_parse_strv_one("", STRV_MAKE_EMPTY);
188         test_config_parse_strv_one("foo", STRV_MAKE("foo"));
189         test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo"));
190         test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo"));
191         test_config_parse_strv_one("\xc3\x80", STRV_MAKE("\xc3\x80"));
192         test_config_parse_strv_one("\xc3\x7f", STRV_MAKE_EMPTY);
193 }
194
195 static void test_config_parse_mode(void) {
196         test_config_parse_mode_one("777", 0777);
197         test_config_parse_mode_one("644", 0644);
198
199         test_config_parse_mode_one("-777", 0);
200         test_config_parse_mode_one("999", 0);
201         test_config_parse_mode_one("garbage", 0);
202         test_config_parse_mode_one("777garbage", 0);
203         test_config_parse_mode_one("777 garbage", 0);
204 }
205
206 static void test_config_parse_sec(void) {
207         test_config_parse_sec_one("1", 1 * USEC_PER_SEC);
208         test_config_parse_sec_one("1s", 1 * USEC_PER_SEC);
209         test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC);
210         test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC);
211
212         test_config_parse_sec_one("-1", 0);
213         test_config_parse_sec_one("10foo", 0);
214         test_config_parse_sec_one("garbage", 0);
215 }
216
217 #if 0 /// UNNEEDED by elogind
218 static void test_config_parse_nsec(void) {
219         test_config_parse_nsec_one("1", 1);
220         test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC);
221         test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC);
222         test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC);
223
224         test_config_parse_nsec_one("-1", 0);
225         test_config_parse_nsec_one("10foo", 0);
226         test_config_parse_nsec_one("garbage", 0);
227 }
228
229 static void test_config_parse_iec_uint64(void) {
230         uint64_t offset = 0;
231         assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
232         assert_se(offset == 4 * 1024 * 1024);
233
234         assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
235 }
236 #endif // 0
237
238 #define x10(x) x x x x x x x x x x
239 #define x100(x) x10(x10(x))
240 #define x1000(x) x10(x100(x))
241
242 static const char* const config_file[] = {
243         "[Section]\n"
244         "setting1=1\n",
245
246         "[Section]\n"
247         "setting1=1",        /* no terminating newline */
248
249         "\n\n\n\n[Section]\n\n\n"
250         "setting1=1",        /* some whitespace, no terminating newline */
251
252         "[Section]\n"
253         "[Section]\n"
254         "setting1=1\n"
255         "setting1=2\n"
256         "setting1=1\n",      /* repeated settings */
257
258         "[Section]\n"
259         "setting1=1\\\n"     /* normal continuation */
260         "2\\\n"
261         "3\n",
262
263         "[Section]\n"
264         "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
265         "\\\\2\n",           /* note that C requires one level of escaping, so the
266                               * parser gets "…1 BS BS BS NL BS BS 2 NL", which
267                               * it translates into "…1 BS BS SP BS BS 2" */
268
269         "\n[Section]\n\n"
270         "setting1="          /* a line above LINE_MAX length */
271         x1000("ABCD")
272         "\n",
273
274         "[Section]\n"
275         "setting1="          /* a line above LINE_MAX length, with continuation */
276         x1000("ABCD") "\\\n"
277         "foobar",
278
279         "[Section]\n"
280         "setting1="          /* a line above the allowed limit: 9 + 1050000 + 1 */
281         x1000(x1000("x") x10("abcde")) "\n",
282
283         "[Section]\n"
284         "setting1="          /* many continuation lines, together above the limit */
285         x1000(x1000("x") x10("abcde") "\\\n") "xxx",
286 };
287
288 static void test_config_parse(unsigned i, const char *s) {
289         char name[] = "/tmp/test-conf-parser.XXXXXX";
290         int fd, r;
291         _cleanup_fclose_ FILE *f = NULL;
292         _cleanup_free_ char *setting1 = NULL;
293
294         const ConfigTableItem items[] = {
295                 { "Section", "setting1",  config_parse_string,   0, &setting1},
296                 {}
297         };
298
299         log_info("== %s[%i] ==", __func__, i);
300
301         fd = mkostemp_safe(name);
302         assert_se(fd >= 0);
303         assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
304
305         assert_se(lseek(fd, 0, SEEK_SET) == 0);
306         assert_se(f = fdopen(fd, "r"));
307
308         /*
309         int config_parse(const char *unit,
310                          const char *filename,
311                          FILE *f,
312                          const char *sections,
313                          ConfigItemLookup lookup,
314                          const void *table,
315                          bool relaxed,
316                          bool allow_include,
317                          bool warn,
318                          void *userdata)
319         */
320
321         r = config_parse(NULL, name, f,
322                          "Section\0",
323                          config_item_table_lookup, items,
324                          false, false, true, NULL);
325
326         switch (i) {
327         case 0 ... 3:
328                 assert_se(r == 0);
329                 assert_se(streq(setting1, "1"));
330                 break;
331
332         case 4:
333                 assert_se(r == 0);
334                 assert_se(streq(setting1, "1 2 3"));
335                 break;
336
337         case 5:
338                 assert_se(r == 0);
339                 assert_se(streq(setting1, "1\\\\ \\\\2"));
340                 break;
341
342         case 6:
343                 assert_se(r == 0);
344                 assert_se(streq(setting1, x1000("ABCD")));
345                 break;
346
347         case 7:
348                 assert_se(r == 0);
349                 assert_se(streq(setting1, x1000("ABCD") " foobar"));
350                 break;
351
352         case 8 ... 9:
353                 assert_se(r == -ENOBUFS);
354                 assert_se(setting1 == NULL);
355                 break;
356         }
357 }
358
359 int main(int argc, char **argv) {
360         unsigned i;
361
362         log_parse_environment();
363         log_open();
364
365         test_config_parse_path();
366         test_config_parse_log_level();
367 #if 0 /// UNNEEDED by elogind
368         test_config_parse_log_facility();
369 #endif // 0
370         test_config_parse_iec_size();
371 #if 0 /// UNNEEDED by elogind
372         test_config_parse_si_size();
373 #endif // 0
374         test_config_parse_int();
375         test_config_parse_unsigned();
376         test_config_parse_strv();
377         test_config_parse_mode();
378         test_config_parse_sec();
379 #if 0 /// UNNEEDED by elogind
380         test_config_parse_nsec();
381         test_config_parse_iec_uint64();
382 #endif // 0
383
384         for (i = 0; i < ELEMENTSOF(config_file); i++)
385                 test_config_parse(i, config_file[i]);
386
387         return 0;
388 }