File: ini_validators_ut_check.c

package info (click to toggle)
ding-libs 0.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,092 kB
  • sloc: ansic: 31,844; makefile: 382; sh: 40
file content (1111 lines) | stat: -rw-r--r-- 35,206 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
/*
    INI LIBRARY

    Unit test for the configuration file validators API.

    Copyright (C) Michal Zidek <mzidek@redhat.com> 2016

    INI Library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    INI Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with INI Library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <check.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/* #define TRACE_LEVEL 7 */
#define TRACE_HOME
#include "trace.h"
#include "ini_configobj.h"
#include "ini_config_priv.h"

#define TEST_DIR_PATH ""
#define TEST_RULES_FILE TEST_DIR_PATH"test_rules.ini"

static void create_rules_from_str(const char *rules,
                                  struct ini_cfgobj **_rules_obj)
{
    FILE *file;
    size_t written;
    int ret;

    /* We want to test actual reading from file using
     * ini_rules_read_from_file, so we create the file here */
    file = fopen(TEST_RULES_FILE, "w");
    fail_if(file == NULL, "fopen() failed: %s", strerror(errno));
    written = fwrite(rules, 1, strlen(rules), file);
    fail_unless(written == strlen(rules));

    /* allow reading */
    ret = chmod(TEST_RULES_FILE, 0664);
    fail_unless(ret == 0, "chmod() failed: %s", strerror(errno));

    fclose(file);

    ret = ini_rules_read_from_file(TEST_RULES_FILE, _rules_obj);
    fail_unless(ret == 0, "read_rules_from_file() failed: %s", strerror(ret));
}

static struct ini_cfgobj *get_ini_config_from_str(char input_data[],
                                                  size_t input_data_len)
{
    struct ini_cfgobj *in_cfg;
    struct ini_cfgfile *file_ctx;
    int ret;

    ret = ini_config_create(&in_cfg);
    fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret);

    ret = ini_config_file_from_mem(input_data, input_data_len, &file_ctx);
    fail_unless(ret == EOK, "Failed to load config. Error %d.\n", ret);

    ret = ini_config_parse(file_ctx, INI_STOP_ON_NONE, INI_MV1S_ALLOW, 0,
                           in_cfg);
    fail_unless(ret == EOK, "Failed to parse config. Error %d.\n", ret);

    ini_config_file_destroy(file_ctx);

    return in_cfg;
}

START_TEST(test_ini_errobj)
{
    struct ini_errobj *errobj;
    int ret;
    const char TEST_MSG1[] = "Test message one.";
    const char TEST_MSG2[] = "Test message two.";
    const char TEST_MSG3[] = "Test message three.";

    ret = ini_errobj_create(NULL);
    fail_unless(ret == EINVAL,
                "ini_errobj_create(NULL) failed with wrong error [%s]",
                strerror(ret));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    /* We just created the errobj, it should be empty */
    fail_unless(ini_errobj_no_more_msgs(errobj));

    /* Now add three messages, after adding each message,
     * check if the errobj has correct content. */
    ret = ini_errobj_add_msg(errobj, TEST_MSG1);
    fail_if(ret != 0, "ini_errobj_add_msg() failed: %s", strerror(ret));
    fail_if(ini_errobj_no_more_msgs(errobj));
    ret = strcmp(TEST_MSG1, ini_errobj_get_msg(errobj));
    fail_if(ret != 0, "TEST_MSG1 was not found.");
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ret = ini_errobj_add_msg(errobj, TEST_MSG2);
    fail_if(ret != 0, "ini_errobj_add_msg() failed: %s", strerror(ret));
    ini_errobj_reset(errobj); /* strart from first message */
    fail_if(ini_errobj_no_more_msgs(errobj));
    ret = strcmp(TEST_MSG1, ini_errobj_get_msg(errobj));
    fail_if(ret != 0, "TEST_MSG1 was not found.");
    ini_errobj_next(errobj);
    fail_if(ini_errobj_no_more_msgs(errobj));
    ret = strcmp(TEST_MSG2, ini_errobj_get_msg(errobj));
    fail_if(ret != 0, "TEST_MSG2 was not found.");
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ret = ini_errobj_add_msg(errobj, TEST_MSG3);
    fail_if(ret != 0, "ini_errobj_add_msg() failed: %s", strerror(ret));
    ini_errobj_reset(errobj); /* strart from first message */
    fail_if(ini_errobj_no_more_msgs(errobj));
    ret = strcmp(TEST_MSG1, ini_errobj_get_msg(errobj));
    fail_if(ret != 0, "TEST_MSG1 was not found.");
    ini_errobj_next(errobj);
    fail_if(ini_errobj_no_more_msgs(errobj));
    ret = strcmp(TEST_MSG2, ini_errobj_get_msg(errobj));
    fail_if(ret != 0, "TEST_MSG2 was not found.");
    ini_errobj_next(errobj);
    fail_if(ini_errobj_no_more_msgs(errobj));
    ret = strcmp(TEST_MSG3, ini_errobj_get_msg(errobj));
    fail_if(ret != 0, "TEST_MSG3 was not found.");
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
}
END_TEST

