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: 2024-04-18 20:40:06 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.13