Line data Source code
1 : #include "common.h"
2 :
3 : /* Testing: data truncation behavior of ct_fetch */
4 10 : TEST_MAIN()
5 : {
6 : CS_CONTEXT *ctx;
7 : CS_CONNECTION *conn;
8 : CS_COMMAND *cmd;
9 10 : bool verbose = false;
10 :
11 : CS_RETCODE ret;
12 : CS_RETCODE results_ret;
13 : CS_INT result_type;
14 : CS_INT num_cols;
15 :
16 : CS_DATAFMT datafmt;
17 10 : CS_INT copied = 0;
18 10 : CS_SMALLINT ind = 0;
19 10 : CS_INT count, row_count = 0;
20 :
21 : CS_CHAR select[1024];
22 :
23 10 : char *addr = NULL;
24 :
25 10 : printf("%s: test data truncation behavior of ct_fetch \n", __FILE__);
26 : if (verbose) {
27 : printf("Trying login\n");
28 : }
29 10 : check_call(try_ctlogin_with_options, (argc, argv, &ctx, &conn, &cmd, verbose));
30 10 : if (common_pwd.fverbose)
31 0 : verbose = true;
32 :
33 10 : strcpy(select, "select name from systypes where datalength(name) > 2*9 order by datalength(name)");
34 10 : printf("%s\n", select);
35 :
36 10 : check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
37 :
38 10 : check_call(ct_send, (cmd));
39 :
40 40 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
41 20 : switch ((int) result_type) {
42 : case CS_CMD_SUCCEED:
43 : break;
44 : case CS_CMD_DONE:
45 : break;
46 0 : case CS_CMD_FAIL:
47 0 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
48 0 : free(addr);
49 0 : return 1;
50 10 : case CS_ROW_RESULT:
51 :
52 10 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
53 10 : fprintf(stderr, "%d column%s\n", num_cols, num_cols==1? "":"s");
54 :
55 10 : check_call(ct_describe, (cmd, 1, &datafmt));
56 :
57 10 : fprintf(stderr, "type = %d\n", datafmt.datatype);
58 10 : fprintf(stderr, "maxlength = %d\n", datafmt.maxlength);
59 10 : if (datafmt.datatype == CS_CHAR_TYPE) {
60 10 : fprintf(stderr, "CS_CHAR_TYPE\n");
61 10 : datafmt.format = CS_FMT_NULLTERM;
62 10 : addr = (char *) malloc(datafmt.maxlength);
63 : } else {
64 0 : fputs("unexpected data type\n", stderr);
65 0 : return 1;
66 : }
67 :
68 10 : fprintf(stderr, "binding column 1 (%s)\n", datafmt.name);
69 :
70 : /* set maxlength to something short to test truncation behavior */
71 10 : if (common_pwd.maxlength)
72 0 : datafmt.maxlength = common_pwd.maxlength;
73 :
74 10 : check_call(ct_bind, (cmd, 1, &datafmt, addr, &copied, &ind));
75 :
76 10 : fprintf(stderr, "fetching rows with datafmt.maxlength = %d\n", datafmt.maxlength);
77 :
78 10 : while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) != CS_END_DATA) {
79 :
80 40 : fprintf(stderr, "ct_fetch() row %d returned %d.\n", row_count, (int) ret);
81 40 : addr[copied] = '\0';
82 40 : fprintf(stderr, "copied %d bytes: [%s]\n", copied, addr);
83 40 : row_count += count;
84 :
85 40 : switch (ret) {
86 40 : case CS_SUCCEED:
87 40 : printf("ct_fetch returned %d row%s\n", count, count==1? "":"s");
88 40 : break;
89 0 : case CS_ROW_FAIL:
90 0 : fprintf(stderr, "error: ct_fetch() returned CS_ROW_FAIL on row %d.\n", row_count);
91 0 : free(addr);
92 0 : return 1;
93 0 : case CS_CANCELED:
94 0 : fprintf(stderr, "error: ct_fetch() returned CS_CANCELED??\n");
95 0 : free(addr);
96 0 : return 1;
97 0 : case CS_FAIL:
98 0 : fprintf(stderr, "error: ct_fetch() returned CS_FAIL.\n");
99 0 : free(addr);
100 0 : return 1;
101 0 : default:
102 0 : fprintf(stderr, "error: ct_fetch() unexpected return.\n");
103 0 : free(addr);
104 0 : return 1;
105 : }
106 : }
107 :
108 : break;
109 : }
110 : }
111 :
112 10 : switch ((int) results_ret) {
113 : case CS_END_RESULTS:
114 : break;
115 0 : case CS_FAIL:
116 0 : fprintf(stderr, "ct_results() failed.\n");
117 0 : free(addr);
118 0 : return 1;
119 : break;
120 0 : default:
121 0 : fprintf(stderr, "ct_results() unexpected return.\n");
122 0 : free(addr);
123 0 : return 1;
124 : }
125 :
126 10 : if (verbose) {
127 0 : printf("Trying logout\n");
128 : }
129 10 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
130 :
131 10 : free(addr);
132 10 : return 0;
133 : }
|