diff --git a/unittest/sql/CMakeLists.txt b/unittest/sql/CMakeLists.txt index 224ce83bae1..3f5854a8c47 100644 --- a/unittest/sql/CMakeLists.txt +++ b/unittest/sql/CMakeLists.txt @@ -34,3 +34,7 @@ MY_ADD_TEST(mf_iocache) ADD_EXECUTABLE(my_json_writer-t my_json_writer-t.cc dummy_builtins.cc) TARGET_LINK_LIBRARIES(my_json_writer-t sql mytap) MY_ADD_TEST(my_json_writer) + +ADD_EXECUTABLE(mysql_opt_bind-t mysql_opt_bind-t.c dummy_builtins.cc) +TARGET_LINK_LIBRARIES(mysql_opt_bind-t sql mytap) +MY_ADD_TEST(mysql_opt_bind) diff --git a/unittest/sql/mysql_opt_bind-t.c b/unittest/sql/mysql_opt_bind-t.c new file mode 100644 index 00000000000..9a4cc214efd --- /dev/null +++ b/unittest/sql/mysql_opt_bind-t.c @@ -0,0 +1,71 @@ +//TODO license headr +#include +#include +#include + +int main() +{ + static const bool ARG_TRUE= true; + static const char QUERY[]= "SELECT SUBSTRING_INDEX(USER(), '@', -1)"; + + const char *const port_string= getenv("MASTER_MYPORT"); + const unsigned int port= + port_string? (unsigned int)strtoul(port, NULL, 10) : 0; + + //TODO classes allow explicit case handling + const char *bind_addresses[][2] = { + { "127.0.0.1", /* Cloudflare DNS */ "1.0.0.1" }, + { "localhost", "example.com" }, + //TODO IPv6 +}; + int count= sizeof(bind_addresses) / sizeof(const char *); + + plan(count); + for (size_t i= 0; i < count; ++i) + { + const char *bind_address= bind_addresses[i]; + MYSQL connection; + MYSQL_RES *results; + bool success= false; + + // need init thread. mysql_thread_init(); mysql_thread_end(); + if (!mysql_init(&connection)) + { + diag("failed to initialize connection"); + goto end; + } + + if (mysql_options(&connection, MYSQL_OPT_BIND, bind_address)) + { + diag("MYSQL_OPT_BIND not accepted"); + goto close; + } + mysql_options(&connection, MYSQL_OPT_RECONNECT, &ARG_TRUE); + + if (!mysql_real_connect(&connection, + NULL, // server address + NULL, // user + NULL, // password + NULL, // database + port, + NULL, // socket + 0x0 // flags + )) + { + if (/* TODO test fails from invalid addresses */ false) + success= true; + else + diag("failed to connect to the server"); + goto close; + } + + // TODO query + + close: + mysql_close(&connection); + end: + ok(success, bind_address); + } + test_case(); + return exit_status(); +}