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