| 1 | /* Copyright (c) 2015, 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 | |
| 27 | #include "plugin/x/src/ngs/server_client_timeout.h" |
| 28 | #include "unittest/gunit/xplugin/xpl/mock/client.h" |
| 29 | #include "unittest/gunit/xplugin/xpl/mock/session.h" |
| 30 | |
| 31 | namespace xpl { |
| 32 | |
| 33 | namespace test { |
| 34 | |
| 35 | using namespace ::testing; // NOLINT(build/namespaces) |
| 36 | |
| 37 | // The chrono is missing string to Time_point conversion |
| 38 | // lets make initialize time constants in constructor |
| 39 | // relatively from now() |
| 40 | const xpl::chrono::Time_point TIMEPOINT_RELEASE_ALL_BEFORE = xpl::chrono::now(); |
| 41 | const xpl::chrono::Duration DELTA_TO_RELEASE_1 = |
| 42 | xpl::chrono::Milliseconds(-500); |
| 43 | const xpl::chrono::Duration DELTA_TO_RELEASE_2 = |
| 44 | xpl::chrono::Milliseconds(-1000); |
| 45 | const xpl::chrono::Duration DELTA_TO_RELEASE_3 = |
| 46 | xpl::chrono::Milliseconds(-2000); |
| 47 | const xpl::chrono::Duration DELTA_NOT_TO_RELEASE_1 = |
| 48 | xpl::chrono::Milliseconds(2000); |
| 49 | const xpl::chrono::Duration DELTA_NOT_TO_RELEASE_2 = |
| 50 | xpl::chrono::Milliseconds(1000); |
| 51 | const xpl::chrono::Duration DELTA_NOT_TO_RELEASE_3 = |
| 52 | xpl::chrono::Milliseconds(500); |
| 53 | |
| 54 | const xpl::chrono::Time_point TP_TO_RELEASE_1 = |
| 55 | TIMEPOINT_RELEASE_ALL_BEFORE + DELTA_TO_RELEASE_1; |
| 56 | const xpl::chrono::Time_point TP_TO_RELEASE_2 = |
| 57 | TIMEPOINT_RELEASE_ALL_BEFORE + DELTA_TO_RELEASE_2; |
| 58 | const xpl::chrono::Time_point TP_TO_RELEASE_3 = |
| 59 | TIMEPOINT_RELEASE_ALL_BEFORE + DELTA_TO_RELEASE_3; |
| 60 | const xpl::chrono::Time_point TP_NOT_TO_RELEASE_1 = |
| 61 | TIMEPOINT_RELEASE_ALL_BEFORE + DELTA_NOT_TO_RELEASE_1; |
| 62 | const xpl::chrono::Time_point TP_NOT_TO_RELEASE_2 = |
| 63 | TIMEPOINT_RELEASE_ALL_BEFORE + DELTA_NOT_TO_RELEASE_2; |
| 64 | const xpl::chrono::Time_point TP_NOT_TO_RELEASE_3 = |
| 65 | TIMEPOINT_RELEASE_ALL_BEFORE + DELTA_NOT_TO_RELEASE_3; |
| 66 | |
| 67 | class Server_client_timeout_test_suite : public Test { |
| 68 | public: |
| 69 | Server_client_timeout_test_suite() |
| 70 | : sut(new ngs::Server_client_timeout(TIMEPOINT_RELEASE_ALL_BEFORE)) {} |
| 71 | |
| 72 | std::shared_ptr<iface::Client> expectClientValid( |
| 73 | const xpl::chrono::Time_point &tp, |
| 74 | const iface::Client::Client::State state) { |
| 75 | std::shared_ptr<StrictMock<mock::Client>> result; |
| 76 | |
| 77 | result.reset(new StrictMock<mock::Client>()); |
| 78 | |
| 79 | EXPECT_CALL(*result.get(), get_accept_time()).WillOnce(Return(tp)); |
| 80 | EXPECT_CALL(*result.get(), get_state()).WillOnce(Return(state)); |
| 81 | |
| 82 | sut->validate_client_state(result); |
| 83 | |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | std::shared_ptr<iface::Client> expectClientNotValid( |
| 88 | const xpl::chrono::Time_point &tp, |
| 89 | const iface::Client::Client::State state) { |
| 90 | std::shared_ptr<StrictMock<mock::Client>> result; |
| 91 | |
| 92 | result.reset(new StrictMock<mock::Client>()); |
| 93 | |
| 94 | EXPECT_CALL(*result.get(), get_accept_time()).WillOnce(Return(tp)); |
| 95 | EXPECT_CALL(*result.get(), get_state()).WillOnce(Return(state)); |
| 96 | EXPECT_CALL(*result.get(), on_auth_timeout_void()); |
| 97 | EXPECT_CALL(*result.get(), client_id()).WillRepeatedly(Return("")); |
| 98 | |
| 99 | sut->validate_client_state(result); |
| 100 | |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | std::unique_ptr<ngs::Server_client_timeout> sut; |
| 105 | }; |
| 106 | |
| 107 | TEST_F(Server_client_timeout_test_suite, |
| 108 | returnInvalidDate_whenNoClientWasProcessed) { |
| 109 | ASSERT_FALSE(xpl::chrono::is_valid(sut->get_oldest_client_accept_time())); |
| 110 | } |
| 111 | |
| 112 | struct ClientParams { |
| 113 | ClientParams(const xpl::chrono::Duration &Duration, |
| 114 | const iface::Client ::Client::State state) |
| 115 | : m_Duration(Duration), |
| 116 | m_tp(TIMEPOINT_RELEASE_ALL_BEFORE + Duration), |
| 117 | m_state(state) {} |
| 118 | |
| 119 | xpl::chrono::Duration m_Duration; |
| 120 | xpl::chrono::Time_point m_tp; |
| 121 | iface::Client::State m_state; |
| 122 | }; |
| 123 | |
| 124 | void PrintTo(const ClientParams &x, ::std::ostream *os) { |
| 125 | *os << "{ state:" << static_cast<int>(x.m_state) |
| 126 | << ", Durations:" << x.m_Duration.count() << " }"; |
| 127 | } |
| 128 | |
| 129 | class ServerClientTimeoutTestSuiteWithClientsState |
| 130 | : public Server_client_timeout_test_suite, |
| 131 | public ::testing::WithParamInterface<ClientParams> {}; |
| 132 | |
| 133 | class ExpiredClient : public ServerClientTimeoutTestSuiteWithClientsState {}; |
| 134 | class NoExpiredClient_stateNotOk |
| 135 | : public ServerClientTimeoutTestSuiteWithClientsState {}; |
| 136 | class NoExpiredClient_stateOk |
| 137 | : public ServerClientTimeoutTestSuiteWithClientsState {}; |
| 138 | |
| 139 | TEST_P(ExpiredClient, |
| 140 | returnInvalidDateNoFurtherNeedOfChecking_clientReleasedInitiated) { |
| 141 | expectClientNotValid(GetParam().m_tp, GetParam().m_state); |
| 142 | |
| 143 | ASSERT_FALSE(xpl::chrono::is_valid(sut->get_oldest_client_accept_time())); |
| 144 | } |
| 145 | |
| 146 | TEST_P(NoExpiredClient_stateNotOk, |
| 147 | returnClientsAcceptanceDate_thereIsANeedOfFutureChecking) { |
| 148 | const xpl::chrono::Time_point clients_tp = GetParam().m_tp; |
| 149 | expectClientValid(clients_tp, GetParam().m_state); |
| 150 | |
| 151 | ASSERT_TRUE(xpl::chrono::is_valid(sut->get_oldest_client_accept_time())); |
| 152 | ASSERT_EQ(clients_tp, sut->get_oldest_client_accept_time()); |
| 153 | } |
| 154 | |
| 155 | TEST_P(NoExpiredClient_stateOk, |
| 156 | returnInvalidDate_clientRunsCorrectlyNoNeedOfFutureChecking) { |
| 157 | const xpl::chrono::Time_point clients_tp = GetParam().m_tp; |
| 158 | expectClientValid(clients_tp, GetParam().m_state); |
| 159 | |
| 160 | ASSERT_FALSE(xpl::chrono::is_valid(sut->get_oldest_client_accept_time())); |
| 161 | } |
| 162 | |
| 163 | INSTANTIATE_TEST_SUITE_P( |
| 164 | InstantiationOfClientsThatExpiredAndAreInNotValidState, ExpiredClient, |
| 165 | Values(ClientParams(DELTA_TO_RELEASE_1, iface::Client::State::k_accepted), |
| 166 | ClientParams(DELTA_TO_RELEASE_2, iface::Client::State::k_accepted), |
| 167 | ClientParams(DELTA_TO_RELEASE_3, iface::Client::State::k_accepted), |
| 168 | ClientParams(DELTA_TO_RELEASE_1, |
| 169 | iface::Client::State::k_authenticating_first), |
| 170 | ClientParams(DELTA_TO_RELEASE_2, |
| 171 | iface::Client::State::k_authenticating_first), |
| 172 | ClientParams(DELTA_TO_RELEASE_3, |
| 173 | iface::Client::State::k_authenticating_first))); |
| 174 | |
| 175 | INSTANTIATE_TEST_SUITE_P( |
| 176 | InstantiationOfClientsThatExpiredAndAreInNotValidState, |
| 177 | NoExpiredClient_stateNotOk, |
| 178 | Values( |
| 179 | ClientParams(DELTA_NOT_TO_RELEASE_1, iface::Client::State::k_accepted), |
| 180 | ClientParams(DELTA_NOT_TO_RELEASE_2, iface::Client::State::k_accepted), |
| 181 | ClientParams(DELTA_NOT_TO_RELEASE_3, iface::Client::State::k_accepted), |
| 182 | ClientParams(DELTA_NOT_TO_RELEASE_1, |
| 183 | iface::Client::State::k_authenticating_first), |
| 184 | ClientParams(DELTA_NOT_TO_RELEASE_2, |
| 185 | iface::Client::State::k_authenticating_first), |
| 186 | ClientParams(DELTA_NOT_TO_RELEASE_3, |
| 187 | iface::Client::State::k_authenticating_first))); |
| 188 | |
| 189 | INSTANTIATE_TEST_SUITE_P( |
| 190 | InstantiationOfClientsThatNoExpiredAndAreInValidState, |
| 191 | NoExpiredClient_stateOk, |
| 192 | Values( |
| 193 | ClientParams(DELTA_NOT_TO_RELEASE_1, |
| 194 | iface::Client::State::k_accepted_with_session), |
| 195 | ClientParams(DELTA_NOT_TO_RELEASE_1, iface::Client::State::k_running), |
| 196 | ClientParams(DELTA_NOT_TO_RELEASE_1, iface::Client::State::k_closing), |
| 197 | ClientParams(DELTA_NOT_TO_RELEASE_1, iface::Client::State::k_closed), |
| 198 | ClientParams(DELTA_TO_RELEASE_1, |
| 199 | iface::Client::State::k_accepted_with_session), |
| 200 | ClientParams(DELTA_TO_RELEASE_1, iface::Client::State::k_running), |
| 201 | ClientParams(DELTA_TO_RELEASE_1, iface::Client::State::k_closing), |
| 202 | ClientParams(DELTA_TO_RELEASE_1, iface::Client::State::k_closed))); |
| 203 | |
| 204 | TEST_F( |
| 205 | Server_client_timeout_test_suite, |
| 206 | returnDateOfOldestProcessedClient_whenMultipleValidNonAuthClientProcessed) { |
| 207 | expectClientValid(TP_NOT_TO_RELEASE_1, iface::Client::State::k_accepted); |
| 208 | expectClientValid(TP_NOT_TO_RELEASE_2, iface::Client::State::k_accepted); |
| 209 | expectClientValid(TP_NOT_TO_RELEASE_3, iface::Client::State::k_accepted); |
| 210 | |
| 211 | ASSERT_TRUE(xpl::chrono::is_valid(sut->get_oldest_client_accept_time())); |
| 212 | ASSERT_EQ(TP_NOT_TO_RELEASE_3, sut->get_oldest_client_accept_time()); |
| 213 | } |
| 214 | |
| 215 | class Server_client_timeout_test_suite_param |
| 216 | : public Server_client_timeout_test_suite, |
| 217 | public testing::WithParamInterface<iface::Client::State> {}; |
| 218 | |
| 219 | TEST_P(Server_client_timeout_test_suite_param, |
| 220 | returnDateOfOldestNotExpiredNotAuthClient_whenWithMixedClientSet) { |
| 221 | expectClientValid(TP_NOT_TO_RELEASE_1, iface::Client::State::k_accepted); |
| 222 | expectClientValid(TP_NOT_TO_RELEASE_2, iface::Client::State::k_accepted); |
| 223 | expectClientValid(TP_NOT_TO_RELEASE_3, iface::Client::State::k_accepted); |
| 224 | expectClientNotValid(TP_TO_RELEASE_1, GetParam()); |
| 225 | |
| 226 | ASSERT_TRUE(xpl::chrono::is_valid(sut->get_oldest_client_accept_time())); |
| 227 | ASSERT_EQ(TP_NOT_TO_RELEASE_3, sut->get_oldest_client_accept_time()); |
| 228 | } |
| 229 | |
| 230 | INSTANTIATE_TEST_SUITE_P(InstantiationOfTargetedStates, |
| 231 | Server_client_timeout_test_suite_param, |
| 232 | Values(iface::Client::State::k_invalid, |
| 233 | iface::Client::State::k_accepted, |
| 234 | iface::Client::State::k_authenticating_first)); |
| 235 | |
| 236 | TEST_F(Server_client_timeout_test_suite, |
| 237 | returnInvalidDate_whenAllClientAreAuthenticated) { |
| 238 | expectClientValid(TP_TO_RELEASE_1, iface::Client::State::k_running); |
| 239 | expectClientValid(TP_TO_RELEASE_2, iface::Client::State::k_closing); |
| 240 | expectClientValid(TP_TO_RELEASE_3, iface::Client::State::k_closing); |
| 241 | |
| 242 | ASSERT_FALSE(xpl::chrono::is_valid(sut->get_oldest_client_accept_time())); |
| 243 | } |
| 244 | |
| 245 | } // namespace test |
| 246 | } // namespace xpl |