Line data Source code
1 : /*
2 : * Purpose: Test Some conversion, check trimming error and results
3 : * Functions: dbconvert dberrhandle dbmsghandle dbinit dbexit
4 : */
5 :
6 : #include "common.h"
7 : #include <ctype.h>
8 :
9 : static int failure = 0;
10 :
11 : static const char *cur_result = "";
12 : static const char *cur_test = "";
13 : static int cur_line = 0;
14 :
15 : int test(int srctype, const void *srcdata, int srclen, int dsttype, int dstlen);
16 : int err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr);
17 :
18 : int
19 16 : err_handler(DBPROCESS * dbproc TDS_UNUSED, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
20 : {
21 : /*
22 : * For server messages, cancel the query and rely on the
23 : * message handler to spew the appropriate error messages out.
24 : */
25 16 : if (dberr == SYBESMSG)
26 : return INT_CANCEL;
27 :
28 16 : if (dberr == 20049) {
29 16 : fprintf(stderr, "OK: anticipated error %d (%s) arrived\n", dberr, dberrstr);
30 : } else {
31 0 : fprintf(stderr,
32 : "DB-LIBRARY error (severity %d, dberr %d, oserr %d, dberrstr %s, oserrstr %s):\n",
33 : severity, dberr, oserr, dberrstr ? dberrstr : "(null)", oserrstr ? oserrstr : "(null)");
34 : }
35 16 : fflush(stderr);
36 :
37 16 : return INT_CANCEL;
38 : }
39 :
40 : int
41 168 : test(int srctype, const void *srcdata, int srclen, int dsttype, int dstlen)
42 : {
43 : DBCHAR buf[10];
44 : char s[20], *p;
45 : int i, len, correct;
46 : char out[256];
47 :
48 168 : memset(buf, '*', sizeof(buf));
49 168 : len = dbconvert(NULL, srctype, (const BYTE*) srcdata, srclen, dsttype, (BYTE*) buf, dstlen);
50 :
51 : /* build result string */
52 168 : sprintf(out, "len=%d", len);
53 168 : p = s;
54 1848 : for (i = 0; i < sizeof(buf); ++i) {
55 1680 : *p++ = isprint((unsigned char) buf[i]) ? buf[i] : '.';
56 1680 : sprintf(strchr(out, 0), " %02X", (unsigned char) buf[i]);
57 : }
58 168 : *p = 0;
59 :
60 168 : correct = 0;
61 168 : if (len == -1) {
62 16 : if (strcmp(cur_result, "error") == 0)
63 : correct = 1;
64 : } else {
65 152 : if (strcmp(cur_result, out) == 0)
66 : correct = 1;
67 : }
68 : if (!correct) {
69 0 : failure = 1;
70 0 : printf("\nline: %d test: %s\n" "%s\n%s\n" "failed :( should be '%s'\n", cur_line, cur_test, s, out, cur_result);
71 : }
72 168 : return 0;
73 : }
74 :
75 : #define TEST(s,out) \
76 : { cur_result = out; cur_line = __LINE__; cur_test = #s; test s; }
77 :
78 : int
79 8 : main(void)
80 : {
81 8 : if (dbinit() == FAIL)
82 : return 1;
83 :
84 8 : dberrhandle(err_handler);
85 8 : dbmsghandle(syb_msg_handler);
86 :
87 8 : TEST((SYBBINARY, "ciao\0\0", 6, SYBBINARY, -2), "len=6 63 69 61 6F 00 00 2A 2A 2A 2A");
88 8 : TEST((SYBCHAR, "ciao ", 6, SYBCHAR, -2), "len=6 63 69 61 6F 20 20 00 2A 2A 2A");
89 8 : TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, -2), "len=6 63 69 61 6F 00 00 00 2A 2A 2A");
90 8 : TEST((SYBCHAR, "ciao ", 6, SYBCHAR, -1), "len=4 63 69 61 6F 00 2A 2A 2A 2A 2A");
91 8 : TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, -1), "len=6 63 69 61 6F 00 00 00 2A 2A 2A");
92 8 : TEST((SYBCHAR, "ciao ", 6, SYBCHAR, 8), "len=6 63 69 61 6F 20 20 20 20 2A 2A");
93 8 : TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, 8), "len=6 63 69 61 6F 00 00 20 20 2A 2A");
94 8 : TEST((SYBCHAR, "ciao ", 6, SYBCHAR, 4), "error");
95 8 : TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, 4), "error");
96 8 : TEST((SYBCHAR, "ciao ", 6, SYBCHAR, 6), "len=6 63 69 61 6F 20 20 2A 2A 2A 2A");
97 8 : TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, 6), "len=6 63 69 61 6F 00 00 2A 2A 2A 2A");
98 :
99 : /* convert from NULL to BINARY */
100 8 : TEST((SYBBINARY, "", 0, SYBBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
101 8 : TEST((SYBVARBINARY, "", 0, SYBBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
102 8 : TEST((SYBIMAGE, "", 0, SYBBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
103 8 : TEST((SYBBINARY, "", 0, SYBVARBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
104 8 : TEST((SYBVARBINARY, "", 0, SYBVARBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
105 8 : TEST((SYBIMAGE, "", 0, SYBVARBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
106 8 : TEST((SYBBINARY, "", 0, SYBIMAGE, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
107 8 : TEST((SYBVARBINARY, "", 0, SYBIMAGE, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
108 8 : TEST((SYBIMAGE, "", 0, SYBIMAGE, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
109 :
110 8 : TEST((SYBBINARY, "", 0, SYBBINARY, -1), "len=0 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A");
111 :
112 8 : dbexit();
113 8 : if (!failure)
114 8 : printf("All tests passed!\n");
115 8 : return failure;
116 : }
|