LCOV - code coverage report
Current view: top level - src/dblib/unittests - bcp_bind.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 96 101 95.0 %
Date: 2026-11-25 04:57:11 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Purpose: Test bcp_bind argument checks, results were compared with Sybase library (DBVERSION_100)
       3             :  * and MS ODBC bcp functions
       4             :  * Functions: bcp_bind bcp_done bcp_init bcp_sendrow
       5             :  */
       6             : 
       7             : #include "common.h"
       8             : 
       9             : #include <assert.h>
      10             : 
      11             : static DBPROCESS *dbproc;
      12             : 
      13             : static void
      14         550 : exec_sql(const char *sql)
      15             : {
      16             :         RETCODE rc;
      17             : 
      18         550 :         printf("exec: %s\n", sql);
      19         550 :         dbcmd(dbproc, sql);
      20         550 :         if (dbsqlexec(dbproc) == FAIL) {
      21           0 :                 fprintf(stderr, "failed: %s\n", sql);
      22           0 :                 exit(1);
      23             :         }
      24        1116 :         while ((rc = dbresults(dbproc)) == SUCCEED)
      25         566 :                 while (dbnextrow(dbproc) != NO_MORE_ROWS)
      26           0 :                         continue;
      27         550 :         assert(rc == NO_MORE_RESULTS);
      28         550 : }
      29             : 
      30             : /* return value read back from table, "NULL" for null, "none" if no rows */
      31             : static const char *
      32         290 : read_back(const char *table)
      33             : {
      34             :         static char buf[64];
      35             :         char sql[128];
      36         290 :         int rows = 0;
      37             : 
      38         290 :         sprintf(sql, "select convert(varchar(40), x) from %s", table);
      39         290 :         dbcmd(dbproc, sql);
      40         290 :         if (dbsqlexec(dbproc) == FAIL)
      41             :                 return "error";
      42         290 :         strcpy(buf, "none");
      43         870 :         while (dbresults(dbproc) == SUCCEED) {
      44         580 :                 while (dbnextrow(dbproc) == REG_ROW) {
      45         290 :                         BYTE *data = dbdata(dbproc, 1);
      46         290 :                         DBINT len = dbdatlen(dbproc, 1);
      47             : 
      48         290 :                         if (data == NULL)
      49          30 :                                 strcpy(buf, "NULL");
      50             :                         else
      51         260 :                                 sprintf(buf, "%.*s", (int) len, (const char *) data);
      52         290 :                         ++rows;
      53             :                 }
      54             :         }
      55         290 :         if (rows > 1)
      56             :                 return "too many rows";
      57         290 :         return buf;
      58             : }
      59             : 
      60             : typedef struct
      61             : {
      62             :         int line;
      63             :         const char *table;
      64             :         int vartype;
      65             :         const void *varaddr;
      66             :         int prefixlen;
      67             :         DBINT varlen;
      68             :         const char *terminator;
      69             :         int termlen;
      70             :         /* error expected from bcp_bind or bcp_sendrow, 0 if none */
      71             :         int bind_err;
      72             :         int send_err;
      73             :         /* data expected in the table, if row inserted */
      74             :         const char *result;
      75             : } test_case;
      76             : 
      77             : static void
      78         430 : test(const test_case *t)
      79             : {
      80             :         char table_name[64];
      81             :         int msgno;
      82             :         RETCODE rc;
      83             :         DBINT rows;
      84             : 
      85         430 :         printf("Test from line %d\n", t->line);
      86             : 
      87         430 :         sprintf(table_name, "truncate table %s", t->table);
      88         430 :         exec_sql(table_name);
      89             : 
      90         430 :         sprintf(table_name, "%s", t->table);
      91         430 :         rc = bcp_init(dbproc, table_name, NULL, NULL, DB_IN);
      92         430 :         assert(rc == SUCCEED);
      93             : 
      94         430 :         msgno = t->bind_err;
      95         430 :         dbsetuserdata(dbproc, (BYTE *) & msgno);
      96         430 :         rc = bcp_bind(dbproc, (BYTE *) t->varaddr, t->prefixlen, t->varlen, (BYTE *) t->terminator, t->termlen, t->vartype, 1);
      97         430 :         dbsetuserdata(dbproc, NULL);
      98         430 :         if (t->bind_err) {
      99         110 :                 assert(rc == FAIL);
     100         110 :                 assert(msgno == 0);
     101         110 :                 bcp_done(dbproc);
     102         110 :                 return;
     103             :         }
     104         320 :         assert(rc == SUCCEED);
     105             : 
     106         320 :         msgno = t->send_err;
     107         320 :         dbsetuserdata(dbproc, (BYTE *) & msgno);
     108         320 :         rc = bcp_sendrow(dbproc);
     109         320 :         dbsetuserdata(dbproc, NULL);
     110         320 :         if (t->send_err) {
     111          30 :                 assert(rc == FAIL);
     112          30 :                 assert(msgno == 0);
     113             :         } else {
     114         290 :                 assert(rc == SUCCEED);
     115             :         }
     116         320 :         rows = bcp_done(dbproc);
     117         320 :         assert(rows == (t->send_err ? 0 : 1));
     118             : 
     119         320 :         if (t->result && strcmp(read_back(t->table), t->result) != 0) {
     120           0 :                 fprintf(stderr, "line %d: wrong result '%s' expected '%s'\n", t->line, read_back(t->table), t->result);
     121           0 :                 exit(1);
     122             :         }
     123             : }
     124             : 
     125             : static DBINT int_value = 123456;
     126             : static DBINT int_value256 = 256;
     127             : static DBFLT8 flt_value = 1234.0;
     128             : static DBDATETIME dt_value;
     129             : static DBDECIMAL dec_value;
     130             : static unsigned char int_prefix4[5];
     131             : static unsigned char int_prefix8[9];
     132             : static char str_value[] = "hello world";
     133             : static const unsigned char str_prefix[] = "\005hello world";
     134             : 
     135             : static const test_case tests[] = {
     136             :         /* fixed type, varlen must be -1, 0 (NULL) or type size, checked when sending */
     137             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, -1, NULL, 0, 0, 0, "123456"},
     138             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, 0, NULL, 0, 0, 0, "NULL"},
     139             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, 4, NULL, 0, 0, 0, "123456"},
     140             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, 1, NULL, 0, 0, SYBEBCIS, NULL},
     141             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, 10, NULL, 0, 0, SYBEBCIS, NULL},
     142             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, -1, NULL, -1, 0, 0, "123456"},
     143             : 
     144             :         /* fixed type with prefix, a bigger length is accepted */
     145             :         {__LINE__, "bcp_bind_int", SYBINT4, int_prefix4, 1, -1, NULL, 0, 0, 0, "123456"},
     146             :         {__LINE__, "bcp_bind_int", SYBINT4, int_prefix8, 1, -1, NULL, 0, 0, 0, "123456"},
     147             :         {__LINE__, "bcp_bind_int", SYBINT4, int_prefix4, 1, 10, NULL, 0, 0, 0, "123456"},
     148             :         {__LINE__, "bcp_bind_int", SYBINT4, int_prefix8, 1, 4, NULL, 0, 0, 0, "123456"},
     149             :         {__LINE__, "bcp_bind_int", SYBINT4, int_prefix4, 1, 2, NULL, 0, 0, SYBEBCIS, NULL},
     150             : 
     151             :         /*
     152             :          * terminators are allowed for all types, for types other than
     153             :          * character and binary they are ignored
     154             :          */
     155             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, -1, "", 1, 0, 0, "123456"},
     156             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value256, 0, -1, "", 1, 0, 0, "256"},
     157             :         {__LINE__, "bcp_bind_int", SYBFLT8, &flt_value, 0, -1, "", 1, 0, 0, "1234"},
     158             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, -1, NULL, 1, SYBEBCIT, 0, NULL},
     159             :         {__LINE__, "bcp_bind_int", SYBINT4, &int_value, 0, -1, "", 0, 0, 0, "123456"},
     160             :         {__LINE__, "bcp_bind_dt", SYBDATETIME, &dt_value, 0, -1, "", 1, 0, 0, "Nov 10 2026 12:00PM"},
     161             :         {__LINE__, "bcp_bind_dt", SYBDATETIME, &dt_value, 0, -1, NULL, 1, SYBEBCIT, 0, NULL},
     162             :         {__LINE__, "bcp_bind_num", SYBDECIMAL, &dec_value, 0, -1, "", 1, 0, 0, "123.45"},
     163             :         {__LINE__, "bcp_bind_num", SYBDECIMAL, &dec_value, 0, -1, NULL, 1, SYBEBCIT, 0, NULL},
     164             : 
     165             :         /* variable types require length, prefix or terminator */
     166             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, 5, NULL, 0, 0, 0, "hello"},
     167             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, 5, NULL, -1, 0, 0, "hello"},
     168             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, -1, NULL, 0, SYBEVDPT, 0, NULL},
     169             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, -1, NULL, -1, SYBEVDPT, 0, NULL},
     170             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, -1, NULL, 1, SYBEVDPT, 0, NULL},
     171             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, -1, "", 0, SYBEVDPT, 0, NULL},
     172             :         {__LINE__, "bcp_bind_str", SYBCHAR, str_value, 0, -1, NULL, 0, SYBEVDPT, 0, NULL},
     173             :         {__LINE__, "bcp_bind_str", SYBBINARY, str_value, 0, -1, NULL, 0, SYBEVDPT, 0, NULL},
     174             :         {__LINE__, "bcp_bind_str", SYBVARBINARY, str_value, 0, -1, NULL, 0, SYBEVDPT, 0, NULL},
     175             :         {__LINE__, "bcp_bind_str", SYBTEXT, str_value, 0, -1, NULL, 0, SYBEVDPT, 0, NULL},
     176             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, 0, NULL, 0, 0, 0, "NULL"},
     177             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, 0, "", 1, 0, 0, "NULL"},
     178             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, -1, "", 1, 0, 0, "hello world"},
     179             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_value, 0, 5, "", 1, 0, 0, "hello"},
     180             :         {__LINE__, "bcp_bind_str", SYBCHAR, str_value, 0, -1, "", 1, 0, 0, "hello world"},
     181             :         {__LINE__, "bcp_bind_str", SYBTEXT, str_value, 0, -1, "", 1, 0, 0, "hello world"},
     182             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_prefix, 1, -1, NULL, 0, 0, 0, "hello"},
     183             : 
     184             :         /* for variable types prefix takes precedence over length */
     185             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_prefix, 1, 3, NULL, 0, 0, 0, "hello"},
     186             :         {__LINE__, "bcp_bind_str", SYBVARCHAR, str_prefix, 1, 8, NULL, 0, 0, 0, "hello"},
     187             : 
     188             :         /* binary data are copied as is to character columns */
     189             :         {__LINE__, "bcp_bind_str", SYBVARBINARY, str_value, 0, 11, NULL, 0, 0, 0, "hello world"},
     190             :         {__LINE__, "bcp_bind_str", SYBBINARY, str_value, 0, -1, "", 1, 0, 0, "hello world"},
     191             :         {__LINE__, "bcp_bind_str", SYBVARBINARY, str_prefix, 1, -1, NULL, 0, 0, 0, "hello"},
     192             :         {__LINE__, "bcp_bind_str", SYBIMAGE, str_value, 0, 5, NULL, 0, 0, 0, "hello"},
     193             : };
     194             : 
     195          10 : TEST_MAIN()
     196             : {
     197             :         LOGINREC *login;
     198             :         DBTYPEINFO ti;
     199             :         unsigned i;
     200             : 
     201          10 :         int_prefix4[0] = 4;
     202          10 :         memcpy(int_prefix4 + 1, &int_value, 4);
     203          10 :         memcpy(int_prefix8, int_prefix4, 5);
     204          10 :         int_prefix8[0] = 8;
     205             : 
     206          10 :         set_malloc_options();
     207             : 
     208          10 :         read_login_info(argc, argv);
     209             : 
     210          10 :         printf("Starting %s\n", argv[0]);
     211             : 
     212          10 :         dbsetversion(DBVERSION_100);
     213          10 :         dbinit();
     214             : 
     215          10 :         dberrhandle(syb_err_handler);
     216          10 :         dbmsghandle(syb_msg_handler);
     217             : 
     218          10 :         login = dblogin();
     219          10 :         DBSETLPWD(login, PASSWORD);
     220          10 :         DBSETLUSER(login, USER);
     221          10 :         DBSETLAPP(login, "bcp_bind.c unit test");
     222          10 :         BCP_SETL(login, TRUE);
     223             : 
     224          10 :         dbproc = dbopen(login, SERVER);
     225          10 :         if (strlen(DATABASE))
     226          10 :                 dbuse(dbproc, DATABASE);
     227          10 :         dbloginfree(login);
     228             : 
     229          10 :         exec_sql("if exists (select 1 from sysobjects where type = 'U' and name = 'bcp_bind_int') drop table bcp_bind_int");
     230          10 :         exec_sql("if exists (select 1 from sysobjects where type = 'U' and name = 'bcp_bind_str') drop table bcp_bind_str");
     231          10 :         exec_sql("create table bcp_bind_int (x int null)");
     232          10 :         exec_sql("create table bcp_bind_str (x varchar(20) null)");
     233          10 :         exec_sql("if exists (select 1 from sysobjects where type = 'U' and name = 'bcp_bind_dt') drop table bcp_bind_dt");
     234          10 :         exec_sql("if exists (select 1 from sysobjects where type = 'U' and name = 'bcp_bind_num') drop table bcp_bind_num");
     235          10 :         exec_sql("create table bcp_bind_dt (x datetime null)");
     236          10 :         exec_sql("create table bcp_bind_num (x numeric(10,2) null)");
     237             : 
     238             :         /* values with zero bytes inside, to check terminators are ignored */
     239          10 :         ti.precision = 10;
     240          10 :         ti.scale = 2;
     241          10 :         assert(dbconvert_ps(dbproc, SYBVARCHAR, (const BYTE *) "123.45", -1, SYBDECIMAL, (BYTE *) & dec_value,
     242             :                             sizeof(dec_value), &ti) > 0);
     243          10 :         assert(dbconvert(dbproc, SYBVARCHAR, (const BYTE *) "2026-11-10 12:00:00", -1, SYBDATETIME, (BYTE *) & dt_value,
     244             :                          sizeof(dt_value)) > 0);
     245          10 :         assert(memchr(&dt_value, 0, sizeof(dt_value)) != NULL);
     246          10 :         assert(memchr(&dec_value, 0, sizeof(dec_value)) != NULL);
     247             : 
     248         430 :         for (i = 0; i < TDS_VECTOR_SIZE(tests); ++i)
     249         430 :                 test(&tests[i]);
     250             : 
     251          10 :         exec_sql("drop table bcp_bind_int");
     252          10 :         exec_sql("drop table bcp_bind_str");
     253          10 :         exec_sql("drop table bcp_bind_dt");
     254          10 :         exec_sql("drop table bcp_bind_num");
     255             : 
     256          10 :         dbexit();
     257             : 
     258          10 :         printf("%s OK\n", __FILE__);
     259          10 :         return 0;
     260             : }

Generated by: LCOV version 1.13