LCOV - code coverage report
Current view: top level - src/ctlib/unittests - all_types.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 39 45 86.7 %
Date: 2025-07-02 09:40:27 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* try all types from server */
       2             : 
       3             : #include "common.h"
       4             : 
       5             : #include <ctlib.h>
       6             : 
       7             : #define TDS_DONT_DEFINE_DEFAULT_FUNCTIONS
       8             : #include "../../tds/unittests/common.h"
       9             : #include <freetds/tds.h>
      10             : 
      11             : static CS_CONTEXT *ctx = NULL;
      12             : 
      13        1210 : static void test_type(TDSSOCKET *tds TDS_UNUSED, TDSCOLUMN *col)
      14             : {
      15             :         TDSRESULTINFO *resinfo, *bindinfo;
      16             :         TDSCOLUMN *oldcol, *bindcol;
      17             :         char out_buf[256];
      18             :         CS_INT len;
      19             : 
      20             :         /* we should be able to support a type coming from server */
      21        1210 :         if (_ct_get_client_type(col, false) == CS_ILLEGAL_TYPE) {
      22           0 :                 fprintf(stderr, "not supported\n");
      23           0 :                 assert(0);
      24             :         }
      25             : 
      26             :         /* try to convert anyway, this should succeed */
      27        1210 :         resinfo = tds_alloc_results(1);
      28        1210 :         assert(resinfo);
      29        1210 :         bindinfo = tds_alloc_results(1);
      30        1210 :         assert(bindinfo);
      31             : 
      32             :         /* hack to pass our column to _ct_bind_data */
      33        1210 :         oldcol = resinfo->columns[0];
      34        1210 :         resinfo->columns[0] = col;
      35             : 
      36        1210 :         memset(out_buf, '-', sizeof(out_buf));
      37        1210 :         bindcol = bindinfo->columns[0];
      38        1210 :         bindcol->column_varaddr = out_buf;
      39        1210 :         bindcol->column_bindlen = sizeof(out_buf);
      40        1210 :         bindcol->column_bindtype = CS_CHAR_TYPE;
      41        1210 :         bindcol->column_bindfmt = CS_FMT_NULLTERM;
      42        1210 :         len = -1;
      43        1210 :         bindcol->column_lenbind = &len;
      44             : 
      45             :         /* every column should be at least be convertible to something */
      46        1210 :         if (_ct_bind_data(ctx, resinfo, bindinfo, 0)) {
      47           0 :                 fprintf(stderr, "conversion failed\n");
      48           0 :                 assert(0);
      49             :         }
      50             : 
      51             :         /* just safety, we use small data for now */
      52        1210 :         assert(len < sizeof(out_buf));
      53             : 
      54             :         /* we said terminated, check for terminator */
      55        1210 :         assert(len >= 1 && len < sizeof(out_buf));
      56        1210 :         assert(out_buf[len - 1] == 0);
      57        1210 :         printf("output (%d): %s\n", len, out_buf);
      58             : 
      59        1210 :         resinfo->columns[0] = oldcol;
      60        1210 :         tds_free_results(resinfo);
      61        1210 :         tds_free_results(bindinfo);
      62        1210 : }
      63             : 
      64          10 : TEST_MAIN()
      65             : {
      66             :         TDSCONTEXT *tds_ctx;
      67             :         TDSSOCKET *tds;
      68             : 
      69          10 :         tdsdump_open(tds_dir_getenv(TDS_DIR("TDSDUMP")));
      70             : 
      71          10 :         check_call(cs_ctx_alloc, (CS_VERSION_100, &ctx));
      72             : 
      73          10 :         tds_ctx = tds_alloc_context(NULL);
      74          10 :         assert(tds_ctx);
      75          10 :         tds = tds_alloc_socket(tds_ctx, 512);
      76          10 :         assert(tds);
      77          10 :         tds->conn->use_iconv = 0;
      78          10 :         if (TDS_FAILED(tds_iconv_open(tds->conn, "UTF-8", 1))) {
      79           0 :                 fprintf(stderr, "Failed to initialize iconv\n");
      80           0 :                 return 1;
      81             :         }
      82             : 
      83          10 :         tds_all_types(tds, test_type);
      84             : 
      85          10 :         tds_free_socket(tds);
      86          10 :         tds_free_context(tds_ctx);
      87             : 
      88          10 :         cs_ctx_drop(ctx);
      89             : 
      90          10 :         return 0;
      91             : }

Generated by: LCOV version 1.13