START_TEST(test_ini_noerror)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;

    char input_rules[] =
        "[rule/always_succeed]\n"
        "validator = ini_dummy_noerror\n";

    char input_cfg[] =
        "[section]\n"
        "# Content of this file should not matter\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_error)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;
    const char *errmsg;

    char input_rules[] =
        "[rule/generate_error]\n"
        "validator = ini_dummy_error\n";

    char input_wrong_rule[] =
        "[rule/generate_error]\n"
        "valid = ini_dummy_error\n";

    char input_cfg[] =
        "[section]\n"
        "# Content of this file should not matter\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate exactly one error */
    fail_if(ini_errobj_no_more_msgs(errobj));
    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg, "[rule/generate_error]: Error");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_rules_destroy(rules_obj);

    /* test rule with missing validator */
    create_rules_from_str(input_wrong_rule, &rules_obj);

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate exactly one error */
    fail_if(ini_errobj_no_more_msgs(errobj));
    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg, "Rule 'rule/generate_error' has no validator.");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_rules_destroy(rules_obj);
    ini_config_destroy(cfg_obj);
}
END_TEST

START_TEST(test_unknown_validator)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;

    char input_rules[] =
        "[rule/always_succeed]\n"
        "validator = nonexistent_validator\n";

    char input_cfg[] =
        "[section]\n"
        "# Content of this file should not matter\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate exactly one error */
    fail_if(ini_errobj_no_more_msgs(errobj));
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

static int custom_noerror(const char *rule_name,
                          struct ini_cfgobj *rules_obj,
                          struct ini_cfgobj *config_obj,
                          struct ini_errobj *errobj,
                          void **data)
{
    return 0;
}

static int custom_error(const char *rule_name,
                        struct ini_cfgobj *rules_obj,
                        struct ini_cfgobj *config_obj,
                        struct ini_errobj *errobj,
                        void **data)
{
    return ini_errobj_add_msg(errobj, "Error");
}

START_TEST(test_custom_noerror)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;
    struct ini_validator *noerror[] = {
        &(struct ini_validator){ "custom_noerror", custom_noerror, NULL },
        NULL
    };
    struct ini_validator *missing_name[] = {
        &(struct ini_validator){ NULL, custom_noerror, NULL },
        &(struct ini_validator){ "custom_noerror", custom_noerror, NULL },
        NULL
    };

    char input_rules[] =
        "[rule/custom_succeed]\n"
        "validator = custom_noerror\n";

    char input_cfg[] =
        "[section]\n"
        "# Content of this file should not matter\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    /* Pass the custom validator to ini_rules_check() */
    ret = ini_rules_check(rules_obj, cfg_obj, noerror, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate no errors */
    fail_unless(ini_errobj_no_more_msgs(errobj));

    /* Pass wrong external validator to ini_rules_check() */
    /* It should be skipped */
    ret = ini_rules_check(rules_obj, cfg_obj, missing_name, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate no errors */
    fail_unless(ini_errobj_no_more_msgs(errobj), "%s", ini_errobj_get_msg(errobj));

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_custom_error)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;
    struct ini_validator *error[] = {
        &(struct ini_validator){ "custom_error", custom_error, NULL },
        NULL
    };
    struct ini_validator *missing_function[] = {
        &(struct ini_validator){ "custom_noerror", NULL, NULL },
        NULL
    };
    const char *errmsg;

    char input_rules[] =
        "[rule/custom_error]\n"
        "validator = custom_error\n";

    char input_cfg[] =
        "[section]\n"
        "# Content of this file should not matter\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    /* Pass the custom validator to ini_rules_check() */
    ret = ini_rules_check(rules_obj, cfg_obj, error, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate one error */
    fail_if(ini_errobj_no_more_msgs(errobj));
    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg, "[rule/custom_error]: Error");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    /* Pass the custom validator to ini_rules_check() */
    ret = ini_rules_check(rules_obj, cfg_obj, missing_function, errobj);

    /* Should generate one error for missing validator */
    fail_if(ini_errobj_no_more_msgs(errobj));
    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "Rule 'rule/custom_error' uses unknown validator "
                 "'custom_error'.");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);

    ini_rules_destroy(rules_obj);
    ini_config_destroy(cfg_obj);
}
END_TEST

