Debian Codegraph @

unittest/gunit/xplugin/xcl/session_t.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2017, 2026, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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" // NOLINT(build/include_subdir)
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 /** Create mock and return it to the `Session_impl` object.
64 The object takes ownership of the pointer. We hold a copy
65 of it to setup mocking expectations.
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 } // namespace test
149 } // namespace xcl
150
151 #endif // UNITTEST_GUNIT_XPLUGIN_XCL_SESSION_T_H_