LCOV - code coverage report
Current view: top level - src/tds/unittests - common.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 69 97 71.1 %
Date: 2024-03-23 09:12:27 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #define TDS_DONT_DEFINE_DEFAULT_FUNCTIONS
       2             : #include "common.h"
       3             : #include <freetds/replacements.h>
       4             : 
       5             : char USER[512];
       6             : char SERVER[512];
       7             : char PASSWORD[512];
       8             : char DATABASE[512];
       9             : /* TODO use another default ?? */
      10             : char CHARSET[512] = "ISO-8859-1";
      11             : 
      12             : int read_login_info(void);
      13             : 
      14             : int
      15         112 : read_login_info(void)
      16             : {
      17         112 :         FILE *in = NULL;
      18             :         char line[512];
      19             :         char *s1, *s2;
      20             : 
      21         112 :         s1 = getenv("TDSPWDFILE");
      22         112 :         if (s1 && s1[0])
      23           0 :                 in = fopen(s1, "r");
      24           0 :         if (!in)
      25         112 :                 in = fopen("../../../PWD", "r");
      26         112 :         if (!in) {
      27           0 :                 fprintf(stderr, "Can not open PWD file\n\n");
      28           0 :                 return TDS_FAIL;
      29             :         }
      30             : 
      31        2128 :         while (fgets(line, sizeof(line), in)) {
      32        2016 :                 s1 = strtok(line, "=");
      33        2016 :                 s2 = strtok(NULL, "\n");
      34        2016 :                 if (!s1 || !s2) {
      35        1204 :                         continue;
      36             :                 }
      37         812 :                 if (!strcmp(s1, "UID")) {
      38         112 :                         strcpy(USER, s2);
      39         700 :                 } else if (!strcmp(s1, "SRV")) {
      40         112 :                         strcpy(SERVER, s2);
      41         588 :                 } else if (!strcmp(s1, "PWD")) {
      42         112 :                         strcpy(PASSWORD, s2);
      43         476 :                 } else if (!strcmp(s1, "DB")) {
      44         112 :                         strcpy(DATABASE, s2);
      45             :                 }
      46             :         }
      47         112 :         fclose(in);
      48         112 :         return TDS_SUCCESS;
      49             : }
      50             : 
      51             : TDSCONTEXT *test_context = NULL;
      52             : 
      53             : int
      54         112 : try_tds_login(TDSLOGIN ** login, TDSSOCKET ** tds, const char *appname, int verbose)
      55             : {
      56             :         TDSLOGIN *connection;
      57             :         char *appname_copy;
      58             : 
      59         112 :         if (verbose) {
      60           8 :                 printf("Entered tds_try_login()\n");
      61             :         }
      62         112 :         if (!login) {
      63           0 :                 fprintf(stderr, "Invalid TDSLOGIN**\n");
      64           0 :                 return TDS_FAIL;
      65             :         }
      66         112 :         if (!tds) {
      67           0 :                 fprintf(stderr, "Invalid TDSSOCKET**\n");
      68           0 :                 return TDS_FAIL;
      69             :         }
      70             : 
      71         112 :         if (verbose) {
      72           8 :                 printf("Trying read_login_info()\n");
      73             :         }
      74         112 :         read_login_info();
      75             : 
      76         112 :         if (verbose) {
      77           8 :                 printf("Setting login parameters\n");
      78             :         }
      79         112 :         *login = tds_alloc_login(1);
      80         112 :         if (!*login) {
      81           0 :                 fprintf(stderr, "tds_alloc_login() failed.\n");
      82           0 :                 return TDS_FAIL;
      83             :         }
      84         112 :         appname_copy = strdup(appname);
      85         112 :         if (!tds_set_passwd(*login, PASSWORD)
      86         112 :             || !tds_set_user(*login, USER)
      87         112 :             || !appname_copy
      88         112 :             || !tds_set_app(*login, basename(appname_copy))
      89         112 :             || !tds_set_host(*login, "myhost")
      90         112 :             || !tds_set_library(*login, "TDS-Library")
      91         112 :             || !tds_set_server(*login, SERVER)
      92         112 :             || !tds_set_client_charset(*login, CHARSET)
      93         112 :             || !tds_set_language(*login, "us_english")) {
      94           0 :                 free(appname_copy);
      95           0 :                 fprintf(stderr, "tds_alloc_login() failed.\n");
      96           0 :                 return TDS_FAIL;
      97             :         }
      98         112 :         free(appname_copy);
      99             : 
     100         112 :         if (verbose) {
     101           8 :                 printf("Connecting to database\n");
     102             :         }
     103         112 :         test_context = tds_alloc_context(NULL);
     104         112 :         *tds = tds_alloc_socket(test_context, 512);
     105         112 :         tds_set_parent(*tds, NULL);
     106         112 :         connection = tds_read_config_info(*tds, *login, test_context->locale);
     107         112 :         if (!connection || tds_connect_and_login(*tds, connection) != TDS_SUCCESS) {
     108           0 :                 if (connection) {
     109           0 :                         tds_free_socket(*tds);
     110           0 :                         *tds = NULL;
     111           0 :                         tds_free_login(connection);
     112             :                 }
     113           0 :                 fprintf(stderr, "tds_connect_and_login() failed\n");
     114           0 :                 return TDS_FAIL;
     115             :         }
     116         112 :         tds_free_login(connection);
     117             : 
     118         112 :         return TDS_SUCCESS;
     119             : }
     120             : 
     121             : 
     122             : /* Note that this always suceeds */
     123             : int
     124         112 : try_tds_logout(TDSLOGIN * login, TDSSOCKET * tds, int verbose)
     125             : {
     126         112 :         if (verbose) {
     127           8 :                 printf("Entered tds_try_logout()\n");
     128             :         }
     129         112 :         tds_close_socket(tds);
     130         112 :         tds_free_socket(tds);
     131         112 :         tds_free_login(login);
     132         112 :         tds_free_context(test_context);
     133         112 :         test_context = NULL;
     134         112 :         return TDS_SUCCESS;
     135             : }
     136             : 
     137             : /* Run query for which there should be no return results */
     138             : int
     139         566 : run_query(TDSSOCKET * tds, const char *query)
     140             : {
     141             :         int rc;
     142             :         int result_type;
     143             : 
     144         566 :         rc = tds_submit_query(tds, query);
     145         566 :         if (rc != TDS_SUCCESS) {
     146           0 :                 fprintf(stderr, "tds_submit_query() failed for query '%s'\n", query);
     147           0 :                 return TDS_FAIL;
     148             :         }
     149             : 
     150        1194 :         while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
     151             : 
     152         628 :                 switch (result_type) {
     153             :                 case TDS_DONE_RESULT:
     154             :                 case TDS_DONEPROC_RESULT:
     155             :                 case TDS_DONEINPROC_RESULT:
     156             :                         /* ignore possible spurious result (TDS7+ send it) */
     157             :                 case TDS_STATUS_RESULT:
     158             :                         break;
     159           0 :                 default:
     160           0 :                         fprintf(stderr, "Error:  query should not return results\n");
     161           0 :                         return TDS_FAIL;
     162             :                 }
     163             :         }
     164         566 :         if (rc == TDS_FAIL) {
     165           0 :                 fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for '%s'\n", query);
     166           0 :                 return TDS_FAIL;
     167         566 :         } else if (rc != TDS_NO_MORE_RESULTS) {
     168           0 :                 fprintf(stderr, "tds_process_tokens() unexpected return\n");
     169           0 :                 return TDS_FAIL;
     170             :         }
     171             : 
     172             :         return TDS_SUCCESS;
     173             : }

Generated by: LCOV version 1.13