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: 2024-03-23 08:24:27 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           8 : create_tables(DBPROCESS * dbproc, int rows_to_add)
      10             : {
      11             :         int i;
      12             : 
      13           8 :         printf("creating table\n");
      14           8 :         sql_cmd(dbproc);
      15           8 :         dbsqlexec(dbproc);
      16           8 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      17             :                 /* nop */
      18             :         }
      19             : 
      20           8 :         printf("insert\n");
      21          80 :         for (i = 1; i < rows_to_add; i++) {
      22          72 :                 sql_cmd(dbproc);
      23          72 :                 dbsqlexec(dbproc);
      24          72 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
      25             :                         /* nop */
      26             :                 }
      27             :         }
      28           8 : }
      29             : 
      30             : 
      31             : static int
      32          32 : start_query(DBPROCESS * dbproc)
      33             : {
      34             :         int i;
      35             : 
      36          32 :         if (SUCCEED != sql_cmd(dbproc)) {
      37             :                 return 0;
      38             :         }
      39          32 :         if (SUCCEED != dbsqlexec(dbproc)) {
      40             :                 return 0;
      41             :         }
      42             : 
      43          24 :         if (dbresults(dbproc) != SUCCEED)
      44             :                 return 0;
      45             : 
      46          72 :         for (i = 1; i <= dbnumcols(dbproc); i++)
      47          72 :                 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           8 : 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           8 :         int failed = 0;
      75             :         int expected_error;
      76             : 
      77           8 :         set_malloc_options();
      78             : 
      79           8 :         read_login_info(argc, argv);
      80             : 
      81           8 :         printf("Starting %s\n", argv[0]);
      82             : 
      83           8 :         dbinit();
      84             : 
      85           8 :         dberrhandle(syb_err_handler);
      86           8 :         dbmsghandle(syb_msg_handler);
      87             : 
      88           8 :         printf("About to logon\n");
      89             : 
      90           8 :         login = dblogin();
      91           8 :         DBSETLPWD(login, PASSWORD);
      92           8 :         DBSETLUSER(login, USER);
      93           8 :         DBSETLAPP(login, "t0007");
      94             : 
      95           8 :         printf("About to open\n");
      96             : 
      97           8 :         dbproc = dbopen(login, SERVER);
      98           8 :         if (strlen(DATABASE))
      99           8 :                 dbuse(dbproc, DATABASE);
     100           8 :         dbloginfree(login);
     101             : 
     102           8 :         create_tables(dbproc, 10);
     103             : 
     104           8 :         if (!start_query(dbproc)) {
     105           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     106           0 :                 failed = 1;
     107             :         }
     108             : 
     109           8 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
     110           8 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
     111             : 
     112          24 :         for (i = 1; i <= 2; i++) {
     113             :                 char expected[1024];
     114             : 
     115          16 :                 sprintf(expected, "row %07d", i);
     116             : 
     117          16 :                 if (i % 5 == 0) {
     118           0 :                         dbclrbuf(dbproc, 5);
     119             :                 }
     120             : 
     121          16 :                 testint = -1;
     122          16 :                 strcpy(teststr, "bogus");
     123             : 
     124          16 :                 if (REG_ROW != dbnextrow(dbproc)) {
     125           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     126           0 :                         abort();
     127             :                 }
     128          16 :                 if (testint != i) {
     129           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     130           0 :                         abort();
     131             :                 }
     132          16 :                 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          16 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     137             :         }
     138             : 
     139             : 
     140           8 :         printf("second select.  Should fail.\n");
     141             : 
     142           8 :         expected_error = 20019;
     143           8 :         dbsetuserdata(dbproc, (BYTE*) &expected_error);
     144             : 
     145           8 :         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           8 :         dbcancel(dbproc);
     151             :         
     152             :         /* 
     153             :          * Test Binary binding
     154             :          */
     155           8 :         if (!start_query(dbproc)) {
     156           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     157           0 :                 failed = 1;
     158             :         }
     159             : 
     160           8 :         dbbind(dbproc, 1, VARYBINBIND, sizeof(testvbin), (BYTE *) &testvbin);
     161           8 :         dbbind(dbproc, 2, VARYCHARBIND, sizeof(testvstr), (BYTE *) &testvstr);
     162           8 :         dbbind(dbproc, 3, BINARYBIND, sizeof(testint), (BYTE *) &testint);
     163             : 
     164          24 :         for (i = 1; i <= 2; i++) {
     165             :                 char expected[1024];
     166             : 
     167          16 :                 sprintf(expected, "row %07d ", i);
     168             : 
     169          16 :                 testint = -1;
     170          16 :                 memset(&testvbin, '*', sizeof(testvbin));
     171          16 :                 memset(&testvstr, '*', sizeof(testvstr));
     172             : 
     173          16 :                 if (REG_ROW != dbnextrow(dbproc)) {
     174           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     175           0 :                         abort();
     176             :                 }
     177          16 :                 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          16 :                 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          16 :                 memcpy(&testint, testvbin.array, sizeof(testint));
     186          16 :                 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          16 :                 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          16 :                 testvstr.str[testvstr.len] = 0;
     195          16 :                 printf("Read a row of data -> %d %s\n", (int) testint, testvstr.str);
     196             :         }
     197             : 
     198           8 :         dbcancel(dbproc);
     199             : 
     200             :         /*
     201             :          * Test var binary bindings of binary values
     202             :          */
     203           8 :         if (!start_query(dbproc)) {
     204           0 :                 fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
     205           0 :                 failed = 1;
     206             :         }
     207             : 
     208           8 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) &binvaluelength);  /* returned binary string length (all columns) */
     209           8 :         dbbind(dbproc, 2, VARYBINBIND, sizeof(testvbin), (BYTE *) &testvbin);  /* returned as varbinary, bound varbinary */
     210           8 :         dbbind(dbproc, 3, VARYBINBIND, sizeof(testvbin2), (BYTE *) &testvbin2); /* returned as binary, bound varbinary */
     211           8 :         dbbind(dbproc, 4, BINARYBIND, sizeof(testbin), (BYTE *) &testbin);  /* returned as varbinary, bound binary */
     212             : 
     213           8 :         memset(&testvbin, '*', sizeof(testvbin));
     214           8 :         memset(&testvbin2, '*', sizeof(testvbin2));
     215           8 :         memset(&testbin, '@', sizeof(testbin));  /* different. After fetch all buffers should have same value */
     216             : 
     217           8 :         if (REG_ROW != dbnextrow(dbproc)) {
     218           0 :                 fprintf(stderr, "Failed.  Expected a row\n");
     219           0 :                 abort();
     220             :         }
     221             : 
     222           8 :         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           8 :         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           8 :         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           8 :         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           8 :         memset(teststr2, 0, sizeof(teststr2));  /* finally, test binary padding is all zeroes */
     249           8 :         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           8 :         dbexit();
     256             : 
     257           8 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     258           8 :         return failed ? 1 : 0;
     259             : }

Generated by: LCOV version 1.13