LCOV - code coverage report
Current view: top level - src/tds/unittests - portconf.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 52 63 82.5 %
Date: 2025-02-21 09:36:06 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2019  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             : #include <freetds/replacements.h>
      21             : 
      22             : static void
      23          10 : set_interface(void)
      24             : {
      25          10 :         const char *in_file = FREETDS_SRCDIR "/portconf.in";
      26             : 
      27          10 :         FILE *f = fopen(in_file, "r");
      28          10 :         if (!f) {
      29           0 :                 in_file = "portconf.in";
      30           0 :                 f = fopen(in_file, "r");
      31             :         }
      32          10 :         if (!f) {
      33           0 :                 fprintf(stderr, "error opening test file\n");
      34           0 :                 exit(1);
      35             :         }
      36          10 :         fclose(f);
      37          10 :         tds_set_interfaces_file_loc(in_file);
      38          10 : }
      39             : 
      40             : static void
      41         140 : dump_login(const char *name, const TDSLOGIN *login)
      42             : {
      43         140 :         printf("Dump login %s\n", name);
      44             : #define STR(name) printf(" " #name ": %s\n", tds_dstr_cstr(&login->name));
      45             : #define INT(name) printf(" " #name ": %d\n", login->name);
      46         280 :         STR(server_name);
      47         280 :         STR(server_host_name);
      48         280 :         STR(instance_name);
      49         140 :         INT(port);
      50         140 : }
      51             : 
      52             : static void
      53          70 : test0(TDSCONTEXT *ctx, TDSSOCKET *tds, const char *input, const char *expected, int line)
      54             : {
      55             :         TDSLOGIN *login, *connection;
      56          70 :         char *ret = NULL;
      57             : 
      58          70 :         login = tds_alloc_login(true);
      59          70 :         if (!login || !tds_set_server(login, input)) {
      60           0 :                 fprintf(stderr, "Error setting login!\n");
      61           0 :                 exit(1);
      62             :         }
      63             : 
      64          70 :         connection = tds_read_config_info(tds, login, ctx->locale);
      65          70 :         dump_login("input", login);
      66          70 :         dump_login("final", connection);
      67         350 :         if (asprintf(&ret, "%s,%s,%s,%d",
      68          70 :                      tds_dstr_cstr(&connection->server_name),
      69          70 :                      tds_dstr_cstr(&connection->server_host_name),
      70          70 :                      tds_dstr_cstr(&connection->instance_name),
      71             :                      connection->port) < 0)
      72           0 :                 exit(1);
      73          70 :         if (strcmp(ret, expected) != 0) {
      74           0 :                 fprintf(stderr, "Mismatch line %d:\n  OUT: %s\n  EXP: %s\n",
      75             :                         line, ret, expected);
      76           0 :                 exit(1);
      77             :         }
      78          70 :         tds_free_login(connection);
      79          70 :         tds_free_login(login);
      80          70 :         free(ret);
      81          70 : }
      82             : #define test(in, out) test0(ctx, tds, in, out, __LINE__)
      83             : 
      84             : int
      85          10 : main(void)
      86             : {
      87          10 :         TDSCONTEXT *ctx = tds_alloc_context(NULL);
      88          10 :         TDSSOCKET *tds = tds_alloc_socket(ctx, 512);
      89             :         FILE *f;
      90             : 
      91             :         /* set an empty base configuration */
      92          10 :         f = fopen("empty.conf", "w");
      93          10 :         if (f)
      94          10 :                 fclose(f);
      95          10 :         putenv("FREETDSCONF=empty.conf");
      96          10 :         unsetenv("TDSPORT");
      97             : 
      98          10 :         set_interface();
      99          10 :         if (!ctx || !tds) {
     100           0 :                 fprintf(stderr, "Error creating socket!\n");
     101           0 :                 return 1;
     102             :         }
     103             : 
     104          10 :         test("NotExistingServer1:1234", "my_server,8.7.6.5,,1234");
     105          10 :         test("localhost:1234", "localhost,localhost,,1234");
     106          10 :         test("NotExistingServer1\\named", "my_server,8.7.6.5,named,0");
     107          10 :         test("localhost\\named", "localhost,localhost,named,0");
     108          10 :         test("2.3.4.5:2345", "2.3.4.5,2.3.4.5,,2345");
     109          10 :         test("[2::3]:432", "2::3,2::3,,432");
     110          10 :         test("[2::3:4]\\instance", "2::3:4,2::3:4,instance,0");
     111             : 
     112          10 :         tds_free_socket(tds);
     113          10 :         tds_free_context(ctx);
     114          10 :         tds_set_interfaces_file_loc(NULL);
     115             : 
     116          10 :         return 0;
     117             : }
     118             : 

Generated by: LCOV version 1.13