LCOV - code coverage report
Current view: top level - src/dblib/unittests - string_bind.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 86 100 86.0 %
Date: 2025-02-21 09:36:06 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Purpose: Test different string binding combinations.
       3             :  * Functions: dbbind
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : static DBPROCESS *dbproc = NULL;
       9             : static int bind_len = -1;
      10             : static int expected_error = 0;
      11             : static const char *select_cmd = "select 'foo  '";
      12             : 
      13             : static void
      14         260 : test_row(int vartype, const char *vartype_name, const char *expected, int line)
      15             : {
      16             :         char str[11];
      17             :         int i;
      18             : 
      19         260 :         printf("%d: row type %s bind len %d\n", line, vartype_name, bind_len);
      20             : 
      21         260 :         if (dbcmd(dbproc, select_cmd) != SUCCEED) {
      22           0 :                 fprintf(stderr, "error: dbcmd\n");
      23           0 :                 exit(1);
      24             :         }
      25         260 :         dbsqlexec(dbproc);
      26             : 
      27         260 :         if (dbresults(dbproc) != SUCCEED) {
      28           0 :                 fprintf(stderr, "error: expected a result set, none returned.\n");
      29           0 :                 exit(1);
      30             :         }
      31             : 
      32         260 :         memset(str, '$', sizeof(str));
      33         260 :         str[sizeof(str) - 1] = 0;
      34         260 :         if (dbbind(dbproc, 1, vartype, bind_len, (BYTE *) str) != SUCCEED) {
      35           0 :                 fprintf(stderr, "Had problem with bind\n");
      36           0 :                 exit(1);
      37             :         }
      38         260 :         if (dbnextrow(dbproc) != REG_ROW) {
      39           0 :                 fprintf(stderr, "Failed.  Expected a row\n");
      40           0 :                 exit(1);
      41             :         }
      42             : 
      43         260 :         assert(str[sizeof(str) - 1] == 0);
      44         260 :         if (vartype == CHARBIND) {
      45             :                 /* not terminated space padded */
      46          90 :                 char *p = strchr(str, '$');
      47          90 :                 i = p ? p - str : sizeof(str);
      48             :         } else {
      49             :                 /* terminated */
      50         170 :                 char *p = strchr(str, 0);
      51         170 :                 i = p - str + 1;
      52             :         }
      53        1460 :         for (; i < sizeof(str)-1; ++i) {
      54        1460 :                 assert(str[i] == '$');
      55        1460 :                 str[i] = 0;
      56             :         }
      57             : 
      58         260 :         printf("str '%s'\n", str);
      59         260 :         if (strcmp(str, expected) != 0) {
      60           0 :                 fprintf(stderr, "Expected '%s' string\n", expected);
      61           0 :                 exit(1);
      62             :         }
      63             : 
      64         260 :         if (dbnextrow(dbproc) != NO_MORE_ROWS) {
      65           0 :                 fprintf(stderr, "Was expecting no more rows\n");
      66           0 :                 exit(1);
      67             :         }
      68         260 :         assert(expected_error == 0);
      69         260 : }
      70             : 
      71             : #define row(bind, expected) test_row(bind, #bind, expected, __LINE__)
      72             : 
      73             : int
      74          10 : main(int argc, char **argv)
      75             : {
      76             :         LOGINREC *login;
      77             : 
      78          10 :         set_malloc_options();
      79             : 
      80          10 :         read_login_info(argc, argv);
      81             : 
      82          10 :         printf("Starting %s\n", argv[0]);
      83             : 
      84          10 :         dbinit();
      85             : 
      86          10 :         dberrhandle(syb_err_handler);
      87          10 :         dbmsghandle(syb_msg_handler);
      88             : 
      89          10 :         printf("About to logon as \"%s\"\n", USER);
      90             : 
      91          10 :         login = dblogin();
      92          10 :         DBSETLPWD(login, PASSWORD);
      93          10 :         DBSETLUSER(login, USER);
      94          10 :         DBSETLAPP(login, "string_bind");
      95             : 
      96          10 :         printf("About to open \"%s\"\n", SERVER);
      97             : 
      98          10 :         dbproc = dbopen(login, SERVER);
      99          10 :         if (!dbproc) {
     100           0 :                 fprintf(stderr, "Unable to connect to %s\n", SERVER);
     101           0 :                 return 1;
     102             :         }
     103          10 :         dbloginfree(login);
     104             : 
     105          10 :         dbsetuserdata(dbproc, (BYTE*) &expected_error);
     106             : 
     107          10 :         row(NTBSTRINGBIND, "foo");
     108          10 :         row(STRINGBIND, "foo  ");
     109          10 :         row(CHARBIND, "foo  ");
     110             : 
     111          10 :         bind_len = 4;
     112          10 :         row(NTBSTRINGBIND, "foo");
     113          10 :         expected_error = SYBECOFL;
     114          10 :         row(STRINGBIND, "foo");
     115          10 :         expected_error = SYBECOFL;
     116          10 :         row(CHARBIND, "foo ");
     117             : 
     118          10 :         bind_len = 5;
     119          10 :         row(NTBSTRINGBIND, "foo");
     120          10 :         expected_error = SYBECOFL;
     121          10 :         row(STRINGBIND, "foo ");
     122          10 :         row(CHARBIND, "foo  ");
     123             : 
     124          10 :         bind_len = 8;
     125          10 :         row(NTBSTRINGBIND, "foo");
     126          10 :         row(STRINGBIND, "foo    ");
     127          10 :         row(CHARBIND, "foo     ");
     128             : 
     129          10 :         bind_len = 3;
     130          10 :         expected_error = SYBECOFL;
     131          10 :         row(NTBSTRINGBIND, "fo");
     132             : 
     133          10 :         select_cmd = "select 123";
     134             : 
     135          10 :         bind_len = -1;
     136          10 :         row(NTBSTRINGBIND, "123");
     137          10 :         row(STRINGBIND, "123");
     138          10 :         row(CHARBIND, "123");
     139             : 
     140          10 :         bind_len = 4;
     141          10 :         row(NTBSTRINGBIND, "123");
     142          10 :         row(STRINGBIND, "123");
     143          10 :         row(CHARBIND, "123 ");
     144             : 
     145          10 :         bind_len = 6;
     146          10 :         row(NTBSTRINGBIND, "123");
     147          10 :         row(STRINGBIND, "123  ");
     148          10 :         row(CHARBIND, "123   ");
     149             : 
     150          10 :         bind_len = 3;
     151          10 :         expected_error = SYBECOFL;
     152          10 :         row(NTBSTRINGBIND, "12");
     153          10 :         expected_error = SYBECOFL;
     154          10 :         row(STRINGBIND, "12");
     155          10 :         row(CHARBIND, "123");
     156             : 
     157          10 :         bind_len = 2;
     158          10 :         expected_error = SYBECOFL;
     159          10 :         row(CHARBIND, "12");
     160             : 
     161          10 :         dbclose(dbproc);
     162             : 
     163          10 :         dbexit();
     164          10 :         return 0;
     165             : }

Generated by: LCOV version 1.13