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

Generated by: LCOV version 1.13