Debian Codegraph @

ini/ini_validators_ut_check.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Unit test for the configuration file validators API.
5
6 Copyright (C) Michal Zidek <[email protected]> 2016
7
8 INI Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 INI Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <check.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32
33 /* #define TRACE_LEVEL 7 */
34 #define TRACE_HOME
35 #include "trace.h"
36 #include "ini_configobj.h"
37 #include "ini_config_priv.h"
38
39 #define TEST_DIR_PATH ""
40 #define TEST_RULES_FILE TEST_DIR_PATH"test_rules.ini"
41
42 static void create_rules_from_str(const char *rules,
43 struct ini_cfgobj **_rules_obj)
44 {
45 FILE *file;
46 size_t written;
47 int ret;
48
49 /* We want to test actual reading from file using
50 * ini_rules_read_from_file, so we create the file here */
51 file = fopen(TEST_RULES_FILE, "w");
52 fail_if(file == NULL, "fopen() failed: %s", strerror(errno));
53 written = fwrite(rules, 1, strlen(rules), file);
54 fail_unless(written == strlen(rules));
55
56 /* allow reading */
57 ret = chmod(TEST_RULES_FILE, 0664);
58 fail_unless(ret == 0, "chmod() failed: %s", strerror(errno));
59
60 fclose(file);
61
62 ret = ini_rules_read_from_file(TEST_RULES_FILE, _rules_obj);
63 fail_unless(ret == 0, "read_rules_from_file() failed: %s", strerror(ret));
64 }
65
66 static struct ini_cfgobj *get_ini_config_from_str(char input_data[],
67 size_t input_data_len)
68 {
69 struct ini_cfgobj *in_cfg;
70 struct ini_cfgfile *file_ctx;
71 int ret;
72
73 ret = ini_config_create(&in_cfg);
74 fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret);
75
76 ret = ini_config_file_from_mem(input_data, input_data_len, &file_ctx);
77 fail_unless(ret == EOK, "Failed to load config. Error %d.\n", ret);
78
79 ret = ini_config_parse(file_ctx, INI_STOP_ON_NONE, INI_MV1S_ALLOW, 0,
80 in_cfg);
81 fail_unless(ret == EOK, "Failed to parse config. Error %d.\n", ret);
82
83 ini_config_file_destroy(file_ctx);
84
85 return in_cfg;
86 }
87
88 START_TEST(test_ini_errobj)
89 {
90 struct ini_errobj *errobj;
91 int ret;
92 const char TEST_MSG1[] = "Test message one.";
93 const char TEST_MSG2[] = "Test message two.";
94 const char TEST_MSG3[] = "Test message three.";
95
96 ret = ini_errobj_create(NULL);
97 fail_unless(ret == EINVAL,
98 "ini_errobj_create(NULL) failed with wrong error [%s]",
99 strerror(ret));
100
101 ret = ini_errobj_create(&errobj);
102 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
103
104 /* We just created the errobj, it should be empty */
105 fail_unless(ini_errobj_no_more_msgs(errobj));
106
107 /* Now add three messages, after adding each message,
108 * check if the errobj has correct content. */
109 ret = ini_errobj_add_msg(errobj, TEST_MSG1);
110 fail_if(ret != 0, "ini_errobj_add_msg() failed: %s", strerror(ret));
111 fail_if(ini_errobj_no_more_msgs(errobj));
112 ret = strcmp(TEST_MSG1, ini_errobj_get_msg(errobj));
113 fail_if(ret != 0, "TEST_MSG1 was not found.");
114 ini_errobj_next(errobj);
115 fail_unless(ini_errobj_no_more_msgs(errobj));
116
117 ret = ini_errobj_add_msg(errobj, TEST_MSG2);
118 fail_if(ret != 0, "ini_errobj_add_msg() failed: %s", strerror(ret));
119 ini_errobj_reset(errobj); /* strart from first message */
120 fail_if(ini_errobj_no_more_msgs(errobj));
121 ret = strcmp(TEST_MSG1, ini_errobj_get_msg(errobj));
122 fail_if(ret != 0, "TEST_MSG1 was not found.");
123 ini_errobj_next(errobj);
124 fail_if(ini_errobj_no_more_msgs(errobj));
125 ret = strcmp(TEST_MSG2, ini_errobj_get_msg(errobj));
126 fail_if(ret != 0, "TEST_MSG2 was not found.");
127 ini_errobj_next(errobj);
128 fail_unless(ini_errobj_no_more_msgs(errobj));
129
130 ret = ini_errobj_add_msg(errobj, TEST_MSG3);
131 fail_if(ret != 0, "ini_errobj_add_msg() failed: %s", strerror(ret));
132 ini_errobj_reset(errobj); /* strart from first message */
133 fail_if(ini_errobj_no_more_msgs(errobj));
134 ret = strcmp(TEST_MSG1, ini_errobj_get_msg(errobj));
135 fail_if(ret != 0, "TEST_MSG1 was not found.");
136 ini_errobj_next(errobj);
137 fail_if(ini_errobj_no_more_msgs(errobj));
138 ret = strcmp(TEST_MSG2, ini_errobj_get_msg(errobj));
139 fail_if(ret != 0, "TEST_MSG2 was not found.");
140 ini_errobj_next(errobj);
141 fail_if(ini_errobj_no_more_msgs(errobj));
142 ret = strcmp(TEST_MSG3, ini_errobj_get_msg(errobj));
143 fail_if(ret != 0, "TEST_MSG3 was not found.");
144 ini_errobj_next(errobj);
145 fail_unless(ini_errobj_no_more_msgs(errobj));
146
147 ini_errobj_destroy(&errobj);
148 }
149 END_TEST
150
151 START_TEST(test_ini_noerror)
152 {
153 struct ini_cfgobj *rules_obj;
154 struct ini_cfgobj *cfg_obj;
155 struct ini_errobj *errobj;
156 int ret;
157
158 char input_rules[] =
159 "[rule/always_succeed]\n"
160 "validator = ini_dummy_noerror\n";
161
162 char input_cfg[] =
163 "[section]\n"
164 "# Content of this file should not matter\n";
165
166 create_rules_from_str(input_rules, &rules_obj);
167 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
168
169 ret = ini_errobj_create(&errobj);
170 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
171
172 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
173 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
174 fail_unless(ini_errobj_no_more_msgs(errobj));
175
176 ini_errobj_destroy(&errobj);
177 ini_config_destroy(cfg_obj);
178 ini_rules_destroy(rules_obj);
179 }
180 END_TEST
181
182 START_TEST(test_ini_error)
183 {
184 struct ini_cfgobj *rules_obj;
185 struct ini_cfgobj *cfg_obj;
186 struct ini_errobj *errobj;
187 int ret;
188 const char *errmsg;
189
190 char input_rules[] =
191 "[rule/generate_error]\n"
192 "validator = ini_dummy_error\n";
193
194 char input_wrong_rule[] =
195 "[rule/generate_error]\n"
196 "valid = ini_dummy_error\n";
197
198 char input_cfg[] =
199 "[section]\n"
200 "# Content of this file should not matter\n";
201
202 create_rules_from_str(input_rules, &rules_obj);
203 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
204
205 ret = ini_errobj_create(&errobj);
206 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
207
208 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
209 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
210
211 /* Should generate exactly one error */
212 fail_if(ini_errobj_no_more_msgs(errobj));
213 errmsg = ini_errobj_get_msg(errobj);
214 ret = strcmp(errmsg, "[rule/generate_error]: Error");
215 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
216 ini_errobj_next(errobj);
217 fail_unless(ini_errobj_no_more_msgs(errobj));
218
219 ini_errobj_destroy(&errobj);
220 ini_rules_destroy(rules_obj);
221
222 /* test rule with missing validator */
223 create_rules_from_str(input_wrong_rule, &rules_obj);
224
225 ret = ini_errobj_create(&errobj);
226 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
227
228 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
229 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
230
231 /* Should generate exactly one error */
232 fail_if(ini_errobj_no_more_msgs(errobj));
233 errmsg = ini_errobj_get_msg(errobj);
234 ret = strcmp(errmsg, "Rule 'rule/generate_error' has no validator.");
235 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
236 ini_errobj_next(errobj);
237 fail_unless(ini_errobj_no_more_msgs(errobj));
238
239 ini_errobj_destroy(&errobj);
240 ini_rules_destroy(rules_obj);
241 ini_config_destroy(cfg_obj);
242 }
243 END_TEST
244
245 START_TEST(test_unknown_validator)
246 {
247 struct ini_cfgobj *rules_obj;
248 struct ini_cfgobj *cfg_obj;
249 struct ini_errobj *errobj;
250 int ret;
251
252 char input_rules[] =
253 "[rule/always_succeed]\n"
254 "validator = nonexistent_validator\n";
255
256 char input_cfg[] =
257 "[section]\n"
258 "# Content of this file should not matter\n";
259
260 create_rules_from_str(input_rules, &rules_obj);
261 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
262
263 ret = ini_errobj_create(&errobj);
264 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
265
266 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
267 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
268
269 /* Should generate exactly one error */
270 fail_if(ini_errobj_no_more_msgs(errobj));
271 ini_errobj_next(errobj);
272 fail_unless(ini_errobj_no_more_msgs(errobj));
273
274 ini_errobj_destroy(&errobj);
275 ini_config_destroy(cfg_obj);
276 ini_rules_destroy(rules_obj);
277 }
278 END_TEST
279
280 static int custom_noerror(const char *rule_name,
281 struct ini_cfgobj *rules_obj,
282 struct ini_cfgobj *config_obj,
283 struct ini_errobj *errobj,
284 void **data)
285 {
286 return 0;
287 }
288
289 static int custom_error(const char *rule_name,
290 struct ini_cfgobj *rules_obj,
291 struct ini_cfgobj *config_obj,
292 struct ini_errobj *errobj,
293 void **data)
294 {
295 return ini_errobj_add_msg(errobj, "Error");
296 }
297
298 START_TEST(test_custom_noerror)
299 {
300 struct ini_cfgobj *rules_obj;
301 struct ini_cfgobj *cfg_obj;
302 struct ini_errobj *errobj;
303 int ret;
304 struct ini_validator *noerror[] = {
305 &(struct ini_validator){ "custom_noerror", custom_noerror, NULL },
306 NULL
307 };
308 struct ini_validator *missing_name[] = {
309 &(struct ini_validator){ NULL, custom_noerror, NULL },
310 &(struct ini_validator){ "custom_noerror", custom_noerror, NULL },
311 NULL
312 };
313
314 char input_rules[] =
315 "[rule/custom_succeed]\n"
316 "validator = custom_noerror\n";
317
318 char input_cfg[] =
319 "[section]\n"
320 "# Content of this file should not matter\n";
321
322 create_rules_from_str(input_rules, &rules_obj);
323 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
324
325 ret = ini_errobj_create(&errobj);
326 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
327
328 /* Pass the custom validator to ini_rules_check() */
329 ret = ini_rules_check(rules_obj, cfg_obj, noerror, errobj);
330 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
331
332 /* Should generate no errors */
333 fail_unless(ini_errobj_no_more_msgs(errobj));
334
335 /* Pass wrong external validator to ini_rules_check() */
336 /* It should be skipped */
337 ret = ini_rules_check(rules_obj, cfg_obj, missing_name, errobj);
338 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
339
340 /* Should generate no errors */
341 fail_unless(ini_errobj_no_more_msgs(errobj), "%s", ini_errobj_get_msg(errobj));
342
343 ini_errobj_destroy(&errobj);
344 ini_config_destroy(cfg_obj);
345 ini_rules_destroy(rules_obj);
346 }
347 END_TEST
348
349 START_TEST(test_custom_error)
350 {
351 struct ini_cfgobj *rules_obj;
352 struct ini_cfgobj *cfg_obj;
353 struct ini_errobj *errobj;
354 int ret;
355 struct ini_validator *error[] = {
356 &(struct ini_validator){ "custom_error", custom_error, NULL },
357 NULL
358 };
359 struct ini_validator *missing_function[] = {
360 &(struct ini_validator){ "custom_noerror", NULL, NULL },
361 NULL
362 };
363 const char *errmsg;
364
365 char input_rules[] =
366 "[rule/custom_error]\n"
367 "validator = custom_error\n";
368
369 char input_cfg[] =
370 "[section]\n"
371 "# Content of this file should not matter\n";
372
373 create_rules_from_str(input_rules, &rules_obj);
374 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
375
376 ret = ini_errobj_create(&errobj);
377 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
378
379 /* Pass the custom validator to ini_rules_check() */
380 ret = ini_rules_check(rules_obj, cfg_obj, error, errobj);
381 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
382
383 /* Should generate one error */
384 fail_if(ini_errobj_no_more_msgs(errobj));
385 errmsg = ini_errobj_get_msg(errobj);
386 ret = strcmp(errmsg, "[rule/custom_error]: Error");
387 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
388 ini_errobj_next(errobj);
389 fail_unless(ini_errobj_no_more_msgs(errobj));
390
391 ini_errobj_destroy(&errobj);
392
393 ret = ini_errobj_create(&errobj);
394 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
395
396 /* Pass the custom validator to ini_rules_check() */
397 ret = ini_rules_check(rules_obj, cfg_obj, missing_function, errobj);
398
399 /* Should generate one error for missing validator */
400 fail_if(ini_errobj_no_more_msgs(errobj));
401 errmsg = ini_errobj_get_msg(errobj);
402 ret = strcmp(errmsg,
403 "Rule 'rule/custom_error' uses unknown validator "
404 "'custom_error'.");
405 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
406 ini_errobj_next(errobj);
407 fail_unless(ini_errobj_no_more_msgs(errobj));
408
409 ini_errobj_destroy(&errobj);
410
411 ini_rules_destroy(rules_obj);
412 ini_config_destroy(cfg_obj);
413 }
414 END_TEST
415
416 START_TEST(test_ini_allowed_options_ok)
417 {
418 struct ini_cfgobj *rules_obj;
419 struct ini_cfgobj *cfg_obj;
420 struct ini_errobj *errobj;
421 int ret;
422
423 /* Only bar and baz are allowed for foo section */
424 char input_rules[] =
425 "[rule/options_for_foo]\n"
426 "validator = ini_allowed_options\n"
427 "section_re = ^foo$\n"
428 "option = bar\n"
429 "option = baz\n";
430
431 /* Should check only foo section, other sections are
432 * irrelevant and can contain any option */
433 char input_cfg[] =
434 "[foo]\n"
435 "bar = 0\n"
436 "baz = 0\n"
437 "[oof]\n"
438 "opt1 = 1\n";
439
440 create_rules_from_str(input_rules, &rules_obj);
441 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
442
443 ret = ini_errobj_create(&errobj);
444 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
445
446 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
447 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
448
449 /* Should generate no errors */
450 fail_unless(ini_errobj_no_more_msgs(errobj));
451
452 ini_errobj_destroy(&errobj);
453 ini_config_destroy(cfg_obj);
454 ini_rules_destroy(rules_obj);
455 }
456 END_TEST
457
458 START_TEST(test_ini_allowed_options_no_section)
459 {
460 struct ini_cfgobj *rules_obj;
461 struct ini_cfgobj *cfg_obj;
462 struct ini_errobj *errobj;
463 int ret;
464 size_t num_err;
465 const char *errmsg;
466
467 /* Ommit section_re to generate error */
468 char input_rules[] =
469 "[rule/options_for_foo]\n"
470 "validator = ini_allowed_options\n"
471 /* "section_re = ^foo$\n" */
472 "option = bar\n"
473 "option = baz\n";
474
475 /* section_re without value */
476 char input_rules2[] =
477 "[rule/options_for_foo]\n"
478 "validator = ini_allowed_options\n"
479 "section_re = \n"
480 "option = bar\n"
481 "option = baz\n";
482
483 /* Make 4 typos */
484 char input_cfg[] =
485 "[foo]\n"
486 "bar = 0\n"
487 "baz = 0\n";
488
489 create_rules_from_str(input_rules, &rules_obj);
490 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
491
492 ret = ini_errobj_create(&errobj);
493 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
494
495 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
496 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
497
498 /* Should generate 2 errors (one from rules_check and one
499 * from the validator itself) */
500 fail_if(ini_errobj_no_more_msgs(errobj));
501
502 num_err = ini_errobj_count(errobj);
503 fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);
504
505 errmsg = ini_errobj_get_msg(errobj);
506 ret = strcmp(errmsg,
507 "Rule 'rule/options_for_foo' returned error code '22'");
508 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
509 ini_errobj_next(errobj);
510
511 errmsg = ini_errobj_get_msg(errobj);
512 ret = strcmp(errmsg,
513 "[rule/options_for_foo]: Validator misses 'section_re' "
514 "parameter");
515 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
516 ini_errobj_next(errobj);
517 fail_unless(ini_errobj_no_more_msgs(errobj));
518
519 ini_errobj_destroy(&errobj);
520 ini_rules_destroy(rules_obj);
521
522 /* the second test with missing value for section_re */
523
524 create_rules_from_str(input_rules2, &rules_obj);
525
526 ret = ini_errobj_create(&errobj);
527 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
528
529 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
530 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
531
532 /* Should generate 2 errors (one from rules_check and one
533 * from the validator itself) */
534 fail_if(ini_errobj_no_more_msgs(errobj));
535
536 num_err = ini_errobj_count(errobj);
537 fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);
538
539 errmsg = ini_errobj_get_msg(errobj);
540 ret = strcmp(errmsg,
541 "Rule 'rule/options_for_foo' returned error code '22'");
542 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
543 ini_errobj_next(errobj);
544
545 errmsg = ini_errobj_get_msg(errobj);
546 ret = strcmp(errmsg,
547 "[rule/options_for_foo]: Validator misses 'section_re' "
548 "parameter");
549 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
550 ini_errobj_next(errobj);
551 fail_unless(ini_errobj_no_more_msgs(errobj));
552
553 ini_errobj_destroy(&errobj);
554 ini_rules_destroy(rules_obj);
555
556 ini_config_destroy(cfg_obj);
557 }
558 END_TEST
559
560 START_TEST(test_ini_allowed_options_wrong_regex)
561 {
562 struct ini_cfgobj *rules_obj;
563 struct ini_cfgobj *cfg_obj;
564 struct ini_errobj *errobj;
565 int ret;
566 size_t num_err;
567 const char *errmsg;
568
569 /* Ommit section_re to generate error */
570 char input_rules[] =
571 "[rule/options_for_foo]\n"
572 "validator = ini_allowed_options\n"
573 "section_re = ^foo[$\n"
574 "option = bar\n"
575 "option = baz\n";
576
577 /* Make 4 typos */
578 char input_cfg[] =
579 "[foo]\n"
580 "bar = 0\n"
581 "baz = 0\n";
582
583 create_rules_from_str(input_rules, &rules_obj);
584 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
585
586 ret = ini_errobj_create(&errobj);
587 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
588
589 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
590 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
591
592 /* Should generate 2 errors (one from rules_check and one
593 * from the validator itself) */
594 fail_if(ini_errobj_no_more_msgs(errobj));
595
596 num_err = ini_errobj_count(errobj);
597 fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);
598
599 errmsg = ini_errobj_get_msg(errobj);
600 ret = strcmp(errmsg,
601 "Rule 'rule/options_for_foo' returned error code '22'");
602 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
603 ini_errobj_next(errobj);
604
605 /* Different versions of libc produce slightly different error strings
606 * in this case. For simplicity compare against all of them. */
607 errmsg = ini_errobj_get_msg(errobj);
608 ret = strcmp(errmsg,
609 "[rule/options_for_foo]: Cannot compile regular expression "
610 "from option 'section_re'. "
611 "Error: 'Unmatched [ or [^'");
612 if (ret != 0) {
613 ret = strcmp(errmsg,
614 "[rule/options_for_foo]: Cannot compile regular "
615 "expression from option 'section_re'. "
616 "Error: 'brackets ([ ]) not balanced'");
617 }
618
619 if (ret != 0) {
620 ret = strcmp(errmsg,
621 "[rule/options_for_foo]: Cannot compile regular "
622 "expression from option 'section_re'. "
623 "Error: 'Unmatched [, [^, [:, [., or [='");
624 }
625 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
626 ini_errobj_next(errobj);
627
628 fail_unless(ini_errobj_no_more_msgs(errobj));
629
630 ini_errobj_destroy(&errobj);
631 ini_config_destroy(cfg_obj);
632 ini_rules_destroy(rules_obj);
633 }
634 END_TEST
635
636 START_TEST(test_ini_allowed_options_typos)
637 {
638 struct ini_cfgobj *rules_obj;
639 struct ini_cfgobj *cfg_obj;
640 struct ini_errobj *errobj;
641 int ret;
642 size_t num_err;
643
644 /* Only bar and baz are allowed for foo section */
645 char input_rules[] =
646 "[rule/options_for_foo]\n"
647 "validator = ini_allowed_options\n"
648 "section_re = ^foo$\n"
649 "option = bar\n"
650 "option = baz\n";
651
652 /* Make 4 typos */
653 char input_cfg[] =
654 "[foo]\n"
655 "br = 0\n"
656 "bra = 0\n"
657 "abr = 0\n"
658 "abz = 0\n";
659
660 create_rules_from_str(input_rules, &rules_obj);
661 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
662
663 ret = ini_errobj_create(&errobj);
664 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
665
666 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
667 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
668
669 /* Should generate 4 errors */
670 fail_if(ini_errobj_no_more_msgs(errobj));
671
672 num_err = ini_errobj_count(errobj);
673 fail_unless(num_err == 4, "Expected 4 errors, got %d", num_err);
674
675 ini_errobj_destroy(&errobj);
676 ini_config_destroy(cfg_obj);
677 ini_rules_destroy(rules_obj);
678 }
679 END_TEST
680
681 START_TEST(test_ini_allowed_sections_str_ok)
682 {
683 struct ini_cfgobj *rules_obj;
684 struct ini_cfgobj *cfg_obj;
685 struct ini_errobj *errobj;
686 int ret;
687
688 /* Only bar and baz are allowed for foo section */
689 char input_rules[] =
690 "[rule/section_list]\n"
691 "validator = ini_allowed_sections\n"
692 "section = foo\n"
693 "section = bar\n";
694
695 /* Make 4 typos */
696 char input_cfg[] =
697 "[foo]\n"
698 "br = 0\n"
699 "bra = 0\n"
700 "[bar]\n"
701 "abz = 0\n";
702
703 create_rules_from_str(input_rules, &rules_obj);
704 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
705
706 ret = ini_errobj_create(&errobj);
707 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
708
709 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
710 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
711
712 /* Should generate 0 errors */
713 fail_unless(ini_errobj_no_more_msgs(errobj),
714 "Unexpected errors found: [%s]", ini_errobj_get_msg(errobj));
715
716 ini_errobj_destroy(&errobj);
717 ini_config_destroy(cfg_obj);
718 ini_rules_destroy(rules_obj);
719 }
720 END_TEST
721
722 START_TEST(test_ini_allowed_sections_str_typos)
723 {
724 struct ini_cfgobj *rules_obj;
725 struct ini_cfgobj *cfg_obj;
726 struct ini_errobj *errobj;
727 int num_err;
728 int ret;
729
730 /* Only bar and baz are allowed for foo section */
731 char input_rules[] =
732 "[rule/section_list]\n"
733 "validator = ini_allowed_sections\n"
734 "section = foo\n"
735 "section = bar\n";
736
737 /* Make 4 typos */
738 char input_cfg[] =
739 "[fooo]\n"
740 "br = 0\n"
741 "bra = 0\n"
742 "[baar]\n"
743 "abz = 0\n";
744
745 create_rules_from_str(input_rules, &rules_obj);
746 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
747
748 ret = ini_errobj_create(&errobj);
749 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
750
751 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
752 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
753
754 /* Should generate 2 errors */
755 fail_if(ini_errobj_no_more_msgs(errobj),
756 "Expected 2 errors but none found");
757 num_err = ini_errobj_count(errobj);
758 fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);
759
760 ini_errobj_destroy(&errobj);
761 ini_config_destroy(cfg_obj);
762 ini_rules_destroy(rules_obj);
763 }
764 END_TEST
765
766 START_TEST(test_ini_allowed_sections_str_insensitive)
767 {
768 struct ini_cfgobj *rules_obj;
769 struct ini_cfgobj *cfg_obj;
770 struct ini_errobj *errobj;
771 int ret;
772 int i;
773
774 /* Only bar and baz are allowed for foo section */
775 char input_rules_template[] =
776 "[rule/section_list]\n"
777 "validator = ini_allowed_sections\n"
778 "case_insensitive = %s\n"
779 "section = foo\n"
780 "section = bar\n";
781
782 char input_rules[sizeof(input_rules_template) + 10];
783
784 const char *case_insensitive_values[] = { "yes", "Yes", "true", "True",
785 "1", NULL };
786 /* Make 4 typos */
787 char input_cfg[] =
788 "[FOo]\n"
789 "br = 0\n"
790 "bra = 0\n"
791 "[baR]\n"
792 "abz = 0\n";
793
794 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
795
796 for (i = 0; case_insensitive_values[i] != NULL; i++) {
797 sprintf(input_rules, input_rules_template, case_insensitive_values[i]);
798
799 create_rules_from_str(input_rules, &rules_obj);
800
801 ret = ini_errobj_create(&errobj);
802 fail_unless(ret == 0,
803 "ini_errobj_create() failed for case_insensitive = %s: %s",
804 case_insensitive_values[i], strerror(ret));
805
806 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
807 fail_unless(ret == 0,
808 "ini_rules_check() failed for case_insensitive = %s: %s",
809 case_insensitive_values[i], strerror(ret));
810
811 /* Should generate 0 errors */
812 fail_unless(ini_errobj_no_more_msgs(errobj),
813 "Unexpected errors found for case_insensitive = %s: [%s]",
814 case_insensitive_values[i], ini_errobj_get_msg(errobj));
815
816 ini_errobj_destroy(&errobj);
817 ini_rules_destroy(rules_obj);
818 }
819
820 ini_config_destroy(cfg_obj);
821 }
822 END_TEST
823
824 START_TEST(test_ini_allowed_sections_re_ok)
825 {
826 struct ini_cfgobj *rules_obj;
827 struct ini_cfgobj *cfg_obj;
828 struct ini_errobj *errobj;
829 int ret;
830
831 char input_rules[] =
832 "[rule/section_list]\n"
833 "validator = ini_allowed_sections\n"
834 "section_re = ^foo*$\n"
835 "section_re = bar\n";
836
837 char input_cfg[] =
838 "[foooooooooooo]\n"
839 "br = 0\n"
840 "bra = 0\n"
841 "[my_bar]\n"
842 "abz = 0\n";
843
844 create_rules_from_str(input_rules, &rules_obj);
845 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
846
847 ret = ini_errobj_create(&errobj);
848 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
849
850 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
851 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
852
853 /* Should generate 0 errors */
854 fail_unless(ini_errobj_no_more_msgs(errobj), "Unexpected errors found");
855
856 ini_errobj_destroy(&errobj);
857 ini_config_destroy(cfg_obj);
858 ini_rules_destroy(rules_obj);
859 }
860 END_TEST
861
862 START_TEST(test_ini_allowed_sections_re_typos)
863 {
864 struct ini_cfgobj *rules_obj;
865 struct ini_cfgobj *cfg_obj;
866 struct ini_errobj *errobj;
867 int num_err;
868 int ret;
869
870 char input_rules[] =
871 "[rule/section_list]\n"
872 "validator = ini_allowed_sections\n"
873 "section_re = ^foo*$\n"
874 "section_re = bar\n";
875
876 /* Make 4 typos */
877 char input_cfg[] =
878 "[fooooooOooooo]\n"
879 "br = 0\n"
880 "bra = 0\n"
881 "[my_bra]\n"
882 "abz = 0\n";
883
884 create_rules_from_str(input_rules, &rules_obj);
885 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
886
887 ret = ini_errobj_create(&errobj);
888 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
889
890 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
891 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
892
893 /* Should generate 2 errors */
894 fail_if(ini_errobj_no_more_msgs(errobj),
895 "Expected 2 errors but none found");
896 num_err = ini_errobj_count(errobj);
897 fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);
898
899 ini_errobj_destroy(&errobj);
900 ini_config_destroy(cfg_obj);
901 ini_rules_destroy(rules_obj);
902 }
903 END_TEST
904
905 START_TEST(test_ini_allowed_sections_re_insensitive)
906 {
907 struct ini_cfgobj *rules_obj;
908 struct ini_cfgobj *cfg_obj;
909 struct ini_errobj *errobj;
910 int ret;
911
912 /* Only bar and baz are allowed for foo section */
913 char input_rules[] =
914 "[rule/section_list]\n"
915 "validator = ini_allowed_sections\n"
916 "case_insensitive = yes\n"
917 "section_re = ^foo*$\n"
918 "section_re = bar\n";
919
920 /* Make 4 typos */
921 char input_cfg[] =
922 "[FOoOoOoOoOOOOooo]\n"
923 "br = 0\n"
924 "bra = 0\n"
925 "[my_Bar]\n"
926 "abz = 0\n";
927
928 create_rules_from_str(input_rules, &rules_obj);
929 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
930
931 ret = ini_errobj_create(&errobj);
932 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
933
934 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
935 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
936
937 /* Should generate 0 errors */
938 fail_unless(ini_errobj_no_more_msgs(errobj), "Unexpected errors found");
939
940 ini_errobj_destroy(&errobj);
941 ini_config_destroy(cfg_obj);
942 ini_rules_destroy(rules_obj);
943 }
944 END_TEST
945
946 START_TEST(test_ini_allowed_sections_missing_section)
947 {
948 struct ini_cfgobj *rules_obj;
949 struct ini_cfgobj *cfg_obj;
950 struct ini_errobj *errobj;
951 int num_err;
952 int ret;
953 const char *errmsg;
954
955 /* Only bar and baz are allowed for foo section */
956 char input_rules[] =
957 "[rule/section_list]\n"
958 "validator = ini_allowed_sections\n";
959
960 /* Make 4 typos */
961 char input_cfg[] =
962 "[fooo]\n"
963 "br = 0\n"
964 "bra = 0\n"
965 "[baar]\n"
966 "abz = 0\n";
967
968 create_rules_from_str(input_rules, &rules_obj);
969 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
970
971 ret = ini_errobj_create(&errobj);
972 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
973
974 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
975 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
976
977 /* Should generate 1 errors */
978 fail_if(ini_errobj_no_more_msgs(errobj),
979 "Expected 1 errors but none found");
980 num_err = ini_errobj_count(errobj);
981 fail_unless(num_err == 1, "Expected 1 errors, got %d", num_err);
982
983 errmsg = ini_errobj_get_msg(errobj);
984 ret = strcmp(errmsg,
985 "[rule/section_list]: No allowed sections specified. "
986 "Use 'section = default' to allow only default section");
987 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
988 ini_errobj_next(errobj);
989
990 fail_unless(ini_errobj_no_more_msgs(errobj));
991
992 ini_errobj_destroy(&errobj);
993 ini_config_destroy(cfg_obj);
994 ini_rules_destroy(rules_obj);
995 }
996 END_TEST
997
998 START_TEST(test_ini_allowed_sections_wrong_regex)
999 {
1000 struct ini_cfgobj *rules_obj;
1001 struct ini_cfgobj *cfg_obj;
1002 struct ini_errobj *errobj;
1003 int num_err;
1004 int ret;
1005 const char *errmsg;
1006
1007 /* Only bar and baz are allowed for foo section */
1008 char input_rules[] =
1009 "[rule/section_list]\n"
1010 "validator = ini_allowed_sections\n"
1011 "section_re = ^foo\\(*$\n";
1012
1013 /* Make 4 typos */
1014 char input_cfg[] =
1015 "[fooo]\n"
1016 "br = 0\n"
1017 "bra = 0\n"
1018 "[baar]\n"
1019 "abz = 0\n";
1020
1021 create_rules_from_str(input_rules, &rules_obj);
1022 cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));
1023
1024 ret = ini_errobj_create(&errobj);
1025 fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));
1026
1027 ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
1028 fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
1029
1030 /* Should generate 2 errors */
1031 fail_if(ini_errobj_no_more_msgs(errobj),
1032 "Expected 2 errors but none found");
1033 num_err = ini_errobj_count(errobj);
1034 fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);
1035
1036 errmsg = ini_errobj_get_msg(errobj);
1037 ret = strcmp(errmsg,
1038 "Rule 'rule/section_list' returned error code '22'");
1039 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
1040 ini_errobj_next(errobj);
1041
1042 errmsg = ini_errobj_get_msg(errobj);
1043 ret = strcmp(errmsg,
1044 "[rule/section_list]: Validator failed to use regex "
1045 "[^foo\\(*$]:[Unmatched ( or \\(]");
1046 if (ret !=0) {
1047 ret = strcmp(errmsg,
1048 "[rule/section_list]: Validator failed to use regex "
1049 "[^foo\\(*$]:[parentheses not balanced]");
1050 }
1051 fail_unless(ret == 0, "Got msg: [%s]", errmsg);
1052 ini_errobj_next(errobj);
1053
1054 fail_unless(ini_errobj_no_more_msgs(errobj));
1055
1056 ini_errobj_destroy(&errobj);
1057 ini_config_destroy(cfg_obj);
1058 ini_rules_destroy(rules_obj);
1059 }
1060 END_TEST
1061
1062 static Suite *ini_validators_utils_suite(void)
1063 {
1064 Suite *s = suite_create("ini_validators");
1065
1066 TCase *tc_infrastructure = tcase_create("infrastructure");
1067 tcase_add_test(tc_infrastructure, test_ini_errobj);
1068 tcase_add_test(tc_infrastructure, test_ini_noerror);
1069 tcase_add_test(tc_infrastructure, test_ini_error);
1070 tcase_add_test(tc_infrastructure, test_unknown_validator);
1071 tcase_add_test(tc_infrastructure, test_custom_noerror);
1072 tcase_add_test(tc_infrastructure, test_custom_error);
1073
1074 TCase *tc_allowed_options = tcase_create("ini_allowed_options");
1075 tcase_add_test(tc_allowed_options, test_ini_allowed_options_ok);
1076 tcase_add_test(tc_allowed_options, test_ini_allowed_options_no_section);
1077 tcase_add_test(tc_allowed_options, test_ini_allowed_options_wrong_regex);
1078 tcase_add_test(tc_allowed_options, test_ini_allowed_options_typos);
1079
1080 TCase *tc_allowed_sections = tcase_create("ini_allowed_sections");
1081 tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_str_ok);
1082 tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_str_typos);
1083 tcase_add_test(tc_allowed_sections,
1084 test_ini_allowed_sections_str_insensitive);
1085 tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_re_ok);
1086 tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_re_typos);
1087 tcase_add_test(tc_allowed_sections,
1088 test_ini_allowed_sections_re_insensitive);
1089 tcase_add_test(tc_allowed_sections,
1090 test_ini_allowed_sections_missing_section);
1091 tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_wrong_regex);
1092
1093 suite_add_tcase(s, tc_infrastructure);
1094 suite_add_tcase(s, tc_allowed_options);
1095 suite_add_tcase(s, tc_allowed_sections);
1096
1097 return s;
1098 }
1099
1100 int main(void)
1101 {
1102 int number_failed;
1103
1104 Suite *s = ini_validators_utils_suite();
1105 SRunner *sr = srunner_create(s);
1106 /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
1107 srunner_run_all(sr, CK_ENV);
1108 number_failed = srunner_ntests_failed(sr);
1109 srunner_free(sr);
1110 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1111 }