chiark / gitweb /
test-conf-parser: use _cleanup_
[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 static const char* const config_file[] = {
239         "[Section]\n"
240         "setting1=1\n",
241
242         "[Section]\n"
243         "setting1=1",        /* no terminating newline */
244
245         "\n\n\n\n[Section]\n\n\n"
246         "setting1=1",        /* some whitespace, no terminating newline */
247
248         "[Section]\n"
249         "[Section]\n"
250         "setting1=1\n"
251         "setting1=2\n"
252         "setting1=1\n",      /* repeated settings */
253
254         "[Section]\n"
255         "setting1=1\\\n"     /* normal continuation */
256         "2\\\n"
257         "3\n",
258
259         "[Section]\n"
260         "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
261         "\\\\2\n",           /* note that C requires one level of escaping, so the
262                               * parser gets "…1 BS BS BS NL BS BS 2 NL", which
263                               * it translates into "…1 BS BS SP BS BS 2" */
264 };
265
266 static void test_config_parse(unsigned i, const char *s) {
267         char name[] = "/tmp/test-conf-parser.XXXXXX";
268         int fd, r;
269         _cleanup_fclose_ FILE *f = NULL;
270         _cleanup_free_ char *setting1 = NULL;
271
272         const ConfigTableItem items[] = {
273                 { "Section", "setting1",  config_parse_string,   0, &setting1},
274                 {}
275         };
276
277         log_info("== %s[%i] ==", __func__, i);
278
279         fd = mkostemp_safe(name);
280         assert_se(fd >= 0);
281         assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
282
283         assert_se(lseek(fd, 0, SEEK_SET) == 0);
284         assert_se(f = fdopen(fd, "r"));
285
286         /*
287         int config_parse(const char *unit,
288                          const char *filename,
289                          FILE *f,
290                          const char *sections,
291                          ConfigItemLookup lookup,
292                          const void *table,
293                          bool relaxed,
294                          bool allow_include,
295                          bool warn,
296                          void *userdata)
297         */
298
299         r = config_parse(NULL, name, f,
300                          "Section\0",
301                          config_item_table_lookup, items,
302                          false, false, true, NULL);
303         assert_se(r == 0);
304
305         switch (i) {
306         case 0 ... 3:
307                 assert_se(streq(setting1, "1"));
308                 break;
309
310         case 4:
311                 assert_se(streq(setting1, "1 2 3"));
312                 break;
313
314         case 5:
315                 assert_se(streq(setting1, "1\\\\ \\\\2"));
316                 break;
317         }
318 }
319
320 int main(int argc, char **argv) {
321         unsigned i;
322
323         log_parse_environment();
324         log_open();
325
326         test_config_parse_path();
327         test_config_parse_log_level();
328 #if 0 /// UNNEEDED by elogind
329         test_config_parse_log_facility();
330 #endif // 0
331         test_config_parse_iec_size();
332 #if 0 /// UNNEEDED by elogind
333         test_config_parse_si_size();
334 #endif // 0
335         test_config_parse_int();
336         test_config_parse_unsigned();
337         test_config_parse_strv();
338         test_config_parse_mode();
339         test_config_parse_sec();
340 #if 0 /// UNNEEDED by elogind
341         test_config_parse_nsec();
342         test_config_parse_iec_uint64();
343 #endif // 0
344
345         for (i = 0; i < ELEMENTSOF(config_file); i++)
346                 test_config_parse(i, config_file[i]);
347
348         return 0;
349 }