Fix test 'prepare_statement_err3024' and add new regression tests

This commit packs fixes for test 'prepare_statement_err3024' and also
adds regression tests for:
  - ProxySQL handling of binary resultsets with ERR packets.
  - Fix for 'mysql_stmt_store_result' from 'libmariadbclient'.
pull/4016/head
Javier Jaramago Fernández 3 years ago
parent 68f9c63cea
commit b3da7ee68c

@ -101,7 +101,8 @@ OPT=-O2 $(WGCOV) -Wl,--no-as-needed
debug: OPT=-O0 -DDEBUG -ggdb -Wl,--no-as-needed $(WGCOV) $(WASAN)
debug: tests
tests: $(patsubst %.cpp,%,$(wildcard *-t.cpp)) setparser_test reg_test_3504-change_user_libmariadb_helper reg_test_3504-change_user_libmysql_helper set_testing-240.csv test_clickhouse_server_libmysql clickhouse_php_conn-t
tests: $(patsubst %.cpp,%,$(wildcard *-t.cpp)) setparser_test reg_test_3504-change_user_libmariadb_helper reg_test_3504-change_user_libmysql_helper set_testing-240.csv test_clickhouse_server_libmysql clickhouse_php_conn-t \
reg_test_stmt_resultset_err_no_rows_libmysql prepare_statement_err3024_libmysql prepare_statement_err3024_async reg_test_mariadb_stmt_store_result_libmysql reg_test_mariadb_stmt_store_result_async
testgalera: galera_1_timeout_count galera_2_timeout_no_count
testaurora: aurora
@ -156,3 +157,18 @@ reg_test_3504-change_user_libmysql_helper: reg_test_3504-change_user_helper.cpp
test_clickhouse_server_libmysql: test_clickhouse_server-t.cpp
$(CXX) -DLIBMYSQL_HELPER -DDEBUG test_clickhouse_server-t.cpp -I/usr/include/mysql -I$(IDIR) -I$(JSON_IDIR) -I../tap -L$(TAP_LIBDIR) -lpthread -ldl -std=c++11 -ltap -lmysqlclient -o test_clickhouse_server_libmysql-t -DGITVERSION=\"$(GIT_VERSION)\"
reg_test_stmt_resultset_err_no_rows_libmysql: reg_test_stmt_resultset_err_no_rows-t.cpp
$(CXX) -DLIBMYSQL_HELPER reg_test_stmt_resultset_err_no_rows-t.cpp -I/usr/include/mysql -I$(IDIR) -I$(JSON_IDIR) -I../tap $(OPT) -L$(TAP_LIBDIR) -lpthread -ldl -std=c++11 -ltap -lmysqlclient -o reg_test_stmt_resultset_err_no_rows_libmysql-t -DGITVERSION=\"$(GIT_VERSION)\"
reg_test_mariadb_stmt_store_result_libmysql: reg_test_mariadb_stmt_store_result-t.cpp $(TAP_LIBDIR)/libtap.a
$(CXX) -DLIBMYSQL_HELPER reg_test_mariadb_stmt_store_result-t.cpp -I/usr/include/mysql -I$(IDIR) -I$(JSON_IDIR) -I../tap $(OPT) -L$(TAP_LIBDIR) -lpthread -ldl -std=c++11 -ltap -lmysqlclient -o reg_test_mariadb_stmt_store_result_libmysql-t -DGITVERSION=\"$(GIT_VERSION)\"
reg_test_mariadb_stmt_store_result_async: reg_test_mariadb_stmt_store_result-t.cpp $(TAP_LIBDIR)/libtap.a
$(CXX) -DASYNC_API reg_test_mariadb_stmt_store_result-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) $(MYLIBS) -lpthread -ldl -std=c++11 -ltap $(STATIC_LIBS) -o reg_test_mariadb_stmt_store_result_async-t -DGITVERSION=\"$(GIT_VERSION)\"
prepare_statement_err3024_libmysql: prepare_statement_err3024-t.cpp $(TAP_LIBDIR)/libtap.a
$(CXX) -DLIBMYSQL_HELPER prepare_statement_err3024-t.cpp -I/usr/include/mysql -I$(IDIR) -I$(JSON_IDIR) -I../tap $(OPT) -L$(TAP_LIBDIR) -lpthread -ldl -std=c++11 -ltap -lmysqlclient -o prepare_statement_err3024_libmysql-t -DGITVERSION=\"$(GIT_VERSION)\"
prepare_statement_err3024_async: prepare_statement_err3024-t.cpp $(TAP_LIBDIR)/libtap.a
$(CXX) -DASYNC_API prepare_statement_err3024-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) $(MYLIBS) -lpthread -ldl -std=c++11 -ltap $(STATIC_LIBS) -o prepare_statement_err3024_async-t -DGITVERSION=\"$(GIT_VERSION)\"

