1/*
2Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3
4The MySQL Connector/C is licensed under the terms of the GPLv2
5<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
6MySQL Connectors. There are special exceptions to the terms and
7conditions of the GPLv2 as it is applied to this software, see the
8FLOSS License Exception
9<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published
13by the Free Software Foundation; version 2 of the License.
14
15This program is distributed in the hope that it will be useful, but
16WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18for more details.
19
20You should have received a copy of the GNU General Public License along
21with this program; if not, write to the Free Software Foundation, Inc.,
2251 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24#include "my_test.h"
25
26/* Test warnings */
27
28static int test_client_warnings(MYSQL *mysql)
29{
30 int rc;
31
32 rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
33 check_mysql_rc(rc, mysql);
34 rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
35 check_mysql_rc(rc, mysql);
36
37 FAIL_IF(!mysql_warning_count(mysql), "Warning expected");
38
39 return OK;
40}
41
42
43static int test_ps_client_warnings(MYSQL *mysql)
44{
45 int rc;
46 MYSQL_STMT *stmt;
47 const char *query= "DROP TABLE IF EXISTS test_non_exists";
48
49 rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
50 check_mysql_rc(rc, mysql);
51
52 stmt= mysql_stmt_init(mysql);
53 rc= mysql_stmt_prepare(stmt, SL(query));
54 FAIL_IF(rc, mysql_stmt_error(stmt));
55
56 rc= mysql_stmt_execute(stmt);
57 FAIL_IF(rc, mysql_stmt_error(stmt));
58
59 FAIL_IF(!mysql_warning_count(mysql), "Warning expected");
60
61 mysql_stmt_close(stmt);
62
63 return OK;
64}
65
66static int test_server_warnings(MYSQL *mysql)
67{
68 int rc;
69 MYSQL_RES *result;
70
71 rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
72 check_mysql_rc(rc, mysql);
73 rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
74 check_mysql_rc(rc, mysql);
75
76 rc= mysql_query(mysql, "SHOW WARNINGS");
77 check_mysql_rc(rc, mysql);
78
79 result= mysql_store_result(mysql);
80 FAIL_IF(!result, mysql_error(mysql));
81 FAIL_IF(!mysql_num_rows(result), "Empty resultset");
82
83 mysql_free_result(result);
84
85 return OK;
86}
87
88
89/* Test errors */
90
91static int test_client_errors(MYSQL *mysql)
92{
93 int rc;
94
95 rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
96 check_mysql_rc(rc, mysql);
97
98 rc= mysql_query(mysql, "DROP TABLE test_non_exists");
99 FAIL_IF(!rc, "Error expected");
100
101 FAIL_IF(!mysql_errno(mysql), "Error expected");
102 FAIL_IF(!strlen(mysql_error(mysql)), "Empty errormsg");
103 FAIL_IF(strcmp(mysql_sqlstate(mysql), "00000") == 0, "Invalid SQLstate");
104
105 return OK;
106}
107
108static int test_ps_client_errors(MYSQL *mysql)
109{
110 int rc;
111 MYSQL_STMT *stmt;
112 const char *query= "DROP TABLE test_non_exists";
113
114 rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
115 check_mysql_rc(rc, mysql);
116
117 stmt= mysql_stmt_init(mysql);
118 rc= mysql_stmt_prepare(stmt, SL(query));
119 FAIL_IF(rc, mysql_stmt_error(stmt));
120
121 rc= mysql_stmt_execute(stmt);
122 FAIL_IF(!rc, mysql_stmt_error(stmt));
123
124 FAIL_IF(!mysql_stmt_errno(stmt), "Error expected");
125 FAIL_IF(!strlen(mysql_stmt_error(stmt)), "Empty errormsg");
126 FAIL_IF(strcmp(mysql_stmt_sqlstate(stmt), "00000") == 0, "Invalid SQLstate");
127
128 mysql_stmt_close(stmt);
129
130 return OK;
131}
132
133static int test_server_errors(MYSQL *mysql)
134{
135 int rc;
136 MYSQL_RES *result;
137
138 rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
139 check_mysql_rc(rc, mysql);
140
141 rc= mysql_query(mysql, "DROP TABLE test_non_exists");
142 check_mysql_rc(rc, mysql);
143
144 rc= mysql_query(mysql, "SHOW ERRORS");
145 check_mysql_rc(rc, mysql);
146
147 result= mysql_store_result(mysql);
148 FAIL_IF(!result, mysql_error(mysql));
149 FAIL_IF(!mysql_num_rows(result), "Empty resultset");
150 mysql_free_result(result);
151
152 return OK;
153}
154
155/* Bug #16143: mysql_stmt_sqlstate returns an empty string instead of '00000' */
156
157static int test_bug16143(MYSQL *mysql)
158{
159 MYSQL_STMT *stmt;
160
161 stmt= mysql_stmt_init(mysql);
162 FAIL_IF(!stmt, mysql_error(mysql));
163
164 /* Check mysql_stmt_sqlstate return "no error" */
165 FAIL_UNLESS(strcmp(mysql_stmt_sqlstate(stmt), "00000") == 0, "Expected SQLstate 000000");
166
167 mysql_stmt_close(stmt);
168
169 return OK;
170}
171
172/* Test warnings for cuted rows */
173
174static int test_cuted_rows(MYSQL *mysql)
175{
176 int rc, count;
177 MYSQL_RES *result;
178
179 if (!is_mariadb)
180 return SKIP;
181
182 mysql_query(mysql, "DROP TABLE if exists t1");
183 mysql_query(mysql, "DROP TABLE if exists t2");
184
185 rc= mysql_query(mysql, "CREATE TABLE t1(c1 tinyint)");
186 check_mysql_rc(rc, mysql);
187
188 rc= mysql_query(mysql, "CREATE TABLE t2(c1 int not null)");
189 check_mysql_rc(rc, mysql);
190
191 rc= mysql_query(mysql, "INSERT INTO t1 values(10), (NULL), (NULL)");
192 check_mysql_rc(rc, mysql);
193
194 count= mysql_warning_count(mysql);
195 FAIL_UNLESS(count == 0, "warnings != 0");
196
197 rc= mysql_query(mysql, "INSERT INTO t2 SELECT * FROM t1");
198 check_mysql_rc(rc, mysql);
199
200 count= mysql_warning_count(mysql);
201 FAIL_UNLESS(count == 2, "warnings != 2");
202
203 rc= mysql_query(mysql, "SHOW WARNINGS");
204 check_mysql_rc(rc, mysql);
205
206 result= mysql_store_result(mysql);
207 FAIL_IF(!result, "Invalid result set");
208
209 rc= 0;
210 while (mysql_fetch_row(result))
211 rc++;
212 FAIL_UNLESS(rc == 2, "rowcount != 2");
213 mysql_free_result(result);
214
215 rc= mysql_query(mysql, "INSERT INTO t1 VALUES('junk'), (876789)");
216 check_mysql_rc(rc, mysql);
217
218 count= mysql_warning_count(mysql);
219 FAIL_UNLESS(count == 2, "warnings != 2");
220
221 rc= mysql_query(mysql, "SHOW WARNINGS");
222 check_mysql_rc(rc, mysql);
223
224 result= mysql_store_result(mysql);
225 FAIL_IF(!result, "Invalid result set");
226
227 rc= 0;
228 while (mysql_fetch_row(result))
229 rc++;
230 FAIL_UNLESS(rc == 2, "rowcount != 2");
231 mysql_free_result(result);
232
233 rc= mysql_query(mysql, "DROP TABLE t1, t2");
234 check_mysql_rc(rc, mysql);
235 return OK;
236}
237
238static int test_parse_error_and_bad_length(MYSQL *mysql)
239{
240 MYSQL_STMT *stmt;
241 int rc;
242
243 /* check that we get 4 syntax errors over the 4 calls */
244
245 rc= mysql_query(mysql, "SHOW DATABAAAA");
246 FAIL_UNLESS(rc, "Error expected");
247 rc= mysql_real_query(mysql, SL_BIN("SHOW DATABASES\0AAA"));
248 FAIL_UNLESS(rc, "Error expected");
249
250 stmt= mysql_stmt_init(mysql);
251 FAIL_IF(!stmt, mysql_error(mysql));
252 rc= mysql_stmt_prepare(stmt, SL("SHOW DATABAAAA"));
253 FAIL_IF(!rc, "Error expected");
254 mysql_stmt_close(stmt);
255 stmt= mysql_stmt_init(mysql);
256 FAIL_UNLESS(stmt, "");
257 rc= mysql_stmt_prepare(stmt, "SHOW DATABASES", 100);
258 FAIL_IF(!rc, "Error expected");
259 mysql_stmt_close(stmt);
260 return OK;
261}
262
263
264struct my_tests_st my_tests[] = {
265 {"test_client_warnings", test_client_warnings, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
266 {"test_ps_client_warnings", test_ps_client_warnings, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
267 {"test_server_warnings", test_server_warnings, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
268 {"test_client_errors", test_client_errors, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
269 {"test_ps_client_errors", test_ps_client_errors, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
270 {"test_server_errors", test_server_errors, TEST_CONNECTION_DEFAULT, 0, NULL , "Open bug: #42364"},
271 {"test_bug16143", test_bug16143, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
272 {"test_cuted_rows", test_cuted_rows, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
273 {"test_parse_error_and_bad_length", test_parse_error_and_bad_length, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
274 {NULL, NULL, 0, 0, NULL, NULL}
275};
276
277int main(int argc, char **argv)
278{
279 if (argc > 1)
280 get_options(argc, argv);
281
282 get_envvars();
283
284 run_tests(my_tests);
285
286 return(exit_status());
287}
288