LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0014.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 120 145 82.8 %
Date: 2024-03-23 09:12:27 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test sending and receiving TEXT datatype
       3             :  * Functions: dbbind dbmoretext dbreadtext dbtxptr dbtxtimestamp dbwritetext 
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : #define BLOB_BLOCK_SIZE 4096
       9             : 
      10             : char *testargs[] = { "", FREETDS_SRCDIR "/data.bin", "t0014.out" };
      11             : 
      12             : static int
      13          14 : test(int argc, char **argv, int over4k)
      14             : {
      15          14 :         const int rows_to_add = 3;
      16             :         LOGINREC *login;
      17             :         DBPROCESS *dbproc;
      18             :         DBPROCESS *blobproc;
      19             :         int i;
      20             :         DBINT testint;
      21             :         FILE *fp;
      22             :         long result, isiz;
      23             :         char *blob, *rblob;
      24             :         unsigned char *textPtr, *timeStamp;
      25             :         char objname[256];
      26             :         char sqlCmd[256];
      27             :         char rbuf[BLOB_BLOCK_SIZE];
      28             :         long numread;
      29             :         int numtowrite, numwritten;
      30             : 
      31          14 :         set_malloc_options();
      32             : 
      33          14 :         read_login_info(argc, argv);
      34          14 :         printf("Starting %s\n", argv[0]);
      35          14 :         dbinit();
      36             : 
      37          14 :         dberrhandle(syb_err_handler);
      38          14 :         dbmsghandle(syb_msg_handler);
      39             : 
      40          14 :         printf("About to logon\n");
      41             : 
      42          14 :         login = dblogin();
      43          14 :         DBSETLPWD(login, PASSWORD);
      44          14 :         DBSETLUSER(login, USER);
      45          14 :         DBSETLAPP(login, "t0014");
      46             : 
      47          14 :         printf("About to open %s..%s for user '%s'\n", SERVER, DATABASE, USER);
      48             : 
      49          14 :         dbproc = dbopen(login, SERVER);
      50          14 :         blobproc = dbopen(login, SERVER);
      51          14 :         if (strlen(DATABASE)) {
      52          14 :                 dbuse(dbproc, DATABASE);
      53          14 :                 dbuse(blobproc, DATABASE);
      54             :         }
      55          14 :         dbloginfree(login);
      56          14 :         printf("After logon\n");
      57             : 
      58          14 :         printf("About to read binary input file\n");
      59             : 
      60          14 :         if (argc == 1) {
      61             :                 argv = testargs;
      62             :                 argc = 3;
      63             :         }
      64           0 :         if (argc < 3) {
      65           0 :                 fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
      66           0 :                 return 1;
      67             :         }
      68             : 
      69          14 :         if ((fp = fopen(argv[1], "rb")) == NULL) {
      70           0 :                 fprintf(stderr, "Cannot open input file: %s\n", argv[1]);
      71           0 :                 return 2;
      72             :         }
      73          14 :         result = fseek(fp, 0, SEEK_END);
      74          14 :         isiz = ftell(fp);
      75          14 :         result = fseek(fp, 0, SEEK_SET);
      76             : 
      77          14 :         blob = (char *) malloc(isiz);
      78          14 :         result = fread((void *) blob, isiz, 1, fp);
      79          14 :         assert(result == 1);
      80          14 :         fclose(fp);
      81             : 
      82             :         /* FIXME this test seem to not work using temporary tables (sybase?)... */
      83          14 :         printf("Dropping table\n");
      84          14 :         sql_cmd(dbproc);
      85          14 :         dbsqlexec(dbproc);
      86          14 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      87             :                 /* nop */
      88             :         }
      89             : 
      90          14 :         printf("creating table\n");
      91          14 :         sql_cmd(dbproc);
      92          14 :         dbsqlexec(dbproc);
      93          14 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      94             :                 /* nop */
      95             :         }
      96             : 
      97             : 
      98          14 :         printf("insert\n");
      99          56 :         for (i = 0; i < rows_to_add; i++) {
     100          42 :                 sql_cmd(dbproc);
     101          42 :                 dbsqlexec(dbproc);
     102          42 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
     103             :                         /* nop */
     104             :                 }
     105             :         }
     106             : 
     107          36 :         for (i = 0; i < rows_to_add; i++) {
     108          38 :                 sql_cmd(dbproc);
     109          38 :                 dbsqlexec(dbproc);
     110          38 :                 if (dbresults(dbproc) != SUCCEED) {
     111           0 :                         fprintf(stderr, "Error inserting blob\n");
     112           0 :                         return 4;
     113             :                 }
     114             : 
     115          74 :                 while ((result = dbnextrow(dbproc)) != NO_MORE_ROWS) {
     116          38 :                         result = REG_ROW;
     117          38 :                         result = DBTXPLEN;
     118          38 :                         strcpy(objname, "dblib0014.PigTure");
     119          38 :                         textPtr = dbtxptr(dbproc, 1);
     120          38 :                         timeStamp = dbtxtimestamp(dbproc, 1);
     121             : 
     122          38 :                         if (!textPtr && !timeStamp && dbtds(dbproc) >= DBTDS_7_2) {
     123           2 :                                 printf("Protocol 7.2+ detected, test not supported\n");
     124           2 :                                 free(blob);
     125           2 :                                 dbexit();
     126           2 :                                 exit(0);
     127             :                         }
     128             : 
     129             :                         /*
     130             :                          * Use #ifdef if you want to test dbmoretext mode (needed for 16-bit apps)
     131             :                          * Use #ifndef for big buffer version (32-bit)
     132             :                          */
     133          36 :                         if (over4k) {
     134          18 :                                 if (dbwritetext(blobproc, objname, textPtr, DBTXPLEN, timeStamp, TRUE, isiz, (BYTE*) blob) != SUCCEED)
     135             :                                         return 5;
     136             :                         } else {
     137          18 :                                 if (dbwritetext(blobproc, objname, textPtr, DBTXPLEN, timeStamp, TRUE, isiz, NULL) != SUCCEED)
     138             :                                         return 15;
     139          18 :                                 dbsqlok(blobproc);
     140          18 :                                 dbresults(blobproc);
     141             : 
     142          18 :                                 numtowrite = 0;
     143             :                                 /* Send the update value in chunks. */
     144         288 :                                 for (numwritten = 0; numwritten < isiz; numwritten += numtowrite) {
     145         270 :                                         numtowrite = (isiz - numwritten);
     146         270 :                                         if (numtowrite > BLOB_BLOCK_SIZE)
     147         252 :                                                 numtowrite = BLOB_BLOCK_SIZE;
     148         270 :                                         dbmoretext(blobproc, (DBINT) numtowrite, (BYTE *) (blob + numwritten));
     149             :                                 }
     150          18 :                                 dbsqlok(blobproc);
     151          54 :                                 while (dbresults(blobproc) != NO_MORE_RESULTS)
     152          18 :                                         continue;
     153             :                         }
     154             :                 }
     155             :         }
     156             : 
     157          12 :         printf("select\n");
     158             : 
     159          12 :         sql_cmd(dbproc);
     160          12 :         dbsqlexec(dbproc);
     161             : 
     162          12 :         if (dbresults(dbproc) != SUCCEED) {
     163           0 :                 printf("Was expecting a result set.");
     164           0 :                 exit(1);
     165             :         }
     166             : 
     167          24 :         for (i = 1; i <= dbnumcols(dbproc); i++) {
     168          24 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
     169             :         }
     170             : 
     171          12 :         if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
     172           0 :                 fprintf(stderr, "Had problem with bind\n");
     173           0 :                 abort();
     174             :         }
     175             : 
     176          84 :         for (i = 0; i < rows_to_add; i++) {
     177             :         char expected[1024];
     178             : 
     179          36 :                 sprintf(expected, "row %03d", i);
     180             : 
     181          36 :                 if (REG_ROW != dbnextrow(dbproc)) {
     182           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     183           0 :                         exit(1);
     184             :                 }
     185          36 :                 if (testint != i) {
     186           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     187           0 :                         abort();
     188             :                 }
     189             : 
     190             :                 /* get the image */
     191          36 :                 strcpy(sqlCmd, "SET TEXTSIZE 2147483647");
     192          36 :                 dbcmd(blobproc, sqlCmd);
     193          36 :                 dbsqlexec(blobproc);
     194             : 
     195          36 :                 if (dbresults(blobproc) != SUCCEED) {
     196           0 :                         dbcancel(blobproc);
     197           0 :                         return 16;
     198             :                 }
     199             : 
     200          36 :                 sprintf(sqlCmd, "SELECT PigTure FROM dblib0014 WHERE i = %d", i);
     201          36 :                 dbcmd(blobproc, sqlCmd);
     202          36 :                 dbsqlexec(blobproc);
     203          36 :                 if (dbresults(blobproc) != SUCCEED) {
     204           0 :                         fprintf(stderr, "Error extracting blob\n");
     205           0 :                         return 6;
     206             :                 }
     207             : 
     208             :                 numread = 0;
     209             :                 rblob = NULL;
     210         612 :                 while ((result = dbreadtext(blobproc, rbuf, BLOB_BLOCK_SIZE)) != NO_MORE_ROWS) {
     211         576 :                         if (result != 0) {      /* this indicates not end of row */
     212         540 :                                 rblob = (char*) realloc(rblob, result + numread);
     213         540 :                                 memcpy((void *) (rblob + numread), (void *) rbuf, result);
     214         540 :                                 numread += result;
     215             :                         }
     216             :                 }
     217             : 
     218          36 :                 if (i == 0) {
     219          12 :                         printf("Saving first blob data row to file: %s\n", argv[2]);
     220          12 :                         if ((fp = fopen(argv[2], "wb")) == NULL) {
     221           0 :                                 free(blob);
     222           0 :                                 free(rblob);
     223           0 :                                 fprintf(stderr, "Unable to open output file: %s\n", argv[2]);
     224           0 :                                 return 3;
     225             :                         }
     226             : 
     227          12 :                         result = fwrite((void *) rblob, numread, 1, fp);
     228          12 :                         fclose(fp);
     229             :                 }
     230             : 
     231          36 :                 printf("Read blob data row %d --> %s %ld byte comparison\n",
     232          36 :                        (int) testint, (memcmp(blob, rblob, numread)) ? "failed" : "PASSED", numread);
     233          36 :                 free(rblob);
     234             :         }
     235             : 
     236          12 :         if (dbnextrow(dbproc) != NO_MORE_ROWS) {
     237           0 :                 fprintf(stderr, "Was expecting no more rows\n");
     238           0 :                 exit(1);
     239             :         }
     240             : 
     241          12 :         free(blob);
     242             : 
     243          12 :         printf("Dropping table\n");
     244          12 :         sql_cmd(dbproc);
     245          12 :         dbsqlexec(dbproc);
     246          12 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     247             :                 /* nop */
     248             :         }
     249             : 
     250          12 :         dbexit();
     251             : 
     252          12 :         return 0;
     253             : }
     254             : 
     255             : int
     256           8 : main(int argc, char **argv)
     257             : {
     258             :         int res;
     259             : 
     260           8 :         res = test(argc, argv, 0);
     261           6 :         if (!res)
     262           6 :                 res = test(argc, argv, 1);
     263           6 :         if (res)
     264             :                 return res;
     265             : 
     266           6 :         printf("dblib okay on %s\n", __FILE__);
     267           6 :         return 0;
     268             : }
     269             : 

Generated by: LCOV version 1.13