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-02-21 09:36:06 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             : int
      64          10 : main(int argc, char **argv)
      65             : {
      66             :         LOGINREC *login;
      67             :         DBPROCESS *dbproc;
      68             :         int i;
      69             :         char teststr[1024], teststr2[1024];
      70             :         DBINT testint, binvaluelength;
      71             :         DBVARYBIN  testvbin, testvbin2;
      72             :         DBVARYCHAR testvstr;
      73             :         BYTE testbin[10];
      74          10 :         int failed = 0;
      75             :         int expected_error;
      76             : 
      77          10 :         set_malloc_options();
      78             : 
      79          10 :         read_login_info(argc, argv);
      80             : 
      81          10 :         printf("Starting %s\n", argv[0]);
      82             : 
      83          10 :         dbinit();
      84             : 
      85          10 :         dberrhandle(syb_err_handler);
      86          10 :         dbmsghandle(syb_msg_handler);
      87             : 
      88          10 :         printf("About to logon\n");
      89             : 
      90          10 :         login = dblogin();
      91          10 :         DBSETLPWD(login, PASSWORD);
      92          10 :         DBSETLUSER(login, USER);
      93          10 :         DBSETLAPP(login, "t0007");
      94             : 
      95          10 :         printf("About to open\n");
      96             : 
      97          10 :         dbproc = dbopen(login, SERVER);
      98          10 :         if (strlen(DATABASE))
      99          10 :                 dbuse(dbproc, DATABASE);
     100          10 :         dbloginfree(login);
     101             : 
     102          10 :         create_tables(dbproc, 10);
     103             : 
     104          10 :         if (!start_query(dbproc)) {
     105           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     106           0 :                 failed = 1;
     107             :         }
     108             : 
     109          10 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
     110          10 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
     111             : 
     112          30 :         for (i = 1; i <= 2; i++) {
     113             :                 char expected[1024];
     114             : 
     115          20 :                 sprintf(expected, "row %07d", i);
     116             : 
     117          20 :                 if (i % 5 == 0) {
     118           0 :                         dbclrbuf(dbproc, 5);
     119             :                 }
     120             : 
     121          20 :                 testint = -1;
     122          20 :                 strcpy(teststr, "bogus");
     123             : 
     124          20 :                 if (REG_ROW != dbnextrow(dbproc)) {
     125           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     126           0 :                         abort();
     127             :                 }
     128          20 :                 if (testint != i) {
     129           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     130           0 :                         abort();
     131             :                 }
     132          20 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
     133           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
     134           0 :                         abort();
     135             :                 }
     136          20 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     137             :         }
     138             : 
     139             : 
     140          10 :         printf("second select.  Should fail.\n");
     141             : 
     142          10 :         expected_error = 20019;
     143          10 :         dbsetuserdata(dbproc, (BYTE*) &expected_error);
     144             : 
     145          10 :         if (start_query(dbproc)) {
     146           0 :                 fprintf(stderr, "%s:%d: start_query should have failed but didn't\n", __FILE__, __LINE__);
     147           0 :                 failed = 1;
     148             :         }
     149             : 
     150          10 :         dbcancel(dbproc);
     151             :         
     152             :         /* 
     153             :          * Test Binary binding
     154             :          */
     155          10 :         if (!start_query(dbproc)) {
     156           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     157           0 :                 failed = 1;
     158             :         }
     159             : 
     160          10 :         dbbind(dbproc, 1, VARYBINBIND, sizeof(testvbin), (BYTE *) &testvbin);
     161          10 :         dbbind(dbproc, 2, VARYCHARBIND, sizeof(testvstr), (BYTE *) &testvstr);
     162          10 :         dbbind(dbproc, 3, BINARYBIND, sizeof(testint), (BYTE *) &testint);
     163             : 
     164          30 :         for (i = 1; i <= 2; i++) {
     165             :                 char expected[1024];
     166             : 
     167          20 :                 sprintf(expected, "row %07d ", i);
     168             : 
     169          20 :                 testint = -1;
     170          20 :                 memset(&testvbin, '*', sizeof(testvbin));
     171          20 :                 memset(&testvstr, '*', sizeof(testvstr));
     172             : 
     173          20 :                 if (REG_ROW != dbnextrow(dbproc)) {
     174           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     175           0 :                         abort();
     176             :                 }
     177          20 :                 if (testint != i) {
     178           0 :                         fprintf(stderr, "Failed, line %d.  Expected i to be %d, was %d (0x%x)\n", __LINE__, i, (int) testint, (int) testint);
     179           0 :                         abort();
     180             :                 }
     181          20 :                 if (testvbin.len != sizeof(testint)) {
     182           0 :                         fprintf(stderr, "Failed, line %d.  Expected bin length to be %d, was %d\n", __LINE__, (int) sizeof(testint), (int) testvbin.len);
     183           0 :                         abort();
     184             :                 }
     185          20 :                 memcpy(&testint, testvbin.array, sizeof(testint));
     186          20 :                 if (testint != i) {
     187           0 :                         fprintf(stderr, "Failed, line %d.  Expected i to be %d, was %d (0x%x)\n", __LINE__, i, (int) testint, (int) testint);
     188           0 :                         abort();
     189             :                 }
     190          20 :                 if (testvstr.len != strlen(expected) || 0 != strncmp(testvstr.str, expected, strlen(expected))) {
     191           0 :                         printf("Failed, line %d.  Expected s to be |%s|, was |%s|\n", __LINE__, expected, testvstr.str);
     192           0 :                         abort();
     193             :                 }
     194          20 :                 testvstr.str[testvstr.len] = 0;
     195          20 :                 printf("Read a row of data -> %d %s\n", (int) testint, testvstr.str);
     196             :         }
     197             : 
     198          10 :         dbcancel(dbproc);
     199             : 
     200             :         /*
     201             :          * Test var binary bindings of binary values
     202             :          */
     203          10 :         if (!start_query(dbproc)) {
     204           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     205           0 :                 failed = 1;
     206             :         }
     207             : 
     208          10 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) &binvaluelength);  /* returned binary string length (all columns) */
     209          10 :         dbbind(dbproc, 2, VARYBINBIND, sizeof(testvbin), (BYTE *) &testvbin);  /* returned as varbinary, bound varbinary */
     210          10 :         dbbind(dbproc, 3, VARYBINBIND, sizeof(testvbin2), (BYTE *) &testvbin2); /* returned as binary, bound varbinary */
     211          10 :         dbbind(dbproc, 4, BINARYBIND, sizeof(testbin), (BYTE *) &testbin);  /* returned as varbinary, bound binary */
     212             : 
     213          10 :         memset(&testvbin, '*', sizeof(testvbin));
     214          10 :         memset(&testvbin2, '*', sizeof(testvbin2));
     215          10 :         memset(&testbin, '@', sizeof(testbin));  /* different. After fetch all buffers should have same value */
     216             : 
     217          10 :         if (REG_ROW != dbnextrow(dbproc)) {
     218           0 :                 fprintf(stderr, "Failed.  Expected a row\n");
     219           0 :                 abort();
     220             :         }
     221             : 
     222          10 :         if (testvbin.len != binvaluelength) {
     223           0 :                 fprintf(stderr, "Failed, line %d.  Expected bin length to be %d, was %d\n", __LINE__,
     224             :                         (int) binvaluelength, (int) testvbin.len);
     225           0 :                 abort();
     226             :         }
     227             : 
     228          10 :         if (testvbin2.len != binvaluelength) {
     229           0 :                 fprintf(stderr, "Failed, line %d.  Expected bin length to be %d, was %d\n", __LINE__,
     230             :                         (int) binvaluelength, (int) testvbin.len);
     231           0 :                 abort();
     232             :         }
     233             : 
     234          10 :         if (memcmp(testvbin.array, testbin, binvaluelength) != 0) {
     235           0 :                 fprintf(stderr, "Failed, line %d.  Expected buffer to be %s, was %s\n", __LINE__, 
     236             :                         hex_buffer(testbin, binvaluelength, teststr),
     237             :                         hex_buffer(testvbin.array, binvaluelength, teststr2));
     238           0 :                 abort();
     239             :         }
     240             : 
     241          10 :         if (memcmp(testvbin2.array, testbin, binvaluelength) != 0) {
     242           0 :                 fprintf(stderr, "Failed, line %d.  Expected buffer to be %s, was %s\n", __LINE__,
     243             :                         hex_buffer(testbin, binvaluelength, teststr),
     244             :                         hex_buffer(testvbin2.array, binvaluelength, teststr2));
     245           0 :                 abort();
     246             :         }
     247             : 
     248          10 :         memset(teststr2, 0, sizeof(teststr2));  /* finally, test binary padding is all zeroes */
     249          10 :         if (memcmp(testbin + binvaluelength, teststr2, sizeof(testbin) - binvaluelength) != 0) {
     250           0 :                 fprintf(stderr, "Failed, line %d.  Expected binary padding to be zeroes, was %s\n", __LINE__,
     251           0 :                         hex_buffer(testbin + binvaluelength, sizeof(testbin) - binvaluelength, teststr));
     252           0 :                 abort();
     253             :         }
     254             : 
     255          10 :         dbexit();
     256             : 
     257          10 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     258          10 :         return failed ? 1 : 0;
     259             : }

Generated by: LCOV version 1.13