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   SKIP_MYSQL(mysql);
246 
247   mysql_autocommit(mysql, TRUE);
248 
249   rc= mysql_query(mysql, "drop table if exists test_frm_bug");
250   check_mysql_rc(rc, mysql);
251 
252   rc= mysql_query(mysql, "flush tables");
253   check_mysql_rc(rc, mysql);
254 
255   stmt= mysql_stmt_init(mysql);
256   FAIL_IF(!stmt, mysql_error(mysql));
257   rc= mysql_stmt_prepare(stmt, SL("show variables like 'datadir'"));
258   check_stmt_rc(rc, stmt);
259 
260   rc= mysql_stmt_execute(stmt);
261   check_stmt_rc(rc, stmt);
262 
263   memset(my_bind, '\0', sizeof(my_bind));
264   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
265   my_bind[0].buffer= data_dir;
266   my_bind[0].buffer_length= FN_REFLEN;
267   my_bind[1]= my_bind[0];
268 
269   rc= mysql_stmt_bind_result(stmt, my_bind);
270   check_stmt_rc(rc, stmt);
271 
272   rc= mysql_stmt_fetch(stmt);
273   check_stmt_rc(rc, stmt);
274 
275   rc= mysql_stmt_fetch(stmt);
276   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
277 
278   snprintf(test_frm, sizeof(test_frm)-1, "%s/%s/test_frm_bug.frm", data_dir, schema);
279 
280   if (!(test_file= fopen(test_frm, "w")))
281   {
282     mysql_stmt_close(stmt);
283     diag("Can't write to file %s -> SKIP", test_frm);
284     return SKIP;
285   }
286 
287   rc= mysql_query(mysql, "SHOW TABLE STATUS like 'test_frm_bug'");
288   check_mysql_rc(rc, mysql);
289 
290   result= mysql_store_result(mysql);
291   FAIL_IF(!result, "Invalid result set");/* It can't be NULL */
292 
293   rc= 0;
294   while (mysql_fetch_row(result))
295     rc++;
296   FAIL_UNLESS(rc == 1, "rowcount != 0");
297 
298   mysql_data_seek(result, 0);
299 
300   row= mysql_fetch_row(result);
301   FAIL_IF(!row, "couldn't fetch row");
302 
303   FAIL_UNLESS(row[17] != 0, "row[17] != 0");
304 
305   mysql_free_result(result);
306   mysql_stmt_close(stmt);
307 
308   fclose(test_file);
309   mysql_query(mysql, "drop table if exists test_frm_bug");
310   unlink(test_frm);
311   return OK;
312 }
313 
test_wl4166_1(MYSQL * mysql)314 static int test_wl4166_1(MYSQL *mysql)
315 {
316   MYSQL_STMT *stmt;
317   int        int_data;
318   char       str_data[50];
319   char       tiny_data;
320   short      small_data;
321   longlong   big_data;
322   float      real_data;
323   double     double_data;
324   ulong      length[7];
325   my_bool    is_null[7];
326   MYSQL_BIND my_bind[7];
327   const char *query;
328   int rc;
329   int i;
330 
331   if (mysql_get_server_version(mysql) < 50100) {
332     diag("Test requires MySQL Server version 5.1 or above");
333     return SKIP;
334   }
335   rc= mysql_query(mysql, "DROP TABLE IF EXISTS table_4166");
336   check_mysql_rc(rc, mysql);
337 
338   rc= mysql_query(mysql, "CREATE TABLE table_4166(col1 tinyint NOT NULL, "
339                          "col2 varchar(15), col3 int, "
340                          "col4 smallint, col5 bigint, "
341                          "col6 float, col7 double, "
342                          "colX varchar(10) default NULL)");
343   check_mysql_rc(rc, mysql);
344 
345   stmt= mysql_stmt_init(mysql);
346   FAIL_IF(!stmt, mysql_error(mysql));
347   query= "INSERT INTO table_4166(col1, col2, col3, col4, col5, col6, col7) "
348           "VALUES(?, ?, ?, ?, ?, ?, ?)";
349   rc= mysql_stmt_prepare(stmt, SL(query));
350   check_stmt_rc(rc, stmt);
351 
352   FAIL_IF(mysql_stmt_param_count(stmt) != 7, "param_count != 7");
353 
354   memset(my_bind, '\0', sizeof(my_bind));
355   /* tinyint */
356   my_bind[0].buffer_type= MYSQL_TYPE_TINY;
357   my_bind[0].buffer= (void *)&tiny_data;
358   /* string */
359   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
360   my_bind[1].buffer= (void *)str_data;
361   my_bind[1].buffer_length= 1000;                  /* Max string length */
362   /* integer */
363   my_bind[2].buffer_type= MYSQL_TYPE_LONG;
364   my_bind[2].buffer= (void *)&int_data;
365   /* short */
366   my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
367   my_bind[3].buffer= (void *)&small_data;
368   /* bigint */
369   my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
370   my_bind[4].buffer= (void *)&big_data;
371   /* float */
372   my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
373   my_bind[5].buffer= (void *)&real_data;
374   /* double */
375   my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
376   my_bind[6].buffer= (void *)&double_data;
377 
378   for (i= 0; i < (int) array_elements(my_bind); i++)
379   {
380     my_bind[i].length= &length[i];
381     my_bind[i].is_null= &is_null[i];
382     is_null[i]= 0;
383   }
384 
385   rc= mysql_stmt_bind_param(stmt, my_bind);
386   check_stmt_rc(rc, stmt);
387 
388   int_data= 320;
389   small_data= 1867;
390   big_data= 1000;
391   real_data= 2;
392   double_data= 6578.001;
393 
394   /* now, execute the prepared statement to insert 10 records.. */
395   for (tiny_data= 0; tiny_data < 10; tiny_data++)
396   {
397     length[1]= sprintf(str_data, "MySQL%d", int_data);
398     rc= mysql_stmt_execute(stmt);
399     check_stmt_rc(rc, stmt);
400     int_data += 25;
401     small_data += 10;
402     big_data += 100;
403     real_data += 1;
404     double_data += 10.09;
405   }
406 
407   /* force a re-prepare with some DDL */
408 
409   rc= mysql_query(mysql,
410     "ALTER TABLE table_4166 change colX colX varchar(20) default NULL");
411   check_mysql_rc(rc, mysql);
412 
413   /*
414     execute the prepared statement again,
415     without changing the types of parameters already bound.
416   */
417 
418   for (tiny_data= 50; tiny_data < 60; tiny_data++)
419   {
420     length[1]= sprintf(str_data, "MySQL%d", int_data);
421     rc= mysql_stmt_execute(stmt);
422     check_stmt_rc(rc, stmt);
423     int_data += 25;
424     small_data += 10;
425     big_data += 100;
426     real_data += 1;
427     double_data += 10.09;
428   }
429 
430   mysql_stmt_close(stmt);
431 
432   rc= mysql_query(mysql, "DROP TABLE table_4166");
433   check_mysql_rc(rc, mysql);
434   return OK;
435 }
436 
437 
test_wl4166_2(MYSQL * mysql)438 static int test_wl4166_2(MYSQL *mysql)
439 {
440   MYSQL_STMT *stmt;
441   int        c_int;
442   MYSQL_TIME d_date;
443   MYSQL_BIND bind_out[2];
444   int rc;
445 
446   if (mysql_get_server_version(mysql) < 50100) {
447     diag("Test requires MySQL Server version 5.1 or above");
448     return SKIP;
449   }
450 
451   rc= mysql_query(mysql, "drop table if exists t1");
452   check_mysql_rc(rc, mysql);
453   rc= mysql_query(mysql, "create table t1 (c_int int, d_date date)");
454   check_mysql_rc(rc, mysql);
455   rc= mysql_query(mysql,
456                   "insert into t1 (c_int, d_date) values (42, '1948-05-15')");
457   check_mysql_rc(rc, mysql);
458 
459   stmt= mysql_stmt_init(mysql);
460   FAIL_IF(!stmt, mysql_error(mysql));
461   rc= mysql_stmt_prepare(stmt, SL("select * from t1"));
462   check_stmt_rc(rc, stmt);
463 
464   memset(bind_out, '\0', sizeof(bind_out));
465   bind_out[0].buffer_type= MYSQL_TYPE_LONG;
466   bind_out[0].buffer= (void*) &c_int;
467 
468   bind_out[1].buffer_type= MYSQL_TYPE_DATE;
469   bind_out[1].buffer= (void*) &d_date;
470 
471   rc= mysql_stmt_bind_result(stmt, bind_out);
472   check_stmt_rc(rc, stmt);
473 
474   /* int -> varchar transition */
475 
476   rc= mysql_query(mysql,
477                   "alter table t1 change column c_int c_int varchar(11)");
478   check_mysql_rc(rc, mysql);
479 
480   rc= mysql_query(mysql, "FLUSH TABLES");
481   check_mysql_rc(rc, mysql);
482 
483   rc= mysql_stmt_execute(stmt);
484   check_stmt_rc(rc, stmt);
485 
486   rc= mysql_stmt_fetch(stmt);
487   check_stmt_rc(rc, stmt);
488 
489   FAIL_UNLESS(c_int == 42, "c_int != 42");
490   FAIL_UNLESS(d_date.year == 1948, "y!=1948");
491   FAIL_UNLESS(d_date.month == 5, "m != 5");
492   FAIL_UNLESS(d_date.day == 15, "d != 15");
493 
494   rc= mysql_stmt_fetch(stmt);
495   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
496 
497   /* varchar to int retrieval with truncation */
498 
499   rc= mysql_query(mysql, "update t1 set c_int='abcde'");
500   check_mysql_rc(rc, mysql);
501 
502   rc= mysql_stmt_execute(stmt);
503   check_stmt_rc(rc, stmt);
504 
505   rc= mysql_stmt_fetch(stmt);
506   FAIL_IF(!rc, "Error expected");
507 
508   FAIL_UNLESS(c_int == 0, "c != 0");
509 
510   rc= mysql_stmt_fetch(stmt);
511   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
512 
513   /* alter table and increase the number of columns */
514   rc= mysql_query(mysql, "alter table t1 add column d_int int");
515   check_mysql_rc(rc, mysql);
516 
517   rc= mysql_query(mysql, "FLUSH TABLES");
518   check_mysql_rc(rc, mysql);
519 
520   rc= mysql_stmt_execute(stmt);
521   FAIL_IF(!rc, "Error expected");
522 
523   rc= mysql_stmt_reset(stmt);
524   check_stmt_rc(rc, stmt);
525 
526   /* decrease the number of columns */
527   rc= mysql_query(mysql, "alter table t1 drop d_date, drop d_int");
528   check_mysql_rc(rc, mysql);
529   rc= mysql_stmt_execute(stmt);
530   diag("rc=%d error: %d\n", rc, mysql_stmt_errno(stmt));
531   FAIL_IF(!rc, "Error expected");
532 
533   mysql_stmt_close(stmt);
534   rc= mysql_query(mysql, "drop table t1");
535   check_mysql_rc(rc, mysql);
536 
537   return OK;
538 }
539 
540 
541 /**
542   Test how warnings generated during assignment of parameters
543   are (currently not) preserve in case of reprepare.
544 */
545 
test_wl4166_3(MYSQL * mysql)546 static int test_wl4166_3(MYSQL *mysql)
547 {
548   int rc;
549   MYSQL_STMT *stmt;
550   MYSQL_BIND my_bind[1];
551   MYSQL_TIME tm[1];
552 
553   if (mysql_get_server_version(mysql) < 50100) {
554     diag("Test requires MySQL Server version 5.1 or above");
555     return SKIP;
556   }
557 
558   rc= mysql_query(mysql, "drop table if exists t1");
559   check_mysql_rc(rc, mysql);
560 
561   rc= mysql_query(mysql, "create table t1 (year datetime)");
562   check_mysql_rc(rc, mysql);
563 
564   stmt= mysql_stmt_init(mysql);
565   FAIL_IF(!stmt, mysql_error(mysql));
566   rc= mysql_stmt_prepare(stmt, SL("insert into t1 (year) values (?)"));
567   check_stmt_rc(rc, stmt);
568 
569   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "param_count != 1");
570 
571   memset(my_bind, '\0', sizeof(my_bind));
572   my_bind[0].buffer_type= MYSQL_TYPE_DATETIME;
573   my_bind[0].buffer= &tm[0];
574 
575   rc= mysql_stmt_bind_param(stmt, my_bind);
576   check_stmt_rc(rc, stmt);
577 
578   tm[0].year= 2014;
579   tm[0].month= 1; tm[0].day= 1;
580   tm[0].hour= 1; tm[0].minute= 1; tm[0].second= 1;
581   tm[0].second_part= 0; tm[0].neg= 0;
582 
583   /* Cause a statement reprepare */
584   rc= mysql_query(mysql, "alter table t1 add column c int");
585   check_mysql_rc(rc, mysql);
586 
587   rc= mysql_stmt_execute(stmt);
588   diag("rc=%d %s", rc, mysql_stmt_error(stmt));
589   check_stmt_rc(rc, stmt);
590 
591   if (verify_col_data(mysql, "t1", "year", "2014-01-01 01:01:01")) {
592     mysql_stmt_close(stmt);
593     rc= mysql_query(mysql, "drop table t1");
594     return FAIL;
595   }
596 
597   mysql_stmt_close(stmt);
598 
599   rc= mysql_query(mysql, "drop table t1");
600   check_mysql_rc(rc, mysql);
601   return OK;
602 }
603 
604 
605 /**
606   Test that long data parameters, as well as parameters
607   that were originally in a different character set, are
608   preserved in case of reprepare.
609 */
610 
test_wl4166_4(MYSQL * mysql)611 static int test_wl4166_4(MYSQL *mysql)
612 {
613   MYSQL_STMT *stmt;
614   int rc;
615   const char *stmt_text;
616   MYSQL_BIND bind_array[2];
617 
618   /* Represented as numbers to keep UTF8 tools from clobbering them. */
619   const char *koi8= "\xee\xd5\x2c\x20\xda\xc1\x20\xd2\xd9\xc2\xc1\xcc\xcb\xd5";
620   const char *cp1251= "\xcd\xf3\x2c\x20\xe7\xe0\x20\xf0\xfb\xe1\xe0\xeb\xea\xf3";
621   char buf1[16], buf2[16];
622   ulong buf1_len, buf2_len;
623 
624   if (mysql_get_server_version(mysql) < 50100) {
625     diag("Test requires MySQL Server version 5.1 or above");
626     return SKIP;
627   }
628 
629   rc= mysql_query(mysql, "drop table if exists t1");
630   check_mysql_rc(rc, mysql);
631 
632   /*
633     Create table with binary columns, set session character set to cp1251,
634     client character set to koi8, and make sure that there is conversion
635     on insert and no conversion on select
636   */
637   rc= mysql_query(mysql,
638                   "create table t1 (c1 varbinary(255), c2 varbinary(255))");
639   check_mysql_rc(rc, mysql);
640   rc= mysql_query(mysql, "set character_set_client=koi8r, "
641                          "character_set_connection=cp1251, "
642                          "character_set_results=koi8r");
643   check_mysql_rc(rc, mysql);
644 
645   memset(bind_array, '\0', sizeof(bind_array));
646 
647   bind_array[0].buffer_type= MYSQL_TYPE_STRING;
648 
649   bind_array[1].buffer_type= MYSQL_TYPE_STRING;
650   bind_array[1].buffer= (void *) koi8;
651   bind_array[1].buffer_length= (unsigned long)strlen(koi8);
652 
653   stmt= mysql_stmt_init(mysql);
654   check_stmt_rc(rc, stmt);
655 
656   stmt_text= "insert into t1 (c1, c2) values (?, ?)";
657 
658   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
659   check_stmt_rc(rc, stmt);
660 
661   mysql_stmt_bind_param(stmt, bind_array);
662 
663   mysql_stmt_send_long_data(stmt, 0, koi8, (unsigned long)strlen(koi8));
664 
665   /* Cause a reprepare at statement execute */
666   rc= mysql_query(mysql, "alter table t1 add column d int");
667   check_mysql_rc(rc, mysql);
668 
669   rc= mysql_stmt_execute(stmt);
670   check_stmt_rc(rc, stmt);
671 
672   stmt_text= "select c1, c2 from t1";
673 
674   /* c1 and c2 are binary so no conversion will be done on select */
675   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
676   check_stmt_rc(rc, stmt);
677 
678   rc= mysql_stmt_execute(stmt);
679   check_stmt_rc(rc, stmt);
680 
681   bind_array[0].buffer= buf1;
682   bind_array[0].buffer_length= sizeof(buf1);
683   bind_array[0].length= &buf1_len;
684 
685   bind_array[1].buffer= buf2;
686   bind_array[1].buffer_length= sizeof(buf2);
687   bind_array[1].length= &buf2_len;
688 
689   mysql_stmt_bind_result(stmt, bind_array);
690 
691   rc= mysql_stmt_fetch(stmt);
692   check_stmt_rc(rc, stmt);
693 
694   FAIL_UNLESS(buf1_len == strlen(cp1251), "");
695   FAIL_UNLESS(buf2_len == strlen(cp1251), "");
696   FAIL_UNLESS(!memcmp(buf1, cp1251, buf1_len), "");
697   FAIL_UNLESS(!memcmp(buf2, cp1251, buf1_len), "");
698 
699   rc= mysql_stmt_fetch(stmt);
700   FAIL_UNLESS(rc == MYSQL_NO_DATA, "");
701 
702   mysql_stmt_close(stmt);
703 
704   rc= mysql_query(mysql, "drop table t1");
705   check_mysql_rc(rc, mysql);
706   rc= mysql_query(mysql, "set names default");
707   check_mysql_rc(rc, mysql);
708   return OK;
709 }
710 
711 /**
712   Test that COM_REFRESH issues a implicit commit.
713 */
714 
test_wl4284_1(MYSQL * mysql)715 static int test_wl4284_1(MYSQL *mysql)
716 {
717   int rc;
718   MYSQL_ROW row;
719   MYSQL_RES *result;
720 
721   diag("Test temporarily disabled");
722   return SKIP;
723 
724   if (mysql_get_server_version(mysql) < 60000) {
725     diag("Test requires MySQL Server version 6.0 or above");
726     return SKIP;
727   }
728 
729   /* set AUTOCOMMIT to OFF */
730   rc= mysql_autocommit(mysql, FALSE);
731   check_mysql_rc(rc, mysql);
732 
733   rc= mysql_query(mysql, "DROP TABLE IF EXISTS trans");
734   check_mysql_rc(rc, mysql);
735 
736   rc= mysql_query(mysql, "CREATE TABLE trans (a INT) ENGINE=InnoDB");
737 
738   if (mysql_errno(mysql) == ER_UNKNOWN_STORAGE_ENGINE)
739   {
740     diag("InnoDB not configured or available");
741     return SKIP;
742   }
743 
744   check_mysql_rc(rc, mysql);
745 
746 
747   rc= mysql_query(mysql, "INSERT INTO trans VALUES(1)");
748   check_mysql_rc(rc, mysql);
749 
750   rc= mysql_refresh(mysql, REFRESH_GRANT | REFRESH_TABLES);
751   check_mysql_rc(rc, mysql);
752 
753   rc= mysql_rollback(mysql);
754   check_mysql_rc(rc, mysql);
755 
756   rc= mysql_query(mysql, "SELECT * FROM trans");
757   check_mysql_rc(rc, mysql);
758 
759   result= mysql_use_result(mysql);
760   FAIL_IF(!result, "Invalid result set");
761 
762   row= mysql_fetch_row(result);
763   FAIL_IF(!row, "Can't fetch row");
764 
765   mysql_free_result(result);
766 
767   /* set AUTOCOMMIT to OFF */
768   rc= mysql_autocommit(mysql, FALSE);
769   check_mysql_rc(rc, mysql);
770 
771   rc= mysql_query(mysql, "DROP TABLE trans");
772   check_mysql_rc(rc, mysql);
773 
774   return OK;
775 }
776 
test_bug49694(MYSQL * mysql)777 static int test_bug49694(MYSQL *mysql)
778 {
779   int rc;
780   MYSQL_RES *res;
781   MYSQL_ROW row;
782   int i;
783   FILE *fp;
784 
785   diag("Load local infile server : %ld", (mysql->server_capabilities & CLIENT_LOCAL_FILES));
786   diag("Load local infile client : %ld", (mysql->client_flag & CLIENT_LOCAL_FILES));
787 
788   SKIP_LOAD_INFILE_DISABLE;
789   SKIP_SKYSQL;
790 
791   rc= mysql_query(mysql, "select @@LOCAL_INFILE");
792   check_mysql_rc(rc, mysql);
793   res= mysql_store_result(mysql);
794   row= mysql_fetch_row(res);
795   if (atol(row[0]) == 0) {
796       diag("Load local infile disable");
797       return SKIP;
798   }
799 
800   rc= mysql_query(mysql, "DROP TABLE IF EXISTS enclist");
801   check_mysql_rc(rc, mysql);
802 
803   rc= mysql_query(mysql, "CREATE TABLE `enclist` ("
804                          "  `pat_id` int(11) NOT NULL,"
805                          "  `episode_id` int(11) NOT NULL,"
806                          "  `enc_id` double NOT NULL,"
807                          "  PRIMARY KEY (`pat_id`,`episode_id`,`enc_id`)"
808                          ") ENGINE=MyISAM DEFAULT CHARSET=latin1");
809   check_mysql_rc(rc, mysql);
810 
811   fp= fopen("data.csv", "w");
812   FAIL_IF(!fp, "Can't open data.csv");
813 
814   for (i=0; i < 100; i++)
815     fprintf (fp, "%.08d,%d,%f\r\n", 100 + i, i % 3 + 1, 60000.0 + i/100);
816   fclose(fp);
817 
818   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE 'data.csv' INTO TABLE enclist "
819                          "FIELDS TERMINATED BY '.' LINES TERMINATED BY '\r\n'");
820   check_mysql_rc(rc, mysql);
821 
822   rc= mysql_query(mysql, "DELETE FROM enclist");
823   check_mysql_rc(rc, mysql);
824 
825   FAIL_IF(mysql_affected_rows(mysql) != 100, "Import failure. Expected 2 imported rows");
826 
827   rc= mysql_query(mysql, "DROP TABLE enclist");
828   check_mysql_rc(rc, mysql);
829   mysql_free_result(res);
830   return OK;
831 }
832 
test_conc49(MYSQL * mysql)833 static int test_conc49(MYSQL *mysql)
834 {
835   int rc;
836   MYSQL_RES *res;
837   MYSQL_ROW row;
838 
839   int i;
840   FILE *fp;
841 
842   SKIP_LOAD_INFILE_DISABLE;
843   SKIP_SKYSQL;
844 
845   rc= mysql_query(mysql, "select @@LOCAL_INFILE");
846   check_mysql_rc(rc, mysql);
847   res= mysql_store_result(mysql);
848   row= mysql_fetch_row(res);
849 
850   i= !atol(row[0]);
851   if (i) {
852       diag("Load local infile disable");
853       mysql_free_result(res);
854       return SKIP;
855   }
856   mysql_free_result(res);
857 
858   fp= fopen("./sample.csv", "w");
859   for (i=1; i < 4; i++)
860     fprintf(fp, "\"%d\", \"%d\", \"%d\"\r\n", i, i, i);
861   fclose(fp);
862   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc49");
863   check_mysql_rc(rc, mysql);
864   rc= mysql_query(mysql, "CREATE TABLE conc49 (a int, b int, c int) Engine=InnoDB DEFAULT CHARSET=latin1");
865   check_mysql_rc(rc, mysql);
866   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'");
867   check_mysql_rc(rc, mysql);
868 
869   rc= mysql_query(mysql, "SELECT a FROM conc49");
870   check_mysql_rc(rc, mysql);
871   res= mysql_store_result(mysql);
872   rc= (int)mysql_num_rows(res);
873   mysql_free_result(res);
874   FAIL_IF(rc != 3, "3 rows expected");
875   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc49");
876   check_mysql_rc(rc, mysql);
877   return OK;
878 }
879 
test_ldi_path(MYSQL * mysql)880 static int test_ldi_path(MYSQL *mysql)
881 {
882   int rc;
883 
884   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
885   check_mysql_rc(rc, mysql);
886 
887   rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
888   check_mysql_rc(rc, mysql);
889 
890   rc= mysql_query(mysql, "FLUSH TABLES");
891   check_mysql_rc(rc, mysql);
892 
893 #ifdef _WIN32
894   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE 'X:/non_existing_path/data.csv' INTO TABLE t1 "
895                          "FIELDS TERMINATED BY '.' LINES TERMINATED BY '\r\n'");
896 #else
897   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE '/non_existing_path/data.csv' INTO TABLE t1 "
898                          "FIELDS TERMINATED BY '.' LINES TERMINATED BY '\r\n'");
899 #endif
900   FAIL_IF(rc== 0, "Error expected");
901   diag("Error: %d", mysql_errno(mysql));
902   FAIL_IF(mysql_errno(mysql) == 0, "Error expected");
903 
904   rc= mysql_query(mysql, "DROP TABLE t1");
905   check_mysql_rc(rc, mysql);
906   return OK;
907 }
908 
909 #if _WIN32
test_conc44(MYSQL * mysql)910 static int test_conc44(MYSQL *mysql)
911 {
912   char query[1024];
913   char *a_filename= "æøå.csv";
914   int rc;
915   int i;
916   FILE *fp;
917 
918   rc= mysql_set_character_set(mysql, "latin1");
919   check_mysql_rc(rc, mysql);
920 
921   rc= mysql_query(mysql, "DROP TABLE IF EXISTS enclist");
922   check_mysql_rc(rc, mysql);
923 
924   rc= mysql_query(mysql, "CREATE TABLE `enclist` ("
925                          "  `pat_id` int(11) NOT NULL,"
926                          "  `episode_id` int(11) NOT NULL,"
927                          "  `enc_id` double NOT NULL,"
928                          "  PRIMARY KEY (`pat_id`,`episode_id`,`enc_id`)"
929                          ") ENGINE=MyISAM DEFAULT CHARSET=latin1");
930   check_mysql_rc(rc, mysql);
931 
932   fp= fopen(a_filename, "w");
933   FAIL_IF(!fp, "Can't open file");
934 
935   for (i=0; i < 100; i++)
936     fprintf (fp, "%.08d,%d,%f\r\n", 100 + i, i % 3 + 1, 60000.0 + i/100);
937   fclose(fp);
938 
939   sprintf(query, "LOAD DATA LOCAL INFILE '%s' INTO TABLE enclist "
940                          "FIELDS TERMINATED BY '.' LINES TERMINATED BY '\r\n'", a_filename);
941   rc= mysql_query(mysql, query);
942   check_mysql_rc(rc, mysql);
943 
944   rc= mysql_query(mysql, "DELETE FROM enclist");
945   check_mysql_rc(rc, mysql);
946 
947   FAIL_IF(mysql_affected_rows(mysql) != 100, "Import failure. Expected 2 imported rows");
948 
949   rc= mysql_query(mysql, "DROP TABLE enclist");
950   check_mysql_rc(rc, mysql);
951   return OK;
952 }
953 #endif
954 
test_connect_attrs(MYSQL * my)955 static int test_connect_attrs(MYSQL *my)
956 {
957   MYSQL *mysql;
958   MYSQL_RES *result;
959   int rc, len;
960 
961   rc= mysql_query(my, "SELECT * FROM performance_schema.session_connect_attrs LIMIT 1");
962   if (rc != 0)
963   {
964     diag("Server doesn't support connection attributes");
965     return SKIP;
966   }
967 
968   result= mysql_store_result(my);
969   /* MariaDB Connector/C already sent connection attrs after handshake. So if the table is
970      empty, it indicates that the performance schema is disabled */
971   if (!mysql_num_rows(result))
972   {
973     diag("skip: performance_schema not enabled");
974     mysql_free_result(result);
975     return SKIP;
976   }
977   mysql_free_result(result);
978 
979   mysql= mysql_init(NULL);
980 
981   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo0", "bar0");
982   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo1", "bar1");
983   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo2", "bar2");
984 
985   FAIL_IF(!my_test_connect(mysql, hostname, username, password, schema,
986                          port, socketname, 0), mysql_error(my));
987 
988   if (!(mysql->server_capabilities & CLIENT_CONNECT_ATTRS))
989   {
990     diag("Server doesn't support connection attributes");
991     return SKIP;
992   }
993 
994   rc= mysql_query(mysql, "SELECT * FROM performance_schema.session_connect_attrs where attr_name like 'foo%'");
995   check_mysql_rc(rc, mysql);
996   result= mysql_store_result(mysql);
997   rc= (int)mysql_num_rows(result);
998   mysql_free_result(result);
999 
1000   mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_RESET, NULL);
1001   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo0", "bar0");
1002   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo1", "bar1");
1003   mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "foo2", "bar2");
1004   mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "foo0");
1005   mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "foo1");
1006   mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "foo2");
1007 
1008   len= (int)mysql->options.extension->connect_attrs_len;
1009 
1010   mysql_close(mysql);
1011 
1012   FAIL_IF(rc < 3, "Expected 3 or more rows");
1013   FAIL_IF(len != 0, "Expected connection_attr_len=0");
1014 
1015   return OK;
1016 }
1017 
test_conc_114(MYSQL * mysql)1018 static int test_conc_114(MYSQL *mysql)
1019 {
1020   if (mysql_client_find_plugin(mysql, "foo", 0))
1021   {
1022     diag("Null pointer expected");
1023     return FAIL;
1024   }
1025   diag("Error: %s", mysql_error(mysql));
1026   return OK;
1027 }
1028 
1029 /* run with valgrind */
test_conc117(MYSQL * unused)1030 static int test_conc117(MYSQL *unused __attribute__((unused)))
1031 {
1032   my_bool reconnect= 1;
1033   MYSQL *my= mysql_init(NULL);
1034   SKIP_MAXSCALE;
1035   FAIL_IF(!my_test_connect(my, hostname, username, password, schema,
1036                          port, socketname, 0), mysql_error(my));
1037 
1038   mysql_kill(my, mysql_thread_id(my));
1039 
1040   mysql_options(my, MYSQL_OPT_RECONNECT, &reconnect);
1041 
1042   mysql_query(my, "SET @a:=1");
1043   mysql_close(my);
1044 
1045   return OK;
1046 }
1047 
test_read_timeout(MYSQL * unused)1048 static int test_read_timeout(MYSQL *unused __attribute__((unused)))
1049 {
1050   int timeout= 5, rc;
1051   MYSQL *my= mysql_init(NULL);
1052   SKIP_MAXSCALE;
1053   mysql_options(my, MYSQL_OPT_READ_TIMEOUT, &timeout);
1054   FAIL_IF(!my_test_connect(my, hostname, username, password, schema,
1055                          port, socketname, 0), mysql_error(my));
1056 
1057   rc= mysql_query(my, "SELECT SLEEP(50)");
1058 
1059   FAIL_IF(rc == 0, "error expected");
1060   diag("error: %s", mysql_error(my));
1061 
1062   mysql_close(my);
1063 
1064   return OK;
1065 }
1066 
1067 #ifdef HAVE_REMOTEIO
1068 void *remote_plugin;
test_remote1(MYSQL * mysql)1069 static int test_remote1(MYSQL *mysql)
1070 {
1071   int rc;
1072   MYSQL_RES *res;
1073   MYSQL_ROW row;
1074   SKIP_SKYSQL;
1075 
1076   SKIP_SKYSQL;
1077 
1078   remote_plugin= (void *)mysql_client_find_plugin(mysql, "remote_io", MARIADB_CLIENT_REMOTEIO_PLUGIN);
1079   if (!remote_plugin)
1080   {
1081     diag("skip - no remote io plugin available");
1082     diag("error: %s", mysql_error(mysql));
1083     return SKIP;
1084   }
1085 
1086   SKIP_LOAD_INFILE_DISABLE;
1087 
1088   rc= mysql_query(mysql, "select @@LOCAL_INFILE");
1089   check_mysql_rc(rc, mysql);
1090   res= mysql_store_result(mysql);
1091   row= mysql_fetch_row(res);
1092   if (atol(row[0]) == 0) {
1093       diag("Load local infile disable");
1094       return SKIP;
1095   }
1096   mysql_free_result(res);
1097 
1098   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1099   check_mysql_rc(rc, mysql);
1100 
1101   rc= mysql_query(mysql, "CREATE TABLE t1 (a text)");
1102   check_mysql_rc(rc, mysql);
1103 
1104   rc= mysql_query(mysql, "LOAD DATA LOCAL INFILE 'http://www.example.com' INTO TABLE t1");
1105   if (rc && mysql_errno(mysql) == 2058)
1106   {
1107     diag("remote_io plugin not available");
1108     return SKIP;
1109   }
1110   check_mysql_rc(rc, mysql);
1111   return OK;
1112 }
1113 
test_remote2(MYSQL * my)1114 static int test_remote2(MYSQL *my)
1115 {
1116   MYSQL *mysql;
1117 
1118   if (!remote_plugin)
1119   {
1120     diag("skip - no remote io plugin available");
1121     return SKIP;
1122   }
1123   mysql= mysql_init(NULL);
1124 
1125   mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, "http://localhost/test.cnf");
1126   mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "test");
1127   my_test_connect(mysql, hostname, username, password, schema,
1128                          0, socketname, 0), mysql_error(my);
1129   diag("port: %d", mysql->port);
1130   mysql_close(mysql);
1131   return OK;
1132 }
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, port, 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, port, 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, port, 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 
test_conc457(MYSQL * mysql)1502 static int test_conc457(MYSQL *mysql)
1503 {
1504   MYSQL_RES *result;
1505 
1506   SKIP_MYSQL(mysql);
1507 
1508   result= mysql_list_processes(mysql);
1509 
1510   FAIL_IF(mysql_field_count(mysql) != 9, "expected 9 columns");
1511   mysql_free_result(result);
1512   return OK;
1513 }
1514 
test_conc458(MYSQL * my)1515 static int test_conc458(MYSQL *my __attribute__((unused)))
1516 {
1517   MYSQL *mysql= mysql_init(NULL);
1518   FAIL_IF(mysql_get_timeout_value(mysql) != 0, "expected timeout 0");
1519   mysql_close(mysql);
1520   return OK;
1521 }
1522 
1523 
test_conc533(MYSQL * mysql)1524 static int test_conc533(MYSQL *mysql)
1525 {
1526   my_bool skip= 1;
1527   int rc;
1528   MYSQL_RES *result;
1529   MYSQL_ROW row;
1530   MYSQL_STMT *stmt;
1531   MYSQL_BIND bind[1];
1532   char buffer[10];
1533 
1534   rc= mysql_options(mysql, MARIADB_OPT_SKIP_READ_RESPONSE, &skip);
1535 
1536   rc= mysql_real_query(mysql, SL("SELECT 1"));
1537   check_mysql_rc(rc, mysql);
1538 
1539   rc= mysql->methods->db_read_query_result(mysql);
1540   check_mysql_rc(rc, mysql);
1541 
1542   result= mysql_store_result(mysql);
1543   row= mysql_fetch_row(result);
1544 
1545   FAIL_IF(strcmp(row[0], "1"), "Expected value \"1\"");
1546   mysql_free_result(result);
1547 
1548   stmt= mysql_stmt_init(mysql);
1549   rc= mysql_stmt_prepare(stmt, SL("SELECT 1"));
1550   check_stmt_rc(rc, stmt);
1551 
1552   rc= mysql->methods->db_read_prepare_response(stmt);
1553   check_stmt_rc(rc, stmt);
1554 
1555   FAIL_IF(mysql_stmt_field_count(stmt) != 1, "Expected field_count= 1");
1556 
1557   rc= mysql_stmt_execute(stmt);
1558   check_stmt_rc(rc, stmt);
1559 
1560   rc= mysql->methods->db_read_execute_response(stmt);
1561   check_stmt_rc(rc, stmt);
1562 
1563   memset(bind, 0, sizeof(MYSQL_BIND));
1564   bind[0].buffer= buffer;
1565   bind[0].buffer_type= MYSQL_TYPE_STRING;
1566   bind[0].buffer_length= 10;
1567 
1568   rc= mysql_stmt_bind_result(stmt, bind);
1569   check_stmt_rc(rc, stmt);
1570 
1571   rc= mysql_stmt_fetch(stmt);
1572   check_stmt_rc(rc, stmt);
1573 
1574   FAIL_IF(strcmp(buffer, "1"), "Expected value \"1\"");
1575 
1576   mysql_stmt_close(stmt);
1577 
1578   return OK;
1579 }
1580 
display_extended_field_attribute(MYSQL * mysql)1581 int display_extended_field_attribute(MYSQL *mysql)
1582 {
1583   MYSQL_RES *result;
1584   MYSQL_FIELD *fields;
1585 
1586   if (mysql_query(mysql, "CREATE TEMPORARY TABLE t1 (a POINT)"))
1587     return 1;
1588 
1589   if (mysql_query(mysql, "SELECT a FROM t1"))
1590     return 1;
1591 
1592   if (!(result= mysql_store_result(mysql)))
1593     return 1;
1594 
1595   if ((fields= mysql_fetch_fields(result)))
1596   {
1597     MARIADB_CONST_STRING field_attr;
1598 
1599     if (!mariadb_field_attr(&field_attr, &fields[0],
1600                             MARIADB_FIELD_ATTR_DATA_TYPE_NAME))
1601     {
1602       printf("Extended field attribute: %s\n", field_attr.str);
1603     }
1604   }
1605   mysql_free_result(result);
1606   return 0;
1607 }
1608 
1609 
test_ext_field_attr(MYSQL * mysql)1610 static int test_ext_field_attr(MYSQL *mysql)
1611 {
1612   display_extended_field_attribute(mysql);
1613 
1614   return OK;
1615 }
1616 
1617 struct my_tests_st my_tests[] = {
1618   {"test_ext_field_attr", test_ext_field_attr, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1619   {"test_conc533", test_conc533, TEST_CONNECTION_NEW, 0, NULL, NULL},
1620   {"test_conc458", test_conc458, TEST_CONNECTION_NONE, 0, NULL, NULL},
1621   {"test_conc457", test_conc457, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1622   {"test_conc384", test_conc384, TEST_CONNECTION_NONE, 0, NULL, NULL},
1623 #ifndef _WIN32
1624   {"test_mdev12965", test_mdev12965, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1625   {"test_conc395", test_conc395, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1626   {"test_sslenforce", test_sslenforce, TEST_CONNECTION_NONE, 0, NULL, NULL},
1627 #endif
1628   {"test_wl6797", test_wl6797, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1629   {"test_server_status", test_server_status, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1630   {"test_read_timeout", test_read_timeout, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1631   {"test_zerofill", test_zerofill, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1632 #ifdef HAVE_REMOTEIO
1633   {"test_remote1", test_remote1, TEST_CONNECTION_NEW, 0, NULL, NULL},
1634   {"test_remote2", test_remote2, TEST_CONNECTION_NEW, 0, NULL, NULL},
1635 #endif
1636   {"test_get_info", test_get_info, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1637   {"test_conc117", test_conc117, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1638   {"test_conc_114", test_conc_114, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1639   {"test_connect_attrs", test_connect_attrs, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1640   {"test_conc49", test_conc49, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1641   {"test_bug28075", test_bug28075, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1642   {"test_bug28505", test_bug28505, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1643   {"test_debug_example", test_debug_example, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1644   {"test_bug29692", test_bug29692, TEST_CONNECTION_NEW, CLIENT_FOUND_ROWS,  NULL, NULL},
1645   {"test_bug31418", test_bug31418, TEST_CONNECTION_DEFAULT, 0,  NULL, NULL},
1646   {"test_frm_bug", test_frm_bug, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1647   {"test_wl4166_1", test_wl4166_1, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1648   {"test_wl4166_2", test_wl4166_2, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1649   {"test_wl4166_3", test_wl4166_3, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1650   {"test_wl4166_4", test_wl4166_4, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1651   {"test_wl4284_1", test_wl4284_1, TEST_CONNECTION_NEW, 0,  NULL, NULL},
1652   {"test_bug49694", test_bug49694, TEST_CONNECTION_NEW, 0, NULL, NULL},
1653   {"test_ldi_path", test_ldi_path, TEST_CONNECTION_NEW, 0, NULL, NULL},
1654 #ifdef _WIN32
1655   {"test_conc44", test_conc44, TEST_CONNECTION_NEW, 0, NULL, NULL},
1656 #endif
1657   {NULL, NULL, 0, 0, NULL, 0}
1658 };
1659 
1660 
main(int argc,char ** argv)1661 int main(int argc, char **argv)
1662 {
1663   if (argc > 1)
1664     get_options(argc, argv);
1665 
1666   get_envvars();
1667 
1668   run_tests(my_tests);
1669 
1670   return(exit_status());
1671 }
1672