LCOV - code coverage report
Current view: top level - src/odbc/unittests - connect.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 53 61 86.9 %
Date: 2025-01-18 12:13:41 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : 
       4             : static void init_connect(void);
       5             : 
       6             : static void
       7          30 : init_connect(void)
       8             : {
       9          30 :         CHKAllocEnv(&odbc_env, "S");
      10          30 :         CHKAllocConnect(&odbc_conn, "S");
      11          30 : }
      12             : 
      13             : #ifdef _WIN32
      14             : #include <odbcinst.h>
      15             : 
      16             : static char *entry = NULL;
      17             : 
      18             : static char *
      19             : get_entry(const char *key)
      20             : {
      21             :         static TCHAR buf[256];
      22             : 
      23             :         entry = NULL;
      24             :         if (SQLGetPrivateProfileString(T(odbc_server), T(key), TEXT(""), buf, TDS_VECTOR_SIZE(buf), TEXT("odbc.ini")) > 0)
      25             :                 entry = C(buf);
      26             : 
      27             :         return entry;
      28             : }
      29             : #endif
      30             : 
      31             : int
      32           8 : main(int argc, char *argv[])
      33             : {
      34             :         char tmp[1024*4];
      35             :         SQLSMALLINT len;
      36           8 :         int succeeded = 0;
      37             :         bool is_freetds;
      38             :         bool is_ms;
      39             :         SQLRETURN rc;
      40             : 
      41           8 :         if (odbc_read_login_info())
      42           0 :                 exit(1);
      43             : 
      44             :         /*
      45             :          * prepare our odbcinst.ini 
      46             :          * is better to do it before connect cause uniODBC cache INIs
      47             :          * the name must be odbcinst.ini cause unixODBC accept only this name
      48             :          */
      49           8 :         if (odbc_driver[0]) {
      50           8 :                 FILE *f = fopen("odbcinst.ini", "w");
      51             : 
      52           8 :                 if (f) {
      53           8 :                         fprintf(f, "[FreeTDS]\nDriver = %s\n", odbc_driver);
      54           8 :                         fclose(f);
      55             :                         /* force iODBC */
      56           8 :                         setenv("ODBCINSTINI", "./odbcinst.ini", 1);
      57           8 :                         setenv("SYSODBCINSTINI", "./odbcinst.ini", 1);
      58             :                         /* force unixODBC (only directory) */
      59           8 :                         setenv("ODBCSYSINI", ".", 1);
      60             :                 }
      61             :         }
      62             : 
      63           8 :         printf("SQLConnect connect..\n");
      64           8 :         odbc_connect();
      65           8 :         is_freetds = odbc_driver_is_freetds();
      66           8 :         is_ms = odbc_db_is_microsoft();
      67           8 :         odbc_disconnect();
      68           8 :         ++succeeded;
      69             : 
      70           8 :         if (!is_freetds) {
      71           0 :                 printf("Driver is not FreeTDS, exiting\n");
      72           0 :                 odbc_test_skipped();
      73           0 :                 return 0;
      74             :         }
      75             : 
      76             :         /* try connect string with using DSN */
      77           8 :         printf("connect string DSN connect..\n");
      78           8 :         init_connect();
      79           8 :         sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;", odbc_server, odbc_user, odbc_password, odbc_database);
      80           8 :         CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp)/sizeof(SQLTCHAR), &len, SQL_DRIVER_NOPROMPT, "SI");
      81           8 :         odbc_disconnect();
      82           8 :         ++succeeded;
      83             : 
      84             :         /* try connect string using old SERVERNAME specification */
      85           8 :         printf("connect string SERVERNAME connect..\n");
      86           8 :         printf("odbcinst.ini must be configured with FreeTDS driver..\n");
      87             : 
      88             :         /* this is expected to work with unixODBC */
      89           8 :         init_connect();
      90           8 :         sprintf(tmp, "DRIVER=FreeTDS;SERVERNAME=%s;UID=%s;PWD=%s;DATABASE=%s;", odbc_server, odbc_user, odbc_password, odbc_database);
      91           8 :         rc = CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp)/sizeof(SQLTCHAR), &len, SQL_DRIVER_NOPROMPT, "SIE");
      92           8 :         if (rc == SQL_ERROR) {
      93           0 :                 printf("Unable to open data source (ret=%d)\n", rc);
      94             :         } else {
      95             :                 ++succeeded;
      96             :         }
      97           8 :         odbc_disconnect();
      98             : 
      99             :         /* this is expected to work with iODBC
     100             :          * (passing shared object name as driver)
     101             :          */
     102           8 :         if (odbc_driver[0]) {
     103           8 :                 init_connect();
     104           8 :                 sprintf(tmp, "DRIVER=%s;SERVERNAME=%s;UID=%s;PWD=%s;DATABASE=%s;", odbc_driver, odbc_server, odbc_user, odbc_password, odbc_database);
     105           8 :                 rc = CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp)/sizeof(SQLTCHAR), &len, SQL_DRIVER_NOPROMPT, "SIE");
     106           8 :                 if (rc == SQL_ERROR) {
     107           0 :                         printf("Unable to open data source (ret=%d)\n", rc);
     108             :                 } else {
     109           8 :                         ++succeeded;
     110             :                 }
     111           8 :                 odbc_disconnect();
     112             :         }
     113             : 
     114             : #ifdef _WIN32
     115             :         if (get_entry("SERVER")) {
     116             :                 init_connect();
     117             :                 sprintf(tmp, "DRIVER=FreeTDS;SERVER=%s;UID=%s;PWD=%s;DATABASE=%s;", entry, odbc_user, odbc_password, odbc_database);
     118             :                 if (get_entry("TDS_Version"))
     119             :                         sprintf(strchr(tmp, 0), "TDS_Version=%s;", entry);
     120             :                 if (get_entry("Port"))
     121             :                         sprintf(strchr(tmp, 0), "Port=%s;", entry);
     122             :                 rc = CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp)/sizeof(SQLTCHAR), &len, SQL_DRIVER_NOPROMPT, "SIE");
     123             :                 if (rc == SQL_ERROR) {
     124             :                         printf("Unable to open data source (ret=%d)\n", rc);
     125             :                 } else {
     126             :                         ++succeeded;
     127             :                 }
     128             :                 odbc_disconnect();
     129             :         }
     130             : #endif
     131             : 
     132           8 :         if (is_ms) {
     133             :                 char app_name[130];
     134           6 :                 memset(app_name, 'a', sizeof(app_name));
     135           6 :                 app_name[sizeof(app_name) - 1] = 0;
     136             : 
     137             :                 /* Try passing very long APP string.
     138             :                  * The server is supposed to fail the connection if
     139             :                  * this string is too long, make sure we trucate it.
     140             :                  */
     141           6 :                 printf("connect string DSN connect with a long APP..\n");
     142           6 :                 init_connect();
     143           6 :                 sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;APP=%s", odbc_server, odbc_user, odbc_password, odbc_database, app_name);
     144           6 :                 CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp)/sizeof(SQLTCHAR), &len, SQL_DRIVER_NOPROMPT, "SI");
     145           6 :                 odbc_disconnect();
     146             :         }
     147             : 
     148             :         /* at least one should success.. */
     149           8 :         if (succeeded < 3) {
     150           0 :                 ODBC_REPORT_ERROR("Too few successes");
     151           0 :                 exit(1);
     152             :         }
     153             : 
     154           8 :         printf("Done.\n");
     155           8 :         return 0;
     156             : }

Generated by: LCOV version 1.13