| 1 | /* |
| 2 | * Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2.0, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is designed to work with certain software (including |
| 9 | * but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | * as designated in a particular file or component or in included license |
| 11 | * documentation. The authors of MySQL hereby grant you an additional |
| 12 | * permission to link the program and your derivative works with the |
| 13 | * separately licensed software that they have either included with |
| 14 | * the program or referenced in the documentation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License, version 2.0, for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <cstdint> |
| 27 | |
| 28 | #include "errmsg.h" // NOLINT(build/include_subdir) |
| 29 | #include "my_config.h" // NOLINT(build/include_subdir) |
| 30 | #include "my_macros.h" // NOLINT(build/include_subdir) |
| 31 | #include "mysql_version.h" // NOLINT(build/include_subdir) |
| 32 | |
| 33 | #include "plugin/x/generated/mysqlx_error.h" |
| 34 | #include "plugin/x/generated/mysqlx_version.h" |
| 35 | #include "unittest/gunit/xplugin/xcl/message_helpers.h" |
| 36 | #include "unittest/gunit/xplugin/xcl/session_t.h" |
| 37 | |
| 38 | namespace xcl { |
| 39 | namespace test { |
| 40 | namespace { |
| 41 | |
| 42 | const char *expected_user = "user"; |
| 43 | const char *expected_pass = "user_pass"; |
| 44 | const char *expected_host = "host"; |
| 45 | const char *expected_socket_file = "socket_file"; |
| 46 | const char *expected_schema = "schema"; |
| 47 | const uint16_t expected_port = 1290; |
| 48 | const int expected_error_code = 10; |
| 49 | const int expected_error_code_success = 0; |
| 50 | |
| 51 | } // namespace |
| 52 | |
| 53 | using ::testing::An; |
| 54 | using ::testing::InvokeWithoutArgs; |
| 55 | |
| 56 | class Xcl_session_impl_tests_connect : public Xcl_session_impl_tests { |
| 57 | public: |
| 58 | void expect_nothing() {} |
| 59 | |
| 60 | XError assert_reauthenticate(const int error_code) { |
| 61 | EXPECT_CALL(*m_mock_protocol, send(An<const ::Mysqlx::Session::Reset &>())) |
| 62 | .WillOnce(Return(XError{})); |
| 63 | EXPECT_CALL(*m_mock_protocol, recv_ok()) |
| 64 | .WillOnce(Return(XError{error_code, ""})); |
| 65 | |
| 66 | auto result = |
| 67 | m_sut->reauthenticate(expected_user, expected_pass, expected_schema); |
| 68 | |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | XError assert_connect_to_localhost(const int error_code) { |
| 73 | EXPECT_CALL(m_mock_connection, connect_to_localhost(expected_socket_file)) |
| 74 | .WillOnce(Return(XError{error_code, ""})); |
| 75 | |
| 76 | if (0 == error_code) { |
| 77 | EXPECT_CALL( |
| 78 | *m_mock_protocol, |
| 79 | add_notice_handler(_, Handler_position::Begin, Handler_priority_low)) |
| 80 | .WillOnce(Return(4)); |
| 81 | EXPECT_CALL(*m_mock_protocol, remove_notice_handler(4)); |
| 82 | } |
| 83 | |
| 84 | return m_sut->connect(expected_socket_file, expected_user, expected_pass, |
| 85 | expected_schema); |
| 86 | } |
| 87 | |
| 88 | XError assert_connect(const int error_code) { |
| 89 | EXPECT_CALL(m_mock_connection, |
| 90 | connect(expected_host, expected_port, Internet_protocol::Any)) |
| 91 | .WillOnce(Return(XError{error_code, ""})); |
| 92 | |
| 93 | if (0 == error_code) { |
| 94 | EXPECT_CALL( |
| 95 | *m_mock_protocol, |
| 96 | add_notice_handler(_, Handler_position::Begin, Handler_priority_low)) |
| 97 | .WillOnce(Return(4)); |
| 98 | EXPECT_CALL(*m_mock_protocol, remove_notice_handler(4)); |
| 99 | } |
| 100 | |
| 101 | return m_sut->connect(expected_host, expected_port, expected_user, |
| 102 | expected_pass, expected_schema); |
| 103 | } |
| 104 | |
| 105 | void expect_protocol_any_connection() { |
| 106 | m_connection_number = 0; |
| 107 | auto count_connections = [this]() -> XError { |
| 108 | ++m_connection_number; |
| 109 | |
| 110 | return {}; |
| 111 | }; |
| 112 | |
| 113 | EXPECT_CALL(m_mock_connection, |
| 114 | connect(expected_host, expected_port, Internet_protocol::Any)) |
| 115 | .WillRepeatedly(InvokeWithoutArgs(count_connections)); |
| 116 | |
| 117 | EXPECT_CALL(m_mock_connection, connect_to_localhost(expected_socket_file)) |
| 118 | .WillRepeatedly(InvokeWithoutArgs(count_connections)); |
| 119 | } |
| 120 | |
| 121 | void expect_connection_number_to_be_one() { |
| 122 | EXPECT_EQ(1, m_connection_number); |
| 123 | } |
| 124 | |
| 125 | int m_connection_number; |
| 126 | }; |
| 127 | |
| 128 | TEST_F(Xcl_session_impl_tests_connect, reauthenticate_not_connected) { |
| 129 | auto error = |
| 130 | m_sut->reauthenticate(expected_user, expected_pass, expected_schema); |
| 131 | |
| 132 | ASSERT_EQ(CR_CONNECTION_ERROR, error.error()); |
| 133 | } |
| 134 | |
| 135 | TEST_F(Xcl_session_impl_tests_connect, reauthenticate_send_reset_failed) { |
| 136 | const int expected_error_code = 10; |
| 137 | |
| 138 | // Old and new object (sut_) share mocks. Object in sut_ must |
| 139 | // be released first to not interfere with new mocks expectations |
| 140 | m_sut.reset(); |
| 141 | m_sut.reset(make_sut(true).release()); |
| 142 | |
| 143 | EXPECT_CALL(*m_mock_protocol, send(An<const ::Mysqlx::Session::Reset &>())) |
| 144 | .WillOnce(Return(XError{expected_error_code, ""})); |
| 145 | auto error = |
| 146 | m_sut->reauthenticate(expected_user, expected_pass, expected_schema); |
| 147 | |
| 148 | ASSERT_EQ(expected_error_code, error.error()); |
| 149 | |
| 150 | expect_connection_close(); |
| 151 | } |
| 152 | |
| 153 | TEST_F(Xcl_session_impl_tests_connect, connection_tcp_already_connected) { |
| 154 | // Old and new object (sut_) share mocks. Object in sut_ must |
| 155 | // be released first to not interfere with new mocks expectations |
| 156 | m_sut.reset(); |
| 157 | m_sut.reset(make_sut(true).release()); |
| 158 | |
| 159 | auto error = m_sut->connect(expected_host, expected_port, expected_user, |
| 160 | expected_pass, expected_schema); |
| 161 | |
| 162 | ASSERT_EQ(CR_ALREADY_CONNECTED, error.error()); |
| 163 | |
| 164 | error = m_sut->connect(expected_socket_file, expected_user, expected_pass, |
| 165 | expected_schema); |
| 166 | |
| 167 | ASSERT_EQ(CR_ALREADY_CONNECTED, error.error()); |
| 168 | |
| 169 | expect_connection_close(); |
| 170 | } |
| 171 | |
| 172 | TEST_F(Xcl_session_impl_tests_connect, connect_nullptrs) { |
| 173 | m_sut->set_mysql_option(xcl::XSession::Mysqlx_option::Authentication_method, |
| 174 | "MYSQL41"); |
| 175 | |
| 176 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 177 | .WillRepeatedly(Return(false)); |
| 178 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 179 | .WillRepeatedly(Return(false)); |
| 180 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 181 | .WillOnce(Return(Connection_type::Tcp)); |
| 182 | EXPECT_CALL(*m_mock_protocol, add_send_message_handler(_, _, _)); |
| 183 | EXPECT_CALL(*m_mock_protocol, remove_send_message_handler(0)); |
| 184 | EXPECT_CALL(*m_mock_protocol, reset_buffering()); |
| 185 | EXPECT_CALL(m_mock_connection, set_read_timeout(_)) |
| 186 | .WillRepeatedly(Return(XError{})); |
| 187 | EXPECT_CALL(m_mock_connection, set_write_timeout(_)) |
| 188 | .WillRepeatedly(Return(XError{})); |
| 189 | EXPECT_CALL(*m_mock_protocol, execute_authenticate("", "", "", "MYSQL41")) |
| 190 | .WillOnce(Return(XError{})); |
| 191 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 192 | |
| 193 | EXPECT_CALL(*m_mock_protocol, add_notice_handler(_, Handler_position::Begin, |
| 194 | Handler_priority_low)) |
| 195 | .WillOnce(Return(4)); |
| 196 | EXPECT_CALL(*m_mock_protocol, remove_notice_handler(4)); |
| 197 | |
| 198 | EXPECT_CALL(m_mock_connection, |
| 199 | connect("", MYSQLX_TCP_PORT, Internet_protocol::Any)) |
| 200 | .WillOnce(Return(XError{0, ""})); |
| 201 | |
| 202 | const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr); |
| 203 | |
| 204 | ASSERT_FALSE(error); |
| 205 | } |
| 206 | |
| 207 | TEST_F(Xcl_session_impl_tests_connect, connect_localhost_nullptrs) { |
| 208 | m_sut->set_mysql_option(xcl::XSession::Mysqlx_option::Authentication_method, |
| 209 | "MYSQL41"); |
| 210 | |
| 211 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 212 | .WillRepeatedly(Return(false)); |
| 213 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 214 | .WillRepeatedly(Return(false)); |
| 215 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 216 | .WillOnce(Return(Connection_type::Unix_socket)); |
| 217 | EXPECT_CALL(*m_mock_protocol, add_send_message_handler(_, _, _)); |
| 218 | EXPECT_CALL(*m_mock_protocol, remove_send_message_handler(0)); |
| 219 | EXPECT_CALL(*m_mock_protocol, reset_buffering()); |
| 220 | EXPECT_CALL(m_mock_connection, set_read_timeout(_)) |
| 221 | .WillRepeatedly(Return(XError{})); |
| 222 | EXPECT_CALL(m_mock_connection, set_write_timeout(_)) |
| 223 | .WillRepeatedly(Return(XError{})); |
| 224 | EXPECT_CALL(*m_mock_protocol, execute_authenticate("", "", "", "MYSQL41")) |
| 225 | .WillOnce(Return(XError{})); |
| 226 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 227 | |
| 228 | EXPECT_CALL(*m_mock_protocol, add_notice_handler(_, Handler_position::Begin, |
| 229 | Handler_priority_low)) |
| 230 | .WillOnce(Return(4)); |
| 231 | EXPECT_CALL(*m_mock_protocol, remove_notice_handler(4)); |
| 232 | |
| 233 | EXPECT_CALL(m_mock_connection, connect_to_localhost(MYSQLX_UNIX_ADDR)) |
| 234 | .WillOnce(Return(XError{0, ""})); |
| 235 | |
| 236 | const auto error = m_sut->connect(nullptr, nullptr, nullptr, nullptr); |
| 237 | |
| 238 | ASSERT_FALSE(error); |
| 239 | } |
| 240 | |
| 241 | class Xcl_session_impl_tests_connected : public Xcl_session_impl_tests_connect { |
| 242 | public: |
| 243 | void SetUp() override { m_sut = make_sut(true); } |
| 244 | }; |
| 245 | |
| 246 | using Connection_method = XError (Xcl_session_impl_tests_connect::*)(const int); |
| 247 | |
| 248 | using Closure_method = void (Xcl_session_impl_tests_connect::*)(); |
| 249 | |
| 250 | struct Open_close_methods { |
| 251 | Connection_method m_open; |
| 252 | Closure_method m_close; |
| 253 | bool m_is_connected; |
| 254 | std::string m_auth; |
| 255 | static const bool start_connected = true; |
| 256 | static const bool start_disconnected = false; |
| 257 | }; |
| 258 | |
| 259 | // clang-format off |
| 260 | #define CAPABILITIES(value) \ |
| 261 | "capabilities { capabilities { " value " } }" |
| 262 | #define CAP_BOOL(key, value) \ |
| 263 | "name: \"" key "\"" \ |
| 264 | "value { type: SCALAR scalar { type: V_BOOL v_bool: " value " } }" |
| 265 | #define CAP_OBJECT(key, value) \ |
| 266 | "name: \"" key "\"" \ |
| 267 | "value { type: OBJECT obj { " value " } }" |
| 268 | #define FLD_STRING(k, v) \ |
| 269 | "fld { key:\"" k "\"" \ |
| 270 | " value { type: SCALAR scalar { " \ |
| 271 | " type: V_STRING v_string { value: \"" v "\" } } } }" |
| 272 | #ifdef _WIN32 |
| 273 | #define FLD_WIN32_STRING(k, v) \ |
| 274 | "fld { key:\"" k "\"" \ |
| 275 | " value { type: SCALAR scalar { " \ |
| 276 | " type: V_STRING v_string { value: \"" v "\" } } } }" |
| 277 | #else |
| 278 | #define FLD_WIN32_STRING(k, v) |
| 279 | #endif // _WIN32 |
| 280 | |
| 281 | // clang-format on |
| 282 | |
| 283 | class Xcl_session_impl_tests_connect_param |
| 284 | : public Xcl_session_impl_tests_connect, |
| 285 | public WithParamInterface<Open_close_methods> { |
| 286 | public: |
| 287 | using CapabilitiesSet = ::Mysqlx::Connection::CapabilitiesSet; |
| 288 | |
| 289 | public: |
| 290 | void SetUp() override { |
| 291 | m_sut = make_sut(GetParam().m_is_connected); |
| 292 | expect_message_handler_setup(); |
| 293 | } |
| 294 | |
| 295 | void expect_message_handler_setup() { |
| 296 | EXPECT_CALL(*m_mock_protocol, add_send_message_handler(_, _, _)) |
| 297 | .WillOnce(Return(0)); |
| 298 | EXPECT_CALL(*m_mock_protocol, remove_send_message_handler(0)); |
| 299 | EXPECT_CALL(m_mock_connection, set_read_timeout(_)) |
| 300 | .WillOnce(Return(XError{0, ""})); |
| 301 | EXPECT_CALL(m_mock_connection, set_write_timeout(_)) |
| 302 | .WillOnce(Return(XError{0, ""})); |
| 303 | } |
| 304 | |
| 305 | const Message_from_str<CapabilitiesSet> m_cap_set_tls{ |
| 306 | CAPABILITIES(CAP_BOOL("tls", "1"))}; |
| 307 | |
| 308 | const Message_from_str<CapabilitiesSet> m_cap_expired{ |
| 309 | CAPABILITIES(CAP_BOOL("client.pwd_expire_ok", "1"))}; |
| 310 | |
| 311 | // clang-format off |
| 312 | const Message_from_str<CapabilitiesSet> m_cap_connect_attrs{ |
| 313 | CAPABILITIES(CAP_OBJECT("session_connect_attrs", |
| 314 | FLD_STRING("_client_name", HAVE_MYSQLX_FULL_PROTO("libmysqlxclient", |
| 315 | "libmysqlxclient_lite")) |
| 316 | FLD_STRING("_client_version", PACKAGE_VERSION) |
| 317 | FLD_STRING("_os", SYSTEM_TYPE) |
| 318 | FLD_STRING("_platform", MACHINE_TYPE) |
| 319 | FLD_STRING("_client_license", STRINGIFY_ARG(LICENSE)) |
| 320 | FLD_STRING("_pid", |
| 321 | +std::to_string(static_cast<uint64_t>( |
| 322 | IF_WIN(GetCurrentProcessId(), getpid())))+) |
| 323 | FLD_WIN32_STRING("_thread", |
| 324 | +std::to_string(static_cast<uint64_t>(GetCurrentThreadId()))+))) |
| 325 | }; |
| 326 | // clang-format on |
| 327 | }; |
| 328 | |
| 329 | class Xcl_session_impl_tests_challenge_response_connect_param |
| 330 | : public Xcl_session_impl_tests_connect_param { |
| 331 | public: |
| 332 | void SetUp() override { |
| 333 | m_sut = make_sut(GetParam().m_is_connected, GetParam().m_auth); |
| 334 | expect_message_handler_setup(); |
| 335 | } |
| 336 | }; |
| 337 | |
| 338 | class Xcl_session_impl_tests_plain_connect_param |
| 339 | : public Xcl_session_impl_tests_connect_param { |
| 340 | public: |
| 341 | void SetUp() override { |
| 342 | m_sut = make_sut(GetParam().m_is_connected, GetParam().m_auth); |
| 343 | expect_message_handler_setup(); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | TEST_P(Xcl_session_impl_tests_challenge_response_connect_param, |
| 348 | connect_challenge_response_nocaps) { |
| 349 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 350 | .WillRepeatedly(Return(false)); |
| 351 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 352 | .WillRepeatedly(Return(false)); |
| 353 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 354 | .WillOnce(Return(Connection_type::Tcp)); |
| 355 | EXPECT_CALL(*m_mock_protocol, |
| 356 | execute_authenticate(expected_user, expected_pass, |
| 357 | expected_schema, this->GetParam().m_auth)) |
| 358 | .WillOnce(Return(XError{})); |
| 359 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 360 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 361 | |
| 362 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 363 | |
| 364 | ASSERT_FALSE(error); |
| 365 | |
| 366 | (this->*GetParam().m_close)(); |
| 367 | } |
| 368 | |
| 369 | TEST_P(Xcl_session_impl_tests_challenge_response_connect_param, |
| 370 | connect_challenge_response_caps) { |
| 371 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 372 | .WillRepeatedly(Return(false)); |
| 373 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 374 | .WillRepeatedly(Return(false)); |
| 375 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 376 | .WillOnce(Return(Connection_type::Tcp)); |
| 377 | EXPECT_CALL(*m_mock_protocol, |
| 378 | execute_set_capability(Cmp_msg(m_cap_expired.get()))) |
| 379 | .WillOnce(Return(XError{})); |
| 380 | EXPECT_CALL(*m_mock_protocol, |
| 381 | execute_authenticate(expected_user, expected_pass, |
| 382 | expected_schema, this->GetParam().m_auth)) |
| 383 | .WillOnce(Return(XError{})); |
| 384 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 385 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 386 | |
| 387 | m_sut->set_capability(XSession::Capability_can_handle_expired_password, true); |
| 388 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 389 | |
| 390 | ASSERT_FALSE(error); |
| 391 | |
| 392 | (this->*GetParam().m_close)(); |
| 393 | } |
| 394 | |
| 395 | TEST_P(Xcl_session_impl_tests_challenge_response_connect_param, |
| 396 | connect_challenge_response_caps_fails) { |
| 397 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 398 | .WillRepeatedly(Return(false)); |
| 399 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 400 | .WillRepeatedly(Return(false)); |
| 401 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 402 | .WillOnce(Return(Connection_type::Tcp)); |
| 403 | EXPECT_CALL(*m_mock_protocol, |
| 404 | execute_set_capability(Cmp_msg(m_cap_expired.get()))) |
| 405 | .WillOnce(Return(XError{expected_error_code, ""})); |
| 406 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 407 | |
| 408 | m_sut->set_capability(XSession::Capability_can_handle_expired_password, true); |
| 409 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 410 | |
| 411 | ASSERT_EQ(expected_error_code, error.error()); |
| 412 | |
| 413 | (this->*GetParam().m_close)(); |
| 414 | } |
| 415 | |
| 416 | TEST_P(Xcl_session_impl_tests_challenge_response_connect_param, |
| 417 | connect_challenge_response_nocaps_auth_fail) { |
| 418 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 419 | .WillRepeatedly(Return(false)); |
| 420 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 421 | .WillRepeatedly(Return(false)); |
| 422 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 423 | .WillOnce(Return(Connection_type::Tcp)); |
| 424 | EXPECT_CALL(*m_mock_protocol, |
| 425 | execute_authenticate(expected_user, expected_pass, |
| 426 | expected_schema, this->GetParam().m_auth)) |
| 427 | .WillOnce(Return(XError{expected_error_code, ""})); |
| 428 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 429 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 430 | |
| 431 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 432 | |
| 433 | ASSERT_EQ(expected_error_code, error.error()); |
| 434 | |
| 435 | (this->*GetParam().m_close)(); |
| 436 | } |
| 437 | |
| 438 | TEST_P(Xcl_session_impl_tests_challenge_response_connect_param, |
| 439 | connect_plain_connect_attrs_caps) { |
| 440 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 441 | .WillRepeatedly(Return(false)); |
| 442 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 443 | .WillRepeatedly(Return(false)); |
| 444 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 445 | .WillOnce(Return(Connection_type::Tcp)); |
| 446 | EXPECT_CALL(*m_mock_protocol, |
| 447 | execute_set_capability(Cmp_msg(m_cap_connect_attrs.get()))) |
| 448 | .WillOnce(Return(XError{expected_error_code})); |
| 449 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 450 | |
| 451 | m_sut->set_capability(XSession::Capability_session_connect_attrs, |
| 452 | m_sut->get_connect_attrs()); |
| 453 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 454 | |
| 455 | ASSERT_EQ(expected_error_code, error.error()); |
| 456 | |
| 457 | (this->*GetParam().m_close)(); |
| 458 | } |
| 459 | |
| 460 | TEST_P(Xcl_session_impl_tests_plain_connect_param, |
| 461 | connect_plain_nocaps_when_tls_already_works) { |
| 462 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 463 | .WillRepeatedly(Return(true)); |
| 464 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 465 | .WillRepeatedly(Return(true)); |
| 466 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 467 | .WillOnce(Return(Connection_type::Tcp)); |
| 468 | EXPECT_CALL(*m_mock_protocol, |
| 469 | execute_authenticate(expected_user, expected_pass, |
| 470 | expected_schema, this->GetParam().m_auth)) |
| 471 | .WillOnce(Return(XError{})); |
| 472 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 473 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 474 | |
| 475 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 476 | |
| 477 | ASSERT_FALSE(error); |
| 478 | |
| 479 | (this->*GetParam().m_close)(); |
| 480 | } |
| 481 | |
| 482 | TEST_P(Xcl_session_impl_tests_plain_connect_param, |
| 483 | connect_plain_tls_cap_fails) { |
| 484 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 485 | .WillRepeatedly(Return(false)); |
| 486 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 487 | .WillRepeatedly(Return(true)); |
| 488 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 489 | .WillOnce(Return(Connection_type::Tcp)); |
| 490 | EXPECT_CALL(*m_mock_protocol, |
| 491 | execute_set_capability(Cmp_msg(m_cap_set_tls.get()))) |
| 492 | .WillOnce(Return(XError{expected_error_code, ""})); |
| 493 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 494 | |
| 495 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 496 | |
| 497 | ASSERT_EQ(expected_error_code, error.error()); |
| 498 | |
| 499 | (this->*GetParam().m_close)(); |
| 500 | } |
| 501 | |
| 502 | TEST_P(Xcl_session_impl_tests_plain_connect_param, |
| 503 | connect_plain_tls_activate_fails) { |
| 504 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 505 | .WillRepeatedly(Return(false)); |
| 506 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 507 | .WillRepeatedly(Return(true)); |
| 508 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 509 | .WillOnce(Return(Connection_type::Tcp)); |
| 510 | EXPECT_CALL(*m_mock_protocol, |
| 511 | execute_set_capability(Cmp_msg(m_cap_set_tls.get()))) |
| 512 | .WillOnce(Return(XError{})); |
| 513 | EXPECT_CALL(m_mock_connection, activate_tls()) |
| 514 | .WillOnce(Return(XError{expected_error_code, ""})); |
| 515 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 516 | |
| 517 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 518 | |
| 519 | ASSERT_EQ(expected_error_code, error.error()); |
| 520 | |
| 521 | (this->*GetParam().m_close)(); |
| 522 | } |
| 523 | |
| 524 | TEST_P(Xcl_session_impl_tests_plain_connect_param, connect_plain_tls) { |
| 525 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 526 | .WillOnce(Return(false)) |
| 527 | .WillRepeatedly(Return(true)); |
| 528 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 529 | .WillRepeatedly(Return(true)); |
| 530 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 531 | .WillOnce(Return(Connection_type::Tcp)); |
| 532 | EXPECT_CALL(*m_mock_protocol, |
| 533 | execute_set_capability(Cmp_msg(m_cap_set_tls.get()))) |
| 534 | .WillOnce(Return(XError{})); |
| 535 | EXPECT_CALL(m_mock_connection, activate_tls()).WillOnce(Return(XError{})); |
| 536 | EXPECT_CALL(*m_mock_protocol, |
| 537 | execute_authenticate(expected_user, expected_pass, |
| 538 | expected_schema, this->GetParam().m_auth)) |
| 539 | .WillOnce(Return(XError{})); |
| 540 | EXPECT_CALL(*m_mock_protocol, use_compression(Compression_algorithm::k_none)); |
| 541 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 542 | |
| 543 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 544 | |
| 545 | ASSERT_FALSE(error); |
| 546 | |
| 547 | (this->*GetParam().m_close)(); |
| 548 | } |
| 549 | |
| 550 | TEST_P(Xcl_session_impl_tests_plain_connect_param, |
| 551 | connect_plain_tls_caps_fail) { |
| 552 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 553 | .WillRepeatedly(Return(false)); |
| 554 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 555 | .WillRepeatedly(Return(true)); |
| 556 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 557 | .WillOnce(Return(Connection_type::Tcp)); |
| 558 | EXPECT_CALL(*m_mock_protocol, |
| 559 | execute_set_capability(Cmp_msg(m_cap_expired.get()))) |
| 560 | .WillOnce(Return(XError{expected_error_code})); |
| 561 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 562 | |
| 563 | m_sut->set_capability(XSession::Capability_can_handle_expired_password, true); |
| 564 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 565 | |
| 566 | ASSERT_EQ(expected_error_code, error.error()); |
| 567 | |
| 568 | (this->*GetParam().m_close)(); |
| 569 | } |
| 570 | |
| 571 | TEST_P(Xcl_session_impl_tests_plain_connect_param, |
| 572 | connect_plain_connect_attrs_caps) { |
| 573 | EXPECT_CALL(m_mock_connection_state, is_ssl_activated()) |
| 574 | .WillRepeatedly(Return(false)); |
| 575 | EXPECT_CALL(m_mock_connection_state, is_ssl_configured()) |
| 576 | .WillRepeatedly(Return(false)); |
| 577 | EXPECT_CALL(m_mock_connection_state, get_connection_type()) |
| 578 | .WillOnce(Return(Connection_type::Tcp)); |
| 579 | EXPECT_CALL(*m_mock_protocol, |
| 580 | execute_set_capability(Cmp_msg(m_cap_connect_attrs.get()))) |
| 581 | .WillOnce(Return(XError{expected_error_code})); |
| 582 | EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(0)); |
| 583 | |
| 584 | m_sut->set_capability(XSession::Capability_session_connect_attrs, |
| 585 | m_sut->get_connect_attrs()); |
| 586 | auto error = (this->*GetParam().m_open)(expected_error_code_success); |
| 587 | |
| 588 | ASSERT_EQ(expected_error_code, error.error()); |
| 589 | |
| 590 | (this->*GetParam().m_close)(); |
| 591 | } |
| 592 | |
| 593 | TEST_P(Xcl_session_impl_tests_connect_param, connect_fails) { |
| 594 | auto error = (this->*GetParam().m_open)(ER_X_SESSION); |
| 595 | |
| 596 | ASSERT_EQ(ER_X_SESSION, error.error()); |
| 597 | |
| 598 | (this->*GetParam().m_close)(); |
| 599 | } |
| 600 | |
| 601 | auto param_pack_builder_connect = [](const std::string &method) { |
| 602 | return Open_close_methods{ |
| 603 | &Xcl_session_impl_tests_connect_param::assert_connect, |
| 604 | &Xcl_session_impl_tests_connect_param::expect_nothing, |
| 605 | Open_close_methods::start_disconnected, method}; |
| 606 | }; |
| 607 | |
| 608 | auto param_pack_builder_connect_to_localhost = [](const std::string &method) { |
| 609 | return Open_close_methods{ |
| 610 | &Xcl_session_impl_tests_connect_param::assert_connect_to_localhost, |
| 611 | &Xcl_session_impl_tests_connect_param::expect_nothing, |
| 612 | Open_close_methods::start_disconnected, method}; |
| 613 | }; |
| 614 | |
| 615 | auto param_pack_builder_reauthenticate = [](const std::string &method) { |
| 616 | return Open_close_methods{ |
| 617 | &Xcl_session_impl_tests_connect_param::assert_reauthenticate, |
| 618 | &Xcl_session_impl_tests_connect_param::expect_connection_close, |
| 619 | Open_close_methods::start_connected, method}; |
| 620 | }; |
| 621 | |
| 622 | INSTANTIATE_TEST_SUITE_P( |
| 623 | Instantiation_connection_method, Xcl_session_impl_tests_connect_param, |
| 624 | Values(param_pack_builder_connect("PLAIN"), |
| 625 | param_pack_builder_connect_to_localhost("PLAIN"), |
| 626 | param_pack_builder_reauthenticate("PLAIN"), |
| 627 | param_pack_builder_connect("MYSQL41"), |
| 628 | param_pack_builder_connect_to_localhost("MYSQL41"), |
| 629 | param_pack_builder_reauthenticate("MYSQL41"), |
| 630 | param_pack_builder_connect("SHA256_MEMORY"), |
| 631 | param_pack_builder_connect_to_localhost("SHA256_MEMORY"), |
| 632 | param_pack_builder_reauthenticate("SHA256_MEMORY"))); |
| 633 | |
| 634 | INSTANTIATE_TEST_SUITE_P( |
| 635 | Instantiation_connection_method, Xcl_session_impl_tests_plain_connect_param, |
| 636 | Values(param_pack_builder_connect("PLAIN"), |
| 637 | param_pack_builder_connect_to_localhost("PLAIN"), |
| 638 | param_pack_builder_reauthenticate("PLAIN"))); |
| 639 | |
| 640 | INSTANTIATE_TEST_SUITE_P( |
| 641 | Instantiation_connection_method, |
| 642 | Xcl_session_impl_tests_challenge_response_connect_param, |
| 643 | Values(param_pack_builder_connect("MYSQL41"), |
| 644 | param_pack_builder_connect_to_localhost("MYSQL41"), |
| 645 | param_pack_builder_reauthenticate("MYSQL41"), |
| 646 | param_pack_builder_connect("SHA256_MEMORY"), |
| 647 | param_pack_builder_connect_to_localhost("SHA256_MEMORY"), |
| 648 | param_pack_builder_reauthenticate("SHA256_MEMORY"))); |
| 649 | |
| 650 | TEST(get_connect_attrs, not_empty_macro_definitions) { |
| 651 | ASSERT_FALSE(std::string(PACKAGE_VERSION).empty()); |
| 652 | ASSERT_FALSE(std::string(SYSTEM_TYPE).empty()); |
| 653 | ASSERT_FALSE(std::string(MACHINE_TYPE).empty()); |
| 654 | ASSERT_FALSE(std::string(STRINGIFY_ARG(LICENSE)).empty()); |
| 655 | } |
| 656 | |
| 657 | } // namespace test |
| 658 | } // namespace xcl |