| 1 | /* Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <my_macros.h> |
| 27 | |
| 28 | #include <cstdint> |
| 29 | #include <memory> |
| 30 | |
| 31 | #include "plugin/x/src/ngs/protocol_decoder.h" |
| 32 | #include "plugin/x/src/operations_factory.h" |
| 33 | #include "plugin/x/src/variables/system_variables.h" |
| 34 | #include "unittest/gunit/xplugin/xpl/mock/client.h" |
| 35 | #include "unittest/gunit/xplugin/xpl/mock/message_dispatcher.h" |
| 36 | #include "unittest/gunit/xplugin/xpl/mock/protocol_monitor.h" |
| 37 | #include "unittest/gunit/xplugin/xpl/mock/session.h" |
| 38 | #include "unittest/gunit/xplugin/xpl/mock/vio.h" |
| 39 | #include "unittest/gunit/xplugin/xpl/mock/waiting_for_io.h" |
| 40 | |
| 41 | namespace xpl { |
| 42 | namespace test { |
| 43 | |
| 44 | using ::testing::_; |
| 45 | using ::testing::AtLeast; |
| 46 | using ::testing::DoAll; |
| 47 | using ::testing::InSequence; |
| 48 | using ::testing::Return; |
| 49 | using ::testing::ReturnRef; |
| 50 | using ::testing::SetArrayArgument; |
| 51 | using ::testing::StrictMock; |
| 52 | |
| 53 | ACTION_P2(SetSocketErrnoAndReturn, err, result) { |
| 54 | xpl::Operations_factory operations_factory; |
| 55 | |
| 56 | operations_factory.create_system_interface()->set_socket_errno(err); |
| 57 | |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | const uint32_t k_max_wait_timeout_in_sec = IF_WIN(4, 2); |
| 62 | const uint32_t k_max_read_timeout_in_sec = IF_WIN(4, 2); |
| 63 | |
| 64 | class Protocol_decoder_test_suite : public ::testing::Test { |
| 65 | public: |
| 66 | using Strict_mock_client = StrictMock<mock::Client>; |
| 67 | using Strict_mock_vio = StrictMock<mock::Vio>; |
| 68 | using Strict_mock_pmonitor = StrictMock<mock::Protocol_monitor>; |
| 69 | using Strict_mock_wait_for_io = StrictMock<mock::Waiting_for_io>; |
| 70 | |
| 71 | public: |
| 72 | void SetUp() override { |
| 73 | EXPECT_CALL(*m_mock_vio, set_state(_)).Times(AtLeast(1)); |
| 74 | EXPECT_CALL(*m_mock_vio, get_mysql_socket()) |
| 75 | .WillRepeatedly(ReturnRef(m_socket)); |
| 76 | EXPECT_CALL(*m_mock_vio, set_timeout_in_ms(_, _)).Times(AtLeast(1)); |
| 77 | |
| 78 | m_config_global->m_timeouts.m_wait_timeout = k_max_wait_timeout_in_sec; |
| 79 | m_config_global->m_timeouts.m_read_timeout = k_max_read_timeout_in_sec; |
| 80 | |
| 81 | m_sut.reset(new ngs::Protocol_decoder(&m_mock_dispatcher, m_mock_vio, |
| 82 | &m_mock_protocol_monitor, m_config)); |
| 83 | } |
| 84 | |
| 85 | void TearDown() override { m_config_global->m_timeouts = {}; } |
| 86 | |
| 87 | MYSQL_SOCKET m_socket{INVALID_SOCKET, nullptr}; |
| 88 | const std::vector<unsigned char> m_msg{ |
| 89 | 1, 0, 0, 0, 1}; // 1 = size, 0, 0, 0, 1 = Msg_CapGet |
| 90 | |
| 91 | std::shared_ptr<Strict_mock_vio> m_mock_vio{new Strict_mock_vio()}; |
| 92 | std::shared_ptr<ngs::Protocol_global_config> m_config_global{ |
| 93 | new ngs::Protocol_global_config()}; |
| 94 | std::shared_ptr<ngs::Protocol_config> m_config{ |
| 95 | new ngs::Protocol_config(m_config_global)}; |
| 96 | |
| 97 | Strict_mock_pmonitor m_mock_protocol_monitor; |
| 98 | Strict_mock_wait_for_io m_mock_wait_for_io; |
| 99 | mock::Message_dispatcher m_mock_dispatcher; |
| 100 | |
| 101 | std::unique_ptr<ngs::Protocol_decoder> m_sut; |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Test checks if protocol_decoder does't call 'before read' callback |
| 106 | * in case when reporting is forbidden. |
| 107 | */ |
| 108 | TEST_F(Protocol_decoder_test_suite, no_need_for_idle_reporting_read_msg) { |
| 109 | { |
| 110 | InSequence s; |
| 111 | EXPECT_CALL(m_mock_wait_for_io, has_to_report_idle_waiting()) |
| 112 | .WillOnce(Return(false)); |
| 113 | EXPECT_CALL(m_mock_wait_for_io, on_idle_or_before_read()).Times(0); |
| 114 | EXPECT_CALL(*m_mock_vio, read(_, _)) |
| 115 | .WillOnce(DoAll(SetArrayArgument<0>(m_msg.begin(), m_msg.end()), |
| 116 | Return(m_msg.size()))); |
| 117 | EXPECT_CALL(m_mock_dispatcher, handle(_)); |
| 118 | EXPECT_CALL(m_mock_protocol_monitor, on_receive(_)); |
| 119 | } |
| 120 | |
| 121 | m_sut->read_and_decode(&m_mock_wait_for_io); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Test checks if protocol_decoder calls 'before read' callback |
| 126 | * in case when reporting is required. |
| 127 | */ |
| 128 | TEST_F(Protocol_decoder_test_suite, need_idle_reporting_read_msg) { |
| 129 | { |
| 130 | InSequence s; |
| 131 | EXPECT_CALL(m_mock_wait_for_io, has_to_report_idle_waiting()) |
| 132 | .WillOnce(Return(true)); |
| 133 | EXPECT_CALL(m_mock_wait_for_io, on_idle_or_before_read()) |
| 134 | .WillOnce(Return(true)); |
| 135 | EXPECT_CALL(*m_mock_vio, read(_, _)) |
| 136 | .WillOnce(DoAll(SetArrayArgument<0>(m_msg.begin(), m_msg.end()), |
| 137 | Return(m_msg.size()))); |
| 138 | EXPECT_CALL(m_mock_dispatcher, handle(_)); |
| 139 | EXPECT_CALL(m_mock_protocol_monitor, on_receive(_)); |
| 140 | } |
| 141 | |
| 142 | m_sut->read_and_decode(&m_mock_wait_for_io); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Test generates multiple read timeouts that sum of those timeouts |
| 147 | * exceeds `k_max_wait_timeout_in_sec`. In such case Protocol_decoder |
| 148 | * must generate an error. |
| 149 | */ |
| 150 | TEST_F(Protocol_decoder_test_suite, |
| 151 | need_idle_reporting_mutiple_idle_timeouts_exceeds_wait_timeout) { |
| 152 | { |
| 153 | InSequence s; |
| 154 | EXPECT_CALL(m_mock_wait_for_io, has_to_report_idle_waiting()) |
| 155 | .WillOnce(Return(true)); |
| 156 | EXPECT_CALL(m_mock_wait_for_io, on_idle_or_before_read()) |
| 157 | .WillOnce(Return(true)) |
| 158 | .RetiresOnSaturation(); |
| 159 | |
| 160 | // the `wait_timeout` is exceeded after 4 `read` calls |
| 161 | // For first three (nonfatal) the idle function is called. |
| 162 | for (int i = 0; i < 3; ++i) { |
| 163 | EXPECT_CALL(*m_mock_vio, read(_, _)) |
| 164 | .WillOnce(SetSocketErrnoAndReturn(SOCKET_ETIMEDOUT, -1)) |
| 165 | .RetiresOnSaturation(); |
| 166 | EXPECT_CALL(m_mock_wait_for_io, on_idle_or_before_read()) |
| 167 | .WillOnce(Return(true)) |
| 168 | .RetiresOnSaturation(); |
| 169 | } |
| 170 | |
| 171 | // For last failing read operation there is no |
| 172 | // `on_idle_or_before_read` call. |
| 173 | EXPECT_CALL(*m_mock_vio, read(_, _)) |
| 174 | .WillOnce(SetSocketErrnoAndReturn(SOCKET_ETIMEDOUT, -1)) |
| 175 | .RetiresOnSaturation(); |
| 176 | } |
| 177 | |
| 178 | m_sut->read_and_decode(&m_mock_wait_for_io); |
| 179 | } |
| 180 | |
| 181 | } // namespace test |
| 182 | } // namespace xpl |