Debian Codegraph @

unittest/gunit/xplugin/xpl/sasl_authentication_t.cc

raw symbols SCIP
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 <stdexcept>
27
28 #include "my_config.h" // NOLINT(build/include_subdir)
29
30 #include "plugin/x/src/auth_plain.h"
31 #include "plugin/x/src/sql_user_require.h"
32 #include "unittest/gunit/xplugin/xpl/mock/client.h"
33 #include "unittest/gunit/xplugin/xpl/mock/session.h"
34 #include "unittest/gunit/xplugin/xpl/mock/sql_session.h"
35 #include "unittest/gunit/xplugin/xpl/mock/vio.h"
36
37 namespace xpl {
38
39 namespace test {
40
41 using namespace ::testing; // NOLINT(build/namespaces)
42
43 template <typename Auth_type>
44 class AuthenticationTestSuite : public Test {
45 public:
46 void SetUp() override {
47 sut = Auth_type::create(&mock_session, nullptr);
48
49 ON_CALL(mock_data_context, authenticate(_, _, _, _, _, _, _))
50 .WillByDefault(Return(default_error));
51 EXPECT_CALL(mock_connection, get_type())
52 .WillRepeatedly(Return(Connection_tls));
53 EXPECT_CALL(mock_client, connection())
54 .WillRepeatedly(ReturnRef(mock_connection));
55 EXPECT_CALL(mock_session, data_context())
56 .WillRepeatedly(ReturnRef(mock_data_context));
57 EXPECT_CALL(mock_session, client()).WillRepeatedly(ReturnRef(mock_client));
58 }
59
60 void assert_responce(
61 const iface::Authentication::Response &result,
62 const std::string &data = "",
63 const iface::Authentication::Status status =
64 iface::Authentication::Status::k_error,
65 const int error_code = ER_NET_PACKETS_OUT_OF_ORDER) const {
66 ASSERT_EQ(data, result.data);
67 ASSERT_EQ(status, result.status);
68 ASSERT_EQ(error_code, result.error_code);
69 }
70
71 ngs::Error_code default_error;
72
73 StrictMock<mock::Sql_session> mock_data_context;
74 StrictMock<mock::Client> mock_client;
75 StrictMock<mock::Vio> mock_connection;
76 StrictMock<mock::Session> mock_session;
77 std::unique_ptr<iface::Authentication> sut;
78 };
79
80 typedef AuthenticationTestSuite<Sasl_plain_auth> SaslAuthenticationTestSuite;
81
82 TEST_F(SaslAuthenticationTestSuite, handleContinue_fails_always) {
83 iface::Authentication::Response result = sut->handle_continue("");
84
85 assert_responce(result, "", iface::Authentication::Status::k_error,
86 ER_NET_PACKETS_OUT_OF_ORDER);
87 }
88
89 template <typename Auth_type>
90 class ExpectedValuesAuthenticationTestSuite
91 : public AuthenticationTestSuite<Auth_type> {
92 public:
93 ExpectedValuesAuthenticationTestSuite()
94 : expected_database("test_database"),
95 expected_login("test_login"),
96 expected_password("test_password"),
97 expected_password_hash("*4414E26EDED6D661B5386813EBBA95065DBC4728"),
98 expected_host("test_host"),
99 expected_hostname("test_host"),
100 sasl_separator("\0", 1),
101 ec_failur(1, ""),
102 ec_success(0, "") {}
103
104 std::string get_sasl_message(const std::string &login,
105 const std::string &password,
106 const std::string &autorization = "") {
107 return autorization + sasl_separator + login + sasl_separator + password;
108 }
109
110 const char *expected_database;
111 const char *expected_login;
112 const char *expected_password;
113 const char *expected_password_hash;
114 const char *expected_host;
115 const std::string expected_hostname;
116
117 const std::string sasl_separator;
118
119 const ngs::Error_code ec_failur;
120 const ngs::Error_code ec_success;
121 };
122
123 typedef ExpectedValuesAuthenticationTestSuite<Sasl_plain_auth>
124 ExpectedValuesSaslAuthenticationTestSuite;
125
126 TEST_F(ExpectedValuesSaslAuthenticationTestSuite,
127 handleStart_autenticateAndReturnsError_whenIllformedStringNoSeparator) {
128 std::string sasl_login_string = expected_login;
129
130 iface::Authentication::Response result =
131 sut->handle_start("", sasl_login_string, "");
132
133 assert_responce(result, "Invalid user or password",
134 iface::Authentication::Status::k_failed,
135 ER_ACCESS_DENIED_ERROR);
136 }
137
138 TEST_F(ExpectedValuesSaslAuthenticationTestSuite,
139 handleStart_autenticateAndReturnsError_whenIllformedStringOneSeparator) {
140 std::string sasl_login_string = "some data" + sasl_separator + "some data";
141
142 iface::Authentication::Response result =
143 sut->handle_start("", sasl_login_string, "");
144
145 assert_responce(result, "Invalid user or password",
146 iface::Authentication::Status::k_failed,
147 ER_ACCESS_DENIED_ERROR);
148 }
149
150 TEST_F(
151 ExpectedValuesSaslAuthenticationTestSuite,
152 handleStart_autenticateAndReturnsError_whenIllformedStringThusUserNameEmpty) { // NOLINT(whitespace/line_length)
153 const std::string empty_user = "";
154 std::string sasl_login_string =
155 get_sasl_message(empty_user, expected_password, "autorize_as");
156
157 iface::Authentication::Response result =
158 sut->handle_start("", sasl_login_string, "");
159
160 assert_responce(result, "Invalid user or password",
161 iface::Authentication::Status::k_failed,
162 ER_ACCESS_DENIED_ERROR);
163 }
164
165 TEST_F(ExpectedValuesSaslAuthenticationTestSuite,
166 handleStart_autenticateAndReturnsSuccess_whenPasswordEmptyButValid) {
167 const std::string empty_password = "";
168 std::string sasl_login_string =
169 get_sasl_message(expected_login, empty_password);
170 EXPECT_CALL(mock_client, client_address()).WillOnce(Return(expected_host));
171 EXPECT_CALL(mock_client, supports_expired_passwords())
172 .WillOnce(Return(false));
173 EXPECT_CALL(mock_client, client_hostname())
174 .WillOnce(Return(expected_hostname.c_str()));
175 EXPECT_CALL(mock_data_context, password_expired()).WillOnce(Return(false));
176 EXPECT_CALL(
177 mock_data_context,
178 authenticate(StrEq(expected_login), StrEq(expected_hostname.c_str()),
179 StrEq(expected_host), StrEq(""), _, _, false))
180 .WillOnce(Return(ec_success));
181
182 iface::Authentication::Response result =
183 sut->handle_start("", sasl_login_string, "");
184
185 assert_responce(result, "", iface::Authentication::Status::k_succeeded, 0);
186 }
187
188 TEST_F(ExpectedValuesSaslAuthenticationTestSuite,
189 handleStart_autenticateAndReturnsSuccess_whenAuthSucceeded) {
190 std::string sasl_login_string =
191 get_sasl_message(expected_login, expected_password, expected_database);
192
193 EXPECT_CALL(mock_client, client_address()).WillOnce(Return(expected_host));
194 EXPECT_CALL(mock_client, supports_expired_passwords())
195 .WillOnce(Return(false));
196 EXPECT_CALL(mock_client, client_hostname())
197 .WillOnce(Return(expected_hostname.c_str()));
198 EXPECT_CALL(
199 mock_data_context,
200 authenticate(StrEq(expected_login), StrEq(expected_hostname),
201 StrEq(expected_host), StrEq(expected_database), _, _, false))
202 .WillOnce(Return(ec_success));
203 EXPECT_CALL(mock_data_context, password_expired()).WillOnce(Return(false));
204
205 iface::Authentication::Response result =
206 sut->handle_start("", sasl_login_string, "");
207
208 assert_responce(result, "", iface::Authentication::Status::k_succeeded, 0);
209 }
210
211 TEST_F(ExpectedValuesSaslAuthenticationTestSuite,
212 handleStart_autenticateAndReturnsFailure_whenAuthFailures) {
213 std::string sasl_login_string =
214 get_sasl_message(expected_login, expected_password, expected_database);
215
216 EXPECT_CALL(mock_client, client_address()).WillOnce(Return(expected_host));
217 EXPECT_CALL(mock_client, client_hostname())
218 .WillOnce(Return(expected_hostname.c_str()));
219 EXPECT_CALL(mock_client, supports_expired_passwords())
220 .WillOnce(Return(false));
221 EXPECT_CALL(
222 mock_data_context,
223 authenticate(StrEq(expected_login), StrEq(expected_host),
224 StrEq(expected_host), StrEq(expected_database), _, _, false))
225 .WillOnce(Return(ec_failur));
226
227 iface::Authentication::Response result =
228 sut->handle_start("", sasl_login_string, "");
229
230 assert_responce(result, "", iface::Authentication::Status::k_failed, 1);
231 }
232
233 } // namespace test
234
235 } // namespace xpl