1 #undef NDEBUG
2 #include "common.h"
3 #include <assert.h>
4 #include <freetds/utils/string.h>
5 #include <freetds/odbc.h>
6
7 /* test some internal funcions */
8
9 #ifdef _WIN32
10 HINSTANCE hinstFreeTDS;
11 #endif
12
13 #ifdef ENABLE_ODBC_WIDE
14 static void
wide_test(const WCHAR * input,size_t input_len,const char * exp,int line)15 wide_test(const WCHAR* input, size_t input_len, const char *exp, int line)
16 {
17 DSTR s = DSTR_INITIALIZER;
18 SQLWCHAR outbuf[16];
19 SQLINTEGER outlen;
20
21 odbc_dstr_copy_flag((TDS_DBC *) odbc_conn, &s, (int) input_len, (ODBC_CHAR*) input, 1);
22 if (strlen(exp) != tds_dstr_len(&s) || strcmp(exp, tds_dstr_cstr(&s)) != 0) {
23 fprintf(stderr, "%d: Wrong, len %u: %s\n", line,
24 (unsigned) tds_dstr_len(&s), tds_dstr_cstr(&s));
25 exit(1);
26 }
27 outlen = -1;
28 odbc_set_string_flag((TDS_DBC *) odbc_conn, outbuf, TDS_VECTOR_SIZE(outbuf), &outlen,
29 tds_dstr_cstr(&s), (int) tds_dstr_len(&s), 0x11);
30 if (outlen < 0 || outlen !=input_len
31 || memcmp(outbuf, input, input_len * sizeof(input[0])) != 0) {
32 fprintf(stderr, "%d: out_len %u %x %x %x\n", line, outlen, outbuf[0], outbuf[1], outbuf[2]);
33 exit(1);
34 }
35 tds_dstr_free(&s);
36 }
37 #endif
38
39 int
main(int argc,char * argv[])40 main(int argc, char *argv[])
41 {
42 #ifdef ENABLE_ODBC_WIDE
43 DSTR s = DSTR_INITIALIZER;
44
45 #ifdef _WIN32
46 hinstFreeTDS = GetModuleHandle(NULL);
47 #endif
48
49 /* just allocate handles, we don't need to connect */
50 CHKAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &odbc_env, "S");
51 SQLSetEnvAttr(odbc_env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) (SQL_OV_ODBC3), SQL_IS_UINTEGER);
52 CHKAllocHandle(SQL_HANDLE_DBC, odbc_env, &odbc_conn, "S");
53
54 /* check is FreeTDS, if not just return */
55 if (!odbc_driver_is_freetds()) {
56 odbc_disconnect();
57 return 0;
58 }
59
60 odbc_dstr_copy_flag((TDS_DBC *) odbc_conn, &s, 3, (ODBC_CHAR*) "foo", 0);
61 assert(strcmp("foo", tds_dstr_cstr(&s)) == 0);
62
63 #define WIDE_TEST(chars, exp) do { \
64 static const SQLWCHAR input[] = chars; \
65 wide_test(input, TDS_VECTOR_SIZE(input), exp, __LINE__); \
66 } while(0)
67 #define SEP ,
68
69 WIDE_TEST({ 'f' SEP 'o' SEP 'o' }, "foo");
70 WIDE_TEST({ 0x41 }, "A");
71 WIDE_TEST({ 0xA1 }, "\xc2\xA1");
72 WIDE_TEST({ 0x81 }, "\xc2\x81");
73 WIDE_TEST({ 0x101 }, "\xc4\x81");
74 WIDE_TEST({ 0x201 }, "\xc8\x81");
75 WIDE_TEST({ 0x401 }, "\xd0\x81");
76 WIDE_TEST({ 0x801 }, "\xe0\xa0\x81");
77 WIDE_TEST({ 0x1001 }, "\xe1\x80\x81");
78 WIDE_TEST({ 0x2001 }, "\xe2\x80\x81");
79 WIDE_TEST({ 0x4001 }, "\xe4\x80\x81");
80 WIDE_TEST({ 0x8001 }, "\xe8\x80\x81");
81 #if SIZEOF_SQLWCHAR == 2
82 WIDE_TEST({ 0xd800 SEP 0xdc01 }, "\xf0\x90\x80\x81");
83 WIDE_TEST({ 0xd800 SEP 0xdd01 }, "\xf0\x90\x84\x81");
84 WIDE_TEST({ 0xd840 SEP 0xdd01 }, "\xf0\xa0\x84\x81");
85 WIDE_TEST({ 0xd8c0 SEP 0xdd01 }, "\xf1\x80\x84\x81");
86 WIDE_TEST({ 0xd9c0 SEP 0xdd01 }, "\xf2\x80\x84\x81");
87 #else
88 WIDE_TEST({ 0x10001 }, "\xf0\x90\x80\x81");
89 #endif
90
91 tds_dstr_free(&s);
92 odbc_disconnect();
93 #endif
94 return 0;
95 }
96
97