| 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/interface/authentication.h" |
| 28 | #include "unittest/gunit/xplugin/xcl/session_t.h" |
| 29 | |
| 30 | namespace xcl { |
| 31 | namespace test { |
| 32 | |
| 33 | class Auth_chaining_test_suite_base : public Xcl_session_impl_tests { |
| 34 | public: |
| 35 | void SetUp() override { |
| 36 | m_sut = prepare_session(); |
| 37 | EXPECT_CALL(m_mock_connection_state, is_connected()) |
| 38 | .WillRepeatedly(Return(false)); |
| 39 | EXPECT_CALL(m_mock_connection, connect(_, _, _)) |
| 40 | .WillRepeatedly(Return(XError{0, ""})); |
| 41 | EXPECT_CALL(*m_mock_protocol, add_notice_handler(_, Handler_position::Begin, |
| 42 | Handler_priority_low)) |
| 43 | .WillOnce(Return(3)); |
| 44 | EXPECT_CALL(*m_mock_protocol, remove_notice_handler(3)); |
| 45 | EXPECT_CALL(*m_mock_protocol, add_send_message_handler(_, _, _)); |
| 46 | EXPECT_CALL(*m_mock_protocol, remove_send_message_handler(0)); |
| 47 | EXPECT_CALL(m_mock_connection, set_read_timeout(_)) |
| 48 | .WillRepeatedly(Return(XError{})); |
| 49 | EXPECT_CALL(m_mock_connection, set_write_timeout(_)) |
| 50 | .WillRepeatedly(Return(XError{})); |
| 51 | } |
| 52 | |
| 53 | void set_ssl_state(bool is_enabled) { |
| 54 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 55 | .WillRepeatedly(Return(is_enabled)); |
| 56 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 57 | .WillRepeatedly(Return(is_enabled)); |
| 58 | } |
| 59 | |
| 60 | void fill(Mysqlx::Datatypes::Array *cap, |
| 61 | const std::vector<std::string> &values) { |
| 62 | for (const auto &value : values) { |
| 63 | auto any = cap->add_value(); |
| 64 | any->set_type(Mysqlx::Datatypes::Any_Type_SCALAR); |
| 65 | |
| 66 | auto scalar = any->mutable_scalar(); |
| 67 | |
| 68 | scalar->set_type(Mysqlx::Datatypes::Scalar_Type_V_STRING); |
| 69 | |
| 70 | scalar->mutable_v_string()->set_value(value); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | template <typename... Types> |
| 75 | Mysqlx::Connection::Capabilities *make_capability(const Types &...values) { |
| 76 | auto result = new Mysqlx::Connection::Capabilities(); |
| 77 | auto cap = result->add_capabilities(); |
| 78 | |
| 79 | cap->set_name("authentication.mechanisms"); |
| 80 | auto value = cap->mutable_value(); |
| 81 | |
| 82 | value->set_type(Mysqlx::Datatypes::Any_Type_ARRAY); |
| 83 | |
| 84 | fill(value->mutable_array(), {values...}); |
| 85 | |
| 86 | return result; |
| 87 | } |
| 88 | const bool k_fatal = true; |
| 89 | XError m_ok_auth{ |
| 90 | static_cast<int32_t>(xpl::iface::Authentication::Status::k_succeeded), |
| 91 | ""}; |
| 92 | XError m_error_failed_auth{ER_ACCESS_DENIED_ERROR, |
| 93 | "Invalid user or password"}; |
| 94 | XError m_error_fatal_failed_auth{ER_ACCESS_DENIED_ERROR, |
| 95 | "Invalid user or password", k_fatal}; |
| 96 | XError m_error_disconnected{CR_SERVER_GONE_ERROR, "Server gone"}; |
| 97 | }; |
| 98 | |
| 99 | class Auth_chaining_test_suite |
| 100 | : public Auth_chaining_test_suite_base, |
| 101 | public ::testing::WithParamInterface<std::vector<std::string>> { |
| 102 | void SetUp() override { |
| 103 | Auth_chaining_test_suite_base::SetUp(); |
| 104 | EXPECT_CALL(*m_mock_protocol, reset_buffering()); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | TEST_F(Auth_chaining_test_suite, cap_auth_method_server_supports_nothing) { |
| 109 | set_ssl_state(true); |
| 110 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 111 | "FROM_CAPABILITIES"); |
| 112 | |
| 113 | EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw(_)) |
| 114 | .WillOnce(Return(make_capability())); |
| 115 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 116 | .WillOnce(Return(Connection_type::Tcp)); |
| 117 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 118 | |
| 119 | ASSERT_EQ(CR_X_INVALID_AUTH_METHOD, |
| 120 | m_sut->connect("host", 1290, "user", "pass", "schema").error()); |
| 121 | } |
| 122 | |
| 123 | TEST_F(Auth_chaining_test_suite, cap_auth_method_server_supports_plain_no_ssl) { |
| 124 | set_ssl_state(false); |
| 125 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 126 | "FROM_CAPABILITIES"); |
| 127 | |
| 128 | EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw(_)) |
| 129 | .WillOnce(Return(make_capability("PLAIN"))); |
| 130 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 131 | .WillOnce(Return(Connection_type::Tcp)); |
| 132 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 133 | |
| 134 | ASSERT_EQ(CR_X_INVALID_AUTH_METHOD, |
| 135 | m_sut->connect("host", 1290, "user", "pass", "schema").error()); |
| 136 | } |
| 137 | |
| 138 | TEST_F(Auth_chaining_test_suite, cap_auth_method_server_supports_plain) { |
| 139 | set_ssl_state(true); |
| 140 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 141 | "FROM_CAPABILITIES"); |
| 142 | |
| 143 | EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw(_)) |
| 144 | .WillOnce(Return(make_capability("PLAIN"))); |
| 145 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 146 | .WillOnce(Return(m_error_failed_auth)); |
| 147 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 148 | .WillOnce(Return(Connection_type::Tcp)); |
| 149 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 150 | |
| 151 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 152 | } |
| 153 | |
| 154 | TEST_F(Auth_chaining_test_suite, cap_auth_method_server_supports_mysql41) { |
| 155 | set_ssl_state(true); |
| 156 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 157 | "FROM_CAPABILITIES"); |
| 158 | |
| 159 | EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw(_)) |
| 160 | .WillOnce(Return(make_capability("MYSQL41"))); |
| 161 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 162 | .WillOnce(Return(m_error_failed_auth)); |
| 163 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 164 | .WillOnce(Return(Connection_type::Tcp)); |
| 165 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 166 | |
| 167 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 168 | } |
| 169 | |
| 170 | TEST_F(Auth_chaining_test_suite, cap_auth_method_server_supports_memory) { |
| 171 | set_ssl_state(true); |
| 172 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 173 | "FROM_CAPABILITIES"); |
| 174 | |
| 175 | EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw(_)) |
| 176 | .WillOnce(Return(make_capability("SHA256_MEMORY"))); |
| 177 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 178 | .WillOnce(Return(m_error_failed_auth)); |
| 179 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 180 | .WillOnce(Return(Connection_type::Tcp)); |
| 181 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 182 | |
| 183 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 184 | } |
| 185 | |
| 186 | TEST_F(Auth_chaining_test_suite, |
| 187 | cap_auth_method_server_supports_memory_and_unk) { |
| 188 | set_ssl_state(true); |
| 189 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 190 | "FROM_CAPABILITIES"); |
| 191 | |
| 192 | EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw(_)) |
| 193 | .WillOnce(Return(make_capability("SHA256_MEMORY", "UNK"))); |
| 194 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 195 | .WillOnce(Return(m_error_failed_auth)); |
| 196 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 197 | .WillOnce(Return(Connection_type::Tcp)); |
| 198 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 199 | |
| 200 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 201 | } |
| 202 | |
| 203 | TEST_F(Auth_chaining_test_suite, cap_auth_method_server_supports_all_ssl) { |
| 204 | set_ssl_state(true); |
| 205 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 206 | "FROM_CAPABILITIES"); |
| 207 | |
| 208 | EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw(_)) |
| 209 | .WillOnce(Return(make_capability("SHA256_MEMORY", "PLAIN", "MYSQL41"))); |
| 210 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 211 | .WillOnce(Return(m_error_failed_auth)); |
| 212 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 213 | .WillOnce(Return(m_error_failed_auth)); |
| 214 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 215 | .WillOnce(Return(m_error_failed_auth)); |
| 216 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 217 | .WillOnce(Return(Connection_type::Tcp)); |
| 218 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 219 | |
| 220 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 221 | } |
| 222 | |
| 223 | TEST_F(Auth_chaining_test_suite, cap_auth_method_server_supports_all_non_ssl) { |
| 224 | set_ssl_state(false); |
| 225 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 226 | "FROM_CAPABILITIES"); |
| 227 | |
| 228 | EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw(_)) |
| 229 | .WillOnce(Return(make_capability("SHA256_MEMORY", "MYSQL41"))); |
| 230 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 231 | .WillOnce(Return(m_error_failed_auth)); |
| 232 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 233 | .WillOnce(Return(m_error_failed_auth)); |
| 234 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 235 | |
| 236 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 237 | .WillOnce(Return(Connection_type::Tcp)); |
| 238 | |
| 239 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 240 | } |
| 241 | |
| 242 | TEST_F(Auth_chaining_test_suite, auto_auth_method) { |
| 243 | set_ssl_state(true); |
| 244 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 245 | "AUTO"); |
| 246 | |
| 247 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 248 | .WillOnce(Return(m_error_failed_auth)); |
| 249 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 250 | .WillOnce(Return(m_error_failed_auth)); |
| 251 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 252 | .WillOnce(Return(m_error_failed_auth)); |
| 253 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 254 | .WillOnce(Return(Connection_type::Tcp)); |
| 255 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 256 | |
| 257 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 258 | } |
| 259 | |
| 260 | TEST_F(Auth_chaining_test_suite, auto_auth_method_ssl_disabled) { |
| 261 | set_ssl_state(false); |
| 262 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 263 | "AUTO"); |
| 264 | |
| 265 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 266 | .WillOnce(Return(m_error_failed_auth)); |
| 267 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 268 | .WillOnce(Return(m_error_failed_auth)); |
| 269 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 270 | .WillOnce(Return(Connection_type::Tcp)); |
| 271 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 272 | |
| 273 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 274 | } |
| 275 | |
| 276 | TEST_F(Auth_chaining_test_suite, auto_auth_method_unix_socket_connection) { |
| 277 | set_ssl_state(false); |
| 278 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 279 | .WillOnce(Return(Connection_type::Unix_socket)); |
| 280 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 281 | "AUTO"); |
| 282 | |
| 283 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 284 | .WillOnce(Return(m_error_failed_auth)); |
| 285 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 286 | .WillOnce(Return(m_error_failed_auth)); |
| 287 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 288 | .WillOnce(Return(m_error_failed_auth)); |
| 289 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 290 | |
| 291 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 292 | } |
| 293 | |
| 294 | TEST_F(Auth_chaining_test_suite, ambigous_auth_method) { |
| 295 | set_ssl_state(false); |
| 296 | ASSERT_EQ( |
| 297 | CR_X_UNSUPPORTED_OPTION_VALUE, |
| 298 | m_sut |
| 299 | ->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 300 | std::vector<std::string>{"AUTO", "PLAIN"}) |
| 301 | .error()); |
| 302 | |
| 303 | // Default value is not changed |
| 304 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 305 | .WillOnce(Return(m_error_failed_auth)); |
| 306 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 307 | .WillOnce(Return(m_error_failed_auth)); |
| 308 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 309 | .WillOnce(Return(Connection_type::Tcp)); |
| 310 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 311 | |
| 312 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 313 | } |
| 314 | |
| 315 | TEST_F(Auth_chaining_test_suite, ambigous_auth_method_multiple_auto) { |
| 316 | set_ssl_state(false); |
| 317 | ASSERT_EQ( |
| 318 | CR_X_UNSUPPORTED_OPTION_VALUE, |
| 319 | m_sut |
| 320 | ->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 321 | std::vector<std::string>{"AUTO", "AUTO"}) |
| 322 | .error()); |
| 323 | |
| 324 | // Default value is not changed |
| 325 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 326 | .WillOnce(Return(m_error_failed_auth)); |
| 327 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 328 | .WillOnce(Return(m_error_failed_auth)); |
| 329 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 330 | .WillOnce(Return(Connection_type::Tcp)); |
| 331 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 332 | |
| 333 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 334 | } |
| 335 | |
| 336 | TEST_F(Auth_chaining_test_suite, |
| 337 | auto_auth_method_in_compatibility_mode_with_ssl_disabled) { |
| 338 | set_ssl_state(false); |
| 339 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 340 | std::vector<std::string>{"FALLBACK"}); |
| 341 | |
| 342 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 343 | .WillOnce(Return(m_error_failed_auth)); |
| 344 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 345 | .WillOnce(Return(m_error_failed_auth)); |
| 346 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 347 | .WillOnce(Return(Connection_type::Tcp)); |
| 348 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 349 | |
| 350 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 351 | } |
| 352 | |
| 353 | TEST_F(Auth_chaining_test_suite, |
| 354 | auto_auth_method_in_compatibility_mode_with_ssl_enabled) { |
| 355 | set_ssl_state(true); |
| 356 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 357 | std::vector<std::string>{"FALLBACK"}); |
| 358 | |
| 359 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 360 | .WillOnce(Return(m_error_failed_auth)); |
| 361 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 362 | .WillOnce(Return(m_error_failed_auth)); |
| 363 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 364 | .WillOnce(Return(Connection_type::Tcp)); |
| 365 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 366 | |
| 367 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 368 | } |
| 369 | |
| 370 | TEST_F(Auth_chaining_test_suite, |
| 371 | auto_auth_method_in_compatibility_mode_using_unix_socket) { |
| 372 | set_ssl_state(false); |
| 373 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 374 | std::vector<std::string>{"FALLBACK"}); |
| 375 | |
| 376 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 377 | .WillOnce(Return(m_error_failed_auth)); |
| 378 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 379 | .WillOnce(Return(m_error_failed_auth)); |
| 380 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 381 | .WillOnce(Return(Connection_type::Unix_socket)); |
| 382 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 383 | |
| 384 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 385 | } |
| 386 | |
| 387 | TEST_F(Auth_chaining_test_suite, only_wrong_auth_method) { |
| 388 | set_ssl_state(true); |
| 389 | // Wrong method given will mean that we do not use user provided auth and |
| 390 | // resort to Auth::AUTO |
| 391 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 392 | std::vector<std::string>{"FOOBAR_AUTH"}); |
| 393 | |
| 394 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 395 | .WillOnce(Return(m_error_failed_auth)); |
| 396 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 397 | .WillOnce(Return(m_error_failed_auth)); |
| 398 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 399 | .WillOnce(Return(m_error_failed_auth)); |
| 400 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 401 | .WillOnce(Return(Connection_type::Tcp)); |
| 402 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 403 | |
| 404 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 405 | } |
| 406 | |
| 407 | TEST_F(Auth_chaining_test_suite, wrong_and_good_auth_method) { |
| 408 | set_ssl_state(true); |
| 409 | // Wrong method given will mean that we do not use user provided auth and |
| 410 | // resort to Auth::AUTO |
| 411 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 412 | std::vector<std::string>{"FOOBAR_AUTH", "MYSQL41"}); |
| 413 | |
| 414 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 415 | .WillOnce(Return(m_error_failed_auth)); |
| 416 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 417 | .WillOnce(Return(m_error_failed_auth)); |
| 418 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 419 | .WillOnce(Return(m_error_failed_auth)); |
| 420 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 421 | .WillOnce(Return(Connection_type::Tcp)); |
| 422 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 423 | |
| 424 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 425 | } |
| 426 | |
| 427 | TEST_F(Auth_chaining_test_suite, only_sha256_memory_auth_method_ssl_disabled) { |
| 428 | set_ssl_state(false); |
| 429 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 430 | std::vector<std::string>{"SHA256_MEMORY"}); |
| 431 | |
| 432 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 433 | .WillOnce(Return(m_error_failed_auth)); |
| 434 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 435 | .WillOnce(Return(Connection_type::Tcp)); |
| 436 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 437 | |
| 438 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 439 | } |
| 440 | |
| 441 | TEST_F(Auth_chaining_test_suite, only_sha256_memory_auth_method_ssl_enabled) { |
| 442 | set_ssl_state(true); |
| 443 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 444 | std::vector<std::string>{"SHA256_MEMORY"}); |
| 445 | |
| 446 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 447 | .WillOnce(Return(m_error_failed_auth)); |
| 448 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 449 | .WillOnce(Return(Connection_type::Tcp)); |
| 450 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 451 | |
| 452 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 453 | } |
| 454 | |
| 455 | TEST_F(Auth_chaining_test_suite, only_mysql41_auth_method_ssl_disabled) { |
| 456 | set_ssl_state(false); |
| 457 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 458 | std::vector<std::string>{"MYSQL41"}); |
| 459 | |
| 460 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 461 | .WillOnce(Return(m_error_failed_auth)); |
| 462 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 463 | .WillOnce(Return(Connection_type::Tcp)); |
| 464 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 465 | |
| 466 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 467 | } |
| 468 | |
| 469 | TEST_F(Auth_chaining_test_suite, only_mysql41_auth_method_ssl_enabled) { |
| 470 | set_ssl_state(true); |
| 471 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 472 | std::vector<std::string>{"MYSQL41"}); |
| 473 | |
| 474 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 475 | .WillOnce(Return(m_error_failed_auth)); |
| 476 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 477 | .WillOnce(Return(Connection_type::Tcp)); |
| 478 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 479 | |
| 480 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 481 | } |
| 482 | |
| 483 | TEST_F(Auth_chaining_test_suite, only_plain_method_ssl_disabled) { |
| 484 | set_ssl_state(false); |
| 485 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 486 | std::vector<std::string>{"PLAIN"}); |
| 487 | |
| 488 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 489 | .Times(0); |
| 490 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 491 | .WillOnce(Return(Connection_type::Tcp)); |
| 492 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 493 | |
| 494 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 495 | } |
| 496 | |
| 497 | TEST_F(Auth_chaining_test_suite, only_plain_method_ssl_enabled) { |
| 498 | set_ssl_state(true); |
| 499 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 500 | std::vector<std::string>{"PLAIN"}); |
| 501 | |
| 502 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 503 | .WillOnce(Return(m_error_failed_auth)); |
| 504 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 505 | .WillOnce(Return(Connection_type::Tcp)); |
| 506 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 507 | |
| 508 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 509 | } |
| 510 | |
| 511 | TEST_F(Auth_chaining_test_suite, custom_sequence_of_two_auths) { |
| 512 | set_ssl_state(true); |
| 513 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 514 | std::vector<std::string>{"PLAIN", "MYSQL41"}); |
| 515 | |
| 516 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 517 | .WillOnce(Return(m_error_failed_auth)); |
| 518 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 519 | .WillOnce(Return(m_error_failed_auth)); |
| 520 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 521 | .WillOnce(Return(Connection_type::Tcp)); |
| 522 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 523 | |
| 524 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 525 | } |
| 526 | |
| 527 | TEST_F(Auth_chaining_test_suite, custom_sequence_of_three_auths) { |
| 528 | set_ssl_state(true); |
| 529 | m_sut->set_mysql_option( |
| 530 | XSession::Mysqlx_option::Authentication_method, |
| 531 | std::vector<std::string>{"PLAIN", "SHA256_MEMORY", "MYSQL41"}); |
| 532 | |
| 533 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 534 | .WillOnce(Return(m_error_failed_auth)); |
| 535 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 536 | .WillOnce(Return(m_error_failed_auth)); |
| 537 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 538 | .WillOnce(Return(m_error_failed_auth)); |
| 539 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 540 | .WillOnce(Return(Connection_type::Tcp)); |
| 541 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 542 | |
| 543 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 544 | } |
| 545 | |
| 546 | TEST_F(Auth_chaining_test_suite, |
| 547 | custom_sequence_of_three_auths_ignore_con_error_after_fatal) { |
| 548 | set_ssl_state(true); |
| 549 | m_sut->set_mysql_option( |
| 550 | XSession::Mysqlx_option::Authentication_method, |
| 551 | std::vector<std::string>{"PLAIN", "SHA256_MEMORY", "MYSQL41"}); |
| 552 | |
| 553 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 554 | .WillOnce(Return(m_error_failed_auth)); |
| 555 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 556 | .WillOnce(Return(m_error_fatal_failed_auth)); |
| 557 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 558 | .WillOnce(Return(m_error_disconnected)); |
| 559 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 560 | .WillOnce(Return(Connection_type::Tcp)); |
| 561 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 562 | |
| 563 | const auto error = m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 564 | ASSERT_EQ(ER_ACCESS_DENIED_ERROR, error.error()); |
| 565 | } |
| 566 | |
| 567 | TEST_F( |
| 568 | Auth_chaining_test_suite, |
| 569 | custom_sequence_of_three_auths_returns_con_error_when_there_was_no_fatal) { |
| 570 | set_ssl_state(true); |
| 571 | m_sut->set_mysql_option( |
| 572 | XSession::Mysqlx_option::Authentication_method, |
| 573 | std::vector<std::string>{"PLAIN", "SHA256_MEMORY", "MYSQL41"}); |
| 574 | |
| 575 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 576 | .WillOnce(Return(m_error_failed_auth)); |
| 577 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 578 | .WillOnce(Return(m_error_failed_auth)); |
| 579 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 580 | .WillOnce(Return(m_error_disconnected)); |
| 581 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 582 | .WillOnce(Return(Connection_type::Tcp)); |
| 583 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 584 | |
| 585 | const auto error = m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 586 | ASSERT_EQ(CR_SERVER_GONE_ERROR, error.error()); |
| 587 | } |
| 588 | |
| 589 | TEST_F(Auth_chaining_test_suite, duplicate_auth_methods) { |
| 590 | set_ssl_state(true); |
| 591 | // Duplicate auth methods are not an error |
| 592 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 593 | std::vector<std::string>{"MYSQL41", "MYSQL41"}); |
| 594 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 595 | .Times(2) |
| 596 | .WillRepeatedly(Return(m_error_failed_auth)); |
| 597 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 598 | .WillOnce(Return(Connection_type::Tcp)); |
| 599 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 600 | |
| 601 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 602 | } |
| 603 | |
| 604 | TEST_F(Auth_chaining_test_suite, sequence_with_plain_and_no_ssl) { |
| 605 | set_ssl_state(false); |
| 606 | |
| 607 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 608 | std::vector<std::string>{"MYSQL41", "PLAIN"}); |
| 609 | |
| 610 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 611 | .WillOnce(Return(m_error_failed_auth)); |
| 612 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 613 | .Times(0); |
| 614 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 615 | .WillOnce(Return(Connection_type::Tcp)); |
| 616 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 617 | |
| 618 | ASSERT_EQ(CR_X_INVALID_AUTH_METHOD, |
| 619 | m_sut->connect("host", 1290, "user", "pass", "schema").error()); |
| 620 | } |
| 621 | |
| 622 | TEST_F(Auth_chaining_test_suite, sequence_successfull_auth_attempt) { |
| 623 | set_ssl_state(true); |
| 624 | m_sut->set_mysql_option( |
| 625 | XSession::Mysqlx_option::Authentication_method, |
| 626 | std::vector<std::string>{"MYSQL41", "PLAIN", "SHA256_MEMORY"}); |
| 627 | |
| 628 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 629 | .WillOnce(Return(m_error_failed_auth)); |
| 630 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 631 | .WillOnce(Return(XError{})); |
| 632 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 633 | .WillOnce(Return(Connection_type::Tcp)); |
| 634 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 635 | |
| 636 | m_sut->connect("host", 1290, "user", "pass", "schema"); |
| 637 | } |
| 638 | |
| 639 | TEST_P(Auth_chaining_test_suite, custom_error_handling) { |
| 640 | set_ssl_state(true); |
| 641 | auto custom_error_code = 1; |
| 642 | ASSERT_EQ(GetParam().size(), 2); |
| 643 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 644 | GetParam()); |
| 645 | |
| 646 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, GetParam()[0])) |
| 647 | .WillOnce(Return( |
| 648 | XError{custom_error_code, "Error other than invalid user/password"})); |
| 649 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, GetParam()[1])) |
| 650 | .WillOnce(Return(m_error_failed_auth)); |
| 651 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 652 | .WillOnce(Return(Connection_type::Tcp)); |
| 653 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 654 | |
| 655 | ASSERT_EQ(custom_error_code, |
| 656 | m_sut->connect("host", 1290, "user", "pass", "schema").error()); |
| 657 | } |
| 658 | |
| 659 | INSTANTIATE_TEST_SUITE_P( |
| 660 | Instantiation_custom_error_handling, Auth_chaining_test_suite, |
| 661 | ::testing::Values(std::vector<std::string>{"PLAIN", "MYSQL41"}, |
| 662 | std::vector<std::string>{"MYSQL41", "PLAIN"}, |
| 663 | std::vector<std::string>{"SHA256_MEMORY", "MYSQL41"})); |
| 664 | |
| 665 | class Auth_chaining_test_suite_sha256_fail_no_ssl |
| 666 | : public Auth_chaining_test_suite {}; |
| 667 | |
| 668 | TEST_P(Auth_chaining_test_suite_sha256_fail_no_ssl, sha256_fail_with_no_ssl) { |
| 669 | set_ssl_state(false); |
| 670 | |
| 671 | m_sut->set_mysql_option(XSession::Mysqlx_option::Authentication_method, |
| 672 | GetParam()); |
| 673 | |
| 674 | for (const auto &auth : GetParam()) |
| 675 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, auth)) |
| 676 | .WillOnce(Return(m_error_failed_auth)); |
| 677 | |
| 678 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 679 | .WillOnce(Return(Connection_type::Tcp)); |
| 680 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 681 | |
| 682 | ASSERT_EQ(CR_X_AUTH_PLUGIN_ERROR, |
| 683 | m_sut->connect("host", 1290, "user", "pass", "schema").error()); |
| 684 | } |
| 685 | |
| 686 | INSTANTIATE_TEST_SUITE_P( |
| 687 | Instantiation_sha256_fail, Auth_chaining_test_suite_sha256_fail_no_ssl, |
| 688 | ::testing::Values(std::vector<std::string>{"SHA256_MEMORY"}, |
| 689 | std::vector<std::string>{"MYSQL41", "SHA256_MEMORY"}, |
| 690 | std::vector<std::string>{"SHA256_MEMORY", "MYSQL41"})); |
| 691 | |
| 692 | class Auth_chaining_fatal_errors |
| 693 | : public Auth_chaining_test_suite_base, |
| 694 | public ::testing::WithParamInterface<xcl::XError> {}; |
| 695 | |
| 696 | TEST_P(Auth_chaining_fatal_errors, break_chain_on_fatal_errors) { |
| 697 | set_ssl_state(true); |
| 698 | m_sut->set_mysql_option( |
| 699 | XSession::Mysqlx_option::Authentication_method, |
| 700 | std::vector<std::string>{"PLAIN", "SHA256_MEMORY", "MYSQL41"}); |
| 701 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "PLAIN")) |
| 702 | .WillOnce(Return(GetParam())); |
| 703 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "SHA256_MEMORY")) |
| 704 | .Times(0); |
| 705 | EXPECT_CALL(*m_mock_protocol, execute_authenticate(_, _, _, "MYSQL41")) |
| 706 | .Times(0); |
| 707 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 708 | .WillOnce(Return(Connection_type::Tcp)); |
| 709 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 710 | EXPECT_CALL(*m_mock_protocol, reset_buffering()); |
| 711 | |
| 712 | ASSERT_EQ(GetParam().error(), |
| 713 | m_sut->connect("host", 1290, "user", "pass", "schema").error()); |
| 714 | } |
| 715 | |
| 716 | INSTANTIATE_TEST_SUITE_P(Instantiation_auth_chaining_fatal_errors, |
| 717 | Auth_chaining_fatal_errors, |
| 718 | ::testing::Values(xcl::XError{CR_X_READ_TIMEOUT, ""}, |
| 719 | xcl::XError{CR_X_WRITE_TIMEOUT, ""}, |
| 720 | xcl::XError{CR_SERVER_GONE_ERROR, |
| 721 | ""})); |
| 722 | |
| 723 | } // namespace test |
| 724 | } // namespace xcl |