"Fossies" - the Fresh Open Source Software Archive

Member "mariadb-connector-c-3.0.8-src/unittest/libmariadb/connection.c" (18 Dec 2018, 48362 Bytes) of package /linux/misc/mariadb-connector-c-3.0.8-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "connection.c": 3.0.7_vs_3.0.8.

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