LCOV - code coverage report
Current view: top level - src/ctlib/unittests - datafmt.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 50 98 51.0 %
Date: 2025-01-18 12:13:41 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13