START_TEST(test_ini_allowed_options_ok)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;

    /* Only bar and baz are allowed for foo section */
    char input_rules[] =
        "[rule/options_for_foo]\n"
        "validator = ini_allowed_options\n"
        "section_re = ^foo$\n"
        "option = bar\n"
        "option = baz\n";

    /* Should check only foo section, other sections are
     * irrelevant and can contain any option */
    char input_cfg[] =
        "[foo]\n"
        "bar = 0\n"
        "baz = 0\n"
        "[oof]\n"
        "opt1 = 1\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate no errors */
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_options_no_section)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;
    size_t num_err;
    const char *errmsg;

    /* Ommit section_re to generate error */
    char input_rules[] =
        "[rule/options_for_foo]\n"
        "validator = ini_allowed_options\n"
        /* "section_re = ^foo$\n" */
        "option = bar\n"
        "option = baz\n";

    /* section_re without value */
    char input_rules2[] =
        "[rule/options_for_foo]\n"
        "validator = ini_allowed_options\n"
        "section_re = \n"
        "option = bar\n"
        "option = baz\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[foo]\n"
        "bar = 0\n"
        "baz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 2 errors (one from rules_check and one
     * from the validator itself) */
    fail_if(ini_errobj_no_more_msgs(errobj));

    num_err = ini_errobj_count(errobj);
    fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);

    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "Rule 'rule/options_for_foo' returned error code '22'");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);

    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "[rule/options_for_foo]: Validator misses 'section_re' "
                 "parameter");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_rules_destroy(rules_obj);

    /* the second test with missing value for section_re */

    create_rules_from_str(input_rules2, &rules_obj);

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 2 errors (one from rules_check and one
     * from the validator itself) */
    fail_if(ini_errobj_no_more_msgs(errobj));

    num_err = ini_errobj_count(errobj);
    fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);

    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "Rule 'rule/options_for_foo' returned error code '22'");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);

    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "[rule/options_for_foo]: Validator misses 'section_re' "
                 "parameter");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);
    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_rules_destroy(rules_obj);

    ini_config_destroy(cfg_obj);
}
END_TEST

