MXS-1452: Extend kill parsing test

The test now correctly checks for usernames and integer overflow.
This commit is contained in:
Markus Mäkelä
2017-10-02 15:04:38 +03:00
parent 96d160f897
commit 4ee5c991c2

View File

@ -2,12 +2,15 @@
#include "../MySQLClient/mysql_client.cc" #include "../MySQLClient/mysql_client.cc"
#define NO_THREAD_ID 0
int test_one_query(const char *query, bool should_succeed, uint64_t expected_tid, int test_one_query(const char *query, bool should_succeed, uint64_t expected_tid,
int expected_kt) int expected_kt, std::string expected_user)
{ {
char *query_copy = MXS_STRDUP_A(query); char *query_copy = MXS_STRDUP_A(query);
uint64_t result_tid = 1111111; uint64_t result_tid = 1111111;
kill_type_t result_kt = KT_QUERY; kill_type_t result_kt = KT_QUERY;
std::string user;
/* If the parse fails, these should remain unchanged */ /* If the parse fails, these should remain unchanged */
if (!should_succeed) if (!should_succeed)
@ -15,11 +18,11 @@ int test_one_query(const char *query, bool should_succeed, uint64_t expected_tid
result_tid = expected_tid; result_tid = expected_tid;
result_kt = (kill_type_t)expected_kt; result_kt = (kill_type_t)expected_kt;
} }
bool success = parse_kill_query(query_copy, &result_tid, &result_kt); bool success = parse_kill_query(query_copy, &result_tid, &result_kt, &user);
MXS_FREE(query_copy); MXS_FREE(query_copy);
if ((success == should_succeed) && (result_tid == expected_tid) && if (success == should_succeed && result_tid == expected_tid &&
(result_kt == expected_kt)) result_kt == expected_kt && expected_user == user)
{ {
return 0; return 0;
} }
@ -32,13 +35,15 @@ int test_one_query(const char *query, bool should_succeed, uint64_t expected_tid
} }
if (result_tid != expected_tid) if (result_tid != expected_tid)
{ {
printf("Expected thread id '%" PRIu64 "', got '%" PRIu64 "'.\n", printf("Expected thread id '%" PRIu64 "', got '%" PRIu64 "'.\n", expected_tid, result_tid);
expected_tid, result_tid);
} }
if (result_kt != expected_kt) if (result_kt != expected_kt)
{ {
printf("Expected kill type '%u', got '%u'.\n", printf("Expected kill type '%u', got '%u'.\n", expected_kt, result_kt);
expected_kt, result_kt); }
if (expected_user != user)
{
printf("Expected user '%s', got '%s'.\n", expected_user.c_str(), user.c_str());
} }
printf("\n"); printf("\n");
return 1; return 1;
@ -50,6 +55,7 @@ typedef struct test_t
bool should_succeed; bool should_succeed;
uint64_t correct_id; uint64_t correct_id;
int correct_kt; int correct_kt;
const char* correct_user;
} test_t; } test_t;
int main(int argc, char **argv) int main(int argc, char **argv)
@ -66,10 +72,7 @@ int main(int argc, char **argv)
{" kill connection 1A", false, 0, KT_CONNECTION}, {" kill connection 1A", false, 0, KT_CONNECTION},
{" kill connection 1 A ", false, 0, KT_CONNECTION}, {" kill connection 1 A ", false, 0, KT_CONNECTION},
{"kill query 7 ; select * ", false, 0, KT_CONNECTION}, {"kill query 7 ; select * ", false, 0, KT_CONNECTION},
{ {"KIll query 12345678901234567890", false, 0, KT_QUERY}, // 32-bit integer overflow
"KIll query \t \n \t 12345678901234567890 \n \t ",
true, 12345678901234567890ULL, KT_QUERY
},
{"KIll query \t \n \t 21 \n \t ", true, 21, KT_QUERY}, {"KIll query \t \n \t 21 \n \t ", true, 21, KT_QUERY},
{"KIll \t \n \t -6 \n \t ", false, 0, KT_CONNECTION}, {"KIll \t \n \t -6 \n \t ", false, 0, KT_CONNECTION},
{"KIll 12345678901234567890123456 \n \t ", false, 0, KT_CONNECTION}, {"KIll 12345678901234567890123456 \n \t ", false, 0, KT_CONNECTION},
@ -82,7 +85,9 @@ int main(int argc, char **argv)
{" kill HARD 123", true, 123, KT_CONNECTION | KT_HARD}, {" kill HARD 123", true, 123, KT_CONNECTION | KT_HARD},
{" kill SOFT 123", true, 123, KT_CONNECTION | KT_SOFT}, {" kill SOFT 123", true, 123, KT_CONNECTION | KT_SOFT},
{"KIll soft query 21 ", true, 21, KT_QUERY | KT_SOFT}, {"KIll soft query 21 ", true, 21, KT_QUERY | KT_SOFT},
{"KIll query soft 21 ", false, 21, KT_QUERY} {"KIll query soft 21 ", false, 21, KT_QUERY},
{"KIll query user maxuser ", true, NO_THREAD_ID, KT_QUERY, "maxuser"},
{"KIll user query maxuser ", false, NO_THREAD_ID, KT_QUERY}
}; };
int result = 0; int result = 0;
int arr_size = sizeof(tests) / sizeof(test_t); int arr_size = sizeof(tests) / sizeof(test_t);
@ -92,7 +97,9 @@ int main(int argc, char **argv)
bool should_succeed = tests[i].should_succeed; bool should_succeed = tests[i].should_succeed;
uint64_t expected_tid = tests[i].correct_id; uint64_t expected_tid = tests[i].correct_id;
int expected_kt = tests[i].correct_kt; int expected_kt = tests[i].correct_kt;
result += test_one_query(query, should_succeed, expected_tid, expected_kt); std::string expected_user = tests[i].correct_user ? tests[i].correct_user : "";
result += test_one_query(query, should_succeed, expected_tid,
expected_kt, expected_user);
} }
return result; return result;
} }