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 unsigned char buf[4096+80];
37 :
38 10 : TEST_MAIN()
39 : {
40 : static const char out_file[] = "iconv_fread.out";
41 : int i;
42 : FILE *f;
43 10 : TDSCONTEXT *ctx = tds_alloc_context(NULL);
44 10 : TDSSOCKET *tds = tds_alloc_socket(ctx, 512);
45 : TDSICONV * conv;
46 : const tds_dir_char *tdsdump;
47 :
48 10 : tdsdump = tds_dir_getenv(TDS_DIR("TDSDUMP"));
49 10 : if (tdsdump)
50 0 : tdsdump_open(tdsdump);
51 :
52 10 : if (!ctx || !tds) {
53 0 : fprintf(stderr, "Error creating socket!\n");
54 0 : return 1;
55 : }
56 :
57 10 : tds_iconv_open(tds->conn, "ISO-8859-1", 0);
58 :
59 10 : conv = tds_iconv_get(tds->conn, "UTF-8", "ISO-8859-1");
60 10 : if (conv == NULL) {
61 0 : fprintf(stderr, "Error creating conversion, giving up!\n");
62 0 : return 1;
63 : }
64 :
65 10 : f = fopen(out_file, "w+b");
66 10 : if (!f) {
67 0 : fprintf(stderr, "Error opening file!\n");
68 0 : return 1;
69 : }
70 :
71 1450 : for (i = 0; i < 4096+20; ++i) {
72 720 : char *out = NULL;
73 720 : size_t out_len = 0xdeadbeef;
74 : TDSRET res;
75 720 : const unsigned char x = 0x90;
76 :
77 720 : if (i == 32)
78 10 : i = 4096-20;
79 :
80 : /* write test string to file */
81 720 : if (fseek(f, 0L, SEEK_SET)) {
82 0 : fprintf(stderr, "Error seeking!\n");
83 0 : return 1;
84 : }
85 720 : memset(buf, 'a', i);
86 720 : buf[i] = (char) (0xC0 + (x >> 6));
87 720 : buf[i+1] = (char) (0x80 + (x & 0x3f));
88 720 : buf[i+2] = '!';
89 720 : buf[i+3] = '!';
90 :
91 720 : fwrite(buf, 1, i+4, f);
92 720 : if (fseek(f, 0L, SEEK_SET)) {
93 0 : fprintf(stderr, "Error seeking!\n");
94 0 : return 1;
95 : }
96 :
97 : /* convert it */
98 720 : res = tds_bcp_fread(NULL, conv, f, "!!", 2, &out, &out_len);
99 720 : printf("res %d out_len %u\n", (int) res, (unsigned int) out_len);
100 :
101 : /* test */
102 720 : memset(buf, 'a', i);
103 720 : buf[i] = x;
104 720 : assert(TDS_SUCCEED(res));
105 720 : if (out_len != i+1) {
106 0 : fprintf(stderr, "out %u bytes expected %d\n",
107 : (unsigned int) out_len, i+1);
108 0 : return 1;
109 : }
110 720 : assert(memcmp(out, buf, i+1) == 0);
111 720 : free(out);
112 : }
113 10 : fclose(f);
114 10 : unlink(out_file);
115 :
116 10 : tds_free_socket(tds);
117 10 : tds_free_context(ctx);
118 10 : return 0;
119 : }
|