LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0007.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 89 132 67.4 %
Date: 2025-07-26 13:53:51 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test binding strings and ints, attempt 2nd query with results pending. 
       3             :  * Functions: dbbind dbcmd dbnextrow dbopen dbresults dbsqlexec 
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : static void
       9          10 : create_tables(DBPROCESS * dbproc, int rows_to_add)
      10             : {
      11             :         int i;
      12             : 
      13          10 :         printf("creating table\n");
      14          10 :         sql_cmd(dbproc);
      15          10 :         dbsqlexec(dbproc);
      16          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      17             :                 /* nop */
      18             :         }
      19             : 
      20          10 :         printf("insert\n");
      21         100 :         for (i = 1; i < rows_to_add; i++) {
      22          90 :                 sql_cmd(dbproc);
      23          90 :                 dbsqlexec(dbproc);
      24          90 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
      25             :                         /* nop */
      26             :                 }
      27             :         }
      28          10 : }
      29             : 
      30             : 
      31             : static int
      32          40 : start_query(DBPROCESS * dbproc)
      33             : {
      34             :         int i;
      35             : 
      36          40 :         if (SUCCEED != sql_cmd(dbproc)) {
      37             :                 return 0;
      38             :         }
      39          40 :         if (SUCCEED != dbsqlexec(dbproc)) {
      40             :                 return 0;
      41             :         }
      42             : 
      43          30 :         if (dbresults(dbproc) != SUCCEED)
      44             :                 return 0;
      45             : 
      46          90 :         for (i = 1; i <= dbnumcols(dbproc); i++)
      47          90 :                 printf("col %d is named \"%s\"\n", i, dbcolname(dbproc, i));
      48             : 
      49             :         return 1;
      50             : }
      51             : 
      52             : static const char *
      53           0 : hex_buffer(BYTE *binarybuffer, int size, char *textbuf)
      54             : {
      55             :         int i;
      56           0 :         strcpy(textbuf, "0x");  /* must be large enough */
      57           0 :         for (i = 0; i < size; i++) {
      58           0 :                 sprintf(textbuf + 2 + i * 2, "%02X", binarybuffer[i]);
      59             :         }
      60           0 :         return textbuf;  /* convenience */
      61             : }
      62             : 
      63          10 : TEST_MAIN()
      64             : {
      65             :         LOGINREC *login;
      66             :         DBPROCESS *dbproc;
      67             :         int i;
      68             :         char teststr[1024], teststr2[1024];
      69             :         DBINT testint, binvaluelength;
      70             :         DBVARYBIN  testvbin, testvbin2;
      71             :         DBVARYCHAR testvstr;
      72             :         BYTE testbin[10];
      73          10 :         int failed = 0;
      74             :         int expected_error;
      75             : 
      76          10 :         set_malloc_options();
      77             : 
      78          10 :         read_login_info(argc, argv);
      79             : 
      80          10 :         printf("Starting %s\n", argv[0]);
      81             : 
      82          10 :         dbinit();
      83             : 
      84          10 :         dberrhandle(syb_err_handler);
      85          10 :         dbmsghandle(syb_msg_handler);
      86             : 
      87          10 :         printf("About to logon\n");
      88             : 
      89          10 :         login = dblogin();
      90          10 :         DBSETLPWD(login, PASSWORD);
      91          10 :         DBSETLUSER(login, USER);
      92          10 :         DBSETLAPP(login, "t0007");
      93             : 
      94          10 :         printf("About to open\n");
      95             : 
      96          10 :         dbproc = dbopen(login, SERVER);
      97          10 :         if (strlen(DATABASE))
      98          10 :                 dbuse(dbproc, DATABASE);
      99          10 :         dbloginfree(login);
     100             : 
     101          10 :         create_tables(dbproc, 10);
     102             : 
     103          10 :         if (!start_query(dbproc)) {
     104           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     105           0 :                 failed = 1;
     106             :         }
     107             : 
     108          10 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
     109          10 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
     110             : 
     111          30 :         for (i = 1; i <= 2; i++) {
     112             :                 char expected[1024];
     113             : 
     114          20 :                 sprintf(expected, "row %07d", i);
     115             : 
     116          20 :                 if (i % 5 == 0) {
     117           0 :                         dbclrbuf(dbproc, 5);
     118             :                 }
     119             : 
     120          20 :                 testint = -1;
     121          20 :                 strcpy(teststr, "bogus");
     122             : 
     123          20 :                 if (REG_ROW != dbnextrow(dbproc)) {
     124           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     125           0 :                         abort();
     126             :                 }
     127          20 :                 if (testint != i) {
     128           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     129           0 :                         abort();
     130             :                 }
     131          20 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
     132           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
     133           0 :                         abort();
     134             :                 }
     135          20 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     136             :         }
     137             : 
     138             : 
     139          10 :         printf("second select.  Should fail.\n");
     140             : 
     141          10 :         expected_error = 20019;
     142          10 :         dbsetuserdata(dbproc, (BYTE*) &expected_error);
     143             : 
     144          10 :         if (start_query(dbproc)) {
     145           0 :                 fprintf(stderr, "%s:%d: start_query should have failed but didn't\n", __FILE__, __LINE__);
     146           0 :                 failed = 1;
     147             :         }
     148             : 
     149          10 :         dbcancel(dbproc);
     150             :         
     151             :         /* 
     152             :          * Test Binary binding
     153             :          */
     154          10 :         if (!start_query(dbproc)) {
     155           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     156           0 :                 failed = 1;
     157             :         }
     158             : 
     159          10 :         dbbind(dbproc, 1, VARYBINBIND, sizeof(testvbin), (BYTE *) &testvbin);
     160          10 :         dbbind(dbproc, 2, VARYCHARBIND, sizeof(testvstr), (BYTE *) &testvstr);
     161          10 :         dbbind(dbproc, 3, BINARYBIND, sizeof(testint), (BYTE *) &testint);
     162             : 
     163          30 :         for (i = 1; i <= 2; i++) {
     164             :                 char expected[1024];
     165             : 
     166          20 :                 sprintf(expected, "row %07d ", i);
     167             : 
     168          20 :                 testint = -1;
     169          20 :                 memset(&testvbin, '*', sizeof(testvbin));
     170          20 :                 memset(&testvstr, '*', sizeof(testvstr));
     171             : 
     172          20 :                 if (REG_ROW != dbnextrow(dbproc)) {
     173           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     174           0 :                         abort();
     175             :                 }
     176          20 :                 if (testint != i) {
     177           0 :                         fprintf(stderr, "Failed, line %d.  Expected i to be %d, was %d (0x%x)\n", __LINE__, i, (int) testint, (int) testint);
     178           0 :                         abort();
     179             :                 }
     180          20 :                 if (testvbin.len != sizeof(testint)) {
     181           0 :                         fprintf(stderr, "Failed, line %d.  Expected bin length to be %d, was %d\n", __LINE__, (int) sizeof(testint), (int) testvbin.len);
     182           0 :                         abort();
     183             :                 }
     184          20 :                 memcpy(&testint, testvbin.array, sizeof(testint));
     185          20 :                 if (testint != i) {
     186           0 :                         fprintf(stderr, "Failed, line %d.  Expected i to be %d, was %d (0x%x)\n", __LINE__, i, (int) testint, (int) testint);
     187           0 :                         abort();
     188             :                 }
     189          20 :                 if (testvstr.len != strlen(expected) || 0 != strncmp(testvstr.str, expected, strlen(expected))) {
     190           0 :                         printf("Failed, line %d.  Expected s to be |%s|, was |%s|\n", __LINE__, expected, testvstr.str);
     191           0 :                         abort();
     192             :                 }
     193          20 :                 testvstr.str[testvstr.len] = 0;
     194          20 :                 printf("Read a row of data -> %d %s\n", (int) testint, testvstr.str);
     195             :         }
     196             : 
     197          10 :         dbcancel(dbproc);
     198             : 
     199             :         /*
     200             :          * Test var binary bindings of binary values
     201             :          */
     202          10 :         if (!start_query(dbproc)) {
     203           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     204           0 :                 failed = 1;
     205             :         }
     206             : 
     207          10 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) &binvaluelength);  /* returned binary string length (all columns) */
     208          10 :         dbbind(dbproc, 2, VARYBINBIND, sizeof(testvbin), (BYTE *) &testvbin);  /* returned as varbinary, bound varbinary */
     209          10 :         dbbind(dbproc, 3, VARYBINBIND, sizeof(testvbin2), (BYTE *) &testvbin2); /* returned as binary, bound varbinary */
     210          10 :         dbbind(dbproc, 4, BINARYBIND, sizeof(testbin), (BYTE *) &testbin);  /* returned as varbinary, bound binary */
     211             : 
     212          10 :         memset(&testvbin, '*', sizeof(testvbin));
     213          10 :         memset(&testvbin2, '*', sizeof(testvbin2));
     214          10 :         memset(&testbin, '@', sizeof(testbin));  /* different. After fetch all buffers should have same value */
     215             : 
     216          10 :         if (REG_ROW != dbnextrow(dbproc)) {
     217           0 :                 fprintf(stderr, "Failed.  Expected a row\n");
     218           0 :                 abort();
     219             :         }
     220             : 
     221          10 :         if (testvbin.len != binvaluelength) {
     222           0 :                 fprintf(stderr, "Failed, line %d.  Expected bin length to be %d, was %d\n", __LINE__,
     223             :                         (int) binvaluelength, (int) testvbin.len);
     224           0 :                 abort();
     225             :         }
     226             : 
     227          10 :         if (testvbin2.len != binvaluelength) {
     228           0 :                 fprintf(stderr, "Failed, line %d.  Expected bin length to be %d, was %d\n", __LINE__,
     229             :                         (int) binvaluelength, (int) testvbin.len);
     230           0 :                 abort();
     231             :         }
     232             : 
     233          10 :         if (memcmp(testvbin.array, testbin, binvaluelength) != 0) {
     234           0 :                 fprintf(stderr, "Failed, line %d.  Expected buffer to be %s, was %s\n", __LINE__, 
     235             :                         hex_buffer(testbin, binvaluelength, teststr),
     236             :                         hex_buffer(testvbin.array, binvaluelength, teststr2));
     237           0 :                 abort();
     238             :         }
     239             : 
     240          10 :         if (memcmp(testvbin2.array, testbin, binvaluelength) != 0) {
     241           0 :                 fprintf(stderr, "Failed, line %d.  Expected buffer to be %s, was %s\n", __LINE__,
     242             :                         hex_buffer(testbin, binvaluelength, teststr),
     243             :                         hex_buffer(testvbin2.array, binvaluelength, teststr2));
     244           0 :                 abort();
     245             :         }
     246             : 
     247          10 :         memset(teststr2, 0, sizeof(teststr2));  /* finally, test binary padding is all zeroes */
     248          10 :         if (memcmp(testbin + binvaluelength, teststr2, sizeof(testbin) - binvaluelength) != 0) {
     249           0 :                 fprintf(stderr, "Failed, line %d.  Expected binary padding to be zeroes, was %s\n", __LINE__,
     250           0 :                         hex_buffer(testbin + binvaluelength, sizeof(testbin) - binvaluelength, teststr));
     251           0 :                 abort();
     252             :         }
     253             : 
     254          10 :         dbexit();
     255             : 
     256          10 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     257          10 :         return failed ? 1 : 0;
     258             : }

Generated by: LCOV version 1.13