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

Generated by: LCOV version 1.13