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