| 1 | /* Copyright (c) 2020, 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 <memory> |
| 27 | |
| 28 | #include "unittest/gunit/xplugin/xpl/mock/component_services.h" |
| 29 | #include "unittest/gunit/xplugin/xpl/mock/srv_session_services.h" |
| 30 | |
| 31 | #include "plugin/x/src/sql_data_context.h" |
| 32 | |
| 33 | namespace xpl { |
| 34 | namespace test { |
| 35 | |
| 36 | using ::testing::_; |
| 37 | using ::testing::DoAll; |
| 38 | using ::testing::InSequence; |
| 39 | using ::testing::Return; |
| 40 | using ::testing::SetArgPointee; |
| 41 | using ::testing::StrEq; |
| 42 | using ::testing::StrictMock; |
| 43 | |
| 44 | class Sql_data_context_test_suite : public ::testing::Test { |
| 45 | public: |
| 46 | StrictMock<mock::Mysql_plugin_registry> m_mock_plugin_registry; |
| 47 | StrictMock<mock::Service_registry> m_mock_registry; |
| 48 | StrictMock<mock::Service_admin_session> m_mock_admin_session; |
| 49 | StrictMock<mock::Srv_session> m_mock_srv_session; |
| 50 | StrictMock<mock::Srv_session_info> m_mock_srv_session_info; |
| 51 | std::unique_ptr<xpl::Sql_data_context> m_sut{new xpl::Sql_data_context()}; |
| 52 | }; |
| 53 | |
| 54 | TEST_F(Sql_data_context_test_suite, create_object_which_does_nothing) {} |
| 55 | |
| 56 | TEST_F(Sql_data_context_test_suite, initialize_admin_session_and_fail) { |
| 57 | const bool k_request_admin_session = true; |
| 58 | const mysql_service_status_t k_failure = 1; |
| 59 | const mysql_service_status_t k_ok = 0; |
| 60 | |
| 61 | InSequence forceSequenceOfFollowingExpects; |
| 62 | EXPECT_CALL(m_mock_plugin_registry, mysql_plugin_registry_acquire()) |
| 63 | .WillOnce(Return(m_mock_registry.get())); |
| 64 | EXPECT_CALL(m_mock_registry, acquire(StrEq("mysql_admin_session"), _)) |
| 65 | .WillOnce(Return(k_failure)); |
| 66 | EXPECT_CALL(m_mock_plugin_registry, |
| 67 | mysql_plugin_registry_release(m_mock_registry.get())) |
| 68 | .WillOnce(Return(k_ok)); |
| 69 | |
| 70 | // Call the Object-under-test |
| 71 | ASSERT_TRUE(m_sut->init(k_request_admin_session)); |
| 72 | } |
| 73 | |
| 74 | TEST_F(Sql_data_context_test_suite, initialize_admin_session_and_release) { |
| 75 | const bool k_request_admin_session = true; |
| 76 | const mysql_service_status_t k_ok = 0; |
| 77 | const MYSQL_SESSION k_session = reinterpret_cast<MYSQL_SESSION>(10); |
| 78 | |
| 79 | const auto admin_service = |
| 80 | reinterpret_cast<my_h_service>(m_mock_admin_session.get()); |
| 81 | |
| 82 | EXPECT_CALL(m_mock_srv_session_info, get_session_id(k_session)) |
| 83 | .WillRepeatedly(Return(0)); |
| 84 | |
| 85 | InSequence forceSequenceOfFollowingExpects; |
| 86 | EXPECT_CALL(m_mock_plugin_registry, mysql_plugin_registry_acquire()) |
| 87 | .WillOnce(Return(m_mock_registry.get())); |
| 88 | EXPECT_CALL(m_mock_registry, acquire(StrEq("mysql_admin_session"), _)) |
| 89 | .WillOnce(DoAll(SetArgPointee<1>(admin_service), Return(k_ok))); |
| 90 | |
| 91 | EXPECT_CALL(m_mock_admin_session, open(_, _)).WillOnce(Return(k_session)); |
| 92 | |
| 93 | EXPECT_CALL(m_mock_registry, release(admin_service)); |
| 94 | EXPECT_CALL(m_mock_plugin_registry, |
| 95 | mysql_plugin_registry_release(m_mock_registry.get())) |
| 96 | .WillOnce(Return(k_ok)); |
| 97 | |
| 98 | EXPECT_CALL(m_mock_srv_session, close_session(k_session)).WillOnce(Return(0)); |
| 99 | // Call the Object-under-test |
| 100 | ASSERT_FALSE(m_sut->init(k_request_admin_session)); |
| 101 | } |
| 102 | |
| 103 | } // namespace test |
| 104 | } // namespace xpl |