LCOV - code coverage report
Current view: top level - src/utils/unittests - test_base.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 36 41 87.8 %
Date: 2025-07-16 09:22:05 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* Base tests utilities
       2             :  * Copyright (C) 2025 Aaron M. Ucko
       3             :  * Copyright (C) 2025 Frediano Ziglio
       4             :  *
       5             :  * This library is free software; you can redistribute it and/or
       6             :  * modify it under the terms of the GNU Library General Public
       7             :  * License as published by the Free Software Foundation; either
       8             :  * version 2 of the License, or (at your option) any later version.
       9             :  *
      10             :  * This library is distributed in the hope that it will be useful,
      11             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13             :  * Library General Public License for more details.
      14             :  *
      15             :  * You should have received a copy of the GNU Library General Public
      16             :  * License along with this library; if not, write to the
      17             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      18             :  * Boston, MA 02111-1307, USA.
      19             :  */
      20             : 
      21             : #include <freetds/utils/test_base.h>
      22             : 
      23             : #include <stdio.h>
      24             : #include <stdlib.h>
      25             : #include <string.h>
      26             : 
      27             : #ifdef _WIN32
      28             : #  include <crtdbg.h>
      29             : #  include <windows.h>
      30             : 
      31             : static LONG WINAPI
      32             : seh_handler(EXCEPTION_POINTERS* ep TDS_UNUSED)
      33             : {
      34             :         /* Always terminate the test. */
      35             :         return EXCEPTION_EXECUTE_HANDLER;
      36             : }
      37             : 
      38             : static void
      39             : suppress_diag_popup_messages(void)
      40             : {
      41             :         /* Check environment variable for silent abort app at error */
      42             :         const char* value = getenv("DIAG_SILENT_ABORT");
      43             :         if (value  &&  (*value == 'Y'  ||  *value == 'y')) {
      44             :                 /* Windows GPF errors */
      45             :                 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
      46             :                              SEM_NOOPENFILEERRORBOX);
      47             : 
      48             :                 /* Runtime library */
      49             :                 _set_error_mode(_OUT_TO_STDERR);
      50             : 
      51             :                 /* Debug library */
      52             :                 _CrtSetReportFile(_CRT_WARN,   _CRTDBG_FILE_STDERR);
      53             :                 _CrtSetReportMode(_CRT_WARN,   _CRTDBG_MODE_FILE);
      54             :                 _CrtSetReportFile(_CRT_ERROR,  _CRTDBG_FILE_STDERR);
      55             :                 _CrtSetReportMode(_CRT_ERROR,  _CRTDBG_MODE_FILE);
      56             :                 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
      57             :                 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
      58             : 
      59             :                 /* Exceptions(!) */
      60             :                 SetUnhandledExceptionFilter(seh_handler);
      61             :         }
      62             : }
      63             : #endif
      64             : 
      65             : int
      66        2120 : main(int argc, char ** argv)
      67             : {
      68             : #ifdef _WIN32
      69             :         suppress_diag_popup_messages();
      70             : #endif
      71        2120 :         return test_main(argc, argv);
      72             : }
      73             : 
      74             : 
      75             : COMMON_PWD common_pwd;
      76             : 
      77             : const char *
      78        1490 : read_login_info_base(COMMON_PWD *common_pwd, const char *default_path)
      79             : {
      80        1490 :         const char *ret = try_read_login_info_base(common_pwd, default_path);
      81             : 
      82        1490 :         if (!ret)
      83           0 :                 fprintf(stderr, "Cannot open PWD file %s\n\n", default_path);
      84        1490 :         return ret;
      85             : }
      86             : 
      87             : const char *
      88        3034 : try_read_login_info_base(COMMON_PWD *common_pwd, const char *default_path)
      89             : {
      90        3034 :         FILE *in = NULL;
      91             :         char line[512];
      92             :         char *s1, *s2;
      93             :         const char *path;
      94             : 
      95        3034 :         if (common_pwd->initialized)
      96             :                 return default_path;
      97             : 
      98        1750 :         if (!common_pwd->tried_env) {
      99        1750 :                 common_pwd->tried_env = true;
     100        1750 :                 s1 = getenv("TDSPWDFILE");
     101        1750 :                 if (s1 && s1[0]) {
     102           0 :                         in = fopen(s1, "r");
     103           0 :                         if (in)
     104           0 :                                 path = s1;
     105             :                 }
     106             :         }
     107           0 :         if (!in) {
     108        1750 :                 in = fopen(default_path, "r");
     109        1750 :                 if (in) {
     110             :                         path = default_path;
     111             :                 } else {
     112             :                         return NULL;
     113             :                 }
     114             :         }
     115             : 
     116       34650 :         while (fgets(line, sizeof(line), in)) {
     117       32900 :                 s1 = strtok(line, "=");
     118       32900 :                 s2 = strtok(NULL, "\n");
     119       32900 :                 if (!s1 || !s2) {
     120       19950 :                         continue;
     121             :                 }
     122       12950 :                 switch (s1[0]) {
     123        1750 :                 case 'U':
     124        1750 :                         if (!common_pwd->user[0] && !strcmp(s1, "UID"))
     125        1750 :                                 strcpy(common_pwd->user, s2);
     126             :                         break;
     127        1750 :                 case 'S':
     128        1750 :                         if (!common_pwd->server[0] && !strcmp(s1, "SRV"))
     129        1750 :                                 strcpy(common_pwd->server, s2);
     130             :                         break;
     131        1750 :                 case 'P':
     132        1750 :                         if (!common_pwd->password[0] && !strcmp(s1, "PWD"))
     133        1750 :                                 strcpy(common_pwd->password, s2);
     134             :                         break;
     135        1750 :                 case 'D':
     136        1750 :                         if (!common_pwd->database[0] && !strcmp(s1, "DB"))
     137        1750 :                                 strcpy(common_pwd->database, s2);
     138             :                         break;
     139             :                 default:
     140             :                         break;
     141             :                 }
     142             :         }
     143        1750 :         fclose(in);
     144        1750 :         common_pwd->initialized = true;
     145        1750 :         return path;
     146             : }

Generated by: LCOV version 1.13