Debian Codegraph @

ini/ini_config_ut.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Unit test for the INI library.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2009
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 <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #define TRACE_HOME
31 #include "trace.h"
32 #include "ini_config.h"
33 #include "collection.h"
34 #include "collection_tools.h"
35
36
37 int verbose = 0;
38
39 #define COLOUT(foo) \
40 do { \
41 if (verbose) foo; \
42 } while(0)
43
44
45 static int basic_test(void)
46 {
47 int error;
48 struct collection_item *ini_config = NULL;
49 struct collection_item *error_set = NULL;
50
51 error = config_for_app("test", NULL, NULL,
52 &ini_config, INI_STOP_ON_NONE, &error_set);
53 if (error != EINVAL) {
54 printf("Expected error EINVAL got somethign else: %d\n", error);
55 return EINVAL;
56 }
57
58 error = config_for_app("test", "foo", "bar",
59 &ini_config, INI_STOP_ON_ANY, &error_set);
60 if (error != ENOENT) {
61 printf("Expected error ENOENT got somethign else: %d\n", error);
62 return ENOENT;
63 }
64
65 error = config_for_app("test", "./ini.conf", "./ini.d",
66 &ini_config, INI_STOP_ON_NONE, &error_set);
67 if (error) {
68 printf("Attempt to read configuration returned error: %d\n", error);
69 return error;
70 }
71
72 COLOUT(col_debug_collection(ini_config,COL_TRAVERSE_DEFAULT));
73 COLOUT(col_print_collection(ini_config));
74 COLOUT(col_print_collection(error_set));
75
76 COLOUT(printf("\n\n----------------------\n"));
77 /* Output parsing errors (if any) */
78 COLOUT(print_config_parsing_errors(stdout, error_set));
79 COLOUT(printf("----------------------\n\n\n"));
80
81
82 free_ini_config(ini_config);
83 free_ini_config_errors(error_set);
84 return 0;
85 }
86
87 static int single_file(void)
88 {
89 int error;
90 struct collection_item *ini_config = NULL;
91 struct collection_item *error_set = NULL;
92 struct collection_item *metadata = NULL;
93 uint32_t flags;
94
95 error = config_from_file("test", "./not_exist_ini.conf",
96 &ini_config, INI_STOP_ON_NONE, &error_set);
97 if (error) {
98 COLOUT(printf("Attempt to read configuration returned error: %d."
99 " EXPECTED.\n\n", error));
100 if(error != ENOENT) return error;
101 }
102
103 error = config_from_file("test",
104 "./ini.conf",
105 &ini_config,
106 INI_STOP_ON_NONE,
107 &error_set);
108 if (error) {
109 printf("Attempt to read configuration returned error: %d\n", error);
110 return error;
111 }
112
113 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT));
114 COLOUT(col_print_collection(ini_config));
115 COLOUT(col_print_collection(error_set));
116
117 COLOUT(printf("\n\n----------------------\n"));
118 /* Output parsing errors (if any) */
119 COLOUT(print_file_parsing_errors(stdout, error_set));
120 COLOUT(printf("----------------------\n\n\n"));
121
122
123 free_ini_config(ini_config);
124 free_ini_config_errors(error_set);
125
126 ini_config = NULL;
127 error_set = NULL;
128
129 COLOUT(printf("TEST WITH METADATA NO PARSE\n"));
130 flags = INI_META_SEC_ACCESS_FLAG |
131 INI_META_SEC_ERROR_FLAG |
132 INI_META_ACTION_NOPARSE;
133
134 error = config_from_file_with_metadata("test", "./ini.conf",
135 &ini_config, INI_STOP_ON_NONE,
136 NULL,
137 flags,
138 &metadata);
139 if (error) {
140 printf("Attempt to read configuration returned error: %d\n",error);
141 if (metadata) {
142 printf("\n\nMeta data\n");
143 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT);
144 }
145 free_ini_config_metadata(metadata);
146 return error;
147 }
148
149 if (ini_config) {
150 printf("Expected no config but got some.\n");
151 col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT);
152 free_ini_config(ini_config);
153 printf("\n\nMeta data\n");
154 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT);
155 free_ini_config_metadata(metadata);
156 return EINVAL;
157 }
158
159 COLOUT(printf("\n\nMeta data\n"));
160 COLOUT(col_debug_collection(metadata, COL_TRAVERSE_DEFAULT));
161 free_ini_config_metadata(metadata);
162
163 COLOUT(printf("\n\n----------------------\n"));
164
165 error = config_from_file_with_metadata("test", "./ini.conf",
166 &ini_config, INI_STOP_ON_NONE,
167 &error_set,
168 0,
169 NULL);
170 if (error) {
171 printf("Attempt to read configuration returned error: %d\n",error);
172 print_file_parsing_errors(stdout, error_set);
173 free_ini_config_errors(error_set);
174 return error;
175 }
176
177 COLOUT(printf("\n\n----------------------\n"));
178 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT));
179
180 COLOUT(printf("\n\n----------------------\n"));
181 /* Output parsing errors (if any) */
182 COLOUT(print_file_parsing_errors(stdout, error_set));
183 COLOUT(printf("----------------------\n\n\n"));
184
185
186 free_ini_config(ini_config);
187 free_ini_config_errors(error_set);
188
189 return 0;
190 }
191
192 static int single_fd(void)
193 {
194 int error;
195 struct collection_item *ini_config = NULL;
196 struct collection_item *error_set = NULL;
197 struct collection_item *metadata = NULL;
198 uint32_t flags;
199
200 int fd = open("./ini.conf", O_RDONLY);
201 if (fd < 0) {
202 error = errno;
203 printf("Attempt to read configuration returned error: %d\n", error);
204 return error;
205 }
206
207 error = config_from_fd("test", fd, "./ini.conf", &ini_config,
208 INI_STOP_ON_NONE, &error_set);
209 if (error) {
210 printf("Attempt to read configuration returned error: %d\n",error);
211 return error;
212 }
213
214 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT));
215 COLOUT(col_print_collection(ini_config));
216 COLOUT(col_print_collection(error_set));
217
218 COLOUT(printf("\n\n----------------------\n"));
219 /* Output parsing errors (if any) */
220 COLOUT(print_file_parsing_errors(stdout, error_set));
221 COLOUT(printf("----------------------\n\n\n"));
222
223
224 free_ini_config(ini_config);
225 free_ini_config_errors(error_set);
226 close(fd);
227
228 ini_config = NULL;
229 error_set = NULL;
230
231 COLOUT(printf("TEST WITH FILE FD & META DATA\n"));
232
233 fd = open("./ini.conf", O_RDONLY);
234 if (fd < 0) {
235 error = errno;
236 printf("Attempt to read configuration returned error: %d\n", error);
237 return error;
238 }
239
240 flags = INI_META_SEC_ACCESS_FLAG |
241 INI_META_SEC_ERROR_FLAG |
242 INI_META_ACTION_NOPARSE;
243
244 error = config_from_fd_with_metadata("test", fd,
245 "./ini.conf",
246 &ini_config,
247 INI_STOP_ON_NONE,
248 &error_set,
249 flags,
250 &metadata);
251 if (error) {
252 printf("Attempt to read configuration returned error: %d\n",error);
253 printf("\n\nErrors\n");
254 print_file_parsing_errors(stdout, error_set);
255 free_ini_config_errors(error_set);
256 if (metadata) {
257 printf("\n\nMeta data\n");
258 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT);
259 }
260 free_ini_config_metadata(metadata);
261 return error;
262 }
263
264 if (ini_config) {
265 printf("Expected no config but got some.\n");
266 col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT);
267 free_ini_config(ini_config);
268 return EINVAL;
269 }
270
271
272 COLOUT(printf("\n\nMeta data\n"));
273 COLOUT(col_debug_collection(metadata, COL_TRAVERSE_DEFAULT));
274 free_ini_config_metadata(metadata);
275
276
277 error = config_from_fd_with_metadata("test", fd,
278 "./ini.conf",
279 &ini_config,
280 INI_STOP_ON_NONE,
281 &error_set,
282 0,
283 NULL);
284
285 close(fd);
286
287 if (error) {
288 printf("Attempt to read configuration returned error: %d\n",error);
289 printf("\n\nErrors\n");
290 print_file_parsing_errors(stdout, error_set);
291 free_ini_config_errors(error_set);
292 return error;
293 }
294
295 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT));
296
297 COLOUT(printf("\n\n----------------------\n"));
298 /* Output parsing errors (if any) */
299 COLOUT(print_file_parsing_errors(stdout, error_set));
300 COLOUT(printf("----------------------\n\n\n"));
301
302
303 free_ini_config(ini_config);
304 free_ini_config_errors(error_set);
305
306 return 0;
307 }
308
309 static int negative_test(void)
310 {
311 int error;
312 unsigned int count;
313 struct collection_item *ini_config = NULL;
314
315 /* App name is null - expect failure */
316 error = config_for_app(NULL,
317 NULL,
318 NULL,
319 NULL,
320 INI_STOP_ON_NONE,
321 NULL);
322 if (!error) {
323 printf("Expected error: %d got success\n", EINVAL);
324 return -1;
325 }
326
327 /* Config collection storage is NULL - expect failure */
328 error = config_for_app("real",
329 NULL,
330 NULL,
331 NULL,
332 INI_STOP_ON_NONE,
333 NULL);
334 if (!error) {
335 printf("Expected error: %d got success\n", EINVAL);
336 return -1;
337 }
338
339 /* Config collection storage is NULL - expect failure */
340 error = config_for_app("real",
341 "real.conf",
342 NULL,
343 NULL,
344 INI_STOP_ON_NONE,
345 NULL);
346 if (!error) {
347 printf("Expected error: %d got success\n", EINVAL);
348 return -1;
349 }
350
351 /* Expect success but empty config */
352 error = config_for_app("real",
353 "real.conf",
354 NULL,
355 &ini_config,
356 INI_STOP_ON_NONE,
357 NULL);
358 if (error) {
359 printf("Expected success got error: %d\n",error);
360 return error;
361 }
362
363 count = 0;
364 (void)col_get_collection_count(ini_config, &count);
365 if (count > 1) {
366 printf("Expected empty collection but"
367 " got contents with %d elements\n", count);
368 col_print_collection(ini_config);
369 return -1;
370 }
371
372 free_ini_config(ini_config);
373 return 0;
374
375 }
376
377 static int real_test(const char *file)
378 {
379 int error;
380 struct collection_item *ini_config = NULL;
381 struct collection_item *error_set = NULL;
382 struct collection_iterator *iterator = NULL;
383 struct collection_item *item = NULL;
384 int type;
385
386 COLOUT(printf("\n\n===== REAL TEST START ======\n"));
387 COLOUT(printf("Reading collection\n"));
388 error = config_for_app("real", file, "./ini.d",
389 &ini_config, INI_STOP_ON_NONE, &error_set);
390 if (error) {
391 printf("Attempt to read configuration returned error: %d\n", error);
392 return error;
393 }
394
395 COLOUT(printf("Debugging the config collection:\n"));
396 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT));
397 COLOUT(printf("Debugging the error collection:\n"));
398 COLOUT(col_debug_collection(error_set, COL_TRAVERSE_DEFAULT));
399
400 COLOUT(printf("About to print parsing errors:\n"));
401 COLOUT(printf("\n\n----------------------\n"));
402 /* Output parsing errors (if any) */
403 COLOUT(print_config_parsing_errors(stdout, error_set));
404 COLOUT(printf("----------------------\n\n\n"));
405
406 COLOUT(printf("About to bind iterator to print"
407 " the config file contents.\n"));
408 /* Bind iterator */
409 error = col_bind_iterator(&iterator, ini_config,
410 COL_TRAVERSE_DEFAULT|COL_TRAVERSE_END);
411 if (error) {
412 printf("Failed to bind iterator: %d\n",error);
413 col_destroy_collection(ini_config);
414 col_destroy_collection(error_set);
415 return error;
416 }
417
418 COLOUT(printf("About to start iteration loop.\n"));
419 do {
420 /* Loop through a collection */
421 error = col_iterate_collection(iterator, &item);
422 if (error) {
423 printf("Error iterating collection: %d", error);
424 col_unbind_iterator(iterator);
425 return error;
426 }
427
428 /* Are we done ? */
429 if (item == NULL) break;
430
431 type = col_get_item_type(item);
432
433 /* Start of the collection */
434 if (type == COL_TYPE_COLLECTION)
435 COLOUT(printf("Contents of the application's configuration %s\n",
436 col_get_item_property(item, NULL)));
437 /* End of section */
438 else if (type == COL_TYPE_END) COLOUT(printf("\n"));
439 /* Section header ? */
440 else if (type == COL_TYPE_COLLECTIONREF)
441 COLOUT(printf("[%s]\n", col_get_item_property(item, NULL)));
442 /* Anything else - we know they are all strings*/
443 else
444 COLOUT(printf("%s = %s\n",
445 col_get_item_property(item, NULL),
446 (char *)col_get_item_data(item)));
447 }
448 while(1);
449
450 /* Do not forget to unbind iterator - otherwise there will be a leak */
451 COLOUT(printf("About to clean up.\n"));
452 col_unbind_iterator(iterator);
453
454 free_ini_config(ini_config);
455 free_ini_config_errors(error_set);
456 return 0;
457 }
458
459 static int get_test(void)
460 {
461
462 int error;
463 struct collection_item *ini_config = NULL;
464 struct collection_item *error_set = NULL;
465 struct collection_item *item = NULL;
466 int number;
467 long number_long;
468 double number_double;
469 unsigned number_unsigned;
470 unsigned long number_ulong;
471 unsigned char logical = 0;
472 char *str;
473 const char *cstr;
474 const char *cstrn;
475 void *binary;
476 int length;
477 int i = 0;
478 char **strarray;
479 char **strptr;
480 int size;
481 long *array;
482 double *darray;
483 char **prop_array;
484 int32_t val_int32;
485 uint32_t val_uint32;
486 int64_t val_int64;
487 uint64_t val_uint64;
488
489
490 COLOUT(printf("\n\n===== GET TEST START ======\n"));
491 COLOUT(printf("Reading collection\n"));
492
493 error = config_for_app("real", NULL, "./ini.d",
494 &ini_config, INI_STOP_ON_NONE, &error_set);
495 if (error) {
496 printf("Attempt to read configuration returned error: %d\n", error);
497 return error;
498 }
499
500 COLOUT(printf("Debugging the config collection:\n"));
501 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT));
502 COLOUT(printf("Debugging the error collection:\n"));
503 COLOUT(col_debug_collection(error_set, COL_TRAVERSE_DEFAULT));
504 free_ini_config_errors(error_set);
505
506 COLOUT(printf("Negtive test - trying to get non"
507 " existing key-value pair.\n"));
508
509 /* Negative test */
510 item = NULL;
511 error = get_config_item("monitor1", "description1", ini_config, &item);
512 if (error) {
513 printf("Expected success but got error! %d\n", error);
514 free_ini_config(ini_config);
515 return error;
516 }
517
518 /* Item should not be found */
519 if (item != NULL) {
520 printf("Expected NULL but got something else!\n");
521 free_ini_config(ini_config);
522 return -1;
523 }
524
525 /* Another negative test but section exists this time */
526 item = NULL;
527 error = get_config_item("monitor", "description1", ini_config, &item);
528 if (error) {
529 printf("Expected success but got error! %d\n", error);
530 free_ini_config(ini_config);
531 return error;
532 }
533
534 /* Item should not be found */
535 if(item != NULL) {
536 printf("Expected NULL but got something else!\n");
537 free_ini_config(ini_config);
538 return -1;
539 }
540
541 COLOUT(printf("Trying to get an item.\n"));
542
543 /* Positive test */
544 item = NULL;
545 error = get_config_item("monitor", "description", ini_config, &item);
546 if (error) {
547 printf("Expected success but got error! %d\n", error);
548 free_ini_config(ini_config);
549 return error;
550 }
551
552 /* Item should be found */
553 if (item == NULL) {
554 printf("Expected item but got something NULL!\n");
555 free_ini_config(ini_config);
556 return -1;
557 }
558
559 COLOUT(col_debug_item(item));
560
561 COLOUT(printf("Get item as string without duplication"
562 " from the NULL item.\n"));
563
564 /* Get a string without duplicication */
565 /* Negative test */
566 cstrn = get_const_string_config_value(NULL, NULL);
567 if (cstrn != NULL) {
568 printf("Expected error got success.\n");
569 free_ini_config(ini_config);
570 return -1;
571 }
572
573 COLOUT(printf("Get item as string without duplication"
574 "from the correct item.\n"));
575
576 /* Now get string from the right item */
577 error = 0;
578 cstr = get_const_string_config_value(item, &error);
579 if (error) {
580 printf("Expected success got error %d.\n", error);
581 free_ini_config(ini_config);
582 return error;
583 }
584
585 COLOUT(printf("Value: [%s]\n", cstr));
586
587 /* Same thing but create a dup */
588
589 COLOUT(printf("Get item as string with duplication"
590 " from correct item.\n"));
591
592 error = 0;
593 str = get_string_config_value(item, &error);
594 if (error) {
595 printf("Expected success got error %d.\n", error);
596 free_ini_config(ini_config);
597 return error;
598 }
599
600 COLOUT(printf("Value: [%s]\n", str));
601 free(str);
602
603
604 /* Get a badly formated number */
605 COLOUT(printf("Convert item to number with strict conversion.\n"));
606
607 item = NULL;
608 error = get_config_item("monitor", "bad_number", ini_config, &item);
609 if (error) {
610 printf("Expected success but got error! %d\n", error);
611 free_ini_config(ini_config);
612 return error;
613 }
614
615 /* Item should be found */
616 if (item == NULL) {
617 printf("Expected item but got something NULL!\n");
618 free_ini_config(ini_config);
619 return -1;
620 }
621
622 COLOUT(col_debug_item(item));
623
624
625 /* Now try to get value in different ways */
626 error = 0;
627 number = get_int_config_value(item, 1, 10, &error);
628 if (error) {
629 /* We expected error in this case */
630 COLOUT(printf("Expected error.\n"));
631 if(number != 10) {
632 printf("It failed to set default value.\n");
633 free_ini_config(ini_config);
634 return -1;
635 }
636 }
637
638 COLOUT(printf("Convert item to number without strict conversion.\n"));
639
640 error = 0;
641 number = get_int_config_value(item, 0, 10, &error);
642 if (error) {
643 /* We expected error in this case */
644 printf("Did not expect error.\n");
645 free_ini_config(ini_config);
646 return error;
647 }
648
649 if (number != 5) {
650 /* We expected error in this case */
651 printf("We expected that the conversion will return 5.\n");
652 free_ini_config(ini_config);
653 return -1;
654 }
655
656 /* Get real integer */
657
658 COLOUT(printf("Fetch another item from section \"domains/LOCAL\""
659 " named \"enumerate\".\n"));
660
661 item = NULL;
662 error = get_config_item("domains/LOCAL","enumerate", ini_config, &item);
663 if (error) {
664 printf("Expected success but got error! %d\n", error);
665 free_ini_config(ini_config);
666 return error;
667 }
668
669 /* Item should be found */
670 if (item == NULL) {
671 printf("Expected success but got NULL.\n");
672 free_ini_config(ini_config);
673 return -1;
674 }
675
676 COLOUT(printf("Convert item to integer.\n"));
677
678 /* Take number out of it */
679 error = 0;
680 number = get_int_config_value(item, 1, 100, &error);
681 if (error) {
682 printf("Did not expect error. Got %d\n", error);
683 free_ini_config(ini_config);
684 return error;
685 }
686
687 /* It is 3 in the file */
688 if (number != 3) {
689 printf("We expected that the conversion will return 3.\n");
690 free_ini_config(ini_config);
691 return -1;
692 }
693
694 COLOUT(printf("Expected 3 got %d\n", number));
695
696 COLOUT(printf("Convert item to long.\n"));
697
698 /* Take number out of it */
699 error = 0;
700 number_long = get_long_config_value(item, 1, 100, &error);
701 if (error) {
702 printf("Did not expect error. Got %d\n", error);
703 free_ini_config(ini_config);
704 return error;
705 }
706
707 /* It is 3 in the file */
708 if (number_long != 3) {
709 printf("We expected that the conversion will return 3.\n");
710 free_ini_config(ini_config);
711 return -1;
712 }
713
714 COLOUT(printf("Expected 3 got %ld\n", number_long));
715
716 COLOUT(printf("Convert item to unsigned.\n"));
717
718 /* Take number out of it */
719 error = 0;
720 number_unsigned = get_unsigned_config_value(item, 1, 100, &error);
721 if (error) {
722 printf("Did not expect error. Got %d\n", error);
723 free_ini_config(ini_config);
724 return error;
725 }
726
727 /* It is 3 in the file */
728 if(number_unsigned != 3) {
729 printf("We expected that the conversion will return 3.\n");
730 free_ini_config(ini_config);
731 return -1;
732 }
733
734 COLOUT(printf("Expected 3 got %d\n", number_unsigned));
735
736 COLOUT(printf("Convert item to unsigned long.\n"));
737
738 /* Take number out of it */
739 error = 0;
740 number_ulong = get_ulong_config_value(item, 1, 100, &error);
741 if (error) {
742 printf("Did not expect error. Got %d\n", error);
743 free_ini_config(ini_config);
744 return error;
745 }
746
747 /* It is 3 in the file */
748 if (number_ulong != 3) {
749 printf("We expected that the conversion will return 3.\n");
750 free_ini_config(ini_config);
751 return -1;
752 }
753
754 COLOUT(printf("Expected 3 got %lu\n", number_ulong));
755
756 COLOUT(printf("Convert item to double.\n"));
757
758 /* Take number out of it */
759 error = 0;
760 number_double = get_double_config_value(item, 1, 100., &error);
761 if (error) {
762 printf("Did not expect error. Got %d\n", error);
763 free_ini_config(ini_config);
764 return error;
765 }
766
767 /* It is 3 in the file */
768 if (number_double != 3.) {
769 printf("We expected that the conversion will return 3.\n");
770 free_ini_config(ini_config);
771 return -1;
772 }
773
774 COLOUT(printf("Expected 3 got %e\n", number_double));
775
776 COLOUT(printf("Convert item to bool.\n"));
777
778 /* Take number out of it */
779 error = 0;
780 logical = get_bool_config_value(item, 1, &error);
781 if (!error) {
782 printf("Expect error. Got success. Value %d\n", (int) logical);
783 free_ini_config(ini_config);
784 return -1;
785 }
786
787 /* Get real bool item and convert it */
788 COLOUT(printf("Get real bool item \"legacy\" and convert it.\n"));
789
790 item = NULL;
791 error = get_config_item("domains/LOCAL","legacy", ini_config, &item);
792 if (error) {
793 printf("Expected success but got error! %d\n",error);
794 free_ini_config(ini_config);
795 return error;
796 }
797
798 /* Item should be found */
799 if (item == NULL) {
800 printf("Expected success but got NULL.\n");
801 free_ini_config(ini_config);
802 return -1;
803 }
804
805 COLOUT(printf("Convert item to bool.\n"));
806
807 error = 0;
808 logical = get_bool_config_value(item, 1, &error);
809 if (error) {
810 printf("Expect success got error %d.\n", error);
811 free_ini_config(ini_config);
812 return error;
813 }
814
815 if (logical) {
816 printf("Expected false but got true - bad.\n");
817 return -1;
818 }
819
820 COLOUT(printf("In the files it is FALSE so we got false.\n"));
821
822 COLOUT(printf("Get binary item\n"));
823
824 item = NULL;
825 error = get_config_item("domains/EXAMPLE.COM",
826 "binary_test",
827 ini_config,
828 &item);
829 if (error) {
830 printf("Expected success but got error! %d\n", error);
831 free_ini_config(ini_config);
832 return error;
833 }
834
835 /* Item should be found */
836 if (item == NULL) {
837 printf("Expected success but got NULL.\n");
838 free_ini_config(ini_config);
839 return -1;
840 }
841
842 COLOUT(col_debug_item(item));
843
844 error = 0;
845 binary = get_bin_config_value(item, &length, &error);
846 if (error) {
847 printf("Expect success got error %d.\n", error);
848 free_ini_config(ini_config);
849 return error;
850 }
851
852 COLOUT(printf("Binary value (expect 123) = "));
853 COLOUT(for (i=0;i<length;i++) {
854 printf("%d",*((unsigned char*)(binary)+i));
855 });
856 COLOUT(printf("\n"));
857
858 free_bin_config_value(binary);
859
860 COLOUT(printf("Get string array item\n"));
861
862 item = NULL;
863 error = get_config_item("domains", "domainsorder", ini_config, &item);
864 if(error) {
865 printf("Expected success but got error! %d\n",error);
866 free_ini_config(ini_config);
867 return error;
868 }
869
870 /* Item should be found */
871 if (item == NULL) {
872 printf("Expected success but got NULL.\n");
873 free_ini_config(ini_config);
874 return -1;
875 }
876
877 COLOUT(col_debug_item(item));
878
879 COLOUT(printf("Get str array without size.\n"));
880
881 error = 0;
882 strarray = get_string_config_array(item, ",", NULL, &error);
883 if (error) {
884 printf("Expect success got error %d.\n", error);
885 free_ini_config(ini_config);
886 return error;
887 }
888
889 /* Can be used with this cycle */
890 strptr = strarray;
891 while (*strptr != NULL) {
892 COLOUT(printf("[%s]\n",*strptr));
893 strptr++;
894 }
895
896 free_string_config_array(strarray);
897
898 COLOUT(printf("Get raw str array without size.\n"));
899
900 error = 0;
901 strarray = get_raw_string_config_array(item, ",", NULL, &error);
902 if (error) {
903 printf("Expect success got error %d.\n", error);
904 free_ini_config(ini_config);
905 return error;
906 }
907
908 /* Can be used with this cycle */
909 strptr = strarray;
910 while (*strptr != NULL) {
911 COLOUT(printf("[%s]\n",*strptr));
912 strptr++;
913 }
914
915 free_string_config_array(strarray);
916
917 COLOUT(printf("Get str array with size.\n"));
918
919 error = 0;
920 size = 0;
921 strarray = get_string_config_array(item, ",", &size, &error);
922 if (error) {
923 printf("Expect success got error %d.\n", error);
924 free_ini_config(ini_config);
925 return error;
926 }
927
928 /* Can be used with this cycle */
929 COLOUT(for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)));
930
931 free_string_config_array(strarray);
932
933 COLOUT(printf("Get raw str array with size.\n"));
934
935 error = 0;
936 size = 0;
937 strarray = get_raw_string_config_array(item, ",", &size, &error);
938 if (error) {
939 printf("Expect success got error %d.\n", error);
940 free_ini_config(ini_config);
941 return error;
942 }
943
944 /* Can be used with this cycle */
945 COLOUT(for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)));
946
947 free_string_config_array(strarray);
948
949 /**********************************************************/
950
951 COLOUT(printf("Get bad string array \n"));
952
953 item = NULL;
954 error = get_config_item("domains", "badarray", ini_config, &item);
955 if(error) {
956 printf("Expected success but got error! %d\n",error);
957 free_ini_config(ini_config);
958 return error;
959 }
960
961 /* Item should be found */
962 if (item == NULL) {
963 printf("Expected success but got NULL.\n");
964 free_ini_config(ini_config);
965 return -1;
966 }
967
968 COLOUT(col_debug_item(item));
969
970 COLOUT(printf("Get bad str array without size.\n"));
971
972 error = 0;
973 strarray = get_string_config_array(item, ",", NULL, &error);
974 if (error) {
975 printf("Expect success got error %d.\n", error);
976 free_ini_config(ini_config);
977 return error;
978 }
979
980 /* Can be used with this cycle */
981 strptr = strarray;
982 while (*strptr != NULL) {
983 COLOUT(printf("[%s]\n",*strptr));
984 strptr++;
985 }
986
987 free_string_config_array(strarray);
988
989 /**********************************************************/
990
991 COLOUT(printf("Get long array item\n"));
992
993 item = NULL;
994 error = get_config_item("domains/EXAMPLE.COM",
995 "long_array",
996 ini_config,
997 &item);
998 if(error) {
999 printf("Expected success but got error! %d\n", error);
1000 free_ini_config(ini_config);
1001 return error;
1002 }
1003
1004 /* Item should be found */
1005 if (item == NULL) {
1006 printf("Expected success but got NULL.\n");
1007 free_ini_config(ini_config);
1008 return -1;
1009 }
1010
1011 COLOUT(col_debug_item(item));
1012
1013 error = 0;
1014 size = 0; /* Here size is not optional!!! */
1015 array = get_long_config_array(item, &size, &error);
1016 if(error) {
1017 printf("Expect success got error %d.\n", error);
1018 free_ini_config(ini_config);
1019 return error;
1020 }
1021
1022 /* Can be used with this cycle */
1023 COLOUT(for (i=0;i<size;i++) printf("%ld\n", *(array + i)));
1024
1025 free_long_config_array(array);
1026
1027 COLOUT(printf("Get double array item\n"));
1028
1029 item = NULL;
1030 error = get_config_item("domains/EXAMPLE.COM",
1031 "double_array",
1032 ini_config,
1033 &item);
1034 if (error) {
1035 printf("Expected success but got error! %d\n", error);
1036 free_ini_config(ini_config);
1037 return error;
1038 }
1039
1040 /* Item should be found */
1041 if (item == NULL) {
1042 printf("Expected success but got NULL.\n");
1043 free_ini_config(ini_config);
1044 return -1;
1045 }
1046
1047 COLOUT(col_debug_item(item));
1048
1049 error = 0;
1050 size = 0; /* Here size is not optional!!! */
1051 darray = get_double_config_array(item, &size, &error);
1052 if (error) {
1053 printf("Expect success got error %d.\n", error);
1054 free_ini_config(ini_config);
1055 return error;
1056 }
1057
1058 /* Can be used with this cycle */
1059 COLOUT(for (i=0;i<size;i++) printf("%.4f\n", darray[i]));
1060
1061 free_double_config_array(darray);
1062
1063 COLOUT(printf("\n\nSection list - no size\n"));
1064
1065 /* Do not care about the error or size */
1066 prop_array = get_section_list(ini_config, NULL, NULL);
1067 if (prop_array == NULL) {
1068 printf("Expect success got error.\n");
1069 free_ini_config(ini_config);
1070 return -1;
1071 }
1072
1073 i = 0;
1074 COLOUT(while (prop_array[i]) {
1075 printf("Section: [%s]\n", prop_array[i]);
1076 i++;
1077 });
1078
1079 free_section_list(prop_array);
1080
1081 COLOUT(printf("\n\nSection list - with size\n"));
1082
1083 /* Do not care about the error or size */
1084 prop_array = get_section_list(ini_config, &size, NULL);
1085 if (prop_array == NULL) {
1086 printf("Expect success got error.\n");
1087 free_ini_config(ini_config);
1088 return -1;
1089 }
1090
1091 COLOUT(for (i=0;i<size;i++) printf("Section: [%s]\n", prop_array[i]));
1092 free_section_list(prop_array);
1093
1094 COLOUT(printf("\n\nAttributes in the section - with size and error\n"));
1095
1096 /* Do not care about the error or size */
1097 prop_array = get_attribute_list(ini_config,
1098 "domains/EXAMPLE.COM",
1099 &size,
1100 &error);
1101 if (prop_array == NULL) {
1102 printf("Expect success got error.\n");
1103 free_ini_config(ini_config);
1104 return -1;
1105 }
1106
1107 COLOUT(for (i=0;i<size;i++) printf("Attribute: [%s]\n", prop_array[i]));
1108 free_attribute_list(prop_array);
1109
1110
1111 /***************************************/
1112 /* Test special types */
1113 /***************************************/
1114 COLOUT(printf("Test int32_t\n"));
1115
1116 item = NULL;
1117 error = get_config_item("domains/EXAMPLE.COM",
1118 "int32_t",
1119 ini_config,
1120 &item);
1121 if (error) {
1122 printf("Expected success but got error! %d\n", error);
1123 free_ini_config(ini_config);
1124 return error;
1125 }
1126
1127 /* Item should be found */
1128 if (item == NULL) {
1129 printf("Expected success but got NULL.\n");
1130 free_ini_config(ini_config);
1131 return -1;
1132 }
1133
1134 COLOUT(col_debug_item(item));
1135
1136 error = 0;
1137 val_int32 = get_int32_config_value(item, 1, 0, &error);
1138 if (error) {
1139 printf("Expect success got error %d.\n", error);
1140 free_ini_config(ini_config);
1141 return error;
1142 }
1143
1144 COLOUT(printf("Value: %d\n", val_int32));
1145
1146 /***************************************/
1147
1148 COLOUT(printf("Test uint32_t\n"));
1149
1150 item = NULL;
1151 error = get_config_item("domains/EXAMPLE.COM",
1152 "uint32_t",
1153 ini_config,
1154 &item);
1155 if (error) {
1156 printf("Expected success but got error! %d\n", error);
1157 free_ini_config(ini_config);
1158 return error;
1159 }
1160
1161 /* Item should be found */
1162 if (item == NULL) {
1163 printf("Expected success but got NULL.\n");
1164 free_ini_config(ini_config);
1165 return -1;
1166 }
1167
1168 COLOUT(col_debug_item(item));
1169
1170 error = 0;
1171 val_uint32 = get_uint32_config_value(item, 1, 0, &error);
1172 if (error) {
1173 printf("Expect success got error %d.\n", error);
1174 free_ini_config(ini_config);
1175 return error;
1176 }
1177
1178 COLOUT(printf("Value: %u\n", val_uint32));
1179
1180 /***************************************/
1181
1182 COLOUT(printf("Test int64_t\n"));
1183
1184 item = NULL;
1185 error = get_config_item("domains/EXAMPLE.COM",
1186 "int64_t",
1187 ini_config,
1188 &item);
1189 if (error) {
1190 printf("Expected success but got error! %d\n", error);
1191 free_ini_config(ini_config);
1192 return error;
1193 }
1194
1195 /* Item should be found */
1196 if (item == NULL) {
1197 printf("Expected success but got NULL.\n");
1198 free_ini_config(ini_config);
1199 return -1;
1200 }
1201
1202 COLOUT(col_debug_item(item));
1203
1204 error = 0;
1205 val_int64 = get_int64_config_value(item, 1, 0, &error);
1206 if (error) {
1207 printf("Expect success got error %d.\n", error);
1208 free_ini_config(ini_config);
1209 return error;
1210 }
1211
1212 COLOUT(printf("Value: %lld\n", (long long)val_int64));
1213
1214 /***************************************/
1215
1216 COLOUT(printf("Test uint32_t\n"));
1217
1218 item = NULL;
1219 error = get_config_item("domains/EXAMPLE.COM",
1220 "uint64_t",
1221 ini_config,
1222 &item);
1223 if (error) {
1224 printf("Expected success but got error! %d\n", error);
1225 free_ini_config(ini_config);
1226 return error;
1227 }
1228
1229 /* Item should be found */
1230 if (item == NULL) {
1231 printf("Expected success but got NULL.\n");
1232 free_ini_config(ini_config);
1233 return -1;
1234 }
1235
1236 COLOUT(col_debug_item(item));
1237
1238 error = 0;
1239 val_uint64 = get_uint64_config_value(item, 1, 0, &error);
1240 if (error) {
1241 printf("Expect success got error %d.\n", error);
1242 free_ini_config(ini_config);
1243 return error;
1244 }
1245
1246 COLOUT(printf("Value: %llu\n", (unsigned long long)val_uint64));
1247
1248 /***************************************/
1249
1250 COLOUT(printf("Get empty array item\n"));
1251
1252 item = NULL;
1253 error = get_config_item("domains/EXAMPLE.COM",
1254 "empty_value",
1255 ini_config,
1256 &item);
1257 if(error) {
1258 printf("Expected success but got error! %d\n", error);
1259 free_ini_config(ini_config);
1260 return error;
1261 }
1262
1263 /* Item should be found */
1264 if (item == NULL) {
1265 printf("Expected success but got NULL.\n");
1266 free_ini_config(ini_config);
1267 return -1;
1268 }
1269
1270 COLOUT(col_debug_item(item));
1271
1272 error = 0;
1273 size = 0; /* Here size is not optional!!! */
1274 strarray = get_string_config_array(item, ",", &size, &error);
1275 if(error) {
1276 printf("Expect success got error %d.\n", error);
1277 free_ini_config(ini_config);
1278 return error;
1279 }
1280
1281 if (size != 0) {
1282 for (i=0; i<size; i++) printf("%s\n", *(strarray + i));
1283 printf("Expected size=0, got size=%d\n", size);
1284 free_string_config_array(strarray);
1285 free_ini_config(ini_config);
1286 return -1;
1287 }
1288
1289
1290 free_string_config_array(strarray);
1291
1292 free_ini_config(ini_config);
1293 COLOUT(printf("Done with get test!\n"));
1294 return EOK;
1295 }
1296
1297 /* This is an emulation of the case when daemon starts
1298 * and one needs to parse the configuration file
1299 * for the first time and load configuration
1300 */
1301 static int startup_test(void)
1302 {
1303 int error;
1304 struct collection_item *ini_config = NULL;
1305 struct collection_item *error_set = NULL;
1306 struct collection_item *metadata = NULL;
1307 uint32_t flags;
1308
1309
1310 /* At startup we can simplify our life by
1311 * parsing configuration and then checking
1312 * the permissions. It is less optimal from
1313 * the performnce point of view but simple to implement.
1314 * Since it is the start of the daemon we can
1315 * hope that parsing the config file would
1316 * usually not a be a wasted effort.
1317 * If permission check fails that means we should
1318 * exit. Ok so we just parse the INI file for nothing.
1319 * Not a big deal, I would say...
1320 */
1321
1322 COLOUT(printf("STARTUP TEST\n"));
1323
1324 /* Set file permissions to 0664 */
1325 chmod("./ini.conf", 0664);
1326
1327 flags = INI_META_SEC_ACCESS_FLAG |
1328 INI_META_SEC_ERROR_FLAG;
1329
1330 error = config_from_file_with_metadata("test", "./ini.conf",
1331 &ini_config, INI_STOP_ON_NONE,
1332 &error_set,
1333 flags,
1334 &metadata);
1335 /*
1336 * This is just for debugging.
1337 * do not copy into your implementation
1338 */
1339 if (metadata) {
1340 COLOUT(printf("\n\nMeta data\n"));
1341 COLOUT(col_debug_collection(metadata, COL_TRAVERSE_DEFAULT));
1342 }
1343
1344
1345 if (error) {
1346 printf("Attempt to read configuration returned error: %d\n",error);
1347
1348 /* If you want to do any specific error checking, do it here.
1349 * If you want to get the file error code from the
1350 * metadata get it here.
1351 */
1352 free_ini_config_metadata(metadata);
1353
1354 /* Error reporting start ==> */
1355 if (error_set) {
1356 printf("\n\nErrors\n");
1357 col_debug_collection(error_set, COL_TRAVERSE_DEFAULT);
1358 }
1359 /* <==== end */
1360 free_ini_config_errors(error_set);
1361 return error;
1362 }
1363
1364 free_ini_config_errors(error_set);
1365
1366 /* So we are here if we successfully got configuration. */
1367 /* You can check ownership and permissions here in one call */
1368 /* We will check just permissions here. */
1369 error = config_access_check(metadata,
1370 INI_ACCESS_CHECK_MODE, /* add uid & gui flags
1371 * in real case
1372 */
1373 0, /* <- will be real uid in real case */
1374 0, /* <- will be real gid in real case */
1375 0440, /* Checking for r--r----- */
1376 0);
1377 /* This check is expected to fail since
1378 * the actual permissions on the test file are: rw-rw-r--
1379 */
1380
1381 if (!error) {
1382 printf("Expected error got success!\n");
1383 free_ini_config_metadata(metadata);
1384 free_ini_config(ini_config);
1385 return EACCES;
1386 }
1387
1388 error = config_access_check(metadata,
1389 INI_ACCESS_CHECK_MODE, /* add uid & gui flags
1390 * in real case
1391 */
1392 0, /* <- will be real uid in real case */
1393 0, /* <- will be real gid in real case */
1394 0664, /* Checkling for rw-rw-r-- */
1395 0);
1396
1397 if (error) {
1398 printf("Access check failed %d!\n", error);
1399 free_ini_config_metadata(metadata);
1400 free_ini_config(ini_config);
1401 return EACCES;
1402 }
1403
1404
1405 /* Use configuration */
1406
1407 COLOUT(printf("\n\nMeta data\n"));
1408 COLOUT(col_debug_collection(metadata, COL_TRAVERSE_DEFAULT));
1409 free_ini_config_metadata(metadata);
1410
1411 COLOUT(printf("\n\n----------------------\n"));
1412
1413 COLOUT(printf("\n\nConfiguration\n"));
1414 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT));
1415 free_ini_config(ini_config);
1416
1417 return 0;
1418 }
1419
1420 static int reload_test(void)
1421 {
1422
1423 int error;
1424 struct collection_item *ini_config = NULL;
1425 struct collection_item *metadata = NULL;
1426 struct collection_item *saved_metadata = NULL;
1427 uint32_t flags;
1428 int changed = 0;
1429 int fd;
1430
1431 COLOUT(printf("RELOAD TEST\n"));
1432
1433 /* Assume we saved metadata at the beginning
1434 * when we opened the file and read configuration
1435 * for the first time.
1436 * Here we have to emulate it.
1437 */
1438
1439 flags = INI_META_SEC_ACCESS_FLAG |
1440 INI_META_ACTION_NOPARSE;
1441
1442 error = config_from_file_with_metadata("test", "./ini.conf",
1443 &ini_config,
1444 0,
1445 NULL,
1446 flags,
1447 &saved_metadata);
1448 if (error) {
1449 printf("Attempt to read configuration returned error: %d\n",error);
1450 free_ini_config_metadata(saved_metadata);
1451 return error;
1452 }
1453
1454 /*****************************************/
1455
1456 /* We are reloading so we probably doing it becuase
1457 * we got a signal ot some kind of time out expired
1458 * and it might be time for us to check if we need
1459 * to reload. So assume it is time to check...
1460 */
1461
1462 /* It is safer to open file first */
1463 fd = open("./ini.conf", O_RDONLY);
1464 if (fd < 0) {
1465 error = errno;
1466 printf("Attempt to read configuration returned error: %d\n", error);
1467 free_ini_config_metadata(saved_metadata);
1468 return error;
1469 }
1470
1471 /* You migth be checking pretty frequently, once in 5 min for example
1472 * but the config usually does not change for months
1473 * so you do not want to do any extra processing every time you check.
1474 */
1475
1476 /* Do permission check here right away on the file, or... */
1477
1478
1479 flags = INI_META_SEC_ACCESS_FLAG |
1480 INI_META_ACTION_NOPARSE;
1481
1482 error = config_from_fd_with_metadata("test", fd,
1483 "./ini.conf",
1484 &ini_config,
1485 0,
1486 NULL,
1487 flags,
1488 &metadata);
1489 if (error) {
1490 printf("Attempt to read configuration returned error: %d\n",error);
1491 if (metadata) {
1492 printf("\n\nMeta data\n");
1493 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT);
1494 }
1495 free_ini_config_metadata(metadata);
1496 free_ini_config_metadata(saved_metadata);
1497 close(fd);
1498 return error;
1499 }
1500
1501 /* ...or you can do permission check here using the metadata
1502 * as it is done in the startup test.
1503 * For now we skip this part and move on.
1504 */
1505
1506 error = config_changed(metadata, saved_metadata, &changed);
1507
1508 if (error) {
1509 printf("Internal error: %d\n",error);
1510 printf("\n\nSaved Meta data\n");
1511 col_debug_collection(saved_metadata, COL_TRAVERSE_DEFAULT);
1512 printf("\n\nMeta data\n");
1513 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT);
1514 free_ini_config_metadata(saved_metadata);
1515 free_ini_config_metadata(metadata);
1516 close(fd);
1517 return error;
1518
1519 }
1520
1521 if (changed) {
1522
1523 /* Read the config from the descriptor and use it.
1524 * Discard old saved meta data and save
1525 * the latest one for future use...
1526 */
1527
1528 /* Here it would be an error if it is different */
1529 printf("Meta data is supposed to be same but different.\n");
1530 printf("\n\nSaved Meta data\n");
1531 col_debug_collection(saved_metadata, COL_TRAVERSE_DEFAULT);
1532 printf("\n\nMeta data\n");
1533 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT);
1534 }
1535
1536 free_ini_config_metadata(saved_metadata);
1537 free_ini_config_metadata(metadata);
1538 close(fd);
1539
1540 return 0;
1541 }
1542
1543
1544 int main(int argc, char *argv[])
1545 {
1546 int error = EOK;
1547 char *srcdir = NULL;
1548 char *rundir = NULL;
1549 const char inidir[] = "/ini";
1550 int len = 0;
1551 char *var;
1552
1553 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
1554 else {
1555 var = getenv("COMMON_TEST_VERBOSE");
1556 if (var) verbose = 1;
1557 }
1558
1559 COLOUT(printf("Start\n"));
1560
1561 srcdir = getenv("srcdir");
1562 if(srcdir) {
1563
1564 len = strlen(srcdir) + sizeof(inidir);
1565 rundir = malloc(len);
1566 if (!rundir) {
1567 printf("Failed to allocate memory to store path"
1568 " to the test files %d.\n", ENOMEM);
1569 return -1;
1570 }
1571
1572 snprintf(rundir, len, "%s%s", srcdir, inidir);
1573
1574 errno = 0;
1575 if(chdir(rundir) != 0) {
1576 error = errno;
1577 free(rundir);
1578 printf("Failed to change directory, error %d\n", error);
1579 return error;
1580 }
1581 free(rundir);
1582 }
1583
1584 if ((error = basic_test()) ||
1585 (error = single_file()) ||
1586 (error = single_fd()) ||
1587 (error = negative_test()) ||
1588 (error = real_test(NULL)) ||
1589 /* This should result in merged configuration */
1590 (error = real_test("./ini.conf")) ||
1591 (error = startup_test()) ||
1592 (error = reload_test()) ||
1593 (error = get_test())) {
1594 printf("Test failed! Error %d.\n", error);
1595 return -1;
1596 }
1597
1598 COLOUT(printf("Success!\n"));
1599 return 0;
1600 }