| 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 "unittest/gunit/xplugin/xcl/session_t.h" |
| 27 | |
| 28 | namespace xcl { |
| 29 | namespace test { |
| 30 | |
| 31 | TEST_F(Xcl_session_impl_tests, get_protocol_returns_mock) { |
| 32 | ASSERT_EQ(m_mock_protocol, &m_sut->get_protocol()); |
| 33 | } |
| 34 | |
| 35 | TEST_F(Xcl_session_impl_tests, get_client_id) { |
| 36 | const bool local_notice = false; |
| 37 | const XProtocol::Client_id expected_client_id = 1220; |
| 38 | std::string payload; |
| 39 | |
| 40 | encode_session_state_change( |
| 41 | Mysqlx::Notice::SessionStateChanged::CLIENT_ID_ASSIGNED, |
| 42 | expected_client_id, &payload); |
| 43 | |
| 44 | /* Connection was not started, session_id must |
| 45 | be not initialized. */ |
| 46 | ASSERT_EQ(XCL_CLIENT_ID_NOT_VALID, m_sut->client_id()); |
| 47 | |
| 48 | /* Update the session_id */ |
| 49 | ASSERT_EQ(Handler_result::Consumed, |
| 50 | m_out_message_handler(m_mock_protocol, local_notice, |
| 51 | Mysqlx::Notice::Frame::SESSION_STATE_CHANGED, |
| 52 | payload.c_str(), |
| 53 | static_cast<uint32_t>(payload.length()))); |
| 54 | |
| 55 | /* Verify that the session object reports the new ID */ |
| 56 | ASSERT_EQ(expected_client_id, m_sut->client_id()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(Xcl_session_impl_tests, close_not_connected_session) { |
| 60 | m_sut->close(); |
| 61 | ASSERT_TRUE(m_sut->is_protocol()); |
| 62 | } |
| 63 | |
| 64 | TEST_F(Xcl_session_impl_tests, close_connected_session) { |
| 65 | EXPECT_CALL(m_mock_connection_state, is_connected()).WillOnce(Return(true)); |
| 66 | EXPECT_CALL(*m_mock_protocol, execute_close()); |
| 67 | m_sut->close(); |
| 68 | ASSERT_FALSE(m_sut->is_protocol()); |
| 69 | } |
| 70 | |
| 71 | TEST_F(Xcl_session_impl_tests, close_connected_session_in_dtor) { |
| 72 | EXPECT_CALL(m_mock_connection_state, is_connected()).WillOnce(Return(true)); |
| 73 | EXPECT_CALL(m_mock_connection, close()); |
| 74 | m_sut.reset(); |
| 75 | } |
| 76 | |
| 77 | } // namespace test |
| 78 | } // namespace xcl |