LCOV - code coverage report
Current view: top level - src/ctlib/unittests - datafmt.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 43 75 57.3 %
Date: 2025-04-23 23:01:24 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Testing: data truncation behavior of ct_fetch */
       4          10 : TEST_MAIN()
       5             : {
       6             :         CS_CONTEXT *ctx;
       7             :         CS_CONNECTION *conn;
       8             :         CS_COMMAND *cmd;
       9          10 :         int verbose = 0;
      10             : 
      11             :         CS_RETCODE ret;
      12             :         CS_RETCODE results_ret;
      13             :         CS_INT result_type;
      14             :         CS_INT num_cols;
      15             : 
      16             :         CS_DATAFMT datafmt;
      17          10 :         CS_INT copied = 0;
      18          10 :         CS_SMALLINT ind = 0;
      19          10 :         CS_INT count, row_count = 0;
      20             : 
      21             :         CS_CHAR select[1024];
      22             : 
      23          10 :         char *addr = NULL;
      24             : 
      25          10 :         printf("%s: test data truncation behavior of ct_fetch \n", __FILE__);
      26             :         if (verbose) {
      27             :                 printf("Trying login\n");
      28             :         }
      29          10 :         check_call(try_ctlogin_with_options, (argc, argv, &ctx, &conn, &cmd, verbose));
      30          10 :         verbose += common_pwd.fverbose;
      31             : 
      32          10 :         strcpy(select, "select name from systypes where datalength(name) > 2*9 order by datalength(name)");
      33          10 :         printf("%s\n", select);
      34             : 
      35          10 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      36             : 
      37          10 :         check_call(ct_send, (cmd));
      38             : 
      39          40 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      40          20 :                 switch ((int) result_type) {
      41             :                 case CS_CMD_SUCCEED:
      42             :                         break;
      43             :                 case CS_CMD_DONE:
      44             :                         break;
      45           0 :                 case CS_CMD_FAIL:
      46           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      47           0 :                         free(addr);
      48           0 :                         return 1;
      49          10 :                 case CS_ROW_RESULT:
      50             : 
      51          10 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      52          10 :                         fprintf(stderr, "%d column%s\n", num_cols, num_cols==1? "":"s");
      53             : 
      54          10 :                         check_call(ct_describe, (cmd, 1, &datafmt));
      55             : 
      56          10 :                         fprintf(stderr, "type = %d\n", datafmt.datatype);
      57          10 :                         fprintf(stderr, "maxlength = %d\n", datafmt.maxlength);
      58          10 :                         if (datafmt.datatype == CS_CHAR_TYPE) {
      59          10 :                                 fprintf(stderr, "CS_CHAR_TYPE\n");
      60          10 :                                 datafmt.format = CS_FMT_NULLTERM;
      61          10 :                                 addr = (char *) malloc(datafmt.maxlength);
      62             :                         } else {
      63           0 :                                 fputs("unexpected data type\n", stderr);
      64           0 :                                 return 1;
      65             :                         }
      66             : 
      67          10 :                         fprintf(stderr, "binding column 1 (%s)\n", datafmt.name);
      68             : 
      69             :                         /* set maxlength to something short to test truncation behavior */
      70          10 :                         if (common_pwd.maxlength)
      71           0 :                                 datafmt.maxlength = common_pwd.maxlength;
      72             : 
      73          10 :                         check_call(ct_bind, (cmd, 1, &datafmt, addr, &copied, &ind));
      74             : 
      75          10 :                         fprintf(stderr, "fetching rows with datafmt.maxlength = %d\n", datafmt.maxlength);
      76             : 
      77          10 :                         while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) != CS_END_DATA) {
      78             : 
      79          40 :                                 fprintf(stderr, "ct_fetch() row %d returned %d.\n", row_count, (int) ret);
      80          40 :                                 addr[copied] = '\0';
      81          40 :                                 fprintf(stderr, "copied %d bytes: [%s]\n", copied, addr);
      82          40 :                                 row_count += count;
      83             : 
      84          40 :                                 switch (ret) {
      85          40 :                                 case CS_SUCCEED:
      86          40 :                                         printf("ct_fetch returned %d row%s\n", count, count==1? "":"s");
      87          40 :                                         break;
      88           0 :                                 case CS_ROW_FAIL:
      89           0 :                                         fprintf(stderr, "error: ct_fetch() returned CS_ROW_FAIL on row %d.\n", row_count);
      90           0 :                                         free(addr);
      91           0 :                                         return 1;
      92           0 :                                 case CS_CANCELED:
      93           0 :                                         fprintf(stderr, "error: ct_fetch() returned CS_CANCELED??\n");
      94           0 :                                         free(addr);
      95           0 :                                         return 1;
      96           0 :                                 case CS_FAIL:
      97           0 :                                         fprintf(stderr, "error: ct_fetch() returned CS_FAIL.\n");
      98           0 :                                         free(addr);
      99           0 :                                         return 1;
     100           0 :                                 default:
     101           0 :                                         fprintf(stderr, "error: ct_fetch() unexpected return.\n");
     102           0 :                                         free(addr);
     103           0 :                                         return 1;
     104             :                                 }
     105             :                         }
     106             :                         
     107             :                         break;
     108             :                 }
     109             :         }
     110             :         
     111          10 :         switch ((int) results_ret) {
     112             :         case CS_END_RESULTS:
     113             :                 break;
     114           0 :         case CS_FAIL:
     115           0 :                 fprintf(stderr, "ct_results() failed.\n");
     116           0 :                 free(addr);
     117           0 :                 return 1;
     118             :                 break;
     119           0 :         default:
     120           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     121           0 :                 free(addr);
     122           0 :                 return 1;
     123             :         }
     124             : 
     125          10 :         if (verbose) {
     126           0 :                 printf("Trying logout\n");
     127             :         }
     128          10 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     129             : 
     130          10 :         free(addr);
     131          10 :         return 0;
     132             : }

Generated by: LCOV version 1.13