1 | /* |
2 | Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
3 | |
4 | The 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 |
6 | MySQL Connectors. There are special exceptions to the terms and |
7 | conditions of the GPLv2 as it is applied to this software, see the |
8 | FLOSS License Exception |
9 | <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
10 | |
11 | This program is free software; you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published |
13 | by the Free Software Foundation; version 2 of the License. |
14 | |
15 | This program is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
17 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
18 | for more details. |
19 | |
20 | You should have received a copy of the GNU General Public License along |
21 | with this program; if not, write to the Free Software Foundation, Inc., |
22 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
23 | */ |
24 | /** |
25 | Some basic tests of the client API. |
26 | */ |
27 | |
28 | #include "my_test.h" |
29 | |
30 | static int test_conc66(MYSQL *my) |
31 | { |
32 | MYSQL *mysql= mysql_init(NULL); |
33 | int rc; |
34 | FILE *fp; |
35 | char query[1024]; |
36 | |
37 | if (!(fp= fopen("./my-conc66-test.cnf" , "w" ))) |
38 | return FAIL; |
39 | |
40 | fprintf(fp, "[notmygroup]\n" ); |
41 | fprintf(fp, "user=foo\n" ); |
42 | fprintf(fp, "[conc-66]\n" ); |
43 | fprintf(fp, "user=conc66\n" ); |
44 | fprintf(fp, "port=3306\n" ); |
45 | fprintf(fp, "enable-local-infile\n" ); |
46 | fprintf(fp, "password='test\\\";#test'\n" ); |
47 | |
48 | fclose(fp); |
49 | |
50 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "conc-66" ); |
51 | check_mysql_rc(rc, mysql); |
52 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "./my-conc66-test.cnf" ); |
53 | check_mysql_rc(rc, mysql); |
54 | |
55 | sprintf(query, "GRANT ALL ON %s.* TO 'conc66'@'%s' IDENTIFIED BY 'test\";#test'" , schema, this_host ? this_host : "localhost" ); |
56 | rc= mysql_query(my, query); |
57 | check_mysql_rc(rc, my); |
58 | rc= mysql_query(my, "FLUSH PRIVILEGES" ); |
59 | check_mysql_rc(rc, my); |
60 | if (!my_test_connect(mysql, hostname, NULL, |
61 | NULL, schema, port, socketname, 0)) |
62 | { |
63 | diag("user: %s" , mysql->options.user); |
64 | diag("Error: %s" , mysql_error(mysql)); |
65 | return FAIL; |
66 | } |
67 | diag("user: %s" , mysql->options.user); |
68 | |
69 | sprintf(query, "DROP user 'conc66'@'%s'" , this_host ? this_host : "localhost" ); |
70 | rc= mysql_query(my, query); |
71 | |
72 | check_mysql_rc(rc, my); |
73 | mysql_close(mysql); |
74 | return OK; |
75 | } |
76 | |
77 | static int test_bug20023(MYSQL *mysql) |
78 | { |
79 | int sql_big_selects_orig; |
80 | int max_join_size_orig; |
81 | |
82 | int sql_big_selects_2; |
83 | int sql_big_selects_3; |
84 | int sql_big_selects_4; |
85 | int sql_big_selects_5; |
86 | int rc; |
87 | |
88 | if (!is_mariadb) |
89 | return SKIP; |
90 | |
91 | if (mysql_get_server_version(mysql) < 50100) { |
92 | diag("Test requires MySQL Server version 5.1 or above" ); |
93 | return SKIP; |
94 | } |
95 | |
96 | /*********************************************************************** |
97 | Remember original SQL_BIG_SELECTS, MAX_JOIN_SIZE values. |
98 | ***********************************************************************/ |
99 | |
100 | query_int_variable(mysql, |
101 | "@@session.sql_big_selects" , |
102 | &sql_big_selects_orig); |
103 | |
104 | query_int_variable(mysql, |
105 | "@@global.max_join_size" , |
106 | &max_join_size_orig); |
107 | |
108 | /*********************************************************************** |
109 | Test that COM_CHANGE_USER resets the SQL_BIG_SELECTS to the initial value. |
110 | ***********************************************************************/ |
111 | |
112 | /* Issue COM_CHANGE_USER. */ |
113 | rc= mysql_change_user(mysql, username, password, schema); |
114 | check_mysql_rc(rc, mysql); |
115 | |
116 | /* Query SQL_BIG_SELECTS. */ |
117 | |
118 | query_int_variable(mysql, |
119 | "@@session.sql_big_selects" , |
120 | &sql_big_selects_2); |
121 | |
122 | /* Check that SQL_BIG_SELECTS is reset properly. */ |
123 | |
124 | FAIL_UNLESS(sql_big_selects_orig == sql_big_selects_2, "Different value for sql_big_select" ); |
125 | |
126 | /*********************************************************************** |
127 | Test that if MAX_JOIN_SIZE set to non-default value, |
128 | SQL_BIG_SELECTS will be 0. |
129 | ***********************************************************************/ |
130 | |
131 | /* Set MAX_JOIN_SIZE to some non-default value. */ |
132 | |
133 | rc= mysql_query(mysql, "SET @@global.max_join_size = 10000" ); |
134 | check_mysql_rc(rc, mysql); |
135 | rc= mysql_query(mysql, "SET @@session.max_join_size = default" ); |
136 | check_mysql_rc(rc, mysql); |
137 | |
138 | /* Issue COM_CHANGE_USER. */ |
139 | |
140 | rc= mysql_change_user(mysql, username, password, schema); |
141 | check_mysql_rc(rc, mysql); |
142 | |
143 | /* Query SQL_BIG_SELECTS. */ |
144 | |
145 | query_int_variable(mysql, |
146 | "@@session.sql_big_selects" , |
147 | &sql_big_selects_3); |
148 | |
149 | /* Check that SQL_BIG_SELECTS is 0. */ |
150 | |
151 | FAIL_UNLESS(sql_big_selects_3 == 0, "big_selects != 0" ); |
152 | |
153 | /*********************************************************************** |
154 | Test that if MAX_JOIN_SIZE set to default value, |
155 | SQL_BIG_SELECTS will be 1. |
156 | ***********************************************************************/ |
157 | |
158 | /* Set MAX_JOIN_SIZE to the default value (-1). */ |
159 | |
160 | rc= mysql_query(mysql, "SET @@global.max_join_size = cast(-1 as unsigned int)" ); |
161 | rc= mysql_query(mysql, "SET @@session.max_join_size = default" ); |
162 | |
163 | /* Issue COM_CHANGE_USER. */ |
164 | |
165 | rc= mysql_change_user(mysql, username, password, schema); |
166 | check_mysql_rc(rc, mysql); |
167 | |
168 | /* Query SQL_BIG_SELECTS. */ |
169 | |
170 | query_int_variable(mysql, |
171 | "@@session.sql_big_selects" , |
172 | &sql_big_selects_4); |
173 | |
174 | /* Check that SQL_BIG_SELECTS is 1. */ |
175 | |
176 | FAIL_UNLESS(sql_big_selects_4 == 1, "sql_big_select != 1" ); |
177 | |
178 | /*********************************************************************** |
179 | Restore MAX_JOIN_SIZE. |
180 | Check that SQL_BIG_SELECTS will be the original one. |
181 | ***********************************************************************/ |
182 | |
183 | rc= mysql_query(mysql, "SET @@global.max_join_size = cast(-1 as unsigned int)" ); |
184 | check_mysql_rc(rc, mysql); |
185 | |
186 | rc= mysql_query(mysql, "SET @@session.max_join_size = default" ); |
187 | check_mysql_rc(rc, mysql); |
188 | |
189 | /* Issue COM_CHANGE_USER. */ |
190 | |
191 | rc= mysql_change_user(mysql, username, password, schema); |
192 | check_mysql_rc(rc, mysql); |
193 | |
194 | /* Query SQL_BIG_SELECTS. */ |
195 | |
196 | query_int_variable(mysql, |
197 | "@@session.sql_big_selects" , |
198 | &sql_big_selects_5); |
199 | |
200 | /* Check that SQL_BIG_SELECTS is 1. */ |
201 | |
202 | FAIL_UNLESS(sql_big_selects_5 == sql_big_selects_orig, "big_select != 1" ); |
203 | |
204 | /*********************************************************************** |
205 | That's it. Cleanup. |
206 | ***********************************************************************/ |
207 | |
208 | return OK; |
209 | } |
210 | |
211 | static int test_change_user(MYSQL *mysql) |
212 | { |
213 | char buff[256]; |
214 | const char *user_pw= "mysqltest_pw" ; |
215 | const char *user_no_pw= "mysqltest_no_pw" ; |
216 | const char *pw= "password" ; |
217 | const char *db= "mysqltest_user_test_database" ; |
218 | int rc; |
219 | |
220 | diag("Due to mysql_change_user security fix this test will not work anymore." ); |
221 | return(SKIP); |
222 | |
223 | /* Prepare environment */ |
224 | sprintf(buff, "drop database if exists %s" , db); |
225 | rc= mysql_query(mysql, buff); |
226 | check_mysql_rc(rc, mysql) |
227 | |
228 | sprintf(buff, "create database %s" , db); |
229 | rc= mysql_query(mysql, buff); |
230 | check_mysql_rc(rc, mysql) |
231 | |
232 | sprintf(buff, |
233 | "grant select on %s.* to %s@'%%' identified by '%s'" , |
234 | db, |
235 | user_pw, |
236 | pw); |
237 | rc= mysql_query(mysql, buff); |
238 | check_mysql_rc(rc, mysql) |
239 | |
240 | sprintf(buff, |
241 | "grant select on %s.* to %s@'%%'" , |
242 | db, |
243 | user_no_pw); |
244 | rc= mysql_query(mysql, buff); |
245 | check_mysql_rc(rc, mysql) |
246 | |
247 | |
248 | /* Try some combinations */ |
249 | rc= mysql_change_user(mysql, NULL, NULL, NULL); |
250 | FAIL_UNLESS(rc, "Error expected" ); |
251 | |
252 | |
253 | rc= mysql_change_user(mysql, "" , NULL, NULL); |
254 | FAIL_UNLESS(rc, "Error expected" ); |
255 | |
256 | rc= mysql_change_user(mysql, "" , "" , NULL); |
257 | FAIL_UNLESS(rc, "Error expected" ); |
258 | |
259 | rc= mysql_change_user(mysql, "" , "" , "" ); |
260 | FAIL_UNLESS(rc, "Error expected" ); |
261 | |
262 | rc= mysql_change_user(mysql, NULL, "" , "" ); |
263 | FAIL_UNLESS(rc, "Error expected" ); |
264 | |
265 | |
266 | rc= mysql_change_user(mysql, NULL, NULL, "" ); |
267 | FAIL_UNLESS(rc, "Error expected" ); |
268 | |
269 | rc= mysql_change_user(mysql, "" , NULL, "" ); |
270 | FAIL_UNLESS(rc, "Error expected" ); |
271 | |
272 | rc= mysql_change_user(mysql, user_pw, NULL, "" ); |
273 | FAIL_UNLESS(rc, "Error expected" ); |
274 | |
275 | rc= mysql_change_user(mysql, user_pw, "" , "" ); |
276 | FAIL_UNLESS(rc, "Error expected" ); |
277 | |
278 | rc= mysql_change_user(mysql, user_pw, "" , NULL); |
279 | FAIL_UNLESS(rc, "Error expected" ); |
280 | |
281 | rc= mysql_change_user(mysql, user_pw, NULL, NULL); |
282 | FAIL_UNLESS(rc, "Error expected" ); |
283 | |
284 | rc= mysql_change_user(mysql, user_pw, "" , db); |
285 | FAIL_UNLESS(rc, "Error expected" ); |
286 | |
287 | rc= mysql_change_user(mysql, user_pw, NULL, db); |
288 | FAIL_UNLESS(rc, "Error expected" ); |
289 | |
290 | rc= mysql_change_user(mysql, user_pw, pw, db); |
291 | check_mysql_rc(rc, mysql) |
292 | |
293 | rc= mysql_change_user(mysql, user_pw, pw, NULL); |
294 | check_mysql_rc(rc, mysql) |
295 | |
296 | rc= mysql_change_user(mysql, user_pw, pw, "" ); |
297 | check_mysql_rc(rc, mysql) |
298 | |
299 | rc= mysql_change_user(mysql, user_no_pw, pw, db); |
300 | FAIL_UNLESS(rc, "Error expected" ); |
301 | |
302 | rc= mysql_change_user(mysql, user_no_pw, pw, "" ); |
303 | FAIL_UNLESS(rc, "Error expected" ); |
304 | |
305 | rc= mysql_change_user(mysql, user_no_pw, pw, NULL); |
306 | FAIL_UNLESS(rc, "Error expected" ); |
307 | |
308 | rc= mysql_change_user(mysql, user_no_pw, "" , NULL); |
309 | check_mysql_rc(rc, mysql) |
310 | |
311 | rc= mysql_change_user(mysql, user_no_pw, "" , "" ); |
312 | check_mysql_rc(rc, mysql) |
313 | |
314 | rc= mysql_change_user(mysql, user_no_pw, "" , db); |
315 | check_mysql_rc(rc, mysql) |
316 | |
317 | rc= mysql_change_user(mysql, user_no_pw, NULL, db); |
318 | check_mysql_rc(rc, mysql) |
319 | |
320 | rc= mysql_change_user(mysql, "" , pw, db); |
321 | FAIL_UNLESS(rc, "Error expected" ); |
322 | |
323 | rc= mysql_change_user(mysql, "" , pw, "" ); |
324 | FAIL_UNLESS(rc, "Error expected" ); |
325 | |
326 | rc= mysql_change_user(mysql, "" , pw, NULL); |
327 | FAIL_UNLESS(rc, "Error expected" ); |
328 | |
329 | rc= mysql_change_user(mysql, NULL, pw, NULL); |
330 | FAIL_UNLESS(rc, "Error expected" ); |
331 | |
332 | rc= mysql_change_user(mysql, NULL, NULL, db); |
333 | FAIL_UNLESS(rc, "Error expected" ); |
334 | |
335 | rc= mysql_change_user(mysql, NULL, "" , db); |
336 | FAIL_UNLESS(rc, "Error expected" ); |
337 | |
338 | rc= mysql_change_user(mysql, "" , "" , db); |
339 | FAIL_UNLESS(rc, "Error expected" ); |
340 | |
341 | /* Cleanup the environment */ |
342 | |
343 | rc= mysql_change_user(mysql, username, password, schema); |
344 | check_mysql_rc(rc, mysql); |
345 | |
346 | sprintf(buff, "drop database %s" , db); |
347 | rc= mysql_query(mysql, buff); |
348 | check_mysql_rc(rc, mysql) |
349 | |
350 | sprintf(buff, "drop user %s@'%%'" , user_pw); |
351 | rc= mysql_query(mysql, buff); |
352 | check_mysql_rc(rc, mysql) |
353 | |
354 | sprintf(buff, "drop user %s@'%%'" , user_no_pw); |
355 | rc= mysql_query(mysql, buff); |
356 | check_mysql_rc(rc, mysql) |
357 | |
358 | return OK; |
359 | } |
360 | |
361 | /** |
362 | Bug#31669 Buffer overflow in mysql_change_user() |
363 | */ |
364 | |
365 | #define LARGE_BUFFER_SIZE 2048 |
366 | |
367 | static int test_bug31669(MYSQL *mysql) |
368 | { |
369 | int rc; |
370 | static char buff[LARGE_BUFFER_SIZE+1]; |
371 | static char user[USERNAME_CHAR_LENGTH+1]; |
372 | static char db[NAME_CHAR_LEN+1]; |
373 | static char query[LARGE_BUFFER_SIZE*2]; |
374 | |
375 | diag("Due to mysql_change_user security fix this test will not work anymore." ); |
376 | return(SKIP); |
377 | |
378 | rc= mysql_change_user(mysql, NULL, NULL, NULL); |
379 | FAIL_UNLESS(rc, "Error expected" ); |
380 | |
381 | rc= mysql_change_user(mysql, "" , "" , "" ); |
382 | FAIL_UNLESS(rc, "Error expected" ); |
383 | |
384 | memset(buff, 'a', sizeof(buff)); |
385 | |
386 | rc= mysql_change_user(mysql, buff, buff, buff); |
387 | FAIL_UNLESS(rc, "Error epected" ); |
388 | |
389 | rc = mysql_change_user(mysql, username, password, schema); |
390 | check_mysql_rc(rc, mysql); |
391 | |
392 | memset(db, 'a', sizeof(db)); |
393 | db[NAME_CHAR_LEN]= 0; |
394 | sprintf(query, "CREATE DATABASE IF NOT EXISTS %s" , db); |
395 | rc= mysql_query(mysql, query); |
396 | check_mysql_rc(rc, mysql); |
397 | |
398 | memset(user, 'b', sizeof(user)); |
399 | user[USERNAME_CHAR_LENGTH]= 0; |
400 | memset(buff, 'c', sizeof(buff)); |
401 | buff[LARGE_BUFFER_SIZE]= 0; |
402 | sprintf(query, "GRANT ALL PRIVILEGES ON *.* TO '%s'@'%%' IDENTIFIED BY '%s' WITH GRANT OPTION" , user, buff); |
403 | rc= mysql_query(mysql, query); |
404 | check_mysql_rc(rc, mysql); |
405 | |
406 | rc= mysql_query(mysql, "FLUSH PRIVILEGES" ); |
407 | check_mysql_rc(rc, mysql); |
408 | |
409 | rc= mysql_change_user(mysql, user, buff, db); |
410 | check_mysql_rc(rc, mysql); |
411 | |
412 | user[USERNAME_CHAR_LENGTH-1]= 'a'; |
413 | rc= mysql_change_user(mysql, user, buff, db); |
414 | FAIL_UNLESS(rc, "Error expected" ); |
415 | |
416 | user[USERNAME_CHAR_LENGTH-1]= 'b'; |
417 | buff[LARGE_BUFFER_SIZE-1]= 'd'; |
418 | rc= mysql_change_user(mysql, user, buff, db); |
419 | FAIL_UNLESS(rc, "Error expected" ); |
420 | |
421 | buff[LARGE_BUFFER_SIZE-1]= 'c'; |
422 | db[NAME_CHAR_LEN-1]= 'e'; |
423 | rc= mysql_change_user(mysql, user, buff, db); |
424 | FAIL_UNLESS(rc, "Error expected" ); |
425 | |
426 | db[NAME_CHAR_LEN-1]= 'a'; |
427 | rc= mysql_change_user(mysql, user, buff, db); |
428 | FAIL_UNLESS(!rc, "Error expected" ); |
429 | |
430 | rc= mysql_change_user(mysql, user + 1, buff + 1, db + 1); |
431 | FAIL_UNLESS(rc, "Error expected" ); |
432 | |
433 | rc = mysql_change_user(mysql, username, password, schema); |
434 | check_mysql_rc(rc, mysql); |
435 | |
436 | sprintf(query, "DROP DATABASE %s" , db); |
437 | rc= mysql_query(mysql, query); |
438 | check_mysql_rc(rc, mysql); |
439 | |
440 | sprintf(query, "DELETE FROM mysql.user WHERE User='%s'" , user); |
441 | rc= mysql_query(mysql, query); |
442 | check_mysql_rc(rc, mysql); |
443 | FAIL_UNLESS(mysql_affected_rows(mysql) == 1, "" ); |
444 | |
445 | return OK; |
446 | } |
447 | |
448 | /** |
449 | Bug# 33831 my_test_connect() should fail if |
450 | given an already connected MYSQL handle. |
451 | */ |
452 | |
453 | static int test_bug33831(MYSQL *mysql) |
454 | { |
455 | FAIL_IF(my_test_connect(mysql, hostname, username, |
456 | password, schema, port, socketname, 0), |
457 | "Error expected" ); |
458 | |
459 | return OK; |
460 | } |
461 | |
462 | /* Test MYSQL_OPT_RECONNECT, Bug#15719 */ |
463 | |
464 | static int test_opt_reconnect(MYSQL *mysql) |
465 | { |
466 | my_bool my_true= TRUE; |
467 | int rc; |
468 | my_bool reconnect; |
469 | |
470 | printf("true: %d\n" , TRUE); |
471 | |
472 | mysql= mysql_init(NULL); |
473 | FAIL_IF(!mysql, "not enough memory" ); |
474 | |
475 | mysql_get_option(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
476 | FAIL_UNLESS(reconnect == 0, "reconnect != 0" ); |
477 | |
478 | rc= mysql_options(mysql, MYSQL_OPT_RECONNECT, &my_true); |
479 | check_mysql_rc(rc, mysql); |
480 | |
481 | mysql_get_option(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
482 | FAIL_UNLESS(reconnect == 1, "reconnect != 1" ); |
483 | |
484 | if (!(my_test_connect(mysql, hostname, username, |
485 | password, schema, port, |
486 | socketname, 0))) |
487 | { |
488 | diag("connection failed" ); |
489 | mysql_close(mysql); |
490 | return FAIL; |
491 | } |
492 | |
493 | mysql_get_option(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
494 | FAIL_UNLESS(reconnect == 1, "reconnect != 1" ); |
495 | |
496 | mysql_close(mysql); |
497 | |
498 | mysql= mysql_init(NULL); |
499 | FAIL_IF(!mysql, "not enough memory" ); |
500 | |
501 | mysql_get_option(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
502 | FAIL_UNLESS(reconnect == 0, "reconnect != 0" ); |
503 | |
504 | if (!(my_test_connect(mysql, hostname, username, |
505 | password, schema, port, |
506 | socketname, 0))) |
507 | { |
508 | diag("connection failed" ); |
509 | mysql_close(mysql); |
510 | return FAIL; |
511 | } |
512 | |
513 | mysql_get_option(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
514 | FAIL_UNLESS(reconnect == 0, "reconnect != 0" ); |
515 | |
516 | mysql_close(mysql); |
517 | return OK; |
518 | } |
519 | |
520 | |
521 | static int test_compress(MYSQL *mysql) |
522 | { |
523 | MYSQL_RES *res; |
524 | MYSQL_ROW row; |
525 | int rc; |
526 | |
527 | mysql= mysql_init(NULL); |
528 | FAIL_IF(!mysql, "not enough memory" ); |
529 | |
530 | /* use compressed protocol */ |
531 | rc= mysql_options(mysql, MYSQL_OPT_COMPRESS, NULL); |
532 | |
533 | |
534 | |
535 | if (!(my_test_connect(mysql, hostname, username, |
536 | password, schema, port, |
537 | socketname, 0))) |
538 | { |
539 | diag("connection failed" ); |
540 | return FAIL; |
541 | } |
542 | |
543 | rc= mysql_query(mysql, "SHOW STATUS LIKE 'compression'" ); |
544 | check_mysql_rc(rc, mysql); |
545 | res= mysql_store_result(mysql); |
546 | row= mysql_fetch_row(res); |
547 | FAIL_UNLESS(strcmp(row[1], "ON" ) == 0, "Compression off" ); |
548 | mysql_free_result(res); |
549 | |
550 | mysql_close(mysql); |
551 | return OK; |
552 | } |
553 | |
554 | static int test_reconnect(MYSQL *mysql) |
555 | { |
556 | my_bool my_true= TRUE; |
557 | MYSQL *mysql1; |
558 | int rc; |
559 | my_bool reconnect; |
560 | |
561 | mysql1= mysql_init(NULL); |
562 | FAIL_IF(!mysql1, "not enough memory" ); |
563 | |
564 | mysql_get_option(mysql1, MYSQL_OPT_RECONNECT, &reconnect); |
565 | FAIL_UNLESS(reconnect == 0, "reconnect != 0" ); |
566 | |
567 | rc= mysql_options(mysql1, MYSQL_OPT_RECONNECT, &my_true); |
568 | check_mysql_rc(rc, mysql1); |
569 | |
570 | mysql_get_option(mysql1, MYSQL_OPT_RECONNECT, &reconnect); |
571 | FAIL_UNLESS(reconnect == 1, "reconnect != 1" ); |
572 | |
573 | if (!(my_test_connect(mysql1, hostname, username, |
574 | password, schema, port, |
575 | socketname, 0))) |
576 | { |
577 | diag("connection failed" ); |
578 | mysql_close(mysql); |
579 | return FAIL; |
580 | } |
581 | |
582 | mysql_get_option(mysql1, MYSQL_OPT_RECONNECT, &reconnect); |
583 | FAIL_UNLESS(reconnect == 1, "reconnect != 1" ); |
584 | |
585 | diag("Thread_id before kill: %lu" , mysql_thread_id(mysql1)); |
586 | mysql_kill(mysql, mysql_thread_id(mysql1)); |
587 | |
588 | mysql_ping(mysql1); |
589 | |
590 | rc= mysql_query(mysql1, "SELECT 1 FROM DUAL LIMIT 0" ); |
591 | check_mysql_rc(rc, mysql1); |
592 | diag("Thread_id after kill: %lu" , mysql_thread_id(mysql1)); |
593 | |
594 | mysql_get_option(mysql1, MYSQL_OPT_RECONNECT, &reconnect); |
595 | FAIL_UNLESS(reconnect == 1, "reconnect != 1" ); |
596 | mysql_close(mysql1); |
597 | return OK; |
598 | } |
599 | |
600 | int test_conc21(MYSQL *mysql) |
601 | { |
602 | int rc; |
603 | MYSQL_RES *res= NULL; |
604 | MYSQL_ROW row; |
605 | char tmp[256]; |
606 | unsigned int check_server_version= 0; |
607 | int major=0, minor= 0, patch=0; |
608 | |
609 | rc= mysql_query(mysql, "SELECT @@version" ); |
610 | check_mysql_rc(rc, mysql); |
611 | |
612 | res= mysql_store_result(mysql); |
613 | FAIL_IF(res == NULL, "invalid result set" ); |
614 | |
615 | row= mysql_fetch_row(res); |
616 | strcpy(tmp, row[0]); |
617 | mysql_free_result(res); |
618 | |
619 | sscanf(tmp, "%d.%d.%d" , &major, &minor, &patch); |
620 | |
621 | check_server_version= major * 10000 + minor * 100 + patch; |
622 | |
623 | FAIL_IF(mysql_get_server_version(mysql) != check_server_version, "Numeric server version mismatch" ); |
624 | FAIL_IF(strcmp(mysql_get_server_info(mysql), tmp) != 0, "String server version mismatch" ); |
625 | return OK; |
626 | } |
627 | |
628 | int test_conc26(MYSQL *unused __attribute__((unused))) |
629 | { |
630 | MYSQL *mysql= mysql_init(NULL); |
631 | mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "utf8" ); |
632 | |
633 | FAIL_IF(my_test_connect(mysql, hostname, "notexistinguser" , "password" , schema, port, NULL, CLIENT_REMEMBER_OPTIONS), |
634 | "Error expected" ); |
635 | |
636 | FAIL_IF(!mysql->options.charset_name || strcmp(mysql->options.charset_name, "utf8" ) != 0, |
637 | "expected charsetname=utf8" ); |
638 | mysql_close(mysql); |
639 | |
640 | mysql= mysql_init(NULL); |
641 | FAIL_IF(my_test_connect(mysql, hostname, "notexistinguser" , "password" , schema, port, NULL, 0), |
642 | "Error expected" ); |
643 | FAIL_IF(mysql->options.charset_name, "Error: options not freed" ); |
644 | mysql_close(mysql); |
645 | |
646 | return OK; |
647 | } |
648 | |
649 | int test_connection_timeout(MYSQL *unused __attribute__((unused))) |
650 | { |
651 | unsigned int timeout= 5; |
652 | time_t start, elapsed; |
653 | MYSQL *mysql= mysql_init(NULL); |
654 | mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (unsigned int *)&timeout); |
655 | start= time(NULL); |
656 | if (my_test_connect(mysql, "192.168.1.101" , "notexistinguser" , "password" , schema, port, NULL, CLIENT_REMEMBER_OPTIONS)) |
657 | { |
658 | diag("Error expected - maybe you have to change hostname" ); |
659 | return FAIL; |
660 | } |
661 | elapsed= time(NULL) - start; |
662 | diag("elapsed: %lu" , (unsigned long)elapsed); |
663 | mysql_close(mysql); |
664 | FAIL_IF((unsigned int)elapsed > 2 * timeout, "timeout ignored" ) |
665 | return OK; |
666 | } |
667 | |
668 | int test_connection_timeout2(MYSQL *unused __attribute__((unused))) |
669 | { |
670 | unsigned int timeout= 5; |
671 | time_t start, elapsed; |
672 | MYSQL *mysql= mysql_init(NULL); |
673 | mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (unsigned int *)&timeout); |
674 | mysql_options(mysql, MYSQL_INIT_COMMAND, "set @a:=SLEEP(6)" ); |
675 | start= time(NULL); |
676 | if (my_test_connect(mysql, hostname, username, password, schema, port, NULL, CLIENT_REMEMBER_OPTIONS)) |
677 | { |
678 | diag("timeout error expected" ); |
679 | return FAIL; |
680 | } |
681 | elapsed= time(NULL) - start; |
682 | diag("elapsed: %lu" , (unsigned long)elapsed); |
683 | mysql_close(mysql); |
684 | FAIL_IF((unsigned int)elapsed > 2 * timeout, "timeout ignored" ) |
685 | return OK; |
686 | } |
687 | |
688 | int test_connection_timeout3(MYSQL *unused __attribute__((unused))) |
689 | { |
690 | unsigned int timeout= 5; |
691 | unsigned int read_write_timeout= 10; |
692 | int rc; |
693 | time_t start, elapsed; |
694 | MYSQL *mysql= mysql_init(NULL); |
695 | mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (unsigned int *)&timeout); |
696 | mysql_options(mysql, MYSQL_OPT_READ_TIMEOUT, (unsigned int *)&read_write_timeout); |
697 | mysql_options(mysql, MYSQL_OPT_WRITE_TIMEOUT, (unsigned int *)&read_write_timeout); |
698 | mysql_options(mysql, MYSQL_INIT_COMMAND, "set @a:=SLEEP(6)" ); |
699 | start= time(NULL); |
700 | if (my_test_connect(mysql, hostname, username, password, schema, port, socketname, CLIENT_REMEMBER_OPTIONS)) |
701 | { |
702 | diag("timeout error expected" ); |
703 | elapsed= time(NULL) - start; |
704 | diag("elapsed: %lu" , (unsigned long)elapsed); |
705 | return FAIL; |
706 | } |
707 | elapsed= time(NULL) - start; |
708 | diag("elapsed: %lu" , (unsigned long)elapsed); |
709 | FAIL_IF((unsigned int)elapsed > timeout + 1, "timeout ignored" ) |
710 | |
711 | mysql_close(mysql); |
712 | mysql= mysql_init(NULL); |
713 | mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (unsigned int *)&timeout); |
714 | mysql_options(mysql, MYSQL_OPT_READ_TIMEOUT, (unsigned int *)&read_write_timeout); |
715 | mysql_options(mysql, MYSQL_OPT_WRITE_TIMEOUT, (unsigned int *)&read_write_timeout); |
716 | |
717 | if (!my_test_connect(mysql, hostname, username, password, schema, port, socketname, CLIENT_REMEMBER_OPTIONS)) |
718 | { |
719 | diag("Error: %s" , mysql_error(mysql)); |
720 | return FAIL; |
721 | } |
722 | |
723 | start= time(NULL); |
724 | rc= mysql_query(mysql, "SET @a:=SLEEP(12)" ); |
725 | elapsed= time(NULL) - start; |
726 | diag("elapsed: %lu" , (unsigned long)elapsed); |
727 | FAIL_IF(!rc, "timeout expected" ); |
728 | mysql_close(mysql); |
729 | return OK; |
730 | } |
731 | |
732 | |
733 | /* test should run with valgrind */ |
734 | static int test_conc118(MYSQL *mysql) |
735 | { |
736 | int rc; |
737 | my_bool reconnect= 1; |
738 | |
739 | mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
740 | |
741 | mysql->options.unused_1= 1; |
742 | |
743 | rc= mysql_kill(mysql, mysql_thread_id(mysql)); |
744 | |
745 | mysql_ping(mysql); |
746 | |
747 | rc= mysql_query(mysql, "SET @a:=1" ); |
748 | check_mysql_rc(rc, mysql); |
749 | |
750 | FAIL_IF(mysql->options.unused_1 != 1, "options got lost" ); |
751 | |
752 | rc= mysql_kill(mysql, mysql_thread_id(mysql)); |
753 | |
754 | mysql_ping(mysql); |
755 | rc= mysql_query(mysql, "SET @a:=1" ); |
756 | check_mysql_rc(rc, mysql); |
757 | |
758 | return OK; |
759 | } |
760 | |
761 | static int test_wrong_bind_address(MYSQL *unused __attribute__((unused))) |
762 | { |
763 | const char *bind_addr= "100.188.111.112" ; |
764 | MYSQL *mysql; |
765 | |
766 | if (!hostname || !strcmp(hostname, "localhost" )) |
767 | { |
768 | diag("test doesn't work with unix sockets" ); |
769 | return SKIP; |
770 | } |
771 | |
772 | mysql= mysql_init(NULL); |
773 | |
774 | mysql_options(mysql, MYSQL_OPT_BIND, bind_addr); |
775 | if (my_test_connect(mysql, hostname, username, |
776 | password, schema, port, socketname, 0)) |
777 | { |
778 | diag("Error expected" ); |
779 | mysql_close(mysql); |
780 | return FAIL; |
781 | } |
782 | diag("Error: %s" , mysql_error(mysql)); |
783 | mysql_close(mysql); |
784 | return OK; |
785 | } |
786 | |
787 | static int test_bind_address(MYSQL *my) |
788 | { |
789 | MYSQL *mysql; |
790 | char *bind_addr= getenv("MYSQL_TEST_BINDADDR" ); |
791 | char query[128]; |
792 | int rc; |
793 | |
794 | if (!hostname || !strcmp(hostname, "localhost" )) |
795 | { |
796 | diag("test doesn't work with unix sockets" ); |
797 | return SKIP; |
798 | } |
799 | |
800 | sprintf(query, "DROP USER '%s'@'%s'" , username, bind_addr); |
801 | rc= mysql_query(my, query); |
802 | |
803 | sprintf(query, "CREATE USER '%s'@'%s'" , username, bind_addr); |
804 | rc= mysql_query(my, query); |
805 | check_mysql_rc(rc, my); |
806 | |
807 | sprintf(query, "GRANT ALL ON %s.* TO '%s'@'%s'" , schema, username, bind_addr); |
808 | rc= mysql_query(my, query); |
809 | check_mysql_rc(rc, my); |
810 | |
811 | if (!bind_addr) |
812 | { |
813 | diag("No bind address specified" ); |
814 | return SKIP; |
815 | } |
816 | |
817 | mysql= mysql_init(NULL); |
818 | mysql_options(mysql, MYSQL_OPT_BIND, bind_addr); |
819 | |
820 | if (!my_test_connect(mysql, bind_addr, username, |
821 | password, schema, port, socketname, 0)) |
822 | { |
823 | diag("Error: %s\n" , mysql_error(mysql)); |
824 | mysql_close(mysql); |
825 | return FAIL; |
826 | } |
827 | diag("%s" , mysql_get_host_info(mysql)); |
828 | mysql_close(mysql); |
829 | return OK; |
830 | } |
831 | |
832 | static int test_get_options(MYSQL *unused __attribute__((unused))) |
833 | { |
834 | MYSQL *mysql= mysql_init(NULL); |
835 | int options_int[]= {MYSQL_OPT_CONNECT_TIMEOUT, MYSQL_OPT_LOCAL_INFILE, |
836 | MYSQL_OPT_PROTOCOL, MYSQL_OPT_READ_TIMEOUT, MYSQL_OPT_WRITE_TIMEOUT, 0}; |
837 | my_bool options_bool[]= {MYSQL_OPT_RECONNECT, MYSQL_REPORT_DATA_TRUNCATION, |
838 | MYSQL_OPT_COMPRESS, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, MYSQL_SECURE_AUTH, |
839 | #ifdef _WIN32 |
840 | MYSQL_OPT_NAMED_PIPE, |
841 | #endif |
842 | 0}; |
843 | int options_char[]= {MYSQL_READ_DEFAULT_FILE, MYSQL_READ_DEFAULT_GROUP, MYSQL_SET_CHARSET_NAME, |
844 | MYSQL_OPT_SSL_KEY, MYSQL_OPT_SSL_CA, MYSQL_OPT_SSL_CERT, MYSQL_OPT_SSL_CAPATH, |
845 | MYSQL_OPT_SSL_CIPHER, MYSQL_OPT_BIND, MARIADB_OPT_SSL_FP, MARIADB_OPT_SSL_FP_LIST, |
846 | MARIADB_OPT_TLS_PASSPHRASE, 0}; |
847 | |
848 | const char *init_command[3]= {"SET @a:=1" , "SET @b:=2" , "SET @c:=3" }; |
849 | int elements= 0; |
850 | char **command; |
851 | |
852 | |
853 | int intval[2]= {1, 0}; |
854 | my_bool boolval[2]= {1, 0}; |
855 | const char *char1= "test" ; |
856 | char *char2; |
857 | int i; |
858 | MYSQL *userdata; |
859 | const char *attr_key[] = {"foo1" , "foo2" , "foo3" }; |
860 | const char *attr_val[] = {"bar1" , "bar2" , "bar3" }; |
861 | char **key, **val; |
862 | |
863 | for (i=0; options_int[i]; i++) |
864 | { |
865 | mysql_options(mysql, options_int[i], &intval[0]); |
866 | intval[1]= 0; |
867 | mysql_get_optionv(mysql, options_int[i], &intval[1]); |
868 | FAIL_IF(intval[0] != intval[1], "mysql_get_optionv (int) failed" ); |
869 | } |
870 | for (i=0; options_bool[i]; i++) |
871 | { |
872 | mysql_options(mysql, options_bool[i], &boolval[0]); |
873 | intval[1]= 0; |
874 | mysql_get_optionv(mysql, options_bool[i], &boolval[1]); |
875 | FAIL_IF(boolval[0] != boolval[1], "mysql_get_optionv (my_bool) failed" ); |
876 | } |
877 | for (i=0; options_char[i]; i++) |
878 | { |
879 | mysql_options(mysql, options_char[i], char1); |
880 | char2= NULL; |
881 | mysql_get_optionv(mysql, options_char[i], (void *)&char2); |
882 | if (options_char[i] != MYSQL_SET_CHARSET_NAME) |
883 | FAIL_IF(strcmp(char1, char2), "mysql_get_optionv (char) failed" ); |
884 | } |
885 | |
886 | for (i=0; i < 3; i++) |
887 | mysql_options(mysql, MYSQL_INIT_COMMAND, init_command[i]); |
888 | |
889 | mysql_get_optionv(mysql, MYSQL_INIT_COMMAND, &command, &elements); |
890 | FAIL_IF(elements != 3, "expected 3 elements" ); |
891 | for (i=0; i < 3; i++) |
892 | FAIL_IF(strcmp(init_command[i], command[i]), "wrong init command" ); |
893 | for (i=0; i < 3; i++) |
894 | mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, attr_key[i], attr_val[i]); |
895 | |
896 | mysql_get_optionv(mysql, MYSQL_OPT_CONNECT_ATTRS, NULL, NULL, &elements); |
897 | FAIL_IF(elements != 3, "expected 3 connection attributes" ); |
898 | |
899 | key= (char **)malloc(sizeof(char *) * elements); |
900 | val= (char **)malloc(sizeof(char *) * elements); |
901 | |
902 | mysql_get_optionv(mysql, MYSQL_OPT_CONNECT_ATTRS, &key, &val, &elements); |
903 | for (i=0; i < elements; i++) |
904 | { |
905 | diag("%s => %s" , key[i], val[i]); |
906 | } |
907 | |
908 | free(key); |
909 | free(val); |
910 | |
911 | mysql_optionsv(mysql, MARIADB_OPT_USERDATA, "my_app" , (void *)mysql); |
912 | mysql_get_optionv(mysql, MARIADB_OPT_USERDATA, (char *)"my_app" , &userdata); |
913 | |
914 | FAIL_IF(mysql != userdata, "wrong userdata" ); |
915 | mysql_close(mysql); |
916 | return OK; |
917 | } |
918 | |
919 | static int test_sess_track_db(MYSQL *mysql) |
920 | { |
921 | int rc; |
922 | const char *data; |
923 | size_t len; |
924 | |
925 | diag("session tracking not fully supported yet in 10.2" ); |
926 | return SKIP; |
927 | |
928 | if (!(mysql->server_capabilities & CLIENT_SESSION_TRACKING)) |
929 | { |
930 | diag("Server doesn't support session tracking (cap=%lu)" , mysql->server_capabilities); |
931 | return SKIP; |
932 | } |
933 | |
934 | rc= mysql_query(mysql, "USE mysql" ); |
935 | check_mysql_rc(rc, mysql); |
936 | FAIL_IF(strcmp(mysql->db, "mysql" ), "Expected new schema 'mysql'" ); |
937 | |
938 | FAIL_IF(mysql_session_track_get_first(mysql, SESSION_TRACK_SCHEMA, &data, &len), |
939 | "session_track_get_first failed" ); |
940 | FAIL_IF(strncmp(data, "mysql" , len), "Expected new schema 'mysql'" ); |
941 | |
942 | rc= mysql_query(mysql, "USE test" ); |
943 | check_mysql_rc(rc, mysql); |
944 | FAIL_IF(strcmp(mysql->db, "test" ), "Expected new schema 'test'" ); |
945 | |
946 | FAIL_IF(mysql_session_track_get_first(mysql, SESSION_TRACK_SCHEMA, &data, &len), |
947 | "session_track_get_first failed" ); |
948 | FAIL_IF(strncmp(data, "test" , len), "Expected new schema 'test'" ); |
949 | |
950 | diag("charset: %s" , mysql->charset->csname); |
951 | rc= mysql_query(mysql, "SET NAMES utf8" ); |
952 | check_mysql_rc(rc, mysql); |
953 | if (!mysql_session_track_get_first(mysql, SESSION_TRACK_SYSTEM_VARIABLES, &data, &len)) |
954 | do { |
955 | printf("# SESSION_TRACK_VARIABLES: %*.*s\n" , (int)len, (int)len, data); |
956 | } while (!mysql_session_track_get_next(mysql, SESSION_TRACK_SYSTEM_VARIABLES, &data, &len)); |
957 | diag("charset: %s" , mysql->charset->csname); |
958 | FAIL_IF(strcmp(mysql->charset->csname, "utf8" ), "Expected charset 'utf8'" ); |
959 | |
960 | rc= mysql_query(mysql, "SET NAMES latin1" ); |
961 | check_mysql_rc(rc, mysql); |
962 | FAIL_IF(strcmp(mysql->charset->csname, "latin1" ), "Expected charset 'latin1'" ); |
963 | |
964 | rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1" ); |
965 | check_mysql_rc(rc, mysql); |
966 | |
967 | rc= mysql_query(mysql, "CREATE PROCEDURE p1() " |
968 | "BEGIN " |
969 | "SET @@autocommit=0; " |
970 | "SET NAMES utf8; " |
971 | "SET session auto_increment_increment=2; " |
972 | "END " ); |
973 | check_mysql_rc(rc, mysql); |
974 | |
975 | rc= mysql_query(mysql, "CALL p1()" ); |
976 | check_mysql_rc(rc, mysql); |
977 | |
978 | if (!mysql_session_track_get_first(mysql, SESSION_TRACK_SYSTEM_VARIABLES, &data, &len)) |
979 | do { |
980 | printf("# SESSION_TRACK_VARIABLES: %*.*s\n" , (int)len, (int)len, data); |
981 | } while (!mysql_session_track_get_next(mysql, SESSION_TRACK_SYSTEM_VARIABLES, &data, &len)); |
982 | |
983 | return OK; |
984 | } |
985 | |
986 | |
987 | static int test_unix_socket_close(MYSQL *unused __attribute__((unused))) |
988 | { |
989 | #ifdef _WIN32 |
990 | diag("test does not run on Windows" ); |
991 | return SKIP; |
992 | #else |
993 | MYSQL *mysql= mysql_init(NULL); |
994 | FILE *fp; |
995 | int i; |
996 | |
997 | if (!(fp= fopen("./dummy_sock" , "w" ))) |
998 | { |
999 | diag("couldn't create dummy socket" ); |
1000 | return FAIL; |
1001 | } |
1002 | fclose(fp); |
1003 | |
1004 | for (i=0; i < 10000; i++) |
1005 | { |
1006 | mysql_real_connect(mysql, "localhost" , "user" , "passwd" , NULL, 0, "./dummy_sock" , 0); |
1007 | /* check if we run out of sockets */ |
1008 | if (mysql_errno(mysql) == 2001) |
1009 | { |
1010 | diag("out of sockets after %d attempts" , i); |
1011 | mysql_close(mysql); |
1012 | return FAIL; |
1013 | } |
1014 | } |
1015 | mysql_close(mysql); |
1016 | return OK; |
1017 | #endif |
1018 | } |
1019 | |
1020 | |
1021 | static int test_reset(MYSQL *mysql) |
1022 | { |
1023 | int rc; |
1024 | MYSQL_RES *res; |
1025 | |
1026 | if (mysql_get_server_version(mysql) < 100200) |
1027 | return SKIP; |
1028 | |
1029 | rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1" ); |
1030 | check_mysql_rc(rc, mysql); |
1031 | |
1032 | rc= mysql_query(mysql, "CREATE TABLE t1 (a int)" ); |
1033 | check_mysql_rc(rc, mysql); |
1034 | |
1035 | rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1),(2),(3)" ); |
1036 | check_mysql_rc(rc, mysql); |
1037 | |
1038 | FAIL_IF(mysql_affected_rows(mysql) != 3, "Expected 3 rows" ); |
1039 | |
1040 | rc= mysql_reset_connection(mysql); |
1041 | check_mysql_rc(rc, mysql); |
1042 | |
1043 | FAIL_IF(mysql_affected_rows(mysql) != ~(my_ulonglong)0, "Expected 0 rows" ); |
1044 | |
1045 | rc= mysql_query(mysql, "SELECT a FROM t1" ); |
1046 | check_mysql_rc(rc, mysql); |
1047 | |
1048 | rc= mysql_query(mysql, "SELECT 1 FROM DUAL" ); |
1049 | FAIL_IF(!rc, "Error expected" ); |
1050 | |
1051 | rc= mysql_reset_connection(mysql); |
1052 | check_mysql_rc(rc, mysql); |
1053 | |
1054 | res= mysql_store_result(mysql); |
1055 | FAIL_IF(res, "expected no result" ); |
1056 | |
1057 | rc= mysql_query(mysql, "SELECT a FROM t1" ); |
1058 | check_mysql_rc(rc, mysql); |
1059 | |
1060 | res= mysql_use_result(mysql); |
1061 | FAIL_IF(!res, "expected result" ); |
1062 | |
1063 | rc= mysql_reset_connection(mysql); |
1064 | check_mysql_rc(rc, mysql); |
1065 | |
1066 | FAIL_IF(mysql_fetch_row(res), "expected error" ); |
1067 | |
1068 | mysql_free_result(res); |
1069 | |
1070 | rc= mysql_query(mysql, "DROP TABLE t1" ); |
1071 | check_mysql_rc(rc, mysql); |
1072 | |
1073 | return OK; |
1074 | } |
1075 | |
1076 | static int test_auth256(MYSQL *my) |
1077 | { |
1078 | MYSQL *mysql= mysql_init(NULL); |
1079 | int rc; |
1080 | MYSQL_RES *res; |
1081 | my_ulonglong num_rows= 0; |
1082 | char query[1024]; |
1083 | |
1084 | if (!mysql_client_find_plugin(mysql, "sha256_password" , 3)) |
1085 | { |
1086 | diag("sha256_password plugin not available" ); |
1087 | return SKIP; |
1088 | } |
1089 | |
1090 | rc= mysql_query(my, "SELECT * FROM information_schema.plugins where plugin_name='sha256_password'" ); |
1091 | check_mysql_rc(rc, mysql); |
1092 | |
1093 | res= mysql_store_result(my); |
1094 | num_rows= mysql_num_rows(res); |
1095 | mysql_free_result(res); |
1096 | |
1097 | if (!num_rows) |
1098 | { |
1099 | diag("server doesn't support sha256 authentication" ); |
1100 | return SKIP; |
1101 | } |
1102 | |
1103 | rc= mysql_query(my, "DROP USER IF EXISTS sha256user@localhost" ); |
1104 | check_mysql_rc(rc, mysql); |
1105 | |
1106 | sprintf(query, "CREATE user 'sha256user'@'%s' identified with sha256_password by 'foo'" , this_host); |
1107 | rc= mysql_query(my, query); |
1108 | check_mysql_rc(rc, my); |
1109 | |
1110 | if (!mysql_real_connect(mysql, hostname, "sha256user" , "foo" , NULL, port, socketname, 0)) |
1111 | { |
1112 | diag("error: %s" , mysql_error(mysql)); |
1113 | mysql_close(mysql); |
1114 | return FAIL; |
1115 | } |
1116 | mysql_close(mysql); |
1117 | |
1118 | mysql= mysql_init(NULL); |
1119 | mysql_options(mysql, MYSQL_SERVER_PUBLIC_KEY, "rsa_public_key.pem" ); |
1120 | if (!mysql_real_connect(mysql, hostname, "sha256user" , "foo" , NULL, port, socketname, 0)) |
1121 | { |
1122 | diag("error: %s" , mysql_error(mysql)); |
1123 | mysql_close(mysql); |
1124 | return FAIL; |
1125 | } |
1126 | mysql_close(mysql); |
1127 | sprintf(query, "DROP USER 'sha256user'@'%s'" , this_host); |
1128 | rc= mysql_query(my, query); |
1129 | check_mysql_rc(rc, mysql); |
1130 | return OK; |
1131 | } |
1132 | |
1133 | static int test_mdev13100(MYSQL *my __attribute__((unused))) |
1134 | { |
1135 | MYSQL *mysql= mysql_init(NULL); |
1136 | int rc; |
1137 | FILE *fp; |
1138 | |
1139 | if (!(fp= fopen("./mdev13100.cnf" , "w" ))) |
1140 | return FAIL; |
1141 | |
1142 | /* [client] group only */ |
1143 | fprintf(fp, "[client]\n" ); |
1144 | fprintf(fp, "default-character-set=latin2\n" ); |
1145 | |
1146 | fclose(fp); |
1147 | |
1148 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "./mdev13100.cnf" ); |
1149 | check_mysql_rc(rc, mysql); |
1150 | |
1151 | if (!my_test_connect(mysql, hostname, username, |
1152 | password, schema, port, socketname, 0)) |
1153 | { |
1154 | diag("Error: %s" , mysql_error(mysql)); |
1155 | return FAIL; |
1156 | } |
1157 | FAIL_IF(strcmp("latin2" , mysql_character_set_name(mysql)), "Expected charset latin2" ); |
1158 | mysql_close(mysql); |
1159 | |
1160 | /* value from client-mariadb group */ |
1161 | mysql= mysql_init(NULL); |
1162 | if (!(fp= fopen("./mdev13100.cnf" , "w" ))) |
1163 | return FAIL; |
1164 | |
1165 | fprintf(fp, "[client]\n" ); |
1166 | fprintf(fp, "default-character-set=latin1\n" ); |
1167 | fprintf(fp, "[client-server]\n" ); |
1168 | fprintf(fp, "default-character-set=latin2\n" ); |
1169 | |
1170 | fclose(fp); |
1171 | |
1172 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "./mdev13100.cnf" ); |
1173 | check_mysql_rc(rc, mysql); |
1174 | |
1175 | if (!my_test_connect(mysql, hostname, username, |
1176 | password, schema, port, socketname, 0)) |
1177 | { |
1178 | diag("Error: %s" , mysql_error(mysql)); |
1179 | return FAIL; |
1180 | } |
1181 | FAIL_IF(strcmp("latin2" , mysql_character_set_name(mysql)), "Expected charset latin2" ); |
1182 | mysql_close(mysql); |
1183 | |
1184 | /* values from client-mariadb group */ |
1185 | mysql= mysql_init(NULL); |
1186 | |
1187 | if (!(fp= fopen("./mdev13100.cnf" , "w" ))) |
1188 | return FAIL; |
1189 | |
1190 | fprintf(fp, "[client]\n" ); |
1191 | fprintf(fp, "default-character-set=latin1\n" ); |
1192 | fprintf(fp, "[client-server]\n" ); |
1193 | fprintf(fp, "default-character-set=utf8\n" ); |
1194 | fprintf(fp, "[client-mariadb]\n" ); |
1195 | fprintf(fp, "default-character-set=latin2\n" ); |
1196 | |
1197 | fclose(fp); |
1198 | |
1199 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "./mdev13100.cnf" ); |
1200 | check_mysql_rc(rc, mysql); |
1201 | |
1202 | if (!my_test_connect(mysql, hostname, username, |
1203 | password, schema, port, socketname, 0)) |
1204 | { |
1205 | diag("Error: %s" , mysql_error(mysql)); |
1206 | return FAIL; |
1207 | } |
1208 | FAIL_IF(strcmp("latin2" , mysql_character_set_name(mysql)), "Expected charset latin2" ); |
1209 | mysql_close(mysql); |
1210 | |
1211 | /* values from mdev-13100 group */ |
1212 | mysql= mysql_init(NULL); |
1213 | if (!(fp= fopen("./mdev13100.cnf" , "w" ))) |
1214 | return FAIL; |
1215 | |
1216 | fprintf(fp, "[client]\n" ); |
1217 | fprintf(fp, "default-character-set=latin1\n" ); |
1218 | fprintf(fp, "[client-server]\n" ); |
1219 | fprintf(fp, "default-character-set=latin1\n" ); |
1220 | fprintf(fp, "[client-mariadb]\n" ); |
1221 | fprintf(fp, "default-character-set=utf8\n" ); |
1222 | fprintf(fp, "[mdev13100]\n" ); |
1223 | fprintf(fp, "default-character-set=latin2\n" ); |
1224 | |
1225 | fclose(fp); |
1226 | |
1227 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "./mdev13100.cnf" ); |
1228 | check_mysql_rc(rc, mysql); |
1229 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "mdev13100" ); |
1230 | check_mysql_rc(rc, mysql); |
1231 | |
1232 | if (!my_test_connect(mysql, hostname, username, |
1233 | password, schema, port, socketname, 0)) |
1234 | { |
1235 | diag("Error: %s" , mysql_error(mysql)); |
1236 | return FAIL; |
1237 | } |
1238 | FAIL_IF(strcmp("latin2" , mysql_character_set_name(mysql)), "Expected charset latin2" ); |
1239 | mysql_close(mysql); |
1240 | |
1241 | /* values from [programname] group */ |
1242 | mysql= mysql_init(NULL); |
1243 | if (!(fp= fopen("./mdev13100.cnf" , "w" ))) |
1244 | return FAIL; |
1245 | |
1246 | fprintf(fp, "[client]\n" ); |
1247 | fprintf(fp, "default-character-set=utf8\n" ); |
1248 | fprintf(fp, "[client-server]\n" ); |
1249 | fprintf(fp, "default-character-set=utf8\n" ); |
1250 | fprintf(fp, "[client-mariadb]\n" ); |
1251 | fprintf(fp, "default-character-set=latin2\n" ); |
1252 | |
1253 | fclose(fp); |
1254 | |
1255 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "./mdev13100.cnf" ); |
1256 | check_mysql_rc(rc, mysql); |
1257 | rc= mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "" ); |
1258 | check_mysql_rc(rc, mysql); |
1259 | |
1260 | if (!my_test_connect(mysql, hostname, username, |
1261 | password, schema, port, socketname, 0)) |
1262 | { |
1263 | diag("Error: %s" , mysql_error(mysql)); |
1264 | return FAIL; |
1265 | } |
1266 | diag("character set: %s" , mysql_character_set_name(mysql)); |
1267 | FAIL_IF(strcmp("latin2" , mysql_character_set_name(mysql)), "Expected charset latin2" ); |
1268 | mysql_close(mysql); |
1269 | |
1270 | remove("./mdev13100.cnf" ); |
1271 | |
1272 | return OK; |
1273 | } |
1274 | |
1275 | static int test_conc276(MYSQL *unused __attribute__((unused))) |
1276 | { |
1277 | MYSQL *mysql= mysql_init(NULL); |
1278 | int rc; |
1279 | my_bool val= 1; |
1280 | |
1281 | mysql_options(mysql, MYSQL_OPT_SSL_ENFORCE, &val); |
1282 | mysql_options(mysql, MYSQL_OPT_RECONNECT, &val); |
1283 | |
1284 | if (!mysql_real_connect(mysql, hostname, username, password, schema, port, socketname, 0)) |
1285 | { |
1286 | diag("Connection failed. Error: %s" , mysql_error(mysql)); |
1287 | mysql_close(mysql); |
1288 | return FAIL; |
1289 | } |
1290 | diag("Cipher in use: %s" , mysql_get_ssl_cipher(mysql)); |
1291 | |
1292 | rc= mariadb_reconnect(mysql); |
1293 | check_mysql_rc(rc, mysql); |
1294 | |
1295 | diag("Cipher in use: %s" , mysql_get_ssl_cipher(mysql)); |
1296 | /* this shouldn't crash anymore */ |
1297 | rc= mysql_query(mysql, "SET @a:=1" ); |
1298 | check_mysql_rc(rc, mysql); |
1299 | |
1300 | mysql_close(mysql); |
1301 | return OK; |
1302 | } |
1303 | |
1304 | static int test_expired_pw(MYSQL *my) |
1305 | { |
1306 | MYSQL *mysql; |
1307 | int rc; |
1308 | char query[512]; |
1309 | unsigned char expire= 1; |
1310 | |
1311 | if (mariadb_connection(my) || |
1312 | !(my->server_capabilities & CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS)) |
1313 | { |
1314 | diag("Server doesn't support password expiration" ); |
1315 | return SKIP; |
1316 | } |
1317 | sprintf(query, "DROP USER 'foo'@'%s'" , this_host); |
1318 | rc= mysql_query(my, query); |
1319 | |
1320 | sprintf(query, "CREATE USER 'foo'@'%s' IDENTIFIED BY 'foo'" , this_host); |
1321 | rc= mysql_query(my, query); |
1322 | check_mysql_rc(rc, my); |
1323 | |
1324 | sprintf(query, "GRANT ALL ON *.* TO 'foo'@'%s'" , this_host); |
1325 | rc= mysql_query(my, query); |
1326 | check_mysql_rc(rc, my); |
1327 | |
1328 | sprintf(query, "ALTER USER 'foo'@'%s' PASSWORD EXPIRE" , this_host); |
1329 | rc= mysql_query(my, query); |
1330 | check_mysql_rc(rc, my); |
1331 | |
1332 | mysql= mysql_init(NULL); |
1333 | |
1334 | my_test_connect(mysql, hostname, "foo" , "foo" , schema, |
1335 | port, socketname, 0); |
1336 | |
1337 | FAIL_IF(!mysql_errno(mysql), "Error expected" ); |
1338 | mysql_close(mysql); |
1339 | |
1340 | mysql= mysql_init(NULL); |
1341 | mysql_optionsv(mysql, MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, &expire); |
1342 | |
1343 | my_test_connect(mysql, hostname, "foo" , "foo" , schema, |
1344 | port, socketname, 0); |
1345 | |
1346 | diag("error: %d %s" , mysql_errno(mysql), mysql_error(mysql)); |
1347 | FAIL_IF(mysql_errno(mysql) != ER_MUST_CHANGE_PASSWORD && |
1348 | mysql_errno(mysql) != ER_MUST_CHANGE_PASSWORD_LOGIN, "Error 1820/1862 expected" ); |
1349 | |
1350 | mysql_close(mysql); |
1351 | |
1352 | sprintf(query, "DROP USER 'foo'@'%s'" , this_host); |
1353 | rc= mysql_query(my, query); |
1354 | check_mysql_rc(rc, my); |
1355 | |
1356 | return OK; |
1357 | } |
1358 | |
1359 | static int test_conc315(MYSQL *mysql) |
1360 | { |
1361 | int rc; |
1362 | const char *csname; |
1363 | |
1364 | if (!is_mariadb) |
1365 | return SKIP; |
1366 | |
1367 | mysql_get_optionv(mysql, MYSQL_SET_CHARSET_NAME, (void *)&csname); |
1368 | FAIL_UNLESS(strcmp(csname, "utf8" ) == 0, "Wrong default character set" ); |
1369 | |
1370 | rc= mysql_change_user(mysql, username, password, schema); |
1371 | check_mysql_rc(rc, mysql); |
1372 | mysql_get_optionv(mysql, MYSQL_SET_CHARSET_NAME, (void *)&csname); |
1373 | FAIL_UNLESS(strcmp(csname, "utf8" ) == 0, "Wrong default character set" ); |
1374 | return OK; |
1375 | } |
1376 | #ifndef WIN32 |
1377 | static int test_conc317(MYSQL *unused __attribute__((unused))) |
1378 | { |
1379 | MYSQL *mysql; |
1380 | my_bool reconnect = 0; |
1381 | FILE *fp= NULL; |
1382 | const char *env= getenv("MYSQL_TMP_DIR" ); |
1383 | char cnf_file1[FN_REFLEN + 1]; |
1384 | |
1385 | if (travis_test) |
1386 | return SKIP; |
1387 | |
1388 | if (!env) |
1389 | env= "/tmp" ; |
1390 | |
1391 | setenv("HOME" , env, 1); |
1392 | |
1393 | snprintf(cnf_file1, FN_REFLEN, "%s%c.my.cnf" , env, FN_LIBCHAR); |
1394 | |
1395 | FAIL_IF(!access(cnf_file1, R_OK), "access" ); |
1396 | |
1397 | mysql= mysql_init(NULL); |
1398 | fp= fopen(cnf_file1, "w" ); |
1399 | FAIL_IF(!fp, "fopen" ); |
1400 | |
1401 | fprintf(fp, "[client]\ndefault-character-set = latin2\nreconnect= 1\n" ); |
1402 | fclose(fp); |
1403 | |
1404 | mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "" ); |
1405 | my_test_connect(mysql, hostname, username, password, |
1406 | schema, 0, socketname, 0); |
1407 | |
1408 | remove(cnf_file1); |
1409 | |
1410 | FAIL_IF(strcmp(mysql_character_set_name(mysql), "latin2" ), "expected charset latin2" ); |
1411 | mysql_get_optionv(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
1412 | FAIL_IF(reconnect != 1, "expected reconnect=1" ); |
1413 | mysql_close(mysql); |
1414 | return OK; |
1415 | } |
1416 | |
1417 | static int test_conc327(MYSQL *unused __attribute__((unused))) |
1418 | { |
1419 | MYSQL *mysql; |
1420 | my_bool reconnect = 0; |
1421 | FILE *fp1= NULL, *fp2= NULL; |
1422 | const char *env= getenv("MYSQL_TMP_DIR" ); |
1423 | char cnf_file1[FN_REFLEN + 1]; |
1424 | char cnf_file2[FN_REFLEN + 1]; |
1425 | |
1426 | if (travis_test) |
1427 | return SKIP; |
1428 | |
1429 | if (!env) |
1430 | env= "/tmp" ; |
1431 | |
1432 | setenv("HOME" , env, 1); |
1433 | |
1434 | snprintf(cnf_file1, FN_REFLEN, "%s%c.my.cnf" , env, FN_LIBCHAR); |
1435 | snprintf(cnf_file2, FN_REFLEN, "%s%c.my.tmp" , env, FN_LIBCHAR); |
1436 | |
1437 | FAIL_IF(!access(cnf_file1, R_OK), "access" ); |
1438 | |
1439 | fp1= fopen(cnf_file1, "w" ); |
1440 | fp2= fopen(cnf_file2, "w" ); |
1441 | FAIL_IF(!fp1 || !fp2, "fopen failed" ); |
1442 | |
1443 | fprintf(fp1, "!include %s\n" , cnf_file2); |
1444 | |
1445 | fprintf(fp2, "[client]\ndefault-character-set = latin2\nreconnect= 1\n" ); |
1446 | fclose(fp1); |
1447 | fclose(fp2); |
1448 | |
1449 | mysql= mysql_init(NULL); |
1450 | mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "" ); |
1451 | my_test_connect(mysql, hostname, username, password, |
1452 | schema, 0, socketname, 0); |
1453 | |
1454 | remove(cnf_file1); |
1455 | remove(cnf_file2); |
1456 | |
1457 | FAIL_IF(strcmp(mysql_character_set_name(mysql), "latin2" ), "expected charset latin2" ); |
1458 | mysql_get_optionv(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
1459 | FAIL_IF(reconnect != 1, "expected reconnect=1" ); |
1460 | mysql_close(mysql); |
1461 | |
1462 | snprintf(cnf_file1, FN_REFLEN, "%s%cmy.cnf" , env, FN_LIBCHAR); |
1463 | fp1= fopen(cnf_file1, "w" ); |
1464 | fp2= fopen(cnf_file2, "w" ); |
1465 | FAIL_IF(!fp1 || !fp2, "fopen failed" ); |
1466 | |
1467 | fprintf(fp2, "!includedir %s\n" , env); |
1468 | |
1469 | fprintf(fp1, "[client]\ndefault-character-set = latin2\nreconnect= 1\n" ); |
1470 | fclose(fp1); |
1471 | fclose(fp2); |
1472 | mysql= mysql_init(NULL); |
1473 | mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, cnf_file2); |
1474 | my_test_connect(mysql, hostname, username, password, |
1475 | schema, 0, socketname, 0); |
1476 | |
1477 | remove(cnf_file1); |
1478 | remove(cnf_file2); |
1479 | |
1480 | FAIL_IF(strcmp(mysql_character_set_name(mysql), "latin2" ), "expected charset latin2" ); |
1481 | mysql_get_optionv(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
1482 | FAIL_IF(reconnect != 1, "expected reconnect=1" ); |
1483 | mysql_close(mysql); |
1484 | |
1485 | return OK; |
1486 | } |
1487 | #endif |
1488 | |
1489 | struct my_tests_st my_tests[] = { |
1490 | #ifndef WIN32 |
1491 | {"test_conc327" , test_conc327, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1492 | {"test_conc317" , test_conc317, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1493 | #endif |
1494 | {"test_conc315" , test_conc315, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1495 | {"test_expired_pw" , test_expired_pw, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1496 | {"test_conc276" , test_conc276, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
1497 | {"test_mdev13100" , test_mdev13100, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1498 | {"test_auth256" , test_auth256, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1499 | {"test_reset" , test_reset, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1500 | {"test_unix_socket_close" , test_unix_socket_close, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
1501 | {"test_sess_track_db" , test_sess_track_db, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1502 | {"test_get_options" , test_get_options, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1503 | {"test_wrong_bind_address" , test_wrong_bind_address, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1504 | {"test_bind_address" , test_bind_address, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1505 | {"test_conc118" , test_conc118, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1506 | {"test_conc66" , test_conc66, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1507 | {"test_bug20023" , test_bug20023, TEST_CONNECTION_NEW, 0, NULL, NULL}, |
1508 | {"test_bug31669" , test_bug31669, TEST_CONNECTION_NEW, 0, NULL, NULL}, |
1509 | {"test_bug33831" , test_bug33831, TEST_CONNECTION_NEW, 0, NULL, NULL}, |
1510 | {"test_change_user" , test_change_user, TEST_CONNECTION_NEW, 0, NULL, NULL}, |
1511 | {"test_opt_reconnect" , test_opt_reconnect, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
1512 | {"test_compress" , test_compress, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
1513 | {"test_reconnect" , test_reconnect, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1514 | {"test_conc21" , test_conc21, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
1515 | {"test_conc26" , test_conc26, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
1516 | {"test_connection_timeout" , test_connection_timeout, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
1517 | {"test_connection_timeout2" , test_connection_timeout2, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
1518 | {"test_connection_timeout3" , test_connection_timeout3, TEST_CONNECTION_NONE, 0, NULL, NULL}, |
1519 | {NULL, NULL, 0, 0, NULL, NULL} |
1520 | }; |
1521 | |
1522 | |
1523 | int main(int argc, char **argv) |
1524 | { |
1525 | if (argc > 1) |
1526 | get_options(argc, argv); |
1527 | |
1528 | get_envvars(); |
1529 | |
1530 | run_tests(my_tests); |
1531 | |
1532 | return(exit_status()); |
1533 | } |
1534 | |