LCOV - code coverage report
Current view: top level - src/tds/unittests - readconf.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 26 36 72.2 %
Date: 2025-01-18 12:13:41 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2012  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : #include "common.h"
      20             : 
      21             : static FILE *f = NULL;
      22             : static char *return_value = NULL;
      23             : 
      24             : static void
      25          24 : conf_parse(const char *option, const char *value, void *param)
      26             : {
      27          24 :         const char *entry = (const char *) param;
      28          24 :         if (strcmp(option, entry) == 0)
      29          24 :                 return_value = strdup(value);
      30          24 : }
      31             : 
      32             : static void
      33          24 : test(const char *section, const char *entry, const char *expected)
      34             : {
      35          24 :         int fail = 0;
      36             : 
      37          24 :         rewind(f);
      38             : 
      39          24 :         tds_read_conf_section(f, section, conf_parse, (void *) entry);
      40          24 :         if (!expected && return_value) {
      41           0 :                 fprintf(stderr, "return value %s NOT expected\n", return_value);
      42           0 :                 fail = 1;
      43          24 :         } else if (expected && !return_value) {
      44           0 :                 fprintf(stderr, "expected value %s NOT found\n", expected);
      45           0 :                 fail = 1;
      46          24 :         } else if (expected && return_value) {
      47          24 :                 if (strcmp(expected, return_value) != 0) {
      48           0 :                         fprintf(stderr, "expected value %s got %s\n", expected, return_value);
      49           0 :                         fail = 1;
      50             :                 }
      51             :         }
      52             : 
      53          24 :         free(return_value);
      54          24 :         return_value = NULL;
      55          24 :         if (fail)
      56           0 :                 exit(1);
      57          24 : }
      58             : 
      59             : int
      60           8 : main(int argc, char **argv)
      61             : {
      62           8 :         const char *in_file = FREETDS_SRCDIR "/readconf.in";
      63             : 
      64           8 :         f = fopen(in_file, "r");
      65           8 :         if (!f)
      66           0 :                 f = fopen("readconf.in", "r");
      67           8 :         if (!f) {
      68           0 :                 fprintf(stderr, "error opening test file\n");
      69           0 :                 exit(1);
      70             :         }
      71             : 
      72             :         /* option with no spaces */
      73           8 :         test("section1", "opt1", "value1");
      74             : 
      75             :         /* option name with spaces, different case in section name */
      76           8 :         test("section2", "opt two", "value2");
      77             : 
      78           8 :         test("section 3", "opt three", "value three");
      79             : 
      80           8 :         fclose(f);
      81             :         return 0;
      82             : }
      83             : 

Generated by: LCOV version 1.13