@ -1,12 +1,21 @@
#include <vector>
/**
* @file prepare_statement_err3024-t.cpp
* @brief Checks proper handling by ProxySQL of binary resultset holding errors.
* @details This test, performs a mixed set of queries, some of them expecting errors. The test is also
* compiled against 'libmysqlclient' and also makes use of both ASYNC and SYNC APIs.
*/
#include <cstring>
#include <poll.h>
#include <string>
#include <stdio.h>
#include <cstring>
#include <unistd.h>
#include <vector>
#include <mysql.h>
#include "tap.h"
#include "command_line.h"
#include "proxysql_utils.h"
#include "utils.h"
const int NUM_EXECUTIONS = 5;
@ -17,10 +26,108 @@ std::string select_query[3] = {
"SELECT a.* FROM test.sbtest1 a JOIN test.sbtest1 b WHERE (a.id+b.id)%2 LIMIT 10000"
};
#ifndef LIBMYSQL_HELPER
/* Helper function to do the waiting for events on the socket. */
static int wait_for_mysql(MYSQL *mysql, int status) {
struct pollfd pfd;
int timeout, res;
pfd.fd = mysql_get_socket(mysql);
pfd.events =
(status & MYSQL_WAIT_READ ? POLLIN : 0) |
(status & MYSQL_WAIT_WRITE ? POLLOUT : 0) |
(status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0);
if (status & MYSQL_WAIT_TIMEOUT)
timeout = 1000*mysql_get_timeout_value(mysql);
else
timeout = -1;
res = poll(&pfd, 1, timeout);
if (res == 0)
return MYSQL_WAIT_TIMEOUT;
else if (res < 0)
return MYSQL_WAIT_TIMEOUT;
else {
int status = 0;
if (pfd.revents & POLLIN) status |= MYSQL_WAIT_READ;
if (pfd.revents & POLLOUT) status |= MYSQL_WAIT_WRITE;
if (pfd.revents & POLLPRI) status |= MYSQL_WAIT_EXCEPT;
return status;
}
}
#endif
/**
* @brief Function is required to be duplicated in the test because multiple compilations using
* 'libmysqlclient' and 'libmariadbclient'. TODO: Being able to share helper functions targetting different
* connector libraries between tap tests.
*/
int add_more_rows_test_sbtest1(int num_rows, MYSQL *mysql, bool sqlite) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0.0, 9.0);
diag("Creating %d rows in sbtest1", num_rows);
while (num_rows) {
std::stringstream q;
if (sqlite==false) {
q << "INSERT INTO test.sbtest1 (k, c, pad) values ";
} else {
q << "INSERT INTO sbtest1 (k, c, pad) values ";
}
bool put_comma = false;
int i=0;
unsigned int cnt=5+rand()%50;
if (cnt > num_rows) cnt = num_rows;
for (i=0; i<cnt ; ++i) {
num_rows--;
int k = dist(mt);
std::stringstream c;
for (int j=0; j<10; j++) {
for (int k=0; k<11; k++) {
c << dist(mt);
}
if (j<9)
c << "-";
}
std::stringstream pad;
for (int j=0; j<5; j++) {
for (int k=0; k<11; k++) {
pad << dist(mt);
}
if (j<4)
pad << "-";
}
if (put_comma) q << ",";
if (!put_comma) put_comma=true;
q << "(" << k << ",'" << c.str() << "','" << pad.str() << "')";
}
MYSQL_QUERY(mysql, q.str().c_str());
diag("Inserted %d rows ...", i);
}
diag("Done!");
return 0;
}
/**
* @brief Function is required to be duplicated in the test because multiple compilations using
* 'libmysqlclient' and 'libmariadbclient'. TODO: Being able to share helper functions targetting different
* connector libraries between tap tests.
*/
int create_table_test_sbtest1(int num_rows, MYSQL *mysql) {
MYSQL_QUERY(mysql, "CREATE DATABASE IF NOT EXISTS test");
MYSQL_QUERY(mysql, "DROP TABLE IF EXISTS test.sbtest1");
MYSQL_QUERY(mysql, "CREATE TABLE if not exists test.sbtest1 (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `k` int(10) unsigned NOT NULL DEFAULT '0', `c` char(120) NOT NULL DEFAULT '', `pad` char(60) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `k_1` (`k`))");
return add_more_rows_test_sbtest1(num_rows, mysql);
}
using std::string;
int main(int argc, char** argv) {
CommandLine cl;
plan(3+NUM_EXECUTIONS*3); // 3 prepare + 3*execution
plan(3 + NUM_EXECUTIONS*3*2); // 3 prepare + 3 * execution * 2 (execute + store)
if (cl.getEnv()) {
diag("Failed to get the required environmental variables.");
@ -33,6 +140,10 @@ int main(int argc, char** argv) {
return exit_status();
}
#if defined(ASYNC_API) && !defined(LIBMYSQL_HELPER)
mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
#endif
if (!mysql_real_connect(mysql, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql));
return exit_status();
@ -70,14 +181,48 @@ int main(int argc, char** argv) {
// the others should succeed
diag("Executing: %s", select_query[i].c_str());
rc = mysql_stmt_execute(stmt[i]);
if (i==0) { // this should fail
unsigned int sterrno = mysql_stmt_errno(stmt[i]); // we expect error 3024
ok(rc==1 && sterrno==3024, "mysql_stmt_execute at line %d should fail with code 3024. Received code: %u, error: %s", __LINE__ , sterrno, mysql_stmt_error(stmt[i]));
unsigned int sterrno = mysql_stmt_errno(stmt[i]);
const char* strerr = mysql_stmt_error(stmt[i]);
ok(rc == 0, "'mysql_stmt_execute' should succeed. Code: %u, error: %s", sterrno, strerr);
if (rc == 0) {
#if defined(ASYNC_API) && !defined(LIBMYSQL_HELPER)
diag("Using ASYNC API for 'mysql_stmt_store_result'...");
int async_exit_status = 0;
async_exit_status = mysql_stmt_store_result_start(&rc, stmt[i]);
while (async_exit_status) {
async_exit_status = wait_for_mysql(mysql, async_exit_status);
async_exit_status = mysql_stmt_store_result_cont(&rc, stmt[i], async_exit_status);
}
#else
diag("Using SYNC API for 'mysql_stmt_store_result'...");
rc = mysql_stmt_store_result(stmt[i]);
#endif
bool check_res = false;
string check_err {};
sterrno = mysql_stmt_errno(stmt[i]);
strerr = mysql_stmt_error(stmt[i]);
if (i == 0) {
check_res = rc == 1 && sterrno == 3024;
string check_err_t {
"'mysql_stmt_store_result' at line %d should fail with code 3024. Received code: %u, error: %s"
};
string_format(check_err_t, check_err, __LINE__, sterrno, strerr);
} else {
check_res = rc == 0;
check_err = "'mysql_stmt_store_result' should succeed";
}
ok(check_res, "%s", check_err.c_str());
mysql_stmt_free_result(stmt[i]);
} else {
ok(rc==0, "mysql_stmt_execute should succeed");
diag("'mysql_stmt_execute' failed. Continuing with further tests");
}
rc = mysql_stmt_store_result(stmt[i]);
mysql_stmt_free_result(stmt[i]);
}
}
for (int i=0; i<3; i++) {
@ -85,6 +230,7 @@ int main(int argc, char** argv) {
ok(false, "mysql_stmt_close at line %d failed: %s\n", __LINE__ , mysql_error(mysql));
}
}
mysql_close(mysql);
return exit_status();

@ -0,0 +1,218 @@
/**
* @file reg_test_mariadb_stmt_store_result-t.cpp
* @brief Regression test for 'mysql_stmt_store_result' internal error handling.
* @details When failed to execute, 'mysql_stmt_store_result' left and invalid internal state in 'stmt' which
* leads to stalls when the 'mysql_stmt_execute' was called again over the 'stmt'. This test verifies that
* this behavior is no longer present, for both, ASYNC and SYNC APIs. It also compiles against
* 'libmysqlclient' and exercises the same logic against ProxySQL.
*/
#include <cstring>
#include <poll.h>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <vector>
#include <mysql.h>
#include "tap.h"
#include "command_line.h"
#include "utils.h"
#ifndef LIBMYSQL_HELPER
/* Helper function to do the waiting for events on the socket. */
static int wait_for_mysql(MYSQL *mysql, int status) {
struct pollfd pfd;
int timeout, res;
pfd.fd = mysql_get_socket(mysql);
pfd.events =
(status & MYSQL_WAIT_READ ? POLLIN : 0) |
(status & MYSQL_WAIT_WRITE ? POLLOUT : 0) |
(status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0);
if (status & MYSQL_WAIT_TIMEOUT)
timeout = 1000*mysql_get_timeout_value(mysql);
else
timeout = -1;
res = poll(&pfd, 1, timeout);
if (res == 0)
return MYSQL_WAIT_TIMEOUT;
else if (res < 0)
return MYSQL_WAIT_TIMEOUT;
else {
int status = 0;
if (pfd.revents & POLLIN) status |= MYSQL_WAIT_READ;
if (pfd.revents & POLLOUT) status |= MYSQL_WAIT_WRITE;
if (pfd.revents & POLLPRI) status |= MYSQL_WAIT_EXCEPT;
return status;
}
}
#endif
/**
* @brief Function is required to be duplicated in the test because multiple compilations using
* 'libmysqlclient' and 'libmariadbclient'. TODO: Being able to share helper functions targetting different
* connector libraries between tap tests.
*/
int add_more_rows_test_sbtest1(int num_rows, MYSQL *mysql, bool sqlite) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0.0, 9.0);
diag("Creating %d rows in sbtest1", num_rows);
while (num_rows) {
std::stringstream q;
if (sqlite==false) {
q << "INSERT INTO test.sbtest1 (k, c, pad) values ";
} else {
q << "INSERT INTO sbtest1 (k, c, pad) values ";
}
bool put_comma = false;
int i=0;
unsigned int cnt=5+rand()%50;
if (cnt > num_rows) cnt = num_rows;
for (i=0; i<cnt ; ++i) {
num_rows--;
int k = dist(mt);
std::stringstream c;
for (int j=0; j<10; j++) {
for (int k=0; k<11; k++) {
c << dist(mt);
}
if (j<9)
c << "-";
}
std::stringstream pad;
for (int j=0; j<5; j++) {
for (int k=0; k<11; k++) {
pad << dist(mt);
}
if (j<4)
pad << "-";
}
if (put_comma) q << ",";
if (!put_comma) put_comma=true;
q << "(" << k << ",'" << c.str() << "','" << pad.str() << "')";
}
MYSQL_QUERY(mysql, q.str().c_str());
diag("Inserted %d rows ...", i);
}
diag("Done!");
return 0;
}
/**
* @brief Function is required to be duplicated in the test because multiple compilations using
* 'libmysqlclient' and 'libmariadbclient'. TODO: Being able to share helper functions targetting different
* connector libraries between tap tests.
*/
int create_table_test_sbtest1(int num_rows, MYSQL *mysql) {
MYSQL_QUERY(mysql, "CREATE DATABASE IF NOT EXISTS test");
MYSQL_QUERY(mysql, "DROP TABLE IF EXISTS test.sbtest1");
MYSQL_QUERY(mysql, "CREATE TABLE if not exists test.sbtest1 (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `k` int(10) unsigned NOT NULL DEFAULT '0', `c` char(120) NOT NULL DEFAULT '', `pad` char(60) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `k_1` (`k`))");
return add_more_rows_test_sbtest1(num_rows, mysql);
}
std::string select_query = {
"SELECT /*+ MAX_EXECUTION_TIME(10) */ COUNT(*) FROM test.sbtest1 a JOIN test.sbtest1 b WHERE (a.id+b.id)%2"
};
using std::string;
const int TWO_EXECUTIONS = 2;
int main(int argc, char** argv) {
CommandLine cl;
plan(1 + TWO_EXECUTIONS*2); // 1 prepare + executions * 2 (execute + store)
bool use_async = false;
if (argc == 2 && string { argv[1] } == "async") {
use_async = true;
}
if (cl.getEnv()) {
diag("Failed to get the required environmental variables.");
return -1;
}
MYSQL* mysql = mysql_init(NULL);
if (!mysql) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql));
return exit_status();
}
#if defined(ASYNC_API) && !defined(LIBMYSQL_HELPER)
mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
#endif
if (!mysql_real_connect(mysql, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql));
return exit_status();
}
if (create_table_test_sbtest1(1000,mysql)) {
fprintf(stderr, "File %s, line %d, Error: create_table_test_sbtest1() failed\n", __FILE__, __LINE__);
return exit_status();
}
MYSQL_STMT* stmt = nullptr;
// Initialize and prepare the statement
stmt= mysql_stmt_init(mysql);
if (!stmt) {
diag("mysql_stmt_init(), out of memory\n");
return exit_status();
}
if (mysql_stmt_prepare(stmt, select_query.c_str(), strlen(select_query.c_str()))) {
diag("select_query: %s", select_query.c_str());
ok(false, "mysql_stmt_prepare at line %d failed: %s", __LINE__ , mysql_error(mysql));
mysql_close(mysql);
mysql_library_end();
return exit_status();
} else {
ok(true, "Prepare succeeded: %s", select_query.c_str());
}
int rc = 0;
for (int i = 0; i < TWO_EXECUTIONS; i++) {
diag("Executing: %s", select_query.c_str());
rc = mysql_stmt_execute(stmt);
unsigned int sterrno = mysql_stmt_errno(stmt);
const char* strerr = mysql_stmt_error(stmt);
ok(rc == 0, "'mysql_stmt_execute' should succeed. Code: %u, error: %s", sterrno, strerr);
#if defined(ASYNC_API) && !defined(LIBMYSQL_HELPER)
diag("Using ASYNC API for 'mysql_stmt_store_result'...");
int async_exit_status = 0;
async_exit_status = mysql_stmt_store_result_start(&rc, stmt);
while (async_exit_status) {
async_exit_status = wait_for_mysql(mysql, async_exit_status);
async_exit_status = mysql_stmt_store_result_cont(&rc, stmt, async_exit_status);
}
#else
diag("Using SYNC API for 'mysql_stmt_store_result'...");
rc = mysql_stmt_store_result(stmt);
#endif
sterrno = mysql_stmt_errno(stmt);
strerr = mysql_stmt_error(stmt);
bool check_res = rc == 1 && sterrno == 3024;
ok(check_res, "'mysql_stmt_store_result' should fail. Code: %u, error: %s", sterrno, strerr);
mysql_stmt_free_result(stmt);
}
if (mysql_stmt_close(stmt)) {
ok(false, "mysql_stmt_close at line %d failed: %s\n", __LINE__ , mysql_error(mysql));
}
mysql_close(mysql);
return exit_status();
}

