|
| 1 | +/** |
| 2 | + * @file reg_test_5977_fast_forward_unix_socket_compression-t.cpp |
| 3 | + * @brief Regression test for compressed fast-forward sessions using a Unix socket backend. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#include "mysql.h" |
| 9 | + |
| 10 | +#include "tap.h" |
| 11 | +#include "command_line.h" |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +constexpr const char* TEST_USER = "issue5977"; |
| 16 | +constexpr const char* TEST_PASSWORD = "issue5977"; |
| 17 | + |
| 18 | +bool query(MYSQL* mysql, const std::string& sql) { |
| 19 | + return mysql_query(mysql, sql.c_str()) == 0; |
| 20 | +} |
| 21 | + |
| 22 | +bool configure(MYSQL* admin) { |
| 23 | + return |
| 24 | + query(admin, "DELETE FROM mysql_servers WHERE hostgroup_id=5977") && |
| 25 | + query(admin, |
| 26 | + "INSERT INTO mysql_servers " |
| 27 | + "(hostgroup_id,hostname,port,status,compression,max_connections,comment) VALUES " |
| 28 | + "(5977,'/tmp/proxysql_sqlite.sock',0,'ONLINE',0,10,'reg_test_5977')") && |
| 29 | + query(admin, "DELETE FROM mysql_users WHERE username='issue5977'") && |
| 30 | + query(admin, |
| 31 | + "INSERT INTO mysql_users " |
| 32 | + "(username,password,active,default_hostgroup,fast_forward,backend,frontend,comment) VALUES " |
| 33 | + "('issue5977','issue5977',1,5977,1,1,1,'reg_test_5977')") && |
| 34 | + query(admin, "LOAD MYSQL SERVERS TO RUNTIME") && |
| 35 | + query(admin, "LOAD MYSQL USERS TO RUNTIME"); |
| 36 | +} |
| 37 | + |
| 38 | +void cleanup(MYSQL* admin) { |
| 39 | + query(admin, "DELETE FROM mysql_servers WHERE hostgroup_id=5977"); |
| 40 | + query(admin, "DELETE FROM mysql_users WHERE username='issue5977'"); |
| 41 | + query(admin, "LOAD MYSQL SERVERS TO RUNTIME"); |
| 42 | + query(admin, "LOAD MYSQL USERS TO RUNTIME"); |
| 43 | +} |
| 44 | + |
| 45 | +} // namespace |
| 46 | + |
| 47 | +int main(int argc, char** argv) { |
| 48 | + CommandLine cl; |
| 49 | + |
| 50 | + if (cl.getEnv()) { |
| 51 | + diag("Failed to get the required environmental variables."); |
| 52 | + return EXIT_FAILURE; |
| 53 | + } |
| 54 | + |
| 55 | + plan(3); |
| 56 | + |
| 57 | + MYSQL* admin = mysql_init(nullptr); |
| 58 | + const bool admin_connected = admin && mysql_real_connect( |
| 59 | + admin, cl.host, cl.admin_username, cl.admin_password, nullptr, cl.admin_port, nullptr, 0 |
| 60 | + ); |
| 61 | + ok(admin_connected, "ProxySQL Admin connection succeeds"); |
| 62 | + |
| 63 | + bool configured = false; |
| 64 | + if (admin_connected) { |
| 65 | + configured = configure(admin); |
| 66 | + } |
| 67 | + ok(configured, "SQLite3 Unix-socket backend and fast-forward user are configured"); |
| 68 | + |
| 69 | + MYSQL* proxy = mysql_init(nullptr); |
| 70 | + const bool compression_enabled = proxy && mysql_options(proxy, MYSQL_OPT_COMPRESS, nullptr) == 0; |
| 71 | + MYSQL* connected = (configured && compression_enabled) |
| 72 | + ? mysql_real_connect(proxy, cl.host, TEST_USER, TEST_PASSWORD, nullptr, cl.port, nullptr, CLIENT_COMPRESS) |
| 73 | + : nullptr; |
| 74 | + |
| 75 | + bool query_ok = false; |
| 76 | + if (connected && mysql_query(proxy, "SELECT 'fast-forward-unix-socket-compression'") == 0) { |
| 77 | + MYSQL_RES* result = mysql_store_result(proxy); |
| 78 | + MYSQL_ROW row = result ? mysql_fetch_row(result) : nullptr; |
| 79 | + query_ok = row && row[0] && std::string(row[0]) == "fast-forward-unix-socket-compression"; |
| 80 | + if (result) { |
| 81 | + mysql_free_result(result); |
| 82 | + } |
| 83 | + } |
| 84 | + ok(query_ok, "compressed fast-forward query returns the SQLite3 backend result"); |
| 85 | + |
| 86 | + if (proxy) { |
| 87 | + mysql_close(proxy); |
| 88 | + } |
| 89 | + if (admin_connected) { |
| 90 | + cleanup(admin); |
| 91 | + } |
| 92 | + if (admin) { |
| 93 | + mysql_close(admin); |
| 94 | + } |
| 95 | + |
| 96 | + return exit_status(); |
| 97 | +} |
0 commit comments