LCOV - code coverage report
Current view: top level - src/odbc/unittests - connect2.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 62 67 92.5 %
Date: 2026-11-10 08:16:44 Functions: 6 6 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /*
       4             :  * Test setting current "catalog" before and after connection using
       5             :  * either SQLConnect and SQLDriverConnect
       6             :  */
       7             : 
       8             : static int failed = 0;
       9             : 
      10             : static void init_connect(void);
      11             : 
      12             : static void
      13          40 : init_connect(void)
      14             : {
      15          40 :         CHKAllocEnv(&odbc_env, "S");
      16          40 :         CHKAllocConnect(&odbc_conn, "S");
      17          40 : }
      18             : 
      19             : static void
      20          20 : normal_connect(void)
      21             : {
      22          20 :         CHKConnect(T(common_pwd.server), SQL_NTS, T(common_pwd.user), SQL_NTS, T(common_pwd.password), SQL_NTS, "SI");
      23          20 : }
      24             : 
      25             : static void
      26          20 : driver_connect(const char *conn_str)
      27             : {
      28             :         SQLTCHAR tmp[1024];
      29             :         SQLSMALLINT len;
      30             : 
      31          20 :         CHKDriverConnect(NULL, T(conn_str), SQL_NTS, tmp, TDS_VECTOR_SIZE(tmp), &len, SQL_DRIVER_NOPROMPT, "SI");
      32          20 : }
      33             : 
      34             : static void
      35          60 : check_dbname(const char *dbname)
      36             : {
      37             :         SQLINTEGER len;
      38             :         SQLTCHAR out[512];
      39             :         char sql[1024];
      40             : 
      41          60 :         len = sizeof(out);
      42          60 :         CHKGetConnectAttr(SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER) out, sizeof(out), &len, "SI");
      43             : 
      44          60 :         if (strcmp(C(out), dbname) != 0) {
      45           0 :                 fprintf(stderr, "Current database (%s) is not %s\n", C(out), dbname);
      46           0 :                 failed = 1;
      47             :         }
      48             : 
      49          60 :         sprintf(sql, "IF DB_NAME() <> '%s' SELECT 1", dbname);
      50          60 :         CHKAllocStmt(&odbc_stmt, "S");
      51          60 :         odbc_check_no_row(sql);
      52          60 :         SQLFreeStmt(odbc_stmt, SQL_DROP);
      53          60 :         odbc_stmt = SQL_NULL_HSTMT;
      54          60 : }
      55             : 
      56             : static void
      57          50 : set_dbname(const char *dbname)
      58             : {
      59          50 :         CHKSetConnectAttr(SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER) T(dbname), (SQLINTEGER) strlen(dbname) * sizeof(SQLTCHAR), "SI");
      60          50 : }
      61             : 
      62          10 : TEST_MAIN()
      63             : {
      64             :         char tmp[1024 * 3];
      65             : 
      66          10 :         odbc_use_version3 = false;
      67             : 
      68          10 :         if (odbc_read_login_info())
      69           0 :                 exit(1);
      70             : 
      71             :         /* try setting db name before connect */
      72          10 :         printf("SQLConnect before (master)..\n");
      73          10 :         init_connect();
      74          10 :         set_dbname("master");
      75          10 :         normal_connect();
      76          10 :         check_dbname("master");
      77             : 
      78             :         /* check changing db name after connection */
      79          10 :         printf("SQLConnect after..\n");
      80          10 :         set_dbname("tempdb");
      81          10 :         check_dbname("tempdb");
      82             : 
      83          10 :         printf("SQLConnect after not existing..\n");
      84          10 :         strcpy(tmp, "IDontExist");
      85          10 :         CHKSetConnectAttr(SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER) tmp, (SQLINTEGER) strlen(tmp), "E");
      86          10 :         check_dbname("tempdb");
      87             : 
      88          10 :         odbc_disconnect();
      89             : 
      90             :         /* try setting db name before connect */
      91          10 :         printf("SQLConnect before (tempdb)..\n");
      92          10 :         init_connect();
      93          10 :         set_dbname("tempdb");
      94          10 :         normal_connect();
      95          10 :         check_dbname("tempdb");
      96          10 :         odbc_disconnect();
      97             : 
      98             :         /* try with connect string using DSN */
      99          10 :         printf("SQLDriverConnect before 1..\n");
     100          10 :         sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;", common_pwd.server,
     101             :                 common_pwd.user, common_pwd.password, common_pwd.database);
     102          10 :         init_connect();
     103          10 :         set_dbname("master");
     104          10 :         driver_connect(tmp);
     105          10 :         check_dbname(common_pwd.database);
     106          10 :         odbc_disconnect();
     107             : 
     108             :         /* try with connect string using DSN */
     109          10 :         printf("SQLDriverConnect before 2..\n");
     110          10 :         sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;", common_pwd.server, common_pwd.user, common_pwd.password);
     111          10 :         init_connect();
     112          10 :         set_dbname("tempdb");
     113          10 :         driver_connect(tmp);
     114          10 :         check_dbname("tempdb");
     115          10 :         odbc_disconnect();
     116             : 
     117          10 :         if (failed) {
     118           0 :                 printf("Some tests failed\n");
     119           0 :                 return 1;
     120             :         }
     121             : 
     122          10 :         printf("Done.\n");
     123          10 :         return 0;
     124             : }

Generated by: LCOV version 1.13