| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifndef UNITTEST_GUNIT_XPLUGIN_XCL_SESSION_T_H_ |
| 27 |
#define UNITTEST_GUNIT_XPLUGIN_XCL_SESSION_T_H_ |
| 28 |
|
| 29 |
#include <gtest/gtest.h> |
| 30 |
#include <cstdint> |
| 31 |
#include <memory> |
| 32 |
#include <string> |
| 33 |
#include <utility> |
| 34 |
|
| 35 |
#include "errmsg.h" |
| 36 |
#include "plugin/x/client/xsession_impl.h" |
| 37 |
#include "unittest/gunit/xplugin/xcl/mock/connection.h" |
| 38 |
#include "unittest/gunit/xplugin/xcl/mock/connection_state.h" |
| 39 |
#include "unittest/gunit/xplugin/xcl/mock/factory.h" |
| 40 |
#include "unittest/gunit/xplugin/xcl/mock/protocol.h" |
| 41 |
|
| 42 |
namespace xcl { |
| 43 |
namespace test { |
| 44 |
|
| 45 |
using ::testing::_; |
| 46 |
using ::testing::An; |
| 47 |
using ::testing::DoAll; |
| 48 |
using ::testing::Invoke; |
| 49 |
using ::testing::Return; |
| 50 |
using ::testing::ReturnRef; |
| 51 |
using ::testing::SaveArg; |
| 52 |
using ::testing::StrictMock; |
| 53 |
using ::testing::Test; |
| 54 |
using ::testing::Values; |
| 55 |
using ::testing::WithParamInterface; |
| 56 |
|
| 57 |
class Xcl_session_impl_tests : public Test { |
| 58 |
public: |
| 59 |
void SetUp() override { m_sut = make_sut(false); } |
| 60 |
|
| 61 |
std::unique_ptr<Session_impl> prepare_session() { |
| 62 |
std::unique_ptr<Session_impl> result; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
m_mock_protocol = new StrictMock<mock::XProtocol>(); |
| 68 |
m_mock_factory = new StrictMock<mock::Protocol_factory>(); |
| 69 |
|
| 70 |
EXPECT_CALL(*m_mock_factory, create_protocol_raw(_)) |
| 71 |
.WillOnce(DoAll(Invoke(this, &Xcl_session_impl_tests::assign_configs), |
| 72 |
Return(m_mock_protocol))); |
| 73 |
EXPECT_CALL(*m_mock_protocol, add_notice_handler(_, Handler_position::End, |
| 74 |
Handler_priority_high)) |
| 75 |
.WillOnce(DoAll(SaveArg<0>(&m_out_message_handler), Return(2))); |
| 76 |
EXPECT_CALL(*m_mock_protocol, add_notice_handler(_, Handler_position::End, |
| 77 |
Handler_priority_low)) |
| 78 |
.WillOnce(Return(1)); |
| 79 |
EXPECT_CALL(*m_mock_protocol, get_connection()) |
| 80 |
.WillRepeatedly(ReturnRef(m_mock_connection)); |
| 81 |
EXPECT_CALL(m_mock_connection, state()) |
| 82 |
.WillRepeatedly(ReturnRef(m_mock_connection_state)); |
| 83 |
EXPECT_CALL(*m_mock_protocol, use_compression(_, _)) |
| 84 |
.WillRepeatedly(Return()); |
| 85 |
|
| 86 |
result.reset( |
| 87 |
new Session_impl(std::unique_ptr<Protocol_factory>{m_mock_factory})); |
| 88 |
|
| 89 |
return result; |
| 90 |
} |
| 91 |
|
| 92 |
std::unique_ptr<Session_impl> make_sut(const bool is_connected, |
| 93 |
const std::string &auth = "MYSQL41") { |
| 94 |
auto result = prepare_session(); |
| 95 |
|
| 96 |
EXPECT_CALL(m_mock_connection_state, is_connected()) |
| 97 |
.WillRepeatedly(Return(false)); |
| 98 |
result->set_mysql_option( |
| 99 |
xcl::XSession::Mysqlx_option::Authentication_method, auth); |
| 100 |
::testing::Mock::VerifyAndClearExpectations(&m_mock_connection_state); |
| 101 |
EXPECT_CALL(m_mock_connection_state, is_connected()) |
| 102 |
.WillRepeatedly(Return(is_connected)); |
| 103 |
|
| 104 |
return result; |
| 105 |
} |
| 106 |
|
| 107 |
void expect_connection_close() { EXPECT_CALL(m_mock_connection, close()); } |
| 108 |
|
| 109 |
bool encode_session_state_change( |
| 110 |
const Mysqlx::Notice::SessionStateChanged_Parameter param, |
| 111 |
const uint64_t value, std::string *out_payload) { |
| 112 |
Mysqlx::Notice::SessionStateChanged session_state_changed; |
| 113 |
std::string session_state_changed_binary; |
| 114 |
|
| 115 |
session_state_changed.set_param(param); |
| 116 |
|
| 117 |
auto v = session_state_changed.mutable_value()->Add(); |
| 118 |
v->set_type(Mysqlx::Datatypes::Scalar::V_UINT); |
| 119 |
v->set_v_unsigned_int(value); |
| 120 |
|
| 121 |
return session_state_changed.SerializeToString(out_payload); |
| 122 |
} |
| 123 |
|
| 124 |
void assign_configs(const std::shared_ptr<Context> &context) { |
| 125 |
m_out_ssl_config = &context->m_ssl_config; |
| 126 |
m_out_connection_config = &context->m_connection_config; |
| 127 |
} |
| 128 |
|
| 129 |
XError assert_ssl_mode(const std::string &value) { |
| 130 |
return m_sut->set_mysql_option(XSession::Mysqlx_option::Ssl_mode, value); |
| 131 |
} |
| 132 |
|
| 133 |
XError assert_resolve_to(const std::string &value) { |
| 134 |
return m_sut->set_mysql_option(XSession::Mysqlx_option::Hostname_resolve_to, |
| 135 |
value); |
| 136 |
} |
| 137 |
|
| 138 |
XProtocol::Notice_handler m_out_message_handler; |
| 139 |
const Ssl_config *m_out_ssl_config{nullptr}; |
| 140 |
const Connection_config *m_out_connection_config{nullptr}; |
| 141 |
StrictMock<mock::XProtocol> *m_mock_protocol{nullptr}; |
| 142 |
StrictMock<mock::XConnection> m_mock_connection; |
| 143 |
StrictMock<mock::XConnection_state> m_mock_connection_state; |
| 144 |
StrictMock<mock::Protocol_factory> *m_mock_factory; |
| 145 |
std::unique_ptr<Session_impl> m_sut; |
| 146 |
}; |
| 147 |
|
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
#endif |