Debian Codegraph @

ini/ini_parse_ut.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Unit test for the parser object.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2010
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 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <limits.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include "ini_defines.h"
31 #include "ini_configobj.h"
32 #include "ini_config_priv.h"
33 #include "ini_valueobj.h"
34 #include "simplebuffer.h"
35 #include "path_utils.h"
36 #define TRACE_HOME
37 #include "trace.h"
38 #include "collection_tools.h"
39
40 int verbose = 0;
41 char *confdir = NULL;
42
43 #define NUM_TESTS 14
44 #define VAL_SIZE 100
45
46 #define INIOUT(foo) \
47 do { \
48 if (verbose) foo; \
49 } while(0)
50
51 typedef int (*test_fn)(void);
52
53 static int test_one_file(const char *in_filename,
54 const char *out_filename,
55 int edge,
56 int in_mem)
57 {
58 int error = EOK;
59 struct ini_cfgfile *file_ctx = NULL;
60 FILE *ff = NULL;
61 struct ini_cfgobj *ini_config = NULL;
62 struct ini_cfgobj *ini_copy = NULL;
63 char **error_list = NULL;
64 struct simplebuffer *sbobj = NULL;
65 uint32_t left = 0;
66 uint32_t stream_len = 0;
67 void *stream_data = NULL;
68 struct stat file_stats;
69 int stat_ret = 0;
70 FILE *file = NULL;
71
72 INIOUT(printf("<==== Testing file %s ====>\n", in_filename));
73
74 /* Create config collection */
75 error = ini_config_create(&ini_config);
76 if (error) {
77 printf("Failed to create collection. Error %d.\n", error);
78 return error;
79 }
80
81 if (in_mem) {
82 /* Get file stats */
83 errno = 0;
84 stat_ret = stat(in_filename, &file_stats);
85 if (stat_ret == -1) {
86 error = errno;
87 printf("Failed to get file stats. Error %d.\n", error);
88 ini_config_destroy(ini_config);
89 return error;
90 }
91 /* Allocate memory to store file */
92 errno = 0;
93 stream_len = file_stats.st_size;
94 stream_data = malloc(file_stats.st_size + 1);
95 if (!stream_data) {
96 error = errno;
97 printf("Failed to allocate memory for stream. Error %d.\n", error);
98 ini_config_destroy(ini_config);
99 return error;
100 }
101 *((unsigned char *)(stream_data) + stream_len) = '\0';
102 /* Open file */
103 errno = 0;
104 file = fopen(in_filename, "r");
105 if (!stream_data) {
106 error = errno;
107 printf("Failed to open file to prepare stream. Error %d.\n", error);
108 free(stream_data);
109 ini_config_destroy(ini_config);
110 return error;
111 }
112 /* Read file into memory */
113 errno = 0;
114 fread(stream_data, stream_len, 1, file);
115 error = ferror(file);
116 if (error) {
117 printf("Failed to read stream data. Error %d.\n", error);
118 free(stream_data);
119 fclose(file);
120 ini_config_destroy(ini_config);
121 return error;
122 }
123 fclose(file);
124
125 INIOUT(printf("Data len %u\n", stream_len));
126 INIOUT(printf("Data:\n%s\n", (char *)stream_data));
127
128 error = ini_config_file_from_mem(stream_data,
129 stream_len,
130 &file_ctx);
131 if (error) {
132 printf("Failed to open from memory. Error %d.\n", error);
133 free(stream_data);
134 ini_config_destroy(ini_config);
135 return error;
136 }
137 }
138 else {
139 error = ini_config_file_open(in_filename,
140 0, /* TBD */
141 &file_ctx);
142 if (error) {
143 printf("Failed to open file %s for reading. Error %d.\n",
144 in_filename, error);
145 ini_config_destroy(ini_config);
146 return error;
147 }
148 }
149
150 error = ini_config_parse(file_ctx,
151 INI_STOP_ON_NONE,
152 0, /* TBD */
153 0,
154 ini_config);
155 if (error) {
156 INIOUT(printf("Failed to parse configuration. Error %d.\n", error));
157
158 if (ini_config_error_count(ini_config)) {
159 if (in_mem) {
160 INIOUT(printf("Errors detected while parsing"
161 " configuration stored in memory: %s\n",
162 in_filename));
163 }
164 else {
165 INIOUT(printf("Errors detected while parsing: %s\n",
166 ini_config_get_filename(file_ctx)));
167 }
168 ini_config_get_errors(ini_config, &error_list);
169 INIOUT(ini_config_print_errors(stdout, error_list));
170 ini_config_free_errors(error_list);
171 }
172 /* If we are testing memory mapped, return error */
173 if (in_mem) {
174 free(stream_data);
175 ini_config_file_destroy(file_ctx);
176 ini_config_destroy(ini_config);
177 return error;
178 }
179 }
180
181 if (in_mem) free(stream_data);
182 ini_config_file_destroy(file_ctx);
183
184 INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
185
186 /* Copy configuration */
187 error = ini_config_copy(ini_config, &ini_copy);
188 if (error) {
189 printf("Failed to copy configuration. Error %d.\n", error);
190 ini_config_destroy(ini_config);
191 return error;
192 }
193
194 ini_config_destroy(ini_config);
195 ini_config = ini_copy;
196
197 INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
198
199 error = ini_config_set_wrap(ini_config, edge);
200 if (error) {
201 printf("Failed to set custom wrapper. Error %d.\n", error);
202 ini_config_destroy(ini_config);
203 return error;
204 }
205
206 error = simplebuffer_alloc(&sbobj);
207 if (error) {
208 TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
209 ini_config_destroy(ini_config);
210 return error;
211 }
212
213 error = ini_config_serialize(ini_config, sbobj);
214 if (error != EOK) {
215 printf("Failed to serialize configuration. Error %d.\n", error);
216 ini_config_destroy(ini_config);
217 simplebuffer_free(sbobj);
218 return error;
219 }
220
221 errno = 0;
222 ff = fopen(out_filename, "w");
223 if(!ff) {
224 error = errno;
225 printf("Failed to open file [%s] for writing. Error %d.\n",
226 out_filename, error);
227 ini_config_destroy(ini_config);
228 simplebuffer_free(sbobj);
229 return error;
230 }
231
232 /* Save */
233 left = simplebuffer_get_len(sbobj);
234 while (left > 0) {
235 error = simplebuffer_write(fileno(ff), sbobj, &left);
236 if (error) {
237 printf("Failed to write back the configuration %d.\n", error);
238 simplebuffer_free(sbobj);
239 ini_config_destroy(ini_config);
240 fclose(ff);
241 return error;
242 }
243 }
244
245 ini_config_destroy(ini_config);
246 simplebuffer_free(sbobj);
247 fclose(ff);
248
249 return EOK;
250 }
251
252
253 /* Run tests for multiple files */
254 static int read_save_test(void)
255 {
256 int error = EOK;
257 int i = 0;
258 int edge = 5;
259 char infile[PATH_MAX];
260 char outfile[PATH_MAX];
261 char *srcdir = NULL;
262 const char *files[] = { "real",
263 "mysssd",
264 "ipa",
265 "test",
266 "smerge",
267 "real8",
268 "real16be",
269 "real16le",
270 "real32be",
271 "real32le",
272 "symbols",
273 NULL };
274
275 INIOUT(printf("<==== Read save test ====>\n"));
276
277 srcdir = getenv("srcdir");
278
279 while(files[i]) {
280 for ( edge = 10; edge < 100; edge +=19) {
281 snprintf(infile, PATH_MAX, "%s/ini/ini.d/%s.conf",
282 (srcdir == NULL) ? "." : srcdir, files[i]);
283 snprintf(outfile, PATH_MAX, "./%s_%d.conf.out", files[i], edge);
284 error = test_one_file(infile, outfile, edge, 0);
285 INIOUT(printf("Test for file: %s returned %d\n", files[i], error));
286 if (error) return error;
287 }
288 i++;
289 }
290
291 INIOUT(printf("<==== Read save test end ====>\n"));
292
293 return EOK;
294 }
295
296
297 /* Run tests for multiple files */
298 static int read_mem_test(void)
299 {
300 int error = EOK;
301 int i = 0;
302 int edge = 5;
303 char infile[PATH_MAX];
304 char outfile[PATH_MAX];
305 char *srcdir = NULL;
306 const char *files[] = { "real",
307 "mysssd",
308 "ipa",
309 "test",
310 "smerge",
311 "real8",
312 "real16be",
313 "real16le",
314 "real32be",
315 "real32le",
316 "symbols",
317 "new_line",
318 NULL };
319
320 INIOUT(printf("<==== Read mem test ====>\n"));
321
322 srcdir = getenv("srcdir");
323
324 while(files[i]) {
325 for ( edge = 15; edge < 100; edge +=25) {
326 snprintf(infile, PATH_MAX, "%s/ini/ini.d/%s.conf",
327 (srcdir == NULL) ? "." : srcdir, files[i]);
328 snprintf(outfile, PATH_MAX, "./%s_%d.conf.mem.out", files[i], edge);
329 error = test_one_file(infile, outfile, edge, 1);
330 INIOUT(printf("Test for file: %s returned %d\n", files[i], error));
331 if ((error) && (strncmp(files[i], "test", 4) != 0)) return error;
332 }
333 i++;
334 }
335
336 INIOUT(printf("<==== Read mem test end ====>\n"));
337
338 return EOK;
339 }
340
341
342 /* Run tests for multiple files */
343 static int read_again_test(void)
344 {
345 int error = EOK;
346 int i = 0;
347 int edge = 5;
348 char infile[PATH_MAX];
349 char outfile[PATH_MAX];
350 char command[PATH_MAX * 3];
351 const char *files[] = { "real",
352 "mysssd",
353 "ipa",
354 "test",
355 "smerge",
356 "real8",
357 "real16be",
358 "real16le",
359 "real32be",
360 "real32le",
361 "symbols",
362 NULL };
363
364 INIOUT(printf("<==== Read again test ====>\n"));
365
366 while(files[i]) {
367 for ( edge = 10; edge < 100; edge +=19) {
368
369 snprintf(infile, PATH_MAX, "./%s_%d.conf.out", files[i], edge);
370 snprintf(outfile, PATH_MAX, "./%s_%d.conf.2.out", files[i], edge);
371
372 error = test_one_file(infile, outfile, edge, 0);
373 INIOUT(printf("Test for file: %s returned %d\n", files[i], error));
374 if (error) break;
375 snprintf(command, PATH_MAX * 3, "diff -q %s %s", infile, outfile);
376 error = system(command);
377 INIOUT(printf("Comparison of %s %s returned: %d\n",
378 infile, outfile, error));
379 if ((error) || (WEXITSTATUS(error))) {
380 printf("Failed to run copy command %d %d.\n", error,
381 WEXITSTATUS(error));
382 error = -1;
383 break;
384 }
385 }
386
387 i++;
388 }
389
390 INIOUT(printf("<==== Read again test end ====>\n"));
391
392 return error;
393 }
394
395 static int create_expect(const char *checkname)
396 {
397 FILE *ff;
398 int error = EOK;
399
400 errno = 0;
401 ff = fopen(checkname, "w");
402 if(!ff) {
403 error = errno;
404 printf("Failed to open file %s for writing. Error %d.\n",
405 checkname, error);
406 return error;
407 }
408
409 /* Ovewrite */
410 fprintf(ff,"#Hoho section\n");
411 fprintf(ff,"[hoho]\n");
412 fprintf(ff,"#Hoho value\n");
413 fprintf(ff,"val = hoho\n");
414 fprintf(ff,"#End of hoho\n");
415 fprintf(ff,"#Start of section\n");
416 fprintf(ff,"[foo]\n");
417 fprintf(ff,"#Second value\n");
418 fprintf(ff,"bar = second value\n");
419 fprintf(ff,"#End of section\n");
420 /* Preserve */
421 fprintf(ff,"#Hoho section\n");
422 fprintf(ff,"[hoho]\n");
423 fprintf(ff,"#Hoho value\n");
424 fprintf(ff,"val = hoho\n");
425 fprintf(ff,"#End of hoho\n");
426 fprintf(ff,"#Start of section\n");
427 fprintf(ff,"[foo]\n");
428 fprintf(ff,"#First value\n");
429 fprintf(ff,"bar = first value\n");
430 fprintf(ff,"#End of section\n");
431 /* Allow */
432 fprintf(ff,"#Hoho section\n");
433 fprintf(ff,"[hoho]\n");
434 fprintf(ff,"#Hoho value\n");
435 fprintf(ff,"val = hoho\n");
436 fprintf(ff,"#End of hoho\n");
437 fprintf(ff,"#Start of section\n");
438 fprintf(ff,"[foo]\n");
439 fprintf(ff,"#First value\n");
440 fprintf(ff,"bar = first value\n");
441 fprintf(ff,"#Second value\n");
442 fprintf(ff,"bar = second value\n");
443 fprintf(ff,"#End of section\n");
444 /* Detect */
445 fprintf(ff,"#Hoho section\n");
446 fprintf(ff,"[hoho]\n");
447 fprintf(ff,"#Hoho value\n");
448 fprintf(ff,"val = hoho\n");
449 fprintf(ff,"#End of hoho\n");
450 fprintf(ff,"#Start of section\n");
451 fprintf(ff,"[foo]\n");
452 fprintf(ff,"#First value\n");
453 fprintf(ff,"bar = first value\n");
454 fprintf(ff,"#Second value\n");
455 fprintf(ff,"bar = second value\n");
456 fprintf(ff,"#End of section\n");
457
458 fclose(ff);
459
460 return EOK;
461 }
462
463 /* Check merge modes */
464 static int merge_values_test(void)
465 {
466 int error = EOK;
467 int i;
468 struct ini_cfgfile *file_ctx = NULL;
469 FILE *ff = NULL;
470 struct ini_cfgobj *ini_config = NULL;
471 char **error_list = NULL;
472 struct simplebuffer *sbobj = NULL;
473 uint32_t left = 0;
474 uint32_t mflags[] = { INI_MV1S_OVERWRITE,
475 INI_MV1S_ERROR,
476 INI_MV1S_PRESERVE,
477 INI_MV1S_ALLOW,
478 INI_MV1S_DETECT };
479
480 const char *mstr[] = { "OVERWRITE",
481 "ERROR",
482 "PRESERVE",
483 "ALLOW",
484 "DETECT" };
485
486 char filename[PATH_MAX];
487 const char *resname = "./merge.conf.out";
488 const char *checkname = "./expect.conf.out";
489 char command[PATH_MAX * 3];
490 char *srcdir = NULL;
491
492 INIOUT(printf("<==== Merge values test ====>\n"));
493
494 srcdir = getenv("srcdir");
495
496 snprintf(filename, PATH_MAX, "%s/ini/ini.d/foo.conf.in",
497 (srcdir == NULL) ? "." : srcdir);
498
499 error = simplebuffer_alloc(&sbobj);
500 if (error) {
501 TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
502 return error;
503 }
504
505 for (i = 0; i < 5; i++) {
506
507 INIOUT(printf("<==== Testing mode %s ====>\n", mstr[i]));
508
509 /* Create config collection */
510 ini_config = NULL;
511 error = ini_config_create(&ini_config);
512 if (error) {
513 printf("Failed to create collection. Error %d.\n", error);
514 simplebuffer_free(sbobj);
515 return error;
516 }
517
518 file_ctx = NULL;
519 error = ini_config_file_open(filename,
520 0, /* TBD */
521 &file_ctx);
522 if (error) {
523 printf("Failed to open file %s for reading. Error %d.\n",
524 filename, error);
525 printf("Src dir is [%s].\n", (srcdir == NULL) ?
526 "NOT DEFINED" : srcdir);
527 ini_config_destroy(ini_config);
528 simplebuffer_free(sbobj);
529 return error;
530 }
531
532 error = ini_config_parse(file_ctx,
533 INI_STOP_ON_ANY,
534 mflags[i],
535 0,
536 ini_config);
537 if (error) {
538 INIOUT(printf("Failed to parse configuration. Error %d.\n",
539 error));
540
541 if (ini_config_error_count(ini_config)) {
542 INIOUT(printf("Errors detected while parsing: %s\n",
543 ini_config_get_filename(file_ctx)));
544 ini_config_get_errors(ini_config, &error_list);
545 INIOUT(ini_config_print_errors(stdout, error_list));
546 ini_config_free_errors(error_list);
547 }
548
549 if (((mflags[i] != INI_MV1S_ERROR) &&
550 (mflags[i]!= INI_MV1S_DETECT)) ||
551 ((mflags[i] = INI_MV1S_ERROR) && (error != EEXIST)) ||
552 ((mflags[i] = INI_MV1S_DETECT) && (error != EEXIST))) {
553 printf("This is unexpected error %d in mode %d\n",
554 error, mflags[i]);
555 ini_config_destroy(ini_config);
556 simplebuffer_free(sbobj);
557 ini_config_file_destroy(file_ctx);
558 return error;
559 }
560 /* We do not return here intentionally */
561 }
562
563 ini_config_file_destroy(file_ctx);
564
565 INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
566
567 error = ini_config_serialize(ini_config, sbobj);
568 if (error != EOK) {
569 printf("Failed to serialize configuration. Error %d.\n", error);
570 ini_config_destroy(ini_config);
571 simplebuffer_free(sbobj);
572 return error;
573 }
574
575 ini_config_destroy(ini_config);
576 }
577
578 errno = 0;
579 ff = fopen(resname, "w");
580 if(!ff) {
581 error = errno;
582 printf("Failed to open file for writing. Error %d.\n", error);
583 simplebuffer_free(sbobj);
584 return error;
585 }
586
587 /* Save */
588 left = simplebuffer_get_len(sbobj);
589 while (left > 0) {
590 error = simplebuffer_write(fileno(ff), sbobj, &left);
591 if (error) {
592 printf("Failed to write back the configuration %d.\n", error);
593 simplebuffer_free(sbobj);
594 fclose(ff);
595 return error;
596 }
597 }
598
599 simplebuffer_free(sbobj);
600 fclose(ff);
601
602 error = create_expect(checkname);
603 if (error) {
604 printf("Failed to create file with expected contents %d.\n", error);
605 return error;
606 }
607
608 snprintf(command, PATH_MAX * 3, "diff -q %s %s", resname, checkname);
609 error = system(command);
610 INIOUT(printf("Comparison of %s %s returned: %d\n",
611 resname, checkname, error));
612 if ((error) || (WEXITSTATUS(error))) {
613 printf("Failed to run copy command %d %d.\n", error,
614 WEXITSTATUS(error));
615 return -1;
616 }
617
618 INIOUT(printf("<==== Merge values test end ====>\n"));
619
620 return error;
621 }
622
623 /* Check merge modes */
624 static int merge_section_test(void)
625 {
626 int error = EOK;
627 int i, j;
628 struct ini_cfgfile *file_ctx = NULL;
629 FILE *ff = NULL;
630 struct ini_cfgobj *ini_config = NULL;
631 char **error_list = NULL;
632 struct simplebuffer *sbobj = NULL;
633 uint32_t left = 0;
634 uint32_t msecflags[] = { INI_MS_MERGE,
635 INI_MS_ERROR,
636 INI_MS_OVERWRITE,
637 INI_MS_PRESERVE,
638 INI_MS_DETECT };
639
640 uint32_t mflags[] = { INI_MV2S_OVERWRITE,
641 INI_MV2S_ERROR,
642 INI_MV2S_PRESERVE,
643 INI_MV2S_ALLOW,
644 INI_MV2S_DETECT };
645
646 const char *secmstr[] = { "MERGE",
647 "ERROR",
648 "OVERWRITE",
649 "PRESERVE",
650 "DETECT" };
651
652 const char *mstr[] = { "OVERWRITE",
653 "ERROR",
654 "PRESERVE",
655 "ALLOW",
656 "DETECT" };
657
658 char filename[PATH_MAX];
659 char checkname[PATH_MAX];
660 char resname[PATH_MAX];
661 char command[PATH_MAX * 3];
662 char mode[VAL_SIZE];
663 char *srcdir = NULL;
664 char *builddir = NULL;
665
666
667 INIOUT(printf("<==== Merge section test ====>\n"));
668
669 srcdir = getenv("srcdir");
670 builddir = getenv("builddir");
671 snprintf(filename, PATH_MAX, "%s/ini/ini.d/smerge.conf",
672 (srcdir == NULL) ? "." : srcdir);
673 snprintf(checkname, PATH_MAX, "%s/ini/ini.d/sexpect.conf",
674 (srcdir == NULL) ? "." : srcdir);
675 snprintf(resname, PATH_MAX, "%s/smerge.conf.out",
676 (builddir == NULL) ? "." : builddir);
677
678 error = simplebuffer_alloc(&sbobj);
679 if (error) {
680 TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
681 return error;
682 }
683
684 for (i = 0; i < 5; i++) {
685 for (j = 0; j < 5; j++) {
686
687 INIOUT(printf("<==== Testing mode %s + %s ====>\n",
688 secmstr[i], mstr[j]));
689
690 snprintf(mode, VAL_SIZE, "# Section mode: %s, value mode: %s\n",
691 secmstr[i], mstr[j]);
692
693 error = simplebuffer_add_str(sbobj,
694 mode,
695 strlen(mode),
696 VAL_SIZE);
697 if (error) {
698 TRACE_ERROR_NUMBER("Failed to add string.",
699 error);
700 simplebuffer_free(sbobj);
701 return error;
702 }
703
704 /* Create config collection */
705 ini_config = NULL;
706 error = ini_config_create(&ini_config);
707 if (error) {
708 printf("Failed to create collection. "
709 "Error %d.\n", error);
710 simplebuffer_free(sbobj);
711 return error;
712 }
713
714 file_ctx = NULL;
715 error = ini_config_file_open(filename,
716 0, /* TBD */
717 &file_ctx);
718 if (error) {
719 printf("Failed to open file %s for reading. "
720 "Error %d.\n", filename, error);
721 printf("Source is %s.\n", (srcdir == NULL) ?
722 "NOT Defined" : srcdir);
723 ini_config_destroy(ini_config);
724 simplebuffer_free(sbobj);
725 return error;
726 }
727
728 error = ini_config_parse(file_ctx,
729 INI_STOP_ON_ANY,
730 msecflags[i] | mflags[j],
731 0,
732 ini_config);
733 if (error) {
734 INIOUT(printf("Failed to parse configuration. "
735 "Error %d.\n", error));
736
737 if (ini_config_error_count(ini_config)) {
738 INIOUT(printf("Errors detected while parsing: %s\n",
739 ini_config_get_filename(file_ctx)));
740 ini_config_get_errors(ini_config, &error_list);
741 INIOUT(ini_config_print_errors(stdout, error_list));
742 ini_config_free_errors(error_list);
743 }
744
745 if (((msecflags[i] == INI_MS_ERROR) &&
746 (error == EEXIST)) ||
747 ((msecflags[i] == INI_MS_DETECT) &&
748 (error == EEXIST)) ||
749 ((msecflags[i] == INI_MS_MERGE) &&
750 ((mflags[j] == INI_MV2S_ERROR) ||
751 (mflags[j] == INI_MV2S_DETECT)) &&
752 (error == EEXIST))) {
753 INIOUT(printf("This is an expected error "
754 "%d in mode %d + %d\n",
755 error,
756 msecflags[i],
757 mflags[j]));
758 /* We do not return here intentionally */
759 }
760 else {
761 printf("This is unexpected error %d in mode %d + %d\n",
762 error, msecflags[i], mflags[j]);
763 ini_config_destroy(ini_config);
764 simplebuffer_free(sbobj);
765 ini_config_file_destroy(file_ctx);
766 return error;
767 }
768 }
769
770 ini_config_file_destroy(file_ctx);
771
772 INIOUT(col_debug_collection(ini_config->cfg,
773 COL_TRAVERSE_DEFAULT));
774
775 error = ini_config_serialize(ini_config, sbobj);
776 if (error != EOK) {
777 printf("Failed to serialize configuration. "
778 "Error %d.\n", error);
779 ini_config_destroy(ini_config);
780 simplebuffer_free(sbobj);
781 return error;
782 }
783
784 ini_config_destroy(ini_config);
785 }
786 }
787
788 errno = 0;
789 ff = fopen(resname, "w");
790 if(!ff) {
791 error = errno;
792 printf("Failed to open file for writing. Error %d.\n", error);
793 simplebuffer_free(sbobj);
794 return error;
795 }
796
797 /* Save */
798 left = simplebuffer_get_len(sbobj);
799 while (left > 0) {
800 error = simplebuffer_write(fileno(ff), sbobj, &left);
801 if (error) {
802 printf("Failed to write back the configuration %d.\n", error);
803 simplebuffer_free(sbobj);
804 fclose(ff);
805 return error;
806 }
807 }
808
809 simplebuffer_free(sbobj);
810 fclose(ff);
811
812 snprintf(command, PATH_MAX * 3, "diff -q %s %s", resname, checkname);
813 error = system(command);
814 INIOUT(printf("Comparison of %s %s returned: %d\n",
815 resname, checkname, error));
816
817 if ((error) || (WEXITSTATUS(error))) {
818 printf("Failed to run diff command %d %d.\n", error,
819 WEXITSTATUS(error));
820 return -1;
821 }
822
823 INIOUT(printf("<==== Merge section test end ====>\n"));
824
825 return error;
826 }
827
828 static int read_one_file(char *name,
829 struct ini_cfgobj *ini_config,
830 uint32_t collision_flags)
831 {
832 int error = EOK;
833 struct ini_cfgfile *file_ctx = NULL;
834 char **error_list = NULL;
835
836 INIOUT(printf("Reading file %s\n", name));
837
838 file_ctx = NULL;
839 error = ini_config_file_open(name,
840 0,
841 &file_ctx);
842 if (error) {
843 printf("Failed to open file %s for reading. "
844 "Error %d.\n", name, error);
845 return error;
846 }
847
848 INIOUT(printf("Parsing file %s\n", name));
849
850 error = ini_config_parse(file_ctx,
851 INI_STOP_ON_ANY,
852 collision_flags,
853 0,
854 ini_config);
855 if (error) {
856 INIOUT(printf("Failed to parse configuration. "
857 "Error %d.\n", error));
858
859 if (ini_config_error_count(ini_config)) {
860 INIOUT(printf("Errors detected while parsing: %s\n",
861 ini_config_get_filename(file_ctx)));
862 ini_config_get_errors(ini_config, &error_list);
863 INIOUT(ini_config_print_errors(stdout, error_list));
864 ini_config_free_errors(error_list);
865 }
866 ini_config_file_destroy(file_ctx);
867 return error;
868 }
869
870 ini_config_file_destroy(file_ctx);
871
872 INIOUT(printf("Successfully parsed file %s\n", name));
873 return EOK;
874 }
875
876 /* Check merge modes */
877 static int merge_file_test(void)
878 {
879 int error = EOK;
880 int i, j;
881 struct ini_cfgobj *ini_config_first = NULL;
882 struct ini_cfgobj *ini_config_second = NULL;
883 struct ini_cfgobj *ini_config_result = NULL;
884 struct simplebuffer *sbobj = NULL;
885 uint32_t left = 0;
886 FILE *ff = NULL;
887 uint32_t msecflags[] = { INI_MS_MERGE,
888 INI_MS_ERROR,
889 INI_MS_OVERWRITE,
890 INI_MS_PRESERVE,
891 INI_MS_DETECT };
892
893 uint32_t m2flags[] = { INI_MV2S_OVERWRITE,
894 INI_MV2S_ERROR,
895 INI_MV2S_PRESERVE,
896 INI_MV2S_ALLOW,
897 INI_MV2S_DETECT };
898
899 uint32_t m1flags[] = { INI_MV1S_OVERWRITE,
900 INI_MV1S_ERROR,
901 INI_MV1S_PRESERVE,
902 INI_MV1S_ALLOW,
903 INI_MV1S_DETECT };
904
905 const char *secmstr[] = { "MERGE",
906 "ERROR",
907 "OVERWRITE",
908 "PRESERVE",
909 "DETECT" };
910
911 const char *mstr[] = { "OVERWRITE",
912 "ERROR",
913 "PRESERVE",
914 "ALLOW",
915 "DETECT" };
916
917 char firstname[PATH_MAX];
918 char secondname[PATH_MAX];
919 char resname[PATH_MAX];
920 char checkname[PATH_MAX];
921 char command[PATH_MAX * 3];
922 char msg[VAL_SIZE];
923 char mode[VAL_SIZE];
924 char *srcdir = NULL;
925 char *builddir = NULL;
926 uint32_t collision_flags;
927 uint32_t ms_subst;
928 uint32_t mv1s_subst;
929 uint32_t mv2s_subst;
930
931 INIOUT(printf("<==== Merge file test ====>\n"));
932
933 srcdir = getenv("srcdir");
934 builddir = getenv("builddir");
935 snprintf(firstname, PATH_MAX, "%s/ini/ini.d/first.conf",
936 (srcdir == NULL) ? "." : srcdir);
937
938 snprintf(secondname, PATH_MAX, "%s/ini/ini.d/second.conf",
939 (srcdir == NULL) ? "." : srcdir);
940
941 snprintf(checkname, PATH_MAX, "%s/ini/ini.d/mergecheck.conf",
942 (srcdir == NULL) ? "." : srcdir);
943
944 snprintf(resname, PATH_MAX, "%s/mergecheck.conf.out",
945 (builddir == NULL) ? "." : builddir);
946
947 error = simplebuffer_alloc(&sbobj);
948 if (error) {
949 TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
950 return error;
951 }
952
953 for (i = 0; i < 5; i++) {
954 for (j = 0; j < 5; j++) {
955
956 INIOUT(printf("<==== Testing mode %s + %s ====>\n",
957 secmstr[i], mstr[j]));
958
959 snprintf(mode, VAL_SIZE, "# Section mode: %s, value mode: %s\n",
960 secmstr[i], mstr[j]);
961
962 error = simplebuffer_add_str(sbobj,
963 mode,
964 strlen(mode),
965 VAL_SIZE);
966 if (error) {
967 TRACE_ERROR_NUMBER("Failed to add string.",
968 error);
969 simplebuffer_free(sbobj);
970 return error;
971 }
972
973 /* Create first config collection */
974 ini_config_first = NULL;
975 error = ini_config_create(&ini_config_first);
976 if (error) {
977 printf("Failed to create collection. "
978 "Error %d.\n", error);
979 simplebuffer_free(sbobj);
980 return error;
981 }
982
983 /* Create second config collection */
984 ini_config_second = NULL;
985 error = ini_config_create(&ini_config_second);
986 if (error) {
987 printf("Failed to create collection. "
988 "Error %d.\n", error);
989 ini_config_destroy(ini_config_first);
990 simplebuffer_free(sbobj);
991 return error;
992 }
993
994 /* IMPORTANT: Use same collision flags for reading
995 * of the files and then merging.
996 * Mixing the flags would lead to strange results
997 * that would be hard to debug.
998 */
999 /* However here for purely testing purposes
1000 * we will not use error modes in parsing
1001 * otherwise we will not be able to try to merge.
1002 * Instead we replace the error and detect modes
1003 * with allow or merge mode.
1004 */
1005 /* The test actually does not fail in the case of
1006 * PRESERVE + ERROR becuase it should fail at
1007 * the stage of reading file but we suppress
1008 * it so we can try the merge.
1009 * As a result the mode PRESERVE + ERROR
1010 * acts as PRESERVE + ALLOW and does not return an error.
1011 * The same thing happens with PRESERVE + DETECT mode.
1012 * It might be confusing if someone tries to decipher
1013 * the tests, so this comment should help.
1014 */
1015 if ((msecflags[i] == INI_MS_ERROR) ||
1016 (msecflags[i] == INI_MS_DETECT)) {
1017 ms_subst = msecflags[i];
1018 }
1019 else {
1020 ms_subst = INI_MS_MERGE;
1021 }
1022
1023 if ((m2flags[j] == INI_MV2S_ERROR) ||
1024 (m2flags[j] == INI_MV2S_DETECT)) {
1025 mv1s_subst = INI_MV1S_ALLOW;
1026 mv2s_subst = INI_MV2S_ALLOW;
1027 }
1028 else {
1029 mv1s_subst = m1flags[j];
1030 mv2s_subst = m2flags[j];
1031 }
1032
1033 collision_flags = mv1s_subst | mv2s_subst | ms_subst;
1034
1035 error = read_one_file(firstname,
1036 ini_config_first,
1037 collision_flags);
1038 if (error) {
1039 printf("Failed to read %s. "
1040 "Error %d.\n", firstname, error);
1041 printf("Source is %s.\n", (srcdir == NULL) ?
1042 "NOT Defined" : srcdir);
1043 ini_config_destroy(ini_config_first);
1044 ini_config_destroy(ini_config_second);
1045 simplebuffer_free(sbobj);
1046 return error;
1047 }
1048
1049 error = read_one_file(secondname,
1050 ini_config_second,
1051 collision_flags);
1052 if (error) {
1053 printf("Failed to read %s. "
1054 "Error %d.\n", secondname, error);
1055 printf("Source is %s.\n", (srcdir == NULL) ?
1056 "NOT Defined" : srcdir);
1057 ini_config_destroy(ini_config_first);
1058 ini_config_destroy(ini_config_second);
1059 simplebuffer_free(sbobj);
1060 return error;
1061 }
1062
1063 INIOUT(col_debug_collection(ini_config_first->cfg,
1064 COL_TRAVERSE_ONELEVEL));
1065 INIOUT(col_debug_collection(ini_config_second->cfg,
1066 COL_TRAVERSE_ONELEVEL));
1067
1068 ini_config_result = NULL;
1069 error = ini_config_merge(ini_config_first,
1070 ini_config_second,
1071 msecflags[i] | m2flags[j] | m1flags[j],
1072 &ini_config_result);
1073 if (error) {
1074 if ((error == EEXIST) &&
1075 ((msecflags[i] == INI_MS_ERROR) ||
1076 (m2flags[j] == INI_MV2S_ERROR))) {
1077 snprintf(msg, sizeof(msg) -1,
1078 "# This is an expected error "
1079 "%d in mode %d + %d + %d\n\n",
1080 error,
1081 msecflags[i],
1082 m2flags[j],
1083 m1flags[j]);
1084 INIOUT(printf("%s", msg));
1085 error = simplebuffer_add_str(sbobj,
1086 msg,
1087 strlen(msg),
1088 VAL_SIZE);
1089 if (error) {
1090 TRACE_ERROR_NUMBER("Failed to add string.",
1091 error);
1092 ini_config_destroy(ini_config_first);
1093 ini_config_destroy(ini_config_second);
1094 simplebuffer_free(sbobj);
1095 return error;
1096 }
1097
1098 ini_config_destroy(ini_config_first);
1099 ini_config_destroy(ini_config_second);
1100 continue;
1101 }
1102 else if ((error == EEXIST) &&
1103 ((msecflags[i] == INI_MS_DETECT) ||
1104 ((msecflags[i] != INI_MS_ERROR) &&
1105 (m2flags[j] == INI_MV2S_DETECT)))) {
1106 snprintf(msg, sizeof(msg) -1,
1107 "# This is an expected error "
1108 "%d in mode %d + %d + %d\n\n",
1109 error,
1110 msecflags[i],
1111 m2flags[j],
1112 m1flags[j]);
1113 INIOUT(printf("%s", msg));
1114 error = simplebuffer_add_str(sbobj,
1115 msg,
1116 strlen(msg),
1117 VAL_SIZE);
1118 if (error) {
1119 TRACE_ERROR_NUMBER("Failed to add string.",
1120 error);
1121 ini_config_destroy(ini_config_first);
1122 ini_config_destroy(ini_config_second);
1123 simplebuffer_free(sbobj);
1124 return error;
1125 }
1126 /* Falling throught here */
1127 }
1128 else {
1129 TRACE_ERROR_NUMBER("Failed to merge.",
1130 error);
1131 ini_config_destroy(ini_config_first);
1132 ini_config_destroy(ini_config_second);
1133 simplebuffer_free(sbobj);
1134 return error;
1135 }
1136 }
1137
1138 INIOUT(col_debug_collection(ini_config_result->cfg,
1139 COL_TRAVERSE_DEFAULT));
1140
1141 error = ini_config_serialize(ini_config_result, sbobj);
1142 if (error) {
1143 printf("Failed to serialize configuration. "
1144 "Error %d.\n", error);
1145 ini_config_destroy(ini_config_first);
1146 ini_config_destroy(ini_config_second);
1147 ini_config_destroy(ini_config_result);
1148 simplebuffer_free(sbobj);
1149 return error;
1150 }
1151
1152 ini_config_destroy(ini_config_first);
1153 ini_config_destroy(ini_config_second);
1154 ini_config_destroy(ini_config_result);
1155 }
1156 }
1157
1158 errno = 0;
1159 ff = fopen(resname, "w");
1160 if(!ff) {
1161 error = errno;
1162 printf("Failed to open file for writing. Error %d.\n", error);
1163 simplebuffer_free(sbobj);
1164 return error;
1165 }
1166
1167 /* Save */
1168 left = simplebuffer_get_len(sbobj);
1169 while (left > 0) {
1170 error = simplebuffer_write(fileno(ff), sbobj, &left);
1171 if (error) {
1172 printf("Failed to write back the configuration %d.\n", error);
1173 simplebuffer_free(sbobj);
1174 fclose(ff);
1175 return error;
1176 }
1177 }
1178
1179 simplebuffer_free(sbobj);
1180 fclose(ff);
1181
1182 snprintf(command,PATH_MAX * 3, "diff -q %s %s", resname, checkname);
1183 error = system(command);
1184 INIOUT(printf("Comparison of %s %s returned: %d\n",
1185 resname, checkname, error));
1186
1187 if ((error) || (WEXITSTATUS(error))) {
1188 printf("Failed to run diff command %d %d.\n", error,
1189 WEXITSTATUS(error));
1190 return -1;
1191 }
1192
1193 INIOUT(printf("<==== Merge section file end ====>\n"));
1194
1195 return EOK;
1196 }
1197
1198 static int startup_test(void)
1199 {
1200 int error = EOK;
1201 struct ini_cfgfile *file_ctx = NULL;
1202 struct ini_cfgobj *ini_config = NULL;
1203 char **error_list = NULL;
1204 char infile[PATH_MAX];
1205 char outfile[PATH_MAX];
1206 char command[PATH_MAX * 3];
1207 char *srcdir = NULL;
1208 char *builddir;
1209 const struct stat *file_stat;
1210
1211 INIOUT(printf("<==== Startup test ====>\n"));
1212
1213 srcdir = getenv("srcdir");
1214 snprintf(infile, PATH_MAX, "%s/ini/ini.d/foo.conf.in",
1215 (srcdir == NULL) ? "." : srcdir);
1216 builddir = getenv("builddir");
1217 snprintf(outfile, PATH_MAX, "%s/foo.conf",
1218 (builddir == NULL) ? "." : builddir);
1219
1220 snprintf(command, PATH_MAX * 3, "cp %s %s", infile, outfile);
1221 INIOUT(printf("Running command '%s'\n", command));
1222
1223 error = system(command);
1224 if ((error) || (WEXITSTATUS(error))) {
1225 printf("Failed to run copy command %d %d.\n", error,
1226 WEXITSTATUS(error));
1227 return -1;
1228 }
1229
1230 INIOUT(printf("Running chmod 660 on file '%s'\n", outfile));
1231 error = chmod(outfile, S_IRUSR | S_IWUSR);
1232 if(error) {
1233 error = errno;
1234 printf("Failed to run chmod command %d.\n", error);
1235 return error;
1236 }
1237
1238 /* Open config file not collecting stats */
1239 error = ini_config_file_open(outfile,
1240 0,
1241 &file_ctx);
1242 if (error) {
1243 printf("Failed to open file %s for reading. Error %d.\n",
1244 outfile, error);
1245 return error;
1246 }
1247
1248 file_stat = ini_config_get_stat(file_ctx);
1249 if (file_stat) {
1250 printf("Expected NULL got not NULL!\n");
1251 return EINVAL;
1252 }
1253
1254 /* We will check just permissions here. */
1255 error = ini_config_access_check(file_ctx,
1256 INI_ACCESS_CHECK_MODE, /* add uid & gui flags
1257 * in real case
1258 */
1259 0, /* <- will be real uid in real case */
1260 0, /* <- will be real gid in real case */
1261 0440, /* Checking for r--r----- */
1262 0);
1263 /* This check is expected to fail since
1264 * we did not collect stats
1265 */
1266 ini_config_file_destroy(file_ctx);
1267
1268 if (!error) {
1269 printf("Expected error got success!\n");
1270 return EACCES;
1271 }
1272
1273
1274 /* Open config file */
1275 error = ini_config_file_open(outfile,
1276 INI_META_STATS,
1277 &file_ctx);
1278 if (error) {
1279 printf("Failed to open file %s for reading. Error %d.\n",
1280 outfile, error);
1281 return error;
1282 }
1283
1284 /* Get stats */
1285 file_stat = ini_config_get_stat(file_ctx);
1286 if (!file_stat) {
1287 printf("Expected not NULL got NULL!\n");
1288 return EINVAL;
1289 }
1290
1291 INIOUT(printf("File was modified at: %d seconds since Jan 1 1970.\n",
1292 (int)(file_stat->st_mtime)));
1293
1294 /* We will check just permissions here. */
1295 error = ini_config_access_check(file_ctx,
1296 INI_ACCESS_CHECK_MODE, /* add uid & gui flags
1297 * in real case
1298 */
1299 0, /* <- will be real uid in real case */
1300 0, /* <- will be real gid in real case */
1301 0440, /* Checking for r--r----- */
1302 0);
1303 /* This check is expected to fail since
1304 * the actual permissions on the test file are: rw-------
1305 */
1306
1307 if (!error) {
1308 printf("Expected error got success!\n");
1309 ini_config_file_destroy(file_ctx);
1310 return EACCES;
1311 }
1312
1313 error = ini_config_access_check(
1314 file_ctx,
1315 INI_ACCESS_CHECK_MODE, /* add uid & gui flags
1316 * in real case
1317 */
1318 0, /* <- will be real uid in real case */
1319 0, /* <- will be real gid in real case */
1320 0600, /* Checkling for rw------- */
1321 0);
1322
1323 if (error) {
1324 printf("Access check failed %d!\n", error);
1325 ini_config_file_destroy(file_ctx);
1326 return EACCES;
1327 }
1328
1329 /* Create config object */
1330 error = ini_config_create(&ini_config);
1331 if (error) {
1332 printf("Failed to create collection. Error %d.\n", error);
1333 ini_config_file_destroy(file_ctx);
1334 return error;
1335 }
1336
1337 error = ini_config_parse(file_ctx,
1338 INI_STOP_ON_NONE,
1339 0,
1340 0,
1341 ini_config);
1342 if (error) {
1343 INIOUT(printf("Failed to parse configuration. Error %d.\n", error));
1344
1345 if (ini_config_error_count(ini_config)) {
1346 INIOUT(printf("Errors detected while parsing: %s\n",
1347 ini_config_get_filename(file_ctx)));
1348 ini_config_get_errors(ini_config, &error_list);
1349 INIOUT(ini_config_print_errors(stdout, error_list));
1350 ini_config_free_errors(error_list);
1351 }
1352 /* We do not return here intentionally */
1353 }
1354
1355 ini_config_file_destroy(file_ctx);
1356
1357 INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
1358
1359 ini_config_destroy(ini_config);
1360
1361 INIOUT(printf("<==== Startup test end ====>\n"));
1362
1363 return 0;
1364 }
1365
1366 static int reload_test(void)
1367 {
1368 int error = EOK;
1369 struct ini_cfgfile *file_ctx = NULL;
1370 struct ini_cfgfile *file_ctx_new = NULL;
1371 char infile[PATH_MAX];
1372 char outfile[PATH_MAX];
1373 char command[PATH_MAX * 3];
1374 char *srcdir;
1375 char *builddir;
1376 int changed = 0;
1377
1378 INIOUT(printf("<==== Reload test ====>\n"));
1379
1380 srcdir = getenv("srcdir");
1381 snprintf(infile, PATH_MAX, "%s/ini/ini.d/foo.conf.in",
1382 (srcdir == NULL) ? "." : srcdir);
1383 builddir = getenv("builddir");
1384 snprintf(outfile, PATH_MAX, "%s/foo.conf",
1385 (builddir == NULL) ? "." : builddir);
1386
1387 snprintf(command, PATH_MAX * 3, "cp %s %s", infile, outfile);
1388 INIOUT(printf("Running command '%s'\n", command));
1389
1390 error = system(command);
1391 if ((error) || (WEXITSTATUS(error))) {
1392 printf("Failed to run copy command %d %d.\n", error,
1393 WEXITSTATUS(error));
1394 return -1;
1395 }
1396
1397 INIOUT(printf("Running chmod 660 on file '%s'\n", outfile));
1398 error = chmod(outfile, S_IRUSR | S_IWUSR);
1399 if (error) {
1400 error = errno;
1401 printf("Failed to run chmod command %d.\n", error);
1402 return error;
1403 }
1404
1405 INIOUT(printf("About to open file: %s'\n", outfile));
1406
1407 /* Open config file */
1408 error = ini_config_file_open(outfile,
1409 INI_META_STATS,
1410 &file_ctx);
1411 if (error) {
1412 printf("Failed to open file %s for reading. Error %d.\n",
1413 outfile, error);
1414 return error;
1415 }
1416
1417 INIOUT(printf("About to check access to the file.\n"));
1418
1419 error = ini_config_access_check(
1420 file_ctx,
1421 INI_ACCESS_CHECK_MODE, /* add uid & gui flags
1422 * in real case
1423 */
1424 0, /* <- will be real uid in real case */
1425 0, /* <- will be real gid in real case */
1426 0600, /* Checkling for rw------- */
1427 0);
1428
1429 if (error) {
1430 printf("Access check failed %d!\n", error);
1431 ini_config_file_destroy(file_ctx);
1432 return EACCES;
1433 }
1434
1435 /* ... Create config object and read configuration - not shown here.
1436 * See other examples ... */
1437
1438 INIOUT(printf("About to close file.\n"));
1439
1440 /* Now close file but leave the context around */
1441 ini_config_file_close(file_ctx);
1442
1443 INIOUT(printf("About to reopen file.\n"));
1444
1445 /* Some time passed and we received a signal to reload... */
1446 error = ini_config_file_reopen(file_ctx, &file_ctx_new);
1447 if (error) {
1448 printf("Failed to re-open file for reading. Error %d.\n", error);
1449 ini_config_file_destroy(file_ctx);
1450 return error;
1451 }
1452
1453 INIOUT(printf("About to check if the file changed.\n"));
1454
1455 changed = 0;
1456 error = ini_config_changed(file_ctx,
1457 file_ctx_new,
1458 &changed);
1459 if (error) {
1460 printf("Failed to compare files. Error %d.\n", error);
1461 ini_config_file_destroy(file_ctx);
1462 ini_config_file_destroy(file_ctx_new);
1463 return error;
1464 }
1465
1466 /* Check if file changed */
1467 if (changed) {
1468 printf("File changed when it shouldn't. This is unexpected error.\n");
1469 ini_config_file_print(file_ctx);
1470 ini_config_file_print(file_ctx_new);
1471 ini_config_file_destroy(file_ctx);
1472 ini_config_file_destroy(file_ctx_new);
1473 return EINVAL;
1474 }
1475
1476 INIOUT(printf("File did not change - expected. Close and force the change!.\n"));
1477
1478 /* Close file */
1479 ini_config_file_destroy(file_ctx_new);
1480
1481 INIOUT(printf("To force the change delete the file: %s\n", outfile));
1482
1483 /* Emulate as if file changed */
1484 errno = 0;
1485 if (unlink(outfile)) {
1486 error = errno;
1487 printf("Failed to delete file %d.\n", error);
1488 ini_config_file_destroy(file_ctx);
1489 return error;
1490 }
1491
1492 sleep(1);
1493
1494 snprintf(command, PATH_MAX * 3, "cp %s %s", infile, outfile);
1495 INIOUT(printf("Copy file again with command '%s'\n", command));
1496
1497 error = system(command);
1498 if ((error) || (WEXITSTATUS(error))) {
1499 printf("Failed to run copy command %d %d.\n", error, WEXITSTATUS(error));
1500 ini_config_file_destroy(file_ctx);
1501 return -1;
1502 }
1503
1504 INIOUT(printf("Read file again.\n"));
1505
1506 /* Read again */
1507 file_ctx_new = NULL;
1508 error = ini_config_file_reopen(file_ctx, &file_ctx_new);
1509 if (error) {
1510 printf("Failed to re-open file for reading. Error %d.\n", error);
1511 ini_config_file_destroy(file_ctx);
1512 return error;
1513 }
1514
1515 INIOUT(printf("Check if it changed.\n"));
1516
1517 changed = 0;
1518 error = ini_config_changed(file_ctx,
1519 file_ctx_new,
1520 &changed);
1521 if (error) {
1522 printf("Failed to compare files. Error %d.\n", error);
1523 ini_config_file_destroy(file_ctx);
1524 ini_config_file_destroy(file_ctx_new);
1525 return error;
1526 }
1527
1528 INIOUT(printf("Changed value is %d.\n", changed));
1529
1530 /* Check if file changed */
1531 if (!changed) {
1532 printf("File did not change when it should. This is an error.\n");
1533 ini_config_file_print(file_ctx);
1534 ini_config_file_print(file_ctx_new);
1535 ini_config_file_destroy(file_ctx);
1536 ini_config_file_destroy(file_ctx_new);
1537 return EINVAL;
1538 }
1539
1540 INIOUT(printf("File changed!\n"));
1541 INIOUT(ini_config_file_print(file_ctx));
1542 INIOUT(ini_config_file_print(file_ctx_new));
1543
1544 /* We do not need original context any more. */
1545 ini_config_file_destroy(file_ctx);
1546
1547 /* New context is now original context */
1548 file_ctx = file_ctx_new;
1549
1550 /* ... Create config object and read configuration - not shown here.
1551 * See other examples ... */
1552
1553 ini_config_file_destroy(file_ctx);
1554
1555 INIOUT(printf("<==== Reload test end ====>\n"));
1556 return 0;
1557 }
1558
1559 static int test_one_array(struct ini_cfgobj *ini_config,
1560 const char *section,
1561 const char *value,
1562 int raw,
1563 int expect,
1564 const char *sep,
1565 const char *message)
1566 {
1567 struct value_obj *vo = NULL;
1568 int error = 0;
1569 int size = 0;
1570 char **strarray;
1571 char **strptr;
1572 int i;
1573
1574 INIOUT(printf("%s",message));
1575
1576 vo = NULL;
1577 error = ini_get_config_valueobj(section,
1578 value,
1579 ini_config,
1580 INI_GET_FIRST_VALUE,
1581 &vo);
1582 if(error) {
1583 printf("Expected success but got error! %d\n",error);
1584 return error;
1585 }
1586
1587 /* Value should be found */
1588 if (vo == NULL) {
1589 printf("Expected success but got NULL. Section %s value %s, %s\n", section, value, raw ? "raw" : "interpreted");
1590 return -1;
1591 }
1592
1593 INIOUT(value_print(value, vo));
1594
1595 INIOUT(printf("Get str array with size.\n"));
1596
1597 error = 0;
1598
1599 if (raw) strarray = ini_get_raw_string_config_array(vo, sep, &size, &error);
1600 else strarray = ini_get_string_config_array(vo, sep, &size, &error);
1601 if (error) {
1602 printf("Expect success got error %d.\n", error);
1603 return error;
1604 }
1605
1606 /* Can be used with this cycle */
1607 strptr = strarray;
1608 while (*strptr != NULL) {
1609 INIOUT(printf("[%s]\n",*strptr));
1610 strptr++;
1611 }
1612
1613 if (size != expect) {
1614 printf("Expected %d but got %d.\n", expect, size);
1615 ini_free_string_config_array(strarray);
1616 return -1;
1617 }
1618
1619 /* Can be used with this cycle */
1620 INIOUT(for (i=0; i<size; i++) printf("[%s]\n", *(strarray + i)));
1621
1622 ini_free_string_config_array(strarray);
1623
1624 return EOK;
1625 }
1626
1627 static int get_test(void)
1628 {
1629
1630 int error;
1631 int number;
1632 long number_long;
1633 double number_double;
1634 unsigned number_unsigned;
1635 unsigned long number_ulong;
1636 unsigned char logical = 0;
1637 char *str;
1638 const char *cstr;
1639 const char *cstrn;
1640 void *binary;
1641 int length;
1642 int i = 0;
1643 char **strarray;
1644 char **strptr;
1645 int size;
1646 long *array;
1647 double *darray;
1648 char **prop_array;
1649 int32_t val_int32;
1650 uint32_t val_uint32;
1651 int64_t val_int64;
1652 uint64_t val_uint64;
1653 struct ini_cfgfile *file_ctx = NULL;
1654 struct ini_cfgobj *ini_config = NULL;
1655 struct value_obj *vo = NULL;
1656 char **error_list = NULL;
1657 char infile[PATH_MAX];
1658 char *srcdir = NULL;
1659 int bad_val = 0;
1660
1661 /* Define structure for testing arrays in the loop */
1662 struct a_t { char section[100];
1663 char value[100];
1664 int raw;
1665 int expect;
1666 char sep[10];
1667 char message[100]; } array_test[NUM_TESTS] =
1668 {
1669 { "services",
1670 "activeServices",
1671 0,
1672 4,
1673 ",:;",
1674 "Make sure we parse string array properly\n"},
1675
1676 { "services",
1677 "activeServices",
1678 1,
1679 4,
1680 ",:;",
1681 "Make sure we parse raw string array properly\n"},
1682
1683 { "domains",
1684 "domainsOrder",
1685 0,
1686 3,
1687 ",:;",
1688 "Parse string array with comma at the end\n"},
1689
1690 { "domains",
1691 "domainsOrder",
1692 1,
1693 8,
1694 ",:;",
1695 "Parse raw string array with comma at the end\n"},
1696
1697 { "domains",
1698 "badarray",
1699 0,
1700 0,
1701 ",:;",
1702 "Bad array should have no tokens\n"},
1703
1704 { "domains",
1705 "badarray",
1706 1,
1707 6,
1708 ",:;",
1709 "Raw bad array should have right number of tokens.\n"},
1710
1711 { "domains",
1712 "somearray",
1713 0,
1714 0,
1715 ",:;",
1716 "Bad array should have no tokens\n"},
1717
1718 { "domains",
1719 "somearray",
1720 1,
1721 2,
1722 ",:;",
1723 "Raw bad array should have right number of tokens.\n"},
1724
1725 { "domains",
1726 "someotherarray",
1727 0,
1728 0,
1729 ",:;",
1730 "Bad array should have no tokens\n"},
1731
1732 { "domains",
1733 "someotherarray",
1734 1,
1735 3,
1736 ",:;",
1737 "Raw bad array should have right number of tokens.\n"},
1738
1739 { "domains",
1740 "justdelim",
1741 0,
1742 0,
1743 ",:;",
1744 "Bad array should have no tokens\n"},
1745
1746 { "domains",
1747 "justdelim",
1748 1,
1749 5,
1750 ",:;",
1751 "Delimeters only should have right number of tokens.\n"},
1752
1753 { "domains",
1754 "yetanother",
1755 0,
1756 0,
1757 ",:;",
1758 "Empty array should have no tokens.\n"},
1759
1760 { "domains",
1761 "yetanother",
1762 1,
1763 0,
1764 ",:;",
1765 "Empty raw array should have no token.\n"}
1766 };
1767
1768 /*****************************************************/
1769
1770 INIOUT(printf("\n\n<==== GET TEST START =====>\n"));
1771 INIOUT(printf("Creating configuration object\n"));
1772
1773 /* Create config collection */
1774 error = ini_config_create(&ini_config);
1775 if (error) {
1776 printf("Failed to create collection. Error %d.\n", error);
1777 return error;
1778 }
1779
1780 srcdir = getenv("srcdir");
1781 snprintf(infile, PATH_MAX, "%s/ini/ini.d/real.conf",
1782 (srcdir == NULL) ? "." : srcdir);
1783
1784 INIOUT(printf("Reading file %s\n", infile));
1785
1786 error = ini_config_file_open(infile,
1787 0,
1788 &file_ctx);
1789 if (error) {
1790 printf("Failed to open file for reading. Error %d.\n", error);
1791 ini_config_destroy(ini_config);
1792 return error;
1793 }
1794
1795 error = ini_config_parse(file_ctx,
1796 INI_STOP_ON_NONE,
1797 /* Merge section but allow duplicates */
1798 INI_MS_MERGE |
1799 INI_MV1S_ALLOW |
1800 INI_MV2S_ALLOW,
1801 0,
1802 ini_config);
1803 if (error) {
1804 INIOUT(printf("Failed to parse configuration. Error %d.\n", error));
1805
1806 if (ini_config_error_count(ini_config)) {
1807 INIOUT(printf("Errors detected while parsing: %s\n",
1808 ini_config_get_filename(file_ctx)));
1809 ini_config_get_errors(ini_config, &error_list);
1810 INIOUT(ini_config_print_errors(stdout, error_list));
1811 ini_config_free_errors(error_list);
1812 }
1813 /* We do not return here intentionally */
1814 }
1815
1816 ini_config_file_destroy(file_ctx);
1817
1818 INIOUT(printf("Negtive test - trying to get non"
1819 " existing key-value pair.\n"));
1820
1821 /* Negative test */
1822 vo = NULL;
1823 error = ini_get_config_valueobj("monitor1",
1824 "description1",
1825 ini_config,
1826 INI_GET_FIRST_VALUE,
1827 &vo);
1828 if (error) {
1829 printf("Expected success but got error! %d\n", error);
1830 ini_config_destroy(ini_config);
1831 return error;
1832 }
1833
1834 /* Values should not be found */
1835 if (vo != NULL) {
1836 printf("Expected NULL but got something else!\n");
1837 ini_config_destroy(ini_config);
1838 return -1;
1839 }
1840
1841 /* Another negative test but section exists this time */
1842 vo = NULL;
1843 error = ini_get_config_valueobj("monitor",
1844 "description1",
1845 ini_config,
1846 INI_GET_FIRST_VALUE,
1847 &vo);
1848 if (error) {
1849 printf("Expected success but got error! %d\n", error);
1850 ini_config_destroy(ini_config);
1851 return error;
1852 }
1853
1854 /* Valueobj should not be found */
1855 if(vo != NULL) {
1856 printf("Expected NULL but got something else!\n");
1857 ini_config_destroy(ini_config);
1858 return -1;
1859 }
1860
1861 INIOUT(printf("Trying to get a value.\n"));
1862
1863 /* Positive test */
1864 vo = NULL;
1865 error = ini_get_config_valueobj("monitor",
1866 "description",
1867 ini_config,
1868 INI_GET_FIRST_VALUE,
1869 &vo);
1870 if (error) {
1871 printf("Expected success but got error! %d\n", error);
1872 ini_config_destroy(ini_config);
1873 return error;
1874 }
1875
1876 /* Value should be found */
1877 if (vo == NULL) {
1878 printf("Expected value but got NULL!\n");
1879 ini_config_destroy(ini_config);
1880 return -1;
1881 }
1882
1883 INIOUT(value_print("description", vo));
1884
1885 INIOUT(printf("Get values as string without duplication"
1886 " from the NULL valueobj.\n"));
1887
1888 /* Get a string without duplicication */
1889 /* Negative test */
1890 cstrn = ini_get_const_string_config_value(NULL, NULL);
1891 if (cstrn != NULL) {
1892 printf("Expected error got success.\n");
1893 ini_config_destroy(ini_config);
1894 return -1;
1895 }
1896
1897 INIOUT(printf("Get value as string without duplication"
1898 "from the correct value object.\n"));
1899
1900 /* Now get string from the right value object */
1901 error = 0;
1902 cstr = ini_get_const_string_config_value(vo, &error);
1903 if (error) {
1904 printf("Expected success got error %d.\n", error);
1905 ini_config_destroy(ini_config);
1906 return error;
1907 }
1908
1909 INIOUT(printf("Value: [%s]\n", cstr));
1910
1911 /* Same thing but create a dup */
1912
1913 INIOUT(printf("Get value as string with duplication"
1914 " from correct value object.\n"));
1915
1916 error = 0;
1917 str = ini_get_string_config_value(vo, &error);
1918 if (error) {
1919 printf("Expected success got error %d.\n", error);
1920 ini_config_destroy(ini_config);
1921 return error;
1922 }
1923
1924 INIOUT(printf("Value: [%s]\n", str));
1925 free(str);
1926
1927
1928 /* Get a badly formated number */
1929 INIOUT(printf("Convert value to number with strict conversion.\n"));
1930
1931 vo = NULL;
1932 error = ini_get_config_valueobj("monitor",
1933 "bad_number",
1934 ini_config,
1935 INI_GET_FIRST_VALUE,
1936 &vo);
1937 if (error) {
1938 printf("Expected success but got error! %d\n", error);
1939 ini_config_destroy(ini_config);
1940 return error;
1941 }
1942
1943 /* Value should be found */
1944 if (vo == NULL) {
1945 printf("Expected value but got something NULL!\n");
1946 ini_config_destroy(ini_config);
1947 return -1;
1948 }
1949
1950 INIOUT(value_print("bad_number", vo));
1951
1952 /* Now try to get value in different ways */
1953 error = 0;
1954 number = ini_get_int_config_value(vo, 1, 10, &error);
1955 if (error) {
1956 /* We expected error in this case */
1957 INIOUT(printf("Expected error.\n"));
1958 if(number != 10) {
1959 printf("It failed to set default value.\n");
1960 ini_config_destroy(ini_config);
1961 return -1;
1962 }
1963 }
1964 else {
1965 printf("Expected error got success.\n");
1966 ini_config_destroy(ini_config);
1967 return -1;
1968 }
1969
1970 INIOUT(printf("Convert value to number without strict conversion.\n"));
1971
1972 error = 0;
1973 number = ini_get_int_config_value(vo, 0, 10, &error);
1974 if (error) {
1975 printf("Did not expect error.\n");
1976 ini_config_destroy(ini_config);
1977 return error;
1978 }
1979
1980 if (number != 5) {
1981 printf("We expected that the conversion will return 5.\n");
1982 ini_config_destroy(ini_config);
1983 return -1;
1984 }
1985
1986 /* Get real integer */
1987
1988 INIOUT(printf("Fetch another value from section \"domains/LOCAL\""
1989 " named \"enumerate\".\n"));
1990
1991 vo = NULL;
1992 error = ini_get_config_valueobj("domains/LOCAL",
1993 "enumerate",
1994 ini_config,
1995 INI_GET_FIRST_VALUE,
1996 &vo);
1997 if (error) {
1998 printf("Expected success but got error! %d\n", error);
1999 ini_config_destroy(ini_config);
2000 return error;
2001 }
2002
2003 /* Value should be found */
2004 if (vo == NULL) {
2005 printf("Expected success but got NULL.\n");
2006 ini_config_destroy(ini_config);
2007 return -1;
2008 }
2009
2010 INIOUT(printf("Convert value to integer.\n"));
2011
2012 /* Take number out of it */
2013 error = 0;
2014 number = ini_get_int_config_value(vo, 1, 100, &error);
2015 if (error) {
2016 printf("Did not expect error. Got %d\n", error);
2017 ini_config_destroy(ini_config);
2018 return error;
2019 }
2020
2021 /* It is 3 in the file */
2022 if (number != 3) {
2023 printf("We expected that the conversion will return 3.\n");
2024 ini_config_destroy(ini_config);
2025 return -1;
2026 }
2027
2028 INIOUT(printf("Expected 3 got %d\n", number));
2029
2030 INIOUT(printf("Convert value to long.\n"));
2031
2032 /* Take number out of it */
2033 error = 0;
2034 number_long = ini_get_long_config_value(vo, 1, 100, &error);
2035 if (error) {
2036 printf("Did not expect error. Got %d\n", error);
2037 ini_config_destroy(ini_config);
2038 return error;
2039 }
2040
2041 /* It is 3 in the file */
2042 if (number_long != 3) {
2043 printf("We expected that the conversion will return 3.\n");
2044 ini_config_destroy(ini_config);
2045 return -1;
2046 }
2047
2048 INIOUT(printf("Expected 3 got %ld\n", number_long));
2049
2050 INIOUT(printf("Convert value to unsigned.\n"));
2051
2052 /* Take number out of it */
2053 error = 0;
2054 number_unsigned = ini_get_unsigned_config_value(vo, 1, 100, &error);
2055 if (error) {
2056 printf("Did not expect error. Got %d\n", error);
2057 ini_config_destroy(ini_config);
2058 return error;
2059 }
2060
2061 /* It is 3 in the file */
2062 if (number_unsigned != 3) {
2063 printf("We expected that the conversion will return 3.\n");
2064 ini_config_destroy(ini_config);
2065 return -1;
2066 }
2067
2068 INIOUT(printf("Expected 3 got %d\n", number_unsigned));
2069
2070 INIOUT(printf("Convert value to unsigned long.\n"));
2071
2072 /* Take number out of it */
2073 error = 0;
2074 number_ulong = ini_get_ulong_config_value(vo, 1, 100, &error);
2075 if (error) {
2076 printf("Did not expect error. Got %d\n", error);
2077 ini_config_destroy(ini_config);
2078 return error;
2079 }
2080
2081 /* It is 3 in the file */
2082 if (number_ulong != 3) {
2083 printf("We expected that the conversion will return 3.\n");
2084 ini_config_destroy(ini_config);
2085 return -1;
2086 }
2087
2088 INIOUT(printf("Expected 3 got %lu\n", number_ulong));
2089
2090 INIOUT(printf("Convert value to double.\n"));
2091
2092 /* Take number out of it */
2093 error = 0;
2094 number_double = ini_get_double_config_value(vo, 1, 100., &error);
2095 if (error) {
2096 printf("Did not expect error. Got %d\n", error);
2097 ini_config_destroy(ini_config);
2098 return error;
2099 }
2100
2101 /* It is 3 in the file */
2102 if (number_double != 3.) {
2103 printf("We expected that the conversion will return 3.\n");
2104 ini_config_destroy(ini_config);
2105 return -1;
2106 }
2107
2108 INIOUT(printf("Expected 3 got %e\n", number_double));
2109
2110 INIOUT(printf("Convert value to bool.\n"));
2111
2112 /* Take number out of it */
2113 error = 0;
2114 logical = ini_get_bool_config_value(vo, 1, &error);
2115 if (!error) {
2116 printf("Expect error. Got success. Value %d\n", (int) logical);
2117 ini_config_destroy(ini_config);
2118 return -1;
2119 }
2120
2121 /* Get real bool values and convert it */
2122 INIOUT(printf("Get real bool value \"legacy\" and convert it.\n"));
2123
2124 vo = NULL;
2125 error = ini_get_config_valueobj("domains/LOCAL",
2126 "legacy",
2127 ini_config,
2128 INI_GET_FIRST_VALUE,
2129 &vo);
2130 if (error) {
2131 printf("Expected success but got error! %d\n",error);
2132 ini_config_destroy(ini_config);
2133 return error;
2134 }
2135
2136 /* Value should be found */
2137 if (vo == NULL) {
2138 printf("Expected success but got NULL.\n");
2139 ini_config_destroy(ini_config);
2140 return -1;
2141 }
2142
2143 INIOUT(printf("Convert values to bool.\n"));
2144
2145 error = 0;
2146 logical = ini_get_bool_config_value(vo, 1, &error);
2147 if (error) {
2148 printf("Expect success got error %d.\n", error);
2149 ini_config_destroy(ini_config);
2150 return error;
2151 }
2152
2153 if (logical) {
2154 printf("Expected false but got true - bad.\n");
2155 return -1;
2156 }
2157
2158 INIOUT(printf("In the files it is FALSE so we got false.\n"));
2159
2160 INIOUT(printf("Get binary value\n"));
2161
2162 vo = NULL;
2163 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2164 "binary_test",
2165 ini_config,
2166 INI_GET_FIRST_VALUE,
2167 &vo);
2168 if (error) {
2169 printf("Expected success but got error! %d\n", error);
2170 ini_config_destroy(ini_config);
2171 return error;
2172 }
2173
2174 /* Value should be found */
2175 if (vo == NULL) {
2176 printf("Expected success but got NULL.\n");
2177 ini_config_destroy(ini_config);
2178 return -1;
2179 }
2180
2181 INIOUT(value_print("binary_test", vo));
2182
2183 error = 0;
2184 binary = ini_get_bin_config_value(vo, &length, &error);
2185 if (error) {
2186 printf("Expect success got error %d.\n", error);
2187 ini_config_destroy(ini_config);
2188 return error;
2189 }
2190
2191 INIOUT(printf("Binary value (expect 123) = "));
2192 INIOUT(for (i = 0; i < length; i++) {
2193 printf("%d",*((unsigned char*)(binary) + i));
2194 if (*((unsigned char*)(binary) + i) != (i + 1)) bad_val = 1;
2195 });
2196 INIOUT(printf("\n"));
2197
2198 ini_free_bin_config_value(binary);
2199
2200 if (bad_val) {
2201 printf("Unexpected binary value.\n");
2202 ini_config_destroy(ini_config);
2203 return -1;
2204 }
2205
2206 INIOUT(printf("Get another binary value\n"));
2207
2208 bad_val = 0;
2209 vo = NULL;
2210 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2211 "binary_test_two",
2212 ini_config,
2213 INI_GET_FIRST_VALUE,
2214 &vo);
2215 if (error) {
2216 printf("Expected success but got error! %d\n", error);
2217 ini_config_destroy(ini_config);
2218 return error;
2219 }
2220
2221 /* Value should be found */
2222 if (vo == NULL) {
2223 printf("Expected success but got NULL.\n");
2224 ini_config_destroy(ini_config);
2225 return -1;
2226 }
2227
2228 INIOUT(value_print("binary_test_two", vo));
2229
2230 error = 0;
2231 binary = ini_get_bin_config_value(vo, &length, &error);
2232 if (error) {
2233 printf("Expect success got error %d.\n", error);
2234 ini_config_destroy(ini_config);
2235 return error;
2236 }
2237
2238 INIOUT(printf("Binary value (expect abc) = "));
2239 INIOUT(for (i = 0; i < length; i++) {
2240 printf("%x",*((unsigned char*)(binary) + i));
2241 if (*((unsigned char*)(binary) + i) - 10 != i) bad_val = 1;
2242 });
2243 INIOUT(printf("\n"));
2244
2245 ini_free_bin_config_value(binary);
2246
2247 if (bad_val) {
2248 printf("Unexpected binary value.\n");
2249 ini_config_destroy(ini_config);
2250 return -1;
2251 }
2252
2253 INIOUT(printf("Get string array value\n"));
2254
2255 vo = NULL;
2256 error = ini_get_config_valueobj("domains",
2257 "domainsorder",
2258 ini_config,
2259 INI_GET_FIRST_VALUE,
2260 &vo);
2261 if(error) {
2262 printf("Expected success but got error! %d\n",error);
2263 ini_config_destroy(ini_config);
2264 return error;
2265 }
2266
2267 /* Value should be found */
2268 if (vo == NULL) {
2269 printf("Expected success but got NULL.\n");
2270 ini_config_destroy(ini_config);
2271 return -1;
2272 }
2273
2274 INIOUT(value_print("domainsorder", vo));
2275
2276 INIOUT(printf("Get str array without size.\n"));
2277
2278 error = 0;
2279 strarray = ini_get_string_config_array(vo, ",", NULL, &error);
2280 if (error) {
2281 printf("Expect success got error %d.\n", error);
2282 ini_config_destroy(ini_config);
2283 return error;
2284 }
2285
2286 /* Can be used with this cycle */
2287 strptr = strarray;
2288 while (*strptr != NULL) {
2289 INIOUT(printf("[%s]\n",*strptr));
2290 strptr++;
2291 }
2292
2293 ini_free_string_config_array(strarray);
2294
2295 INIOUT(printf("Get raw str array without size.\n"));
2296
2297 error = 0;
2298 strarray = ini_get_raw_string_config_array(vo, ",", NULL, &error);
2299 if (error) {
2300 printf("Expect success got error %d.\n", error);
2301 ini_config_destroy(ini_config);
2302 return error;
2303 }
2304
2305 /* Can be used with this cycle */
2306 strptr = strarray;
2307 while (*strptr != NULL) {
2308 INIOUT(printf("[%s]\n",*strptr));
2309 strptr++;
2310 }
2311
2312 ini_free_string_config_array(strarray);
2313
2314 INIOUT(printf("Get str array with size.\n"));
2315
2316 error = 0;
2317 size = 0;
2318 strarray = ini_get_string_config_array(vo, ",", &size, &error);
2319 if (error) {
2320 printf("Expect success got error %d.\n", error);
2321 ini_config_destroy(ini_config);
2322 return error;
2323 }
2324
2325 /* Can be used with this cycle */
2326 INIOUT(for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)));
2327
2328 ini_free_string_config_array(strarray);
2329
2330 INIOUT(printf("Get raw str array with size.\n"));
2331
2332 error = 0;
2333 size = 0;
2334 strarray = ini_get_raw_string_config_array(vo, ",", &size, &error);
2335 if (error) {
2336 printf("Expect success got error %d.\n", error);
2337 ini_config_destroy(ini_config);
2338 return error;
2339 }
2340
2341 /* Can be used with this cycle */
2342 INIOUT(for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)));
2343
2344 ini_free_string_config_array(strarray);
2345
2346 /**********************************************************/
2347
2348 INIOUT(printf("Get bad string array \n"));
2349
2350 vo = NULL;
2351 error = ini_get_config_valueobj("domains",
2352 "badarray",
2353 ini_config,
2354 INI_GET_FIRST_VALUE,
2355 &vo);
2356 if(error) {
2357 printf("Expected success but got error! %d\n",error);
2358 ini_config_destroy(ini_config);
2359 return error;
2360 }
2361
2362 /* Value should be found */
2363 if (vo == NULL) {
2364 printf("Expected success but got NULL.\n");
2365 ini_config_destroy(ini_config);
2366 return -1;
2367 }
2368
2369 INIOUT(value_print("badarray", vo));
2370
2371 INIOUT(printf("Get bad str array without size.\n"));
2372
2373 error = 0;
2374 strarray = ini_get_string_config_array(vo, ",", NULL, &error);
2375 if (error) {
2376 printf("Expect success got error %d.\n", error);
2377 ini_config_destroy(ini_config);
2378 return error;
2379 }
2380
2381 /* Can be used with this cycle */
2382 strptr = strarray;
2383 while (*strptr != NULL) {
2384 INIOUT(printf("[%s]\n",*strptr));
2385 strptr++;
2386 }
2387
2388 ini_free_string_config_array(strarray);
2389
2390 /**********************************************************/
2391
2392 for (i = 0; i< NUM_TESTS; i++) {
2393 error = test_one_array(ini_config,
2394 array_test[i].section,
2395 array_test[i].value,
2396 array_test[i].raw,
2397 array_test[i].expect,
2398 array_test[i].sep,
2399 array_test[i].message);
2400 if (error) {
2401 /* Message is printed inside function */
2402 ini_config_destroy(ini_config);
2403 return error;
2404 }
2405 }
2406
2407 /**********************************************************/
2408
2409 INIOUT(printf("Get long array value\n"));
2410
2411 vo = NULL;
2412 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2413 "long_array",
2414 ini_config,
2415 INI_GET_FIRST_VALUE,
2416 &vo);
2417 if(error) {
2418 printf("Expected success but got error! %d\n", error);
2419 ini_config_destroy(ini_config);
2420 return error;
2421 }
2422
2423 /* Value should be found */
2424 if (vo == NULL) {
2425 printf("Expected success but got NULL.\n");
2426 ini_config_destroy(ini_config);
2427 return -1;
2428 }
2429
2430 INIOUT(value_print("long_array", vo));
2431
2432 error = 0;
2433 size = 0; /* Here size is not optional!!! */
2434 array = ini_get_long_config_array(vo, &size, &error);
2435 if(error) {
2436 printf("Expect success got error %d.\n", error);
2437 ini_config_destroy(ini_config);
2438 return error;
2439 }
2440
2441 /* Can be used with this cycle */
2442 INIOUT(for (i=0;i<size;i++) printf("%ld\n", *(array + i)));
2443
2444 ini_free_long_config_array(array);
2445
2446 INIOUT(printf("Get double array value\n"));
2447
2448 vo = NULL;
2449 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2450 "double_array",
2451 ini_config,
2452 INI_GET_FIRST_VALUE,
2453 &vo);
2454 if (error) {
2455 printf("Expected success but got error! %d\n", error);
2456 ini_config_destroy(ini_config);
2457 return error;
2458 }
2459
2460 /* Values should be found */
2461 if (vo == NULL) {
2462 printf("Expected success but got NULL.\n");
2463 ini_config_destroy(ini_config);
2464 return -1;
2465 }
2466
2467 INIOUT(value_print("double_array", vo));
2468
2469 error = 0;
2470 size = 0; /* Here size is not optional!!! */
2471 darray = ini_get_double_config_array(vo, &size, &error);
2472 if (error) {
2473 printf("Expect success got error %d.\n", error);
2474 ini_config_destroy(ini_config);
2475 return error;
2476 }
2477
2478 /* Can be used with this cycle */
2479 INIOUT(for (i=0;i<size;i++) printf("%.4f\n", darray[i]));
2480
2481 ini_free_double_config_array(darray);
2482
2483 INIOUT(printf("\n\nSection list - no size\n"));
2484
2485 /* Do not care about the error or size */
2486 prop_array = ini_get_section_list(ini_config, NULL, NULL);
2487 if (prop_array == NULL) {
2488 printf("Expect success got error.\n");
2489 ini_config_destroy(ini_config);
2490 return -1;
2491 }
2492
2493 i = 0;
2494 INIOUT(while (prop_array[i]) {
2495 printf("Section: [%s]\n", prop_array[i]);
2496 i++;
2497 });
2498
2499 ini_free_section_list(prop_array);
2500
2501 INIOUT(printf("\n\nSection list - with size\n"));
2502
2503 /* Do not care about the error or size */
2504 prop_array = ini_get_section_list(ini_config, &size, NULL);
2505 if (prop_array == NULL) {
2506 printf("Expect success got error.\n");
2507 ini_config_destroy(ini_config);
2508 return -1;
2509 }
2510
2511 INIOUT(for (i=0;i<size;i++) printf("Section: [%s]\n", prop_array[i]));
2512 ini_free_section_list(prop_array);
2513
2514 INIOUT(printf("\n\nAttributes in the section - with size and error\n"));
2515
2516 /* Do not care about the error or size */
2517 prop_array = ini_get_attribute_list(ini_config,
2518 "domains/EXAMPLE.COM",
2519 &size,
2520 &error);
2521 if (prop_array == NULL) {
2522 printf("Expect success got error.\n");
2523 ini_config_destroy(ini_config);
2524 return -1;
2525 }
2526
2527 INIOUT(for (i=0;i<size;i++) printf("Attribute: [%s]\n", prop_array[i]));
2528 ini_free_attribute_list(prop_array);
2529
2530
2531 /***************************************/
2532 /* Test special types */
2533 /***************************************/
2534 INIOUT(printf("Test int32_t\n"));
2535
2536 vo = NULL;
2537 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2538 "int32_t",
2539 ini_config,
2540 INI_GET_FIRST_VALUE,
2541 &vo);
2542 if (error) {
2543 printf("Expected success but got error! %d\n", error);
2544 ini_config_destroy(ini_config);
2545 return error;
2546 }
2547
2548 /* Value should be found */
2549 if (vo == NULL) {
2550 printf("Expected success but got NULL.\n");
2551 ini_config_destroy(ini_config);
2552 return -1;
2553 }
2554
2555 INIOUT(value_print("int32_t", vo));
2556
2557 error = 0;
2558 val_int32 = ini_get_int32_config_value(vo, 1, 0, &error);
2559 if (error) {
2560 printf("Expect success got error %d.\n", error);
2561 ini_config_destroy(ini_config);
2562 return error;
2563 }
2564
2565 INIOUT(printf("Value: %d\n", val_int32));
2566
2567 /***************************************/
2568
2569 INIOUT(printf("Test uint32_t\n"));
2570
2571 vo = NULL;
2572 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2573 "uint32_t",
2574 ini_config,
2575 INI_GET_FIRST_VALUE,
2576 &vo);
2577 if (error) {
2578 printf("Expected success but got error! %d\n", error);
2579 ini_config_destroy(ini_config);
2580 return error;
2581 }
2582
2583 /* Valu should be found */
2584 if (vo == NULL) {
2585 printf("Expected success but got NULL.\n");
2586 ini_config_destroy(ini_config);
2587 return -1;
2588 }
2589
2590 INIOUT(value_print("uint32_t", vo));
2591
2592 error = 0;
2593 val_uint32 = ini_get_uint32_config_value(vo, 1, 0, &error);
2594 if (error) {
2595 printf("Expect success got error %d.\n", error);
2596 ini_config_destroy(ini_config);
2597 return error;
2598 }
2599
2600 INIOUT(printf("Value: %u\n", val_uint32));
2601
2602 /***************************************/
2603
2604 INIOUT(printf("Test int64_t\n"));
2605
2606 vo = NULL;
2607 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2608 "int64_t",
2609 ini_config,
2610 INI_GET_FIRST_VALUE,
2611 &vo);
2612 if (error) {
2613 printf("Expected success but got error! %d\n", error);
2614 ini_config_destroy(ini_config);
2615 return error;
2616 }
2617
2618 /* Value should be found */
2619 if (vo == NULL) {
2620 printf("Expected success but got NULL.\n");
2621 ini_config_destroy(ini_config);
2622 return -1;
2623 }
2624
2625 INIOUT(value_print("int64_t", vo));
2626
2627 error = 0;
2628 val_int64 = ini_get_int64_config_value(vo, 1, 0, &error);
2629 if (error) {
2630 printf("Expect success got error %d.\n", error);
2631 ini_config_destroy(ini_config);
2632 return error;
2633 }
2634
2635 INIOUT(printf("Value: %lld\n", (long long)val_int64));
2636
2637 /***************************************/
2638
2639 INIOUT(printf("Test uint32_t\n"));
2640
2641 vo = NULL;
2642 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2643 "uint64_t",
2644 ini_config,
2645 INI_GET_FIRST_VALUE,
2646 &vo);
2647 if (error) {
2648 printf("Expected success but got error! %d\n", error);
2649 ini_config_destroy(ini_config);
2650 return error;
2651 }
2652
2653 /* Value should be found */
2654 if (vo == NULL) {
2655 printf("Expected success but got NULL.\n");
2656 ini_config_destroy(ini_config);
2657 return -1;
2658 }
2659
2660 INIOUT(value_print("uint64_t", vo));
2661
2662 error = 0;
2663 val_uint64 = ini_get_uint64_config_value(vo, 1, 0, &error);
2664 if (error) {
2665 printf("Expect success got error %d.\n", error);
2666 ini_config_destroy(ini_config);
2667 return error;
2668 }
2669
2670 INIOUT(printf("Value: %llu\n", (unsigned long long)val_uint64));
2671
2672 /***************************************/
2673
2674 INIOUT(printf("Get empty array value object\n"));
2675
2676 vo = NULL;
2677 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2678 "empty_value",
2679 ini_config,
2680 INI_GET_FIRST_VALUE,
2681 &vo);
2682 if(error) {
2683 printf("Expected success but got error! %d\n", error);
2684 ini_config_destroy(ini_config);
2685 return error;
2686 }
2687
2688 /* Value should be found */
2689 if (vo == NULL) {
2690 printf("Expected success but got NULL.\n");
2691 ini_config_destroy(ini_config);
2692 return -1;
2693 }
2694
2695 INIOUT(value_print("empty_value", vo));
2696
2697 error = 0;
2698 size = 0; /* Here size is not optional!!! */
2699 strarray = ini_get_string_config_array(vo, ",", &size, &error);
2700 if(error) {
2701 printf("Expect success got error %d.\n", error);
2702 ini_config_destroy(ini_config);
2703 return error;
2704 }
2705
2706 if (size != 0) {
2707 for (i=0; i<size; i++) printf("%s\n", *(strarray + i));
2708 printf("Expected size=0, got size=%d\n", size);
2709 ini_free_string_config_array(strarray);
2710 ini_config_destroy(ini_config);
2711 return -1;
2712 }
2713
2714 ini_free_string_config_array(strarray);
2715
2716 /***************************************/
2717
2718 INIOUT(printf("\nGet last value\n"));
2719
2720 vo = NULL;
2721 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2722 "server",
2723 ini_config,
2724 INI_GET_LAST_VALUE,
2725 &vo);
2726 if(error) {
2727 printf("Expected success but got error! %d\n", error);
2728 ini_config_destroy(ini_config);
2729 return error;
2730 }
2731
2732 /* Value should be found */
2733 if (vo == NULL) {
2734 printf("Expected success but got NULL.\n");
2735 ini_config_destroy(ini_config);
2736 return -1;
2737 }
2738
2739 INIOUT(value_print("server", vo));
2740
2741 /***************************************/
2742
2743 INIOUT(printf("\nGet sequence of the multi-value keys\n"));
2744
2745 vo = NULL;
2746 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2747 "server",
2748 ini_config,
2749 INI_GET_FIRST_VALUE,
2750 &vo);
2751 if(error) {
2752 printf("Expected success but got error! %d\n", error);
2753 ini_config_destroy(ini_config);
2754 return error;
2755 }
2756
2757 /* Value should be found */
2758 if (vo == NULL) {
2759 printf("Expected success but got NULL.\n");
2760 ini_config_destroy(ini_config);
2761 return -1;
2762 }
2763
2764 INIOUT(value_print("server", vo));
2765
2766
2767 do {
2768
2769 vo = NULL;
2770 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2771 "server",
2772 ini_config,
2773 INI_GET_NEXT_VALUE,
2774 &vo);
2775 if(error) {
2776 printf("Expected success but got error! %d\n", error);
2777 ini_config_destroy(ini_config);
2778 return error;
2779 }
2780
2781 if (vo == NULL) break;
2782
2783 INIOUT(value_print("server", vo));
2784 }
2785 while(1);
2786
2787 /***************************************/
2788
2789 INIOUT(printf("\nGet multi-value keys without prefetching\n"));
2790
2791 do {
2792
2793 vo = NULL;
2794 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2795 "server",
2796 ini_config,
2797 INI_GET_NEXT_VALUE,
2798 &vo);
2799 if(error) {
2800 printf("Expected success but got error! %d\n", error);
2801 ini_config_destroy(ini_config);
2802 return error;
2803 }
2804
2805 if (vo == NULL) break;
2806
2807 INIOUT(value_print("server", vo));
2808 }
2809 while(1);
2810
2811 /***************************************/
2812
2813 INIOUT(printf("\nGet multi-value keys with key interrupt\n"));
2814
2815 i = 0;
2816
2817 vo = NULL;
2818 do {
2819
2820 vo = NULL;
2821 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2822 "server",
2823 ini_config,
2824 INI_GET_NEXT_VALUE,
2825 &vo);
2826 if(error) {
2827 printf("Expected success but got error! %d\n", error);
2828 ini_config_destroy(ini_config);
2829 return error;
2830 }
2831
2832 if (vo == NULL) break;
2833
2834 INIOUT(value_print("server", vo));
2835 i++;
2836
2837 if (i==2) {
2838 vo = NULL;
2839 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2840 "empty_value",
2841 ini_config,
2842 INI_GET_NEXT_VALUE,
2843 &vo);
2844 if(error) {
2845 printf("Expected success but got error! %d\n", error);
2846 ini_config_destroy(ini_config);
2847 return error;
2848 }
2849 }
2850 }
2851 while(1);
2852
2853 if (i != 6) {
2854 printf("Expected 6 iterations got %d\n", i);
2855 ini_config_destroy(ini_config);
2856 return -1;
2857 }
2858
2859 /***************************************/
2860
2861 INIOUT(printf("\nGet multi-value keys with section interrupt\n"));
2862
2863 i = 0;
2864
2865 vo = NULL;
2866 do {
2867
2868 vo = NULL;
2869 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
2870 "server",
2871 ini_config,
2872 INI_GET_NEXT_VALUE,
2873 &vo);
2874 if(error) {
2875 printf("Expected success but got error! %d\n", error);
2876 ini_config_destroy(ini_config);
2877 return error;
2878 }
2879
2880 if (vo == NULL) break;
2881
2882 INIOUT(value_print("server", vo));
2883 i++;
2884
2885 if (i==2) {
2886 vo = NULL;
2887 error = ini_get_config_valueobj("domains",
2888 "badarray",
2889 ini_config,
2890 INI_GET_NEXT_VALUE,
2891 &vo);
2892 if(error) {
2893 printf("Expected success but got error! %d\n", error);
2894 ini_config_destroy(ini_config);
2895 return error;
2896 }
2897 }
2898 }
2899 while(1);
2900
2901 if (i != 6) {
2902 printf("Expected 6 iterations got %d\n", i);
2903 ini_config_destroy(ini_config);
2904 return -1;
2905 }
2906
2907 ini_config_destroy(ini_config);
2908
2909 INIOUT(printf("\n<==== GET TEST END =====>\n\n"));
2910 return EOK;
2911 }
2912
2913 static int space_test(void)
2914 {
2915
2916 int error;
2917 struct ini_cfgfile *file_ctx = NULL;
2918 struct ini_cfgobj *ini_config = NULL;
2919 char **error_list = NULL;
2920 char infile[PATH_MAX];
2921 char *srcdir = NULL;
2922 unsigned errnum;
2923 int i;
2924 uint32_t flags[] = { INI_PARSE_NOWRAP,
2925 INI_PARSE_NOWRAP |
2926 INI_PARSE_NOSPACE,
2927 INI_PARSE_NOWRAP |
2928 INI_PARSE_NOTAB,
2929 INI_PARSE_NOWRAP |
2930 INI_PARSE_NOSPACE |
2931 INI_PARSE_NOTAB };
2932
2933 INIOUT(printf("\n\n<==== SPACE TEST START =====>\n"));
2934
2935 srcdir = getenv("srcdir");
2936 snprintf(infile, PATH_MAX, "%s/ini/ini.d/space.conf",
2937 (srcdir == NULL) ? "." : srcdir);
2938
2939
2940 for (i = 0; i < 4; i++ ) {
2941
2942 INIOUT(printf("Reading file %s\n", infile));
2943 error = ini_config_file_open(infile,
2944 0,
2945 &file_ctx);
2946 if (error) {
2947 printf("Failed to open file for reading. Error %d.\n", error);
2948 return error;
2949 }
2950
2951 INIOUT(printf("Creating configuration object\n"));
2952 error = ini_config_create(&ini_config);
2953 if (error) {
2954 printf("Failed to create object. Error %d.\n", error);
2955 ini_config_file_destroy(file_ctx);
2956 return error;
2957 }
2958 INIOUT(printf("Parsing\n"));
2959 error = ini_config_parse(file_ctx,
2960 INI_STOP_ON_NONE,
2961 0,
2962 flags[i],
2963 ini_config);
2964 if (error) {
2965 INIOUT(printf("Failed to parse configuration. Error %d.\n", error));
2966
2967 errnum = ini_config_error_count(ini_config);
2968 if (errnum) {
2969 INIOUT(printf("Errors detected while parsing: %s\n",
2970 ini_config_get_filename(file_ctx)));
2971 ini_config_get_errors(ini_config, &error_list);
2972 INIOUT(ini_config_print_errors(stdout, error_list));
2973 ini_config_free_errors(error_list);
2974 }
2975 if (((i == 0) && (errnum != 0)) ||
2976 ((i == 1) && (errnum != 3)) ||
2977 ((i == 2) && (errnum != 3)) ||
2978 ((i == 3) && (errnum != 4))) {
2979 printf("Failed to open file for reading. Error %d.\n", error);
2980 ini_config_file_destroy(file_ctx);
2981 ini_config_destroy(ini_config);
2982 return -1;
2983 }
2984 /* We do not return here intentionally */
2985 }
2986
2987 INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
2988 ini_config_destroy(ini_config);
2989 ini_config_file_destroy(file_ctx);
2990 }
2991
2992 INIOUT(printf("\n<==== SPACE TEST END =====>\n\n"));
2993 return EOK;
2994 }
2995
2996
2997 static int trim_test(void)
2998 {
2999 int error;
3000 struct ini_cfgfile *file_ctx = NULL;
3001 struct ini_cfgobj *ini_config = NULL;
3002 char **error_list = NULL;
3003 char infile[PATH_MAX];
3004 char *srcdir = NULL;
3005 const char *value;
3006 struct value_obj *vo = NULL;
3007
3008 INIOUT(printf("\n\n<==== TRIM TEST START =====>\n"));
3009
3010 srcdir = getenv("srcdir");
3011 snprintf(infile, PATH_MAX, "%s/ini/ini.d/real.conf",
3012 (srcdir == NULL) ? "." : srcdir);
3013
3014
3015 INIOUT(printf("Reading file %s\n", infile));
3016 error = ini_config_file_open(infile,
3017 0,
3018 &file_ctx);
3019 if (error) {
3020 printf("Failed to open file for reading. Error %d.\n", error);
3021 return error;
3022 }
3023
3024 INIOUT(printf("Creating configuration object\n"));
3025 error = ini_config_create(&ini_config);
3026 if (error) {
3027 printf("Failed to create object. Error %d.\n", error);
3028 ini_config_file_destroy(file_ctx);
3029 return error;
3030 }
3031 INIOUT(printf("Parsing\n"));
3032 error = ini_config_parse(file_ctx,
3033 INI_STOP_ON_NONE,
3034 0,
3035 0,
3036 ini_config);
3037 if (error) {
3038 INIOUT(printf("Failed to parse configuration. "
3039 "Error %d.\n", error));
3040
3041 if (ini_config_error_count(ini_config)) {
3042 INIOUT(printf("Errors detected while parsing: %s\n",
3043 ini_config_get_filename(file_ctx)));
3044 ini_config_get_errors(ini_config, &error_list);
3045 INIOUT(ini_config_print_errors(stdout, error_list));
3046 ini_config_free_errors(error_list);
3047 }
3048 ini_config_file_destroy(file_ctx);
3049 return error;
3050 }
3051
3052 INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
3053 ini_config_file_destroy(file_ctx);
3054
3055 vo = NULL;
3056 error = ini_get_config_valueobj("domains/EXAMPLE.COM",
3057 "description",
3058 ini_config,
3059 INI_GET_FIRST_VALUE,
3060 &vo);
3061 if(error) {
3062 printf("Expected success but got error! %d\n",error);
3063 ini_config_destroy(ini_config);
3064 return error;
3065 }
3066
3067 /* Value should be found */
3068 if (vo == NULL) {
3069 printf("Expected success but got NULL.\n");
3070 ini_config_destroy(ini_config);
3071 return -1;
3072 }
3073
3074 value = ini_get_const_string_config_value(vo, NULL);
3075
3076 if (value == NULL) {
3077 printf("No value.\n");
3078 ini_config_destroy(ini_config);
3079 return -1;
3080 }
3081
3082 if(value[strlen(value) - 1] == ' ') {
3083 printf("Trailing space is not trimmed.\n");
3084 ini_config_destroy(ini_config);
3085 return -1;
3086 }
3087
3088 INIOUT(printf("[%s]\n", value));
3089
3090 ini_config_destroy(ini_config);
3091
3092 INIOUT(printf("\n<==== TRIM TEST END =====>\n\n"));
3093 return EOK;
3094 }
3095
3096 static int comment_test(void)
3097 {
3098 int error;
3099 struct ini_cfgfile *file_ctx = NULL;
3100 struct ini_cfgobj *ini_config = NULL;
3101 char **error_list = NULL;
3102 char infile[PATH_MAX];
3103 char *srcdir = NULL;
3104 int err_count = 0;
3105
3106 INIOUT(printf("\n\n<==== COMMENT TEST START =====>\n"));
3107
3108 srcdir = getenv("srcdir");
3109 snprintf(infile, PATH_MAX, "%s/ini/ini.d/comment.conf",
3110 (srcdir == NULL) ? "." : srcdir);
3111
3112
3113 INIOUT(printf("Reading file %s\n", infile));
3114 error = ini_config_file_open(infile,
3115 0,
3116 &file_ctx);
3117 if (error) {
3118 printf("Failed to open file for reading. Error %d.\n", error);
3119 return error;
3120 }
3121
3122 INIOUT(printf("Creating configuration object\n"));
3123 error = ini_config_create(&ini_config);
3124 if (error) {
3125 printf("Failed to create object. Error %d.\n", error);
3126 ini_config_file_destroy(file_ctx);
3127 return error;
3128 }
3129 INIOUT(printf("Parsing\n"));
3130 error = ini_config_parse(file_ctx,
3131 INI_STOP_ON_NONE,
3132 0,
3133 0,
3134 ini_config);
3135 if (error) {
3136 INIOUT(printf("Failed to parse configuration. "
3137 "Error %d.\n", error));
3138 err_count = ini_config_error_count(ini_config);
3139 if (err_count) {
3140 INIOUT(printf("Errors detected while parsing: %s\n",
3141 ini_config_get_filename(file_ctx)));
3142 ini_config_get_errors(ini_config, &error_list);
3143 INIOUT(ini_config_print_errors(stdout, error_list));
3144 ini_config_free_errors(error_list);
3145 }
3146 }
3147
3148 INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT));
3149 ini_config_file_destroy(file_ctx);
3150 ini_config_destroy(ini_config);
3151
3152 if(err_count != 4) {
3153 printf("Expected 4 errors got: %d\n", err_count);
3154 return 1;
3155 }
3156
3157 INIOUT(printf("\n<==== COMMENT TEST END =====>\n\n"));
3158 return EOK;
3159 }
3160
3161 static void create_boms(void)
3162 {
3163 FILE *f;
3164
3165 f = fopen("bom2be","wb");
3166 fprintf(f,"%c%c", 0xFE, 0xFF);
3167 fclose(f);
3168 f = fopen("bom2le","wb");
3169 fprintf(f,"%c%c", 0xFF, 0xFE);
3170 fclose(f);
3171 f = fopen("bom4be","wb");
3172 fprintf(f,"%c%c%c%c", 0x00, 0x00, 0xFE, 0xFF);
3173 fclose(f);
3174 f = fopen("bom4le","wb");
3175 fprintf(f,"%c%c%c%c", 0xFF, 0xFE, 0x00, 0x00);
3176 fclose(f);
3177 f = fopen("bom3","wb");
3178 fprintf(f,"%c%c%c", 0xEF, 0xBB, 0xBF);
3179 fclose(f);
3180 }
3181
3182
3183 /* Main function of the unit test */
3184 int main(int argc, char *argv[])
3185 {
3186 int error = 0;
3187 test_fn tests[] = { read_save_test,
3188 read_again_test,
3189 read_mem_test,
3190 merge_values_test,
3191 merge_section_test,
3192 merge_file_test,
3193 startup_test,
3194 reload_test,
3195 get_test,
3196 space_test,
3197 trim_test,
3198 comment_test,
3199 NULL };
3200 test_fn t;
3201 int i = 0;
3202 char *var;
3203
3204 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
3205 else {
3206 var = getenv("COMMON_TEST_VERBOSE");
3207 if (var) verbose = 1;
3208 }
3209
3210 /* Create boms in case we want to create more test files */
3211 create_boms();
3212
3213 INIOUT(printf("Start\n"));
3214
3215 while ((t = tests[i++])) {
3216 error = t();
3217 fflush(NULL);
3218 if (error) {
3219 INIOUT(printf("Failed with error %d!\n", error));
3220 return error;
3221 }
3222 }
3223
3224 INIOUT(printf("Success!\n"));
3225
3226 return 0;
3227 }