LCOV - code coverage report
Current view: top level - src/tds/unittests - iconv_fread.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 38 51 74.5 %
Date: 2024-04-18 21:21:48 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2006   Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : #include "common.h"
      21             : #include <freetds/iconv.h>
      22             : 
      23             : #if HAVE_UNISTD_H
      24             : #undef getpid
      25             : #include <unistd.h>
      26             : #endif /* HAVE_UNISTD_H */
      27             : 
      28             : #if HAVE_STDLIB_H
      29             : #include <stdlib.h>
      30             : #endif /* HAVE_STDLIB_H */
      31             : 
      32             : #include <assert.h>
      33             : 
      34             : /* test tds_bcp_fread */
      35             : 
      36             : static char buf[4096+80];
      37             : 
      38             : int
      39           8 : main(int argc, char **argv)
      40             : {
      41             :         static const char out_file[] = "iconv_fread.out";
      42             :         int i;
      43             :         FILE *f;
      44           8 :         TDSCONTEXT *ctx = tds_alloc_context(NULL);
      45           8 :         TDSSOCKET *tds = tds_alloc_socket(ctx, 512);
      46             :         TDSICONV * conv;
      47             :         const char *tdsdump;
      48             : 
      49           8 :         tdsdump = getenv("TDSDUMP");
      50           8 :         if (tdsdump)
      51           0 :                 tdsdump_open(tdsdump);
      52             : 
      53           8 :         if (!ctx || !tds) {
      54           0 :                 fprintf(stderr, "Error creating socket!\n");
      55           0 :                 return 1;
      56             :         }
      57             : 
      58           8 :         tds_iconv_open(tds->conn, "ISO-8859-1", 0);
      59             : 
      60           8 :         conv = tds_iconv_get(tds->conn, "UTF-8", "ISO-8859-1");
      61           8 :         if (conv == NULL) {
      62           0 :                 fprintf(stderr, "Error creating conversion, giving up!\n");
      63           0 :                 return 1;
      64             :         }
      65             : 
      66           8 :         f = fopen(out_file, "w+b");
      67           8 :         if (!f) {
      68           0 :                 fprintf(stderr, "Error opening file!\n");
      69           0 :                 return 1;
      70             :         }
      71             : 
      72        1160 :         for (i = 0; i < 4096+20; ++i) {
      73         576 :                 char *out = NULL;
      74         576 :                 size_t out_len = 0xdeadbeef;
      75             :                 TDSRET res;
      76         576 :                 const unsigned char x = 0x90;
      77             : 
      78         576 :                 if (i == 32)
      79           8 :                         i = 4096-20;
      80             : 
      81             :                 /* write test string to file */
      82         576 :                 if (fseek(f, 0L, SEEK_SET)) {
      83           0 :                         fprintf(stderr, "Error seeking!\n");
      84           0 :                         return 1;
      85             :                 }
      86         576 :                 memset(buf, 'a', i);
      87         576 :                 buf[i] = 0xC0 + (x >> 6);
      88         576 :                 buf[i+1] = 0x80 + (x & 0x3f);
      89         576 :                 buf[i+2] = '!';
      90         576 :                 buf[i+3] = '!';
      91             : 
      92         576 :                 fwrite(buf, 1, i+4, f);
      93         576 :                 if (fseek(f, 0L, SEEK_SET)) {
      94           0 :                         fprintf(stderr, "Error seeking!\n");
      95           0 :                         return 1;
      96             :                 }
      97             : 
      98             :                 /* convert it */
      99         576 :                 res = tds_bcp_fread(NULL, conv, f, "!!", 2, &out, &out_len);
     100         576 :                 printf("res %d out_len %u\n", (int) res, (unsigned int) out_len);
     101             : 
     102             :                 /* test */
     103         576 :                 memset(buf, 'a', i);
     104         576 :                 buf[i] = (char) x;
     105         576 :                 assert(TDS_SUCCEED(res));
     106         576 :                 if (out_len != i+1) {
     107           0 :                         fprintf(stderr, "out %u bytes expected %d\n",
     108             :                                 (unsigned int) out_len, i+1);
     109           0 :                         return 1;
     110             :                 }
     111         576 :                 assert(memcmp(out, buf, i+1) == 0);
     112         576 :                 free(out);
     113             :         }
     114           8 :         fclose(f);
     115           8 :         unlink(out_file);
     116             : 
     117           8 :         tds_free_socket(tds);
     118           8 :         tds_free_context(ctx);
     119           8 :         return 0;
     120             : }

Generated by: LCOV version 1.13