@ -0,0 +1,192 @@
/**
* @file reg_test_stmt_resultset_err_no_rows-t.cpp
* @brief Checks handling of STMT binary resultsets with errors without rows.
* @details This test compiles against 'libmariadb' and 'libmysqlclient'. This testing is completed by
* 'reg_test_stmt_resultset_err_no_rows_php-t.cpp' which performs the same checks but using PHP default
* connector.
*/
#include <iostream>
#include <ctime>
#include <string>
#include <string.h>
#include <stdio.h>
#include <tuple>
#include <unistd.h>
#include <utility>
#include <vector>
#include <mysql.h>
#include <mysql/mysqld_error.h>
#include "proxysql_utils.h"
#include "tap.h"
#include "command_line.h"
#include "utils.h"
using std::string;
using std::vector;
using std::tuple;
using test_case_t = tuple<bool,string,string>;
const uint32_t STRING_SIZE = 1024;
const vector<test_case_t> TEST_CASES {
{true, "$.", ""}, {false, "$", "[\"a\", \"b\"]"}, {true, "$.", ""}, {false, "$.b", "[\"c\"]"}
};
int main(int argc, char** argv) {
int res = EXIT_SUCCESS;
plan(TEST_CASES.size());
CommandLine cl;
if (cl.getEnv()) {
diag("Failed to get the required environmental variables.");
return EXIT_FAILURE;
}
MYSQL* proxy = mysql_init(NULL);
MYSQL* admin = mysql_init(NULL);
#ifdef LIBMYSQL_HELPER
enum mysql_ssl_mode ssl_mode = SSL_MODE_DISABLED;
mysql_options(proxy, MYSQL_OPT_SSL_MODE, &ssl_mode);
#endif
proxy->options.client_flag &= ~CLIENT_DEPRECATE_EOF;
if (!mysql_real_connect(proxy, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxy));
return EXIT_FAILURE;
}
if (!mysql_real_connect(admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(admin));
return EXIT_FAILURE;
}
const string stmt_query { "SELECT json_keys('{\"a\": 0, \"b\": {\"c\": 1}}', ?)" };
for (const test_case_t& test_case : TEST_CASES) {
const bool exp_fail { std::get<0>(test_case) };
const string& param { std::get<1>(test_case) };
const string& exp_res { std::get<2>(test_case) };
MYSQL_STMT* stmt = mysql_stmt_init(proxy);
if (!stmt) {
diag("mysql_stmt_init(), out of memory");
res = EXIT_FAILURE;
goto exit;
}
if (mysql_stmt_prepare(stmt, stmt_query.c_str(), strlen(stmt_query.c_str()))) {
diag("mysql_stmt_prepare at line %d failed: %s", __LINE__ , mysql_error(proxy));
mysql_close(proxy);
res = EXIT_FAILURE;
goto exit;
}
MYSQL_BIND bind_params;
memset(&bind_params, 0, sizeof(MYSQL_BIND));
char str_data[STRING_SIZE] = { 0 };
uint64_t str_length = 0;
bind_params.buffer_type = MYSQL_TYPE_STRING;
bind_params.buffer = static_cast<char*>(str_data);
bind_params.buffer_length = STRING_SIZE;
bind_params.is_null = 0;
bind_params.length = &str_length;
if (mysql_stmt_bind_param(stmt, &bind_params)) {
diag("mysql_stmt_bind_result at line %d failed: '%s'", __LINE__ , mysql_stmt_error(stmt));
res = EXIT_FAILURE;
goto exit;
}
strncpy(str_data, param.c_str(), STRING_SIZE);
str_length = strlen(str_data);
int exec_res = mysql_stmt_execute(stmt);
if (exec_res) {
ok(exp_fail, "'mysql_stmt_execute' returned error: '%s'", mysql_stmt_error(stmt));
if (exp_fail) {
diag("mysql_stmt_execute at line %d failed: '%s'", __LINE__, mysql_stmt_error(stmt));
mysql_stmt_close(stmt);
continue;
} else {
diag("mysql_stmt_execute at line %d failed: '%s'", __LINE__, mysql_stmt_error(stmt));
res = EXIT_FAILURE;
goto exit;
}
}
MYSQL_BIND bind;
memset(&bind, 0, sizeof(bind));
char data_c2[STRING_SIZE] = { 0 };
#ifdef LIBMYSQL_HELPER
bool is_null[1];
bool error[1];
long unsigned int length[1];
#else
char is_null[1];
char error[1];
long unsigned int length[1];
#endif
bind.buffer_type = MYSQL_TYPE_STRING;
bind.buffer = (char *)&data_c2;
bind.buffer_length = STRING_SIZE;
bind.is_null = &is_null[0];
bind.length = &length[0];
bind.error = &error[0];
if (mysql_stmt_bind_result(stmt, &bind)) {
diag("mysql_stmt_bind_result at line %d failed: %s", __LINE__, mysql_stmt_error(stmt));
res = EXIT_FAILURE;
goto exit;
}
int res_err = mysql_stmt_store_result(stmt);
if (res_err) {
ok(exp_fail, "'mysql_stmt_store_result' returned error: '%s'", mysql_stmt_error(stmt));
if (exp_fail) {
mysql_stmt_close(stmt);
continue;
} else {
res = EXIT_FAILURE;
goto exit;
}
} else {
int field_count = mysql_stmt_field_count(stmt);
if (mysql_stmt_fetch(stmt) == 1 && field_count == 1) {
diag("mysql_stmt_fetch at line %d failed: %s", __LINE__, mysql_stmt_error(stmt));
res = EXIT_FAILURE;
goto exit;
}
ok(
string { data_c2 } == exp_res && field_count == 1,
"Prepared statement SELECT matched expected - FieldCount: '%d', Exp: '%s', Act: '%s'",
field_count, exp_res.c_str(), data_c2
);
}
mysql_stmt_close(stmt);
}
exit:
mysql_close(proxy);
mysql_close(admin);
if (res == EXIT_SUCCESS) {
return exit_status();
} else {
return res;
}
}

