| 1 | /* Copyright (c) 2018, 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 "plugin/x/src/interface/listener_factory.h" |
| 29 | #include "plugin/x/src/ngs/socket_acceptors_task.h" |
| 30 | #include "plugin/x/src/variables/system_variables.h" |
| 31 | #include "unittest/gunit/xplugin/xpl/mock/listener_factory.h" |
| 32 | #include "unittest/gunit/xplugin/xpl/mock/service_sys_variables.h" |
| 33 | #include "unittest/gunit/xplugin/xpl/mock/socket_events.h" |
| 34 | |
| 35 | namespace xpl { |
| 36 | namespace test { |
| 37 | |
| 38 | using ::testing::_; |
| 39 | using ::testing::DoAll; |
| 40 | using ::testing::Ref; |
| 41 | using ::testing::Return; |
| 42 | using ::testing::SetArgPointee; |
| 43 | using ::testing::StrEq; |
| 44 | |
| 45 | const uint32_t k_backlog = 10; |
| 46 | const char *k_unix_file = "unix test"; |
| 47 | const char *k_host = "host test"; |
| 48 | const char *k_net_ns = ""; |
| 49 | const uint16_t k_port = 11; |
| 50 | const uint32_t k_open_timeout = 12; |
| 51 | |
| 52 | class Socket_acceptors_task_suite : public ::testing::Test { |
| 53 | public: |
| 54 | void SetUp() override { |
| 55 | xpl::Plugin_system_variables::initialize(&m_mock_sys_vars, {}, {}, false); |
| 56 | } |
| 57 | |
| 58 | void TearDown() override { xpl::Plugin_system_variables::cleanup(); } |
| 59 | |
| 60 | mock::Listener_factory m_mock_factory; |
| 61 | mock::Service_sys_variables m_mock_sys_vars; |
| 62 | std::shared_ptr<mock::Socket_events> m_mock_event{new mock::Socket_events()}; |
| 63 | }; |
| 64 | |
| 65 | TEST_F(Socket_acceptors_task_suite, prepare_without_any_interface) { |
| 66 | std::string skip_net = "NO"; |
| 67 | std::string socket = "some socket"; |
| 68 | EXPECT_CALL(m_mock_sys_vars, get_variable(_, StrEq("skip_networking"), _, _)) |
| 69 | .WillOnce(DoAll(SetArgPointee<2>(&skip_net[0]), Return(true))); |
| 70 | EXPECT_CALL(m_mock_sys_vars, get_variable(_, StrEq("socket"), _, _)) |
| 71 | .WillOnce(DoAll(SetArgPointee<2>(&socket[0]), Return(true))); |
| 72 | EXPECT_CALL(m_mock_factory, |
| 73 | create_tcp_socket_listener_ptr( |
| 74 | StrEq(k_host), _, k_port, k_open_timeout, |
| 75 | Ref(*(iface::Socket_events *)m_mock_event.get()), k_backlog)); |
| 76 | |
| 77 | #if defined(HAVE_SYS_UN_H) |
| 78 | EXPECT_CALL(m_mock_factory, |
| 79 | create_unix_socket_listener_ptr( |
| 80 | StrEq(k_unix_file), |
| 81 | Ref(*(iface::Socket_events *)m_mock_event.get()), k_backlog)); |
| 82 | #endif |
| 83 | |
| 84 | ngs::Socket_acceptors_task sut(m_mock_factory, k_host, k_port, k_open_timeout, |
| 85 | k_unix_file, k_backlog, m_mock_event); |
| 86 | |
| 87 | iface::Server_task::Task_context context; |
| 88 | ASSERT_FALSE(sut.prepare(&context)); |
| 89 | } |
| 90 | |
| 91 | } // namespace test |
| 92 | } // namespace xpl |