LCOV - code coverage report
Current view: top level - ini - ini_config_ut.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 484 816 59.3 %
Date: 2014-04-01 Functions: 9 9 100.0 %

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

Generated by: LCOV version 1.10