START_TEST(test_ini_allowed_options_wrong_regex)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;
    size_t num_err;
    const char *errmsg;

    /* Ommit section_re to generate error */
    char input_rules[] =
        "[rule/options_for_foo]\n"
        "validator = ini_allowed_options\n"
        "section_re = ^foo[$\n"
        "option = bar\n"
        "option = baz\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[foo]\n"
        "bar = 0\n"
        "baz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 2 errors (one from rules_check and one
     * from the validator itself) */
    fail_if(ini_errobj_no_more_msgs(errobj));

    num_err = ini_errobj_count(errobj);
    fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);

    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "Rule 'rule/options_for_foo' returned error code '22'");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);

    /* Different versions of libc produce slightly different error strings
     * in this case. For simplicity compare against all of them. */
    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "[rule/options_for_foo]: Cannot compile regular expression "
                 "from option 'section_re'. "
                 "Error: 'Unmatched [ or [^'");
    if (ret != 0) {
        ret = strcmp(errmsg,
                     "[rule/options_for_foo]: Cannot compile regular "
		     "expression from option 'section_re'. "
                     "Error: 'brackets ([ ]) not balanced'");
    }

    if (ret != 0) {
         ret = strcmp(errmsg,
                     "[rule/options_for_foo]: Cannot compile regular "
		     "expression from option 'section_re'. "
		     "Error: 'Unmatched [, [^, [:, [., or [='");
    }
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);

    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_options_typos)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;
    size_t num_err;

    /* Only bar and baz are allowed for foo section */
    char input_rules[] =
        "[rule/options_for_foo]\n"
        "validator = ini_allowed_options\n"
        "section_re = ^foo$\n"
        "option = bar\n"
        "option = baz\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[foo]\n"
        "br = 0\n"
        "bra = 0\n"
        "abr = 0\n"
        "abz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 4 errors */
    fail_if(ini_errobj_no_more_msgs(errobj));

    num_err = ini_errobj_count(errobj);
    fail_unless(num_err == 4, "Expected 4 errors, got %d", num_err);

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_sections_str_ok)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;

    /* Only bar and baz are allowed for foo section */
    char input_rules[] =
        "[rule/section_list]\n"
        "validator = ini_allowed_sections\n"
        "section = foo\n"
        "section = bar\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[foo]\n"
        "br = 0\n"
        "bra = 0\n"
        "[bar]\n"
        "abz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 0 errors */
    fail_unless(ini_errobj_no_more_msgs(errobj),
                "Unexpected errors found: [%s]", ini_errobj_get_msg(errobj));

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_sections_str_typos)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int num_err;
    int ret;

    /* Only bar and baz are allowed for foo section */
    char input_rules[] =
        "[rule/section_list]\n"
        "validator = ini_allowed_sections\n"
        "section = foo\n"
        "section = bar\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[fooo]\n"
        "br = 0\n"
        "bra = 0\n"
        "[baar]\n"
        "abz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 2 errors */
    fail_if(ini_errobj_no_more_msgs(errobj),
            "Expected 2 errors but none found");
    num_err = ini_errobj_count(errobj);
    fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_sections_str_insensitive)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;
    int i;

    /* Only bar and baz are allowed for foo section */
    char input_rules_template[] =
        "[rule/section_list]\n"
        "validator = ini_allowed_sections\n"
        "case_insensitive = %s\n"
        "section = foo\n"
        "section = bar\n";

    char input_rules[sizeof(input_rules_template) + 10];

    const char *case_insensitive_values[] = { "yes", "Yes", "true", "True",
                                              "1", NULL };
    /* Make 4 typos */
    char input_cfg[] =
        "[FOo]\n"
        "br = 0\n"
        "bra = 0\n"
        "[baR]\n"
        "abz = 0\n";

    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    for (i = 0; case_insensitive_values[i] != NULL; i++) {
        sprintf(input_rules, input_rules_template, case_insensitive_values[i]);

        create_rules_from_str(input_rules, &rules_obj);

        ret = ini_errobj_create(&errobj);
        fail_unless(ret == 0,
                    "ini_errobj_create() failed for case_insensitive = %s: %s",
                    case_insensitive_values[i], strerror(ret));

        ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
        fail_unless(ret == 0,
                    "ini_rules_check() failed for case_insensitive = %s: %s",
                    case_insensitive_values[i], strerror(ret));

        /* Should generate 0 errors */
        fail_unless(ini_errobj_no_more_msgs(errobj),
                    "Unexpected errors found for case_insensitive = %s: [%s]",
                    case_insensitive_values[i], ini_errobj_get_msg(errobj));

        ini_errobj_destroy(&errobj);
        ini_rules_destroy(rules_obj);
    }

    ini_config_destroy(cfg_obj);
}
END_TEST

START_TEST(test_ini_allowed_sections_re_ok)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;

    char input_rules[] =
        "[rule/section_list]\n"
        "validator = ini_allowed_sections\n"
        "section_re = ^foo*$\n"
        "section_re = bar\n";

    char input_cfg[] =
        "[foooooooooooo]\n"
        "br = 0\n"
        "bra = 0\n"
        "[my_bar]\n"
        "abz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 0 errors */
    fail_unless(ini_errobj_no_more_msgs(errobj), "Unexpected errors found");

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_sections_re_typos)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int num_err;
    int ret;

    char input_rules[] =
        "[rule/section_list]\n"
        "validator = ini_allowed_sections\n"
        "section_re = ^foo*$\n"
        "section_re = bar\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[fooooooOooooo]\n"
        "br = 0\n"
        "bra = 0\n"
        "[my_bra]\n"
        "abz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 2 errors */
    fail_if(ini_errobj_no_more_msgs(errobj),
            "Expected 2 errors but none found");
    num_err = ini_errobj_count(errobj);
    fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_sections_re_insensitive)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int ret;

    /* Only bar and baz are allowed for foo section */
    char input_rules[] =
        "[rule/section_list]\n"
        "validator = ini_allowed_sections\n"
        "case_insensitive = yes\n"
        "section_re = ^foo*$\n"
        "section_re = bar\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[FOoOoOoOoOOOOooo]\n"
        "br = 0\n"
        "bra = 0\n"
        "[my_Bar]\n"
        "abz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 0 errors */
    fail_unless(ini_errobj_no_more_msgs(errobj), "Unexpected errors found");

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_sections_missing_section)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int num_err;
    int ret;
    const char *errmsg;

    /* Only bar and baz are allowed for foo section */
    char input_rules[] =
        "[rule/section_list]\n"
        "validator = ini_allowed_sections\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[fooo]\n"
        "br = 0\n"
        "bra = 0\n"
        "[baar]\n"
        "abz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 1 errors */
    fail_if(ini_errobj_no_more_msgs(errobj),
            "Expected 1 errors but none found");
    num_err = ini_errobj_count(errobj);
    fail_unless(num_err == 1, "Expected 1 errors, got %d", num_err);

    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "[rule/section_list]: No allowed sections specified. "
                 "Use 'section = default' to allow only default section");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);

    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

