From b812563ecafcff6fcc5e180d7d7bbfb4bedd5106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Mon, 22 May 2023 16:55:53 +0200 Subject: [PATCH] Add TAP test verifying behavior of 'mysql-default_collation_connection' --- .../tests/test_default_conn_collation-t.cpp | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 test/tap/tests/test_default_conn_collation-t.cpp diff --git a/test/tap/tests/test_default_conn_collation-t.cpp b/test/tap/tests/test_default_conn_collation-t.cpp new file mode 100644 index 000000000..803bd7c1f --- /dev/null +++ b/test/tap/tests/test_default_conn_collation-t.cpp @@ -0,0 +1,97 @@ +/** + * @file test_default_conn_collation-t.cpp + * @brief Verifies that 'mysql-default_collation_connection' behaves as expected. + */ + +#include +#include + +#include +#include + +#include "mysql.h" +#include "mysql/mysqld_error.h" + +#include "tap.h" +#include "utils.h" +#include "command_line.h" + +using std::string; + +const MARIADB_CHARSET_INFO * proxysql_find_charset_nr(unsigned int nr) { + const MARIADB_CHARSET_INFO * c = mariadb_compiled_charsets; + do { + if (c->nr == nr) { + return c; + } + ++c; + } while (c[0].nr != 0); + return NULL; +} + +int check_all_collations(const CommandLine& cl, MYSQL* admin) { + const MARIADB_CHARSET_INFO* c = mariadb_compiled_charsets; + + do { + if (c[0].nr > 255) { + diag("Skipping collation '%d-%s'...", c[0].nr, c[0].name); + c += 1; + continue; + } else { + diag("Testing collation '%d-%s'...", c[0].nr, c[0].name); + } + + const char* collate_name = c->name; + string SET_STMT { "SET mysql-default_collation_connection='" + string { collate_name } + "'" }; + + MYSQL_QUERY_T(admin, SET_STMT.c_str()); + MYSQL_QUERY_T(admin, "LOAD MYSQL VARIABLES TO RUNTIME"); + + MYSQL* proxy = mysql_init(NULL); + + 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; + } + + const MARIADB_CHARSET_INFO* charset_info = proxysql_find_charset_nr(proxy->server_language); + printf("%s\n", charset_info->name); + + mysql_close(proxy); + + ok( + strcmp(c->name, charset_info->name) == 0, + "Set collation should match ProxySQL received one - Exp: %s, Act: %s\n", + c->name, charset_info->name + ); + + c += 1; + } while (c[0].nr != 0); + + return EXIT_SUCCESS; +} + +int main(int argc, char** argv) { + CommandLine cl; + + if (cl.getEnv()) { + diag("Failed to get the required environmental variables."); + return EXIT_FAILURE; + } + + + MYSQL* admin = mysql_init(NULL); + + 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; + } + + check_all_collations(cl, admin); + +cleanup: + + mysql_close(admin); + + return exit_status(); +}