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