START_TEST(test_ini_allowed_sections_wrong_regex)
{
    struct ini_cfgobj *rules_obj;
    struct ini_cfgobj *cfg_obj;
    struct ini_errobj *errobj;
    int num_err;
    int ret;
    const char *errmsg;

    /* Only bar and baz are allowed for foo section */
    char input_rules[] =
        "[rule/section_list]\n"
        "validator = ini_allowed_sections\n"
        "section_re = ^foo\\(*$\n";

    /* Make 4 typos */
    char input_cfg[] =
        "[fooo]\n"
        "br = 0\n"
        "bra = 0\n"
        "[baar]\n"
        "abz = 0\n";

    create_rules_from_str(input_rules, &rules_obj);
    cfg_obj = get_ini_config_from_str(input_cfg, sizeof(input_cfg));

    ret = ini_errobj_create(&errobj);
    fail_unless(ret == 0, "ini_errobj_create() failed: %s", strerror(ret));

    ret = ini_rules_check(rules_obj, cfg_obj, NULL, errobj);
    fail_unless(ret == 0, "ini_rules_check() failed: %s", strerror(ret));

    /* Should generate 2 errors */
    fail_if(ini_errobj_no_more_msgs(errobj),
            "Expected 2 errors but none found");
    num_err = ini_errobj_count(errobj);
    fail_unless(num_err == 2, "Expected 2 errors, got %d", num_err);

    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "Rule 'rule/section_list' returned error code '22'");
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);

    errmsg = ini_errobj_get_msg(errobj);
    ret = strcmp(errmsg,
                 "[rule/section_list]: Validator failed to use regex "
                 "[^foo\\(*$]:[Unmatched ( or \\(]");
    if (ret !=0) {
        ret = strcmp(errmsg,
                     "[rule/section_list]: Validator failed to use regex "
                     "[^foo\\(*$]:[parentheses not balanced]");
    }
    fail_unless(ret == 0, "Got msg: [%s]", errmsg);
    ini_errobj_next(errobj);

    fail_unless(ini_errobj_no_more_msgs(errobj));

    ini_errobj_destroy(&errobj);
    ini_config_destroy(cfg_obj);
    ini_rules_destroy(rules_obj);
}
END_TEST

static Suite *ini_validators_utils_suite(void)
{
    Suite *s = suite_create("ini_validators");

    TCase *tc_infrastructure = tcase_create("infrastructure");
    tcase_add_test(tc_infrastructure, test_ini_errobj);
    tcase_add_test(tc_infrastructure, test_ini_noerror);
    tcase_add_test(tc_infrastructure, test_ini_error);
    tcase_add_test(tc_infrastructure, test_unknown_validator);
    tcase_add_test(tc_infrastructure, test_custom_noerror);
    tcase_add_test(tc_infrastructure, test_custom_error);

    TCase *tc_allowed_options = tcase_create("ini_allowed_options");
    tcase_add_test(tc_allowed_options, test_ini_allowed_options_ok);
    tcase_add_test(tc_allowed_options, test_ini_allowed_options_no_section);
    tcase_add_test(tc_allowed_options, test_ini_allowed_options_wrong_regex);
    tcase_add_test(tc_allowed_options, test_ini_allowed_options_typos);

    TCase *tc_allowed_sections = tcase_create("ini_allowed_sections");
    tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_str_ok);
    tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_str_typos);
    tcase_add_test(tc_allowed_sections,
                   test_ini_allowed_sections_str_insensitive);
    tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_re_ok);
    tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_re_typos);
    tcase_add_test(tc_allowed_sections,
                   test_ini_allowed_sections_re_insensitive);
    tcase_add_test(tc_allowed_sections,
                   test_ini_allowed_sections_missing_section);
    tcase_add_test(tc_allowed_sections, test_ini_allowed_sections_wrong_regex);

    suite_add_tcase(s, tc_infrastructure);
    suite_add_tcase(s, tc_allowed_options);
    suite_add_tcase(s, tc_allowed_sections);

    return s;
}

int main(void)
{
    int number_failed;

    Suite *s = ini_validators_utils_suite();
    SRunner *sr = srunner_create(s);
    /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
    srunner_run_all(sr, CK_ENV);
    number_failed = srunner_ntests_failed(sr);
    srunner_free(sr);
    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}