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

Generated by: LCOV version 1.13