| 1 | /* Copyright (c) 2017, 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/auth_challenge_response.h" |
| 28 | #include "plugin/x/src/sql_user_require.h" |
| 29 | #include "unittest/gunit/xplugin/xpl/mock/account_verification.h" |
| 30 | #include "unittest/gunit/xplugin/xpl/mock/account_verification_handler.h" |
| 31 | #include "unittest/gunit/xplugin/xpl/mock/authentication.h" |
| 32 | #include "unittest/gunit/xplugin/xpl/mock/session.h" |
| 33 | |
| 34 | namespace xpl { |
| 35 | |
| 36 | static const uint8_t ER_SUCCESS = 0; |
| 37 | |
| 38 | namespace test { |
| 39 | |
| 40 | using ::testing::_; |
| 41 | |
| 42 | namespace { |
| 43 | const char *const EMPTY = ""; |
| 44 | const char *const AUTH_DATA = "ALA_MA_KOTA"; |
| 45 | const char *const SALT = "SALT"; |
| 46 | |
| 47 | ::testing::AssertionResult assert_response( |
| 48 | const char *e1_expr, const char *e2_expr, |
| 49 | const iface::Authentication::Response &e1, |
| 50 | const iface::Authentication::Response &e2) { |
| 51 | return (e1.data == e2.data && e1.status == e2.status && |
| 52 | e1.error_code == e2.error_code) |
| 53 | ? ::testing::AssertionSuccess() |
| 54 | : (::testing::AssertionFailure() |
| 55 | << "Value of: " << e2_expr << "\nActual: {" |
| 56 | << static_cast<uint32_t>(e2.status) << ", " << e2.error_code |
| 57 | << ", " << e2.data << "}\n" |
| 58 | << "Expected: " << e1_expr << "\nWhich is: {" |
| 59 | << static_cast<uint32_t>(e1.status) << ", " << e1.error_code |
| 60 | << ", " << e1.data << "}"); |
| 61 | } |
| 62 | |
| 63 | template <typename T1, typename T2> |
| 64 | void ASSERT_RESPONSE(const T1 &a, const T2 &b) { |
| 65 | ASSERT_PRED_FORMAT2(assert_response, a, b); |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | struct Auth_selector { |
| 71 | std::string m_name; |
| 72 | iface::Account_verification::Account_type m_verificator_type; |
| 73 | }; |
| 74 | |
| 75 | class Sasl_challenge_response_auth_test |
| 76 | : public ::testing::TestWithParam<Auth_selector> { |
| 77 | public: |
| 78 | void SetUp() override { |
| 79 | if (GetParam().m_name == "SHA256_MEMORY") |
| 80 | auth = std::unique_ptr<Sasl_sha256_memory_auth>( |
| 81 | new Sasl_sha256_memory_auth(mock_handler)); |
| 82 | else if (GetParam().m_name == "MYSQL41") |
| 83 | auth = std::unique_ptr<Sasl_mysql41_auth>( |
| 84 | new Sasl_mysql41_auth(mock_handler)); |
| 85 | else |
| 86 | throw std::logic_error("Invalid test case auth method"); |
| 87 | } |
| 88 | |
| 89 | ::testing::StrictMock<mock::Account_verification_handler> *mock_handler{ |
| 90 | new ::testing::StrictMock<mock::Account_verification_handler>()}; |
| 91 | std::unique_ptr<iface::Authentication> auth; |
| 92 | ::testing::StrictMock<mock::Account_verification> mock_account_verification; |
| 93 | ::testing::StrictMock<mock::Authentication> mock_authentication; |
| 94 | |
| 95 | using Response = iface::Authentication::Response; |
| 96 | }; |
| 97 | |
| 98 | TEST_P(Sasl_challenge_response_auth_test, handle_start_get_salt) { |
| 99 | EXPECT_CALL(*mock_handler, |
| 100 | get_account_verificator(GetParam().m_verificator_type)) |
| 101 | .WillOnce(::testing::Return(&mock_account_verification)); |
| 102 | |
| 103 | EXPECT_CALL(mock_account_verification, get_salt()) |
| 104 | .WillOnce(::testing::ReturnRefOfCopy(std::string(SALT))); |
| 105 | |
| 106 | ASSERT_RESPONSE( |
| 107 | Response(iface::Authentication::Status::k_ongoing, ER_SUCCESS, SALT), |
| 108 | auth->handle_start(GetParam().m_name, AUTH_DATA, EMPTY)); |
| 109 | } |
| 110 | |
| 111 | TEST_P(Sasl_challenge_response_auth_test, handle_start_call_twice) { |
| 112 | EXPECT_CALL(*mock_handler, |
| 113 | get_account_verificator(GetParam().m_verificator_type)) |
| 114 | .WillOnce(::testing::Return(&mock_account_verification)); |
| 115 | |
| 116 | EXPECT_CALL(mock_account_verification, get_salt()) |
| 117 | .WillOnce(::testing::ReturnRefOfCopy(std::string(SALT))); |
| 118 | |
| 119 | ASSERT_RESPONSE( |
| 120 | Response(iface::Authentication::Status::k_ongoing, ER_SUCCESS, SALT), |
| 121 | auth->handle_start(GetParam().m_name, AUTH_DATA, EMPTY)); |
| 122 | ASSERT_RESPONSE(Response(iface::Authentication::Status::k_error, |
| 123 | ER_NET_PACKETS_OUT_OF_ORDER, EMPTY), |
| 124 | auth->handle_start(GetParam().m_name, AUTH_DATA, EMPTY)); |
| 125 | } |
| 126 | |
| 127 | TEST_P(Sasl_challenge_response_auth_test, |
| 128 | handle_continue_without_previous_start) { |
| 129 | ASSERT_RESPONSE(Response(iface::Authentication::Status::k_error, |
| 130 | ER_NET_PACKETS_OUT_OF_ORDER, EMPTY), |
| 131 | auth->handle_continue(AUTH_DATA)); |
| 132 | } |
| 133 | |
| 134 | TEST_P(Sasl_challenge_response_auth_test, handle_continue_succeeded) { |
| 135 | EXPECT_CALL(*mock_handler, |
| 136 | get_account_verificator(GetParam().m_verificator_type)) |
| 137 | .WillOnce(::testing::Return(&mock_account_verification)); |
| 138 | |
| 139 | EXPECT_CALL(mock_account_verification, get_salt()) |
| 140 | .WillOnce(::testing::ReturnRefOfCopy(std::string(SALT))); |
| 141 | |
| 142 | ASSERT_RESPONSE( |
| 143 | Response(iface::Authentication::Status::k_ongoing, ER_SUCCESS, SALT), |
| 144 | auth->handle_start(GetParam().m_name, AUTH_DATA, EMPTY)); |
| 145 | |
| 146 | EXPECT_CALL(*mock_handler, authenticate(_, _, AUTH_DATA)) |
| 147 | .WillOnce(::testing::Return(ngs::Success())); |
| 148 | |
| 149 | ASSERT_RESPONSE( |
| 150 | Response(iface::Authentication::Status::k_succeeded, ER_SUCCESS, EMPTY), |
| 151 | auth->handle_continue(AUTH_DATA)); |
| 152 | } |
| 153 | |
| 154 | TEST_P(Sasl_challenge_response_auth_test, handle_continue_failed) { |
| 155 | EXPECT_CALL(*mock_handler, |
| 156 | get_account_verificator(GetParam().m_verificator_type)) |
| 157 | .WillOnce(::testing::Return(&mock_account_verification)); |
| 158 | |
| 159 | EXPECT_CALL(mock_account_verification, get_salt()) |
| 160 | .WillOnce(::testing::ReturnRefOfCopy(std::string(SALT))); |
| 161 | |
| 162 | ASSERT_RESPONSE( |
| 163 | Response(iface::Authentication::Status::k_ongoing, ER_SUCCESS, SALT), |
| 164 | auth->handle_start(GetParam().m_name, AUTH_DATA, EMPTY)); |
| 165 | |
| 166 | ngs::Error_code expect_error(ER_NO_SUCH_USER, "Invalid user or password"); |
| 167 | EXPECT_CALL(*mock_handler, authenticate(_, _, AUTH_DATA)) |
| 168 | .WillOnce(::testing::Return(expect_error)); |
| 169 | |
| 170 | ASSERT_RESPONSE(Response(iface::Authentication::Status::k_failed, |
| 171 | expect_error.error, expect_error.message), |
| 172 | auth->handle_continue(AUTH_DATA)); |
| 173 | } |
| 174 | |
| 175 | INSTANTIATE_TEST_SUITE_P( |
| 176 | Instantiation_auth_mechanism, Sasl_challenge_response_auth_test, |
| 177 | ::testing::Values( |
| 178 | Auth_selector{ |
| 179 | "SHA256_MEMORY", |
| 180 | iface::Account_verification::Account_type::k_sha256_memory}, |
| 181 | Auth_selector{"MYSQL41", |
| 182 | iface::Account_verification::Account_type::k_native})); |
| 183 | |
| 184 | } // namespace test |
| 185 | } // namespace xpl |