@ -0,0 +1,111 @@
#!/usr/bin/env php
<?php
/**
* @file reg_test_stmt_resultset_err_no_rows.php
* @brief Checks handling of STMT binary resultsets with errors without rows.
* @details Performs an analogous logic to test 'reg_test_stmt_resultset_err_no_rows-t'
*/
error_reporting(E_ALL ^ E_NOTICE);
$res = 0;
$cases = 0;
function ok($sucess, $message) {
global $res;
global $cases;
$ok_msg = "";
if ($sucess) {
$ok_msg = "ok";
} else {
$ok_msg = "not ok";
$res = 1;
}
$cases += 1;
echo $ok_msg, " - ", $message, "\n";
}
function diag($message) {
echo ":: ", $message, "\n";
}
$test_cases = [[true, '$.', ""], [true, '$', "[\"a\", \"b\"]"], [true, '$.', ""], [false, '$.b', "[\"c\"]"]];
$plan_tests = count($test_cases);
echo "1..".$plan_tests.PHP_EOL;
$admin_user = getenv("TAP_ADMINUSERNAME");
$admin_user = $admin_user == false ? "admin" : $admin_user;
$admin_pass = getenv("TAP_ADMINPASSWORD");
$admin_pass = $admin_pass == false ? "admin" : $admin_pass;
$admin_port = getenv("TAP_ADMINPORT");
$admin_port = $admin_port == false ? 6032 : $admin_port;
$username = getenv("TAP_USERNAME");
$username = $username == false ? "root" : $username;
$password = getenv("TAP_PASSWORD");
$password = $password == false ? "root" : $password;
$port = getenv("TAP_PORT");
$port = $port == false ? "6033" : $port;
echo ":: Creating ProxySQL Admin connection...".PHP_EOL;
$proxy_admin = new mysqli("127.0.0.1", $admin_user, $admin_pass, "", $admin_port);
if ($proxy_admin->connect_errno) {
die("PorxySQL connect failed: " . $proxy->connect_error);
}
echo ":: ProxySQL Admin connection completed".PHP_EOL;
echo ":: Creating ProxySQL connection...".PHP_EOL;
$proxy = new mysqli("127.0.0.1", $username, $password, "", $port);
if ($proxy->connect_errno) {
die("PorxySQL connect failed: " . $proxy->connect_error);
}
echo ":: ProxySQL connection completed".PHP_EOL;
$stmt = $proxy->prepare("SELECT json_keys('{\"a\": 1, \"b\": {\"c\": 30}}', ?)");
foreach ($test_cases as $test_case) {
[$exp_fail, $param, $exp_res] = $test_case;
try {
echo ":: Binding param: '", $param, "'\n";
$sql_select_limit = $param;
$stmt->bind_param('s', $sql_select_limit);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
$row = $result->fetch_array(MYSQLI_NUM);
$field_count = $result->field_count;
$field_val = $row[0];
ok(
$field_count == 1 && $field_val == $exp_res,
"Fetch value should match expected - ".
"FieldCount: '".$field_count."', Exp: '".$exp_res."', Act: '".$field_val
);
} else {
diag("Received invalid number of rows '".$result->num_rows."'");
}
} catch (Exception $e) {
ok($exp_fail == true, "Operation failed with error: '".$e->getMessage()."'");
}
}
$success = ($plan_tests == $cases && $res == 0);
$exit_code = $success == false ? 1 : 0;
exit($exit_code);
?>

