LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0013.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 125 163 76.7 %
Date: 2024-03-23 09:12:27 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.13