| 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 | #include <string> |
| 27 | |
| 28 | #include "plugin/x/src/account_verification_handler.h" |
| 29 | #include "plugin/x/src/sql_user_require.h" |
| 30 | #include "plugin/x/src/xpl_resultset.h" |
| 31 | #include "unittest/gunit/xplugin/xpl/mock/account_verification.h" |
| 32 | #include "unittest/gunit/xplugin/xpl/mock/authentication.h" |
| 33 | #include "unittest/gunit/xplugin/xpl/mock/client.h" |
| 34 | #include "unittest/gunit/xplugin/xpl/mock/session.h" |
| 35 | #include "unittest/gunit/xplugin/xpl/mock/sql_session.h" |
| 36 | #include "unittest/gunit/xplugin/xpl/mock/vio.h" |
| 37 | #include "unittest/gunit/xplugin/xpl/one_row_resultset.h" |
| 38 | |
| 39 | namespace xpl { |
| 40 | |
| 41 | #define ER_SUCCESS 0 |
| 42 | #define NOT ! |
| 43 | |
| 44 | namespace test { |
| 45 | using namespace ::testing; // NOLINT(build/namespaces) |
| 46 | |
| 47 | namespace { |
| 48 | const char *const EMPTY = ""; |
| 49 | const char *const USER_NAME = "TEST"; |
| 50 | const char *const USER_DB = "TEST_DB"; |
| 51 | const char *const USER_IP = "100.20.20.10"; |
| 52 | const char *const EXPECTED_HASH = "AABBCCDD"; |
| 53 | const char *const NOT_EXPECTED_HASH = "ALA_MA_KOTA"; |
| 54 | const bool REQUIRE_SECURE_TRANSPORT = true; |
| 55 | const bool ACCOUNT_LOCKED = true; |
| 56 | const bool PASSWORD_EXPIRED = true; |
| 57 | const bool DISCONNECT_ON_EXPIRED_PASSWORD = true; |
| 58 | const bool OFFLINE_MODE = true; |
| 59 | const char *const AUTH_PLUGIN_NAME = "mysql_native_password"; |
| 60 | const char *const WRONG_AUTH_PLUGIN_NAME = "wrong_password"; |
| 61 | } // namespace |
| 62 | |
| 63 | class User_verification_test : public Test { |
| 64 | public: |
| 65 | StrictMock<mock::Client> mock_client; |
| 66 | StrictMock<mock::Session> mock_session; |
| 67 | StrictMock<mock::Vio> mock_connection; |
| 68 | StrictMock<mock::Sql_session> mock_sql_session; |
| 69 | mock::Account_verification *mock_account_verification{ |
| 70 | new StrictMock<mock::Account_verification>()}; |
| 71 | |
| 72 | iface::Authentication_info m_auth_info; |
| 73 | |
| 74 | Account_verification_handler handler{ |
| 75 | &mock_session, iface::Account_verification::Account_type::k_native, |
| 76 | mock_account_verification}; |
| 77 | |
| 78 | void SetUp() override { |
| 79 | EXPECT_CALL(mock_session, data_context()) |
| 80 | .WillRepeatedly(ReturnRef(mock_sql_session)); |
| 81 | EXPECT_CALL(mock_session, client()).WillRepeatedly(ReturnRef(mock_client)); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | TEST_F(User_verification_test, everything_matches_and_hash_is_right) { |
| 86 | One_row_resultset data{NOT REQUIRE_SECURE_TRANSPORT, |
| 87 | EXPECTED_HASH, |
| 88 | AUTH_PLUGIN_NAME, |
| 89 | NOT ACCOUNT_LOCKED, |
| 90 | NOT PASSWORD_EXPIRED, |
| 91 | NOT DISCONNECT_ON_EXPIRED_PASSWORD, |
| 92 | NOT OFFLINE_MODE, |
| 93 | EMPTY, |
| 94 | EMPTY, |
| 95 | EMPTY, |
| 96 | EMPTY}; |
| 97 | |
| 98 | EXPECT_CALL(mock_sql_session, execute(_, _, _)) |
| 99 | .WillOnce(DoAll(SetUpResultset(data), Return(ngs::Success()))); |
| 100 | |
| 101 | EXPECT_CALL(mock_client, connection()) |
| 102 | .WillRepeatedly(ReturnRef(mock_connection)); |
| 103 | |
| 104 | EXPECT_CALL(*mock_account_verification, |
| 105 | verify_authentication_string(_, _, _, _)) |
| 106 | .WillOnce(Return(true)); |
| 107 | |
| 108 | EXPECT_EQ( |
| 109 | ER_SUCCESS, |
| 110 | handler.verify_account(USER_NAME, USER_IP, EXPECTED_HASH, &m_auth_info) |
| 111 | .error); |
| 112 | } |
| 113 | |
| 114 | TEST_F(User_verification_test, forwards_error_from_query_execution) { |
| 115 | const ngs::Error_code expected_error(ER_DATA_OUT_OF_RANGE, ""); |
| 116 | |
| 117 | EXPECT_CALL(mock_sql_session, execute(_, _, _)) |
| 118 | .WillOnce(Return(expected_error)); |
| 119 | |
| 120 | EXPECT_EQ( |
| 121 | expected_error.error, |
| 122 | handler.verify_account(USER_NAME, USER_IP, EXPECTED_HASH, &m_auth_info) |
| 123 | .error); |
| 124 | } |
| 125 | |
| 126 | TEST_F(User_verification_test, dont_match_anything_when_hash_isnt_right) { |
| 127 | One_row_resultset data{NOT REQUIRE_SECURE_TRANSPORT, |
| 128 | NOT_EXPECTED_HASH, |
| 129 | AUTH_PLUGIN_NAME, |
| 130 | NOT ACCOUNT_LOCKED, |
| 131 | NOT PASSWORD_EXPIRED, |
| 132 | NOT DISCONNECT_ON_EXPIRED_PASSWORD, |
| 133 | NOT OFFLINE_MODE, |
| 134 | EMPTY, |
| 135 | EMPTY, |
| 136 | EMPTY, |
| 137 | EMPTY}; |
| 138 | |
| 139 | EXPECT_CALL(mock_sql_session, execute(_, _, _)) |
| 140 | .WillOnce(DoAll(SetUpResultset(data), Return(ngs::Success()))); |
| 141 | |
| 142 | EXPECT_CALL(*mock_account_verification, |
| 143 | verify_authentication_string(_, _, _, _)) |
| 144 | .WillOnce(Return(false)); |
| 145 | |
| 146 | EXPECT_EQ( |
| 147 | ER_ACCESS_DENIED_ERROR, |
| 148 | handler.verify_account(USER_NAME, USER_IP, EXPECTED_HASH, &m_auth_info) |
| 149 | .error); |
| 150 | } |
| 151 | |
| 152 | TEST_F(User_verification_test, |
| 153 | everything_matches_and_hash_is_right_and_autocommit_off) { |
| 154 | One_row_resultset data{NOT REQUIRE_SECURE_TRANSPORT, |
| 155 | EXPECTED_HASH, |
| 156 | AUTH_PLUGIN_NAME, |
| 157 | NOT ACCOUNT_LOCKED, |
| 158 | NOT PASSWORD_EXPIRED, |
| 159 | NOT DISCONNECT_ON_EXPIRED_PASSWORD, |
| 160 | NOT OFFLINE_MODE, |
| 161 | EMPTY, |
| 162 | EMPTY, |
| 163 | EMPTY, |
| 164 | EMPTY}; |
| 165 | data.set_server_status(SERVER_STATUS_IN_TRANS); |
| 166 | |
| 167 | EXPECT_CALL(mock_sql_session, execute(_, _, _)) |
| 168 | .WillRepeatedly(DoAll(SetUpResultset(data), Return(ngs::Success()))); |
| 169 | |
| 170 | const std::string k_commit = "commit"; |
| 171 | EXPECT_CALL(mock_sql_session, execute(k_commit.data(), k_commit.length(), _)) |
| 172 | .WillRepeatedly(Return(ngs::Success())); |
| 173 | |
| 174 | EXPECT_CALL(mock_client, connection()) |
| 175 | .WillRepeatedly(ReturnRef(mock_connection)); |
| 176 | |
| 177 | EXPECT_CALL(*mock_account_verification, |
| 178 | verify_authentication_string(_, _, _, _)) |
| 179 | .WillOnce(Return(true)); |
| 180 | |
| 181 | EXPECT_EQ( |
| 182 | ER_SUCCESS, |
| 183 | handler.verify_account(USER_NAME, USER_IP, EXPECTED_HASH, &m_auth_info) |
| 184 | .error); |
| 185 | } |
| 186 | |
| 187 | struct Test_param { |
| 188 | bool account_locked; |
| 189 | bool offline_mode; |
| 190 | bool password_expired; |
| 191 | std::string plugin_name; |
| 192 | int expected_error; |
| 193 | }; |
| 194 | |
| 195 | class User_verification_param_test : public User_verification_test, |
| 196 | public WithParamInterface<Test_param> {}; |
| 197 | |
| 198 | TEST_P(User_verification_param_test, User_verification_on_given_account_param) { |
| 199 | const Test_param ¶m = GetParam(); |
| 200 | |
| 201 | One_row_resultset data{NOT REQUIRE_SECURE_TRANSPORT, |
| 202 | EXPECTED_HASH, |
| 203 | param.plugin_name.c_str(), |
| 204 | param.account_locked, |
| 205 | param.password_expired, |
| 206 | NOT DISCONNECT_ON_EXPIRED_PASSWORD, |
| 207 | param.offline_mode, |
| 208 | EMPTY, |
| 209 | EMPTY, |
| 210 | EMPTY, |
| 211 | EMPTY}; |
| 212 | |
| 213 | EXPECT_CALL(mock_client, client_hostname_or_address()) |
| 214 | .WillRepeatedly(Return("")); |
| 215 | |
| 216 | EXPECT_CALL(mock_sql_session, execute(_, _, _)) |
| 217 | .WillOnce(DoAll(SetUpResultset(data), Return(ngs::Success()))); |
| 218 | |
| 219 | if (param.plugin_name == AUTH_PLUGIN_NAME) |
| 220 | EXPECT_CALL(*mock_account_verification, |
| 221 | verify_authentication_string(_, _, _, _)) |
| 222 | .WillOnce(Return(true)); |
| 223 | |
| 224 | EXPECT_EQ( |
| 225 | param.expected_error, |
| 226 | handler.verify_account(USER_NAME, USER_IP, EXPECTED_HASH, &m_auth_info) |
| 227 | .error); |
| 228 | } |
| 229 | |
| 230 | Test_param combinations[] = { |
| 231 | {ACCOUNT_LOCKED, NOT OFFLINE_MODE, NOT PASSWORD_EXPIRED, AUTH_PLUGIN_NAME, |
| 232 | ER_ACCOUNT_HAS_BEEN_LOCKED}, |
| 233 | {NOT ACCOUNT_LOCKED, NOT OFFLINE_MODE, PASSWORD_EXPIRED, AUTH_PLUGIN_NAME, |
| 234 | ER_MUST_CHANGE_PASSWORD_LOGIN}, |
| 235 | {NOT ACCOUNT_LOCKED, OFFLINE_MODE, NOT PASSWORD_EXPIRED, AUTH_PLUGIN_NAME, |
| 236 | ER_SERVER_OFFLINE_MODE}, |
| 237 | {NOT ACCOUNT_LOCKED, NOT OFFLINE_MODE, NOT PASSWORD_EXPIRED, |
| 238 | WRONG_AUTH_PLUGIN_NAME, ER_ACCESS_DENIED_ERROR}}; |
| 239 | |
| 240 | INSTANTIATE_TEST_SUITE_P(User_verification, User_verification_param_test, |
| 241 | ValuesIn(combinations)); |
| 242 | |
| 243 | struct Test_param_connection_type { |
| 244 | bool requires_secure; |
| 245 | Connection_type type; |
| 246 | int expected_error; |
| 247 | }; |
| 248 | |
| 249 | class User_verification_param_test_with_connection_type_combinations |
| 250 | : public User_verification_test, |
| 251 | public WithParamInterface<Test_param_connection_type> {}; |
| 252 | |
| 253 | TEST_P(User_verification_param_test_with_connection_type_combinations, |
| 254 | User_verification_on_given_account_connection_type) { |
| 255 | const Test_param_connection_type ¶m = GetParam(); |
| 256 | |
| 257 | EXPECT_CALL(mock_client, connection()) |
| 258 | .WillRepeatedly(ReturnRef(mock_connection)); |
| 259 | |
| 260 | if (param.requires_secure) |
| 261 | EXPECT_CALL(mock_connection, get_type()).WillOnce(Return(param.type)); |
| 262 | |
| 263 | EXPECT_CALL(*mock_account_verification, |
| 264 | verify_authentication_string(_, _, _, _)) |
| 265 | .WillOnce(Return(true)); |
| 266 | |
| 267 | One_row_resultset data{param.requires_secure, |
| 268 | EXPECTED_HASH, |
| 269 | AUTH_PLUGIN_NAME, |
| 270 | NOT ACCOUNT_LOCKED, |
| 271 | NOT PASSWORD_EXPIRED, |
| 272 | NOT DISCONNECT_ON_EXPIRED_PASSWORD, |
| 273 | NOT OFFLINE_MODE, |
| 274 | EMPTY, |
| 275 | EMPTY, |
| 276 | EMPTY, |
| 277 | EMPTY}; |
| 278 | |
| 279 | EXPECT_CALL(mock_sql_session, execute(_, _, _)) |
| 280 | .WillOnce(DoAll(SetUpResultset(data), Return(ngs::Success()))); |
| 281 | |
| 282 | EXPECT_EQ( |
| 283 | param.expected_error, |
| 284 | handler.verify_account(USER_NAME, USER_IP, EXPECTED_HASH, &m_auth_info) |
| 285 | .error); |
| 286 | } |
| 287 | |
| 288 | Test_param_connection_type connection_combinations[] = { |
| 289 | {NOT REQUIRE_SECURE_TRANSPORT, Connection_tcpip, ER_SUCCESS}, |
| 290 | {NOT REQUIRE_SECURE_TRANSPORT, Connection_namedpipe, ER_SUCCESS}, |
| 291 | {NOT REQUIRE_SECURE_TRANSPORT, Connection_tls, ER_SUCCESS}, |
| 292 | {NOT REQUIRE_SECURE_TRANSPORT, Connection_unixsocket, ER_SUCCESS}, |
| 293 | {REQUIRE_SECURE_TRANSPORT, Connection_unixsocket, ER_SUCCESS}, |
| 294 | {REQUIRE_SECURE_TRANSPORT, Connection_tls, ER_SUCCESS}, |
| 295 | {REQUIRE_SECURE_TRANSPORT, Connection_tcpip, ER_SECURE_TRANSPORT_REQUIRED}, |
| 296 | {REQUIRE_SECURE_TRANSPORT, Connection_namedpipe, |
| 297 | ER_SECURE_TRANSPORT_REQUIRED}}; |
| 298 | |
| 299 | INSTANTIATE_TEST_SUITE_P( |
| 300 | User_verification, |
| 301 | User_verification_param_test_with_connection_type_combinations, |
| 302 | ValuesIn(connection_combinations)); |
| 303 | |
| 304 | struct Test_param_sasl_message { |
| 305 | std::string schema; |
| 306 | std::string user; |
| 307 | std::string password; |
| 308 | int expected_error; |
| 309 | std::string get_message() const { |
| 310 | return schema + '\0' + user + '\0' + password; |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | class Split_sasl_message_test |
| 315 | : public User_verification_test, |
| 316 | public WithParamInterface<Test_param_sasl_message> { |
| 317 | public: |
| 318 | StrictMock<mock::Authentication> mock_authentication; |
| 319 | }; |
| 320 | |
| 321 | TEST_P(Split_sasl_message_test, Split_sasl_message_on_given_param) { |
| 322 | const Test_param_sasl_message ¶m = GetParam(); |
| 323 | |
| 324 | if (param.expected_error == ER_SUCCESS) { |
| 325 | EXPECT_CALL(mock_client, client_address()); |
| 326 | EXPECT_CALL(mock_client, client_hostname()); |
| 327 | EXPECT_CALL(mock_client, supports_expired_passwords()); |
| 328 | EXPECT_CALL(mock_sql_session, password_expired()).WillOnce(Return(false)); |
| 329 | EXPECT_CALL(mock_session, data_context()) |
| 330 | .WillOnce(ReturnRef(mock_sql_session)); |
| 331 | EXPECT_CALL( |
| 332 | mock_sql_session, |
| 333 | authenticate(StrEq(param.user), _, _, StrEq(param.schema), |
| 334 | StrEq(param.password), Ref(mock_authentication), _)) |
| 335 | .WillOnce(Return(ngs::Success())); |
| 336 | } |
| 337 | |
| 338 | EXPECT_EQ( |
| 339 | param.expected_error, |
| 340 | handler |
| 341 | .authenticate(mock_authentication, &m_auth_info, param.get_message()) |
| 342 | .error); |
| 343 | } |
| 344 | |
| 345 | Test_param_sasl_message sasl_message[] = { |
| 346 | {EMPTY, EMPTY, EMPTY, ER_ACCESS_DENIED_ERROR}, |
| 347 | {USER_DB, EMPTY, EMPTY, ER_ACCESS_DENIED_ERROR}, |
| 348 | {EMPTY, USER_NAME, EMPTY, ER_SUCCESS}, |
| 349 | {EMPTY, EMPTY, EXPECTED_HASH, ER_ACCESS_DENIED_ERROR}, |
| 350 | {USER_DB, USER_NAME, EMPTY, ER_SUCCESS}, |
| 351 | {EMPTY, USER_NAME, EXPECTED_HASH, ER_SUCCESS}, |
| 352 | {USER_DB, EMPTY, EXPECTED_HASH, ER_ACCESS_DENIED_ERROR}, |
| 353 | {USER_DB, USER_NAME, EXPECTED_HASH, ER_SUCCESS}}; |
| 354 | |
| 355 | INSTANTIATE_TEST_SUITE_P(User_verification, Split_sasl_message_test, |
| 356 | ValuesIn(sasl_message)); |
| 357 | } // namespace test |
| 358 | } // namespace xpl |