diff --git a/test/tap/tests/Makefile b/test/tap/tests/Makefile index aad8003f1..878824b45 100644 --- a/test/tap/tests/Makefile +++ b/test/tap/tests/Makefile @@ -121,8 +121,8 @@ test_set_collation-t: test_set_collation-t.cpp $(TAP_LIBDIR)/libtap.a setparser_test: setparser_test.cpp $(TAP_LIBDIR)/libtap.a $(RE2_PATH)/util/test.cc $(LDIR)/set_parser.cpp $(LIBPROXYSQLAR) g++ -DDEBUG setparser_test.cpp $(RE2_PATH)/util/test.cc ../../../src/obj/proxysql_global.o $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(MYLIBS) -ltap -ldl -lpthread -o setparser_test -DGITVERSION=\"$(GIT_VERSION)\" -reg_test_3504-change_user_libmariadb_helper: reg_test_3504-change_user_libmariadb_helper.cpp - $(CXX) -DDEBUG reg_test_3504-change_user_libmariadb_helper.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) $(MYLIBS) -lpthread -ldl -std=c++11 -ltap $(STATIC_LIBS) -o reg_test_3504-change_user_libmariadb_helper -DGITVERSION=\"$(GIT_VERSION)\" +reg_test_3504-change_user_libmariadb_helper: reg_test_3504-change_user_helper.cpp + $(CXX) -DDEBUG reg_test_3504-change_user_helper.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) $(MYLIBS) -lpthread -ldl -std=c++11 -ltap $(STATIC_LIBS) -o reg_test_3504-change_user_libmariadb_helper -DGITVERSION=\"$(GIT_VERSION)\" -reg_test_3504-change_user_libmysql_helper: reg_test_3504-change_user_libmysql_helper.cpp - $(CXX) -DDEBUG reg_test_3504-change_user_libmysql_helper.cpp -I/usr/include/mysql -I$(IDIR) -I$(JSON_IDIR) -I../tap -L$(TAP_LIBDIR) -lpthread -ldl -std=c++11 -ltap -lmysqlclient -o reg_test_3504-change_user_libmysql_helper -DGITVERSION=\"$(GIT_VERSION)\" +reg_test_3504-change_user_libmysql_helper: reg_test_3504-change_user_helper.cpp + $(CXX) -DLIBMYSQL_HELPER -DDEBUG reg_test_3504-change_user_helper.cpp -I/usr/include/mysql -I$(IDIR) -I$(JSON_IDIR) -I../tap -L$(TAP_LIBDIR) -lpthread -ldl -std=c++11 -ltap -lmysqlclient -o reg_test_3504-change_user_libmysql_helper -DGITVERSION=\"$(GIT_VERSION)\" diff --git a/test/tap/tests/reg_test_3504-change_user_libmariadb_helper.cpp b/test/tap/tests/reg_test_3504-change_user_helper.cpp similarity index 82% rename from test/tap/tests/reg_test_3504-change_user_libmariadb_helper.cpp rename to test/tap/tests/reg_test_3504-change_user_helper.cpp index 86f2d7b6b..b3aac2fd1 100644 --- a/test/tap/tests/reg_test_3504-change_user_libmariadb_helper.cpp +++ b/test/tap/tests/reg_test_3504-change_user_helper.cpp @@ -1,9 +1,11 @@ /** - * @file reg_test_3504-change_user_libmariadb_helper.cpp + * @file reg_test_3504-change_user_helper.cpp * @brief This is a helper file to connect and execute a 'COM_CHANGE_USER' using - * 'libmariadb' client library. It receives the inputs parameters for making the - * connection as a JSON from the calling test and also returns it's output as a - * JSON. + * 'libmariadb'/'libmysql' client library. The library election should be + * performed by means of the macro 'LIBMYSQL_HELPER', when specified, the file + * should be compiled against 'libmysql' library, when not, against 'libmariadb'. + * It receives the inputs parameters for making the connection as a JSON from + * the calling test and also returns it's output as a JSON. * * Success JSON format: * { @@ -112,6 +114,28 @@ int main(int argc, char** argv) { mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4"); } +#ifdef LIBMYSQL_HELPER + if (SSL == false) { + enum mysql_ssl_mode ssl_mode = SSL_MODE_DISABLED; + mysql_options(&mysql, MYSQL_OPT_SSL_MODE, &ssl_mode); + } + + if ( + !mysql_real_connect( + &mysql, "127.0.0.1", user.c_str(), pass.c_str(), "information_schema", + port, NULL, 0 + ) + ) { + string_format( + "Failed to connect to database: Error: %s\n", err_msg, + mysql_error(&mysql) + ); + output["err_msg"] = err_msg; + res = EXIT_FAILURE; + + goto exit; + } +#else if (SSL == true) { mysql_ssl_set(&mysql, NULL, NULL, NULL, NULL, NULL); conn_res = mysql_real_connect( @@ -135,6 +159,7 @@ int main(int argc, char** argv) { goto exit; } +#endif mysql_get_option(&mysql, MYSQL_DEFAULT_AUTH, &default_auth); output["def_auth_plugin"] = std::string { default_auth }; diff --git a/test/tap/tests/reg_test_3504-change_user_libmysql_helper.cpp b/test/tap/tests/reg_test_3504-change_user_libmysql_helper.cpp deleted file mode 100644 index bdedbfe3c..000000000 --- a/test/tap/tests/reg_test_3504-change_user_libmysql_helper.cpp +++ /dev/null @@ -1,181 +0,0 @@ -/** - * @file reg_test_3504-change_user_libmysql_helper.cpp - * @brief This is a helper file to connect and execute a 'COM_CHANGE_USER' using - * 'libmysql' client library. It receives the inputs parameters for making the - * connection as a JSON from the calling test and also returns it's output as a - * JSON. - * - * Success JSON format: - * { - * "def_auth_plugin": Default auth plugin that was used by the - * client for the connection. - * "switching_auth_type": The 'switching_auth_type' that was required in the - * connection, obtained via ProxySQL internal session. - * "ssl_enabled": Confirmation that SSL is enabled in ProxySQL connection, - * obtained via ProxySQL internal session. - * } - * - * Failure JSON format: - * { - * "err_msg": Error message holding the reason for the failed execution. - * } - */ - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "proxysql_utils.h" -#include "json.hpp" -#include "utils.h" - -using nlohmann::json; - -void parse_result_json_column(MYSQL_RES *result, json& j) { - if(!result) return; - MYSQL_ROW row; - - while ((row = mysql_fetch_row(result))) { - j = json::parse(row[0]); - } -} - -int main(int argc, char** argv) { - nlohmann::json output {}; - std::string err_msg {}; - int res = EXIT_SUCCESS; - - // Extract options - std::string user {}; - std::string pass {}; - std::string auth {}; - std::string charset {}; - int port; - bool SSL; - bool CHANGE_USER; - - // MySQL handle - MYSQL mysql; - mysql_init(&mysql); - - // Real 'AUTH' after connection - char* default_auth = nullptr; - - if (argc != 2) { - output["err_msg"] = "Invalid number of paramenters. Argc: '" - + std::to_string(argc) + "'"; - res = EXIT_FAILURE; - goto exit; - } else { - try { - nlohmann::json input = json::parse(argv[1]); - - user = input.at("user"); - pass = input.at("pass"); - auth = input.at("auth"); - charset = input.at("charset"); - port = input.at("port"); - SSL = input.at("SSL"); - CHANGE_USER = input.at("CHANGE_USER"); - } catch (std::exception& ex) { - output["err_msg"] = - std::string { "Exception while parsing input parameter: '" } + - ex.what() + "'"; - res = EXIT_FAILURE; - goto exit; - } - } - - // Default options if not set - if (port == 0) { - port = 6033; - } - - // options - mysql_options(&mysql, MYSQL_DEFAULT_AUTH, auth.c_str()); - - if (auth == "mysql_clear_password") { - bool enable_cleartext = true; - mysql_options(&mysql, MYSQL_ENABLE_CLEARTEXT_PLUGIN, &enable_cleartext); - } - - if (charset != "") { - mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4"); - } - - if (SSL == false) { - enum mysql_ssl_mode ssl_mode = SSL_MODE_DISABLED; - mysql_options(&mysql, MYSQL_OPT_SSL_MODE, &ssl_mode); - } - - if ( - !mysql_real_connect( - &mysql, "127.0.0.1", user.c_str(), pass.c_str(), "information_schema", - port, NULL, 0 - ) - ) { - string_format( - "Failed to connect to database: Error: %s\n", err_msg, - mysql_error(&mysql) - ); - output["err_msg"] = err_msg; - res = EXIT_FAILURE; - - goto exit; - } - - mysql_get_option(&mysql, MYSQL_DEFAULT_AUTH, &default_auth); - output["def_auth_plugin"] = std::string { default_auth }; - - { - json j_status; - MYSQL_QUERY(&mysql, "PROXYSQL INTERNAL SESSION"); - MYSQL_RES* tr_res = mysql_store_result(&mysql); - parse_result_json_column(tr_res, j_status); - mysql_free_result(tr_res); - - int auth_selected = -1; - bool ssl_enabled = false; - - try { - auth_selected = j_status.at("client").at("switching_auth_type"); - ssl_enabled = j_status.at("client").at("encrypted"); - } catch (const std::exception& ex) { - string_format( - "Exception getting 'auth_plugin': '%s'", err_msg, ex.what() - ); - output["err_msg"] = err_msg; - res = EXIT_FAILURE; - - goto exit; - } - - output["switching_auth_type"] = auth_selected; - output["ssl_enabled"] = ssl_enabled; - } - - if (CHANGE_USER) { - if (mysql_change_user(&mysql, "root", "root", "information_schema")) { - string_format( - "Failed to change user. Error: %s\n", err_msg, mysql_error(&mysql) - ); - output["err_msg"] = err_msg; - res = EXIT_FAILURE; - - goto exit; - } - } - -exit: - mysql_close(&mysql); - - std::cout << output.dump(); - - return res; -}