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 #include "my_test.h"
25 #include "ma_common.h"
26 
27 #include <mysql/client_plugin.h>
28 
29 
30 /*
31   Bug#28075 "COM_DEBUG crashes mysqld"
32 */
33 #ifdef _WIN32
34 #define R_OK 4
35 #endif
36 
test_bug28075(MYSQL * mysql)37 static int test_bug28075(MYSQL *mysql)
38 {
39   int rc;
40 
41   SKIP_SKYSQL;
42   SKIP_MAXSCALE;
43 
44   rc= mysql_dump_debug_info(mysql);
45   check_mysql_rc(rc, mysql);
46 
47   rc= mysql_ping(mysql);
48   check_mysql_rc(rc, mysql);
49 
50   return OK;
51 }
52 
53 /*
54   Bug#28505: mysql_affected_rows() returns wrong value if CLIENT_FOUND_ROWS
55   flag is set.
56 */
57 
test_bug28505(MYSQL * mysql)58 static int test_bug28505(MYSQL *mysql)
59 {
60   unsigned long long res;
61   int rc;
62 
63   rc= mysql_query(mysql, "drop table if exists t1");
64   check_mysql_rc(rc, mysql);
65   rc= mysql_query(mysql, "create table t1(f1 int primary key)");
66   check_mysql_rc(rc, mysql);
67   rc= mysql_query(mysql, "insert into t1 values(1)");
68   check_mysql_rc(rc, mysql);
69   rc= mysql_query(mysql, "insert into t1 values(1) on duplicate key update f1=1");
70   check_mysql_rc(rc, mysql);
71   res= mysql_affected_rows(mysql);
72   FAIL_UNLESS(!res, "res != 0");
73   rc= mysql_query(mysql, "drop table t1");
74   check_mysql_rc(rc, mysql);
75   return OK;
76 }
77 
78 /*
79   Bug #29692  	Single row inserts can incorrectly report a huge number of
80   row insertions
81 */
82 
test_bug29692(MYSQL * mysql)83 static int test_bug29692(MYSQL *mysql)
84 {
85   int rc;
86   rc= mysql_query(mysql, "drop table if exists t1");
87   check_mysql_rc(rc, mysql);
88   rc= mysql_query(mysql, "create table t1(f1 int)");
89   check_mysql_rc(rc, mysql);
90   rc= mysql_query(mysql, "insert into t1 values(1)");
91   check_mysql_rc(rc, mysql);
92   FAIL_UNLESS(1 == mysql_affected_rows(mysql), "affected_rows != 1");
93   rc= mysql_query(mysql, "drop table t1");
94   check_mysql_rc(rc, mysql);
95   return OK;
96 }
97 
bug31418_impl()98 static int bug31418_impl()
99 {
100   my_bool is_null;
101   MYSQL *mysql;
102   int rc;
103 
104 
105   /* Create a new connection. */
106 
107   mysql= test_connect(NULL);
108   if (!mysql)
109     return FAIL;
110 
111   /***********************************************************************
112     Check that lock is free:
113       - IS_FREE_LOCK() should return 1;
114       - IS_USED_LOCK() should return NULL;
115   ***********************************************************************/
116 
117   is_null= query_int_variable(mysql,
118                               "IS_FREE_LOCK('bug31418')",
119                               &rc);
120   FAIL_UNLESS(!is_null && rc, "rc = 0");
121 
122   is_null= query_int_variable(mysql,
123                               "IS_USED_LOCK('bug31418')",
124                               &rc);
125   FAIL_UNLESS(is_null, "rc = 0");
126 
127   /***********************************************************************
128     Acquire lock and check the lock status (the lock must be in use):
129       - IS_FREE_LOCK() should return 0;
130       - IS_USED_LOCK() should return non-zero thread id;
131   ***********************************************************************/
132 
133   query_int_variable(mysql, "GET_LOCK('bug31418', 1)", &rc);
134   FAIL_UNLESS(rc, "rc = 0");
135 
136   is_null= query_int_variable(mysql,
137                               "IS_FREE_LOCK('bug31418')",
138                               &rc);
139   FAIL_UNLESS(!is_null && !rc, "rc = 0");
140 
141   is_null= query_int_variable(mysql,
142                               "IS_USED_LOCK('bug31418')",
143                               &rc);
144   FAIL_UNLESS(!is_null && rc, "rc = 0");
145 
146   /***********************************************************************
147     Issue COM_CHANGE_USER command and check the lock status
148     (the lock must be free):
149       - IS_FREE_LOCK() should return 1;
150       - IS_USED_LOCK() should return NULL;
151   **********************************************************************/
152 
153   rc= mysql_change_user(mysql, username, password, schema ? schema : "test");
154   check_mysql_rc(rc, mysql);
155 
156   is_null= query_int_variable(mysql,
157                               "IS_FREE_LOCK('bug31418')",
158                               &rc);
159   FAIL_UNLESS(!is_null && rc, "rc = 0");
160 
161   is_null= query_int_variable(mysql,
162                               "IS_USED_LOCK('bug31418')",
163                               &rc);
164   FAIL_UNLESS(is_null, "rc = 0");
165 
166   /***********************************************************************
167    That's it. Cleanup.
168   ***********************************************************************/
169 
170   mysql_close(mysql);
171   return OK;
172 }
173 
test_bug31418(MYSQL * unused)174 static int test_bug31418(MYSQL *unused __attribute__((unused)))
175 {
176   int i;
177   SKIP_MAXSCALE;
178 
179   if (!is_mariadb)
180     return SKIP;
181   /* Run test case for BUG#31418 for three different connections. */
182 
183   for (i=0; i < 3; i++)
184     if (bug31418_impl())
185       return FAIL;
186 
187   return OK;
188 }
189 
190 /* Query processing */
191 
test_debug_example(MYSQL * mysql)192 static int test_debug_example(MYSQL *mysql)
193 {
194   int rc;
195   MYSQL_RES *result;
196 
197 
198   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_debug_example");
199   check_mysql_rc(rc, mysql);
200 
201   rc= mysql_query(mysql, "CREATE TABLE test_debug_example("
202                          "id INT PRIMARY KEY AUTO_INCREMENT, "
203                          "name VARCHAR(20), xxx INT)");
204   check_mysql_rc(rc, mysql);
205 
206   rc= mysql_query(mysql, "INSERT INTO test_debug_example (name) "
207                          "VALUES ('mysql')");
208   check_mysql_rc(rc, mysql);
209 
210   rc= mysql_query(mysql, "UPDATE test_debug_example SET name='updated' "
211                          "WHERE name='deleted'");
212   check_mysql_rc(rc, mysql);
213 
214   rc= mysql_query(mysql, "SELECT * FROM test_debug_example where name='mysql'");
215   check_mysql_rc(rc, mysql);
216 
217   result= mysql_use_result(mysql);
218   FAIL_IF(!result, "Invalid result set");
219 
220   while (mysql_fetch_row(result));
221   mysql_free_result(result);
222 
223   rc= mysql_query(mysql, "DROP TABLE test_debug_example");
224   check_mysql_rc(rc, mysql);
225   return OK;
226 }
227 
228 /*
229   Test a crash when invalid/corrupted .frm is used in the
230   SHOW TABLE STATUS
231   bug #93 (reported by serg@mysql.com).
232 */
233 
test_frm_bug(MYSQL * mysql)234 static int test_frm_bug(MYSQL *mysql)
235 {
236   MYSQL_STMT *stmt;
237   MYSQL_BIND my_bind[2];
238   MYSQL_RES  *result;
239   MYSQL_ROW  row;
240   FILE       *test_file;
241   char       data_dir[FN_REFLEN];
242   char       test_frm[1024];
243   int        rc;
244 
245   mysql_autocommit(mysql, TRUE);
246 
247   rc= mysql_query(mysql, "drop table if exists test_frm_bug");
248   check_mysql_rc(rc, mysql);
249 
250   rc= mysql_query(mysql, "flush tables");
251   check_mysql_rc(rc, mysql);
252 
253   stmt= mysql_stmt_init(mysql);
254   FAIL_IF(!stmt, mysql_error(mysql));
255   rc= mysql_stmt_prepare(stmt, SL("show variables like 'datadir'"));
256   check_stmt_rc(rc, stmt);
257 
258   rc= mysql_stmt_execute(stmt);
259   check_stmt_rc(rc, stmt);
260 
261   memset(my_bind, '\0', sizeof(my_bind));
262   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
263   my_bind[0].buffer= data_dir;
264   my_bind[0].buffer_length= FN_REFLEN;
265   my_bind[1]= my_bind[0];
266 
267   rc= mysql_stmt_bind_result(stmt, my_bind);
268   check_stmt_rc(rc, stmt);
269 
270   rc= mysql_stmt_fetch(stmt);
271   check_stmt_rc(rc, stmt);
272 
273   rc= mysql_stmt_fetch(stmt);
274   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
275 
276   snprintf(test_frm, sizeof(test_frm)-1, "%s/%s/test_frm_bug.frm", data_dir, schema);
277 
278   if (!(test_file= fopen(test_frm, "w")))
279   {
280     mysql_stmt_close(stmt);
281     diag("Can't write to file %s -> SKIP", test_frm);
282     return SKIP;
283   }
284 
285   rc= mysql_query(mysql, "SHOW TABLE STATUS like 'test_frm_bug'");
286   check_mysql_rc(rc, mysql);
287 
288   result= mysql_store_result(mysql);
289   FAIL_IF(!result, "Invalid result set");/* It can't be NULL */
290 
291   rc= 0;
292   while (mysql_fetch_row(result))
293     rc++;
294   FAIL_UNLESS(rc == 1, "rowcount != 1");
295 
296   mysql_data_seek(result, 0);
297 
298   row= mysql_fetch_row(result);
299   FAIL_IF(!row, "couldn't fetch row");
300 
301   FAIL_UNLESS(row[17] != 0, "row[17] != 0");
302 
303   mysql_free_result(result);
304   mysql_stmt_close(stmt);
305 
306   fclose(test_file);
307   mysql_query(mysql, "drop table if exists test_frm_bug");
308   unlink(test_frm);
309   return OK;
310 }
311 
test_wl4166_1(MYSQL * mysql)312 static int test_wl4166_1(MYSQL *mysql)
313 {
314   MYSQL_STMT *stmt;
315   int        int_data;
316   char       str_data[50];
317   char       tiny_data;
318   short      small_data;
319   longlong   big_data;
320   float      real_data;
321   double     double_data;
322   ulong      length[7];
323   my_bool    is_null[7];
324   MYSQL_BIND my_bind[7];
325   const char *query;
326   int rc;
327   int i;
328 
329   if (mysql_get_server_version(mysql) < 50100) {
330     diag("Test requires MySQL Server version 5.1 or above");
331     return SKIP;
332   }
333   rc= mysql_query(mysql, "DROP TABLE IF EXISTS table_4166");
334   check_mysql_rc(rc, mysql);
335 
336   rc= mysql_query(mysql, "CREATE TABLE table_4166(col1 tinyint NOT NULL, "
337                          "col2 varchar(15), col3 int, "
338                          "col4 smallint, col5 bigint, "
339                          "col6 float, col7 double, "
340                          "colX varchar(10) default NULL)");
341   check_mysql_rc(rc, mysql);
342 
343   stmt= mysql_stmt_init(mysql);
344   FAIL_IF(!stmt, mysql_error(mysql));
345   query= "INSERT INTO table_4166(col1, col2, col3, col4, col5, col6, col7) "
346           "VALUES(?, ?, ?, ?, ?, ?, ?)";
347   rc= mysql_stmt_prepare(stmt, SL(query));
348   check_stmt_rc(rc, stmt);
349 
350   FAIL_IF(mysql_stmt_param_count(stmt) != 7, "param_count != 7");
351 
352   memset(my_bind, '\0', sizeof(my_bind));
353   /* tinyint */
354   my_bind[0].buffer_type= MYSQL_TYPE_TINY;
355   my_bind[0].buffer= (void *)&tiny_data;
356   /* string */
357   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
358   my_bind[1].buffer= (void *)str_data;
359   my_bind[1].buffer_length= 1000;                  /* Max string length */
360   /* integer */
361   my_bind[2].buffer_type= MYSQL_TYPE_LONG;
362   my_bind[2].buffer= (void *)&int_data;
363   /* short */
364   my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
365   my_bind[3].buffer= (void *)&small_data;
366   /* bigint */
367   my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
368   my_bind[4].buffer= (void *)&big_data;
369   /* float */
370   my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
371   my_bind[5].buffer= (void *)&real_data;
372   /* double */
373   my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
374   my_bind[6].buffer= (void *)&double_data;
375 
376   for (i= 0; i < (int) array_elements(my_bind); i++)
377   {
378     my_bind[i].length= &length[i];
379     my_bind[i].is_null= &is_null[i];
380     is_null[i]= 0;
381   }
382 
383   rc= mysql_stmt_bind_param(stmt, my_bind);
384   check_stmt_rc(rc, stmt);
385 
386   int_data= 320;
387   small_data= 1867;
388   big_data= 1000;
389   real_data= 2;
390   double_data= 6578.001;
391 
392   /* now, execute the prepared statement to insert 10 records.. */
393   for (tiny_data= 0; tiny_data < 10; tiny_data++)
394   {
395     length[1]= sprintf(str_data, "MySQL%d", int_data);
396     rc= mysql_stmt_execute(stmt);
397     check_stmt_rc(rc, stmt);
398     int_data += 25;
399     small_data += 10;
400     big_data += 100;
401     real_data += 1;
402     double_data += 10.09;
403   }
404 
405   /* force a re-prepare with some DDL */
406 
407   rc= mysql_query(mysql,
408     "ALTER TABLE table_4166 change colX colX varchar(20) default NULL");
409   check_mysql_rc(rc, mysql);
410 
411   /*
412     execute the prepared statement again,
413     without changing the types of parameters already bound.
414   */
415 
416   for (tiny_data= 50; tiny_data < 60; tiny_data++)
417   {
418     length[1]= sprintf(str_data, "MySQL%d", int_data);
419     rc= mysql_stmt_execute(stmt);
420     check_stmt_rc(rc, stmt);
421     int_data += 25;
422     small_data += 10;
423     big_data += 100;
424     real_data += 1;
425     double_data += 10.09;
426   }
427 
428   mysql_stmt_close(stmt);
429 
430   rc= mysql_query(mysql, "DROP TABLE table_4166");
431   check_mysql_rc(rc, mysql);
432   return OK;
433 }
434 
435 
test_wl4166_2(MYSQL * mysql)436 static int test_wl4166_2(MYSQL *mysql)
437 {
438   MYSQL_STMT *stmt;
439   int        c_int;
440   MYSQL_TIME d_date;
441   MYSQL_BIND bind_out[2];
442   int rc;
443 
444   if (mysql_get_server_version(mysql) < 50100) {
445     diag("Test requires MySQL Server version 5.1 or above");
446     return SKIP;
447   }
448 
449   rc= mysql_query(mysql, "drop table if exists t1");
450   check_mysql_rc(rc, mysql);
451   rc= mysql_query(mysql, "create table t1 (c_int int, d_date date)");
452   check_mysql_rc(rc, mysql);
453   rc= mysql_query(mysql,
454                   "insert into t1 (c_int, d_date) values (42, '1948-05-15')");
455   check_mysql_rc(rc, mysql);
456 
457   stmt= mysql_stmt_init(mysql);
458   FAIL_IF(!stmt, mysql_error(mysql));
459   rc= mysql_stmt_prepare(stmt, SL("select * from t1"));
460   check_stmt_rc(rc, stmt);
461 
462   memset(bind_out, '\0', sizeof(bind_out));
463   bind_out[0].buffer_type= MYSQL_TYPE_LONG;
464   bind_out[0].buffer= (void*) &c_int;
465 
466   bind_out[1].buffer_type= MYSQL_TYPE_DATE;
467   bind_out[1].buffer= (void*) &d_date;
468 
469   rc= mysql_stmt_bind_result(stmt, bind_out);
470   check_stmt_rc(rc, stmt);
471 
472   /* int -> varchar transition */
473 
474   rc= mysql_query(mysql,
475                   "alter table t1 change column c_int c_int varchar(11)");
476   check_mysql_rc(rc, mysql);
477 
478   rc= mysql_query(mysql, "FLUSH TABLES");
479   check_mysql_rc(rc, mysql);
480 
481   rc= mysql_stmt_execute(stmt);
482   check_stmt_rc(rc, stmt);
483 
484   rc= mysql_stmt_fetch(stmt);
485   check_stmt_rc(rc, stmt);
486 
487   FAIL_UNLESS(c_int == 42, "c_int != 42");
488   FAIL_UNLESS(d_date.year == 1948, "y!=1948");
489   FAIL_UNLESS(d_date.month == 5, "m != 5");
490   FAIL_UNLESS(d_date.day == 15, "d != 15");
491 
492   rc= mysql_stmt_fetch(stmt);
493   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
494 
495   /* varchar to int retrieval with truncation */
496 
497   rc= mysql_query(mysql, "update t1 set c_int='abcde'");
498   check_mysql_rc(rc, mysql);
499 
500   rc= mysql_stmt_execute(stmt);
501   check_stmt_rc(rc, stmt);
502 
503   rc= mysql_stmt_fetch(stmt);
504   FAIL_IF(!rc, "Error expected");
505 
506   FAIL_UNLESS(c_int == 0, "c != 0");
507 
508   rc= mysql_stmt_fetch(stmt);
509   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
510 
511   /* alter table and increase the number of columns */
512   rc= mysql_query(mysql, "alter table t1 add column d_int int");
513   check_mysql_rc(rc, mysql);
514 
515   rc= mysql_query(mysql, "FLUSH TABLES");
516   check_mysql_rc(rc, mysql);
517 
518   rc= mysql_stmt_execute(stmt);
519   FAIL_IF(!rc, "Error expected");
520 
521   rc= mysql_stmt_reset(stmt);
522   check_stmt_rc(rc, stmt);
523 
524   /* decrease the number of columns */
525   rc= mysql_query(mysql, "alter table t1 drop d_date, drop d_int");
526   check_mysql_rc(rc, mysql);
527   rc= mysql_stmt_execute(stmt);
528   diag("rc=%d error: %d\n", rc, mysql_stmt_errno(stmt));
529   FAIL_IF(!rc, "Error expected");
530 
531   mysql_stmt_close(stmt);
532   rc= mysql_query(mysql, "drop table t1");
533   check_mysql_rc(rc, mysql);
534 
535   return OK;
536 }
537 
538 
539 /**
540   Test how warnings generated during assignment of parameters
541   are (currently not) preserve in case of reprepare.
542 */
543 
test_wl4166_3(MYSQL * mysql)544 static int test_wl4166_3(MYSQL *mysql)
545 {
546   int rc;
547   MYSQL_STMT *stmt;
548   MYSQL_BIND my_bind[1];
549   MYSQL_TIME tm[1];
550 
551   if (mysql_get_server_version(mysql) < 50100) {
552     diag("Test requires MySQL Server version 5.1 or above");
553     return SKIP;
554   }
555 
556   rc= mysql_query(mysql, "drop table if exists t1");
557   check_mysql_rc(rc, mysql);
558 
559   rc= mysql_query(mysql, "create table t1 (year datetime)");
560   check_mysql_rc(rc, mysql);
561 
562   stmt= mysql_stmt_init(mysql);
563   FAIL_IF(!stmt, mysql_error(mysql));
564   rc= mysql_stmt_prepare(stmt, SL("insert into t1 (year) values (?)"));
565   check_stmt_rc(rc, stmt);
566 
567   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "param_count != 1");
568 
569   memset(my_bind, '\0', sizeof(my_bind));
570   my_bind[0].buffer_type= MYSQL_TYPE_DATETIME;
571   my_bind[0].buffer= &tm[0];
572 
573   rc= mysql_stmt_bind_param(stmt, my_bind);
574   check_stmt_rc(rc, stmt);
575 
576   tm[0].year= 2014;
577   tm[0].month= 1; tm[0].day= 1;
578   tm[0].hour= 1; tm[0].minute= 1; tm[0].second= 1;
579   tm[0].second_part= 0; tm[0].neg= 0;
580 
581   /* Cause a statement reprepare */
582   rc= mysql_query(mysql, "alter table t1 add column c int");
583   check_mysql_rc(rc, mysql);
584 
585   rc= mysql_stmt_execute(stmt);
586   diag("rc=%d %s", rc, mysql_stmt_error(stmt));
587   check_stmt_rc(rc, stmt);
588 
589   if (verify_col_data(mysql, "t1", "year", "2014-01-01 01:01:01")) {
590     mysql_stmt_close(stmt);
591     rc= mysql_query(mysql, "drop table t1");
592     return FAIL;
593   }
594 
595   mysql_stmt_close(stmt);
596 
597   rc= mysql_query(mysql, "drop table t1");
598   check_mysql_rc(rc, mysql);
599   return OK;
600 }
601 
602 
603 /**
604   Test that long data parameters, as well as parameters
605   that were originally in a different character set, are
606   preserved in case of reprepare.
607 */
608 
test_wl4166_4(MYSQL * mysql)609 static int test_wl4166_4(MYSQL *mysql)
610 {
611   MYSQL_STMT *stmt;
612   int rc;
613   const char *stmt_text;
614   MYSQL_BIND bind_array[2];
615 
616   /* Represented as numbers to keep UTF8 tools from clobbering them. */
617   const char *koi8= "\xee\xd5\x2c\x20\xda\xc1\x20\xd2\xd9\xc2\xc1\xcc\xcb\xd5";
618   const char *cp1251= "\xcd\xf3\x2c\x20\xe7\xe0\x20\xf0\xfb\xe1\xe0\xeb\xea\xf3";
619   char buf1[16], buf2[16];
620   ulong buf1_len, buf2_len;
621 
622   if (mysql_get_server_version(mysql) < 50100) {
623     diag("Test requires MySQL Server version 5.1 or above");
624     return SKIP;
625   }
626 
627   rc= mysql_query(mysql, "drop table if exists t1");
628   check_mysql_rc(rc, mysql);
629 
630   /*
631     Create table with binary columns, set session character set to cp1251,
632     client character set to koi8, and make sure that there is conversion
633     on insert and no conversion on select
634   */
635   rc= mysql_query(mysql,
636                   "create table t1 (c1 varbinary(255), c2 varbinary(255))");
637   check_mysql_rc(rc, mysql);
638   rc= mysql_query(mysql, "set character_set_client=koi8r, "
639                          "character_set_connection=cp1251, "
640                          "character_set_results=koi8r");
641   check_mysql_rc(rc, mysql);
642 
643   memset(bind_array, '\0', sizeof(bind_array));
644 
645   bind_array[0].buffer_type= MYSQL_TYPE_STRING;
646 
647   bind_array[1].buffer_type= MYSQL_TYPE_STRING;
648   bind_array[1].buffer= (void *) koi8;
649   bind_array[1].buffer_length= (unsigned long)strlen(koi8);
650 
651   stmt= mysql_stmt_init(mysql);
652   check_stmt_rc(rc, stmt);
653 
654   stmt_text= "insert into t1 (c1, c2) values (?, ?)";
655 
656   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
657   check_stmt_rc(rc, stmt);
658 
659   mysql_stmt_bind_param(stmt, bind_array);
660 
661   mysql_stmt_send_long_data(stmt, 0, koi8, (unsigned long)strlen(koi8));
662 
663   /* Cause a reprepare at statement execute */
664   rc= mysql_query(mysql, "alter table t1 add column d int");
665   check_mysql_rc(rc, mysql);
666 
667   rc= mysql_stmt_execute(stmt);
668   check_stmt_rc(rc, stmt);
669 
670   stmt_text= "select c1, c2 from t1";
671 
672   /* c1 and c2 are binary so no conversion will be done on select */
673   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
674   check_stmt_rc(rc, stmt);
675 
676   rc= mysql_stmt_execute(stmt);
677   check_stmt_rc(rc, stmt);
678 
679   bind_array[0].buffer= buf1;
680   bind_array[0].buffer_length= sizeof(buf1);
681   bind_array[0].length= &buf1_len;
682 
683   bind_array[1].buffer= buf2;
684   bind_array[1].buffer_length= sizeof(buf2);
685   bind_array[1].length= &buf2_len;
686 
687   mysql_stmt_bind_result(stmt, bind_array);
688 
689   rc= mysql_stmt_fetch(stmt);
690   check_stmt_rc(rc, stmt);
691 
692   FAIL_UNLESS(buf1_len == strlen(cp1251), "");
693   FAIL_UNLESS(buf2_len == strlen(cp1251), "");
694   FAIL_UNLESS(!memcmp(buf1, cp1251, buf1_len), "");
695   FAIL_UNLESS(!memcmp(buf2, cp1251, buf1_len), "");
696 
697   rc= mysql_stmt_fetch(stmt);
698   FAIL_UNLESS(rc == MYSQL_NO_DATA, "");
699 
700   mysql_stmt_close(stmt);
701 
702   rc= mysql_query(mysql, "drop table t1");
703   check_mysql_rc(rc, mysql);
704   rc= mysql_query(mysql, "set names default");
705   check_mysql_rc(rc, mysql);
706   return OK;
707 }
708 
709 /**
710   Test that COM_REFRESH issues a implicit commit.
711 */
712 
test_wl4284_1(MYSQL * mysql)713 static int test_wl4284_1(MYSQL *mysql)
714 {
715   int rc;
716   MYSQL_ROW row;
717   MYSQL_RES *result;
718 
719   diag("Test temporarily disabled");
720   return SKIP;
721 
722   if (mysql_get_server_version(mysql) < 60000) {
723     diag("Test requires MySQL Server version 6.0 or above");
724     return SKIP;
725   }
726 
727   /* set AUTOCOMMIT to OFF */
728   rc= mysql_autocommit(mysql, FALSE);
729   check_mysql_rc(rc, mysql);
730 
731   rc= mysql_query(mysql, "DROP TABLE IF EXISTS trans");
732   check_mysql_rc(rc, mysql);
733 
734   rc= mysql_query(mysql, "CREATE TABLE trans (a INT) ENGINE=InnoDB");
735 
736   if (mysql_errno(mysql) == ER_UNKNOWN_STORAGE_ENGINE)
737   {
738     diag("InnoDB not configured or available");
739     return SKIP;
740   }
741 
742   check_mysql_rc(rc, mysql);
743 
744 
745   rc= mysql_query(mysql, "INSERT INTO trans VALUES(1)");
746   check_mysql_rc(rc, mysql);
747 
748   rc= mysql_refresh(mysql, REFRESH_GRANT | REFRESH_TABLES);
749   check_mysql_rc(rc, mysql);
750 
751   rc= mysql_rollback(mysql);
752   check_mysql_rc(rc, mysql);
753 
754   rc= mysql_query(mysql, "SELECT * FROM trans");
755   check_mysql_rc(rc, mysql);
756 
757   result= mysql_use_result(mysql);
758   FAIL_IF(!result, "Invalid result set");
759 
760   row= mysql_fetch_row(result);
761   FAIL_IF(!row, "Can't fetch row");
762 
763   mysql_free_result(result);
764 
765   /* set AUTOCOMMIT to OFF */
766   rc= mysql_autocommit(mysql, FALSE);
767   check_mysql_rc(rc, mysql);
768 
769   rc= mysql_query(mysql, "DROP TABLE trans");
770   check_mysql_rc(rc, mysql);
771 
772   return OK;
773 }
774 
test_bug49694(MYSQL * mysql)775 static int test_bug49694(MYSQL *mysql)
776 {
777   int rc;
778   MYSQL_RES *res;
779   MYSQL_ROW row;
780   int i;
781   FILE *fp;
782 
783   diag("Load local infile server : %ld", (mysql->server_capabilities & CLIENT_LOCAL_FILES));
784   diag("Load local infile client : %ld", (mysql->client_flag & CLIENT_LOCAL_FILES));
785 
786   SKIP_LOAD_INFILE_DISABLE;
787   SKIP_SKYSQL;
788 
789   rc= mysql_query(mysql, "select @@LOCAL_INFILE");
790   check_mysql_rc(rc, mysql);
791   res= mysql_store_result(mysql);
792   row= mysql_fetch_row(res);
793   if (atol(row[0]) == 0) {
794       diag("Load local infile disable");
795       return SKIP;
796   }
797 
798   rc= mysql_query(mysql, "DROP TABLE IF EXISTS enclist");
799   check_mysql_rc(rc, mysql);
800 
801   rc= mysql_query(mysql, "CREATE TABLE `enclist` ("
802                          "  `pat_id` int(11) NOT NULL,"
803                          "  `episode_id` int(11) NOT NULL,"
804                          "  `enc_id` double NOT NULL,"
805                          "  PRIMARY KEY (`pat_id`,`episode_id`,`enc_id`)"
806                          ") ENGINE=MyISAM DEFAULT CHARSET=latin1");
807   check_mysql_rc(rc, mysql);
808 
809   fp= fopen("data.csv", "w");
810   FAIL_IF(!fp, "Can't open data.csv");
811 
812   for (i=0; i < 100; i++)
813     fprintf (fp, "%.08d,%d,%f\r\n", 100 + i, i % 3 + 1, 60000.0 + i/100);
814   fclose(fp);
815 
816   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE 'data.csv' INTO TABLE enclist "
817                          "FIELDS TERMINATED BY '.' LINES TERMINATED BY '\r\n'");
818   check_mysql_rc(rc, mysql);
819 
820   rc= mysql_query(mysql, "DELETE FROM enclist");
821   check_mysql_rc(rc, mysql);
822 
823   FAIL_IF(mysql_affected_rows(mysql) != 100, "Import failure. Expected 2 imported rows");
824 
825   rc= mysql_query(mysql, "DROP TABLE enclist");
826   check_mysql_rc(rc, mysql);
827   mysql_free_result(res);
828   return OK;
829 }
830 
test_conc49(MYSQL * mysql)831 static int test_conc49(MYSQL *mysql)
832 {
833   int rc;
834   MYSQL_RES *res;
835   MYSQL_ROW row;
836 
837   int i;
838   FILE *fp;
839 
840   SKIP_LOAD_INFILE_DISABLE;
841   SKIP_SKYSQL;
842 
843   rc= mysql_query(mysql, "select @@LOCAL_INFILE");
844   check_mysql_rc(rc, mysql);
845   res= mysql_store_result(mysql);
846   row= mysql_fetch_row(res);
847 
848   i= !atol(row[0]);
849   mysql_free_result(res);
850   if (i) {
851       diag("Load local infile disable");
852       return SKIP;
853   }
854 
855   fp= fopen("./sample.csv", "w");
856   for (i=1; i < 4; i++)
857     fprintf(fp, "\"%d\", \"%d\", \"%d\"\r\n", i, i, i);
858   fclose(fp);
859   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc49");
860   check_mysql_rc(rc, mysql);
861   rc= mysql_query(mysql, "CREATE TABLE conc49 (a int, b int, c int) Engine=InnoDB DEFAULT CHARSET=latin1");
862   check_mysql_rc(rc, mysql);
863   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE './sample.csv' INTO TABLE conc49 FIELDS ESCAPED BY ' ' TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n'");
864   check_mysql_rc(rc, mysql);
865 
866   rc= mysql_query(mysql, "SELECT a FROM conc49");
867   check_mysql_rc(rc, mysql);
868   res= mysql_store_result(mysql);
869   rc= (int)mysql_num_rows(res);
870   mysql_free_result(res);
871   FAIL_IF(rc != 3, "3 rows expected");
872   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc49");
873   check_mysql_rc(rc, mysql);
874   return OK;
875 }
876 
test_ldi_path(MYSQL * mysql)877 static int test_ldi_path(MYSQL *mysql)
878 {
879   int rc;
880 
881   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
882   check_mysql_rc(rc, mysql);
883 
884   rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
885   check_mysql_rc(rc, mysql);
886 
887   rc= mysql_query(mysql, "FLUSH TABLES");
888   check_mysql_rc(rc, mysql);
889 
890 #ifdef _WIN32
891   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE 'X:/non_existing_path/data.csv' INTO TABLE t1 "
892                          "FIELDS TERMINATED BY '.' LINES TERMINATED BY '\r\n'");
893 #else
894   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE '/non_existing_path/data.csv' INTO TABLE t1 "
895                          "FIELDS TERMINATED BY '.' LINES TERMINATED BY '\r\n'");
896 #endif
897   FAIL_IF(rc== 0, "Error expected");
898   diag("Error: %d", mysql_errno(mysql));
899   FAIL_IF(mysql_errno(mysql) == 0, "Error expected");
900 
901   rc= mysql_query(mysql, "DROP TABLE t1");
902   check_mysql_rc(rc, mysql);
903   return OK;
904 }
905 
906 #if _WIN32
test_conc44(MYSQL * mysql)907 static int test_conc44(MYSQL *mysql)
908 {
909   char query[1024];
910   char *a_filename= "æøå.csv";
911   int rc;
912   int i;
913   FILE *fp;
914 
915   rc= mysql_set_character_set(mysql, "latin1");
916   check_mysql_rc(rc, mysql);
917 
918   rc= mysql_query(mysql, "DROP TABLE IF EXISTS enclist");
919   check_mysql_rc(rc, mysql);
920 
921   rc= mysql_query(mysql, "CREATE TABLE `enclist` ("
922                          "  `pat_id` int(11) NOT NULL,"
923                          "  `episode_id` int(11) NOT NULL,"
924                          "  `enc_id` double NOT NULL,"
925                          "  PRIMARY KEY (`pat_id`,`episode_id`,`enc_id`)"
926                          ") ENGINE=MyISAM DEFAULT CHARSET=latin1");
927   check_mysql_rc(rc, mysql);
928 
929   fp= fopen(a_filename, "w");
930   FAIL_IF(!fp, "Can't open file");
931 
932   for (i=0; i < 100; i++)
933     fprintf (fp, "%.08d,%d,%f\r\n", 100 + i, i % 3 + 1, 60000.0 + i/100);
934   fclose(fp);
935 
936   sprintf(query, "LOAD DATA LOCAL INFILE '%s' INTO TABLE enclist "
937                          "FIELDS TERMINATED BY '.' LINES TERMINATED BY '\r\n'", a_filename);
938   rc= mysql_query(mysql, query);
939   check_mysql_rc(rc, mysql);
940 
941   rc= mysql_query(mysql, "DELETE FROM enclist");
942   check_mysql_rc(rc, mysql);
943 
944   FAIL_IF(mysql_affected_rows(mysql) != 100, "Import failure. Expected 2 imported rows");
945 
946   rc= mysql_query(mysql, "DROP TABLE enclist");
947   check_mysql_rc(rc, mysql);
948   return OK;
949 }
950 #endif
951 
test_connect_attrs(MYSQL * my)952 static int test_connect_attrs(MYSQL *my)
953 {
954   MYSQL *mysql;
955   MYSQL_RES *result;
956   int rc, len;
957 
958   rc= mysql_query(my, "SELECT * FROM performance_schema.session_connect_attrs LIMIT 1");
959   if (rc != 0)
960   {
961     diag("Server doesn't support connection attributes");
962     return SKIP;
963   }
964 
965   result= mysql_store_result(my);
966   /* MariaDB Connector/C already sent connection attrs after handshake. So if the table is
967      empty, it indicates that the performance schema is disabled */
968   if (!mysql_num_rows(result))
969   {
970     diag("skip: performance_schema not enabled");
971     mysql_free_result(result);
972     return SKIP;
973   }
974   mysql_free_result(result);
975 
976   mysql= mysql_init(NULL);
977 
978   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo0", "bar0");
979   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo1", "bar1");
980   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo2", "bar2");
981 
982   FAIL_IF(!my_test_connect(mysql, hostname, username, password, schema,
983                          port, socketname, 0), mysql_error(my));
984 
985   if (!(mysql->server_capabilities & CLIENT_CONNECT_ATTRS))
986   {
987     diag("Server doesn't support connection attributes");
988     return SKIP;
989   }
990 
991   rc= mysql_query(mysql, "SELECT * FROM performance_schema.session_connect_attrs where attr_name like 'foo%'");
992   check_mysql_rc(rc, mysql);
993   result= mysql_store_result(mysql);
994   rc= (int)mysql_num_rows(result);
995   mysql_free_result(result);
996 
997   mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_RESET, NULL);
998   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo0", "bar0");
999   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo1", "bar1");
1000   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo2", "bar2");
1001   mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "foo0");
1002   mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "foo1");
1003   mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "foo2");
1004 
1005   len= (int)mysql->options.extension->connect_attrs_len;
1006 
1007   mysql_close(mysql);
1008 
1009   FAIL_IF(rc < 3, "Expected 3 or more rows");
1010   FAIL_IF(len != 0, "Expected connection_attr_len=0");
1011 
1012   return OK;
1013 }
1014 
test_conc_114(MYSQL * mysql)1015 static int test_conc_114(MYSQL *mysql)
1016 {
1017   if (mysql_client_find_plugin(mysql, "foo", 0))
1018   {
1019     diag("Null pointer expected");
1020     return FAIL;
1021   }
1022   diag("Error: %s", mysql_error(mysql));
1023   return OK;
1024 }
1025 
1026 /* run with valgrind */
test_conc117(MYSQL * unused)1027 static int test_conc117(MYSQL *unused __attribute__((unused)))
1028 {
1029   my_bool reconnect= 1;
1030   MYSQL *my= mysql_init(NULL);
1031   SKIP_MAXSCALE;
1032   FAIL_IF(!my_test_connect(my, hostname, username, password, schema,
1033                          port, socketname, 0), mysql_error(my));
1034 
1035   mysql_kill(my, mysql_thread_id(my));
1036 
1037   mysql_options(my, MYSQL_OPT_RECONNECT, &reconnect);
1038 
1039   mysql_query(my, "SET @a:=1");
1040   mysql_close(my);
1041 
1042   return OK;
1043 }
1044 
test_read_timeout(MYSQL * unused)1045 static int test_read_timeout(MYSQL *unused __attribute__((unused)))
1046 {
1047   int timeout= 5, rc;
1048   MYSQL *my= mysql_init(NULL);
1049   SKIP_MAXSCALE;
1050   mysql_options(my, MYSQL_OPT_READ_TIMEOUT, &timeout);
1051   FAIL_IF(!my_test_connect(my, hostname, username, password, schema,
1052                          port, socketname, 0), mysql_error(my));
1053 
1054   rc= mysql_query(my, "SELECT SLEEP(50)");
1055 
1056   FAIL_IF(rc == 0, "error expected");
1057   diag("error: %s", mysql_error(my));
1058 
1059   mysql_close(my);
1060 
1061   return OK;
1062 }
1063 
1064 #ifndef __has_feature
1065 # define __has_feature(x) 0
1066 #endif
1067 #if !__has_feature(memory_sanitizer)
1068 #ifdef HAVE_REMOTEIO
1069 void *remote_plugin;
test_remote1(MYSQL * mysql)1070 static int test_remote1(MYSQL *mysql)
1071 {
1072   int rc;
1073   MYSQL_RES *res;
1074   MYSQL_ROW row;
1075   SKIP_SKYSQL;
1076 
1077   remote_plugin= (void *)mysql_client_find_plugin(mysql, "remote_io", MARIADB_CLIENT_REMOTEIO_PLUGIN);
1078   if (!remote_plugin)
1079   {
1080     diag("skip - no remote io plugin available");
1081     diag("error: %s", mysql_error(mysql));
1082     return SKIP;
1083   }
1084 
1085   SKIP_LOAD_INFILE_DISABLE;
1086 
1087   rc= mysql_query(mysql, "select @@LOCAL_INFILE");
1088   check_mysql_rc(rc, mysql);
1089   res= mysql_store_result(mysql);
1090   row= mysql_fetch_row(res);
1091   if (atol(row[0]) == 0) {
1092       diag("Load local infile disable");
1093       return SKIP;
1094   }
1095   mysql_free_result(res);
1096 
1097   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1098   check_mysql_rc(rc, mysql);
1099 
1100   rc= mysql_query(mysql, "CREATE TABLE t1 (a text)");
1101   check_mysql_rc(rc, mysql);
1102 
1103   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE 'http://www.example.com' INTO TABLE t1");
1104   if (rc && mysql_errno(mysql) == 2058)
1105   {
1106     diag("remote_io plugin not available");
1107     return SKIP;
1108   }
1109   check_mysql_rc(rc, mysql);
1110   return OK;
1111 }
1112 
test_remote2(MYSQL * my)1113 static int test_remote2(MYSQL *my)
1114 {
1115   MYSQL *mysql;
1116 
1117   if (!remote_plugin)
1118   {
1119     diag("skip - no remote io plugin available");
1120     return SKIP;
1121   }
1122   mysql= mysql_init(NULL);
1123 
1124   mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "http://localhost/test.cnf");
1125   mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "test");
1126   my_test_connect(mysql, hostname, username, password, schema,
1127                          0, socketname, 0), mysql_error(my);
1128   diag("port: %d", mysql->port);
1129   mysql_close(mysql);
1130   return OK;
1131 }
1132 #endif
1133 #endif
1134 
1135 #ifndef _WIN32
test_mdev12965(MYSQL * unused)1136 static int test_mdev12965(MYSQL *unused __attribute__((unused)))
1137 {
1138   MYSQL *mysql;
1139   my_bool reconnect = 0;
1140   FILE *fp= NULL;
1141   const char *env= getenv("MYSQL_TMP_DIR");
1142   char cnf_file1[FN_REFLEN + 1];
1143 
1144   SKIP_SKYSQL;
1145   if (travis_test)
1146     return SKIP;
1147 
1148   if (!env)
1149     env= "/tmp";
1150 
1151   setenv("HOME", env, 1);
1152 
1153   snprintf(cnf_file1, FN_REFLEN, "%s%c.my.cnf", env, FN_LIBCHAR);
1154 
1155   diag("Config file: %s", cnf_file1);
1156 
1157   FAIL_IF(!access(cnf_file1, R_OK), "access");
1158 
1159   mysql= mysql_init(NULL);
1160   fp= fopen(cnf_file1, "w");
1161   FAIL_IF(!fp, "fopen");
1162 
1163   fprintf(fp, "[client]\ndefault-character-set=latin2\nreconnect=1\n");
1164   fclose(fp);
1165 
1166   mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "");
1167   my_test_connect(mysql, hostname, username, password,
1168                   schema, 0, socketname, 0);
1169 
1170   remove(cnf_file1);
1171 
1172   FAIL_IF(strcmp(mysql_character_set_name(mysql), "latin2"), "expected charset latin2");
1173   mysql_get_optionv(mysql, MYSQL_OPT_RECONNECT, &reconnect);
1174   FAIL_IF(reconnect != 1, "expected reconnect=1");
1175   mysql_close(mysql);
1176   return OK;
1177 }
1178 #endif
1179 
test_get_info(MYSQL * mysql)1180 static int test_get_info(MYSQL *mysql)
1181 {
1182   size_t sval;
1183   unsigned int ival;
1184   char *cval;
1185   int rc;
1186   MY_CHARSET_INFO cs;
1187   MARIADB_CHARSET_INFO *ci;
1188   char **errors;
1189 
1190   rc= mariadb_get_infov(mysql, MARIADB_MAX_ALLOWED_PACKET, &sval);
1191   FAIL_IF(rc, "mysql_get_info failed");
1192   diag("max_allowed_packet: %lu", (unsigned long)sval);
1193   rc= mariadb_get_infov(mysql, MARIADB_NET_BUFFER_LENGTH, &sval);
1194   FAIL_IF(rc, "mysql_get_info failed");
1195   diag("net_buffer_length: %lu", (unsigned long)sval);
1196   rc= mariadb_get_infov(mysql, MARIADB_CLIENT_VERSION_ID, &sval);
1197   FAIL_IF(rc, "mysql_get_info failed");
1198   diag("client_version_id: %lu", (unsigned long)sval);
1199   rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_SERVER_VERSION_ID, &sval);
1200   FAIL_IF(rc, "mysql_get_info failed");
1201   diag("server_version_id: %lu", (unsigned long)sval);
1202   rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_MARIADB_CHARSET_INFO, &cs);
1203   FAIL_IF(rc, "mysql_get_info failed");
1204   diag("charset name: %s", cs.csname);
1205   rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_PVIO_TYPE, &ival);
1206   FAIL_IF(rc, "mysql_get_info failed");
1207   diag("connection type: %d", ival);
1208   rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_PROTOCOL_VERSION_ID, &ival);
1209   FAIL_IF(rc, "mysql_get_info failed");
1210   diag("protocol_version: %d", ival);
1211   rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_SERVER_TYPE, &cval);
1212   FAIL_IF(rc, "mysql_get_info failed");
1213   diag("server_type: %s", cval);
1214   rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_SERVER_VERSION, &cval);
1215   FAIL_IF(rc, "mysql_get_info failed");
1216   diag("server_version: %s", cval);
1217   rc= mariadb_get_infov(mysql, MARIADB_CLIENT_VERSION, &cval);
1218   FAIL_IF(rc, "mysql_get_info failed");
1219   diag("client_version: %s", cval);
1220   rc= mariadb_get_infov(mysql, MARIADB_CHARSET_NAME, &ci, "utf8");
1221   FAIL_IF(rc, "mysql_get_info failed");
1222   diag("charset_name: %s", ci->csname);
1223   diag("charset_nr: %d", ci->nr);
1224   rc= mariadb_get_infov(mysql, MARIADB_CHARSET_ID, &ci, 63);
1225   FAIL_IF(rc, "mysql_get_info failed");
1226   diag("charset_name: %s", ci->csname);
1227   rc= mariadb_get_infov(mysql, MARIADB_CLIENT_ERRORS, &errors);
1228   FAIL_IF(rc, "mysql_get_info failed");
1229   diag("error[0]: %s", errors[0]);
1230   rc= mysql_query(mysql, "DROP TABLE IF exists t1");
1231   check_mysql_rc(rc, mysql);
1232   rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
1233   check_mysql_rc(rc, mysql);
1234   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1),(2)");
1235   check_mysql_rc(rc, mysql);
1236   rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_INFO, &cval);
1237   FAIL_IF(rc, "mysql_get_info failed");
1238   diag("mariadb_info: %s", cval);
1239   return OK;
1240 }
1241 
test_zerofill(MYSQL * mysql)1242 static int test_zerofill(MYSQL *mysql)
1243 {
1244   int rc;
1245   MYSQL_ROW row;
1246   MYSQL_RES *res;
1247 
1248   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1249   check_mysql_rc(rc, mysql);
1250 
1251   rc= mysql_query(mysql, "CREATE TABLE t1 (a int(10) zerofill)");
1252   check_mysql_rc(rc, mysql);
1253 
1254   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
1255   check_mysql_rc(rc, mysql);
1256 
1257   rc= mysql_query(mysql, "SELECT a FROM t1");
1258   check_mysql_rc(rc, mysql);
1259 
1260   if ((res= mysql_store_result(mysql)))
1261   {
1262     row= mysql_fetch_row(res);
1263     diag("zerofill: %s", row[0]);
1264     mysql_free_result(res);
1265   }
1266   return OK;
1267 }
1268 
test_server_status(MYSQL * mysql)1269 static int test_server_status(MYSQL *mysql)
1270 {
1271   int rc;
1272   unsigned int server_status;
1273 //  MYSQL_STMT *stmt;
1274 
1275   if (mysql_get_server_version(mysql) < 100200)
1276     return SKIP;
1277 
1278 //  stmt= mysql_stmt_init(mysql);
1279 
1280   rc= mysql_autocommit(mysql, 1);
1281   mariadb_get_infov(mysql, MARIADB_CONNECTION_SERVER_STATUS, &server_status);
1282   FAIL_IF(!(server_status & SERVER_STATUS_AUTOCOMMIT),
1283           "autocommit flag not set");
1284 
1285   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1286   check_mysql_rc(rc, mysql);
1287 
1288   rc= mysql_query(mysql, "CREATE TABLE t1 (a int, b int)");
1289   check_mysql_rc(rc, mysql);
1290 
1291   rc= mysql_query(mysql, "INSERT INTO t1 (a) VALUES (1),(2),(3),(4),(5)");
1292   check_mysql_rc(rc, mysql);
1293 
1294   rc= mysql_query(mysql, "UPDATE t1 SET a=9 WHERE a=8");
1295   check_mysql_rc(rc, mysql);
1296 
1297   mariadb_get_infov(mysql, MARIADB_CONNECTION_SERVER_STATUS, &server_status);
1298   FAIL_IF(!(server_status & SERVER_QUERY_NO_INDEX_USED), "autocommit flag not set");
1299 
1300   rc= mysql_query(mysql, "CREATE SCHEMA test_tmp");
1301   check_mysql_rc(rc, mysql);
1302 
1303   rc= mysql_select_db(mysql, "test_tmp");
1304   check_mysql_rc(rc, mysql);
1305 
1306   rc= mysql_query(mysql, "DROP SCHEMA test_tmp");
1307   check_mysql_rc(rc, mysql);
1308 
1309   mariadb_get_infov(mysql, MARIADB_CONNECTION_SERVER_STATUS, &server_status);
1310   FAIL_IF(!(server_status & SERVER_STATUS_DB_DROPPED),
1311           "DB_DROP flag not set");
1312 
1313   FAIL_IF(!(server_status & SERVER_SESSION_STATE_CHANGED),
1314           "SESSION_STATE_CHANGED flag not set");
1315 
1316   rc= mysql_select_db(mysql, schema);
1317   check_mysql_rc(rc, mysql);
1318 
1319 //  mysql_stmt_close(stmt);
1320 
1321   return OK;
1322 }
1323 
test_wl6797(MYSQL * mysql)1324 static int test_wl6797(MYSQL *mysql)
1325 {
1326   MYSQL_STMT *stmt;
1327   int        rc;
1328   const char *stmt_text;
1329   my_ulonglong res;
1330 
1331   if (mysql_get_server_version(mysql) < 50703 ||
1332       (mariadb_connection(mysql) && mysql_get_server_version(mysql) < 100203))
1333   {
1334     diag("Skipping test_wl6797: "
1335             "tested feature does not exist in versions before MySQL 5.7.3 and MariaDB 10.2\n");
1336     return OK;
1337   }
1338   /* clean up the session */
1339   rc= mysql_reset_connection(mysql);
1340   FAIL_UNLESS(rc == 0, "");
1341 
1342   /* do prepare of a query */
1343   mysql_query(mysql, "use test");
1344   mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1345   mysql_query(mysql, "CREATE TABLE t1 (a int)");
1346 
1347   stmt= mysql_stmt_init(mysql);
1348   stmt_text= "INSERT INTO t1 VALUES (1), (2)";
1349 
1350   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
1351   check_mysql_rc(rc, mysql);
1352 
1353   /* Execute the insert statement */
1354   rc= mysql_stmt_execute(stmt);
1355   check_mysql_rc(rc, mysql);
1356 
1357   /*
1358    clean the session this should remove the prepare statement
1359    from the cache.
1360   */
1361   rc= mysql_reset_connection(mysql);
1362   FAIL_UNLESS(rc == 0, "");
1363 
1364   /* this below stmt should report error */
1365   rc= mysql_stmt_execute(stmt);
1366   FAIL_IF(rc == 0, "");
1367 
1368   /*
1369    bug#17653288: MYSQL_RESET_CONNECTION DOES NOT RESET LAST_INSERT_ID
1370   */
1371 
1372   mysql_query(mysql, "DROP TABLE IF EXISTS t2");
1373   rc= mysql_query(mysql, "CREATE TABLE t2 (a int NOT NULL PRIMARY KEY"\
1374                          " auto_increment)");
1375   check_mysql_rc(rc, mysql);
1376   rc= mysql_query(mysql, "INSERT INTO t2 VALUES (null)");
1377   check_mysql_rc(rc, mysql);
1378   res= mysql_insert_id(mysql);
1379   FAIL_UNLESS(res == 1, "");
1380   rc= mysql_reset_connection(mysql);
1381   FAIL_UNLESS(rc == 0, "");
1382   res= mysql_insert_id(mysql);
1383   FAIL_UNLESS(res == 0, "");
1384 
1385   rc= mysql_query(mysql, "INSERT INTO t2 VALUES (last_insert_id(100))");
1386   check_mysql_rc(rc, mysql);
1387   res= mysql_insert_id(mysql);
1388   FAIL_UNLESS(res == 100, "");
1389   rc= mysql_reset_connection(mysql);
1390   FAIL_UNLESS(rc == 0, "");
1391   res= mysql_insert_id(mysql);
1392   FAIL_UNLESS(res == 0, "");
1393 
1394   mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1395   mysql_query(mysql, "DROP TABLE IF EXISTS t2");
1396   mysql_stmt_close(stmt);
1397   return OK;
1398 }
1399 
test_conc384(MYSQL * my)1400 static int test_conc384(MYSQL *my __attribute__((unused)))
1401 {
1402   char value[1000];
1403   int len;
1404   MYSQL *mysql= mysql_init(NULL);
1405 
1406   memset(&value, 'A', 999);
1407   value[999]= 0;
1408 
1409   mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo", value);
1410   len= (int)mysql->options.extension->connect_attrs_len;
1411   /* Length: 1 (=len) + 3 (="foo") + 3 (=len) + 999 (="AAA...") = 1006 */
1412   FAIL_IF(len != 1006, "Wrong length");
1413   mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "foo");
1414   len= (int)mysql->options.extension->connect_attrs_len;
1415   /* Length should be zero after deleting the connection attribute */
1416   FAIL_IF(len != 0, "Wrong length");
1417   mysql_close(mysql);
1418   return OK;
1419 }
1420 
1421 #ifndef _WIN32
test_conc395(MYSQL * unused)1422 static int test_conc395(MYSQL *unused __attribute__((unused)))
1423 {
1424   MYSQL *mysql;
1425   FILE *fp= NULL;
1426   const char *env= getenv("MYSQL_TMP_DIR");
1427   char cnf_file1[FN_REFLEN + 1];
1428 
1429   SKIP_SKYSQL;
1430   if (travis_test)
1431     return SKIP;
1432 
1433   if (!env)
1434     env= "/tmp";
1435 
1436   setenv("HOME", env, 1);
1437 
1438   snprintf(cnf_file1, FN_REFLEN, "%s%c.my.cnf", env, FN_LIBCHAR);
1439 
1440   FAIL_IF(!access(cnf_file1, R_OK), "access");
1441 
1442   mysql= mysql_init(NULL);
1443   fp= fopen(cnf_file1, "w");
1444   FAIL_IF(!fp, "fopen");
1445 
1446   /* Mix dash and underscore */
1447   fprintf(fp, "[client]\ndefault_character-set=latin2\n");
1448   fclose(fp);
1449 
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 
1456   FAIL_IF(strcmp(mysql_character_set_name(mysql), "latin2"), "expected charset latin2");
1457   mysql_close(mysql);
1458   return OK;
1459 }
1460 
test_sslenforce(MYSQL * unused)1461 static int test_sslenforce(MYSQL *unused __attribute__((unused)))
1462 {
1463   MYSQL *mysql;
1464   FILE *fp= NULL;
1465   const char *env= getenv("MYSQL_TMP_DIR");
1466   char cnf_file1[FN_REFLEN + 1];
1467 
1468   SKIP_NOTLS;
1469   SKIP_SKYSQL;
1470 
1471   if (travis_test)
1472     return SKIP;
1473 
1474   if (!env)
1475     env= "/tmp";
1476   setenv("HOME", env, 1);
1477 
1478   snprintf(cnf_file1, FN_REFLEN, "%s%c.my.cnf", env, FN_LIBCHAR);
1479 
1480   FAIL_IF(!access(cnf_file1, R_OK), "access");
1481 
1482   mysql= mysql_init(NULL);
1483   fp= fopen(cnf_file1, "w");
1484   FAIL_IF(!fp, "fopen");
1485 
1486   /* Mix dash and underscore */
1487   fprintf(fp, "[client]\nssl_enforce=1\n");
1488   fclose(fp);
1489 
1490   mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "");
1491   my_test_connect(mysql, hostname, username, password,
1492                   schema, 0, socketname, 0);
1493 
1494   remove(cnf_file1);
1495 
1496   FAIL_IF(!mysql_get_ssl_cipher(mysql), "no secure connection");
1497   mysql_close(mysql);
1498   return OK;
1499 }
1500 #endif
1501 
1502 #if !__has_feature(memory_sanitizer)
test_conc457(MYSQL * mysql)1503 static int test_conc457(MYSQL *mysql)
1504 {
1505   MYSQL_RES *result;
1506 
1507   SKIP_MYSQL(mysql);
1508 
1509   result= mysql_list_processes(mysql);
1510 
1511   FAIL_IF(mysql_field_count(mysql) != 9, "expected 9 columns");
1512   mysql_free_result(result);
1513   return OK;
1514 }
1515 #endif
1516 
test_conc458(MYSQL * my)1517 static int test_conc458(MYSQL *my __attribute__((unused)))
1518 {
1519   MYSQL *mysql= mysql_init(NULL);
1520   FAIL_IF(mysql_get_timeout_value(mysql) != 0, "expected timeout 0");
1521   mysql_close(mysql);
1522   return OK;
1523 }
1524 
1525 
1526 struct my_tests_st my_tests[] = {
1527   {"test_conc458", test_conc458, TEST_CONNECTION_NONE, 0, NULL, NULL},
1528 #if !__has_feature(memory_sanitizer)
1529   {"test_conc457", test_conc457, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1530 #endif
1531   {"test_conc384", test_conc384, TEST_CONNECTION_NONE, 0, NULL, NULL},
1532 #ifndef _WIN32
1533   {"test_mdev12965", test_mdev12965, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1534   {"test_conc395", test_conc395, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1535   {"test_sslenforce", test_sslenforce, TEST_CONNECTION_NONE, 0, NULL, NULL},
1536 #endif
1537   {"test_wl6797", test_wl6797, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1538   {"test_server_status", test_server_status, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1539   {"test_read_timeout", test_read_timeout, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1540   {"test_zerofill", test_zerofill, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1541 #if !__has_feature(memory_sanitizer)
1542 #ifdef HAVE_REMOTEIO
1543   {"test_remote1", test_remote1, TEST_CONNECTION_NEW, 0, NULL, NULL},
1544   {"test_remote2", test_remote2, TEST_CONNECTION_NEW, 0, NULL, NULL},
1545 #endif
1546 #endif
1547   {"test_get_info", test_get_info, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1548   {"test_conc117", test_conc117, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1549   {"test_conc_114", test_conc_114, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1550   {"test_connect_attrs", test_connect_attrs, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1551   {"test_conc49", test_conc49, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1552   {"test_bug28075", test_bug28075, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1553   {"test_bug28505", test_bug28505, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1554   {"test_debug_example", test_debug_example, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1555   {"test_bug29692", test_bug29692, TEST_CONNECTION_NEW, CLIENT_FOUND_ROWS,  NULL, NULL},
1556   {"test_bug31418", test_bug31418, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1557   {"test_frm_bug", test_frm_bug, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1558   {"test_wl4166_1", test_wl4166_1, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1559   {"test_wl4166_2", test_wl4166_2, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1560   {"test_wl4166_3", test_wl4166_3, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1561   {"test_wl4166_4", test_wl4166_4, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1562   {"test_wl4284_1", test_wl4284_1, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1563   {"test_bug49694", test_bug49694, TEST_CONNECTION_NEW, 0, NULL, NULL},
1564   {"test_ldi_path", test_ldi_path, TEST_CONNECTION_NEW, 0, NULL, NULL},
1565 #ifdef _WIN32
1566   {"test_conc44", test_conc44, TEST_CONNECTION_NEW, 0, NULL, NULL},
1567 #endif
1568   {NULL, NULL, 0, 0, NULL, 0}
1569 };
1570 
1571 
main(int argc,char ** argv)1572 int main(int argc, char **argv)
1573 {
1574   if (argc > 1)
1575     get_options(argc, argv);
1576 
1577   get_envvars();
1578 
1579   run_tests(my_tests);
1580 
1581   return(exit_status());
1582 }
1583