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