@ -0,0 +1,46 @@
/**
* @file reg_test_stmt_resultset_err_no_rows_php-t.cpp
* @brief Checks handling of STMT binary resultsets with errors by means of PHP test
* 'reg_test_stmt_resultset_err_no_rows.php'.
*/
#include <iostream>
#include <utility>
#include <vector>
#include <mysql.h>
#include <mysql/mysqld_error.h>
#include "proxysql_utils.h"
#include "tap.h"
#include "command_line.h"
#include "utils.h"
using std::string;
int main(int argc, char** argv) {
plan(1);
CommandLine cl {};
if (cl.getEnv()) {
diag("Failed to get the required environmental variables.");
return EXIT_FAILURE;
}
string php_stdout {};
string php_stderr {};
const string php_path { string{ cl.workdir } + "./reg_test_stmt_resultset_err_no_rows.php" };
to_opts opts {2 * 1000 * 1000, 0, 0, 0};
int exec_res = wexecvp(php_path, {}, &opts, php_stdout, php_stderr);
diag("Output from executed test: '%s'", php_path.c_str());
diag("========================================================================");
std::cout << php_stdout;
diag("========================================================================");
ok(exec_res == EXIT_SUCCESS, "Test exited with code '%d'", exec_res);
}
Loading…
Cancel
Save