LCOV - code coverage report
Current view: top level - src/tds/unittests - utf8_3.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 39 60 65.0 %
Date: 2026-11-25 04:57:11 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005  Brian Bruns
       3             :  * Copyright (C) 2010 Frediano Ziglio
       4             :  *
       5             :  * This library is free software; you can redistribute it and/or
       6             :  * modify it under the terms of the GNU Library General Public
       7             :  * License as published by the Free Software Foundation; either
       8             :  * version 2 of the License, or (at your option) any later version.
       9             :  *
      10             :  * This library is distributed in the hope that it will be useful,
      11             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13             :  * Library General Public License for more details.
      14             :  *
      15             :  * You should have received a copy of the GNU Library General Public
      16             :  * License along with this library; if not, write to the
      17             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      18             :  * Boston, MA 02111-1307, USA.
      19             :  */
      20             : #include "common.h"
      21             : 
      22             : #include <freetds/utils/string.h>
      23             : #include <freetds/replacements.h>
      24             : #include <ctype.h>
      25             : #include <assert.h>
      26             : 
      27             : static TDSSOCKET *tds;
      28             : 
      29             : static void
      30         328 : test(const char *buf)
      31             : {
      32             :         char query[1024];
      33             :         char tmp[129 * 3];
      34             :         int rc;
      35             :         TDS_INT result_type;
      36             :         int done_flags;
      37         328 :         const unsigned row_token_flags = TDS_STOPAT_ROWFMT | TDS_STOPAT_DONE | TDS_RETURN_ROW | TDS_RETURN_COMPUTE;
      38             : 
      39         328 :         to_utf8(buf, tmp);
      40         328 :         sprintf(query, "SELECT 1 AS [%s]", tmp);
      41             : 
      42             :         /* do a select and check all results */
      43         328 :         rc = tds_submit_query(tds, query);
      44         328 :         if (rc != TDS_SUCCESS) {
      45           0 :                 fprintf(stderr, "tds_submit_query() failed\n");
      46           0 :                 exit(1);
      47             :         }
      48             : 
      49         328 :         if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) {
      50           0 :                 fprintf(stderr, "tds_process_tokens() failed\n");
      51           0 :                 exit(1);
      52             :         }
      53             : 
      54         328 :         if (result_type != TDS_ROWFMT_RESULT) {
      55           0 :                 fprintf(stderr, "expected row fmt() failed\n");
      56           0 :                 exit(1);
      57             :         }
      58             : 
      59         328 :         if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) {
      60           0 :                 fprintf(stderr, "tds_process_tokens() failed\n");
      61           0 :                 exit(1);
      62             :         }
      63             : 
      64         328 :         if (result_type != TDS_ROW_RESULT) {
      65           0 :                 fprintf(stderr, "expected row result() failed\n");
      66           0 :                 exit(1);
      67             :         }
      68             : 
      69         656 :         while ((rc = tds_process_tokens(tds, &result_type, NULL, row_token_flags)) == TDS_SUCCESS) {
      70             :                 TDSCOLUMN *curcol;
      71             : 
      72         656 :                 if (result_type != TDS_ROW_RESULT)
      73             :                         break;
      74             : 
      75         328 :                 curcol = tds->current_results->columns[0];
      76             : 
      77         656 :                 if (strcmp(tmp, tds_dstr_cstr(&curcol->column_name)) != 0) {
      78           0 :                         strlcpy(query, tds_dstr_cstr(&curcol->column_name), sizeof(query));
      79           0 :                         fprintf(stderr, "Wrong result Got: '%s' len %u\n        Expected: '%s' len %u\n", query,
      80           0 :                                 (unsigned) tds_dstr_len(&curcol->column_name), tmp, (unsigned int) strlen(tmp));
      81           0 :                         exit(1);
      82             :                 }
      83             :         }
      84             : 
      85         328 :         if (rc != TDS_SUCCESS || result_type == TDS_ROW_RESULT || result_type == TDS_COMPUTE_RESULT) {
      86           0 :                 fprintf(stderr, "tds_process_tokens() unexpected return\n");
      87           0 :                 exit(1);
      88             :         }
      89             : 
      90         656 :         while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
      91         328 :                 switch (result_type) {
      92             :                 case TDS_NO_MORE_RESULTS:
      93           0 :                         return;
      94             : 
      95         328 :                 case TDS_DONE_RESULT:
      96             :                 case TDS_DONEPROC_RESULT:
      97             :                 case TDS_DONEINPROC_RESULT:
      98         328 :                         if (!(done_flags & TDS_DONE_ERROR))
      99             :                                 break;
     100             : 
     101             :                 default:
     102           0 :                         fprintf(stderr, "tds_proces_tokens() unexpected result_type\n");
     103           0 :                         exit(1);
     104             :                         break;
     105             :                 }
     106         328 :         }
     107             : }
     108             : 
     109          10 : TEST_MAIN()
     110             : {
     111             :         TDSLOGIN *login;
     112             :         int ret;
     113          10 :         int verbose = 0;
     114             : 
     115             :         /* use UTF-8 as our coding */
     116          10 :         strcpy(common_pwd.charset, "UTF-8");
     117             : 
     118          10 :         ret = try_tds_login(&login, &tds, __FILE__, verbose);
     119          10 :         if (ret != TDS_SUCCESS) {
     120           0 :                 fprintf(stderr, "try_tds_login() failed\n");
     121           0 :                 return 1;
     122             :         }
     123             : 
     124          10 :         if (IS_TDS7_PLUS(tds->conn)) {
     125             :                 char buf[129 * 8];
     126             :                 int i;
     127             : 
     128             :                 /* build a string of length 128 */
     129           8 :                 strcpy(buf, "");
     130        1032 :                 for (i = 1; i <= 128; ++i) {
     131        1024 :                         sprintf(strchr(buf, 0), "&#x%04x;", 0x4000 + i);
     132             :                 }
     133             : 
     134             :                 /* do all test */
     135             :                 for (i = 1;;) {
     136         328 :                         printf("Testing len %d\n", i);
     137         328 :                         test(buf + 8 * (128 - i));
     138         328 :                         if (i == 128)
     139             :                                 break;
     140         320 :                         ++i;
     141         320 :                         if (i > 12)
     142         232 :                                 i += 3;
     143         320 :                         if (i >= 128)
     144           8 :                                 i = 128;
     145             :                 }
     146             :         }
     147             : 
     148          10 :         try_tds_logout(login, tds, verbose);
     149          10 :         return 0;
     150             : }

Generated by: LCOV version 1.13