kpartx: fix bad dm_devn return
[multipath-tools/.git] / tests / parser.c
1 /*
2  * Copyright (c) 2018 SUSE Linux GmbH
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <stdbool.h>
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <setjmp.h>
23 #include <stdlib.h>
24 #include <cmocka.h>
25 // #include "list.h"
26 #include "parser.h"
27 #include "vector.h"
28
29 #include "globals.c"
30
31 /* Set these to 1 to get success for current broken behavior */
32 /* Strip leading whitespace between quotes */
33 #define LSTRIP_QUOTED_WSP 0
34 /* Stop parsing at 2nd quote */
35 #define TWO_QUOTES_ONLY 0
36
37 static char *test_file = "test.conf";
38
39 /* Missing declaration */
40 int validate_config_strvec(vector strvec, char *file);
41
42 /* Stringify helpers */
43 #define _str_(x) #x
44 #define str(x) _str_(x)
45
46 static int setup(void **state)
47 {
48         return 0;
49 }
50
51 static int teardown(void **state)
52 {
53         return 0;
54 }
55
56 static void test01(void **state)
57 {
58         vector v = alloc_strvec("keyword value");
59         char *val;
60
61         assert_int_equal(validate_config_strvec(v, test_file), 0);
62         assert_int_equal(VECTOR_SIZE(v), 2);
63         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
64         assert_string_equal(VECTOR_SLOT(v, 1), "value");
65
66         val = set_value(v);
67         assert_string_equal(val, "value");
68
69         free(val);
70         free_strvec(v);
71 }
72
73 static void test02(void **state)
74 {
75         vector v = alloc_strvec("keyword \"value\"");
76         char *val;
77
78         assert_int_equal(validate_config_strvec(v, test_file), 0);
79         assert_int_equal(VECTOR_SIZE(v), 4);
80         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
81         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
82         assert_string_equal(VECTOR_SLOT(v, 2), "value");
83         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
84
85         val = set_value(v);
86         assert_string_equal(val, "value");
87
88         free(val);
89         free_strvec(v);
90 }
91
92 static void test03(void **state)
93 {
94         vector v = alloc_strvec("keyword value\n");
95         char *val;
96
97         assert_int_equal(validate_config_strvec(v, test_file), 0);
98         assert_int_equal(VECTOR_SIZE(v), 2);
99         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
100         assert_string_equal(VECTOR_SLOT(v, 1), "value");
101
102         val = set_value(v);
103         assert_string_equal(val, "value");
104
105         free(val);
106         free_strvec(v);
107 }
108
109 static void test04(void **state)
110 {
111         vector v = alloc_strvec("keyword \t   value   \t \n   ");
112         char *val;
113
114         assert_int_equal(validate_config_strvec(v, test_file), 0);
115         assert_int_equal(VECTOR_SIZE(v), 2);
116         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
117         assert_string_equal(VECTOR_SLOT(v, 1), "value");
118
119         val = set_value(v);
120         assert_string_equal(val, "value");
121
122         free(val);
123         free_strvec(v);
124 }
125
126 static void test05(void **state)
127 {
128         vector v = alloc_strvec("keyword \t   value   \t ! comment  ");
129         char *val;
130
131         assert_int_equal(validate_config_strvec(v, test_file), 0);
132         assert_int_equal(VECTOR_SIZE(v), 2);
133         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
134         assert_string_equal(VECTOR_SLOT(v, 1), "value");
135
136         val = set_value(v);
137         assert_string_equal(val, "value");
138
139         free(val);
140         free_strvec(v);
141 }
142
143 static void test06(void **state)
144 {
145         vector v = alloc_strvec("keyword \t   value   # \n comment  ");
146         char *val;
147
148         assert_int_equal(validate_config_strvec(v, test_file), 0);
149         assert_int_equal(VECTOR_SIZE(v), 2);
150         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
151         assert_string_equal(VECTOR_SLOT(v, 1), "value");
152
153         val = set_value(v);
154         assert_string_equal(val, "value");
155
156         free(val);
157         free_strvec(v);
158 }
159
160 static void test07(void **state)
161 {
162         vector v = alloc_strvec("keyword \t   value   more  ");
163         char *val;
164
165         assert_int_equal(validate_config_strvec(v, test_file), 0);
166         assert_int_equal(VECTOR_SIZE(v), 3);
167         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
168         assert_string_equal(VECTOR_SLOT(v, 1), "value");
169         assert_string_equal(VECTOR_SLOT(v, 2), "more");
170
171         val = set_value(v);
172         assert_string_equal(val, "value");
173
174         free(val);
175         free_strvec(v);
176 }
177
178 static void test08(void **state)
179 {
180 #define QUOTED08 "  value   more  "
181 #define QUOTED08B "value   more  "
182         vector v = alloc_strvec("keyword \t \"" QUOTED08 "\"");
183         char *val;
184
185         assert_int_equal(validate_config_strvec(v, test_file), 0);
186         assert_int_equal(VECTOR_SIZE(v), 4);
187         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
188         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
189 #if LSTRIP_QUOTED_WSP
190         assert_string_equal(VECTOR_SLOT(v, 2), QUOTED08B);
191 #else
192         assert_string_equal(VECTOR_SLOT(v, 2), QUOTED08);
193 #endif
194         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
195
196         val = set_value(v);
197 #if LSTRIP_QUOTED_WSP
198         assert_string_equal(val, QUOTED08B);
199 #else
200         assert_string_equal(val, QUOTED08);
201 #endif
202         free(val);
203         free_strvec(v);
204 }
205
206 static void test09(void **state)
207 {
208 #define QUOTED09 "value # more"
209         vector v = alloc_strvec("keyword \"" QUOTED09 "\"");
210         char *val;
211
212         assert_int_equal(validate_config_strvec(v, test_file), 0);
213         assert_int_equal(VECTOR_SIZE(v), 4);
214         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
215         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
216         assert_string_equal(VECTOR_SLOT(v, 2), QUOTED09);
217         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
218
219         val = set_value(v);
220         assert_string_equal(val, QUOTED09);
221
222         free(val);
223         free_strvec(v);
224 }
225
226 static void test10(void **state)
227 {
228 #define QUOTED10 "value ! more"
229         vector v = alloc_strvec("keyword \"" QUOTED10 "\"");
230         char *val;
231
232         assert_int_equal(validate_config_strvec(v, test_file), 0);
233         assert_int_equal(VECTOR_SIZE(v), 4);
234         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
235         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
236         assert_string_equal(VECTOR_SLOT(v, 2), QUOTED10);
237         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
238
239         val = set_value(v);
240         assert_string_equal(val, QUOTED10);
241
242         free(val);
243         free_strvec(v);
244 }
245
246 static void test11(void **state)
247 {
248 #define QUOTED11 "value comment"
249         vector v = alloc_strvec("keyword\"" QUOTED11 "\"");
250         char *val;
251
252         assert_int_equal(validate_config_strvec(v, test_file), 0);
253         assert_int_equal(VECTOR_SIZE(v), 4);
254         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
255         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
256         assert_string_equal(VECTOR_SLOT(v, 2), QUOTED11);
257         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
258
259         val = set_value(v);
260         assert_string_equal(val, QUOTED11);
261
262         free(val);
263         free_strvec(v);
264 }
265
266 static void test12(void **state)
267 {
268         vector v = alloc_strvec("key\"word\"");
269         char *val;
270
271         assert_int_equal(validate_config_strvec(v, test_file), 0);
272         assert_int_equal(VECTOR_SIZE(v), 4);
273         assert_string_equal(VECTOR_SLOT(v, 0), "key");
274         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
275         assert_string_equal(VECTOR_SLOT(v, 2), "word");
276         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
277
278         val = set_value(v);
279         assert_string_equal(val, "word");
280
281         free(val);
282         free_strvec(v);
283 }
284
285 static void test13(void **state)
286 {
287         vector v = alloc_strvec("keyword value \"quoted\"");
288         char *val;
289
290         assert_int_equal(validate_config_strvec(v, test_file), 0);
291         assert_int_equal(VECTOR_SIZE(v), 5);
292         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
293         assert_string_equal(VECTOR_SLOT(v, 1), "value");
294         assert_true(is_quote(VECTOR_SLOT(v, 2)));;
295         assert_string_equal(VECTOR_SLOT(v, 3), "quoted");
296         assert_true(is_quote(VECTOR_SLOT(v, 4)));;
297
298         val = set_value(v);
299         assert_string_equal(val, "value");
300
301         free(val);
302         free_strvec(v);
303 }
304
305 static void test14(void **state)
306 {
307         vector v = alloc_strvec("keyword \"value \"  comment\"\"");
308         char *val;
309
310         assert_int_equal(validate_config_strvec(v, test_file), 0);
311         assert_int_equal(VECTOR_SIZE(v), 7);
312         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
313         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
314         assert_string_equal(VECTOR_SLOT(v, 2), "value ");
315         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
316         assert_string_equal(VECTOR_SLOT(v, 4), "comment");
317         assert_true(is_quote(VECTOR_SLOT(v, 5)));;
318         assert_true(is_quote(VECTOR_SLOT(v, 6)));;
319
320         val = set_value(v);
321         assert_string_equal(val, "value ");
322
323         free(val);
324         free_strvec(v);
325 }
326
327 static void test15(void **state)
328 {
329 #define QUOTED15 "word  value\n  comment"
330         vector v = alloc_strvec("key\"" QUOTED15 "\"");
331         char *val;
332
333         assert_int_equal(VECTOR_SIZE(v), 4);
334         assert_string_equal(VECTOR_SLOT(v, 0), "key");
335         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
336         assert_string_equal(VECTOR_SLOT(v, 2), QUOTED15);
337         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
338         assert_int_equal(validate_config_strvec(v, test_file), 0);
339
340         val = set_value(v);
341         assert_string_equal(val, QUOTED15);
342
343         free(val);
344         free_strvec(v);
345 }
346
347 static void test16(void **state)
348 {
349         vector v = alloc_strvec("keyword \"2.5\"\" SSD\"");
350         char *val;
351
352 #if TWO_QUOTES_ONLY
353         assert_int_equal(validate_config_strvec(v, test_file), 0);
354         assert_int_equal(VECTOR_SIZE(v), 6);
355         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
356         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
357         assert_string_equal(VECTOR_SLOT(v, 2), "2.5");
358         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
359         assert_string_equal(VECTOR_SLOT(v, 4), "SSD");
360         assert_true(is_quote(VECTOR_SLOT(v, 5)));;
361
362         val = set_value(v);
363         assert_string_equal(val, "2.5");
364 #else
365         assert_int_equal(VECTOR_SIZE(v), 4);
366         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
367         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
368         assert_string_equal(VECTOR_SLOT(v, 2), "2.5\" SSD");
369         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
370
371         val = set_value(v);
372         assert_string_equal(val, "2.5\" SSD");
373 #endif
374         free(val);
375         free_strvec(v);
376 }
377
378 static void test17(void **state)
379 {
380         vector v = alloc_strvec("keyword \"\"\"\"\" is empty\"");
381         char *val;
382 #if TWO_QUOTES_ONLY
383         assert_int_equal(validate_config_strvec(v, test_file), 0);
384         assert_int_equal(VECTOR_SIZE(v), 6);
385         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
386         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
387         assert_true(is_quote(VECTOR_SLOT(v, 2)));;
388         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
389 #if LSTRIP_QUOTED_WSP
390         assert_string_equal(VECTOR_SLOT(v, 4), "is empty");
391 #else
392         assert_string_equal(VECTOR_SLOT(v, 4), " is empty");
393 #endif
394         assert_true(is_quote(VECTOR_SLOT(v, 5)));;
395
396         val = set_value(v);
397         assert_string_equal(val, "");
398 #else
399         assert_int_equal(validate_config_strvec(v, test_file), 0);
400         assert_int_equal(VECTOR_SIZE(v), 4);
401         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
402         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
403         assert_string_equal(VECTOR_SLOT(v, 2), "\"\" is empty");
404         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
405
406         val = set_value(v);
407         assert_string_equal(val, "\"\" is empty");
408 #endif
409         free(val);
410         free_strvec(v);
411 }
412
413 static void test18(void **state)
414 {
415         vector v = alloc_strvec("keyword \"\"\"\"");
416         char *val;
417 #if TWO_QUOTES_ONLY
418         assert_int_equal(validate_config_strvec(v, test_file), 0);
419         assert_int_equal(VECTOR_SIZE(v), 5);
420         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
421         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
422         assert_true(is_quote(VECTOR_SLOT(v, 2)));;
423         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
424         assert_true(is_quote(VECTOR_SLOT(v, 4)));;
425
426         val = set_value(v);
427         assert_string_equal(val, "");
428 #else
429         assert_int_equal(validate_config_strvec(v, test_file), 0);
430         assert_int_equal(VECTOR_SIZE(v), 4);
431         assert_string_equal(VECTOR_SLOT(v, 0), "keyword");
432         assert_true(is_quote(VECTOR_SLOT(v, 1)));;
433         assert_string_equal(VECTOR_SLOT(v, 2), "\"");
434         assert_true(is_quote(VECTOR_SLOT(v, 3)));;
435
436         val = set_value(v);
437         assert_string_equal(val, "\"");
438 #endif
439         free(val);
440         free_strvec(v);
441 }
442
443 int test_config_parser(void)
444 {
445         const struct CMUnitTest tests[] = {
446                 cmocka_unit_test(test01),
447                 cmocka_unit_test(test02),
448                 cmocka_unit_test(test03),
449                 cmocka_unit_test(test04),
450                 cmocka_unit_test(test05),
451                 cmocka_unit_test(test06),
452                 cmocka_unit_test(test07),
453                 cmocka_unit_test(test08),
454                 cmocka_unit_test(test09),
455                 cmocka_unit_test(test10),
456                 cmocka_unit_test(test11),
457                 cmocka_unit_test(test12),
458                 cmocka_unit_test(test13),
459                 cmocka_unit_test(test14),
460                 cmocka_unit_test(test15),
461                 cmocka_unit_test(test16),
462                 cmocka_unit_test(test17),
463                 cmocka_unit_test(test18),
464         };
465         return cmocka_run_group_tests(tests, setup, teardown);
466 }
467
468 int main(void)
469 {
470         int ret = 0;
471
472         ret += test_config_parser();
473         return ret;
474 }