| 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_plain.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/session.h" |
| 32 | |
| 33 | namespace xpl { |
| 34 | |
| 35 | #define ER_SUCCESS 0 |
| 36 | |
| 37 | namespace test { |
| 38 | using namespace ::testing; // NOLINT(build/namespaces) |
| 39 | |
| 40 | namespace { |
| 41 | const char *const EMPTY = ""; |
| 42 | const char *const AUTH_DATA = "ALA_MA_KOTA"; |
| 43 | const char *const MECHANISM = "MYSQL41"; |
| 44 | |
| 45 | AssertionResult assert_responce(const char *e1_expr, const char *e2_expr, |
| 46 | const iface::Authentication::Response &e1, |
| 47 | const iface::Authentication::Response &e2) { |
| 48 | return (e1.data == e2.data && e1.status == e2.status && |
| 49 | e1.error_code == e2.error_code) |
| 50 | ? ::testing::AssertionSuccess() |
| 51 | : (::testing::AssertionFailure() |
| 52 | << "Value of: " << e2_expr << "\nActual: {" |
| 53 | << static_cast<uint32_t>(e2.status) << ", " << e2.error_code |
| 54 | << ", " << e2.data << "}\n" |
| 55 | << "Expected: " << e1_expr << "\nWhich is: {" |
| 56 | << static_cast<uint32_t>(e1.status) << ", " << e1.error_code |
| 57 | << ", " << e1.data << "}"); |
| 58 | } |
| 59 | |
| 60 | #define ASSERT_RESPONCE(a, b) ASSERT_PRED_FORMAT2(assert_responce, a, b); |
| 61 | } // namespace |
| 62 | |
| 63 | class Sasl_plain_auth_test : public Test { |
| 64 | public: |
| 65 | StrictMock<mock::Account_verification_handler> *mock_handler{ |
| 66 | new StrictMock<mock::Account_verification_handler>()}; |
| 67 | Sasl_plain_auth auth{mock_handler}; |
| 68 | StrictMock<mock::Account_verification> mock_account_verification; |
| 69 | |
| 70 | typedef iface::Authentication::Response Response; |
| 71 | }; |
| 72 | |
| 73 | TEST_F(Sasl_plain_auth_test, handle_start_authenticate_succeeded) { |
| 74 | EXPECT_CALL(*mock_handler, authenticate(_, _, AUTH_DATA)) |
| 75 | .WillOnce(Return(ngs::Success())); |
| 76 | |
| 77 | ASSERT_RESPONCE(Response(iface::Authentication::Status::k_succeeded), |
| 78 | auth.handle_start(MECHANISM, AUTH_DATA, EMPTY)); |
| 79 | } |
| 80 | |
| 81 | TEST_F(Sasl_plain_auth_test, handle_start_authenticate_failed) { |
| 82 | ngs::Error_code expect_error(ER_NO_SUCH_USER, "Invalid user or password"); |
| 83 | EXPECT_CALL(*mock_handler, authenticate(_, _, AUTH_DATA)) |
| 84 | .WillOnce(Return(expect_error)); |
| 85 | |
| 86 | ASSERT_RESPONCE(Response(iface::Authentication::Status::k_failed, |
| 87 | expect_error.error, expect_error.message), |
| 88 | auth.handle_start(MECHANISM, AUTH_DATA, EMPTY)); |
| 89 | } |
| 90 | |
| 91 | TEST_F(Sasl_plain_auth_test, handle_continue_without_previous_start) { |
| 92 | ASSERT_RESPONCE(Response(iface::Authentication::Status::k_error, |
| 93 | ER_NET_PACKETS_OUT_OF_ORDER, EMPTY), |
| 94 | auth.handle_continue(AUTH_DATA)); |
| 95 | } |
| 96 | |
| 97 | TEST_F(Sasl_plain_auth_test, handle_continue_allways_failed) { |
| 98 | EXPECT_CALL(*mock_handler, authenticate(_, _, AUTH_DATA)) |
| 99 | .WillOnce(Return(ngs::Success())); |
| 100 | |
| 101 | ASSERT_RESPONCE(Response(iface::Authentication::Status::k_succeeded), |
| 102 | auth.handle_start(MECHANISM, AUTH_DATA, EMPTY)); |
| 103 | |
| 104 | ASSERT_RESPONCE(Response(iface::Authentication::Status::k_error, |
| 105 | ER_NET_PACKETS_OUT_OF_ORDER, EMPTY), |
| 106 | auth.handle_continue(AUTH_DATA)); |
| 107 | } |
| 108 | |
| 109 | } // namespace test |
| 110 | } // namespace xpl |