add gtest for MySQL Protocol handshake

This commit is contained in:
BohuTANG 2020-04-22 10:27:06 +08:00 committed by zhang2014
parent f5a53dd270
commit b9e2c0d72c
2 changed files with 35 additions and 6 deletions

View File

@ -517,11 +517,9 @@ public:
buffer.ignore(10);
if (capability_flags & MySQLProtocol::CLIENT_SECURE_CONNECTION)
{
UInt8 part2_length = (auth_plugin_data_length - AUTH_PLUGIN_DATA_PART_1_LENGTH) > 13
? 13
: (auth_plugin_data_length - AUTH_PLUGIN_DATA_PART_1_LENGTH);
auth_plugin_data.resize(part2_length + AUTH_PLUGIN_DATA_PART_1_LENGTH - 1);
buffer.readStrict(auth_plugin_data.data() + AUTH_PLUGIN_DATA_PART_1_LENGTH, part2_length - 1);
UInt8 part2_length = (SCRAMBLE_LENGTH - AUTH_PLUGIN_DATA_PART_1_LENGTH);
auth_plugin_data.resize(SCRAMBLE_LENGTH);
buffer.readStrict(auth_plugin_data.data() + AUTH_PLUGIN_DATA_PART_1_LENGTH, part2_length);
buffer.ignore(1);
}
@ -958,7 +956,7 @@ public:
packetType = PACKET_EOF;
eof.readPayloadImpl(payload);
break;
};
}
}
ResponsePacketType getType() { return packetType; }

View File

@ -0,0 +1,31 @@
#include <string>
#include <gtest/gtest.h>
#include <Core/MySQLProtocol.h>
#include <IO/ReadBufferFromString.h>
#include <IO/WriteBufferFromString.h>
using namespace DB;
using namespace MySQLProtocol;
TEST(MySQLProtocol, Handshake)
{
UInt32 server_capability_flags = CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | CLIENT_PLUGIN_AUTH
| CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA | CLIENT_CONNECT_WITH_DB | CLIENT_DEPRECATE_EOF;
std::string s;
WriteBufferFromString out(s);
Handshake server_handshake(server_capability_flags, 0, "ClickHouse", "mysql_native_password", "aaaaaaaaaaaaaaaaaaaaa");
server_handshake.writePayloadImpl(out);
ReadBufferFromString in(s);
Handshake client_handshake;
client_handshake.readPayloadImpl(in);
EXPECT_EQ(server_handshake.capability_flags, client_handshake.capability_flags);
EXPECT_EQ(server_handshake.status_flags, client_handshake.status_flags);
EXPECT_EQ(server_handshake.server_version, client_handshake.server_version);
EXPECT_EQ(server_handshake.protocol_version, client_handshake.protocol_version);
EXPECT_EQ(server_handshake.auth_plugin_data.substr(0, 20), client_handshake.auth_plugin_data);
EXPECT_EQ(server_handshake.auth_plugin_name, client_handshake.auth_plugin_name);
}