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

Generated by: LCOV version 1.13